Skip to content

Commit

Permalink
1045 copy page links (#1380)
Browse files Browse the repository at this point in the history
* Add copy button
#1045

* copy links from month view
#1045

* Fix click to dayview from side-month
#1045
  • Loading branch information
stevencohn authored May 2, 2024
1 parent 71e946e commit 16893a4
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 31 deletions.
15 changes: 9 additions & 6 deletions OneMore/Commands/Favorites/FavoritesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public List<Favorite> SortFavorites()
var root = XElement.Load(path, LoadOptions.None);

var favorites = root.Elements()
.Where(e => e.Attribute("notebookID") != null)
.Where(e => e.Attribute("notebookID") is not null)
.ToList();

favorites.Remove();
Expand Down Expand Up @@ -264,7 +264,10 @@ private async Task ConfirmByID(Favorite favorite)
try
{
notebook = await one.GetNotebook(favorite.NotebookID, OneNote.Scope.Pages);
notebooks.Add(favorite.NotebookID, notebook);
if (notebook is not null)
{
notebooks.Add(favorite.NotebookID, notebook);
}
}
catch (COMException exc) when ((uint)exc.ErrorCode == ErrorCodes.hrObjectMissing)
{
Expand All @@ -276,7 +279,7 @@ private async Task ConfirmByID(Favorite favorite)
}
}

if (notebook != null &&
if (notebook is not null &&
notebook.Descendants().Any(e => e.Attribute("ID")?.Value == favorite.ObjectID))
{
favorite.Status = FavoriteStatus.Known;
Expand Down Expand Up @@ -304,14 +307,14 @@ private async Task<bool> ConfirmByLocation(Favorite favorite)
}

var notebook = notebooks.Values.FirstOrDefault(n => n.Attribute("name")?.Value == parts[0]);
if (notebook == null)
if (notebook is null)
{
// first part should be name of notebook
var nx = books.GetNamespaceOfPrefix(OneNote.Prefix);
var book = books.Elements(nx + "Notebook")
.FirstOrDefault(n => n.Attribute("name")?.Value == parts[0]);

if (book == null)
if (book is null)
{
logger.WriteLine($"broken link to favorite notebook {favorite.Location}");
favorite.Status = FavoriteStatus.Suspect;
Expand All @@ -335,7 +338,7 @@ private async Task<bool> ConfirmByLocation(Favorite favorite)
for (int i = 1; i < parts.Length; i++)
{
node = node.Elements().FirstOrDefault(n => n.Attribute("name")?.Value == parts[i]);
if (node == null)
if (node is null)
{
logger.WriteLine($"broken link to favorite parts[{i}] {favorite.Location}");
favorite.Status = FavoriteStatus.Suspect;
Expand Down
35 changes: 34 additions & 1 deletion OneMore/OneNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public HierarchyNode GetHierarchyNode(string nodeId)


/// <summary>
/// Gets a onenote:hyperline to an object on the specified page
/// Gets a onenote:hyperlink to an object on the specified page
/// </summary>
/// <param name="pageId">The ID of a page</param>
/// <param name="objectId">
Expand Down Expand Up @@ -462,6 +462,39 @@ public string GetHyperlink(string pageId, string objectId)
}


/// <summary>
/// Gets a Web hyperlink to an object on the specified page
/// </summary>
/// <param name="pageId">The ID of a page</param>
/// <param name="objectId">
/// The ID of an object on the page or string.Empty to link to the page itself
/// </param>
/// <returns></returns>
public string GetWebHyperlink(string pageId, string objectId)
{
try
{
onenote.GetWebHyperlinkToObject(pageId, objectId, out var hyperlink);
return hyperlink.SafeUrlEncode();
}
catch (Exception exc)
{
if (exc.HResult == ObjectDoesNotExist)
{
// objectIDs are ephemeral, generated on-the-fly from the current machine
// so will not exist when viewing the same page on a different machine;
// they are consistent on a single machine, probably using some hardware
// based heuristics I presume
logger.WriteLine("GetWebHyperlink, object does not exist. Possible cross-machine query");
return null;
}

logger.WriteLine("GetWebHyperlink error", exc);
return null;
}
}


/// <summary>
/// Gets the OneNote namespace for the given element
/// </summary>
Expand Down
14 changes: 12 additions & 2 deletions OneMoreCalendar/CalendarForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace OneMoreCalendar
/// </summary>
internal partial class CalendarForm : ThemedForm
{
private const int ManualDelta = 1000;

private DateTime date;
private CalendarPages pages;

Expand Down Expand Up @@ -102,7 +104,7 @@ public override void OnThemeChange()

private async Task SetMonth(int delta)
{
if (delta < 1000)
if (delta < ManualDelta)
{
date = delta == 0
? DateTime.Now.StartOfMonth()
Expand Down Expand Up @@ -154,8 +156,16 @@ private void ChangeView(object sender, EventArgs e)
}


private void ClickDayView(object sender, CalendarDayEventArgs e)
private async void ClickDayView(object sender, CalendarDayEventArgs e)
{
if (e.DayDate.Month != date.Month)
{
SuspendLayout();
date = e.DayDate.StartOfMonth();
await SetMonth(ManualDelta);
ResumeLayout();
}

dayButton.Checked = true;
}

Expand Down
6 changes: 6 additions & 0 deletions OneMoreCalendar/CalendarPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ internal class CalendarPage


public bool HasReminders { get; set; }


public string Hyperlink { get; set; }


public string WebHyperlink { get; set; }
}
}
11 changes: 9 additions & 2 deletions OneMoreCalendar/MonthView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 16893a4

Please sign in to comment.