Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
391 lines (354 loc) · 12.2 KB
/
Copy pathModel.js
File metadata and controls
391 lines (354 loc) · 12.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Quick Note model for vt.quicknote.
//
// Plain JS so it can be unit tested under Node. QML owns Hyprland / Wayland
// bindings, DesktopEntries, FileView, and wl-paste. The only persisted
// content is the user's note file — no side index of focus, titles, or
// clipboard.
var DEFAULT_RELATIVE = "/.local/share/vt.quicknote/note.md";
var STAMP_SEP = " \u00b7 ";
var MAX_CAPTURE = 256 * 1024;
function isAppId(id) {
id = String(id || "").trim();
return id.length > 0 && id !== "Unknown";
}
// Stable app identifier. Prefer the Wayland appId, then Hyprland's
// initialClass / class. Window titles are never used as ids.
function appId(toplevel) {
if (!toplevel) return "";
if (toplevel.appId) {
var waylandId = String(toplevel.appId).trim();
if (isAppId(waylandId)) return waylandId;
}
var ipc = toplevel.lastIpcObject || {};
var id = String(ipc.initialClass || "").trim();
if (!isAppId(id)) id = String(ipc.class || "").trim();
return isAppId(id) ? id : "";
}
function windowTitle(toplevel) {
if (!toplevel) return "";
if (toplevel.title) {
var title = String(toplevel.title).trim();
if (title) return title;
}
var ipc = toplevel.lastIpcObject || {};
var name = String(ipc.title || "").trim();
if (!name) name = String(ipc.initialTitle || "").trim();
return name;
}
function prettyAppId(id) {
var value = String(id || "").trim();
if (!value) return "";
var parts = value.split(".");
var last = parts[parts.length - 1] || value;
last = last.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim();
return last.replace(/\b[a-z]/g, function(ch) { return ch.toUpperCase(); });
}
// Titles leak document / tab names. Those must never become the app label
// written into a default (app-mode) stamp.
function looksLikeWindowTitle(name) {
var value = String(name || "");
if (!value) return false;
if (value.length > 40) return true;
if (value.indexOf(" | ") !== -1) return true;
if (value.indexOf(" \u2014 ") !== -1) return true;
if (value.indexOf(" \u2013 ") !== -1) return true;
if (value.indexOf(" - ") !== -1) return true;
return false;
}
function resolveAppName(id, desktopName, storedName) {
if (!isAppId(id)) return "";
var desktop = String(desktopName || "").trim();
if (desktop) return desktop;
var stored = String(storedName || "").trim();
if (stored && stored !== id && !looksLikeWindowTitle(stored)) return stored;
return prettyAppId(id);
}
function defaultNotePath(home) {
return String(home || "").replace(/\/$/, "") + DEFAULT_RELATIVE;
}
function expandPath(home, path) {
var h = String(home || "").replace(/\/$/, "");
var p = String(path || "").trim();
if (!p) return defaultNotePath(h);
if (p === "~") return h;
if (p.indexOf("~/") === 0) return h + p.slice(1);
if (p.indexOf("$HOME/") === 0) return h + p.slice(5);
if (p.charAt(0) === "/") return p;
return h + "/" + p;
}
function isPluginCreatedPath(home, path) {
return expandPath(home, path) === defaultNotePath(home);
}
function noteDir(path) {
var p = String(path || "");
var slash = p.lastIndexOf("/");
return slash > 0 ? p.slice(0, slash) : "";
}
function normalizeStampMode(mode) {
var value = String(mode || "app").trim().toLowerCase();
if (value === "title" || value === "off") return value;
return "app";
}
function isFalseyFlag(value) {
if (value === false || value === 0) return true;
var s = String(value === undefined || value === null ? "" : value).trim().toLowerCase();
return s === "false" || s === "off" || s === "0" || s === "no";
}
// Peek is on unless the user turned it off. Accepts a boolean or the
// common shell.json string forms.
function normalizePeekEnabled(value) {
if (value === undefined || value === null || value === "") return true;
return !isFalseyFlag(value);
}
function normalizePeekCorner(value) {
var s = String(value || "").trim().toLowerCase().replace(/[_\s]+/g, "-");
if (s === "top-left" || s === "tl" || s === "topleft") return "top-left";
if (s === "top-right" || s === "tr" || s === "topright") return "top-right";
if (s === "bottom-left" || s === "bl" || s === "bottomleft") return "bottom-left";
if (s === "bottom-right" || s === "br" || s === "bottomright") return "bottom-right";
return "bottom-right";
}
function peekAlignTop(corner) {
return normalizePeekCorner(corner).indexOf("top-") === 0;
}
function peekAlignLeft(corner) {
return normalizePeekCorner(corner).slice(-5) === "-left";
}
// Keep the peek off the Omarchy bar when it shares that screen edge.
function peekMargins(corner, barPosition, barClearance, gapsOut) {
var top = peekAlignTop(corner);
var left = peekAlignLeft(corner);
var gap = Number(gapsOut);
if (!isFinite(gap)) gap = 0;
var clearance = Number(barClearance);
if (!isFinite(clearance) || clearance < gap) clearance = gap;
var pos = String(barPosition || "top");
return {
top: top && pos === "top" ? clearance : gap,
bottom: !top && pos === "bottom" ? clearance : gap,
left: left && pos === "left" ? clearance : gap,
right: !left && pos === "right" ? clearance : gap
};
}
function normalizeLabel(value) {
return String(value || "").trim().toLowerCase();
}
function pad2(n) {
return n < 10 ? "0" + n : String(n);
}
function formatStampHeading(date, appLabel, title) {
var label = String(appLabel || "").trim();
if (!label) return "";
var t = String(title || "").trim();
return "## " + (t ? label + STAMP_SEP + t : label);
}
function stampTitleForMode(mode, title) {
if (normalizeStampMode(mode) !== "title") return "";
return String(title || "").trim();
}
function parseStamps(text) {
var src = String(text || "");
var stamps = [];
var re = /^## (?:(?:(\d{4}-\d{2}-\d{2}) )?(\d{1,2}:\d{2}) )?(.+)$/gm;
var match;
while ((match = re.exec(src))) {
var rest = String(match[3] || "");
var appLabel = rest;
var title = "";
var sepAt = rest.indexOf(STAMP_SEP);
if (sepAt !== -1) {
appLabel = rest.slice(0, sepAt);
title = rest.slice(sepAt + STAMP_SEP.length);
}
stamps.push({
index: match.index,
heading: match[0],
date: match[1] || "",
time: match[2] || "",
appLabel: String(appLabel || "").trim(),
title: String(title || "").trim()
});
}
return stamps;
}
function lastStamp(text) {
var stamps = parseStamps(text);
return stamps.length ? stamps[stamps.length - 1] : null;
}
function labelsMatch(stampLabel, appName, appId) {
var a = normalizeLabel(stampLabel);
if (!a) return false;
if (a === normalizeLabel(appName)) return true;
if (a === normalizeLabel(prettyAppId(appId))) return true;
if (a === normalizeLabel(appId)) return true;
return false;
}
function shouldAddStamp(text, mode, appLabel, title, appId) {
if (normalizeStampMode(mode) === "off") return false;
if (!String(appLabel || "").trim()) return false;
return matchingStamp(text, mode, appLabel, appId, title) === null;
}
// Reuse the latest section for this app. Title mode only reuses a heading
// that already carries that exact title; a new page gets a new heading.
function matchingStamp(text, mode, appName, appId, title) {
var stamps = parseStamps(text);
var hits = [];
for (var i = 0; i < stamps.length; i++) {
if (labelsMatch(stamps[i].appLabel, appName, appId)) hits.push(stamps[i]);
}
if (hits.length === 0) return null;
if (normalizeStampMode(mode) === "title") {
var wanted = String(title || "").trim();
for (var j = hits.length - 1; j >= 0; j--) {
if (hits[j].title === wanted) return hits[j];
}
return null;
}
return hits[hits.length - 1];
}
function sectionEnd(text, stamp) {
if (!stamp) return String(text || "").length;
var src = String(text || "");
var stamps = parseStamps(src);
var end = src.length;
for (var i = 0; i < stamps.length; i++) {
if (stamps[i].index > stamp.index) {
end = stamps[i].index;
break;
}
}
return end;
}
// Put the caret on a new line after the last content in this app's section.
// Inserts a newline only when the file would otherwise glue text together.
function ensureWriteLine(text, stamp) {
var src = String(text || "");
if (!stamp) {
if (src && src.charAt(src.length - 1) !== "\n") src += "\n";
return { text: src, cursor: src.length };
}
var end = sectionEnd(src, stamp);
var start = stamp.index + String(stamp.heading || "").length;
var body = src.slice(start, end);
var trimmed = body.replace(/\s+$/, "");
var contentEnd = start + trimmed.length;
if (contentEnd >= src.length) {
if (src.length && src.charAt(src.length - 1) !== "\n") {
src += "\n";
contentEnd = src.length;
}
return { text: src, cursor: src.length };
}
if (src.charAt(contentEnd) !== "\n") {
src = src.slice(0, contentEnd) + "\n" + src.slice(contentEnd);
}
return { text: src, cursor: contentEnd + 1 };
}
function applyCaptureInSection(text, stamp, body) {
var clip = clipBody(body);
if (!clip) return String(text || "");
var prepared = ensureWriteLine(text, stamp);
return prepared.text.slice(0, prepared.cursor) + clip + "\n" + prepared.text.slice(prepared.cursor);
}
function appendBlock(text, block) {
var src = String(text || "");
var add = String(block || "");
if (!add) return src;
if (!src.trim()) return add;
var out = src.replace(/\s*$/, "");
return out + "\n\n" + add;
}
function applyStamp(text, heading) {
var head = String(heading || "").trim();
if (!head) return String(text || "");
return appendBlock(text, head + "\n\n");
}
function clipBody(body) {
var clip = String(body || "").replace(/^\s+/, "").replace(/\s+$/, "");
if (clip.length > MAX_CAPTURE) clip = clip.slice(0, MAX_CAPTURE);
return clip;
}
function applyCapture(text, heading, body) {
var clip = clipBody(body);
if (!clip) return String(text || "");
var src = applyStamp(text, heading);
if (!String(src || "").trim()) return clip + "\n";
var out = String(src);
if (out.charAt(out.length - 1) !== "\n") out += "\n";
return out + clip + "\n";
}
function matchPeek(stamps, appName, appId, title) {
if (!Array.isArray(stamps) || stamps.length === 0) return null;
if (!isAppId(appId) && !String(appName || "").trim()) return null;
var hits = [];
for (var i = 0; i < stamps.length; i++) {
if (labelsMatch(stamps[i].appLabel, appName, appId)) hits.push(stamps[i]);
}
if (hits.length === 0) return null;
var t = String(title || "").trim();
if (t) {
for (var j = hits.length - 1; j >= 0; j--) {
if (hits[j].title && hits[j].title === t) return hits[j];
}
}
return hits[hits.length - 1];
}
function excerpt(text, stamp, maxChars) {
if (!stamp) return "";
var src = String(text || "");
var start = stamp.index + String(stamp.heading || "").length;
var stamps = parseStamps(src);
var end = src.length;
for (var i = 0; i < stamps.length; i++) {
if (stamps[i].index > stamp.index) {
end = stamps[i].index;
break;
}
}
var body = src.slice(start, end).replace(/^\s+/, "").replace(/\s+$/, "");
var limit = maxChars > 0 ? maxChars : 160;
if (body.length > limit) {
body = body.slice(0, limit).replace(/\s+\S*$/, "");
if (body) body += "\u2026";
}
return body;
}
if (typeof module !== "undefined") {
module.exports = {
DEFAULT_RELATIVE: DEFAULT_RELATIVE,
STAMP_SEP: STAMP_SEP,
MAX_CAPTURE: MAX_CAPTURE,
isAppId: isAppId,
appId: appId,
windowTitle: windowTitle,
prettyAppId: prettyAppId,
looksLikeWindowTitle: looksLikeWindowTitle,
resolveAppName: resolveAppName,
defaultNotePath: defaultNotePath,
expandPath: expandPath,
isPluginCreatedPath: isPluginCreatedPath,
noteDir: noteDir,
normalizeStampMode: normalizeStampMode,
normalizePeekEnabled: normalizePeekEnabled,
normalizePeekCorner: normalizePeekCorner,
peekAlignTop: peekAlignTop,
peekAlignLeft: peekAlignLeft,
peekMargins: peekMargins,
normalizeLabel: normalizeLabel,
formatStampHeading: formatStampHeading,
stampTitleForMode: stampTitleForMode,
parseStamps: parseStamps,
lastStamp: lastStamp,
labelsMatch: labelsMatch,
shouldAddStamp: shouldAddStamp,
matchingStamp: matchingStamp,
sectionEnd: sectionEnd,
ensureWriteLine: ensureWriteLine,
applyCaptureInSection: applyCaptureInSection,
applyStamp: applyStamp,
applyCapture: applyCapture,
clipBody: clipBody,
matchPeek: matchPeek,
excerpt: excerpt
};
}