@awni/mdplus 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +177 -2
- package/dist/base.css +213 -0
- package/dist/cli.js +4 -0
- package/dist/comments.css +55 -0
- package/dist/comments.js +211 -0
- package/dist/hljs.cjs +4 -0
- package/dist/index.js +497 -0
- package/dist/katex-fonts.json +1 -0
- package/dist/katex.css +1 -0
- package/dist/main.js +528 -0
- package/dist/page.js +28 -0
- package/dist/reload.js +14 -0
- package/dist/tips.css +19 -0
- package/dist/tips.js +95 -0
- package/mdplus.vsix +0 -0
- package/package.json +40 -4
package/dist/comments.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Add, reply to and resolve comments from the page (mdplus serve, and the VS Code extension).
|
|
2
|
+
// Each action is sent to the host, which edits the note; the page then updates like after any
|
|
3
|
+
// other change. The host sets window.__mdplusEdit = { token, rev, author, send? } and puts source
|
|
4
|
+
// lines on text blocks (data-l) and numbers on threads (data-t). Nothing here runs in a rendered
|
|
5
|
+
// file: the script and its styles are added by the host only.
|
|
6
|
+
(function () {
|
|
7
|
+
var E = window.__mdplusEdit;
|
|
8
|
+
if (!E) return;
|
|
9
|
+
|
|
10
|
+
var css = [
|
|
11
|
+
":root{--cmu-field:color-mix(in srgb,var(--fg) 3.5%,var(--bg));--cmu-ring:color-mix(in srgb,var(--accent) 24%,transparent);--cmu-shadow:0 2px 4px rgb(0 0 0/.08),0 8px 22px rgb(0 0 0/.14);--cmu-ok:var(--c-tip)}",
|
|
12
|
+
".cm-btn{-webkit-appearance:none;appearance:none;display:inline-flex;align-items:center;justify-content:center;gap:5px;height:26px;padding:0 10px;font:600 12px/1 var(--font-ui);color:var(--fg);background:var(--bg);border:1px solid var(--rule);border-radius:6px;cursor:pointer;user-select:none;-webkit-user-select:none;transition:background .12s,border-color .12s,color .12s,box-shadow .12s,transform .06s,opacity .12s}",
|
|
13
|
+
".cm-btn:hover{background:var(--cmu-field);border-color:color-mix(in srgb,var(--fg) 28%,var(--rule))}",
|
|
14
|
+
".cm-btn:active{transform:translateY(1px)}",
|
|
15
|
+
".cm-btn:focus-visible{outline:none;box-shadow:0 0 0 3px var(--cmu-ring);border-color:var(--accent)}",
|
|
16
|
+
".cm-btn.primary{color:var(--bg);background:var(--accent);border-color:var(--accent);box-shadow:0 1px 2px rgb(0 0 0/.17),inset 0 1px 0 rgb(255 255 255/.18)}",
|
|
17
|
+
".cm-btn.primary:hover{background:color-mix(in srgb,var(--accent) 88%,var(--fg));border-color:color-mix(in srgb,var(--accent) 88%,var(--fg))}",
|
|
18
|
+
".cm-btn.primary:disabled{opacity:.45;cursor:default;transform:none}",
|
|
19
|
+
".cm-btn.ghost{border-color:transparent;background:none;color:var(--muted);font-weight:500}",
|
|
20
|
+
".cm-btn.ghost:hover{background:var(--cmu-field);color:var(--fg);border-color:transparent}",
|
|
21
|
+
".cm-btn svg{width:14px;height:14px;flex:none}",
|
|
22
|
+
"#cm-pill{position:absolute;z-index:30;height:30px;padding:0 12px 0 9px;border-radius:999px;box-shadow:var(--cmu-shadow);animation:cm-pop .13s ease-out}",
|
|
23
|
+
"#cm-pill svg{color:var(--accent);width:15px;height:15px}",
|
|
24
|
+
"#cm-pill:hover{border-color:var(--accent);background:var(--bg)}",
|
|
25
|
+
"#cm-pop{position:absolute;z-index:31;width:min(21rem,calc(100vw - 24px));padding:10px;background:var(--bg);border:1px solid var(--rule);border-radius:10px;box-shadow:var(--cmu-shadow);font:13px/1.45 var(--font-ui);color:var(--fg);animation:cm-pop .13s ease-out}",
|
|
26
|
+
"#cm-pop textarea{display:block;width:100%;min-height:3.6em;max-height:14em;field-sizing:content;font:13px/1.45 var(--font-ui);color:var(--fg);background:var(--cmu-field);border:1px solid var(--rule);border-radius:7px;padding:6px 8px;resize:none;margin:0 0 8px;transition:border-color .12s,box-shadow .12s,background .12s}",
|
|
27
|
+
"#cm-pop textarea::placeholder{color:color-mix(in srgb,var(--muted) 75%,transparent)}",
|
|
28
|
+
"#cm-pop textarea:focus{outline:none;background:var(--bg);border-color:var(--accent);box-shadow:0 0 0 3px var(--cmu-ring)}",
|
|
29
|
+
"#cm-pop .row{display:flex;align-items:center;gap:6px}",
|
|
30
|
+
"#cm-pop .hint{margin-right:auto;display:inline-flex;gap:3px;align-items:center;font-size:11px;color:var(--muted)}",
|
|
31
|
+
"#cm-pop kbd{font:500 10.5px/1 var(--font-ui);color:var(--muted);background:var(--cmu-field);border:1px solid var(--rule);border-bottom-width:2px;border-radius:4px;padding:2px 4px}",
|
|
32
|
+
"#cm-pop .err{margin:0 0 8px;color:var(--c-caution);font-size:12px}",
|
|
33
|
+
".cm-thread[data-t]{position:relative}",
|
|
34
|
+
".cm-acts{position:absolute;top:-13px;right:8px;display:flex;gap:1px;padding:2px;background:var(--bg);border:1px solid var(--rule);border-radius:8px;box-shadow:var(--cmu-shadow);opacity:0;transform:translateY(3px);pointer-events:none;transition:opacity .12s,transform .12s}",
|
|
35
|
+
".cm-thread:hover>.cm-acts,.cm-acts:focus-within,.cm-thread.cm-busy>.cm-acts{opacity:1;transform:none;pointer-events:auto}",
|
|
36
|
+
".cm-acts .cm-btn{height:22px;padding:0 7px 0 5px;font-size:11.5px}",
|
|
37
|
+
".cm-acts .cm-btn[data-act=resolve]:hover,.cm-acts .cm-btn.sure{color:var(--cmu-ok);background:color-mix(in srgb,var(--cmu-ok) 12%,var(--bg))}",
|
|
38
|
+
"#cm-toast{position:fixed;left:50%;bottom:16px;transform:translateX(-50%);z-index:40;max-width:min(34rem,92vw);padding:8px 13px;border-radius:10px;background:var(--fg);color:var(--bg);font:12.5px/1.45 var(--font-ui);box-shadow:var(--cmu-shadow);animation:cm-pop .16s ease-out}",
|
|
39
|
+
"::highlight(mdplus-sel){background:var(--cm-hl-on,#f5d76eb8)}",
|
|
40
|
+
"@keyframes cm-pop{from{opacity:0;translate:0 4px}}",
|
|
41
|
+
"@media (prefers-reduced-motion:reduce){#cm-pill,#cm-pop,#cm-toast{animation:none}.cm-btn,.cm-acts{transition:none}}",
|
|
42
|
+
"@media print{#cm-pill,#cm-pop,#cm-toast,.cm-acts{display:none!important}}",
|
|
43
|
+
].join("\n");
|
|
44
|
+
var style = document.createElement("style");
|
|
45
|
+
style.textContent = css;
|
|
46
|
+
document.head.appendChild(style);
|
|
47
|
+
|
|
48
|
+
var svg = function (d) { return '<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">' + d + "</svg>"; };
|
|
49
|
+
var I_MSG = svg('<path d="M2.5 4.5a2 2 0 0 1 2-2h7a2 2 0 0 1 2 2v4.5a2 2 0 0 1-2 2H7.5L4.5 13.5V11a2 2 0 0 1-2-2z"/><path d="M8 5v3.5M6.25 6.75h3.5"/>');
|
|
50
|
+
var I_REPLY = svg('<path d="M6.5 4 3 7.5 6.5 11"/><path d="M3 7.5h6a4 4 0 0 1 4 4v.5"/>');
|
|
51
|
+
var I_CHECK = svg('<path d="m3.5 8.5 3 3 6-7"/>');
|
|
52
|
+
var el = function (tag, attrs, html) { var e = document.createElement(tag); for (var k in attrs) e.setAttribute(k, attrs[k]); if (html) e.innerHTML = html; return e; };
|
|
53
|
+
|
|
54
|
+
// E.send(body, done) replaces the request where the page has another way to reach its host
|
|
55
|
+
// (the VS Code extension); done gets { ok, exact } or { error }.
|
|
56
|
+
function post(body, done) {
|
|
57
|
+
if (E.send) return E.send(body, done);
|
|
58
|
+
body.page = location.pathname;
|
|
59
|
+
fetch("/__mdplus/comment", { method: "POST", headers: { "content-type": "application/json", "x-mdplus-token": E.token }, body: JSON.stringify(body) })
|
|
60
|
+
.then(function (r) { return r.json().catch(function () { return { error: "The server answered " + r.status + "." }; }); })
|
|
61
|
+
.then(done, function () { done({ error: "Cannot reach mdplus serve. Is it still running?" }); });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var toastT;
|
|
65
|
+
function toast(text) {
|
|
66
|
+
var t = document.getElementById("cm-toast") || document.body.appendChild(el("div", { id: "cm-toast", role: "status" }));
|
|
67
|
+
t.textContent = text;
|
|
68
|
+
clearTimeout(toastT);
|
|
69
|
+
toastT = setTimeout(function () { t.remove(); }, 5000);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The selection as the server needs it: the source lines of its block, and the selected
|
|
73
|
+
// content in pieces (text as shown, math by its TeX, anything else as a gap).
|
|
74
|
+
var BLOCK = "p,h1,h2,h3,h4,h5,h6,li,td,th,pre,figcaption,.math-display,.cm-below";
|
|
75
|
+
var ATOM = ".katex,.katex-error,a.ref,a.cite,.footnote-ref,.env-head,.sec-num";
|
|
76
|
+
function blockOf(node) {
|
|
77
|
+
var e = node.nodeType === 1 ? node : node.parentElement, b = e && e.closest(BLOCK);
|
|
78
|
+
return b && b.hasAttribute("data-l") && !e.closest(".cm-thread,.toc,.title-block,#cm-pop") ? b : null;
|
|
79
|
+
}
|
|
80
|
+
function describe(range) {
|
|
81
|
+
var b = blockOf(range.startContainer);
|
|
82
|
+
if (!b || !range.toString().trim()) return null;
|
|
83
|
+
var out = { l: b.getAttribute("data-l").split(",").map(Number), pieces: null, frac: 0 };
|
|
84
|
+
if (blockOf(range.endContainer) !== b) { // e.g. a triple click ends at the start of the next block
|
|
85
|
+
var rest = range.cloneRange();
|
|
86
|
+
rest.setStartAfter(b);
|
|
87
|
+
if (rest.toString().trim()) return out; // really spans blocks: a point comment on the first
|
|
88
|
+
}
|
|
89
|
+
var w = document.createTreeWalker(b, NodeFilter.SHOW_TEXT), n, pieces = [], total = 0, before = 0, started = false, last = null;
|
|
90
|
+
while ((n = w.nextNode())) {
|
|
91
|
+
var p = n.parentElement, len = n.data.length;
|
|
92
|
+
if (p.closest(".cm-thread")) continue;
|
|
93
|
+
total += len;
|
|
94
|
+
if (!range.intersectsNode(n)) { if (!started) before += len; continue; }
|
|
95
|
+
var atom = p.closest(ATOM);
|
|
96
|
+
if (atom && b.contains(atom)) {
|
|
97
|
+
started = true;
|
|
98
|
+
if (atom !== last) { var tex = atom.querySelector("annotation"); pieces.push(tex ? { m: tex.textContent } : { any: 1 }); last = atom; }
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
var s = n === range.startContainer ? range.startOffset : 0, e = n === range.endContainer ? range.endOffset : len;
|
|
102
|
+
if (!started) before += s;
|
|
103
|
+
started = true;
|
|
104
|
+
var t = n.data.slice(s, e);
|
|
105
|
+
if (!t) continue;
|
|
106
|
+
if (last === p && pieces.length && pieces[pieces.length - 1].t != null) pieces[pieces.length - 1].t += t;
|
|
107
|
+
else pieces.push({ t: t });
|
|
108
|
+
last = p;
|
|
109
|
+
}
|
|
110
|
+
while (pieces.length && pieces[0].t != null && !pieces[0].t.trim()) pieces.shift();
|
|
111
|
+
while (pieces.length && pieces[pieces.length - 1].t != null && !pieces[pieces.length - 1].t.trim()) pieces.pop();
|
|
112
|
+
if (pieces.length && pieces[0].t != null) pieces[0].t = pieces[0].t.replace(/^\s+/, "");
|
|
113
|
+
if (pieces.length && pieces[pieces.length - 1].t != null) pieces[pieces.length - 1].t = pieces[pieces.length - 1].t.replace(/\s+$/, "");
|
|
114
|
+
out.pieces = pieces.length ? pieces : null;
|
|
115
|
+
out.frac = total ? before / total : 0;
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// One composer, floating under what it comments on, so a page update does not take it away.
|
|
120
|
+
var pop = null;
|
|
121
|
+
function closePop() {
|
|
122
|
+
if (!pop) return;
|
|
123
|
+
pop.remove(); pop = null;
|
|
124
|
+
if (window.CSS && CSS.highlights) CSS.highlights.delete("mdplus-sel");
|
|
125
|
+
document.querySelectorAll(".cm-busy").forEach(function (t) { t.classList.remove("cm-busy"); });
|
|
126
|
+
}
|
|
127
|
+
function compose(rect, reply, send) {
|
|
128
|
+
closePop();
|
|
129
|
+
pop = el("div", { id: "cm-pop", role: "dialog", "aria-label": reply ? "Reply" : "New comment" },
|
|
130
|
+
'<textarea rows="2" placeholder="' + (reply ? "Reply…" : "Add a comment…") + '"></textarea><p class="err" hidden></p>' +
|
|
131
|
+
'<div class="row"><span class="hint" title="⌘↵ or Ctrl+↵ saves, Esc cancels"><kbd>⌘</kbd><kbd>↵</kbd></span>' +
|
|
132
|
+
'<button class="cm-btn ghost" data-do="cancel">Cancel</button><button class="cm-btn primary" data-do="save" disabled>' + (reply ? "Reply" : "Comment") + "</button></div>");
|
|
133
|
+
document.body.appendChild(pop);
|
|
134
|
+
var w = pop.offsetWidth, left = Math.max(12, Math.min(rect.left, innerWidth - w - 12));
|
|
135
|
+
pop.style.left = left + scrollX + "px";
|
|
136
|
+
pop.style.top = rect.bottom + scrollY + 8 + "px";
|
|
137
|
+
var ta = pop.querySelector("textarea"), save = pop.querySelector("[data-do=save]"), err = pop.querySelector(".err"), rev = E.rev;
|
|
138
|
+
function submit() {
|
|
139
|
+
if (save.disabled) return;
|
|
140
|
+
save.disabled = true;
|
|
141
|
+
send(ta.value, rev, function (r) {
|
|
142
|
+
if (r.ok) { closePop(); if (r.exact === false) toast("The selection spans markup, so the comment was attached to the end of its block."); return; }
|
|
143
|
+
err.textContent = r.error || "The comment was not saved."; err.hidden = false; save.disabled = false;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
ta.addEventListener("input", function () { save.disabled = !ta.value.trim(); });
|
|
147
|
+
ta.addEventListener("keydown", function (e) {
|
|
148
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); submit(); }
|
|
149
|
+
if (e.key === "Escape") closePop();
|
|
150
|
+
});
|
|
151
|
+
save.addEventListener("click", submit);
|
|
152
|
+
pop.querySelector("[data-do=cancel]").addEventListener("click", closePop);
|
|
153
|
+
ta.focus({ preventScroll: true });
|
|
154
|
+
pop.scrollIntoView({ block: "nearest" });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// "Comment" pill at the end of a selection.
|
|
158
|
+
var pill = null, picked = null;
|
|
159
|
+
function hidePill() { if (pill) { pill.remove(); pill = null; } }
|
|
160
|
+
function onSelect() {
|
|
161
|
+
var s = getSelection();
|
|
162
|
+
if (!s.rangeCount || s.isCollapsed || (pop && pop.contains(s.anchorNode))) { hidePill(); return; }
|
|
163
|
+
var range = s.getRangeAt(0), d = describe(range);
|
|
164
|
+
if (!d) { hidePill(); return; }
|
|
165
|
+
var rs = range.getClientRects(), r = null;
|
|
166
|
+
for (var i = rs.length - 1; i >= 0 && !r; i--) if (rs[i].width > 1) r = rs[i]; // not an empty rectangle at a line start
|
|
167
|
+
if (!r) { hidePill(); return; }
|
|
168
|
+
picked = { d: d, range: range.cloneRange(), rect: r };
|
|
169
|
+
if (!pill) {
|
|
170
|
+
pill = el("button", { id: "cm-pill", class: "cm-btn", type: "button" }, I_MSG + "Comment");
|
|
171
|
+
pill.addEventListener("mousedown", function (e) { e.preventDefault(); });
|
|
172
|
+
pill.addEventListener("click", function () {
|
|
173
|
+
var p = picked;
|
|
174
|
+
hidePill();
|
|
175
|
+
if (window.CSS && CSS.highlights && window.Highlight) CSS.highlights.set("mdplus-sel", new Highlight(p.range));
|
|
176
|
+
getSelection().removeAllRanges();
|
|
177
|
+
compose(p.rect, false, function (text, rev, done) { post({ op: "add", rev: rev, l: p.d.l, pieces: p.d.pieces, frac: p.d.frac, text: text }, done); });
|
|
178
|
+
});
|
|
179
|
+
document.body.appendChild(pill);
|
|
180
|
+
}
|
|
181
|
+
pill.style.left = Math.max(8, Math.min(r.right - 50, innerWidth - 120)) + scrollX + "px";
|
|
182
|
+
pill.style.top = Math.max(8, r.top - 38) + scrollY + "px";
|
|
183
|
+
}
|
|
184
|
+
document.addEventListener("mouseup", function (e) { if (!e.target.closest || !e.target.closest("#cm-pill,#cm-pop")) setTimeout(onSelect, 0); });
|
|
185
|
+
document.addEventListener("keyup", function (e) { if (e.shiftKey || e.key === "Shift") onSelect(); });
|
|
186
|
+
document.addEventListener("mousedown", function (e) { if (pop && !e.target.closest("#cm-pop,.cm-acts") && !pop.querySelector("textarea").value.trim()) closePop(); });
|
|
187
|
+
|
|
188
|
+
// Reply and Resolve on a card; the buttons are added when the card is first pointed at.
|
|
189
|
+
document.addEventListener("mouseover", function (e) {
|
|
190
|
+
var t = e.target.closest && e.target.closest(".cm-thread[data-t]");
|
|
191
|
+
if (!t || t.querySelector(":scope > .cm-acts")) return;
|
|
192
|
+
t.appendChild(el("span", { class: "cm-acts" },
|
|
193
|
+
'<button class="cm-btn ghost" type="button" data-act="reply">' + I_REPLY + "Reply</button>" +
|
|
194
|
+
'<button class="cm-btn ghost" type="button" data-act="resolve" title="Removes the thread from the note; the highlighted text stays">' + I_CHECK + "Resolve</button>"));
|
|
195
|
+
});
|
|
196
|
+
document.addEventListener("click", function (e) {
|
|
197
|
+
var b = e.target.closest && e.target.closest(".cm-acts [data-act]");
|
|
198
|
+
if (!b) return;
|
|
199
|
+
var t = b.closest(".cm-thread"), n = +t.getAttribute("data-t");
|
|
200
|
+
if (b.getAttribute("data-act") === "reply") {
|
|
201
|
+
var r = t.getBoundingClientRect();
|
|
202
|
+
compose({ left: r.left, bottom: r.bottom }, true, function (text, rev, done) { post({ op: "reply", rev: rev, thread: n, text: text }, done); });
|
|
203
|
+
t.classList.add("cm-busy");
|
|
204
|
+
} else if (!b.classList.contains("sure")) { // Resolve asks once more: it deletes from the note
|
|
205
|
+
b.classList.add("sure"); b.lastChild.textContent = "Delete thread?";
|
|
206
|
+
setTimeout(function () { if (b.isConnected) { b.classList.remove("sure"); b.lastChild.textContent = "Resolve"; } }, 3000);
|
|
207
|
+
} else {
|
|
208
|
+
post({ op: "resolve", rev: E.rev, thread: n }, function (r) { if (!r.ok) toast(r.error || "The thread was not resolved."); });
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
})();
|