@humanonlyweb/writer 0.2.0 → 0.2.2
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/dist/components/WriterContent.d.ts +35 -0
- package/dist/components/WriterContent.js +85 -40
- package/dist/components/WriterEditor.js +86 -57
- package/dist/components/WriterEditor.vue.d.ts +3 -1
- package/dist/config.d.ts +21 -6
- package/dist/config.js +4 -1
- package/dist/docs/blocks.md +8 -3
- package/dist/docs/changelog.md +9 -0
- package/dist/docs/components/content.md +11 -8
- package/dist/docs/components/editor.md +15 -14
- package/dist/docs/images.md +3 -3
- package/dist/docs/parsing.md +8 -7
- package/dist/docs/security.md +21 -5
- package/dist/docs/storing.md +1 -1
- package/dist/editor/commands.js +26 -10
- package/dist/editor/images.d.ts +2 -0
- package/dist/editor/images.js +94 -46
- package/dist/editor/live.js +67 -34
- package/dist/editor/setup.js +151 -84
- package/dist/index.d.ts +5 -5
- package/dist/load-parser.js +20 -9
- package/dist/nuxt/module.d.ts +5 -2
- package/dist/nuxt/module.js +16 -1
- package/dist/package.js +1 -1
- package/dist/parse.d.ts +3 -3
- package/dist/parse.js +35 -17
- package/dist/parser-options.d.ts +17 -3
- package/dist/render.d.ts +7 -0
- package/dist/render.js +194 -42
- package/dist/sanitize.d.ts +3 -1
- package/dist/sanitize.js +214 -23
- package/dist/stats.js +28 -2
- package/package.json +13 -12
package/dist/docs/images.md
CHANGED
|
@@ -42,13 +42,13 @@ A card's buttons say Replace and Remove. To show icons instead, fill the editor'
|
|
|
42
42
|
|
|
43
43
|
## Uploads
|
|
44
44
|
|
|
45
|
-
Give the editor an `upload` function: it gets the `File` and resolves to its URL.
|
|
45
|
+
Give the editor an `upload` function: it gets the `File` (and an optional `AbortSignal` that aborts if the editor unmounts mid-upload) and resolves to its URL. Set `accept` to allow picking non-image files too (e.g. `accept="image/*,.pdf"`).
|
|
46
46
|
|
|
47
47
|
```ts
|
|
48
|
-
async function upload(file: File) {
|
|
48
|
+
async function upload(file: File, signal?: AbortSignal) {
|
|
49
49
|
const body = new FormData();
|
|
50
50
|
body.append("file", file);
|
|
51
|
-
const { url } = await $fetch<{ url: string }>("/api/uploads", { method: "POST", body });
|
|
51
|
+
const { url } = await $fetch<{ url: string }>("/api/uploads", { method: "POST", body, signal });
|
|
52
52
|
return url;
|
|
53
53
|
}
|
|
54
54
|
```
|
package/dist/docs/parsing.md
CHANGED
|
@@ -18,13 +18,14 @@ document.meta.toc; // { links: [{ id, text, depth, children }] }
|
|
|
18
18
|
|
|
19
19
|
## Options
|
|
20
20
|
|
|
21
|
-
| Option | Default | What it does
|
|
22
|
-
| ----------- | ------- |
|
|
23
|
-
| `footnotes` | `true` | `[^1]` footnotes. An object sets `label` and `backRef`.
|
|
24
|
-
| `highlight` | `false` | Highlights fenced code with [rangi](https://comark.dev/plugins/built-in/rangi). An object sets `theme`, `lineNumbers`. Loads only for documents with code. |
|
|
25
|
-
| `toc` | `false` | Table of contents in `meta.toc`. An object sets `depth`.
|
|
26
|
-
| `linkify` | `true` | URL-like text becomes links.
|
|
27
|
-
| `
|
|
21
|
+
| Option | Default | What it does |
|
|
22
|
+
| ----------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
23
|
+
| `footnotes` | `true` | `[^1]` footnotes. An object sets `label` and `backRef`. |
|
|
24
|
+
| `highlight` | `false` | Highlights fenced code and inline `` `code`{lang="…"} `` with [rangi](https://comark.dev/plugins/built-in/rangi). An object sets `theme`, `lineNumbers`, `preStyles`, `inlineCode`. Loads only for documents with code. |
|
|
25
|
+
| `toc` | `false` | Table of contents in `meta.toc`. An object sets `depth`. |
|
|
26
|
+
| `linkify` | `true` | URL-like text becomes links. |
|
|
27
|
+
| `security` | `{}` | Extra sanitization (`stripClasses`, `stripIds`, `allowDataImages`, `allowedLinkPrefixes`, `allowedImagePrefixes`). See [Security](/security). |
|
|
28
|
+
| `plugins` | `[]` | Other Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. Not in `nuxt.config`: they aren't serializable, so parse on the server. |
|
|
28
29
|
|
|
29
30
|
## Syntax
|
|
30
31
|
|
package/dist/docs/security.md
CHANGED
|
@@ -4,13 +4,29 @@
|
|
|
4
4
|
|
|
5
5
|
- Raw HTML is text.
|
|
6
6
|
- Only Markdown's own elements and registered blocks render. App-wide components can't be reached from Markdown.
|
|
7
|
-
- `script`, `style`, `iframe`, `object` and similar are removed, with their content. So are `on*` handlers, `style` attributes (except a highlighter's colours inside code) and URLs that aren't relative, `http(s)`, `mailto` or `tel`.
|
|
8
|
-
-
|
|
7
|
+
- `script`, `style`, `iframe`, `object` and similar are removed, with their content. So are `on*` handlers, Vue prop/attribute modifiers (`.`, `^`, `@`, `v-on:`, `v-bind:`), `style` attributes (except a highlighter's colours and line display inside code), unsafe object props and URLs (including `to` and custom `*url`/`*href`/`*src` block props) that aren't relative, `http(s)`, `mailto` or `tel`.
|
|
8
|
+
- Task-list checkboxes keep only safe checkbox attributes (`type`, `checked`, `disabled`, `aria-label`, `class`), and AST depth is capped to guard against stack-overflow payloads.
|
|
9
|
+
- `<WriterContent>` sanitizes nodes, `frontmatter`, `meta` and `data` again before rendering, so a stored document is safe even if it didn't come from this parser.
|
|
9
10
|
|
|
10
|
-
The checks are exported from `@humanonlyweb/writer/parse`: `sanitize(nodes, isAllowed?)`, `isSafeUrl(url)` and `NATIVE_TAGS`.
|
|
11
|
+
The checks are exported from `@humanonlyweb/writer/parse`: `sanitize(nodes, isAllowed?, options?)`, `isSafeUrl(url)` and `NATIVE_TAGS`.
|
|
12
|
+
|
|
13
|
+
## Strict mode for untrusted authors
|
|
14
|
+
|
|
15
|
+
Pass `security` in `parser` options to lock down user-generated content further:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
createParser({
|
|
19
|
+
security: {
|
|
20
|
+
stripClasses: true,
|
|
21
|
+
stripIds: true,
|
|
22
|
+
allowDataImages: false,
|
|
23
|
+
allowedLinkPrefixes: ["https://example.com"],
|
|
24
|
+
allowedImagePrefixes: ["https://cdn.example.com"],
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
```
|
|
11
28
|
|
|
12
29
|
## What's left to you
|
|
13
30
|
|
|
14
|
-
-
|
|
15
|
-
- Blocks get attributes as written. Validate any URL your block puts in an attribute other than `href` or `src`.
|
|
31
|
+
- By default, authors can set `class` and `id` with `{…}`. With untrusted authors and utility-class CSS (`fixed inset-0`), enable `security.stripClasses` / `security.stripIds` or render those elements through `components`.
|
|
16
32
|
- CodeMirror injects `<style>` elements. Under a strict CSP, pass `EditorView.cspNonce.of(nonce)` in `extensions`.
|
package/dist/docs/storing.md
CHANGED
|
@@ -30,7 +30,7 @@ Outside Nuxt, `createParser(options)` from `@humanonlyweb/writer/parse` with the
|
|
|
30
30
|
|
|
31
31
|
## Re-parsing
|
|
32
32
|
|
|
33
|
-
The parsed document is derived, so it can go stale. `writerParserVersion` is this package's version plus a hash of your `parser` options. When a stored one differs, parse the `body` again: on read, or in a batch after deploying. Outside Nuxt, do the same with the package version and your options.
|
|
33
|
+
The parsed document is derived, so it can go stale. `writerParserVersion` is this package's version plus a hash of your `parser` options and the installed `comark` / `rangi` versions. When a stored one differs, parse the `body` again: on read, or in a batch after deploying. Outside Nuxt, do the same with the package version and your options.
|
|
34
34
|
|
|
35
35
|
## Drafts and published versions
|
|
36
36
|
|
package/dist/editor/commands.js
CHANGED
|
@@ -203,25 +203,33 @@ function codeBlock(state) {
|
|
|
203
203
|
userEvent
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
|
+
const FOOTNOTE_REF = /\[\^([^\]\s]+)\]/g;
|
|
206
207
|
/** Adds the next free `[^n]` at the cursor and its definition at the end, with the cursor there. */
|
|
207
208
|
function footnote(state) {
|
|
208
|
-
const
|
|
209
|
-
const
|
|
209
|
+
const used = /* @__PURE__ */ new Set();
|
|
210
|
+
const iter = state.doc.iterLines();
|
|
211
|
+
while (!iter.next().done) {
|
|
212
|
+
const line = iter.value;
|
|
213
|
+
if (!line.includes("[^")) continue;
|
|
214
|
+
FOOTNOTE_REF.lastIndex = 0;
|
|
215
|
+
for (let match = FOOTNOTE_REF.exec(line); match; match = FOOTNOTE_REF.exec(line)) if (match[1]) used.add(match[1]);
|
|
216
|
+
}
|
|
210
217
|
let label = 1;
|
|
211
218
|
while (used.has(String(label))) label++;
|
|
212
219
|
const reference = `[^${label}]`;
|
|
220
|
+
const docLength = state.doc.length;
|
|
213
221
|
const at = state.selection.main.to;
|
|
214
|
-
const
|
|
215
|
-
const definition = `${
|
|
222
|
+
const tail = at === docLength ? reference : state.sliceDoc(Math.max(0, docLength - 2), docLength);
|
|
223
|
+
const definition = `${tail.endsWith("\n\n") ? "" : tail.endsWith("\n") ? "\n" : "\n\n"}[^${label}]: `;
|
|
216
224
|
return {
|
|
217
225
|
changes: [{
|
|
218
226
|
from: at,
|
|
219
227
|
insert: reference
|
|
220
228
|
}, {
|
|
221
|
-
from:
|
|
229
|
+
from: docLength,
|
|
222
230
|
insert: definition
|
|
223
231
|
}],
|
|
224
|
-
selection: EditorSelection.cursor(
|
|
232
|
+
selection: EditorSelection.cursor(docLength + reference.length + definition.length),
|
|
225
233
|
scrollIntoView: true,
|
|
226
234
|
userEvent: "input.insert"
|
|
227
235
|
};
|
|
@@ -241,11 +249,19 @@ const BLOCK_FENCE = /^\s*:{2,}/;
|
|
|
241
249
|
const QUOTE_PREFIX = /^[ \t]*(?:>[ \t]?)+/;
|
|
242
250
|
/** Up to a list item's text, quote markers included: `> - `, ` 1. `. */
|
|
243
251
|
const LIST_PREFIX = /^((?:[ \t]*>)*)([ \t]*(?:[-*+]|\d+[.)])[ \t]+)/;
|
|
252
|
+
const FRONTMATTER_END = /^---[ \t]*$/;
|
|
244
253
|
function inFrontmatter(state, pos) {
|
|
245
|
-
const doc = state
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
254
|
+
const { doc } = state;
|
|
255
|
+
if (doc.lines < 2 || doc.line(1).text !== "---") return false;
|
|
256
|
+
for (let n = 2; n <= doc.lines; n++) {
|
|
257
|
+
const line = doc.line(n);
|
|
258
|
+
if (FRONTMATTER_END.test(line.text)) return pos <= line.from;
|
|
259
|
+
if (line.from > pos) {
|
|
260
|
+
for (let m = n + 1; m <= doc.lines; m++) if (FRONTMATTER_END.test(doc.line(m).text)) return true;
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return false;
|
|
249
265
|
}
|
|
250
266
|
function placeAt(state, pos) {
|
|
251
267
|
const line = state.doc.lineAt(pos);
|
package/dist/editor/images.d.ts
CHANGED
package/dist/editor/images.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { UPLOAD_PLACEHOLDER, isUploadPlaceholder } from "../placeholder.js";
|
|
2
|
+
import { isSafeDataImage, isSafeUrl } from "../sanitize.js";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
2
4
|
import { WidgetType } from "@codemirror/view";
|
|
3
5
|
// src/editor/images.ts
|
|
4
6
|
const IMAGE = /^(\s*)!\[((?:\\.|[^\]\\\n])*)\]\(\s*(<[^>\n]*>|[^\s)]*)(?:\s+"((?:\\.|[^"\\\n])*)")?\s*\)(\{[^}\n]*\})?\s*$/;
|
|
7
|
+
const WIDTH_ATTR = /(?:^|[\s{])width="?(\d+)"?/;
|
|
8
|
+
const HEIGHT_ATTR = /(?:^|[\s{])height="?(\d+)"?/;
|
|
5
9
|
const unescape = (text) => text.replace(/\\(.)/g, "$1");
|
|
10
|
+
const wrapUrl = (url) => /[\s()]/.test(url) ? `<${url}>` : url;
|
|
6
11
|
function parseImageLine(text) {
|
|
7
12
|
const match = IMAGE.exec(text);
|
|
8
13
|
if (!match) return null;
|
|
@@ -21,13 +26,12 @@ function formatImageLine({ indent, alt, url, title, attrs }) {
|
|
|
21
26
|
}
|
|
22
27
|
/** Width and height in an attribute block, which reserve the image's space on the page. */
|
|
23
28
|
function imageSize(attrs) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
29
|
+
if (!attrs) return {};
|
|
30
|
+
const w = WIDTH_ATTR.exec(attrs)?.[1];
|
|
31
|
+
const h = HEIGHT_ATTR.exec(attrs)?.[1];
|
|
28
32
|
return {
|
|
29
|
-
width:
|
|
30
|
-
height:
|
|
33
|
+
width: w ? Number(w) : void 0,
|
|
34
|
+
height: h ? Number(h) : void 0
|
|
31
35
|
};
|
|
32
36
|
}
|
|
33
37
|
function withSize(attrs, width, height) {
|
|
@@ -36,23 +40,31 @@ function withSize(attrs, width, height) {
|
|
|
36
40
|
/** Local previews of files still uploading, by their placeholder URL. */
|
|
37
41
|
const previews = /* @__PURE__ */ new Map();
|
|
38
42
|
let uploadCount = 0;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
43
|
+
const sizeOf = Effect.fnUntraced(function* (file) {
|
|
44
|
+
return yield* Effect.tryPromise({
|
|
45
|
+
try: async () => {
|
|
46
|
+
const bitmap = await createImageBitmap(file);
|
|
47
|
+
const size = {
|
|
48
|
+
width: bitmap.width,
|
|
49
|
+
height: bitmap.height
|
|
50
|
+
};
|
|
51
|
+
bitmap.close();
|
|
52
|
+
return size;
|
|
53
|
+
},
|
|
54
|
+
catch: () => void 0
|
|
55
|
+
}).pipe(Effect.catch(() => Effect.succeed(void 0)));
|
|
56
|
+
});
|
|
52
57
|
/** Where `token` is in the document, followed by the end of its URL. */
|
|
53
58
|
function find(view, token) {
|
|
54
|
-
const
|
|
55
|
-
|
|
59
|
+
const iter = view.state.doc.iterLines();
|
|
60
|
+
let offset = 0;
|
|
61
|
+
while (!iter.next().done) {
|
|
62
|
+
const line = iter.value;
|
|
63
|
+
if (line.includes(token)) {
|
|
64
|
+
for (let at = line.indexOf(token); at >= 0; at = line.indexOf(token, at + 1)) if (/[)\s]/.test(line[at + token.length] ?? "")) return offset + at;
|
|
65
|
+
}
|
|
66
|
+
offset += line.length + 1;
|
|
67
|
+
}
|
|
56
68
|
return -1;
|
|
57
69
|
}
|
|
58
70
|
/** A placeholder URL, unique across page loads: a leftover one in a saved document never matches. */
|
|
@@ -61,10 +73,10 @@ const nextToken = () => `${UPLOAD_PLACEHOLDER}${Date.now().toString(36)}${(++upl
|
|
|
61
73
|
* The Markdown a file is while it uploads. An image shows from the local file, at its real
|
|
62
74
|
* size, and keeps the alt text and caption of the image it replaces.
|
|
63
75
|
*/
|
|
64
|
-
|
|
76
|
+
const placeholderLine = Effect.fnUntraced(function* (file, token, replacing) {
|
|
65
77
|
if (!file.type.startsWith("image/")) return `[${file.name.replace(/[[\]]/g, "")}](${token})`;
|
|
66
78
|
previews.set(token, URL.createObjectURL(file));
|
|
67
|
-
const size =
|
|
79
|
+
const size = yield* sizeOf(file);
|
|
68
80
|
return formatImageLine({
|
|
69
81
|
indent: replacing?.indent ?? "",
|
|
70
82
|
alt: replacing?.alt ?? "",
|
|
@@ -72,27 +84,47 @@ async function placeholderLine(file, token, replacing) {
|
|
|
72
84
|
title: replacing?.title ?? "",
|
|
73
85
|
attrs: size ? withSize(replacing?.attrs || "{}", size.width, size.height) : replacing?.attrs ?? ""
|
|
74
86
|
});
|
|
87
|
+
});
|
|
88
|
+
/**
|
|
89
|
+
* Releases `token`'s local blob URL: immediately if `immediate`, or after a 30s handoff
|
|
90
|
+
* window (interrupted and revoked early if the editor scope closes).
|
|
91
|
+
*/
|
|
92
|
+
function releasePreview(token, immediate, scope) {
|
|
93
|
+
const local = previews.get(token);
|
|
94
|
+
previews.delete(token);
|
|
95
|
+
if (!local) return;
|
|
96
|
+
if (immediate) {
|
|
97
|
+
URL.revokeObjectURL(local);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const revoke = Effect.sleep(3e4).pipe(Effect.ensuring(Effect.sync(() => {
|
|
101
|
+
URL.revokeObjectURL(local);
|
|
102
|
+
})));
|
|
103
|
+
if (scope) Effect.runSync(Effect.forkIn(revoke, scope));
|
|
104
|
+
else Effect.runFork(revoke);
|
|
75
105
|
}
|
|
76
106
|
/** Uploads one file and puts its URL in place of `token`, or undoes the placeholder. */
|
|
77
|
-
|
|
107
|
+
const finishOne = Effect.fnUntraced(function* (view, file, token, options, previous) {
|
|
78
108
|
let settled = false;
|
|
79
109
|
const settle = () => {
|
|
80
110
|
if (!settled) options.onProgress(-1);
|
|
81
111
|
settled = true;
|
|
82
112
|
};
|
|
83
|
-
|
|
84
|
-
|
|
113
|
+
yield* Effect.tryPromise({
|
|
114
|
+
try: (signal) => options.upload(file, signal),
|
|
115
|
+
catch: (error) => error
|
|
116
|
+
}).pipe(Effect.flatMap((url) => Effect.sync(() => {
|
|
85
117
|
settle();
|
|
86
118
|
const at = find(view, token);
|
|
87
119
|
if (at >= 0) view.dispatch({
|
|
88
120
|
changes: {
|
|
89
121
|
from: at,
|
|
90
122
|
to: at + token.length,
|
|
91
|
-
insert: url
|
|
123
|
+
insert: wrapUrl(url)
|
|
92
124
|
},
|
|
93
125
|
userEvent: "input.upload"
|
|
94
126
|
});
|
|
95
|
-
} catch
|
|
127
|
+
})), Effect.catch((error) => Effect.sync(() => {
|
|
96
128
|
settle();
|
|
97
129
|
const at = find(view, token);
|
|
98
130
|
if (at >= 0) {
|
|
@@ -116,21 +148,21 @@ async function finish(view, file, token, options, previous) {
|
|
|
116
148
|
});
|
|
117
149
|
}
|
|
118
150
|
options.onError(error, file);
|
|
119
|
-
}
|
|
151
|
+
})), Effect.onInterrupt(() => Effect.sync(() => {
|
|
152
|
+
releasePreview(token, true);
|
|
153
|
+
})), Effect.ensuring(Effect.sync(() => {
|
|
120
154
|
settle();
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
}
|
|
155
|
+
releasePreview(token, false, options.scope);
|
|
156
|
+
})));
|
|
157
|
+
});
|
|
126
158
|
/** The newlines to add to `newlines` for a blank line. */
|
|
127
159
|
const gap = (newlines) => "\n".repeat(Math.max(0, 2 - newlines));
|
|
128
160
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
161
|
+
* Effectful upload pipeline: inserts placeholders in one edit and uploads files concurrently.
|
|
162
|
+
* When forked inside the editor's `Scope`, unmounting interrupts all in-flight uploads and
|
|
163
|
+
* revokes local preview URLs automatically.
|
|
132
164
|
*/
|
|
133
|
-
|
|
165
|
+
const uploadFilesEffect = Effect.fnUntraced(function* (view, files, options, target) {
|
|
134
166
|
const replace = "replace" in target ? target.replace : void 0;
|
|
135
167
|
const uploads = (replace ? files.slice(0, 1) : files).map((file) => ({
|
|
136
168
|
file,
|
|
@@ -140,7 +172,9 @@ async function uploadFiles(view, files, options, target) {
|
|
|
140
172
|
options.onProgress(uploads.length);
|
|
141
173
|
const current = replace ? parseImageLine(view.state.sliceDoc(replace.from, replace.to)) : null;
|
|
142
174
|
const previous = uploads[0]?.file.type.startsWith("image/") ? current : null;
|
|
143
|
-
const lines =
|
|
175
|
+
const lines = yield* Effect.all(uploads.map(({ file, token }) => placeholderLine(file, token, previous)), { concurrency: "unbounded" }).pipe(Effect.onInterrupt(() => Effect.sync(() => {
|
|
176
|
+
for (const { token } of uploads) releasePreview(token, true);
|
|
177
|
+
})));
|
|
144
178
|
if (replace) view.dispatch({
|
|
145
179
|
changes: {
|
|
146
180
|
...replace,
|
|
@@ -165,7 +199,20 @@ async function uploadFiles(view, files, options, target) {
|
|
|
165
199
|
userEvent: "input.upload"
|
|
166
200
|
});
|
|
167
201
|
}
|
|
168
|
-
|
|
202
|
+
yield* Effect.all(uploads.map(({ file, token }) => finishOne(view, file, token, options, previous)), { concurrency: "unbounded" });
|
|
203
|
+
});
|
|
204
|
+
/**
|
|
205
|
+
* Uploads `files`, inserted at `at`, each on its own line, in one edit; or uploads the
|
|
206
|
+
* first into the image line `replace`. Placeholders go in at once and the uploads run
|
|
207
|
+
* side by side.
|
|
208
|
+
*/
|
|
209
|
+
function uploadFiles(view, files, options, target) {
|
|
210
|
+
const effect = uploadFilesEffect(view, files, options, target);
|
|
211
|
+
if (options.scope) {
|
|
212
|
+
Effect.runSync(Effect.forkIn(effect, options.scope));
|
|
213
|
+
return Promise.resolve();
|
|
214
|
+
}
|
|
215
|
+
return Effect.runPromise(effect);
|
|
169
216
|
}
|
|
170
217
|
/** A card button: its words, or an icon named by them (`data-icon`, for styling). */
|
|
171
218
|
function action(label, icon) {
|
|
@@ -186,10 +233,10 @@ function element(tag, props = {}, ...children) {
|
|
|
186
233
|
node.append(...children);
|
|
187
234
|
return node;
|
|
188
235
|
}
|
|
189
|
-
function pickFile(onFile) {
|
|
236
|
+
function pickFile(onFile, accept = "image/*") {
|
|
190
237
|
const input = element("input", {
|
|
191
238
|
type: "file",
|
|
192
|
-
accept
|
|
239
|
+
accept
|
|
193
240
|
});
|
|
194
241
|
input.addEventListener("change", () => {
|
|
195
242
|
const file = input.files?.[0];
|
|
@@ -209,7 +256,7 @@ var ImageCardWidget = class extends WidgetType {
|
|
|
209
256
|
this.options = options;
|
|
210
257
|
}
|
|
211
258
|
eq(other) {
|
|
212
|
-
return other.source === this.source && other.selected === this.selected;
|
|
259
|
+
return other.source === this.source && other.selected === this.selected && other.options.translations === this.options.translations;
|
|
213
260
|
}
|
|
214
261
|
/** The image line this card stands for, wherever it is now. */
|
|
215
262
|
range(view, dom) {
|
|
@@ -304,7 +351,7 @@ var ImageCardWidget = class extends WidgetType {
|
|
|
304
351
|
}
|
|
305
352
|
if (upload) {
|
|
306
353
|
const choosePicked = () => {
|
|
307
|
-
if (!view.state.readOnly) pickFile((file) => upload(view, file, this.range(view, root)));
|
|
354
|
+
if (!view.state.readOnly) pickFile((file) => upload(view, file, this.range(view, root)), this.options.accept ?? "image/*");
|
|
308
355
|
};
|
|
309
356
|
choose.addEventListener("click", choosePicked);
|
|
310
357
|
replace.addEventListener("click", choosePicked);
|
|
@@ -368,7 +415,8 @@ var ImageCardWidget = class extends WidgetType {
|
|
|
368
415
|
if (!card || !line) return;
|
|
369
416
|
const { width, height } = imageSize(line.attrs);
|
|
370
417
|
const local = previews.get(line.url);
|
|
371
|
-
const
|
|
418
|
+
const safeRemote = !isUploadPlaceholder(line.url) && (isSafeUrl(line.url) || isSafeDataImage(line.url)) ? line.url : "";
|
|
419
|
+
const src = local ?? safeRemote;
|
|
372
420
|
card.root.toggleAttribute("data-selected", this.selected);
|
|
373
421
|
card.frame.toggleAttribute("data-empty", !src);
|
|
374
422
|
card.frame.toggleAttribute("data-uploading", !!local);
|
|
@@ -403,4 +451,4 @@ var ImageCardWidget = class extends WidgetType {
|
|
|
403
451
|
return true;
|
|
404
452
|
}
|
|
405
453
|
};
|
|
406
|
-
export { ImageCardWidget, formatImageLine, imageSize, parseImageLine, uploadFiles };
|
|
454
|
+
export { ImageCardWidget, formatImageLine, imageSize, parseImageLine, uploadFiles, uploadFilesEffect };
|
package/dist/editor/live.js
CHANGED
|
@@ -19,10 +19,17 @@ function findPreviews(doc) {
|
|
|
19
19
|
const previews = [];
|
|
20
20
|
const open = [];
|
|
21
21
|
let fence;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
22
|
+
let offset = 0;
|
|
23
|
+
const iter = doc.iterLines();
|
|
24
|
+
while (!iter.next().done) {
|
|
25
|
+
const text = iter.value;
|
|
26
|
+
const from = offset;
|
|
27
|
+
const to = offset + text.length;
|
|
28
|
+
offset = to + 1;
|
|
29
|
+
const first = text.trimStart();
|
|
30
|
+
if (!first) continue;
|
|
31
|
+
const firstChar = first[0];
|
|
32
|
+
const marker = firstChar === "`" || firstChar === "~" ? FENCE.exec(text)?.[1] : void 0;
|
|
26
33
|
if (fence) {
|
|
27
34
|
if (!!marker && marker[0] === fence[0] && marker.length >= fence.length && !text.slice(text.indexOf(marker) + marker.length).trim()) fence = void 0;
|
|
28
35
|
continue;
|
|
@@ -32,30 +39,32 @@ function findPreviews(doc) {
|
|
|
32
39
|
continue;
|
|
33
40
|
}
|
|
34
41
|
if (!open.length && indentOf(text) >= 4) continue;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
42
|
+
if (firstChar === ":") {
|
|
43
|
+
const closer = CLOSER.exec(text)?.[1];
|
|
44
|
+
if (closer) {
|
|
45
|
+
const index = open.findLastIndex((block) => block.colons === closer.length);
|
|
46
|
+
if (index < 0) continue;
|
|
47
|
+
const [block] = open.splice(index);
|
|
48
|
+
if (index === 0 && block) previews.push({
|
|
49
|
+
kind: "block",
|
|
50
|
+
from: block.from,
|
|
51
|
+
to
|
|
52
|
+
});
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const opener = OPENER.exec(text)?.[1];
|
|
56
|
+
if (opener) {
|
|
57
|
+
open.push({
|
|
58
|
+
colons: opener.length,
|
|
59
|
+
from
|
|
60
|
+
});
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
54
63
|
}
|
|
55
|
-
if (!open.length &&
|
|
64
|
+
if (!open.length && firstChar === "!" && first.startsWith("![") && parseImageLine(text)) previews.push({
|
|
56
65
|
kind: "image",
|
|
57
|
-
from
|
|
58
|
-
to
|
|
66
|
+
from,
|
|
67
|
+
to
|
|
59
68
|
});
|
|
60
69
|
}
|
|
61
70
|
return previews;
|
|
@@ -118,17 +127,19 @@ var BlockWidget = class extends WidgetType {
|
|
|
118
127
|
};
|
|
119
128
|
const openLine = Decoration.line({ class: "cm-md-block-open" });
|
|
120
129
|
const fenceLine = Decoration.line({ class: "cm-md-block-open cm-md-block-fence" });
|
|
121
|
-
function previewDecorations(state, previews, options) {
|
|
130
|
+
function previewDecorations(state, previews, touched, options) {
|
|
122
131
|
const ranges = [];
|
|
123
|
-
for (
|
|
132
|
+
for (let i = 0; i < previews.length; i++) {
|
|
133
|
+
const { kind, from, to } = previews[i];
|
|
134
|
+
const isTouched = touched[i];
|
|
124
135
|
const source = state.sliceDoc(from, to);
|
|
125
136
|
if (kind === "image") {
|
|
126
|
-
const widget = new ImageCardWidget(source,
|
|
137
|
+
const widget = new ImageCardWidget(source, isTouched, options);
|
|
127
138
|
ranges.push(Decoration.replace({
|
|
128
139
|
widget,
|
|
129
140
|
block: true
|
|
130
141
|
}).range(from, to));
|
|
131
|
-
} else if (
|
|
142
|
+
} else if (isTouched) {
|
|
132
143
|
const first = state.doc.lineAt(from).number;
|
|
133
144
|
const last = state.doc.lineAt(to).number;
|
|
134
145
|
for (let n = first; n <= last; n++) ranges.push((n === first || n === last ? fenceLine : openLine).range(state.doc.line(n).from));
|
|
@@ -146,17 +157,39 @@ function previewField(options) {
|
|
|
146
157
|
return StateField.define({
|
|
147
158
|
create(state) {
|
|
148
159
|
const previews = findPreviews(state.doc);
|
|
160
|
+
const touched = previews.map((p) => touches(state, p.from, p.to));
|
|
149
161
|
return {
|
|
150
162
|
previews,
|
|
151
|
-
|
|
163
|
+
touched,
|
|
164
|
+
decorations: previewDecorations(state, previews, touched, options)
|
|
152
165
|
};
|
|
153
166
|
},
|
|
154
167
|
update(value, tr) {
|
|
155
168
|
if (!tr.docChanged && !tr.selection && tr.startState.field(focused) === tr.state.field(focused)) return value;
|
|
156
|
-
|
|
169
|
+
if (!tr.docChanged) {
|
|
170
|
+
const previews = value.previews;
|
|
171
|
+
if (!previews.length) return value;
|
|
172
|
+
let changed = false;
|
|
173
|
+
const touched = [];
|
|
174
|
+
for (let i = 0; i < previews.length; i++) {
|
|
175
|
+
const p = previews[i];
|
|
176
|
+
const next = touches(tr.state, p.from, p.to);
|
|
177
|
+
touched.push(next);
|
|
178
|
+
if (next !== value.touched[i]) changed = true;
|
|
179
|
+
}
|
|
180
|
+
if (!changed) return value;
|
|
181
|
+
return {
|
|
182
|
+
previews,
|
|
183
|
+
touched,
|
|
184
|
+
decorations: previewDecorations(tr.state, previews, touched, options)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const previews = findPreviews(tr.state.doc);
|
|
188
|
+
const touched = previews.map((p) => touches(tr.state, p.from, p.to));
|
|
157
189
|
return {
|
|
158
190
|
previews,
|
|
159
|
-
|
|
191
|
+
touched,
|
|
192
|
+
decorations: previewDecorations(tr.state, previews, touched, options)
|
|
160
193
|
};
|
|
161
194
|
},
|
|
162
195
|
provide: (field) => EditorView.decorations.from(field, (value) => value.decorations)
|
|
@@ -233,7 +266,7 @@ function inlineDecorations(view, previews, task) {
|
|
|
233
266
|
const last = state.doc.lineAt(range.to).number;
|
|
234
267
|
for (let n = state.doc.lineAt(range.from).number; n <= last; n++) cursorLines.add(n);
|
|
235
268
|
}
|
|
236
|
-
const onCursorLine = (pos) => cursorLines.has(state.doc.lineAt(pos).number);
|
|
269
|
+
const onCursorLine = (pos) => cursorLines.size > 0 && cursorLines.has(state.doc.lineAt(pos).number);
|
|
237
270
|
const open = previews.filter((p) => p.kind === "block" && touches(state, p.from, p.to));
|
|
238
271
|
const inOpenBlock = (pos) => open.some((p) => pos >= p.from && pos <= p.to);
|
|
239
272
|
const withSpace = (to) => state.sliceDoc(to, to + 1) === " " ? to + 1 : to;
|