|
| 1 | +using Microsoft.UI.Text; |
| 2 | +using Microsoft.UI.Xaml.Controls; |
| 3 | + |
| 4 | +namespace NoteUI; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Repairs note link formatting that is lost during RTF round-trips |
| 8 | +/// and provides shared link detection logic. |
| 9 | +/// </summary> |
| 10 | +internal static class NoteLinkHelper |
| 11 | +{ |
| 12 | + internal static readonly Windows.UI.Color LinkColor = Windows.UI.Color.FromArgb(255, 140, 200, 120); |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Scans the editor for note link IDs (GUID between zero-width spaces) |
| 16 | + /// and re-applies Hidden formatting, link color, and underline. |
| 17 | + /// Uses NotesManager to look up the title by GUID for robust title detection. |
| 18 | + /// </summary> |
| 19 | + internal static void RepairHiddenLinks(RichEditBox editor, NotesManager? notesManager = null) |
| 20 | + { |
| 21 | + editor.Document.GetText(TextGetOptions.None, out var text); |
| 22 | + var len = text.TrimEnd('\r', '\n').Length; |
| 23 | + if (len == 0) return; |
| 24 | + |
| 25 | + int pos = 0; |
| 26 | + while (pos < len) |
| 27 | + { |
| 28 | + int start = text.IndexOf('\u200B', pos); |
| 29 | + if (start < 0) break; |
| 30 | + |
| 31 | + int end = text.IndexOf('\u200B', start + 1); |
| 32 | + if (end < 0) break; |
| 33 | + |
| 34 | + var between = text[(start + 1)..end]; |
| 35 | + |
| 36 | + if (between.Length == 36 && Guid.TryParse(between, out _)) |
| 37 | + { |
| 38 | + // Restore Hidden + link color on the GUID range |
| 39 | + var idRange = editor.Document.GetRange(start, end + 1); |
| 40 | + idRange.CharacterFormat.Hidden = FormatEffect.On; |
| 41 | + idRange.CharacterFormat.ForegroundColor = LinkColor; |
| 42 | + |
| 43 | + // Find the link title before the GUID |
| 44 | + int titleEnd = start; |
| 45 | + int titleStart = titleEnd; |
| 46 | + |
| 47 | + // Strategy 1: look up note title by GUID |
| 48 | + string? expectedTitle = null; |
| 49 | + if (notesManager != null) |
| 50 | + { |
| 51 | + var note = notesManager.Notes.FirstOrDefault(n => n.Id == between); |
| 52 | + if (note != null) expectedTitle = note.Title; |
| 53 | + } |
| 54 | + |
| 55 | + if (expectedTitle != null && titleEnd >= expectedTitle.Length) |
| 56 | + { |
| 57 | + // Check if the text before the GUID matches the expected title |
| 58 | + var candidate = text.Substring(titleEnd - expectedTitle.Length, expectedTitle.Length); |
| 59 | + if (candidate == expectedTitle) |
| 60 | + titleStart = titleEnd - expectedTitle.Length; |
| 61 | + } |
| 62 | + |
| 63 | + // Strategy 2: fallback — walk backwards while underlined |
| 64 | + if (titleStart == titleEnd) |
| 65 | + { |
| 66 | + while (titleStart > 0) |
| 67 | + { |
| 68 | + var ch = editor.Document.GetRange(titleStart - 1, titleStart); |
| 69 | + ch.GetText(TextGetOptions.None, out var c); |
| 70 | + if (c == "\r" || c == "\n" || c == "\u200B") break; |
| 71 | + if (ch.CharacterFormat.Underline == UnderlineType.None) break; |
| 72 | + titleStart--; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (titleStart < titleEnd) |
| 77 | + { |
| 78 | + var titleRange = editor.Document.GetRange(titleStart, titleEnd); |
| 79 | + titleRange.CharacterFormat.ForegroundColor = LinkColor; |
| 80 | + titleRange.CharacterFormat.Underline = UnderlineType.Single; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pos = end + 1; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Detects if the cursor is on a note link and returns the target note ID. |
| 90 | + /// </summary> |
| 91 | + internal static string? DetectLinkAtCursor(RichEditBox editor, NotesManager notesManager) |
| 92 | + { |
| 93 | + var sel = editor.Document.Selection; |
| 94 | + if (sel == null) return null; |
| 95 | + |
| 96 | + var pos = sel.StartPosition; |
| 97 | + |
| 98 | + var fg = editor.Document.GetRange(pos, pos + 1).CharacterFormat.ForegroundColor; |
| 99 | + if (!IsLinkColor(fg) && pos > 0) |
| 100 | + fg = editor.Document.GetRange(pos - 1, pos).CharacterFormat.ForegroundColor; |
| 101 | + if (!IsLinkColor(fg)) return null; |
| 102 | + |
| 103 | + int end = pos; |
| 104 | + editor.Document.GetText(TextGetOptions.None, out var plainText); |
| 105 | + int textLen = plainText.TrimEnd('\r', '\n').Length; |
| 106 | + while (end < textLen) |
| 107 | + { |
| 108 | + var next = editor.Document.GetRange(end, end + 1); |
| 109 | + if (!IsLinkColor(next.CharacterFormat.ForegroundColor)) break; |
| 110 | + end++; |
| 111 | + } |
| 112 | + |
| 113 | + int start = pos > 0 ? pos - 1 : 0; |
| 114 | + while (start > 0) |
| 115 | + { |
| 116 | + var prev = editor.Document.GetRange(start - 1, start); |
| 117 | + prev.GetText(TextGetOptions.None, out var ch); |
| 118 | + if (ch == "\r" || ch == "\n" || !IsLinkColor(prev.CharacterFormat.ForegroundColor)) break; |
| 119 | + start--; |
| 120 | + } |
| 121 | + |
| 122 | + var linkRange = editor.Document.GetRange(start, end); |
| 123 | + linkRange.GetText(TextGetOptions.IncludeNumbering, out var visibleText); |
| 124 | + linkRange.GetText(TextGetOptions.None, out var fullText); |
| 125 | + visibleText = visibleText?.TrimEnd('\r', '\n') ?? ""; |
| 126 | + fullText = fullText?.TrimEnd('\r', '\n') ?? ""; |
| 127 | + |
| 128 | + string? noteId = null; |
| 129 | + if (fullText.Length > visibleText.Length && fullText.StartsWith(visibleText, StringComparison.Ordinal)) |
| 130 | + noteId = fullText[visibleText.Length..].Trim('\u200B'); |
| 131 | + |
| 132 | + if (string.IsNullOrEmpty(noteId)) |
| 133 | + { |
| 134 | + var target = notesManager.GetByTitle(visibleText); |
| 135 | + if (target != null) noteId = target.Id; |
| 136 | + } |
| 137 | + |
| 138 | + return noteId; |
| 139 | + } |
| 140 | + |
| 141 | + internal static bool IsLinkColor(Windows.UI.Color c) |
| 142 | + => c.R == LinkColor.R && c.G == LinkColor.G && c.B == LinkColor.B; |
| 143 | +} |
0 commit comments