Skip to content

Commit

Permalink
Merge pull request #26 from Odotocodot/dev
Browse files Browse the repository at this point in the history
Version 2.1.1
  • Loading branch information
Odotocodot authored Jan 4, 2025
2 parents 5f56aa8 + b72050d commit ece36f5
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 186 deletions.
13 changes: 13 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 2.1.1 - 2025-1-4

### Changes

- Indicated Windows/Microsoft Search is required for default and scoped search in readme. ([#24](https://github.com/Odotocodot/Flow.Launcher.Plugin.OneNote/issues/24))
- Added some badges.
- Updated OneNote page result tooltips.
- Changed recent pages default keyword from `rcntpgs:` to `rp:`.

### Fixes

- Fixed spelling and grammar mistakes.

## 2.1.0 - 2024-6-24

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <!-- Required for bringing OneNote to front -->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <!-- Required for bringing OneNote to front -->
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion Flow.Launcher.Plugin.OneNote/Keywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class Keywords
{
public const string NotebookExplorerSeparator = "\\";
public string NotebookExplorer { get; set; } = $"nb:{NotebookExplorerSeparator}";
public string RecentPages { get; set; } = "rcntpgs:";
public string RecentPages { get; set; } = "rp:";
public string TitleSearch { get; set; } = "*";
public string ScopedSearch { get; set; } = ">";
}
Expand Down
13 changes: 7 additions & 6 deletions Flow.Launcher.Plugin.OneNote/ResultCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<Result> EmptyQuery()
new Result
{
Title = "View notebook explorer",
SubTitle = $"Type \"{settings.Keywords.NotebookExplorer}\" or select this option to search by notebook structure ",
SubTitle = $"Type \"{settings.Keywords.NotebookExplorer}\" or select this option to search by notebook structure",
AutoCompleteText = $"{ActionKeyword} {settings.Keywords.NotebookExplorer}",
IcoPath = iconProvider.NotebookExplorer,
Score = 2000,
Expand Down Expand Up @@ -194,9 +194,10 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple

subTitle = subTitle[..^(page.Name.Length + PathSeparator.Length)];
toolTip =
$"Created:\t\t{page.Created:F}\n" +
$"Last Modified:\t{page.LastModified:F}";

$"""
{"Created:",-15} {page.Created:F}
{"Last Modified:",-15} {page.LastModified:F}
""";
iconInfo = new IconGeneratorInfo(page);
break;
default:
Expand Down Expand Up @@ -239,7 +240,7 @@ public Result CreatePageResult(OneNotePage page, string query)
public Result CreateRecentPageResult(OneNotePage page)
{
var result = CreateOneNoteItemResult(page, false, null);
result.SubTitle = $"{page.LastModified.Humanize()}\t{result.SubTitle}";
result.SubTitle = $"{page.LastModified.Humanize()} | {result.SubTitle}";
result.IcoPath = iconProvider.Recent;
return result;
}
Expand Down Expand Up @@ -454,7 +455,7 @@ private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection section, string
public static List<Result> NoMatchesFound()
{
return SingleResult("No matches found",
"Try searching something else, or syncing your notebooks.",
"Try searching something else, or syncing your notebooks",
IconProvider.Logo);
}
public List<Result> InvalidQuery()
Expand Down
173 changes: 173 additions & 0 deletions Flow.Launcher.Plugin.OneNote/SearchManager.NotebookExplorer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Odotocodot.OneNote.Linq;

namespace Flow.Launcher.Plugin.OneNote
{
public partial class SearchManager
{
private sealed class NotebookExplorer
{
private readonly SearchManager searchManager;
private readonly ResultCreator resultCreator;

private Keywords Keywords => searchManager.settings.Keywords;
internal NotebookExplorer(SearchManager searchManager, ResultCreator resultCreator)
{
this.searchManager = searchManager;
this.resultCreator = resultCreator;
}

internal List<Result> Query(Query query)
{
var results = new List<Result>();

string fullSearch = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];

IOneNoteItem parent = null;
IEnumerable<IOneNoteItem> collection = OneNoteApplication.GetNotebooks();

string[] searches = fullSearch.Split(Keywords.NotebookExplorerSeparator, StringSplitOptions.None);

for (int i = -1; i < searches.Length - 1; i++)
{
if (i < 0)
{
continue;
}

parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
if (parent == null)
{
return results;
}

collection = parent.Children;
}

string lastSearch = searches[^1];

results = lastSearch switch
{
// Empty search so show all in collection
string search when string.IsNullOrWhiteSpace(search)
=> EmptySearch(parent, collection),

// Search by title
string search when search.StartsWith(Keywords.TitleSearch) && parent is not OneNotePage
=> searchManager.TitleSearch(search, parent, collection),

// Scoped search
string search when search.StartsWith(Keywords.ScopedSearch) && parent is OneNoteNotebook or OneNoteSectionGroup
=> ScopedSearch(search, parent),

// Default search
_ => Explorer(lastSearch, parent, collection),
};

if (parent != null)
{
var result = resultCreator.CreateOneNoteItemResult(parent, false, score: 4000);
result.Title = $"Open \"{parent.Name}\" in OneNote";
result.SubTitle = lastSearch switch
{
string search when search.StartsWith(Keywords.TitleSearch)
=> $"Now searching by title in \"{parent.Name}\"",

string search when search.StartsWith(Keywords.ScopedSearch)
=> $"Now searching all pages in \"{parent.Name}\"",

_ => $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item",
};

results.Add(result);
}

return results;
}

private List<Result> EmptySearch(IOneNoteItem parent, IEnumerable<IOneNoteItem> collection)
{
List<Result> results = collection.Where(searchManager.SettingsCheck)
.Select(item => resultCreator.CreateOneNoteItemResult(item, true))
.ToList();
if (results.Any())
return results;
return resultCreator.NoItemsInCollection(results, parent);
}

private List<Result> ScopedSearch(string query, IOneNoteItem parent)
{
if (query.Length == Keywords.ScopedSearch.Length)
{
return ResultCreator.NoMatchesFound();
}

if (!char.IsLetterOrDigit(query[Keywords.ScopedSearch.Length]))
{
return resultCreator.InvalidQuery();
}

string currentSearch = query[Keywords.TitleSearch.Length..];

var results = OneNoteApplication.FindPages(currentSearch, parent)
.Select(pg => resultCreator.CreatePageResult(pg, currentSearch))
.ToList();

if (!results.Any())
{
results = ResultCreator.NoMatchesFound();
}

return results;
}
#nullable enable
private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
{
List<int>? highlightData = null;
int score = 0;

var results = collection.Where(searchManager.SettingsCheck)
.Where(item => searchManager.FuzzySearch(item.Name, search, out highlightData, out score))
.Select(item => resultCreator.CreateOneNoteItemResult(item, true, highlightData, score))
.ToList();

AddCreateNewOneNoteItemResults(search, parent, results);
return results;
}

private void AddCreateNewOneNoteItemResults(string newItemName, IOneNoteItem? parent, List<Result> results)
{
if (results.Any(result => string.Equals(newItemName.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
{
return;
}

if (parent?.IsInRecycleBin() == true)
{
return;
}

switch (parent)
{
case null:
results.Add(resultCreator.CreateNewNotebookResult(newItemName));
break;
case OneNoteNotebook:
case OneNoteSectionGroup:
results.Add(resultCreator.CreateNewSectionResult(newItemName, parent));
results.Add(resultCreator.CreateNewSectionGroupResult(newItemName, parent));
break;
case OneNoteSection section:
if (!section.Locked)
{
results.Add(resultCreator.CreateNewPageResult(newItemName, section));
}

break;
}
}
}
}
}
Loading

0 comments on commit ece36f5

Please sign in to comment.