Skip to content

Commit

Permalink
Paste text with CRLF as hard-breaks, paragraphs (#1746)
Browse files Browse the repository at this point in the history
* Paste text with CRLF as hard-breaks, paragraphs
#1745

* simplify PageEditor async requirements
#1745
  • Loading branch information
stevencohn authored Dec 31, 2024
1 parent 6eb5dd7 commit cd17623
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion OneMore/Commands/Edit/ConvertMarkdownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override async Task Execute(params object[] args)

foreach (var outline in outlines.ToList())
{
var content = await editor.ExtractSelectedContent(outline);
var content = editor.ExtractSelectedContent(outline);
logger.Debug("outline - - - - - - - - - - - - - - - - - - - - - -");
logger.Debug(content);
logger.Debug("/outline");
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Edit/CopyAsMarkdownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override async Task Execute(params object[] args)
#region WriteMarkdown(PageEditor editor, MarkdownWriter writer, XElement start)
async void WriteMarkdown(PageEditor editor, MarkdownWriter writer, XElement start)
{
var content = await editor.ExtractSelectedContent(start);
var content = editor.ExtractSelectedContent(start);

logger.Debug("markdown content");
logger.Debug(content);
Expand Down
58 changes: 32 additions & 26 deletions OneMore/Commands/Edit/PasteTextCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//************************************************************************************************
// Copyright © 2022 Steven M Cohn. All rights reserved.
// Copyright © 2022 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
Expand All @@ -23,44 +23,50 @@ public PasteTextCommand()

public override async Task Execute(params object[] args)
{
await using var one = new OneNote(out var page, out var ns);
var elements = page.Root.Descendants(ns + "T")
.Where(e => e.Attribute("selected")?.Value == "all");

var text = await new ClipboardProvider().GetText();
if (string.IsNullOrEmpty(text))
{
return;
}

var editor = new PageEditor(page);
var content = new XElement(ns + "T", new XCData(text));
await using var one = new OneNote(out var page, out var ns);
PageNamespace.Set(ns);

if (!elements.Any())
var elements = page.Root.Descendants(ns + "T")
.Where(e => e.Attribute("selected")?.Value == "all");

var editor = new PageEditor(page)
{
// empty page so add new content
editor.AddNextParagraph(content);
}
else if (elements.Count() > 1)
KeepSelected = false
};

if (elements.Any())
{
// selected multiple runs so replace them all
editor.ReplaceSelectedWith(content);
editor.ExtractSelectedContent();
}
else

// OneNote transforms \r\n into soft-break <br> but we want hard-breaks,
// so split text into lines...

var lines = text.Split(new string[] { "\r\n" }, System.StringSplitOptions.None);

XElement first = null;
for (var i = lines.Length - 1; i >= 0; i--)
{
var line = elements.First();
if (line.Value.Length == 0)
{
// empty cdata, unselected cursor so just insert
line.GetCData().Value = text;
}
else
{
// something is selected so replace it
editor.ReplaceSelectedWith(content);
}
var run = new XElement(ns + "T", new XCData(lines[i]));
first ??= run;

editor.InsertAtAnchor(run);
}

// position insertion cursor after last line...

page.Root.DescendantNodes().OfType<XAttribute>()
.Where(a => a.Name == "selected")
.Remove();

first?.SetAttributeValue("selected", "all");

await one.Update(page);
}
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Edit/PreviewMarkdownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override async Task Execute(params object[] args)
// collect all outlines on page, in document-order
foreach (var outline in page.BodyOutlines)
{
var content = await editor.ExtractSelectedContent(outline);
var content = editor.ExtractSelectedContent(outline);
if (content != null)
{
if (paragraphs.Count >= 1)
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Commands/Page/ExtractContentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override async Task Execute(params object[] args)
}

var editor = new PageEditor(page);
var content = await editor.ExtractSelectedContent();
var content = editor.ExtractSelectedContent();

var sectionEditor = new SectionEditor(await one.GetSection());

Expand Down
4 changes: 2 additions & 2 deletions OneMore/Commands/Snippets/InsertBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override async Task Execute(params object[] args)
cell.SetContent(MakeDefaultContent(addTitle));

var editor = new PageEditor(page);
await editor.ExtractSelectedContent();
editor.ExtractSelectedContent();

var box = new XElement(ns + "OE", table.Root);
if (editor.Anchor.Name.LocalName.In("OE", "HTMLBlock"))
Expand All @@ -137,7 +137,7 @@ public override async Task Execute(params object[] args)
KeepSelected = true
};

var content = await editor.ExtractSelectedContent();
var content = editor.ExtractSelectedContent();

if (!content.HasElements)
{
Expand Down
4 changes: 2 additions & 2 deletions OneMore/Commands/Snippets/InsertInfoBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override async Task Execute(params object[] args)
));

var editor = new PageEditor(page);
await editor.ExtractSelectedContent();
editor.ExtractSelectedContent();
anchor = editor.Anchor;
}
else
Expand All @@ -80,7 +80,7 @@ public override async Task Execute(params object[] args)
KeepSelected = true
};

content = await editor.ExtractSelectedContent();
content = editor.ExtractSelectedContent();
anchor = editor.Anchor;

editor.Deselect();
Expand Down
13 changes: 6 additions & 7 deletions OneMore/Models/PageEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace River.OneMoreAddIn.Models
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

Expand Down Expand Up @@ -637,7 +636,7 @@ public bool EditNode(XElement cursor, Func<XNode, XNode> edit)
/// Default is to process all page content.
/// </param>
/// <returns>An OEChildren XElement</returns>
public async Task<XElement> ExtractSelectedContent(XElement targetOutline = null)
public XElement ExtractSelectedContent(XElement targetOutline = null)
{
schema = new PageSchema();

Expand Down Expand Up @@ -694,7 +693,7 @@ public async Task<XElement> ExtractSelectedContent(XElement targetOutline = null
// : Anchor.Elements(ns + "OE").Select(e => e.Attribute("objectID").Value).FirstOrDefault();
//logger.WriteLine($"first anchor ({oid})", Anchor);

var snippets = await ExtractSnippets(runs);
var snippets = ExtractSnippets(runs);
RebuildContent(snippets, content);

//logger.WriteLine($"content", content);
Expand Down Expand Up @@ -760,9 +759,8 @@ private XElement WholeTableSelected(XElement cell)
}


private async Task<List<Snippet>> ExtractSnippets(List<XElement> runs)
private List<Snippet> ExtractSnippets(List<XElement> runs)
{
await using var one = new OneNote();
var tables = new List<XElement>();

var snippets = new List<Snippet>();
Expand Down Expand Up @@ -867,6 +865,7 @@ next is null &&
if (run.Name.LocalName == "Image" &&
run.Elements(ns + "CallbackID").FirstOrDefault() is XElement callback)
{
using var one = new OneNote();
var data = one.GetPageContent(page.PageId,
callback.Attribute("callbackID").Value);

Expand Down Expand Up @@ -1078,9 +1077,9 @@ private void CleanupOrphanedElements()
}


public async Task<bool> ReplaceSelectedContent(XElement replacement)
public bool ReplaceSelectedContent(XElement replacement)
{
var content = await ExtractSelectedContent();
var content = ExtractSelectedContent();

if (!content.HasElements)
{
Expand Down

0 comments on commit cd17623

Please sign in to comment.