-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSnippetManager.cs
More file actions
113 lines (98 loc) · 3.3 KB
/
SnippetManager.cs
File metadata and controls
113 lines (98 loc) · 3.3 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
using System.Text.Json;
namespace NoteUI;
public class SnippetManager
{
private readonly List<SnippetEntry> _snippets = [];
private static readonly string SaveDir =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NoteUI");
private static readonly string SavePath = Path.Combine(SaveDir, "snippets.json");
public IReadOnlyList<SnippetEntry> Snippets => _snippets;
public void Load()
{
try
{
if (!File.Exists(SavePath)) return;
var json = File.ReadAllText(SavePath);
var entries = JsonSerializer.Deserialize<List<SnippetEntry>>(json);
if (entries != null)
{
_snippets.Clear();
_snippets.AddRange(entries);
}
}
catch { }
}
public void Save()
{
try
{
Directory.CreateDirectory(SaveDir);
var json = JsonSerializer.Serialize(_snippets, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(SavePath, json);
}
catch { }
}
public SnippetEntry AddSnippet(string noteId, string keyword, string prefix, string content)
{
// Remove existing snippet for same noteId if any
_snippets.RemoveAll(s => s.NoteId == noteId);
var snippet = new SnippetEntry
{
Id = Guid.NewGuid().ToString(),
NoteId = noteId,
Keyword = keyword,
Prefix = prefix,
Content = content,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
};
_snippets.Add(snippet);
Save();
return snippet;
}
public void RemoveSnippet(string noteId)
{
_snippets.RemoveAll(s => s.NoteId == noteId);
Save();
}
public void UpdateContent(string noteId, string content)
{
var snippet = _snippets.FirstOrDefault(s => s.NoteId == noteId);
if (snippet == null) return;
snippet.Content = content;
snippet.UpdatedAt = DateTime.Now;
Save();
}
public SnippetEntry? FindByNoteId(string noteId)
{
return _snippets.FirstOrDefault(s => s.NoteId == noteId);
}
public SnippetEntry? FindByTrigger(string typed)
{
if (string.IsNullOrEmpty(typed)) return null;
return _snippets.FirstOrDefault(s =>
{
var trigger = string.IsNullOrEmpty(s.Prefix) ? s.Keyword : s.Prefix + s.Keyword;
return trigger.Equals(typed, StringComparison.Ordinal);
});
}
public bool IsKeywordTaken(string keyword, string prefix, string? excludeNoteId = null)
{
if (string.IsNullOrEmpty(keyword)) return false;
return _snippets.Any(s =>
s.Keyword.Equals(keyword, StringComparison.Ordinal) &&
s.Prefix == prefix &&
s.NoteId != excludeNoteId);
}
}
public class SnippetEntry
{
public string Id { get; set; } = "";
public string NoteId { get; set; } = "";
public string Keyword { get; set; } = "";
public string Prefix { get; set; } = "";
public string Content { get; set; } = "";
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string Trigger => string.IsNullOrEmpty(Prefix) ? Keyword : Prefix + Keyword;
}