Repository navigation
Expand file tree
/
Copy pathQuickNoteContracts.cs
More file actions
131 lines (109 loc) · 3.76 KB
/
Copy pathQuickNoteContracts.cs
File metadata and controls
131 lines (109 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Globalization;
using System.IO;
using System.Windows.Media;
namespace AiteBar;
internal static class QuickNoteBrush
{
public static Brush FromHex(string color)
{
var brush = (Brush)new BrushConverter().ConvertFromInvariantString(color)!;
if (brush.CanFreeze)
{
brush.Freeze();
}
return brush;
}
}
internal static class QuickNoteUrlValidator
{
public static bool IsSafeHttpUrl(string? url) =>
!string.IsNullOrWhiteSpace(url) &&
Uri.TryCreate(url, UriKind.Absolute, out Uri? uri) &&
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
}
internal static class QuickNoteFonts
{
public const string DefaultFamilyName = "Segoe UI";
public const string CodeFamilyName = "JetBrains Mono";
public static FontFamily Default { get; } = new(DefaultFamilyName);
public static FontFamily Code { get; } = new(
new Uri(Path.Combine(AppContext.BaseDirectory, "Resources", "Fonts", "JetBrainsMono") + Path.DirectorySeparatorChar, UriKind.Absolute),
"./#" + CodeFamilyName);
}
internal static class QuickNoteTags
{
private const string HeadingPrefix = "heading:";
private const string IndentPrefix = "indent:";
private const string LinkPrefix = "link:";
public const string Code = "code";
public const string CodeHeader = "code-header";
public const string Quote = "quote";
public const string Divider = "divider";
public const string TaskChecked = "task:checked";
public const string TaskUnchecked = "task:unchecked";
public const string UserStrikethrough = "user:strikethrough";
public const string TaskStrikethrough = "task:strikethrough";
public static string Task(bool isChecked) => isChecked ? TaskChecked : TaskUnchecked;
public static bool TryGetTaskState(object? tag, out bool isChecked)
{
isChecked = false;
if (Equals(tag, TaskChecked))
{
isChecked = true;
return true;
}
if (Equals(tag, TaskUnchecked))
{
isChecked = false;
return true;
}
return false;
}
public static string Heading(int level) =>
HeadingPrefix + level.ToString(CultureInfo.InvariantCulture);
public static bool TryGetHeadingLevel(object? tag, out int level)
{
level = 0;
return TryGetValue(tag, HeadingPrefix, out string value) &&
int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out level) &&
level is >= 1 and <= 6;
}
public static string? Indent(string value) =>
string.IsNullOrEmpty(value) ? null : IndentPrefix + value;
public static string GetIndent(object? tag, string fallback = "") =>
TryGetValue(tag, IndentPrefix, out string value) ? value : fallback;
public static string Link(string url) => LinkPrefix + url;
public static string? GetLink(object? tag) =>
TryGetValue(tag, LinkPrefix, out string value) ? value : null;
private static bool TryGetValue(object? tag, string prefix, out string value)
{
value = string.Empty;
if (tag is not string text || !text.StartsWith(prefix, StringComparison.Ordinal))
{
return false;
}
value = text[prefix.Length..];
return true;
}
}
internal enum ClearFormattingScope
{
InlineOnly,
All,
PreserveLinks
}
// Find & Replace options used by Quick Note editor
internal struct FindReplaceOptions
{
public bool CaseSensitive;
public bool WholeWord;
public bool UseRegex;
public bool SearchSelectionOnly;
}
// Result information for Find & Replace operations
internal struct FindReplaceResult
{
public int MatchesFound;
public int ReplacementsMade;
}