Skip to content

Commit

Permalink
Add HashtaggerDialog to cherry-pick tags for insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Jul 20, 2024
1 parent b6014ce commit f55fe42
Show file tree
Hide file tree
Showing 22 changed files with 4,510 additions and 63 deletions.
8 changes: 7 additions & 1 deletion OneMore/AddInCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ public async Task GotoFavoriteCmd(IRibbonControl control)
=> await factory.Run<GotoFavoriteCommand>(control.Tag); //tag=pageid


[Command("ribHashtaggerButton_Label", Keys.Alt | Keys.T, "ribSearchMenu")]
public async Task HashtaggerCmd(IRibbonControl control)
=> await factory.Run<HashtaggerCommand>(1);



[Command("ribHighlightButton_Label", Keys.Control | Keys.Shift | Keys.H, "ribEditMenu")]
public async Task HighlightCmd(IRibbonControl control)
=> await factory.Run<HighlightCommand>(1);
Expand Down Expand Up @@ -923,7 +929,7 @@ public async Task TaggedCmd(IRibbonControl control)
=> await factory.Run<TaggedCommand>();


[Command("ribTaggingButton_Label", Keys.Alt | Keys.T, "ribSearchMenu")]
[Command("ribTaggingButton_Label", Keys.None, "ribSearchMenu")]
public async Task TaggingCmd(IRibbonControl control)
=> await factory.Run<TaggingCommand>();

Expand Down
55 changes: 4 additions & 51 deletions OneMore/Commands/Snippets/InsertDateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

internal class InsertDateCommand : Command
{
Expand All @@ -19,59 +18,13 @@ public InsertDateCommand()

public override async Task Execute(params object[] args)
{
await using var one = new OneNote(out var page, out var ns);
await using var one = new OneNote(out var page, out _);

var includeTime = (bool)args[0];
var text = DateTime.Now.ToString(includeTime ? "yyy-MM-dd hh:mm tt" : "yyy-MM-dd");
var content = new XElement(ns + "T", new XCData(text));

var cursor = page.GetTextCursor(allowPageTitle: true);

if (page.SelectionScope == SelectionScope.Region || page.SelectionSpecial)
{
// replace region or hyperlink/MathML
page.ReplaceSelectedWithContent(content);
}
else if (cursor == null) // && page.SelectionScope == SelectionScope.Empty)
{
// can't find cursor to append to page
page.AddNextParagraph(content);
}
else
{
var line = page.Root .Descendants(ns + "T")
.FirstOrDefault(e =>
e.Attributes().Any(a => a.Name == "selected" && a.Value == "all"));

if (line is null)
{
// this case should not happen; should be handled above
page.AddNextParagraph(content);
}
else
{
if (line.FirstAncestor(ns + "Title", ns + "Outline") is XElement title)
{
// special case to insert date before heading;
// if cursor is before first char of title, the entire title is "selected"
// so rather than replace the title, just insert the date before it
var first = title.Elements(ns + "OE").Elements(ns + "T").First();
var cdata = first.GetCData();
cdata.Value = $"{text} {cdata.Value}";
}
else if (line.Value.Length == 0)
{
// empty cdata, unselected cursor so just insert
line.GetCData().Value = text;
}
else
{
// this case should not happen; should be handled above
// something is selected so replace it
page.ReplaceSelectedWithContent(content);
}
}
}
var editor = new PageEditor(page);
editor.InsertOrReplace(text);

await one.Update(page);
}
Expand Down
4 changes: 2 additions & 2 deletions OneMore/Commands/Tagging/HashtagProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private int Upgrade3to4(SQLiteConnection con)
cmd.ExecuteNonQuery();

cmd.CommandText = "CREATE VIEW IF NOT EXISTS page_hashtags (moreID, tags) AS " +
"SELECT t.moreID, group_concat(DISTINCT(t.tag)) AS tags " +
"SELECT t.moreID, group_concat(DISTINCT(t.tag)) AS tags " +
"FROM hashtag t GROUP BY t.moreID";

cmd.ExecuteNonQuery();
Expand Down Expand Up @@ -837,7 +837,7 @@ public IEnumerable<string> ReadTagNames(string notebookID = null, string section
ReportError("error reading list of tag names", cmd, exc);
}

return tags;
return tags.OrderBy(s => s.Replace("#", "").ToLower());
}


Expand Down
60 changes: 60 additions & 0 deletions OneMore/Commands/Tagging/HashtaggerCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//************************************************************************************************
// Copyright © 2024 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

internal class HashtaggerCommand : Command
{

public HashtaggerCommand()
{
}


public override async Task Execute(params object[] args)
{
await using var one = new OneNote(out var page, out var ns);

using var dialog = new HashtaggerDialog(page);
if (dialog.ShowDialog(owner) != DialogResult.OK)
{
return;
}

var updated = false;
if (dialog.AddToBank)
{
var banker = new TagBankCommand();
banker.MakeWordBank(page, ns);
if (banker.BankOutline is not null)
{
var run = banker.BankOutline.Descendants(ns + "T").FirstOrDefault();
if (run.GetCData() is XCData cdata)
{
cdata.Value = $"{cdata.Value} {dialog.Tags}";
updated = true;
}
}
}
else
{
var editor = new PageEditor(page);
editor.InsertOrReplace(dialog.Tags);
updated = true;
}

if (updated)
{

await one.Update(page);
}
}
}
}
Loading

0 comments on commit f55fe42

Please sign in to comment.