Skip to content

Commit 9e1273c

Browse files
committed
Add a preview panel for creating pages
1 parent dc170ca commit 9e1273c

File tree

5 files changed

+177
-18
lines changed

5 files changed

+177
-18
lines changed

Flow.Launcher.Plugin.OneNote/Flow.Launcher.Plugin.OneNote.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
<PackageReference Include="Flow.Launcher.Plugin" Version="4.1.0" />
2929
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
3030
<PackageReference Include="ModernWpfUI" Version="0.9.6" />
31-
<PackageReference Include="Odotocodot.OneNote.Linq" Version="1.0.0" />
32-
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
33-
<PackageReference Include="System.Management" Version="8.0.0" />
31+
<PackageReference Include="Odotocodot.OneNote.Linq" Version="1.1.0" />
32+
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
3433
</ItemGroup>
3534

3635
<ItemGroup>

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Windows.Controls;
35
using Flow.Launcher.Plugin.OneNote.Icons;
6+
using Flow.Launcher.Plugin.OneNote.UI.Views;
47
using Humanizer;
58
using Odotocodot.OneNote.Linq;
69

@@ -92,6 +95,7 @@ public List<Result> EmptyQuery()
9295
Title = "New quick note",
9396
IcoPath = iconProvider.QuickNote,
9497
Score = -4000,
98+
PreviewPanel = new Lazy<UserControl>(() => new NewOneNotePagePreviewPanel(context, null, null)),
9599
Action = c =>
96100
{
97101
OneNoteApplication.CreateQuickNote(true);
@@ -228,6 +232,7 @@ public Result CreateNewPageResult(string newPageName, OneNoteSection section)
228232
SubTitle = $"Path: {GetNicePath(section)}{PathSeparator}{newPageName}",
229233
AutoCompleteText = $"{GetAutoCompleteText}{newPageName}",
230234
IcoPath = iconProvider.NewPage,
235+
PreviewPanel = new Lazy<UserControl>(() => new NewOneNotePagePreviewPanel(context, section, newPageName)),
231236
Action = c =>
232237
{
233238
OneNoteApplication.CreatePage(section, newPageName, true);
@@ -380,20 +385,21 @@ public List<Result> NoItemsInCollection(List<Result> results, IOneNoteItem paren
380385
// Can create page
381386
if (!section.Locked)
382387
{
383-
results.Add(NoItemsInCollectionResult("page", iconProvider.NewPage));
388+
results.Add(NoItemsInCollectionResult("page", iconProvider.NewPage, section: section));
384389
}
385390
break;
386391
}
387392

388393
return results;
389394

390-
static Result NoItemsInCollectionResult(string title, string iconPath, string subTitle = null)
395+
Result NoItemsInCollectionResult(string title, string iconPath, string subTitle = null, OneNoteSection section = null)
391396
{
392397
return new Result
393398
{
394399
Title = $"Create {title}: \"\"",
395400
SubTitle = $"No {subTitle ?? title}s found. Type a valid title to create one",
396401
IcoPath = iconPath,
402+
PreviewPanel = section != null ? new Lazy<UserControl>(() => new NewOneNotePagePreviewPanel(context, section, null)) : null ,
397403
};
398404
}
399405
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Windows.Input;
3+
using Odotocodot.OneNote.Linq;
4+
5+
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
6+
{
7+
public class NewOneNotePageViewModel : Model
8+
{
9+
private string pageTitle;
10+
private string pageContent;
11+
private readonly OneNoteSection section;
12+
private readonly PluginInitContext context;
13+
14+
public NewOneNotePageViewModel(PluginInitContext context, OneNoteSection section, string pageTitle)
15+
{
16+
this.context = context;
17+
this.section = section;
18+
PageTitle = pageTitle;
19+
CreateCommand = new RelayCommand(_ => CreatePage(false));
20+
CreateAndOpenCommand = new RelayCommand(_ => CreatePage(true));
21+
}
22+
23+
private void CreatePage(bool openImmediately)
24+
{
25+
var id = OneNoteApplication.CreatePage(section, PageTitle, false);
26+
var page = (OneNotePage)OneNoteItemExtensions.FindByID(id);
27+
var pageContentXml = page.GetPageContent();
28+
var xmlWrap = $"<one:Outline><one:Position x=\"36.0\" y=\"86.4000015258789\" z=\"0\"/><one:Size width=\"72.0\" height=\"13.42771339416504\"/><one:OEChildren><one:OE alignment=\"left\"><one:T><![CDATA[{PageContent}]]></one:T></one:OE></one:OEChildren></one:Outline>";
29+
pageContentXml = pageContentXml.Insert(pageContentXml.IndexOf("</one:Page>", StringComparison.Ordinal), xmlWrap);
30+
OneNoteApplication.UpdatePageContent(pageContentXml);
31+
Main.ForceReQuery();
32+
if (!openImmediately)
33+
return;
34+
page.OpenInOneNote();
35+
context.API.HideMainWindow();
36+
}
37+
public string PageTitle
38+
{
39+
get => pageTitle;
40+
set => SetProperty(ref pageTitle, value);
41+
}
42+
43+
public string PageContent
44+
{
45+
get => pageContent;
46+
set => SetProperty(ref pageContent, value);
47+
}
48+
49+
public ICommand CreateCommand { get; }
50+
public ICommand CreateAndOpenCommand { get; }
51+
}
52+
}
Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,89 @@
1-
<UserControl x:Class="Flow.Launcher.Plugin.OneNote.UI.Views.NewOneNotePagePreviewPanel"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="clr-namespace:Flow.Launcher.Plugin.OneNote.UI.Views"
7-
mc:Ignorable="d"
8-
d:DesignHeight="300" d:DesignWidth="300">
9-
<Grid>
10-
11-
</Grid>
12-
</UserControl>
1+
<UserControl
2+
x:Class="Flow.Launcher.Plugin.OneNote.UI.Views.NewOneNotePagePreviewPanel"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:ui="http://schemas.modernwpf.com/2019"
8+
xmlns:vm="clr-namespace:Flow.Launcher.Plugin.OneNote.UI.ViewModels"
9+
d:DataContext="{d:DesignInstance Type=vm:NewOneNotePageViewModel}"
10+
d:DesignHeight="300"
11+
d:DesignWidth="300"
12+
Loaded="NewOneNotePagePreviewPanel_OnLoaded"
13+
mc:Ignorable="d">
14+
<UserControl.Resources>
15+
<ResourceDictionary>
16+
<ResourceDictionary.MergedDictionaries>
17+
<ResourceDictionary Source="/Flow.Launcher.Plugin.OneNote;component/UI/Styles.xaml" />
18+
</ResourceDictionary.MergedDictionaries>
19+
</ResourceDictionary>
20+
</UserControl.Resources>
21+
<DockPanel Margin="10,5,5,5" LastChildFill="True">
22+
<DockPanel>
23+
<Grid
24+
Margin="0,0,0,5"
25+
HorizontalAlignment="Stretch"
26+
DockPanel.Dock="Top">
27+
<TextBlock
28+
HorizontalAlignment="Center"
29+
VerticalAlignment="Center"
30+
FontSize="15"
31+
Text="Create Page" />
32+
<!--<Image
33+
Width="32"
34+
Height="32"
35+
HorizontalAlignment="Right"
36+
Source="{Binding NewPageUri}" />-->
37+
</Grid>
38+
<Grid PreviewKeyDown="OnKeyDown">
39+
<Grid.RowDefinitions>
40+
<RowDefinition Height="Auto" />
41+
<RowDefinition Height="5" />
42+
<RowDefinition Height="*" />
43+
<RowDefinition Height="5" />
44+
<RowDefinition Height="Auto" />
45+
</Grid.RowDefinitions>
46+
<TextBox
47+
Grid.Row="0"
48+
x:Name="TextBoxPageTitle"
49+
ui:ControlHelper.PlaceholderText="Page Title"
50+
Text="{Binding PageTitle}" />
51+
<TextBox
52+
Grid.Row="2"
53+
ui:ControlHelper.PlaceholderText="Page Content"
54+
ui:TextBoxHelper.IsEnabled="True"
55+
ui:TextContextMenu.UsingTextContextMenu="True"
56+
AcceptsReturn="True"
57+
HorizontalScrollBarVisibility="Auto"
58+
MinLines="3"
59+
Text="{Binding PageContent}"
60+
TextWrapping="NoWrap"
61+
VerticalScrollBarVisibility="Auto" />
62+
<WrapPanel
63+
Grid.Row="4"
64+
HorizontalAlignment="Center"
65+
Orientation="Horizontal">
66+
<Button
67+
Width="70"
68+
Height="{Binding ElementName=ButtonCreateAndOpen, Path=ActualHeight}"
69+
Margin="4,0,25,0"
70+
ToolTip="Creates a page in OneNote"
71+
Command="{Binding CreateCommand}"
72+
Content="Create"
73+
Style="{DynamicResource SettingButton}" />
74+
<Button
75+
x:Name="ButtonCreateAndOpen"
76+
Width="70"
77+
Margin="25,0,4,0"
78+
ToolTip="Creates a page in OneNote and opens it"
79+
Command="{Binding CreateAndOpenCommand}"
80+
Style="{DynamicResource SettingButton}">
81+
<TextBlock TextAlignment="Center">
82+
Create<LineBreak />
83+
&amp; Open</TextBlock>
84+
</Button>
85+
</WrapPanel>
86+
</Grid>
87+
</DockPanel>
88+
</DockPanel>
89+
</UserControl>
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
using System.Windows;
12
using System.Windows.Controls;
3+
using System.Windows.Input;
4+
using Flow.Launcher.Plugin.OneNote.UI.ViewModels;
5+
using Odotocodot.OneNote.Linq;
26

37
namespace Flow.Launcher.Plugin.OneNote.UI.Views;
48

59
public partial class NewOneNotePagePreviewPanel : UserControl
610
{
7-
public NewOneNotePagePreviewPanel()
11+
public NewOneNotePagePreviewPanel(PluginInitContext context, OneNoteSection section, string pageTitle)
812
{
913
InitializeComponent();
14+
DataContext = new NewOneNotePageViewModel(context, section, pageTitle);
15+
}
16+
private void NewOneNotePagePreviewPanel_OnLoaded(object sender, RoutedEventArgs e)
17+
{
18+
TextBoxPageTitle.Focus();
19+
}
20+
21+
private void OnKeyDown(object sender, KeyEventArgs e)
22+
{
23+
if (e.Key != Key.Tab)
24+
return;
25+
26+
if (e.Source is not (TextBox or Button))
27+
return;
28+
29+
var focusNavigationDirection = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
30+
? FocusNavigationDirection.Previous
31+
: FocusNavigationDirection.Next;
32+
((UIElement)Keyboard.FocusedElement)?.MoveFocus(new TraversalRequest(focusNavigationDirection));
33+
34+
e.Handled = true;
1035
}
1136
}

0 commit comments

Comments
 (0)