Skip to content

Commit

Permalink
1539 keep cursor inside box snippets (stevencohn#1545)
Browse files Browse the repository at this point in the history
* keep cursor inside box snippets
stevencohn#1539

* move cursor after wrapped content
stevencohn#1539

* comment
  • Loading branch information
stevencohn authored and weissm committed Sep 7, 2024
1 parent b4c8d16 commit 9b2883b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 12 deletions.
28 changes: 25 additions & 3 deletions OneMore/Commands/Snippets/InsertBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,27 @@ public override async Task Execute(params object[] args)
cell.SetContent(MakeDefaultContent(addTitle));

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

var box = new XElement(ns + "OE", table.Root);
if (editor.Anchor.Name.LocalName.In("OE", "HTMLBlock"))
{
editor.Anchor.AddAfterSelf(box);
}
else // if (localName.In("OEChildren", "Outline"))
{
editor.Anchor.AddFirst(box);
}
}
else
{
// selection range found so move it into snippet
var editor = new PageEditor(page);
var editor = new PageEditor(page)
{
// the extracted content will be selected=all, keep it that way
KeepSelected = true
};

var content = await editor.ExtractSelectedContent();

if (!content.HasElements)
Expand All @@ -131,6 +146,9 @@ public override async Task Execute(params object[] args)
return;
}

editor.Deselect();
editor.FollowWithCurosr(content);

cell.SetContent(content);

var shading = DetermineShading(page, content);
Expand Down Expand Up @@ -235,7 +253,11 @@ private XElement MakeDefaultContent(bool withTitle)

return new XElement(ns + "OEChildren",
new Paragraph(string.Empty),
new Paragraph(Resx.phrase_YourContentHere),
new Paragraph(
new XElement(ns + "T",
new XAttribute("selected", "all"),
new XCData(Resx.phrase_YourContentHere))
),
new Paragraph(string.Empty)
);
}
Expand Down
20 changes: 15 additions & 5 deletions OneMore/Commands/Snippets/InsertInfoBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,28 @@ public override async Task Execute(params object[] args)
{
content = new XElement(ns + "OE",
new XAttribute("style", normalStyle.ToCss()),
new XElement(ns + "T", new XCData(Resx.phrase_YourContentHere)
new XElement(ns + "T",
new XAttribute("selected", "all"),
new XCData(Resx.phrase_YourContentHere)
));

var editor = new PageEditor(page);
await editor.ExtractSelectedContent();
anchor = editor.Anchor;
}
else
{
var editor = new PageEditor(page);
var editor = new PageEditor(page)
{
// the extracted content will be selected=all, keep it that way
KeepSelected = true
};

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

content.Descendants().Attributes()
.Where(a => a.Name == "selected")
.Remove();
editor.Deselect();
editor.FollowWithCurosr(content);
}

// inner table...
Expand Down
71 changes: 67 additions & 4 deletions OneMore/Models/PageEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace River.OneMoreAddIn.Models
{
using NStandard;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -104,6 +105,13 @@ public PageEditor(Page page)
public XElement Anchor { get; private set; }


/// <summary>
/// Gets or sets a Boolean indicating whether to maintain the selected state of
/// extracted content. Default is to remove selected state.
/// </summary>
public bool KeepSelected { get; set; }


/// <summary>
/// Signals EditSelected(), EditNode() and, by dependency, GetSelectedText() methods
/// that editor scanning should be done in reverse doc-order. This must be set prior
Expand Down Expand Up @@ -135,6 +143,60 @@ public void AddNextParagraph(params XElement[] content)
}


/// <summary>
/// Removes the selected attribute from the page
/// </summary>
public void Deselect(XElement root = null)
{
// clean up selected attributes; keep only select snippets

(root ?? page.Root).Descendants().Attributes()
.Where(a => a.Name == "selected")
.Remove();
}


public void FollowWithCurosr(XElement root)
{
var last = root.Descendants()
.Attributes("selected")
.Where(a => a.Value == "all")
.Select(a => a.Parent)
.LastOrDefault();

if (last is not null)
{
Deselect(root);

// Within an OE, you're allowed one image, one table, inserted file,
// or a mix of Ink and Text pieces...

if (last.Name.LocalName.In("T", "InkWord"))
{
if (last.GetCData().Value == string.Empty)
{
last.SetAttributeValue("selected", "all");
}
else
{
last.AddAfterSelf(new XElement(ns + "T",
new XAttribute("selected", "all"),
new XCData(string.Empty))
);
}
}
else
{
last.AddAfterSelf(new XElement(ns + "OE",
new XElement(ns + "T",
new XAttribute("selected", "all"),
new XCData(string.Empty))
));
}
}
}


/// <summary>
/// Gets the currently selected text. If the text cursor is positioned over a word but
/// with zero selection length then that word is returned; othewise, text from the selected
Expand Down Expand Up @@ -1015,10 +1077,11 @@ private void CleanupOrphanedElements()
//logger.WriteLine($"cleaning ~~> {(items.Any() ? items.Count() : 0)} OEChildren");
items.Remove();

// clean up selected attributes; keep only select snippets
page.Root.DescendantNodes().OfType<XAttribute>()
.Where(a => a.Name.LocalName == "selected")
.Remove();
if (!KeepSelected)
{
// clean up selected attributes; keep only select snippets
Deselect();
}

// patch any empty cells, cheap but effective!
foreach (var item in page.Root.Descendants(ns + "Cell")
Expand Down

0 comments on commit 9b2883b

Please sign in to comment.