@writeon/core 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 +49 -2
- package/dist/commands-C_HMYTN3.js +243 -0
- package/dist/commands.d.ts +51 -0
- package/dist/commands.js +1 -0
- package/dist/doc-9UhfWLHH.js +77 -0
- package/dist/extension-DKF04JmH.js +210 -0
- package/dist/extension.d.ts +59 -0
- package/dist/extension.js +4 -0
- package/dist/extensions.d.ts +44 -0
- package/dist/extensions.js +331 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +371 -0
- package/dist/input-rules-BBQyq8l5.js +28 -0
- package/dist/presets.d.ts +47 -0
- package/dist/presets.js +38 -0
- package/dist/types-Dy7Bm7XX.d.ts +225 -0
- package/llms-full.txt +10811 -0
- package/llms.txt +53 -0
- package/package.json +86 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ma'ruf Sheraliyev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @writeon/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The editor of [writeon](../../README.md): a framework-agnostic, headless, accessible rich text
|
|
4
|
+
editor built on ProseMirror. Plain TypeScript, no DOM needed until you mount it (SSR-safe).
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @writeon/core # or pnpm add / bun add
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
The ProseMirror packages are peer dependencies (one copy in your app); npm 7+, pnpm and bun
|
|
11
|
+
install them for you. With Yarn, add them yourself:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
yarn add @writeon/core prosemirror-commands prosemirror-history prosemirror-inputrules \
|
|
15
|
+
prosemirror-keymap prosemirror-model prosemirror-schema-list prosemirror-state \
|
|
16
|
+
prosemirror-transform prosemirror-view
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { createEditor, debounce } from '@writeon/core'
|
|
21
|
+
import { toggleBold } from '@writeon/core/commands'
|
|
22
|
+
import { basic } from '@writeon/core/presets'
|
|
23
|
+
|
|
24
|
+
const editor = createEditor({
|
|
25
|
+
element: document.querySelector('#editor'),
|
|
26
|
+
extensions: basic(),
|
|
27
|
+
label: 'Article text', // required: names the field for screen readers
|
|
28
|
+
content: savedDoc, // DocJSON from your database
|
|
29
|
+
onChange: debounce((editor) => save(editor.getJSON()), 500),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
editor.run(toggleBold()) // commands are factories: call them, then run
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Entry | What it has |
|
|
36
|
+
| -------------------------- | ---------------------------------------------------------------------------- |
|
|
37
|
+
| `@writeon/core` | `createEditor`, `DocJSON` and its types, `migrateDoc`, `debounce`, URL rules |
|
|
38
|
+
| `@writeon/core/presets` | `basic()`: paragraphs, headings, lists, quotes, marks, links, history |
|
|
39
|
+
| `@writeon/core/commands` | Command factories for `editor.run()` / `editor.can()` |
|
|
40
|
+
| `@writeon/core/extension` | `defineExtension`, `createSchema`, `docFromJSON`, `markInputRule` |
|
|
41
|
+
| `@writeon/core/extensions` | The single extensions behind `basic()` |
|
|
42
|
+
|
|
43
|
+
Store `editor.getJSON()`, not HTML. On the server, check it with `@writeon/server` before saving.
|
|
44
|
+
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
- Guide, recipes and API reference: the Writeon documentation site.
|
|
48
|
+
- **For AI coding assistants:** the whole documentation for this version is installed with the
|
|
49
|
+
package, in `node_modules/@writeon/core/llms-full.txt` (index: `llms.txt` next to it). Read it
|
|
50
|
+
before writing editor code instead of guessing the API.
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { lift, setBlockType, toggleMark, wrapIn } from "prosemirror-commands";
|
|
2
|
+
import { redo as redo$1, undo as undo$1 } from "prosemirror-history";
|
|
3
|
+
import { liftListItem as liftListItem$1, sinkListItem as sinkListItem$1, wrapInList } from "prosemirror-schema-list";
|
|
4
|
+
// src/url.ts
|
|
5
|
+
/** Protocols allowed in links unless configured otherwise. */
|
|
6
|
+
const DEFAULT_PROTOCOLS = [
|
|
7
|
+
"http",
|
|
8
|
+
"https",
|
|
9
|
+
"mailto",
|
|
10
|
+
"tel"
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Whether `href` is safe to put in a link.
|
|
14
|
+
*
|
|
15
|
+
* Absolute URLs must use one of `protocols`. Relative URLs (`/path`, `#id`,
|
|
16
|
+
* `page.html`) are allowed. Control characters and whitespace are removed
|
|
17
|
+
* before the check, because browsers ignore them in the scheme
|
|
18
|
+
* (`java\tscript:` runs as `javascript:`).
|
|
19
|
+
*/
|
|
20
|
+
function isAllowedUrl(href, protocols = DEFAULT_PROTOCOLS) {
|
|
21
|
+
if (typeof href !== "string") return false;
|
|
22
|
+
const cleaned = href.replace(/[\u0000-\u001f\u007f-\u009f\s]/g, "");
|
|
23
|
+
if (cleaned === "") return false;
|
|
24
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(cleaned);
|
|
25
|
+
if (!scheme) return true;
|
|
26
|
+
const name = scheme[1].toLowerCase();
|
|
27
|
+
return protocols.some((p) => p.toLowerCase() === name);
|
|
28
|
+
}
|
|
29
|
+
// src/commands/index.ts
|
|
30
|
+
/**
|
|
31
|
+
* Command factories for `editor.run()` and `editor.can()`: marks, block types,
|
|
32
|
+
* lists, links, history.
|
|
33
|
+
*
|
|
34
|
+
* @packageDocumentation
|
|
35
|
+
* @module @writeon/core/commands
|
|
36
|
+
*/
|
|
37
|
+
const markToggle = (name) => () => (state, dispatch, view) => {
|
|
38
|
+
const type = state.schema.marks[name];
|
|
39
|
+
return type ? toggleMark(type)(state, dispatch, view) : false;
|
|
40
|
+
};
|
|
41
|
+
/** Toggle bold on the selection. Requires the `bold` extension. */
|
|
42
|
+
const toggleBold = markToggle("bold");
|
|
43
|
+
/** Toggle italic on the selection. Requires the `italic` extension. */
|
|
44
|
+
const toggleItalic = markToggle("italic");
|
|
45
|
+
/** Toggle underline on the selection. Requires the `underline` extension. */
|
|
46
|
+
const toggleUnderline = markToggle("underline");
|
|
47
|
+
/** Toggle strikethrough on the selection. Requires the `strike` extension. */
|
|
48
|
+
const toggleStrike = markToggle("strike");
|
|
49
|
+
/** Toggle inline code on the selection. Requires the `code` extension. */
|
|
50
|
+
const toggleCode = markToggle("code");
|
|
51
|
+
/** Remove every mark from the selection (or from what is typed next). */
|
|
52
|
+
function clearFormatting() {
|
|
53
|
+
return (state, dispatch) => {
|
|
54
|
+
const { from, to, empty } = state.selection;
|
|
55
|
+
if (empty) {
|
|
56
|
+
var _state$storedMarks;
|
|
57
|
+
if (!((_state$storedMarks = state.storedMarks) === null || _state$storedMarks === void 0 ? void 0 : _state$storedMarks.length) && !state.selection.$from.marks().length) return false;
|
|
58
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.setStoredMarks([]));
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (!hasAnyMark(state, from, to)) return false;
|
|
62
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.removeMark(from, to).setStoredMarks([]));
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function hasAnyMark(state, from, to) {
|
|
67
|
+
let found = false;
|
|
68
|
+
state.doc.nodesBetween(from, to, (node) => {
|
|
69
|
+
if (node.marks.length > 0) found = true;
|
|
70
|
+
return !found;
|
|
71
|
+
});
|
|
72
|
+
return found;
|
|
73
|
+
}
|
|
74
|
+
/** Turn the selected blocks into paragraphs. */
|
|
75
|
+
function setParagraph() {
|
|
76
|
+
return (state, dispatch, view) => {
|
|
77
|
+
const type = state.schema.nodes.paragraph;
|
|
78
|
+
return type ? setBlockType(type)(state, dispatch, view) : false;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Turn the selected blocks into headings of `level`. Requires the `heading` extension. */
|
|
82
|
+
function setHeading(level) {
|
|
83
|
+
return (state, dispatch, view) => {
|
|
84
|
+
const type = state.schema.nodes.heading;
|
|
85
|
+
if (!type || !allowedLevels(type.spec).includes(level)) return false;
|
|
86
|
+
return setBlockType(type, { level })(state, dispatch, view);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Heading of `level` if the block is not one already, otherwise back to a paragraph. */
|
|
90
|
+
function toggleHeading(level) {
|
|
91
|
+
return (state, dispatch, view) => {
|
|
92
|
+
const parent = state.selection.$from.parent;
|
|
93
|
+
if (parent.type.name === "heading" && parent.attrs.level === level) return setParagraph()(state, dispatch, view);
|
|
94
|
+
return setHeading(level)(state, dispatch, view);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function allowedLevels(spec) {
|
|
98
|
+
var _ref;
|
|
99
|
+
return (_ref = spec.levels) !== null && _ref !== void 0 ? _ref : [
|
|
100
|
+
1,
|
|
101
|
+
2,
|
|
102
|
+
3,
|
|
103
|
+
4,
|
|
104
|
+
5,
|
|
105
|
+
6
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
/** Wrap the selection in a blockquote, or lift it out of one. Requires `blockquote`. */
|
|
109
|
+
function toggleBlockquote() {
|
|
110
|
+
return (state, dispatch, view) => {
|
|
111
|
+
const type = state.schema.nodes.blockquote;
|
|
112
|
+
if (!type) return false;
|
|
113
|
+
if (findAncestor(state.selection.$from, (node) => node.type === type)) return lift(state, dispatch);
|
|
114
|
+
return wrapIn(type)(state, dispatch, view);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Insert a line break inside the current block. Requires `hardBreak`. */
|
|
118
|
+
function insertHardBreak() {
|
|
119
|
+
return (state, dispatch) => {
|
|
120
|
+
const type = state.schema.nodes.hardBreak;
|
|
121
|
+
if (!type || !state.selection.$from.parent.inlineContent) return false;
|
|
122
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView());
|
|
123
|
+
return true;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const toggleList = (name) => () => (state, dispatch, view) => {
|
|
127
|
+
const listType = state.schema.nodes[name];
|
|
128
|
+
const itemType = state.schema.nodes.listItem;
|
|
129
|
+
if (!listType || !itemType) return false;
|
|
130
|
+
const list = findAncestor(state.selection.$from, (node) => node.type.name === "bulletList" || node.type.name === "orderedList");
|
|
131
|
+
if (list && list.node.type === listType) return liftListItem$1(itemType)(state, dispatch);
|
|
132
|
+
if (list) {
|
|
133
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.setNodeMarkup(list.pos, listType));
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return wrapInList(listType)(state, dispatch, view);
|
|
137
|
+
};
|
|
138
|
+
/** Bullet list on/off, or switch an ordered list to bullets. Requires `list`. */
|
|
139
|
+
const toggleBulletList = toggleList("bulletList");
|
|
140
|
+
/** Ordered list on/off, or switch a bullet list to numbers. Requires `list`. */
|
|
141
|
+
const toggleOrderedList = toggleList("orderedList");
|
|
142
|
+
/** Indent the list item (nest it under the previous one). Requires `list`. */
|
|
143
|
+
function sinkListItem() {
|
|
144
|
+
return (state, dispatch) => {
|
|
145
|
+
const type = state.schema.nodes.listItem;
|
|
146
|
+
return type ? sinkListItem$1(type)(state, dispatch) : false;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** Outdent the list item, leaving the list at the top level. Requires `list`. */
|
|
150
|
+
function liftListItem() {
|
|
151
|
+
return (state, dispatch) => {
|
|
152
|
+
const type = state.schema.nodes.listItem;
|
|
153
|
+
return type ? liftListItem$1(type)(state, dispatch) : false;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Link the selection to `href`. With an empty selection inside a link, the
|
|
158
|
+
* whole link is updated; with an empty selection elsewhere, `href` is inserted
|
|
159
|
+
* as linked text. Returns `false` for URLs with a protocol that is not allowed.
|
|
160
|
+
* Requires the `link` extension.
|
|
161
|
+
*/
|
|
162
|
+
function setLink(attrs) {
|
|
163
|
+
return (state, dispatch) => {
|
|
164
|
+
var _ref2, _attrs$title;
|
|
165
|
+
const type = state.schema.marks.link;
|
|
166
|
+
if (!type) return false;
|
|
167
|
+
const protocols = (_ref2 = type.spec.protocols) !== null && _ref2 !== void 0 ? _ref2 : DEFAULT_PROTOCOLS;
|
|
168
|
+
const href = attrs.href.trim();
|
|
169
|
+
if (!isAllowedUrl(href, protocols)) return false;
|
|
170
|
+
const mark = type.create({
|
|
171
|
+
href,
|
|
172
|
+
title: (_attrs$title = attrs.title) !== null && _attrs$title !== void 0 ? _attrs$title : null
|
|
173
|
+
});
|
|
174
|
+
const { from, to, empty, $from } = state.selection;
|
|
175
|
+
if (!empty) {
|
|
176
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.removeMark(from, to, type).addMark(from, to, mark));
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
const range = markRange($from, type);
|
|
180
|
+
if (range) {
|
|
181
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.removeMark(range.from, range.to, type).addMark(range.from, range.to, mark));
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
if (!$from.parent.inlineContent) return false;
|
|
185
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.insert(from, state.schema.text(href, [mark])));
|
|
186
|
+
return true;
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/** Remove the link from the selection, or the whole link the cursor is in. */
|
|
190
|
+
function unsetLink() {
|
|
191
|
+
return (state, dispatch) => {
|
|
192
|
+
const type = state.schema.marks.link;
|
|
193
|
+
if (!type) return false;
|
|
194
|
+
const { from, to, empty, $from } = state.selection;
|
|
195
|
+
const range = empty ? markRange($from, type) : {
|
|
196
|
+
from,
|
|
197
|
+
to
|
|
198
|
+
};
|
|
199
|
+
if (!range || !state.doc.rangeHasMark(range.from, range.to, type)) return false;
|
|
200
|
+
dispatch === null || dispatch === void 0 || dispatch(state.tr.removeMark(range.from, range.to, type));
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** The extent of the mark of `type` around `$pos`, merging adjacent text with an equal mark. */
|
|
205
|
+
function markRange($pos, type) {
|
|
206
|
+
const ranges = [];
|
|
207
|
+
let pos = $pos.start();
|
|
208
|
+
$pos.parent.forEach((child) => {
|
|
209
|
+
const mark = child.marks.find((m) => m.type === type);
|
|
210
|
+
const last = ranges[ranges.length - 1];
|
|
211
|
+
if (mark && last && last.to === pos && last.mark.eq(mark)) last.to = pos + child.nodeSize;
|
|
212
|
+
else if (mark) ranges.push({
|
|
213
|
+
mark,
|
|
214
|
+
from: pos,
|
|
215
|
+
to: pos + child.nodeSize
|
|
216
|
+
});
|
|
217
|
+
pos += child.nodeSize;
|
|
218
|
+
});
|
|
219
|
+
const hit = ranges.find((r) => r.from <= $pos.pos && $pos.pos <= r.to);
|
|
220
|
+
return hit ? {
|
|
221
|
+
from: hit.from,
|
|
222
|
+
to: hit.to
|
|
223
|
+
} : null;
|
|
224
|
+
}
|
|
225
|
+
/** Undo the last change. Requires the `history` extension. */
|
|
226
|
+
function undo() {
|
|
227
|
+
return (state, dispatch) => undo$1(state, dispatch);
|
|
228
|
+
}
|
|
229
|
+
/** Redo the last undone change. Requires the `history` extension. */
|
|
230
|
+
function redo() {
|
|
231
|
+
return (state, dispatch) => redo$1(state, dispatch);
|
|
232
|
+
}
|
|
233
|
+
function findAncestor($pos, predicate) {
|
|
234
|
+
for (let depth = $pos.depth; depth > 0; depth--) {
|
|
235
|
+
const node = $pos.node(depth);
|
|
236
|
+
if (predicate(node)) return {
|
|
237
|
+
node,
|
|
238
|
+
pos: $pos.before(depth)
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
export { toggleUnderline as _, setHeading as a, DEFAULT_PROTOCOLS as b, sinkListItem as c, toggleBulletList as d, toggleCode as f, toggleStrike as g, toggleOrderedList as h, redo as i, toggleBlockquote as l, toggleItalic as m, insertHardBreak as n, setLink as o, toggleHeading as p, liftListItem as r, setParagraph as s, clearFormatting as t, toggleBold as u, undo as v, isAllowedUrl as x, unsetLink as y };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { t as Command } from "./types-Dy7Bm7XX.js";
|
|
2
|
+
/** Toggle bold on the selection. Requires the `bold` extension. */
|
|
3
|
+
export declare const toggleBold: () => Command;
|
|
4
|
+
/** Toggle italic on the selection. Requires the `italic` extension. */
|
|
5
|
+
export declare const toggleItalic: () => Command;
|
|
6
|
+
/** Toggle underline on the selection. Requires the `underline` extension. */
|
|
7
|
+
export declare const toggleUnderline: () => Command;
|
|
8
|
+
/** Toggle strikethrough on the selection. Requires the `strike` extension. */
|
|
9
|
+
export declare const toggleStrike: () => Command;
|
|
10
|
+
/** Toggle inline code on the selection. Requires the `code` extension. */
|
|
11
|
+
export declare const toggleCode: () => Command;
|
|
12
|
+
/** Remove every mark from the selection (or from what is typed next). */
|
|
13
|
+
export declare function clearFormatting(): Command;
|
|
14
|
+
/** Turn the selected blocks into paragraphs. */
|
|
15
|
+
export declare function setParagraph(): Command;
|
|
16
|
+
/** Turn the selected blocks into headings of `level`. Requires the `heading` extension. */
|
|
17
|
+
export declare function setHeading(level: number): Command;
|
|
18
|
+
/** Heading of `level` if the block is not one already, otherwise back to a paragraph. */
|
|
19
|
+
export declare function toggleHeading(level: number): Command;
|
|
20
|
+
/** Wrap the selection in a blockquote, or lift it out of one. Requires `blockquote`. */
|
|
21
|
+
export declare function toggleBlockquote(): Command;
|
|
22
|
+
/** Insert a line break inside the current block. Requires `hardBreak`. */
|
|
23
|
+
export declare function insertHardBreak(): Command;
|
|
24
|
+
/** Bullet list on/off, or switch an ordered list to bullets. Requires `list`. */
|
|
25
|
+
export declare const toggleBulletList: () => Command;
|
|
26
|
+
/** Ordered list on/off, or switch a bullet list to numbers. Requires `list`. */
|
|
27
|
+
export declare const toggleOrderedList: () => Command;
|
|
28
|
+
/** Indent the list item (nest it under the previous one). Requires `list`. */
|
|
29
|
+
export declare function sinkListItem(): Command;
|
|
30
|
+
/** Outdent the list item, leaving the list at the top level. Requires `list`. */
|
|
31
|
+
export declare function liftListItem(): Command;
|
|
32
|
+
/** Attributes for `setLink()`. */
|
|
33
|
+
export interface LinkAttrs {
|
|
34
|
+
/** Link target. Trimmed; must use an allowed protocol. */
|
|
35
|
+
href: string;
|
|
36
|
+
/** Optional `title` attribute. Default: none. */
|
|
37
|
+
title?: string | null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Link the selection to `href`. With an empty selection inside a link, the
|
|
41
|
+
* whole link is updated; with an empty selection elsewhere, `href` is inserted
|
|
42
|
+
* as linked text. Returns `false` for URLs with a protocol that is not allowed.
|
|
43
|
+
* Requires the `link` extension.
|
|
44
|
+
*/
|
|
45
|
+
export declare function setLink(attrs: LinkAttrs): Command;
|
|
46
|
+
/** Remove the link from the selection, or the whole link the cursor is in. */
|
|
47
|
+
export declare function unsetLink(): Command;
|
|
48
|
+
/** Undo the last change. Requires the `history` extension. */
|
|
49
|
+
export declare function undo(): Command;
|
|
50
|
+
/** Redo the last undone change. Requires the `history` extension. */
|
|
51
|
+
export declare function redo(): Command;
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as clearFormatting, n as insertHardBreak, r as liftListItem, i as redo, a as setHeading, o as setLink, s as setParagraph, c as sinkListItem, l as toggleBlockquote, u as toggleBold, d as toggleBulletList, f as toggleCode, p as toggleHeading, m as toggleItalic, h as toggleOrderedList, g as toggleStrike, _ as toggleUnderline, v as undo, y as unsetLink } from "./commands-C_HMYTN3.js";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/migrate.ts
|
|
2
|
+
/**
|
|
3
|
+
* Version of the document format written by this release. It changes only
|
|
4
|
+
* when the library changes how documents are stored; each change ships a
|
|
5
|
+
* migration so documents saved by older versions keep loading.
|
|
6
|
+
*/
|
|
7
|
+
const SCHEMA_VERSION = 1;
|
|
8
|
+
/** Migrations shipped with the library, keyed by the version they upgrade from. */
|
|
9
|
+
const MIGRATIONS = {};
|
|
10
|
+
/**
|
|
11
|
+
* Bring a stored document up to the current `SCHEMA_VERSION`. Documents
|
|
12
|
+
* without a version are treated as version 1. Throws for documents written by
|
|
13
|
+
* a newer version of the library, which this release cannot read safely.
|
|
14
|
+
*/
|
|
15
|
+
function migrateDoc(doc) {
|
|
16
|
+
return runMigrations(doc, MIGRATIONS, 1);
|
|
17
|
+
}
|
|
18
|
+
/** @internal Exported for tests. */
|
|
19
|
+
function runMigrations(doc, migrations, target) {
|
|
20
|
+
var _doc$schemaVersion;
|
|
21
|
+
const from = (_doc$schemaVersion = doc.schemaVersion) !== null && _doc$schemaVersion !== void 0 ? _doc$schemaVersion : 1;
|
|
22
|
+
if (!Number.isInteger(from) || from < 1) throw new TypeError(`[writeon] Invalid schemaVersion: ${String(doc.schemaVersion)}.`);
|
|
23
|
+
if (from > target) throw new RangeError(`[writeon] This document uses format version ${from}, but this release only reads up to version ${target}. Update @writeon/* to load it.`);
|
|
24
|
+
let content = doc.content;
|
|
25
|
+
for (let version = from; version < target; version++) {
|
|
26
|
+
const migration = migrations[version];
|
|
27
|
+
if (!migration) throw new Error(`[writeon] Missing migration from version ${version}.`);
|
|
28
|
+
content = migration(content);
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
type: "doc",
|
|
32
|
+
schemaVersion: target,
|
|
33
|
+
content
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// src/doc.ts
|
|
37
|
+
const VALIDATE_HINT = "Documents that come from outside the editor (a request body, an import, another app) should go through validate() from @writeon/server first: it drops what the schema does not allow.";
|
|
38
|
+
/**
|
|
39
|
+
* Turn a `DocJSON` into a ProseMirror document of `schema`: migrates older
|
|
40
|
+
* format versions, accepts an empty document, and checks every node. Throws
|
|
41
|
+
* an error that says how to fix the document (or the extensions) when it does
|
|
42
|
+
* not fit the schema.
|
|
43
|
+
*
|
|
44
|
+
* The editor, `toHTML()` and `toMarkdown()` load documents with it; use it in
|
|
45
|
+
* code that builds on the schema directly.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const schema = createSchema(extensions)
|
|
50
|
+
* const node = docFromJSON(schema, saved)
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function docFromJSON(schema, json) {
|
|
54
|
+
if (typeof json !== "object" || json === null || json.type !== "doc" || !Array.isArray(json.content)) throw new TypeError("[writeon] Expected a DocJSON object ({ type: \"doc\", schemaVersion, content: [...] }). Convert HTML or Markdown first with fromHTML() / fromMarkdown().");
|
|
55
|
+
const { content } = migrateDoc(json);
|
|
56
|
+
if (content.length === 0) return schema.topNodeType.createAndFill();
|
|
57
|
+
try {
|
|
58
|
+
const doc = schema.nodeFromJSON({
|
|
59
|
+
type: "doc",
|
|
60
|
+
content
|
|
61
|
+
});
|
|
62
|
+
doc.check();
|
|
63
|
+
return doc;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw explain(error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function explain(error) {
|
|
69
|
+
var _message$match, _message$match2;
|
|
70
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
71
|
+
const node = (_message$match = message.match(/^Unknown node type: (\S+)/)) === null || _message$match === void 0 ? void 0 : _message$match[1];
|
|
72
|
+
const mark = (_message$match2 = message.match(/^There is no mark type (\S+) in this schema/)) === null || _message$match2 === void 0 ? void 0 : _message$match2[1];
|
|
73
|
+
const missing = node ? `a "${node}" node` : mark ? `a "${mark}" mark` : null;
|
|
74
|
+
if (missing) return /* @__PURE__ */ new RangeError(`[writeon] The document has ${missing}, but none of the extensions defines it. Add the extension that does to the same extensions array everywhere (editor, server, HTML/Markdown); tables, task lists, code blocks, images and mentions come from advancedSchema() in @writeon/advanced/schema. Documents that come from outside the editor (a request body, an import, another app) should go through validate() from @writeon/server first: it drops what the schema does not allow.`);
|
|
75
|
+
return /* @__PURE__ */ new RangeError(`[writeon] The document does not fit the schema: ${message}. ${VALIDATE_HINT}`);
|
|
76
|
+
}
|
|
77
|
+
export { SCHEMA_VERSION as n, migrateDoc as r, docFromJSON as t };
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { baseKeymap } from "prosemirror-commands";
|
|
2
|
+
import { inputRules } from "prosemirror-inputrules";
|
|
3
|
+
import { keymap } from "prosemirror-keymap";
|
|
4
|
+
import { Schema } from "prosemirror-model";
|
|
5
|
+
import { t as clearFormatting } from "./commands-C_HMYTN3.js";
|
|
6
|
+
// src/extension.ts
|
|
7
|
+
const DEFAULT_PRIORITY = 100;
|
|
8
|
+
/**
|
|
9
|
+
* Define an extension. The factory receives the options the user passes and
|
|
10
|
+
* returns what the extension contributes.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* export const highlight = defineExtension((options: { color?: string } = {}) => ({
|
|
15
|
+
* name: 'highlight',
|
|
16
|
+
* marks: { highlight: { parseDOM: [{ tag: 'mark' }], toDOM: () => ['mark', 0] } },
|
|
17
|
+
* }))
|
|
18
|
+
*
|
|
19
|
+
* createEditor({ extensions: [...basic(), highlight({ color: 'yellow' })], label: 'Text' })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function defineExtension(factory) {
|
|
23
|
+
return (...args) => {
|
|
24
|
+
const spec = factory(...args);
|
|
25
|
+
return {
|
|
26
|
+
name: spec.name,
|
|
27
|
+
spec
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Nodes every document needs. Always present, before any other extension. */
|
|
32
|
+
const coreExtension = defineExtension(() => ({
|
|
33
|
+
name: "core",
|
|
34
|
+
priority: -1e400,
|
|
35
|
+
nodes: {
|
|
36
|
+
doc: { content: "block+" },
|
|
37
|
+
paragraph: {
|
|
38
|
+
content: "inline*",
|
|
39
|
+
group: "block",
|
|
40
|
+
parseDOM: [{ tag: "p" }],
|
|
41
|
+
toDOM: () => ["p", 0]
|
|
42
|
+
},
|
|
43
|
+
text: { group: "inline" }
|
|
44
|
+
},
|
|
45
|
+
keymap: () => ({
|
|
46
|
+
...baseKeymap,
|
|
47
|
+
"Mod-\\": clearFormatting()
|
|
48
|
+
})
|
|
49
|
+
}))();
|
|
50
|
+
/**
|
|
51
|
+
* Build the ProseMirror schema for a list of extensions, exactly as
|
|
52
|
+
* `createEditor()` would. Use it on the server to validate or render documents
|
|
53
|
+
* with the same configuration as the client. Throws on duplicate names,
|
|
54
|
+
* missing requirements or conflicting node/mark names.
|
|
55
|
+
*/
|
|
56
|
+
function createSchema(extensions) {
|
|
57
|
+
return buildSchema([coreExtension, ...extensions]);
|
|
58
|
+
}
|
|
59
|
+
/** Validate extensions, build the schema and collect plugins. */
|
|
60
|
+
function resolveExtensions(extensions) {
|
|
61
|
+
const all = [coreExtension, ...extensions];
|
|
62
|
+
const schema = buildSchema(all);
|
|
63
|
+
return {
|
|
64
|
+
schema,
|
|
65
|
+
plugins: collectPlugins(all, schema)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const pluginCache = /* @__PURE__ */ new WeakMap();
|
|
69
|
+
/** @internal Plugins for extensions whose nodes and marks are already in `schema`, in priority order. */
|
|
70
|
+
function collectPlugins(extensions, schema) {
|
|
71
|
+
const all = extensions[0] === coreExtension ? extensions : [coreExtension, ...extensions];
|
|
72
|
+
const ctx = { schema };
|
|
73
|
+
const ordered = [...all].sort((a, b) => {
|
|
74
|
+
var _b$spec$priority, _a$spec$priority;
|
|
75
|
+
return ((_b$spec$priority = b.spec.priority) !== null && _b$spec$priority !== void 0 ? _b$spec$priority : DEFAULT_PRIORITY) - ((_a$spec$priority = a.spec.priority) !== null && _a$spec$priority !== void 0 ? _a$spec$priority : DEFAULT_PRIORITY);
|
|
76
|
+
});
|
|
77
|
+
const plugins = [];
|
|
78
|
+
const rules = ordered.flatMap((ext) => {
|
|
79
|
+
var _ext$spec$inputRules, _ext$spec$inputRules2, _ext$spec;
|
|
80
|
+
return (_ext$spec$inputRules = (_ext$spec$inputRules2 = (_ext$spec = ext.spec).inputRules) === null || _ext$spec$inputRules2 === void 0 ? void 0 : _ext$spec$inputRules2.call(_ext$spec, ctx)) !== null && _ext$spec$inputRules !== void 0 ? _ext$spec$inputRules : [];
|
|
81
|
+
});
|
|
82
|
+
if (rules.length > 0) plugins.push(inputRules({ rules }));
|
|
83
|
+
for (const ext of ordered) {
|
|
84
|
+
let cached = pluginCache.get(ext);
|
|
85
|
+
if (!cached || cached.schema !== schema) {
|
|
86
|
+
var _ext$spec$keymap, _ext$spec2, _ext$spec$plugins, _ext$spec$plugins2, _ext$spec3;
|
|
87
|
+
const own = [];
|
|
88
|
+
const bindings = (_ext$spec$keymap = (_ext$spec2 = ext.spec).keymap) === null || _ext$spec$keymap === void 0 ? void 0 : _ext$spec$keymap.call(_ext$spec2, ctx);
|
|
89
|
+
if (bindings) own.push(keymap(bindings));
|
|
90
|
+
own.push(...(_ext$spec$plugins = (_ext$spec$plugins2 = (_ext$spec3 = ext.spec).plugins) === null || _ext$spec$plugins2 === void 0 ? void 0 : _ext$spec$plugins2.call(_ext$spec3, ctx)) !== null && _ext$spec$plugins !== void 0 ? _ext$spec$plugins : []);
|
|
91
|
+
cached = {
|
|
92
|
+
schema,
|
|
93
|
+
plugins: own
|
|
94
|
+
};
|
|
95
|
+
pluginCache.set(ext, cached);
|
|
96
|
+
}
|
|
97
|
+
plugins.push(...cached.plugins);
|
|
98
|
+
}
|
|
99
|
+
return plugins;
|
|
100
|
+
}
|
|
101
|
+
function buildSchema(all) {
|
|
102
|
+
const names = /* @__PURE__ */ new Set();
|
|
103
|
+
for (const ext of all) {
|
|
104
|
+
if (names.has(ext.name)) throw new Error(`[writeon] Duplicate extension "${ext.name}". Each extension can be added once. Presets already include their extensions: configure it through the preset (e.g. basic({ ${ext.name}: { … } }) or basic({ ${ext.name}: false })) instead of adding it again.`);
|
|
105
|
+
names.add(ext.name);
|
|
106
|
+
}
|
|
107
|
+
for (const ext of all) {
|
|
108
|
+
var _ext$spec$requires;
|
|
109
|
+
const missing = ((_ext$spec$requires = ext.spec.requires) !== null && _ext$spec$requires !== void 0 ? _ext$spec$requires : []).filter((name) => !names.has(name));
|
|
110
|
+
if (missing.length > 0) throw new Error(`[writeon] Extension "${ext.name}" requires ${missing.map((n) => `"${n}"`).join(", ")}, which is not added. Add it to the same extensions array.`);
|
|
111
|
+
}
|
|
112
|
+
const nodes = {};
|
|
113
|
+
const marks = {};
|
|
114
|
+
const owner = /* @__PURE__ */ new Map();
|
|
115
|
+
const claim = (kind, key, ext) => {
|
|
116
|
+
const id = `${kind}:${key}`;
|
|
117
|
+
const prev = owner.get(id);
|
|
118
|
+
if (prev !== void 0) throw new Error(`[writeon] ${kind} "${key}" is defined by both "${prev}" and "${ext}".`);
|
|
119
|
+
owner.set(id, ext);
|
|
120
|
+
};
|
|
121
|
+
for (const ext of all) {
|
|
122
|
+
var _ext$spec$nodes, _ext$spec$marks;
|
|
123
|
+
for (const [key, spec] of Object.entries((_ext$spec$nodes = ext.spec.nodes) !== null && _ext$spec$nodes !== void 0 ? _ext$spec$nodes : {})) {
|
|
124
|
+
claim("Node", key, ext.name);
|
|
125
|
+
nodes[key] = spec;
|
|
126
|
+
}
|
|
127
|
+
for (const [key, spec] of Object.entries((_ext$spec$marks = ext.spec.marks) !== null && _ext$spec$marks !== void 0 ? _ext$spec$marks : {})) {
|
|
128
|
+
claim("Mark", key, ext.name);
|
|
129
|
+
marks[key] = spec;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const ext of all) {
|
|
133
|
+
var _ext$spec$attributes;
|
|
134
|
+
for (const attribute of (_ext$spec$attributes = ext.spec.attributes) !== null && _ext$spec$attributes !== void 0 ? _ext$spec$attributes : []) for (const type of attribute.types) {
|
|
135
|
+
var _spec$attrs;
|
|
136
|
+
const spec = nodes[type];
|
|
137
|
+
if (!spec) continue;
|
|
138
|
+
claim("Attribute", `${type}.${attribute.name}`, ext.name);
|
|
139
|
+
if ((_spec$attrs = spec.attrs) === null || _spec$attrs === void 0 ? void 0 : _spec$attrs[attribute.name]) throw new Error(`[writeon] Extension "${ext.name}" adds attribute "${attribute.name}" to "${type}", which already has it.`);
|
|
140
|
+
nodes[type] = withAttribute(spec, attribute);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return new Schema({
|
|
144
|
+
nodes,
|
|
145
|
+
marks
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** A node spec with one more attribute: stored, parsed from the DOM and rendered. */
|
|
149
|
+
function withAttribute(spec, attribute) {
|
|
150
|
+
const { name, validate } = attribute;
|
|
151
|
+
const read = (element) => {
|
|
152
|
+
const value = attribute.parse(element);
|
|
153
|
+
return value === void 0 || validate && !validate(value) ? attribute.default : value;
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
...spec,
|
|
157
|
+
attrs: {
|
|
158
|
+
...spec.attrs,
|
|
159
|
+
[name]: {
|
|
160
|
+
default: attribute.default,
|
|
161
|
+
...validate && { validate: (value) => {
|
|
162
|
+
if (value !== attribute.default && !validate(value)) throw new RangeError(`[writeon] Invalid value for ${name}: ${JSON.stringify(value)}`);
|
|
163
|
+
} }
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
...spec.parseDOM && { parseDOM: spec.parseDOM.map((rule) => {
|
|
167
|
+
const own = rule.getAttrs;
|
|
168
|
+
return {
|
|
169
|
+
...rule,
|
|
170
|
+
getAttrs: (element) => {
|
|
171
|
+
var _rule$attrs;
|
|
172
|
+
const base = own ? own(element) : (_rule$attrs = rule.attrs) !== null && _rule$attrs !== void 0 ? _rule$attrs : {};
|
|
173
|
+
if (base === false || base === null) return base;
|
|
174
|
+
return {
|
|
175
|
+
...base,
|
|
176
|
+
[name]: read(element)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}) },
|
|
181
|
+
...spec.toDOM && { toDOM: (node) => addDOMAttrs(spec.toDOM(node), attribute.render(node.attrs[name])) }
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/** Merge attributes into the outer element of an output spec (styles are joined). */
|
|
185
|
+
function addDOMAttrs(out, extra) {
|
|
186
|
+
var _dom;
|
|
187
|
+
if (!extra) return out;
|
|
188
|
+
if (Array.isArray(out)) {
|
|
189
|
+
const [tag, ...rest] = out;
|
|
190
|
+
const first = rest[0];
|
|
191
|
+
const own = first && typeof first === "object" && !Array.isArray(first) && !("nodeType" in first) ? rest.shift() : {};
|
|
192
|
+
const merged = {
|
|
193
|
+
...own,
|
|
194
|
+
...extra
|
|
195
|
+
};
|
|
196
|
+
if (own.style && extra.style) merged.style = `${own.style.replace(/;?\s*$/, ";")} ${extra.style}`;
|
|
197
|
+
return [
|
|
198
|
+
tag,
|
|
199
|
+
merged,
|
|
200
|
+
...rest
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
const element = (_dom = out.dom) !== null && _dom !== void 0 ? _dom : out;
|
|
204
|
+
if (element && typeof element.setAttribute === "function") for (const [key, value] of Object.entries(extra)) {
|
|
205
|
+
const previous = key === "style" ? element.getAttribute("style") : null;
|
|
206
|
+
element.setAttribute(key, previous ? `${previous}; ${value}` : value);
|
|
207
|
+
}
|
|
208
|
+
return out;
|
|
209
|
+
}
|
|
210
|
+
export { resolveExtensions as i, createSchema as n, defineExtension as r, collectPlugins as t };
|