@humanonlyweb/writer 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 +355 -2
- package/dist/components/WriterContent.d.ts +57 -0
- package/dist/components/WriterContent.js +114 -0
- package/dist/components/WriterEditor.js +232 -0
- package/dist/components/WriterEditor.vue.d.ts +70 -0
- package/dist/components/WriterToolbar.js +135 -0
- package/dist/components/WriterToolbar.vue.d.ts +27 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.js +41 -0
- package/dist/editor/commands.d.ts +1 -0
- package/dist/editor/commands.js +294 -0
- package/dist/editor/images.d.ts +1 -0
- package/dist/editor/images.js +388 -0
- package/dist/editor/live.d.ts +2 -0
- package/dist/editor/live.js +410 -0
- package/dist/editor/setup.d.ts +9 -0
- package/dist/editor/setup.js +466 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +8 -0
- package/dist/load-parser.js +12 -0
- package/dist/nuxt/module.d.ts +38 -0
- package/dist/nuxt/module.js +92 -0
- package/dist/nuxt/runtime/plugin.d.ts +2 -0
- package/dist/nuxt/runtime/plugin.js +24 -0
- package/dist/nuxt/runtime/server/parse.d.ts +12 -0
- package/dist/nuxt/runtime/server/parse.js +15 -0
- package/dist/package.js +3 -0
- package/dist/parse.d.ts +31 -0
- package/dist/parse.js +62 -0
- package/dist/placeholder.js +5 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.js +97 -0
- package/dist/sanitize.d.ts +11 -0
- package/dist/sanitize.js +139 -0
- package/dist/stats.d.ts +8 -0
- package/dist/stats.js +17 -0
- package/dist/styles/core.css +275 -0
- package/dist/translations.d.ts +26 -0
- package/dist/translations.js +47 -0
- package/dist/types.d.ts +9 -0
- package/docs/api.md +258 -0
- package/package.json +96 -4
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { activeCommands, commands, insertBlock, lineBreak, newParagraph } from "./commands.js";
|
|
2
|
+
import { uploadFiles } from "./images.js";
|
|
3
|
+
import { focused, livePreview } from "./live.js";
|
|
4
|
+
import { autocompletion, completionKeymap, snippet } from "@codemirror/autocomplete";
|
|
5
|
+
import { defaultKeymap, history, historyKeymap, redo, redoDepth, undo, undoDepth } from "@codemirror/commands";
|
|
6
|
+
import { markdownKeymap, markdownLanguage } from "@codemirror/lang-markdown";
|
|
7
|
+
import { HighlightStyle, Language, LanguageDescription, LanguageSupport, ParseContext, syntaxHighlighting, syntaxTree } from "@codemirror/language";
|
|
8
|
+
import { Compartment, EditorSelection, EditorState, Prec } from "@codemirror/state";
|
|
9
|
+
import { Decoration, EditorView, ViewPlugin, keymap, placeholder } from "@codemirror/view";
|
|
10
|
+
import { tags } from "@lezer/highlight";
|
|
11
|
+
import { MarkdownParser, parseCode } from "@lezer/markdown";
|
|
12
|
+
// src/editor/setup.ts
|
|
13
|
+
const highlightStyle = HighlightStyle.define([
|
|
14
|
+
{
|
|
15
|
+
tag: tags.strong,
|
|
16
|
+
class: "cm-md-strong"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
tag: tags.emphasis,
|
|
20
|
+
class: "cm-md-em"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
tag: tags.strikethrough,
|
|
24
|
+
class: "cm-md-strike"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
tag: tags.monospace,
|
|
28
|
+
class: "cm-md-code"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
tag: [tags.link, tags.url],
|
|
32
|
+
class: "cm-md-link"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
tag: tags.quote,
|
|
36
|
+
class: "cm-md-quote"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
tag: [
|
|
40
|
+
tags.processingInstruction,
|
|
41
|
+
tags.meta,
|
|
42
|
+
tags.contentSeparator
|
|
43
|
+
],
|
|
44
|
+
class: "cm-md-mark"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
tag: tags.comment,
|
|
48
|
+
class: "cm-md-comment"
|
|
49
|
+
}
|
|
50
|
+
]);
|
|
51
|
+
const HEADING = /^(?:ATX|Setext)Heading([1-6])$/;
|
|
52
|
+
const codeLine = Decoration.line({ class: "cm-md-codeblock" });
|
|
53
|
+
/**
|
|
54
|
+
* Line classes for headings and code blocks, so their marks (`#`, fences) take the same
|
|
55
|
+
* size and font as their text. Visible lines only.
|
|
56
|
+
*/
|
|
57
|
+
const blockLines = ViewPlugin.fromClass(class {
|
|
58
|
+
decorations;
|
|
59
|
+
constructor(view) {
|
|
60
|
+
this.decorations = this.build(view);
|
|
61
|
+
}
|
|
62
|
+
update(update) {
|
|
63
|
+
if (update.docChanged || update.viewportChanged || syntaxTree(update.state) !== syntaxTree(update.startState)) this.decorations = this.build(update.view);
|
|
64
|
+
}
|
|
65
|
+
build(view) {
|
|
66
|
+
const lines = [];
|
|
67
|
+
for (const { from, to } of view.visibleRanges) syntaxTree(view.state).iterate({
|
|
68
|
+
from,
|
|
69
|
+
to,
|
|
70
|
+
enter(node) {
|
|
71
|
+
const { doc } = view.state;
|
|
72
|
+
if (node.name === "FencedCode" || node.name === "CodeBlock") {
|
|
73
|
+
const last = doc.lineAt(node.to).number;
|
|
74
|
+
for (let n = doc.lineAt(node.from).number; n <= last; n++) lines.push(codeLine.range(doc.line(n).from));
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const level = HEADING.exec(node.name)?.[1];
|
|
78
|
+
if (!level) return void 0;
|
|
79
|
+
const line = doc.lineAt(node.from);
|
|
80
|
+
lines.push(Decoration.line({ class: `cm-md-h${level}` }).range(line.from));
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return Decoration.set(lines, true);
|
|
85
|
+
}
|
|
86
|
+
}, { decorations: (plugin) => plugin.decorations });
|
|
87
|
+
/**
|
|
88
|
+
* GFM Markdown, with Enter continuing lists and quotes. Not lang-markdown's `markdown()`:
|
|
89
|
+
* it always bundles HTML, CSS and JavaScript support (a third of the editor) for raw
|
|
90
|
+
* HTML, which Comark shows as text anyway.
|
|
91
|
+
*/
|
|
92
|
+
function markdownSupport(codeLanguages) {
|
|
93
|
+
const base = markdownLanguage.parser;
|
|
94
|
+
const language = codeLanguages.length && base instanceof MarkdownParser ? new Language(markdownLanguage.data, base.configure(parseCode({ codeParser(info) {
|
|
95
|
+
const name = /\S*/.exec(info)?.[0] ?? "";
|
|
96
|
+
const found = LanguageDescription.matchLanguageName(codeLanguages, name, true);
|
|
97
|
+
if (!found) return null;
|
|
98
|
+
return found.support ? found.support.language.parser : ParseContext.getSkippingParser(found.load());
|
|
99
|
+
} })), [], "markdown") : markdownLanguage;
|
|
100
|
+
return new LanguageSupport(language, Prec.high(keymap.of(markdownKeymap)));
|
|
101
|
+
}
|
|
102
|
+
const theme = EditorView.theme({
|
|
103
|
+
"&": {
|
|
104
|
+
color: "inherit",
|
|
105
|
+
backgroundColor: "transparent",
|
|
106
|
+
font: "inherit"
|
|
107
|
+
},
|
|
108
|
+
"&.cm-focused": { outline: "none" },
|
|
109
|
+
".cm-scroller": {
|
|
110
|
+
fontFamily: "inherit",
|
|
111
|
+
lineHeight: "inherit"
|
|
112
|
+
},
|
|
113
|
+
".cm-content": {
|
|
114
|
+
caretColor: "currentColor",
|
|
115
|
+
padding: "0"
|
|
116
|
+
},
|
|
117
|
+
".cm-line": { padding: "0" },
|
|
118
|
+
".cm-line.cm-md-quote-line": {
|
|
119
|
+
paddingInlineStart: "var(--writer-quote-indent, 1em)",
|
|
120
|
+
boxShadow: "inset var(--writer-quote-border-width, 3px) 0 var(--writer-quote-border-color, color-mix(in srgb, currentColor 25%, transparent))"
|
|
121
|
+
},
|
|
122
|
+
".cm-cursor, .cm-dropCursor": { borderLeftColor: "currentColor" },
|
|
123
|
+
".cm-md-preview > [data-part='writer-content'] > :first-child, .cm-md-image figure": { marginBlockStart: "0 !important" },
|
|
124
|
+
".cm-md-preview > [data-part='writer-content'] > :last-child, .cm-md-image figure": { marginBlockEnd: "0 !important" },
|
|
125
|
+
".cm-line.cm-md-block-open": {
|
|
126
|
+
paddingInline: "var(--writer-block-inset, 0.75em)",
|
|
127
|
+
background: "var(--writer-block-background, color-mix(in srgb, currentColor 4%, transparent))"
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
const URL_PATTERN = /^https?:\/\/\S+$/;
|
|
131
|
+
const snapshot = (state) => ({
|
|
132
|
+
active: activeCommands(state),
|
|
133
|
+
undo: undoDepth(state) > 0,
|
|
134
|
+
redo: redoDepth(state) > 0
|
|
135
|
+
});
|
|
136
|
+
function createEditor(options) {
|
|
137
|
+
const readonly = new Compartment();
|
|
138
|
+
const live = new Compartment();
|
|
139
|
+
let liveOn = !!options.preview;
|
|
140
|
+
/**
|
|
141
|
+
* In live preview, an empty image card to drop or choose a file into (or type a URL);
|
|
142
|
+
* in source mode, the Markdown to fill in.
|
|
143
|
+
*/
|
|
144
|
+
const imageCommand = (state) => liveOn ? insertBlock("![]()\n")(state) : insertBlock("", 2)(state);
|
|
145
|
+
const attributes = new Compartment();
|
|
146
|
+
const blockNames = new Set(options.blocks.map((block) => block.name));
|
|
147
|
+
const { translations: t } = options;
|
|
148
|
+
function run(command) {
|
|
149
|
+
if (view.state.readOnly) return false;
|
|
150
|
+
const spec = command(view.state);
|
|
151
|
+
if (!spec) return false;
|
|
152
|
+
view.dispatch(view.state.update(spec, { scrollIntoView: true }));
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
let uploading = 0;
|
|
156
|
+
const uploadOptions = options.upload ? {
|
|
157
|
+
upload: options.upload,
|
|
158
|
+
onError: options.onUploadError,
|
|
159
|
+
onProgress: (change) => options.onUploading(uploading += change)
|
|
160
|
+
} : void 0;
|
|
161
|
+
/** Uploads files to the cursor (or `at`), each on its own line. */
|
|
162
|
+
function insertFiles(files, at = view.state.selection.main.head) {
|
|
163
|
+
if (!uploadOptions || view.state.readOnly || !files.length) return false;
|
|
164
|
+
uploadFiles(view, files, uploadOptions, { at });
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
const liveOptions = (render) => ({
|
|
168
|
+
render,
|
|
169
|
+
translations: t,
|
|
170
|
+
uploadInto: uploadOptions ? (target, file, range) => void uploadFiles(target, [file], uploadOptions, { replace: range }) : void 0
|
|
171
|
+
});
|
|
172
|
+
const slashItems = [
|
|
173
|
+
...[
|
|
174
|
+
"heading1",
|
|
175
|
+
"heading2",
|
|
176
|
+
"heading3"
|
|
177
|
+
].map((name, i) => ({
|
|
178
|
+
name,
|
|
179
|
+
label: t.commands[name],
|
|
180
|
+
snippet: `${"#".repeat(i + 1)} \${}`
|
|
181
|
+
})),
|
|
182
|
+
{
|
|
183
|
+
name: "bulletList",
|
|
184
|
+
label: t.commands.bulletList,
|
|
185
|
+
snippet: "- ${}"
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "orderedList",
|
|
189
|
+
label: t.commands.orderedList,
|
|
190
|
+
snippet: "1. ${}"
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: "taskList",
|
|
194
|
+
label: t.commands.taskList,
|
|
195
|
+
snippet: "- [ ] ${}"
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: "quote",
|
|
199
|
+
label: t.commands.quote,
|
|
200
|
+
snippet: "> ${}"
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: "codeBlock",
|
|
204
|
+
label: t.commands.codeBlock,
|
|
205
|
+
snippet: "```${language}\n${}\n```"
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "table",
|
|
209
|
+
label: t.commands.table,
|
|
210
|
+
snippet: "| ${Column} | ${Column} |\n| --- | --- |\n| ${} | |"
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "divider",
|
|
214
|
+
label: t.commands.divider,
|
|
215
|
+
snippet: "---\n${}"
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: "image",
|
|
219
|
+
label: t.commands.image,
|
|
220
|
+
snippet: ""
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
name: "footnote",
|
|
224
|
+
label: t.commands.footnote,
|
|
225
|
+
snippet: ""
|
|
226
|
+
},
|
|
227
|
+
...options.blocks
|
|
228
|
+
];
|
|
229
|
+
/** `/` on an otherwise empty line lists blocks to insert. */
|
|
230
|
+
const slash = (context) => {
|
|
231
|
+
const line = context.state.doc.lineAt(context.pos);
|
|
232
|
+
const match = /^\s*\/([\w-]*)$/.exec(line.text.slice(0, context.pos - line.from));
|
|
233
|
+
if (!match || line.to !== context.pos) return null;
|
|
234
|
+
const query = match[1];
|
|
235
|
+
return {
|
|
236
|
+
from: context.pos - query.length,
|
|
237
|
+
options: slashItems.map((item) => ({
|
|
238
|
+
label: item.label,
|
|
239
|
+
detail: blockNames.has(item.name) ? `::${item.name}` : void 0,
|
|
240
|
+
type: "text",
|
|
241
|
+
apply: (view, completion, start, end) => {
|
|
242
|
+
const command = item.name === "footnote" ? commands.footnote : item.name === "image" ? imageCommand : void 0;
|
|
243
|
+
if (!command) return snippet(item.snippet)(view, completion, start - 1, end);
|
|
244
|
+
const removeSlash = view.state.update({ changes: {
|
|
245
|
+
from: start - 1,
|
|
246
|
+
to: end
|
|
247
|
+
} });
|
|
248
|
+
const added = removeSlash.state.update(command(removeSlash.state) ?? {});
|
|
249
|
+
view.dispatch({
|
|
250
|
+
changes: removeSlash.changes.compose(added.changes),
|
|
251
|
+
selection: added.selection,
|
|
252
|
+
scrollIntoView: true,
|
|
253
|
+
userEvent: "input.insert"
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
})),
|
|
257
|
+
validFor: /^[\w-]*$/
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
/** `](` followed by a query suggests the app's pages. */
|
|
261
|
+
const linkSource = async (context) => {
|
|
262
|
+
const match = context.matchBefore(/\]\([^)\s]*/);
|
|
263
|
+
if (!match || !options.links) return null;
|
|
264
|
+
const query = match.text.slice(2);
|
|
265
|
+
const suggestions = await options.links(query);
|
|
266
|
+
if (context.aborted || !suggestions.length) return null;
|
|
267
|
+
return {
|
|
268
|
+
from: match.from + 2,
|
|
269
|
+
filter: false,
|
|
270
|
+
options: suggestions.map((item) => ({
|
|
271
|
+
label: item.label,
|
|
272
|
+
detail: item.url,
|
|
273
|
+
type: "text",
|
|
274
|
+
apply: item.url
|
|
275
|
+
}))
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
const formatting = [
|
|
279
|
+
{
|
|
280
|
+
key: "Mod-b",
|
|
281
|
+
run: () => run(commands.bold)
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
key: "Mod-i",
|
|
285
|
+
run: () => run(commands.italic)
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
key: "Mod-k",
|
|
289
|
+
run: () => run(commands.link)
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
key: "Mod-e",
|
|
293
|
+
run: () => run(commands.code)
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
key: "Mod-Shift-x",
|
|
297
|
+
run: () => run(commands.strikethrough)
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
key: "Mod-Alt-1",
|
|
301
|
+
run: () => run(commands.heading1)
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
key: "Mod-Alt-2",
|
|
305
|
+
run: () => run(commands.heading2)
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
key: "Mod-Alt-3",
|
|
309
|
+
run: () => run(commands.heading3)
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
key: "Mod-Shift-7",
|
|
313
|
+
run: () => run(commands.orderedList)
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
key: "Mod-Shift-8",
|
|
317
|
+
run: () => run(commands.bulletList)
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
key: "Mod-Shift-9",
|
|
321
|
+
run: () => run(commands.taskList)
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
key: "Mod-Alt-f",
|
|
325
|
+
run: () => run(commands.footnote)
|
|
326
|
+
}
|
|
327
|
+
];
|
|
328
|
+
let doc = options.doc;
|
|
329
|
+
const state = EditorState.create({
|
|
330
|
+
doc,
|
|
331
|
+
selection: options.selection ? EditorSelection.single(Math.min(options.selection.anchor, doc.length), Math.min(options.selection.head, doc.length)) : void 0,
|
|
332
|
+
extensions: [
|
|
333
|
+
history(),
|
|
334
|
+
Prec.high(keymap.of([{
|
|
335
|
+
key: "Enter",
|
|
336
|
+
run: () => run(newParagraph)
|
|
337
|
+
}, {
|
|
338
|
+
key: "Shift-Enter",
|
|
339
|
+
run: () => run(lineBreak)
|
|
340
|
+
}])),
|
|
341
|
+
markdownSupport(options.codeLanguages ?? []),
|
|
342
|
+
syntaxHighlighting(highlightStyle),
|
|
343
|
+
blockLines,
|
|
344
|
+
EditorView.lineWrapping,
|
|
345
|
+
EditorView.perLineTextDirection.of(true),
|
|
346
|
+
autocompletion({
|
|
347
|
+
override: [slash, linkSource],
|
|
348
|
+
icons: false
|
|
349
|
+
}),
|
|
350
|
+
readonly.of([EditorState.readOnly.of(options.readonly)]),
|
|
351
|
+
focused,
|
|
352
|
+
live.of(options.preview ? livePreview(liveOptions(options.preview)) : []),
|
|
353
|
+
options.placeholder ? placeholder(options.placeholder) : [],
|
|
354
|
+
attributes.of(EditorView.contentAttributes.of(options.attributes)),
|
|
355
|
+
keymap.of([
|
|
356
|
+
...formatting,
|
|
357
|
+
...completionKeymap,
|
|
358
|
+
...historyKeymap,
|
|
359
|
+
...defaultKeymap
|
|
360
|
+
]),
|
|
361
|
+
EditorView.domEventHandlers({
|
|
362
|
+
paste(event, view) {
|
|
363
|
+
const files = [...event.clipboardData?.files ?? []];
|
|
364
|
+
if (files.length && insertFiles(files)) {
|
|
365
|
+
event.preventDefault();
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
const text = event.clipboardData?.getData("text/plain").trim() ?? "";
|
|
369
|
+
if (!URL_PATTERN.test(text) || view.state.selection.ranges.some((r) => r.empty)) return false;
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
view.dispatch({
|
|
372
|
+
...view.state.changeByRange((range) => {
|
|
373
|
+
const insert = `[${view.state.sliceDoc(range.from, range.to)}](${text})`;
|
|
374
|
+
return {
|
|
375
|
+
changes: {
|
|
376
|
+
from: range.from,
|
|
377
|
+
to: range.to,
|
|
378
|
+
insert
|
|
379
|
+
},
|
|
380
|
+
range: EditorSelection.cursor(range.from + insert.length)
|
|
381
|
+
};
|
|
382
|
+
}),
|
|
383
|
+
userEvent: "input.paste"
|
|
384
|
+
});
|
|
385
|
+
return true;
|
|
386
|
+
},
|
|
387
|
+
drop(event, view) {
|
|
388
|
+
const files = [...event.dataTransfer?.files ?? []];
|
|
389
|
+
if (!files.length || !options.upload) return false;
|
|
390
|
+
const at = view.posAtCoords({
|
|
391
|
+
x: event.clientX,
|
|
392
|
+
y: event.clientY
|
|
393
|
+
});
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
return insertFiles(files, at ?? void 0);
|
|
396
|
+
},
|
|
397
|
+
focus: () => options.onFocus(true),
|
|
398
|
+
blur: () => options.onFocus(false)
|
|
399
|
+
}),
|
|
400
|
+
EditorView.updateListener.of((update) => {
|
|
401
|
+
if (update.docChanged) {
|
|
402
|
+
doc = update.state.doc.toString();
|
|
403
|
+
options.onChange(doc);
|
|
404
|
+
}
|
|
405
|
+
if (update.docChanged || update.selectionSet || syntaxTree(update.state) !== syntaxTree(update.startState)) options.onState(snapshot(update.state));
|
|
406
|
+
}),
|
|
407
|
+
theme,
|
|
408
|
+
options.extensions ?? []
|
|
409
|
+
]
|
|
410
|
+
});
|
|
411
|
+
const view = new EditorView({
|
|
412
|
+
state,
|
|
413
|
+
parent: options.parent
|
|
414
|
+
});
|
|
415
|
+
options.onState(snapshot(view.state));
|
|
416
|
+
return {
|
|
417
|
+
view,
|
|
418
|
+
run(name) {
|
|
419
|
+
if (name === "undo") return !view.state.readOnly && undo(view);
|
|
420
|
+
if (name === "redo") return !view.state.readOnly && redo(view);
|
|
421
|
+
return run(commands[name]);
|
|
422
|
+
},
|
|
423
|
+
insertFiles,
|
|
424
|
+
insert(text) {
|
|
425
|
+
if (view.state.readOnly) return;
|
|
426
|
+
view.dispatch(view.state.replaceSelection(text), {
|
|
427
|
+
scrollIntoView: true,
|
|
428
|
+
userEvent: "input"
|
|
429
|
+
});
|
|
430
|
+
},
|
|
431
|
+
/** The current Markdown, without serializing the document again. */
|
|
432
|
+
getDoc: () => doc,
|
|
433
|
+
/** Replaces only what differs, so the selection and undo history survive an outside change. */
|
|
434
|
+
setDoc(next) {
|
|
435
|
+
if (next === doc) return;
|
|
436
|
+
const current = doc;
|
|
437
|
+
let start = 0;
|
|
438
|
+
const max = Math.min(current.length, next.length);
|
|
439
|
+
while (start < max && current.charCodeAt(start) === next.charCodeAt(start)) start++;
|
|
440
|
+
let end = 0;
|
|
441
|
+
while (end < max - start && current.charCodeAt(current.length - 1 - end) === next.charCodeAt(next.length - 1 - end)) end++;
|
|
442
|
+
view.dispatch({
|
|
443
|
+
changes: {
|
|
444
|
+
from: start,
|
|
445
|
+
to: current.length - end,
|
|
446
|
+
insert: next.slice(start, next.length - end)
|
|
447
|
+
},
|
|
448
|
+
userEvent: "input.external"
|
|
449
|
+
});
|
|
450
|
+
},
|
|
451
|
+
/** Switches between live preview (with a renderer) and plain source. */
|
|
452
|
+
insertImage: () => run(imageCommand),
|
|
453
|
+
setPreview(render) {
|
|
454
|
+
liveOn = !!render;
|
|
455
|
+
view.dispatch({ effects: live.reconfigure(render ? livePreview(liveOptions(render)) : []) });
|
|
456
|
+
},
|
|
457
|
+
setReadonly(value) {
|
|
458
|
+
view.dispatch({ effects: readonly.reconfigure(EditorState.readOnly.of(value)) });
|
|
459
|
+
},
|
|
460
|
+
setAttributes(value) {
|
|
461
|
+
view.dispatch({ effects: attributes.reconfigure(EditorView.contentAttributes.of(value)) });
|
|
462
|
+
},
|
|
463
|
+
destroy: () => view.destroy()
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
export { createEditor };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import _default from "./components/WriterContent.js";
|
|
2
|
+
import { CommandName, WriterTranslations, WriterTranslationsInput, defaultTranslations } from "./translations.js";
|
|
3
|
+
import _default$1 from "./components/WriterEditor.vue.js";
|
|
4
|
+
import { ToolbarEditor } from "./types.js";
|
|
5
|
+
import _default$2 from "./components/WriterToolbar.vue.js";
|
|
6
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
7
|
+
import { MarkdownDocument, ParserOptions } from "./parse.js";
|
|
8
|
+
import { WRITER_CONFIG_KEY, WriterBlock, WriterConfig, WriterOptions, createWriter, useTranslations, useWriterConfig } from "./config.js";
|
|
9
|
+
import { RenderOptions, renderDocument } from "./render.js";
|
|
10
|
+
export { type CommandName, type MarkdownDocument, type ParserOptions, type RenderOptions, type ToolbarEditor, WRITER_CONFIG_KEY, type WriterBlock, type WriterConfig, _default as WriterContent, _default$1 as WriterEditor, type WriterOptions, _default$2 as WriterToolbar, type WriterTranslations, type WriterTranslationsInput, countWords, createWriter, defaultTranslations, readingMinutes, renderDocument, useTranslations, useWriterConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defaultTranslations } from "./translations.js";
|
|
2
|
+
import { WRITER_CONFIG_KEY, createWriter, useTranslations, useWriterConfig } from "./config.js";
|
|
3
|
+
import { renderDocument } from "./render.js";
|
|
4
|
+
import WriterContent_default from "./components/WriterContent.js";
|
|
5
|
+
import _sfc_main from "./components/WriterEditor.js";
|
|
6
|
+
import _sfc_main$1 from "./components/WriterToolbar.js";
|
|
7
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
8
|
+
export { WRITER_CONFIG_KEY, WriterContent_default as WriterContent, _sfc_main as WriterEditor, _sfc_main$1 as WriterToolbar, countWords, createWriter, defaultTranslations, readingMinutes, renderDocument, useTranslations, useWriterConfig };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/load-parser.ts
|
|
2
|
+
const parsers = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function loadParser(options) {
|
|
4
|
+
let parser = parsers.get(options);
|
|
5
|
+
if (!parser) {
|
|
6
|
+
parser = import("./parse.js").then(({ createParser }) => createParser(options));
|
|
7
|
+
parsers.set(options, parser);
|
|
8
|
+
parser.catch(() => parsers.delete(options));
|
|
9
|
+
}
|
|
10
|
+
return parser;
|
|
11
|
+
}
|
|
12
|
+
export { loadParser };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { WriterTranslationsInput } from "../translations.js";
|
|
2
|
+
import { ParserOptions } from "../parse.js";
|
|
3
|
+
/** Auto-registered components. A test keeps it in step with `src/index.ts`. */
|
|
4
|
+
export declare const COMPONENTS: readonly ["WriterContent", "WriterEditor", "WriterToolbar"];
|
|
5
|
+
export interface ModuleOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Folder inside each layer's `components/` whose components become blocks:
|
|
8
|
+
* `Callout.vue` → `::callout`, `media/Video.vue` → `::media-video`. `false` for none.
|
|
9
|
+
*/
|
|
10
|
+
blocks: string | false;
|
|
11
|
+
/**
|
|
12
|
+
* Replace how native elements render, app-wide, by component name:
|
|
13
|
+
* `{ a: "NuxtLink", img: "NuxtImg" }`. Any component Nuxt registers works.
|
|
14
|
+
*/
|
|
15
|
+
components: Record<string, string>;
|
|
16
|
+
/** Inject the core stylesheet. Turn off only to import it yourself, e.g. into a layer order. */
|
|
17
|
+
css: boolean;
|
|
18
|
+
/** Parser options for Markdown strings. Plugins aren't serializable: pre-parse on the server for those. */
|
|
19
|
+
parser: Omit<ParserOptions, "plugins">;
|
|
20
|
+
}
|
|
21
|
+
/** Labels and snippets for blocks, and the components' text, in `app.config.ts` under `writer`. */
|
|
22
|
+
export interface WriterAppConfig {
|
|
23
|
+
blocks?: Record<string, {
|
|
24
|
+
label?: string;
|
|
25
|
+
snippet?: string;
|
|
26
|
+
}>;
|
|
27
|
+
translations?: WriterTranslationsInput;
|
|
28
|
+
}
|
|
29
|
+
declare module "@nuxt/schema" {
|
|
30
|
+
interface AppConfigInput {
|
|
31
|
+
writer?: WriterAppConfig;
|
|
32
|
+
}
|
|
33
|
+
interface AppConfig {
|
|
34
|
+
writer?: WriterAppConfig;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
declare const _default: import("@nuxt/schema").NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
38
|
+
export { _default as default };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { version } from "../package.js";
|
|
2
|
+
import { kebabCase } from "comark/utils";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { addComponent, addPlugin, addServerImports, addTemplate, createResolver, defineNuxtModule, getLayerDirectories, resolveFiles, updateTemplates } from "@nuxt/kit";
|
|
5
|
+
// src/nuxt/module.ts
|
|
6
|
+
const PACKAGE = "@humanonlyweb/writer";
|
|
7
|
+
const TEMPLATE = "humanonlyweb-writer.mjs";
|
|
8
|
+
/** Auto-registered components. A test keeps it in step with `src/index.ts`. */
|
|
9
|
+
const COMPONENTS = [
|
|
10
|
+
"WriterContent",
|
|
11
|
+
"WriterEditor",
|
|
12
|
+
"WriterToolbar"
|
|
13
|
+
];
|
|
14
|
+
var module_default = defineNuxtModule({
|
|
15
|
+
meta: {
|
|
16
|
+
name: PACKAGE,
|
|
17
|
+
version,
|
|
18
|
+
configKey: "writer",
|
|
19
|
+
compatibility: { nuxt: ">=4.0.0" }
|
|
20
|
+
},
|
|
21
|
+
defaults: {
|
|
22
|
+
blocks: "blocks",
|
|
23
|
+
components: {},
|
|
24
|
+
css: true,
|
|
25
|
+
parser: {}
|
|
26
|
+
},
|
|
27
|
+
setup(options, nuxt) {
|
|
28
|
+
const resolver = createResolver(import.meta.url);
|
|
29
|
+
const entry = resolver.resolve("../index");
|
|
30
|
+
nuxt.options.build.transpile.push("comark");
|
|
31
|
+
nuxt.options.build.transpile.push(resolver.resolve("./runtime"));
|
|
32
|
+
for (const name of COMPONENTS) addComponent({
|
|
33
|
+
name,
|
|
34
|
+
export: name,
|
|
35
|
+
filePath: entry
|
|
36
|
+
});
|
|
37
|
+
for (const [tag, name] of Object.entries(options.components)) if (!/^[A-Z_$][\w$]*$/i.test(name)) throw new Error(`[${PACKAGE}] components.${tag}: "${name}" isn't a component name.`);
|
|
38
|
+
if (options.css) nuxt.options.css.push(resolver.resolve("../styles/core.css"));
|
|
39
|
+
const folder = options.blocks;
|
|
40
|
+
const blockDirs = folder ? getLayerDirectories(nuxt).map((layer) => resolver.resolve(layer.app, "components", folder)) : [];
|
|
41
|
+
addTemplate({
|
|
42
|
+
filename: TEMPLATE,
|
|
43
|
+
write: false,
|
|
44
|
+
async getContents() {
|
|
45
|
+
const entries = [];
|
|
46
|
+
const seen = /* @__PURE__ */ new Set();
|
|
47
|
+
const files = await Promise.all(blockDirs.map((dir) => resolveFiles(dir, "**/*.vue")));
|
|
48
|
+
for (const [index, dir] of blockDirs.entries()) for (const file of files[index] ?? []) {
|
|
49
|
+
const name = kebabCase(file.slice(dir.length + 1).replace(/\.vue$/, "").replaceAll("/", "-"));
|
|
50
|
+
if (seen.has(name)) continue;
|
|
51
|
+
seen.add(name);
|
|
52
|
+
entries.push(` ${JSON.stringify(name)}: defineAsyncComponent(() => import(${JSON.stringify(file)})),`);
|
|
53
|
+
}
|
|
54
|
+
const elements = Object.entries(options.components);
|
|
55
|
+
return [
|
|
56
|
+
`import { defineAsyncComponent } from "vue";`,
|
|
57
|
+
...elements.length ? [`import { ${[...new Set(elements.map(([, name]) => name))].join(", ")} } from "#components";`] : [],
|
|
58
|
+
`export const components = {${elements.map(([tag, name]) => ` ${JSON.stringify(tag)}: ${name},`).join("")} };`,
|
|
59
|
+
`export const blocks = {\n${entries.join("\n")}\n};`,
|
|
60
|
+
`export const parser = ${JSON.stringify(options.parser)};`
|
|
61
|
+
].join("\n");
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
nuxt.hook("builder:watch", async (event, path) => {
|
|
65
|
+
if (event !== "add" && event !== "unlink") return;
|
|
66
|
+
const file = resolver.resolve(nuxt.options.rootDir, path);
|
|
67
|
+
if (blockDirs.some((dir) => file.startsWith(`${dir}/`))) await updateTemplates({ filter: (template) => template.filename === TEMPLATE });
|
|
68
|
+
});
|
|
69
|
+
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
70
|
+
nuxt.hook("nitro:config", (config) => {
|
|
71
|
+
config.virtual ??= {};
|
|
72
|
+
const hash = createHash("sha256").update(JSON.stringify(options.parser)).digest("hex");
|
|
73
|
+
const version$1 = `${version}+${hash.slice(0, 8)}`;
|
|
74
|
+
config.virtual["#humanonlyweb-writer/parser"] = [`export default ${JSON.stringify(options.parser)};`, `export const version = ${JSON.stringify(version$1)};`].join("\n");
|
|
75
|
+
});
|
|
76
|
+
addServerImports([
|
|
77
|
+
{
|
|
78
|
+
name: "parseWriterDocument",
|
|
79
|
+
from: resolver.resolve("./runtime/server/parse")
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "writerParserVersion",
|
|
83
|
+
from: resolver.resolve("./runtime/server/parse")
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: "countWords",
|
|
87
|
+
from: resolver.resolve("../stats")
|
|
88
|
+
}
|
|
89
|
+
]);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
export { COMPONENTS, module_default as default };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createWriter } from "../../config.js";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import { defineNuxtPlugin, useAppConfig } from "nuxt/app";
|
|
4
|
+
import { blocks, components, parser } from "#build/humanonlyweb-writer.mjs";
|
|
5
|
+
// src/nuxt/runtime/plugin.ts
|
|
6
|
+
var plugin_default = defineNuxtPlugin({
|
|
7
|
+
name: "humanonlyweb-writer",
|
|
8
|
+
setup(nuxtApp) {
|
|
9
|
+
const appConfig = useAppConfig();
|
|
10
|
+
const meta = appConfig.writer?.blocks ?? {};
|
|
11
|
+
const withMeta = {};
|
|
12
|
+
for (const [name, component] of Object.entries(blocks)) withMeta[name] = {
|
|
13
|
+
component,
|
|
14
|
+
...meta[name]
|
|
15
|
+
};
|
|
16
|
+
nuxtApp.vueApp.use(createWriter({
|
|
17
|
+
blocks: withMeta,
|
|
18
|
+
components,
|
|
19
|
+
parser,
|
|
20
|
+
translations: computed(() => appConfig.writer?.translations ?? {})
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
export { plugin_default as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Parse } from "../../../parse.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses Markdown on the server with the module's `parser` options: the same ones the
|
|
4
|
+
* app renders with. Store the result beside the Markdown and render it with
|
|
5
|
+
* `<WriterContent>`, so pages load neither the parser nor the editor.
|
|
6
|
+
*/
|
|
7
|
+
export declare const parseWriterDocument: Parse;
|
|
8
|
+
/**
|
|
9
|
+
* This package's version and a hash of the `parser` options. Store it with each parsed
|
|
10
|
+
* document; one stored with another value is due for parsing again.
|
|
11
|
+
*/
|
|
12
|
+
export declare const writerParserVersion: string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createParser } from "../../../parse.js";
|
|
2
|
+
import options, { version } from "#humanonlyweb-writer/parser";
|
|
3
|
+
// src/nuxt/runtime/server/parse.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses Markdown on the server with the module's `parser` options: the same ones the
|
|
6
|
+
* app renders with. Store the result beside the Markdown and render it with
|
|
7
|
+
* `<WriterContent>`, so pages load neither the parser nor the editor.
|
|
8
|
+
*/
|
|
9
|
+
const parseWriterDocument = createParser(options);
|
|
10
|
+
/**
|
|
11
|
+
* This package's version and a hash of the `parser` options. Store it with each parsed
|
|
12
|
+
* document; one stored with another value is due for parsing again.
|
|
13
|
+
*/
|
|
14
|
+
const writerParserVersion = version;
|
|
15
|
+
export { parseWriterDocument, writerParserVersion };
|