@humanonlyweb/writer 0.0.0-stage → 0.1.1

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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -2
  3. package/dist/components/WriterContent.d.ts +57 -0
  4. package/dist/components/WriterContent.js +114 -0
  5. package/dist/components/WriterEditor.js +232 -0
  6. package/dist/components/WriterEditor.vue.d.ts +70 -0
  7. package/dist/components/WriterToolbar.js +135 -0
  8. package/dist/components/WriterToolbar.vue.d.ts +27 -0
  9. package/dist/config.d.ts +39 -0
  10. package/dist/config.js +41 -0
  11. package/dist/docs/README.md +24 -0
  12. package/dist/docs/blocks.md +64 -0
  13. package/dist/docs/changelog.md +17 -0
  14. package/dist/docs/components/content.md +51 -0
  15. package/dist/docs/components/editor.md +236 -0
  16. package/dist/docs/components/toolbar.md +146 -0
  17. package/dist/docs/images.md +80 -0
  18. package/dist/docs/index.md +86 -0
  19. package/dist/docs/installation.md +74 -0
  20. package/dist/docs/limitations.md +11 -0
  21. package/dist/docs/localisation.md +35 -0
  22. package/dist/docs/parsing.md +47 -0
  23. package/dist/docs/security.md +16 -0
  24. package/dist/docs/storing.md +105 -0
  25. package/dist/docs/styling.md +47 -0
  26. package/dist/docs/word-count.md +16 -0
  27. package/dist/editor/commands.d.ts +1 -0
  28. package/dist/editor/commands.js +359 -0
  29. package/dist/editor/images.d.ts +1 -0
  30. package/dist/editor/images.js +400 -0
  31. package/dist/editor/live.d.ts +2 -0
  32. package/dist/editor/live.js +411 -0
  33. package/dist/editor/setup.d.ts +9 -0
  34. package/dist/editor/setup.js +485 -0
  35. package/dist/index.d.ts +11 -0
  36. package/dist/index.js +8 -0
  37. package/dist/load-parser.js +12 -0
  38. package/dist/nuxt/module.d.ts +35 -0
  39. package/dist/nuxt/module.js +114 -0
  40. package/dist/nuxt/runtime/plugin.d.ts +2 -0
  41. package/dist/nuxt/runtime/plugin.js +25 -0
  42. package/dist/nuxt/runtime/server/parse.d.ts +12 -0
  43. package/dist/nuxt/runtime/server/parse.js +15 -0
  44. package/dist/package.js +3 -0
  45. package/dist/parse.d.ts +16 -0
  46. package/dist/parse.js +62 -0
  47. package/dist/parser-options.d.ts +19 -0
  48. package/dist/placeholder.js +5 -0
  49. package/dist/render.d.ts +10 -0
  50. package/dist/render.js +97 -0
  51. package/dist/sanitize.d.ts +11 -0
  52. package/dist/sanitize.js +139 -0
  53. package/dist/stats.d.ts +8 -0
  54. package/dist/stats.js +17 -0
  55. package/dist/styles/core.css +275 -0
  56. package/dist/translations.d.ts +26 -0
  57. package/dist/translations.js +47 -0
  58. package/dist/types.d.ts +9 -0
  59. package/package.json +101 -4
@@ -0,0 +1,411 @@
1
+ import { ImageCardWidget, parseImageLine } from "./images.js";
2
+ import { syntaxTree } from "@codemirror/language";
3
+ import { EditorSelection, EditorState, Prec, StateEffect, StateField } from "@codemirror/state";
4
+ import { Decoration, EditorView, ViewPlugin, WidgetType, keymap } from "@codemirror/view";
5
+ // src/editor/live.ts
6
+ const FENCE = /^\s{0,3}(`{3,}|~{3,})/;
7
+ const OPENER = /^\s*(:{2,})[a-z][\w-]*/i;
8
+ const CLOSER = /^\s*(:{2,})\s*$/;
9
+ /** Columns of leading whitespace, tabs to the next multiple of four. */
10
+ function indentOf(text) {
11
+ let columns = 0;
12
+ for (const char of text) if (char === " ") columns++;
13
+ else if (char === " ") columns += 4 - columns % 4;
14
+ else break;
15
+ return columns;
16
+ }
17
+ /** Top-level `::block … ::` ranges and lone image lines, skipping fenced code. */
18
+ function findPreviews(doc) {
19
+ const previews = [];
20
+ const open = [];
21
+ let fence;
22
+ for (let n = 1; n <= doc.lines; n++) {
23
+ const line = doc.line(n);
24
+ const text = line.text;
25
+ const marker = FENCE.exec(text)?.[1];
26
+ if (fence) {
27
+ if (!!marker && marker[0] === fence[0] && marker.length >= fence.length && !text.slice(text.indexOf(marker) + marker.length).trim()) fence = void 0;
28
+ continue;
29
+ }
30
+ if (marker) {
31
+ fence = marker;
32
+ continue;
33
+ }
34
+ if (!open.length && indentOf(text) >= 4) continue;
35
+ const closer = CLOSER.exec(text)?.[1];
36
+ if (closer) {
37
+ const index = open.findLastIndex((block) => block.colons === closer.length);
38
+ if (index < 0) continue;
39
+ const [block] = open.splice(index);
40
+ if (index === 0 && block) previews.push({
41
+ kind: "block",
42
+ from: block.from,
43
+ to: line.to
44
+ });
45
+ continue;
46
+ }
47
+ const opener = OPENER.exec(text)?.[1];
48
+ if (opener) {
49
+ open.push({
50
+ colons: opener.length,
51
+ from: line.from
52
+ });
53
+ continue;
54
+ }
55
+ if (!open.length && text.includes("![") && parseImageLine(text)) previews.push({
56
+ kind: "image",
57
+ from: line.from,
58
+ to: line.to
59
+ });
60
+ }
61
+ return previews;
62
+ }
63
+ const setFocused = StateEffect.define();
64
+ /**
65
+ * Whether the author is in the editor. Away, everything shows as published: blocks
66
+ * close and cards deselect. The selection is kept for when they come back. Part of the
67
+ * editor itself, not of `livePreview`, so turning live preview on knows the answer.
68
+ */
69
+ const focused = StateField.define({
70
+ create: () => false,
71
+ update(value, tr) {
72
+ for (const effect of tr.effects) if (effect.is(setFocused)) value = effect.value;
73
+ return value;
74
+ },
75
+ provide: () => EditorView.focusChangeEffect.of((_, focusing) => focusing || document.hasFocus() ? setFocused.of(focusing) : null)
76
+ });
77
+ /** Selection ranges in effect: none while the author is elsewhere. */
78
+ const activeRanges = (state) => state.field(focused) ? state.selection.ranges : [];
79
+ const touches = (state, from, to) => activeRanges(state).some((range) => range.from <= to && range.to >= from);
80
+ const cleanups = /* @__PURE__ */ new WeakMap();
81
+ var BlockWidget = class extends WidgetType {
82
+ source;
83
+ render;
84
+ constructor(source, render) {
85
+ super();
86
+ this.source = source;
87
+ this.render = render;
88
+ }
89
+ eq(other) {
90
+ return other.source === this.source;
91
+ }
92
+ toDOM(view) {
93
+ const wrapper = document.createElement("div");
94
+ wrapper.className = "cm-md-preview";
95
+ wrapper.dataset.part = "writer-preview";
96
+ wrapper.addEventListener("mousedown", (event) => {
97
+ event.preventDefault();
98
+ const opener = view.state.doc.lineAt(view.posAtDOM(wrapper));
99
+ const anchor = Math.min(opener.to + 1, view.state.doc.length);
100
+ view.dispatch({ selection: { anchor } });
101
+ view.focus();
102
+ });
103
+ cleanups.set(wrapper, this.render(this.source, wrapper).then((cleanup) => {
104
+ view.requestMeasure();
105
+ return cleanup;
106
+ }, (error) => {
107
+ console.error("[writer] couldn't render a block preview:", error);
108
+ return () => {};
109
+ }));
110
+ return wrapper;
111
+ }
112
+ destroy(dom) {
113
+ cleanups.get(dom)?.then((cleanup) => cleanup());
114
+ }
115
+ ignoreEvent() {
116
+ return true;
117
+ }
118
+ };
119
+ const openLine = Decoration.line({ class: "cm-md-block-open" });
120
+ const fenceLine = Decoration.line({ class: "cm-md-block-open cm-md-block-fence" });
121
+ function previewDecorations(state, previews, options) {
122
+ const ranges = [];
123
+ for (const { kind, from, to } of previews) {
124
+ const source = state.sliceDoc(from, to);
125
+ if (kind === "image") {
126
+ const widget = new ImageCardWidget(source, touches(state, from, to), options);
127
+ ranges.push(Decoration.replace({
128
+ widget,
129
+ block: true
130
+ }).range(from, to));
131
+ } else if (touches(state, from, to)) {
132
+ const first = state.doc.lineAt(from).number;
133
+ const last = state.doc.lineAt(to).number;
134
+ for (let n = first; n <= last; n++) ranges.push((n === first || n === last ? fenceLine : openLine).range(state.doc.line(n).from));
135
+ } else {
136
+ const widget = new BlockWidget(source, options.render);
137
+ ranges.push(Decoration.replace({
138
+ widget,
139
+ block: true
140
+ }).range(from, to));
141
+ }
142
+ }
143
+ return Decoration.set(ranges, true);
144
+ }
145
+ function previewField(options) {
146
+ return StateField.define({
147
+ create(state) {
148
+ const previews = findPreviews(state.doc);
149
+ return {
150
+ previews,
151
+ decorations: previewDecorations(state, previews, options)
152
+ };
153
+ },
154
+ update(value, tr) {
155
+ if (!tr.docChanged && !tr.selection && tr.startState.field(focused) === tr.state.field(focused)) return value;
156
+ const previews = tr.docChanged ? findPreviews(tr.state.doc) : value.previews;
157
+ return {
158
+ previews,
159
+ decorations: previewDecorations(tr.state, previews, options)
160
+ };
161
+ },
162
+ provide: (field) => EditorView.decorations.from(field, (value) => value.decorations)
163
+ });
164
+ }
165
+ const hide = Decoration.replace({});
166
+ var BulletWidget = class extends WidgetType {
167
+ eq() {
168
+ return true;
169
+ }
170
+ toDOM() {
171
+ const span = document.createElement("span");
172
+ span.className = "cm-md-bullet";
173
+ span.textContent = "•";
174
+ return span;
175
+ }
176
+ };
177
+ var CheckboxWidget = class extends WidgetType {
178
+ checked;
179
+ label;
180
+ constructor(checked, label) {
181
+ super();
182
+ this.checked = checked;
183
+ this.label = label;
184
+ }
185
+ eq(other) {
186
+ return other.checked === this.checked && other.label === this.label;
187
+ }
188
+ toDOM(view) {
189
+ const box = document.createElement("input");
190
+ box.type = "checkbox";
191
+ box.className = "cm-md-task";
192
+ box.checked = this.checked;
193
+ box.tabIndex = -1;
194
+ box.setAttribute("aria-label", this.label);
195
+ box.addEventListener("mousedown", (event) => event.preventDefault());
196
+ box.addEventListener("click", (event) => {
197
+ event.preventDefault();
198
+ if (view.state.readOnly) return;
199
+ const at = view.posAtDOM(box);
200
+ if (!/^\[[ xX]\]$/.test(view.state.sliceDoc(at, at + 3))) return;
201
+ view.dispatch({
202
+ changes: {
203
+ from: at + 1,
204
+ to: at + 2,
205
+ insert: this.checked ? " " : "x"
206
+ },
207
+ userEvent: "input.toggle"
208
+ });
209
+ });
210
+ return box;
211
+ }
212
+ ignoreEvent() {
213
+ return true;
214
+ }
215
+ };
216
+ const bullet = Decoration.replace({ widget: new BulletWidget() });
217
+ /** A task's text without Markdown marks or URLs, for its checkbox's name. */
218
+ const taskLabel = (text) => text.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/[*_~`]/g, "").trim();
219
+ const quoteLine = Decoration.line({ class: "cm-md-quote-line" });
220
+ const ruleLine = Decoration.line({ class: "cm-md-hr" });
221
+ const INLINE = /* @__PURE__ */ new Set([
222
+ "Emphasis",
223
+ "StrongEmphasis",
224
+ "Strikethrough",
225
+ "InlineCode",
226
+ "Link"
227
+ ]);
228
+ function inlineDecorations(view, previews, task) {
229
+ const { state } = view;
230
+ const ranges = [];
231
+ const cursorLines = /* @__PURE__ */ new Set();
232
+ for (const range of activeRanges(state)) {
233
+ const last = state.doc.lineAt(range.to).number;
234
+ for (let n = state.doc.lineAt(range.from).number; n <= last; n++) cursorLines.add(n);
235
+ }
236
+ const onCursorLine = (pos) => cursorLines.has(state.doc.lineAt(pos).number);
237
+ const open = previews.filter((p) => p.kind === "block" && touches(state, p.from, p.to));
238
+ const inOpenBlock = (pos) => open.some((p) => pos >= p.from && pos <= p.to);
239
+ const withSpace = (to) => state.sliceDoc(to, to + 1) === " " ? to + 1 : to;
240
+ for (const { from, to } of view.visibleRanges) syntaxTree(state).iterate({
241
+ from,
242
+ to,
243
+ enter(node) {
244
+ if (inOpenBlock(node.from)) return false;
245
+ const name = node.name;
246
+ if (name === "FencedCode" || name === "CodeBlock" || name === "Image") return false;
247
+ if (name === "Blockquote") {
248
+ const last = state.doc.lineAt(node.to).number;
249
+ for (let n = state.doc.lineAt(node.from).number; n <= last; n++) ranges.push(quoteLine.range(state.doc.line(n).from));
250
+ return;
251
+ }
252
+ if (name === "HorizontalRule") {
253
+ if (!onCursorLine(node.from)) {
254
+ ranges.push(ruleLine.range(state.doc.lineAt(node.from).from));
255
+ ranges.push(hide.range(node.from, node.to));
256
+ }
257
+ return false;
258
+ }
259
+ if (INLINE.has(name)) {
260
+ if (touches(state, node.from, node.to)) return void 0;
261
+ if (name === "Link") {
262
+ if (!node.node.getChild("URL")) return false;
263
+ for (let child = node.node.firstChild; child; child = child.nextSibling) {
264
+ if (child.name !== "LinkMark") continue;
265
+ if (state.sliceDoc(child.from, child.to) === "(") {
266
+ ranges.push(hide.range(child.from, node.to));
267
+ break;
268
+ }
269
+ ranges.push(hide.range(child.from, child.to));
270
+ }
271
+ return false;
272
+ }
273
+ for (let child = node.node.firstChild; child; child = child.nextSibling) if (child.name.endsWith("Mark") && child.name !== "LinkMark") ranges.push(hide.range(child.from, child.to));
274
+ return;
275
+ }
276
+ if (onCursorLine(node.from)) return void 0;
277
+ switch (name) {
278
+ case "HeaderMark":
279
+ if (node.node.parent?.name.startsWith("ATXHeading")) ranges.push(hide.range(node.from, withSpace(node.to)));
280
+ return;
281
+ case "QuoteMark":
282
+ ranges.push(hide.range(node.from, withSpace(node.to)));
283
+ return;
284
+ case "ListMark":
285
+ if (/^[-*+]$/.test(state.sliceDoc(node.from, node.to))) {
286
+ if (node.node.nextSibling?.name === "Task") ranges.push(hide.range(node.from, withSpace(node.to)));
287
+ else ranges.push(bullet.range(node.from, node.to));
288
+ }
289
+ return;
290
+ case "HardBreak":
291
+ if (state.sliceDoc(node.from, node.from + 1) === "\\") ranges.push(hide.range(node.from, node.from + 1));
292
+ return;
293
+ case "TaskMarker": {
294
+ const widget = new CheckboxWidget(/x/i.test(state.sliceDoc(node.from, node.to)), taskLabel(state.sliceDoc(node.to, state.doc.lineAt(node.to).to)) || task);
295
+ ranges.push(Decoration.replace({ widget }).range(node.from, node.to));
296
+ return;
297
+ }
298
+ default: return;
299
+ }
300
+ }
301
+ });
302
+ return Decoration.set(ranges, true);
303
+ }
304
+ const imageAt = (previews, pos) => previews.find((p) => p.kind === "image" && pos >= p.from && pos <= p.to);
305
+ function cardKeys(field) {
306
+ const selectedImage = (state) => {
307
+ const { from, to } = state.selection.main;
308
+ return state.field(field).previews.find((p) => p.kind === "image" && p.from === from && p.to === to);
309
+ };
310
+ const removeOrSelect = (direction) => (view) => {
311
+ const { state } = view;
312
+ if (state.readOnly) return false;
313
+ const selected = selectedImage(state);
314
+ if (selected) {
315
+ const to = Math.min(selected.to + 1, state.doc.length);
316
+ view.dispatch({
317
+ changes: {
318
+ from: selected.from,
319
+ to
320
+ },
321
+ userEvent: "delete.image"
322
+ });
323
+ return true;
324
+ }
325
+ const { main } = state.selection;
326
+ if (!main.empty) return false;
327
+ const line = state.doc.lineAt(main.head);
328
+ if (direction < 0 ? main.head !== line.from : main.head !== line.to) return false;
329
+ const neighbour = direction < 0 ? line.from - 1 : line.to + 1;
330
+ if (neighbour < 0 || neighbour > state.doc.length) return false;
331
+ const image = imageAt(state.field(field).previews, neighbour);
332
+ if (!image) return false;
333
+ view.dispatch({ selection: {
334
+ anchor: image.from,
335
+ head: image.to
336
+ } });
337
+ return true;
338
+ };
339
+ return Prec.high(keymap.of([
340
+ {
341
+ key: "Backspace",
342
+ run: removeOrSelect(-1)
343
+ },
344
+ {
345
+ key: "Delete",
346
+ run: removeOrSelect(1)
347
+ },
348
+ {
349
+ key: "Enter",
350
+ run: (view) => {
351
+ const selected = !view.state.readOnly && selectedImage(view.state);
352
+ if (!selected) return false;
353
+ view.dispatch({
354
+ changes: {
355
+ from: selected.to,
356
+ insert: "\n"
357
+ },
358
+ selection: { anchor: selected.to + 1 },
359
+ userEvent: "input"
360
+ });
361
+ return true;
362
+ }
363
+ }
364
+ ]));
365
+ }
366
+ /**
367
+ * Arrow keys step into rendered things instead of over them: onto an image card selects
368
+ * it, onto a block opens it with the cursor at its first or last line of content. A
369
+ * cursor never rests inside a card's line, where typing would break the image.
370
+ */
371
+ function stepIntoPreviews(field) {
372
+ return EditorState.transactionFilter.of((tr) => {
373
+ if (tr.docChanged || !tr.selection || !tr.selection.main.empty) return tr;
374
+ const before = tr.startState.selection.main.head;
375
+ const after = tr.selection.main.head;
376
+ const previews = tr.startState.field(field).previews;
377
+ const keyboard = !tr.isUserEvent("select.pointer");
378
+ const down = after > before;
379
+ const target = previews.find((p) => after >= p.from && after <= p.to || keyboard && (down ? before < p.from && after > p.to : before > p.to && after < p.from));
380
+ if (!target || before >= target.from && before <= target.to && target.kind === "block") return tr;
381
+ const { doc } = tr.startState;
382
+ let selection;
383
+ if (target.kind === "image") selection = down ? EditorSelection.range(target.from, target.to) : EditorSelection.range(target.to, target.from);
384
+ else if (keyboard && (after < target.from || after > target.to || after === target.from || after === target.to)) {
385
+ const content = down ? doc.lineAt(target.from).to + 1 : doc.lineAt(target.to).from - 1;
386
+ selection = EditorSelection.cursor(Math.max(target.from, Math.min(content, target.to)));
387
+ } else return tr;
388
+ return [tr, {
389
+ selection,
390
+ sequential: true
391
+ }];
392
+ });
393
+ }
394
+ function livePreview(options) {
395
+ const field = previewField(options);
396
+ return [
397
+ field,
398
+ ViewPlugin.fromClass(class {
399
+ decorations;
400
+ constructor(view) {
401
+ this.decorations = inlineDecorations(view, view.state.field(field).previews, options.translations.task);
402
+ }
403
+ update(update) {
404
+ if (update.docChanged || update.selectionSet || update.viewportChanged || update.state.field(focused) !== update.startState.field(focused) || syntaxTree(update.state) !== syntaxTree(update.startState)) this.decorations = inlineDecorations(update.view, update.state.field(field).previews, options.translations.task);
405
+ }
406
+ }, { decorations: (plugin) => plugin.decorations }),
407
+ cardKeys(field),
408
+ stepIntoPreviews(field)
409
+ ];
410
+ }
411
+ export { findPreviews, focused, livePreview };
@@ -0,0 +1,9 @@
1
+ import "./commands.js";
2
+ import "./live.js";
3
+ import { LanguageDescription } from "@codemirror/language";
4
+ import { Extension } from "@codemirror/state";
5
+ import { EditorView } from "@codemirror/view";
6
+ export interface LinkSuggestion {
7
+ label: string;
8
+ url: string;
9
+ }