@mdbase-dev/ui 0.1.0-beta.164 → 0.1.0-beta.166
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/README.md +41 -0
- package/css/live-preview.css +387 -0
- package/css/palette.css +2 -1
- package/dist/apps.d.ts +6 -1
- package/dist/apps.js +25 -0
- package/dist/collections/CollectionGate.js +14 -3
- package/dist/collections/model.d.ts +4 -1
- package/dist/collections/model.js +19 -2
- package/dist/live-preview/blocks.d.ts +3 -0
- package/dist/live-preview/blocks.js +381 -0
- package/dist/live-preview/inline.d.ts +20 -0
- package/dist/live-preview/inline.js +418 -0
- package/dist/live-preview/keys.d.ts +7 -0
- package/dist/live-preview/keys.js +143 -0
- package/dist/live-preview/reveal.d.ts +29 -0
- package/dist/live-preview/reveal.js +97 -0
- package/dist/live-preview/table-controls.d.ts +75 -0
- package/dist/live-preview/table-controls.js +734 -0
- package/dist/live-preview/table-model.d.ts +113 -0
- package/dist/live-preview/table-model.js +510 -0
- package/dist/live-preview/table.d.ts +2 -0
- package/dist/live-preview/table.js +832 -0
- package/dist/live-preview.d.ts +16 -0
- package/dist/live-preview.js +47 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/package.json +30 -4
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { indentLess, indentMore } from "@codemirror/commands";
|
|
2
|
+
import { syntaxTree } from "@codemirror/language";
|
|
3
|
+
import { StateEffect, StateField } from "@codemirror/state";
|
|
4
|
+
import { Decoration, EditorView, WidgetType, keymap } from "@codemirror/view";
|
|
5
|
+
import { i18n as _i18n } from "@lingui/core";
|
|
6
|
+
import { isRevealed, revealField } from "./reveal.js";
|
|
7
|
+
const toggleFold = StateEffect.define({
|
|
8
|
+
map: (value, changes) => ({
|
|
9
|
+
...value,
|
|
10
|
+
from: changes.mapPos(value.from)
|
|
11
|
+
})
|
|
12
|
+
});
|
|
13
|
+
const folds = StateField.define({
|
|
14
|
+
create: () => new Map(),
|
|
15
|
+
update(value, tr) {
|
|
16
|
+
const next = new Map();
|
|
17
|
+
for (const [pos, folded] of value) next.set(tr.changes.mapPos(pos), folded);
|
|
18
|
+
for (const effect of tr.effects) if (effect.is(toggleFold)) next.set(effect.value.from, effect.value.folded);
|
|
19
|
+
return next;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const hide = Decoration.replace({});
|
|
23
|
+
const codeLine = edge => Decoration.line({
|
|
24
|
+
class: `cm-lp-code-block ${edge}`
|
|
25
|
+
});
|
|
26
|
+
const footnote = Decoration.mark({
|
|
27
|
+
class: "cm-lp-footnote-ref"
|
|
28
|
+
});
|
|
29
|
+
const types = {
|
|
30
|
+
note: "✎",
|
|
31
|
+
info: "ⓘ",
|
|
32
|
+
tip: "✦",
|
|
33
|
+
success: "✓",
|
|
34
|
+
warning: "!",
|
|
35
|
+
danger: "!",
|
|
36
|
+
question: "?",
|
|
37
|
+
quote: "❞",
|
|
38
|
+
example: "☷",
|
|
39
|
+
todo: "✓"
|
|
40
|
+
};
|
|
41
|
+
function typeTitle(type) {
|
|
42
|
+
switch (type) {
|
|
43
|
+
case "info":
|
|
44
|
+
return _i18n._(
|
|
45
|
+
/** i18n */
|
|
46
|
+
{
|
|
47
|
+
id: "L1ri9i",
|
|
48
|
+
message: "Info",
|
|
49
|
+
context: "callout type"
|
|
50
|
+
});
|
|
51
|
+
case "tip":
|
|
52
|
+
return _i18n._(
|
|
53
|
+
/** i18n */
|
|
54
|
+
{
|
|
55
|
+
id: "jQw-3a",
|
|
56
|
+
message: "Tip",
|
|
57
|
+
context: "callout type"
|
|
58
|
+
});
|
|
59
|
+
case "success":
|
|
60
|
+
return _i18n._(
|
|
61
|
+
/** i18n */
|
|
62
|
+
{
|
|
63
|
+
id: "mx_YJT",
|
|
64
|
+
message: "Success",
|
|
65
|
+
context: "callout type"
|
|
66
|
+
});
|
|
67
|
+
case "warning":
|
|
68
|
+
return _i18n._(
|
|
69
|
+
/** i18n */
|
|
70
|
+
{
|
|
71
|
+
id: "c-3VHy",
|
|
72
|
+
message: "Warning",
|
|
73
|
+
context: "callout type"
|
|
74
|
+
});
|
|
75
|
+
case "danger":
|
|
76
|
+
return _i18n._(
|
|
77
|
+
/** i18n */
|
|
78
|
+
{
|
|
79
|
+
id: "N24alS",
|
|
80
|
+
message: "Danger",
|
|
81
|
+
context: "callout type"
|
|
82
|
+
});
|
|
83
|
+
case "question":
|
|
84
|
+
return _i18n._(
|
|
85
|
+
/** i18n */
|
|
86
|
+
{
|
|
87
|
+
id: "n0NZ1n",
|
|
88
|
+
message: "Question",
|
|
89
|
+
context: "callout type"
|
|
90
|
+
});
|
|
91
|
+
case "quote":
|
|
92
|
+
return _i18n._(
|
|
93
|
+
/** i18n */
|
|
94
|
+
{
|
|
95
|
+
id: "yRul2Z",
|
|
96
|
+
message: "Quote",
|
|
97
|
+
context: "callout type"
|
|
98
|
+
});
|
|
99
|
+
case "example":
|
|
100
|
+
return _i18n._(
|
|
101
|
+
/** i18n */
|
|
102
|
+
{
|
|
103
|
+
id: "bV3Pem",
|
|
104
|
+
message: "Example",
|
|
105
|
+
context: "callout type"
|
|
106
|
+
});
|
|
107
|
+
case "todo":
|
|
108
|
+
return _i18n._(
|
|
109
|
+
/** i18n */
|
|
110
|
+
{
|
|
111
|
+
id: "rjMmwY",
|
|
112
|
+
message: "Todo",
|
|
113
|
+
context: "callout type"
|
|
114
|
+
});
|
|
115
|
+
default:
|
|
116
|
+
return _i18n._(
|
|
117
|
+
/** i18n */
|
|
118
|
+
{
|
|
119
|
+
id: "rbUlsJ",
|
|
120
|
+
message: "Note",
|
|
121
|
+
context: "callout type"
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export function calloutHeader(text) {
|
|
126
|
+
return /^\s*>\s*\[!([\w-]+)\]([+-])?(?:[ \t]+(.*?))?[ \t]*$/i.exec(text);
|
|
127
|
+
}
|
|
128
|
+
class CodeHeader extends WidgetType {
|
|
129
|
+
language;
|
|
130
|
+
code;
|
|
131
|
+
constructor(language, code) {
|
|
132
|
+
super();
|
|
133
|
+
this.language = language;
|
|
134
|
+
this.code = code;
|
|
135
|
+
}
|
|
136
|
+
eq(other) {
|
|
137
|
+
return this.language === other.language && this.code === other.code;
|
|
138
|
+
}
|
|
139
|
+
toDOM() {
|
|
140
|
+
const row = document.createElement("span");
|
|
141
|
+
row.className = "cm-lp-code-header";
|
|
142
|
+
const label = document.createElement("span");
|
|
143
|
+
label.textContent = this.language;
|
|
144
|
+
const button = document.createElement("button");
|
|
145
|
+
button.type = "button";
|
|
146
|
+
button.textContent = _i18n._(
|
|
147
|
+
/** i18n */
|
|
148
|
+
{
|
|
149
|
+
id: "he3ygx",
|
|
150
|
+
message: "Copy"
|
|
151
|
+
});
|
|
152
|
+
button.setAttribute("aria-label", _i18n._(
|
|
153
|
+
/** i18n */
|
|
154
|
+
{
|
|
155
|
+
id: "NmPNJJ",
|
|
156
|
+
message: "Copy code"
|
|
157
|
+
}));
|
|
158
|
+
button.addEventListener("click", () => {
|
|
159
|
+
void navigator.clipboard.writeText(this.code).then(() => {
|
|
160
|
+
button.textContent = _i18n._(
|
|
161
|
+
/** i18n */
|
|
162
|
+
{
|
|
163
|
+
id: "6V3Ea3",
|
|
164
|
+
message: "Copied"
|
|
165
|
+
});
|
|
166
|
+
window.setTimeout(() => {
|
|
167
|
+
button.textContent = _i18n._(
|
|
168
|
+
/** i18n */
|
|
169
|
+
{
|
|
170
|
+
id: "he3ygx",
|
|
171
|
+
message: "Copy"
|
|
172
|
+
});
|
|
173
|
+
}, 1500);
|
|
174
|
+
}).catch(() => {
|
|
175
|
+
button.textContent = _i18n._(
|
|
176
|
+
/** i18n */
|
|
177
|
+
{
|
|
178
|
+
id: "9sj0G8",
|
|
179
|
+
message: "Copy failed"
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
row.append(label, button);
|
|
184
|
+
return row;
|
|
185
|
+
}
|
|
186
|
+
ignoreEvent() {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
class CalloutHeader extends WidgetType {
|
|
191
|
+
type;
|
|
192
|
+
title;
|
|
193
|
+
from;
|
|
194
|
+
foldable;
|
|
195
|
+
folded;
|
|
196
|
+
constructor(type, title, from, foldable, folded) {
|
|
197
|
+
super();
|
|
198
|
+
this.type = type;
|
|
199
|
+
this.title = title;
|
|
200
|
+
this.from = from;
|
|
201
|
+
this.foldable = foldable;
|
|
202
|
+
this.folded = folded;
|
|
203
|
+
}
|
|
204
|
+
eq(other) {
|
|
205
|
+
return this.type === other.type && this.title === other.title && this.from === other.from && this.foldable === other.foldable && this.folded === other.folded;
|
|
206
|
+
}
|
|
207
|
+
toDOM(view) {
|
|
208
|
+
const row = document.createElement("span");
|
|
209
|
+
row.className = "cm-lp-callout-header";
|
|
210
|
+
const icon = document.createElement("span");
|
|
211
|
+
icon.className = "cm-lp-callout-icon";
|
|
212
|
+
icon.setAttribute("aria-hidden", "true");
|
|
213
|
+
icon.textContent = types[this.type];
|
|
214
|
+
const title = document.createElement("span");
|
|
215
|
+
title.textContent = this.title;
|
|
216
|
+
row.append(icon, title);
|
|
217
|
+
if (this.foldable) {
|
|
218
|
+
const button = document.createElement("button");
|
|
219
|
+
button.type = "button";
|
|
220
|
+
button.textContent = this.folded ? "▸" : "▾";
|
|
221
|
+
button.setAttribute("aria-expanded", String(!this.folded));
|
|
222
|
+
button.setAttribute("aria-label", this.folded ? _i18n._(
|
|
223
|
+
/** i18n */
|
|
224
|
+
{
|
|
225
|
+
id: "mPqqvq",
|
|
226
|
+
message: "Expand callout"
|
|
227
|
+
}) : _i18n._(
|
|
228
|
+
/** i18n */
|
|
229
|
+
{
|
|
230
|
+
id: "Q4mVPq",
|
|
231
|
+
message: "Collapse callout"
|
|
232
|
+
}));
|
|
233
|
+
button.addEventListener("click", () => view.dispatch({
|
|
234
|
+
effects: toggleFold.of({
|
|
235
|
+
from: this.from,
|
|
236
|
+
folded: !this.folded
|
|
237
|
+
})
|
|
238
|
+
}));
|
|
239
|
+
row.append(button);
|
|
240
|
+
}
|
|
241
|
+
return row;
|
|
242
|
+
}
|
|
243
|
+
ignoreEvent() {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export function blockPreview() {
|
|
248
|
+
return [folds, EditorView.decorations.compute(["doc", revealField, folds], build), keymap.of([{
|
|
249
|
+
key: "Tab",
|
|
250
|
+
run: view => inCode(view.state) && indentMore(view)
|
|
251
|
+
}, {
|
|
252
|
+
key: "Shift-Tab",
|
|
253
|
+
run: view => inCode(view.state) && indentLess(view)
|
|
254
|
+
}])];
|
|
255
|
+
}
|
|
256
|
+
function inCode(state) {
|
|
257
|
+
const positions = state.selection.ranges.map(({
|
|
258
|
+
head
|
|
259
|
+
}) => head);
|
|
260
|
+
const matched = positions.map(() => false);
|
|
261
|
+
syntaxTree(state).iterate({
|
|
262
|
+
enter(node) {
|
|
263
|
+
if (node.name !== "FencedCode" && node.name !== "CodeBlock") return;
|
|
264
|
+
positions.forEach((pos, index) => {
|
|
265
|
+
if (pos > node.from && pos < node.to) matched[index] = true;
|
|
266
|
+
});
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
return matched.every(Boolean);
|
|
271
|
+
}
|
|
272
|
+
function build(state) {
|
|
273
|
+
const ranges = [];
|
|
274
|
+
const skip = [];
|
|
275
|
+
const foldState = state.field(folds);
|
|
276
|
+
syntaxTree(state).iterate({
|
|
277
|
+
enter(node) {
|
|
278
|
+
if (node.name === "FencedCode" || node.name === "CodeBlock") {
|
|
279
|
+
const first = state.doc.lineAt(node.from),
|
|
280
|
+
last = state.doc.lineAt(node.to);
|
|
281
|
+
skip.push({
|
|
282
|
+
from: node.from,
|
|
283
|
+
to: node.to
|
|
284
|
+
});
|
|
285
|
+
const fenced = node.name === "FencedCode";
|
|
286
|
+
const opening = fenced ? /^\s*(`{3,}|~{3,})\s*([^\s`]*)/.exec(first.text) : null;
|
|
287
|
+
const closed = opening && last.number > first.number && new RegExp(`^\\s*${opening[1][0]}{${opening[1].length},}\\s*$`).test(last.text);
|
|
288
|
+
const codeFrom = fenced ? Math.min(first.to + 1, state.doc.length) : first.from;
|
|
289
|
+
const codeTo = closed ? Math.max(codeFrom, last.from - 1) : last.to;
|
|
290
|
+
const code = state.sliceDoc(codeFrom, codeTo);
|
|
291
|
+
for (let n = first.number; n <= last.number; n++) {
|
|
292
|
+
const line = state.doc.line(n);
|
|
293
|
+
const edge = `${n === first.number ? "cm-lp-code-first" : ""} ${n === last.number ? "cm-lp-code-last" : ""}`;
|
|
294
|
+
ranges.push(codeLine(edge).range(line.from));
|
|
295
|
+
if (!fenced && !isRevealed(state, line.from, line.to)) {
|
|
296
|
+
const indent = /^(?: {4}|\t)/.exec(line.text);
|
|
297
|
+
if (indent) ranges.push(hide.range(line.from, line.from + indent[0].length));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (fenced && !isRevealed(state, first.from, first.to)) {
|
|
301
|
+
ranges.push(Decoration.replace({
|
|
302
|
+
widget: new CodeHeader(opening?.[2] ?? "", code)
|
|
303
|
+
}).range(first.from, first.to));
|
|
304
|
+
} else if (!fenced) {
|
|
305
|
+
ranges.push(Decoration.widget({
|
|
306
|
+
widget: new CodeHeader("", code.replace(/^(?: {4}|\t)/gm, "")),
|
|
307
|
+
side: -1
|
|
308
|
+
}).range(first.from));
|
|
309
|
+
}
|
|
310
|
+
if (closed && !isRevealed(state, last.from, last.to)) {
|
|
311
|
+
ranges.push(Decoration.line({
|
|
312
|
+
class: "cm-lp-code-fence-end"
|
|
313
|
+
}).range(last.from));
|
|
314
|
+
ranges.push(hide.range(last.from, last.to));
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
if (node.name === "Blockquote") {
|
|
319
|
+
if (node.node.parent?.name === "Blockquote") return;
|
|
320
|
+
const first = state.doc.lineAt(node.from),
|
|
321
|
+
last = state.doc.lineAt(node.to);
|
|
322
|
+
const match = calloutHeader(first.text);
|
|
323
|
+
if (!match) return;
|
|
324
|
+
const type = match[1].toLowerCase() in types ? match[1].toLowerCase() : "note";
|
|
325
|
+
const title = match[3] || typeTitle(type);
|
|
326
|
+
const folded = match[2] ? foldState.get(first.from) ?? match[2] === "-" : false;
|
|
327
|
+
for (let n = first.number; n <= last.number; n++) {
|
|
328
|
+
const line = state.doc.line(n);
|
|
329
|
+
ranges.push(Decoration.line({
|
|
330
|
+
class: `cm-lp-callout cm-lp-callout-${type}${n === first.number ? " cm-lp-callout-first" : ""}${n === last.number || folded && n === first.number ? " cm-lp-callout-last" : ""}`
|
|
331
|
+
}).range(line.from));
|
|
332
|
+
}
|
|
333
|
+
if (!isRevealed(state, first.from, first.to)) ranges.push(Decoration.replace({
|
|
334
|
+
widget: new CalloutHeader(type, title, first.from, !!match[2], folded)
|
|
335
|
+
}).range(first.from, first.to));
|
|
336
|
+
if (folded && first.number < last.number && !isRevealed(state, first.to + 1, last.to)) {
|
|
337
|
+
ranges.push(Decoration.replace({
|
|
338
|
+
block: true
|
|
339
|
+
}).range(first.to + 1, last.to));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
const definitions = new Map();
|
|
345
|
+
for (let n = 1; n <= state.doc.lines; n++) {
|
|
346
|
+
const line = state.doc.line(n);
|
|
347
|
+
if (skip.some(part => line.from >= part.from && line.from <= part.to)) continue;
|
|
348
|
+
const def = /^ {0,3}\[\^([^\]\s]+)\]:\s*(.*)$/.exec(line.text);
|
|
349
|
+
if (def) {
|
|
350
|
+
definitions.set(def[1].toLowerCase(), def[2]);
|
|
351
|
+
ranges.push(Decoration.line({
|
|
352
|
+
class: "cm-lp-footnote-definition"
|
|
353
|
+
}).range(line.from));
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (let n = 1; n <= state.doc.lines; n++) {
|
|
358
|
+
const line = state.doc.line(n);
|
|
359
|
+
if (skip.some(part => line.from >= part.from && line.from <= part.to) || /^ {0,3}\[\^[^\]]+\]:/.test(line.text)) continue;
|
|
360
|
+
for (const match of line.text.matchAll(/(?<![\\!])\[\^([^\]\s]+)\]/g)) {
|
|
361
|
+
const from = line.from + match.index,
|
|
362
|
+
to = from + match[0].length;
|
|
363
|
+
const node = syntaxTree(state).resolveInner(from, 1);
|
|
364
|
+
if (["InlineCode", "FencedCode", "CodeBlock", "HTMLBlock"].some(name => {
|
|
365
|
+
for (let p = node; p; p = p.parent) if (p.name === name) return true;
|
|
366
|
+
return false;
|
|
367
|
+
})) continue;
|
|
368
|
+
if (!isRevealed(state, from, to)) {
|
|
369
|
+
const text = definitions.get(match[1].toLowerCase());
|
|
370
|
+
ranges.push((text ? Decoration.mark({
|
|
371
|
+
class: "cm-lp-footnote-ref",
|
|
372
|
+
attributes: {
|
|
373
|
+
title: text
|
|
374
|
+
}
|
|
375
|
+
}) : footnote).range(from + 2, to - 1));
|
|
376
|
+
ranges.push(hide.range(from, from + 2), hide.range(to - 1, to));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return Decoration.set(ranges, true);
|
|
381
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { EditorState } from "@codemirror/state";
|
|
2
|
+
import { ViewPlugin, type DecorationSet, type ViewUpdate } from "@codemirror/view";
|
|
3
|
+
export interface LivePreviewLink {
|
|
4
|
+
/** Start of the whole link source, including `[[` for a wikilink. */
|
|
5
|
+
from: number;
|
|
6
|
+
to: number;
|
|
7
|
+
target: string;
|
|
8
|
+
format: "markdown" | "wikilink";
|
|
9
|
+
}
|
|
10
|
+
export interface LivePreviewOptions {
|
|
11
|
+
/** Whether a rendered link points nowhere. The app owns link resolution. */
|
|
12
|
+
linkMissing?: (state: EditorState, link: LivePreviewLink) => boolean;
|
|
13
|
+
/** Rebuild when a transaction carries app state that changes `linkMissing`. */
|
|
14
|
+
refresh?: (update: ViewUpdate) => boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Rendered Markdown: markers hide away from the selection, and lists, tasks and links read as written. */
|
|
17
|
+
export declare function inlinePreview(options: LivePreviewOptions): ViewPlugin<{
|
|
18
|
+
decorations: DecorationSet;
|
|
19
|
+
atomic: DecorationSet;
|
|
20
|
+
}>;
|