@elia-ori/editor 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/dist/index.cjs +4928 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +4893 -0
- package/dist/index.js.map +1 -0
- package/dist/styles/editor.css +277 -0
- package/dist/styles/editor.css.map +1 -0
- package/dist/styles/editor.d.cts +2 -0
- package/dist/styles/editor.d.ts +2 -0
- package/package.json +92 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,4893 @@
|
|
|
1
|
+
// src/EliaEditor.tsx
|
|
2
|
+
import { useRef as useRef2, useEffect, useCallback } from "react";
|
|
3
|
+
import { useEditor, EditorContent } from "@tiptap/react";
|
|
4
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
5
|
+
import Placeholder from "@tiptap/extension-placeholder";
|
|
6
|
+
import Underline from "@tiptap/extension-underline";
|
|
7
|
+
import TextAlign from "@tiptap/extension-text-align";
|
|
8
|
+
import Link from "@tiptap/extension-link";
|
|
9
|
+
import { TextStyle } from "@tiptap/extension-text-style";
|
|
10
|
+
import { Color } from "@tiptap/extension-color";
|
|
11
|
+
import { Highlight } from "@tiptap/extension-highlight";
|
|
12
|
+
import Subscript from "@tiptap/extension-subscript";
|
|
13
|
+
import Superscript from "@tiptap/extension-superscript";
|
|
14
|
+
import TaskList from "@tiptap/extension-task-list";
|
|
15
|
+
import TaskItem from "@tiptap/extension-task-item";
|
|
16
|
+
import { Table } from "@tiptap/extension-table";
|
|
17
|
+
import { TableRow } from "@tiptap/extension-table-row";
|
|
18
|
+
import { TableHeader } from "@tiptap/extension-table-header";
|
|
19
|
+
import { TableCell } from "@tiptap/extension-table-cell";
|
|
20
|
+
import ImageResize from "tiptap-extension-resize-image";
|
|
21
|
+
|
|
22
|
+
// node_modules/@tiptap/core/dist/index.js
|
|
23
|
+
import { liftTarget } from "@tiptap/pm/transform";
|
|
24
|
+
import { createParagraphNear as originalCreateParagraphNear } from "@tiptap/pm/commands";
|
|
25
|
+
import { TextSelection } from "@tiptap/pm/state";
|
|
26
|
+
import { deleteSelection as originalDeleteSelection } from "@tiptap/pm/commands";
|
|
27
|
+
import { exitCode as originalExitCode } from "@tiptap/pm/commands";
|
|
28
|
+
import { TextSelection as TextSelection2 } from "@tiptap/pm/state";
|
|
29
|
+
import { TextSelection as TextSelection3 } from "@tiptap/pm/state";
|
|
30
|
+
import { Selection, TextSelection as TextSelection4 } from "@tiptap/pm/state";
|
|
31
|
+
import { Fragment as Fragment2 } from "@tiptap/pm/model";
|
|
32
|
+
import { DOMParser, Fragment, Node as ProseMirrorNode, Schema } from "@tiptap/pm/model";
|
|
33
|
+
import { Selection as Selection2 } from "@tiptap/pm/state";
|
|
34
|
+
import { ReplaceAroundStep, ReplaceStep } from "@tiptap/pm/transform";
|
|
35
|
+
import {
|
|
36
|
+
joinBackward as originalJoinBackward,
|
|
37
|
+
joinDown as originalJoinDown,
|
|
38
|
+
joinForward as originalJoinForward,
|
|
39
|
+
joinUp as originalJoinUp
|
|
40
|
+
} from "@tiptap/pm/commands";
|
|
41
|
+
import { joinPoint } from "@tiptap/pm/transform";
|
|
42
|
+
import { joinPoint as joinPoint2 } from "@tiptap/pm/transform";
|
|
43
|
+
import { joinTextblockBackward as originalCommand } from "@tiptap/pm/commands";
|
|
44
|
+
import { joinTextblockForward as originalCommand2 } from "@tiptap/pm/commands";
|
|
45
|
+
import { lift as originalLift } from "@tiptap/pm/commands";
|
|
46
|
+
import { liftEmptyBlock as originalLiftEmptyBlock } from "@tiptap/pm/commands";
|
|
47
|
+
import { liftListItem as originalLiftListItem } from "@tiptap/pm/schema-list";
|
|
48
|
+
import { newlineInCode as originalNewlineInCode } from "@tiptap/pm/commands";
|
|
49
|
+
import { AllSelection } from "@tiptap/pm/state";
|
|
50
|
+
import { selectNodeBackward as originalSelectNodeBackward } from "@tiptap/pm/commands";
|
|
51
|
+
import { selectNodeForward as originalSelectNodeForward } from "@tiptap/pm/commands";
|
|
52
|
+
import { selectParentNode as originalSelectParentNode } from "@tiptap/pm/commands";
|
|
53
|
+
import { selectTextblockEnd as originalSelectTextblockEnd } from "@tiptap/pm/commands";
|
|
54
|
+
import { selectTextblockStart as originalSelectTextblockStart } from "@tiptap/pm/commands";
|
|
55
|
+
import { Transform } from "@tiptap/pm/transform";
|
|
56
|
+
import { Node } from "@tiptap/pm/model";
|
|
57
|
+
import { DOMSerializer } from "@tiptap/pm/model";
|
|
58
|
+
import { Schema as Schema2 } from "@tiptap/pm/model";
|
|
59
|
+
import { DOMParser as DOMParser2 } from "@tiptap/pm/model";
|
|
60
|
+
import { Node as Node2 } from "@tiptap/pm/model";
|
|
61
|
+
import { NodeSelection } from "@tiptap/pm/state";
|
|
62
|
+
import { setBlockType } from "@tiptap/pm/commands";
|
|
63
|
+
import { NodeSelection as NodeSelection2 } from "@tiptap/pm/state";
|
|
64
|
+
import { TextSelection as TextSelection5 } from "@tiptap/pm/state";
|
|
65
|
+
import { sinkListItem as originalSinkListItem } from "@tiptap/pm/schema-list";
|
|
66
|
+
import { NodeSelection as NodeSelection3, TextSelection as TextSelection6 } from "@tiptap/pm/state";
|
|
67
|
+
import { canSplit } from "@tiptap/pm/transform";
|
|
68
|
+
import { Fragment as Fragment3, Slice } from "@tiptap/pm/model";
|
|
69
|
+
import { TextSelection as TextSelection7 } from "@tiptap/pm/state";
|
|
70
|
+
import { canSplit as canSplit2 } from "@tiptap/pm/transform";
|
|
71
|
+
import { canJoin } from "@tiptap/pm/transform";
|
|
72
|
+
import { wrapIn as originalWrapIn } from "@tiptap/pm/commands";
|
|
73
|
+
import { wrapInList as originalWrapInList } from "@tiptap/pm/schema-list";
|
|
74
|
+
import { EditorState } from "@tiptap/pm/state";
|
|
75
|
+
import { EditorView } from "@tiptap/pm/view";
|
|
76
|
+
import { keymap } from "@tiptap/pm/keymap";
|
|
77
|
+
import { Fragment as Fragment4 } from "@tiptap/pm/model";
|
|
78
|
+
import { Plugin } from "@tiptap/pm/state";
|
|
79
|
+
import { Fragment as Fragment5 } from "@tiptap/pm/model";
|
|
80
|
+
import { Plugin as Plugin2 } from "@tiptap/pm/state";
|
|
81
|
+
import { Plugin as Plugin3, PluginKey } from "@tiptap/pm/state";
|
|
82
|
+
import { RemoveMarkStep } from "@tiptap/pm/transform";
|
|
83
|
+
import { Plugin as Plugin4, PluginKey as PluginKey2 } from "@tiptap/pm/state";
|
|
84
|
+
import { Plugin as Plugin5, PluginKey as PluginKey3 } from "@tiptap/pm/state";
|
|
85
|
+
import { Plugin as Plugin6, PluginKey as PluginKey4 } from "@tiptap/pm/state";
|
|
86
|
+
import { Plugin as Plugin7, PluginKey as PluginKey5, Selection as Selection3 } from "@tiptap/pm/state";
|
|
87
|
+
import { Plugin as Plugin8, PluginKey as PluginKey6 } from "@tiptap/pm/state";
|
|
88
|
+
import { Plugin as Plugin9, PluginKey as PluginKey7 } from "@tiptap/pm/state";
|
|
89
|
+
import { Plugin as Plugin10, PluginKey as PluginKey8 } from "@tiptap/pm/state";
|
|
90
|
+
import { canJoin as canJoin2, findWrapping } from "@tiptap/pm/transform";
|
|
91
|
+
import { NodeSelection as NodeSelection4 } from "@tiptap/pm/state";
|
|
92
|
+
import { NodeSelection as NodeSelection5 } from "@tiptap/pm/state";
|
|
93
|
+
var __defProp = Object.defineProperty;
|
|
94
|
+
var __export = (target, all) => {
|
|
95
|
+
for (var name in all)
|
|
96
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
97
|
+
};
|
|
98
|
+
function createChainableState(config) {
|
|
99
|
+
const { state, transaction } = config;
|
|
100
|
+
let { selection } = transaction;
|
|
101
|
+
let { doc } = transaction;
|
|
102
|
+
let { storedMarks } = transaction;
|
|
103
|
+
return {
|
|
104
|
+
...state,
|
|
105
|
+
apply: state.apply.bind(state),
|
|
106
|
+
applyTransaction: state.applyTransaction.bind(state),
|
|
107
|
+
plugins: state.plugins,
|
|
108
|
+
schema: state.schema,
|
|
109
|
+
reconfigure: state.reconfigure.bind(state),
|
|
110
|
+
toJSON: state.toJSON.bind(state),
|
|
111
|
+
get storedMarks() {
|
|
112
|
+
return storedMarks;
|
|
113
|
+
},
|
|
114
|
+
get selection() {
|
|
115
|
+
return selection;
|
|
116
|
+
},
|
|
117
|
+
get doc() {
|
|
118
|
+
return doc;
|
|
119
|
+
},
|
|
120
|
+
get tr() {
|
|
121
|
+
selection = transaction.selection;
|
|
122
|
+
doc = transaction.doc;
|
|
123
|
+
storedMarks = transaction.storedMarks;
|
|
124
|
+
return transaction;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
var CommandManager = class {
|
|
129
|
+
constructor(props) {
|
|
130
|
+
this.editor = props.editor;
|
|
131
|
+
this.rawCommands = this.editor.extensionManager.commands;
|
|
132
|
+
this.customState = props.state;
|
|
133
|
+
}
|
|
134
|
+
get hasCustomState() {
|
|
135
|
+
return !!this.customState;
|
|
136
|
+
}
|
|
137
|
+
get state() {
|
|
138
|
+
return this.customState || this.editor.state;
|
|
139
|
+
}
|
|
140
|
+
get commands() {
|
|
141
|
+
const { rawCommands, editor, state } = this;
|
|
142
|
+
const { view } = editor;
|
|
143
|
+
const { tr } = state;
|
|
144
|
+
const props = this.buildProps(tr);
|
|
145
|
+
return Object.fromEntries(
|
|
146
|
+
Object.entries(rawCommands).map(([name, command2]) => {
|
|
147
|
+
const method = (...args) => {
|
|
148
|
+
const callback = command2(...args)(props);
|
|
149
|
+
if (!tr.getMeta("preventDispatch") && !this.hasCustomState) {
|
|
150
|
+
view.dispatch(tr);
|
|
151
|
+
}
|
|
152
|
+
return callback;
|
|
153
|
+
};
|
|
154
|
+
return [name, method];
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
get chain() {
|
|
159
|
+
return () => this.createChain();
|
|
160
|
+
}
|
|
161
|
+
get can() {
|
|
162
|
+
return () => this.createCan();
|
|
163
|
+
}
|
|
164
|
+
createChain(startTr, shouldDispatch = true) {
|
|
165
|
+
const { rawCommands, editor, state } = this;
|
|
166
|
+
const { view } = editor;
|
|
167
|
+
const callbacks = [];
|
|
168
|
+
const hasStartTransaction = !!startTr;
|
|
169
|
+
const tr = startTr || state.tr;
|
|
170
|
+
const run3 = () => {
|
|
171
|
+
if (!hasStartTransaction && shouldDispatch && !tr.getMeta("preventDispatch") && !this.hasCustomState) {
|
|
172
|
+
view.dispatch(tr);
|
|
173
|
+
}
|
|
174
|
+
return callbacks.every((callback) => callback === true);
|
|
175
|
+
};
|
|
176
|
+
const chain = {
|
|
177
|
+
...Object.fromEntries(
|
|
178
|
+
Object.entries(rawCommands).map(([name, command2]) => {
|
|
179
|
+
const chainedCommand = (...args) => {
|
|
180
|
+
const props = this.buildProps(tr, shouldDispatch);
|
|
181
|
+
const callback = command2(...args)(props);
|
|
182
|
+
callbacks.push(callback);
|
|
183
|
+
return chain;
|
|
184
|
+
};
|
|
185
|
+
return [name, chainedCommand];
|
|
186
|
+
})
|
|
187
|
+
),
|
|
188
|
+
run: run3
|
|
189
|
+
};
|
|
190
|
+
return chain;
|
|
191
|
+
}
|
|
192
|
+
createCan(startTr) {
|
|
193
|
+
const { rawCommands, state } = this;
|
|
194
|
+
const dispatch = false;
|
|
195
|
+
const tr = startTr || state.tr;
|
|
196
|
+
const props = this.buildProps(tr, dispatch);
|
|
197
|
+
const formattedCommands = Object.fromEntries(
|
|
198
|
+
Object.entries(rawCommands).map(([name, command2]) => {
|
|
199
|
+
return [name, (...args) => command2(...args)({ ...props, dispatch: void 0 })];
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
return {
|
|
203
|
+
...formattedCommands,
|
|
204
|
+
chain: () => this.createChain(tr, dispatch)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
buildProps(tr, shouldDispatch = true) {
|
|
208
|
+
const { rawCommands, editor, state } = this;
|
|
209
|
+
const { view } = editor;
|
|
210
|
+
const props = {
|
|
211
|
+
tr,
|
|
212
|
+
editor,
|
|
213
|
+
view,
|
|
214
|
+
state: createChainableState({
|
|
215
|
+
state,
|
|
216
|
+
transaction: tr
|
|
217
|
+
}),
|
|
218
|
+
dispatch: shouldDispatch ? () => void 0 : void 0,
|
|
219
|
+
chain: () => this.createChain(tr, shouldDispatch),
|
|
220
|
+
can: () => this.createCan(tr),
|
|
221
|
+
get commands() {
|
|
222
|
+
return Object.fromEntries(
|
|
223
|
+
Object.entries(rawCommands).map(([name, command2]) => {
|
|
224
|
+
return [name, (...args) => command2(...args)(props)];
|
|
225
|
+
})
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
return props;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
var commands_exports = {};
|
|
233
|
+
__export(commands_exports, {
|
|
234
|
+
blur: () => blur,
|
|
235
|
+
clearContent: () => clearContent,
|
|
236
|
+
clearNodes: () => clearNodes,
|
|
237
|
+
command: () => command,
|
|
238
|
+
createParagraphNear: () => createParagraphNear,
|
|
239
|
+
cut: () => cut,
|
|
240
|
+
deleteCurrentNode: () => deleteCurrentNode,
|
|
241
|
+
deleteNode: () => deleteNode,
|
|
242
|
+
deleteRange: () => deleteRange,
|
|
243
|
+
deleteSelection: () => deleteSelection,
|
|
244
|
+
enter: () => enter,
|
|
245
|
+
exitCode: () => exitCode,
|
|
246
|
+
extendMarkRange: () => extendMarkRange,
|
|
247
|
+
first: () => first,
|
|
248
|
+
focus: () => focus,
|
|
249
|
+
forEach: () => forEach,
|
|
250
|
+
insertContent: () => insertContent,
|
|
251
|
+
insertContentAt: () => insertContentAt,
|
|
252
|
+
joinBackward: () => joinBackward,
|
|
253
|
+
joinDown: () => joinDown,
|
|
254
|
+
joinForward: () => joinForward,
|
|
255
|
+
joinItemBackward: () => joinItemBackward,
|
|
256
|
+
joinItemForward: () => joinItemForward,
|
|
257
|
+
joinTextblockBackward: () => joinTextblockBackward,
|
|
258
|
+
joinTextblockForward: () => joinTextblockForward,
|
|
259
|
+
joinUp: () => joinUp,
|
|
260
|
+
keyboardShortcut: () => keyboardShortcut,
|
|
261
|
+
lift: () => lift,
|
|
262
|
+
liftEmptyBlock: () => liftEmptyBlock,
|
|
263
|
+
liftListItem: () => liftListItem,
|
|
264
|
+
newlineInCode: () => newlineInCode,
|
|
265
|
+
resetAttributes: () => resetAttributes,
|
|
266
|
+
scrollIntoView: () => scrollIntoView,
|
|
267
|
+
selectAll: () => selectAll,
|
|
268
|
+
selectNodeBackward: () => selectNodeBackward,
|
|
269
|
+
selectNodeForward: () => selectNodeForward,
|
|
270
|
+
selectParentNode: () => selectParentNode,
|
|
271
|
+
selectTextblockEnd: () => selectTextblockEnd,
|
|
272
|
+
selectTextblockStart: () => selectTextblockStart,
|
|
273
|
+
setContent: () => setContent,
|
|
274
|
+
setMark: () => setMark,
|
|
275
|
+
setMeta: () => setMeta,
|
|
276
|
+
setNode: () => setNode,
|
|
277
|
+
setNodeSelection: () => setNodeSelection,
|
|
278
|
+
setTextDirection: () => setTextDirection,
|
|
279
|
+
setTextSelection: () => setTextSelection,
|
|
280
|
+
sinkListItem: () => sinkListItem,
|
|
281
|
+
splitBlock: () => splitBlock,
|
|
282
|
+
splitListItem: () => splitListItem,
|
|
283
|
+
toggleList: () => toggleList,
|
|
284
|
+
toggleMark: () => toggleMark,
|
|
285
|
+
toggleNode: () => toggleNode,
|
|
286
|
+
toggleWrap: () => toggleWrap,
|
|
287
|
+
undoInputRule: () => undoInputRule,
|
|
288
|
+
unsetAllMarks: () => unsetAllMarks,
|
|
289
|
+
unsetMark: () => unsetMark,
|
|
290
|
+
unsetTextDirection: () => unsetTextDirection,
|
|
291
|
+
updateAttributes: () => updateAttributes,
|
|
292
|
+
wrapIn: () => wrapIn,
|
|
293
|
+
wrapInList: () => wrapInList
|
|
294
|
+
});
|
|
295
|
+
var blur = () => ({ editor, view }) => {
|
|
296
|
+
requestAnimationFrame(() => {
|
|
297
|
+
var _a;
|
|
298
|
+
if (!editor.isDestroyed) {
|
|
299
|
+
;
|
|
300
|
+
view.dom.blur();
|
|
301
|
+
(_a = window == null ? void 0 : window.getSelection()) == null ? void 0 : _a.removeAllRanges();
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
return true;
|
|
305
|
+
};
|
|
306
|
+
var clearContent = (emitUpdate = true) => ({ commands }) => {
|
|
307
|
+
return commands.setContent("", { emitUpdate });
|
|
308
|
+
};
|
|
309
|
+
var clearNodes = () => ({ state, tr, dispatch }) => {
|
|
310
|
+
const { selection } = tr;
|
|
311
|
+
const { ranges } = selection;
|
|
312
|
+
if (!dispatch) {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
ranges.forEach(({ $from, $to }) => {
|
|
316
|
+
state.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
|
|
317
|
+
if (node.type.isText) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const { doc, mapping } = tr;
|
|
321
|
+
const $mappedFrom = doc.resolve(mapping.map(pos));
|
|
322
|
+
const $mappedTo = doc.resolve(mapping.map(pos + node.nodeSize));
|
|
323
|
+
const nodeRange = $mappedFrom.blockRange($mappedTo);
|
|
324
|
+
if (!nodeRange) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const targetLiftDepth = liftTarget(nodeRange);
|
|
328
|
+
if (node.type.isTextblock) {
|
|
329
|
+
const { defaultType } = $mappedFrom.parent.contentMatchAt($mappedFrom.index());
|
|
330
|
+
tr.setNodeMarkup(nodeRange.start, defaultType);
|
|
331
|
+
}
|
|
332
|
+
if (targetLiftDepth || targetLiftDepth === 0) {
|
|
333
|
+
tr.lift(nodeRange, targetLiftDepth);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
return true;
|
|
338
|
+
};
|
|
339
|
+
var command = (fn) => (props) => {
|
|
340
|
+
return fn(props);
|
|
341
|
+
};
|
|
342
|
+
var createParagraphNear = () => ({ state, dispatch }) => {
|
|
343
|
+
return originalCreateParagraphNear(state, dispatch);
|
|
344
|
+
};
|
|
345
|
+
var cut = (originRange, targetPos) => ({ editor, tr }) => {
|
|
346
|
+
const { state } = editor;
|
|
347
|
+
const contentSlice = state.doc.slice(originRange.from, originRange.to);
|
|
348
|
+
tr.deleteRange(originRange.from, originRange.to);
|
|
349
|
+
const newPos = tr.mapping.map(targetPos);
|
|
350
|
+
tr.insert(newPos, contentSlice.content);
|
|
351
|
+
tr.setSelection(new TextSelection(tr.doc.resolve(Math.max(newPos - 1, 0))));
|
|
352
|
+
return true;
|
|
353
|
+
};
|
|
354
|
+
var deleteCurrentNode = () => ({ tr, dispatch }) => {
|
|
355
|
+
const { selection } = tr;
|
|
356
|
+
const currentNode = selection.$anchor.node();
|
|
357
|
+
if (currentNode.content.size > 0) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
const $pos = tr.selection.$anchor;
|
|
361
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
362
|
+
const node = $pos.node(depth);
|
|
363
|
+
if (node.type === currentNode.type) {
|
|
364
|
+
if (dispatch) {
|
|
365
|
+
const from = $pos.before(depth);
|
|
366
|
+
const to = $pos.after(depth);
|
|
367
|
+
tr.delete(from, to).scrollIntoView();
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return false;
|
|
373
|
+
};
|
|
374
|
+
function getNodeType(nameOrType, schema) {
|
|
375
|
+
if (typeof nameOrType === "string") {
|
|
376
|
+
if (!schema.nodes[nameOrType]) {
|
|
377
|
+
throw Error(`There is no node type named '${nameOrType}'. Maybe you forgot to add the extension?`);
|
|
378
|
+
}
|
|
379
|
+
return schema.nodes[nameOrType];
|
|
380
|
+
}
|
|
381
|
+
return nameOrType;
|
|
382
|
+
}
|
|
383
|
+
var deleteNode = (typeOrName) => ({ tr, state, dispatch }) => {
|
|
384
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
385
|
+
const $pos = tr.selection.$anchor;
|
|
386
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
387
|
+
const node = $pos.node(depth);
|
|
388
|
+
if (node.type === type) {
|
|
389
|
+
if (dispatch) {
|
|
390
|
+
const from = $pos.before(depth);
|
|
391
|
+
const to = $pos.after(depth);
|
|
392
|
+
tr.delete(from, to).scrollIntoView();
|
|
393
|
+
}
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return false;
|
|
398
|
+
};
|
|
399
|
+
var deleteRange = (range) => ({ tr, dispatch }) => {
|
|
400
|
+
const { from, to } = range;
|
|
401
|
+
if (dispatch) {
|
|
402
|
+
tr.delete(from, to);
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
};
|
|
406
|
+
var deleteSelection = () => ({ state, dispatch }) => {
|
|
407
|
+
return originalDeleteSelection(state, dispatch);
|
|
408
|
+
};
|
|
409
|
+
var enter = () => ({ commands }) => {
|
|
410
|
+
return commands.keyboardShortcut("Enter");
|
|
411
|
+
};
|
|
412
|
+
var exitCode = () => ({ state, dispatch }) => {
|
|
413
|
+
return originalExitCode(state, dispatch);
|
|
414
|
+
};
|
|
415
|
+
function isRegExp(value) {
|
|
416
|
+
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
417
|
+
}
|
|
418
|
+
function objectIncludes(object1, object2, options = { strict: true }) {
|
|
419
|
+
const keys = Object.keys(object2);
|
|
420
|
+
if (!keys.length) {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
return keys.every((key) => {
|
|
424
|
+
if (options.strict) {
|
|
425
|
+
return object2[key] === object1[key];
|
|
426
|
+
}
|
|
427
|
+
if (isRegExp(object2[key])) {
|
|
428
|
+
return object2[key].test(object1[key]);
|
|
429
|
+
}
|
|
430
|
+
return object2[key] === object1[key];
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
function findMarkInSet(marks, type, attributes = {}) {
|
|
434
|
+
return marks.find((item) => {
|
|
435
|
+
return item.type === type && objectIncludes(
|
|
436
|
+
// Only check equality for the attributes that are provided
|
|
437
|
+
Object.fromEntries(Object.keys(attributes).map((k) => [k, item.attrs[k]])),
|
|
438
|
+
attributes
|
|
439
|
+
);
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
function isMarkInSet(marks, type, attributes = {}) {
|
|
443
|
+
return !!findMarkInSet(marks, type, attributes);
|
|
444
|
+
}
|
|
445
|
+
function getMarkRange($pos, type, attributes) {
|
|
446
|
+
var _a;
|
|
447
|
+
if (!$pos || !type) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
let start = $pos.parent.childAfter($pos.parentOffset);
|
|
451
|
+
if (!start.node || !start.node.marks.some((mark2) => mark2.type === type)) {
|
|
452
|
+
start = $pos.parent.childBefore($pos.parentOffset);
|
|
453
|
+
}
|
|
454
|
+
if (!start.node || !start.node.marks.some((mark2) => mark2.type === type)) {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
attributes = attributes || ((_a = start.node.marks[0]) == null ? void 0 : _a.attrs);
|
|
458
|
+
const mark = findMarkInSet([...start.node.marks], type, attributes);
|
|
459
|
+
if (!mark) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
let startIndex = start.index;
|
|
463
|
+
let startPos = $pos.start() + start.offset;
|
|
464
|
+
let endIndex = startIndex + 1;
|
|
465
|
+
let endPos = startPos + start.node.nodeSize;
|
|
466
|
+
while (startIndex > 0 && isMarkInSet([...$pos.parent.child(startIndex - 1).marks], type, attributes)) {
|
|
467
|
+
startIndex -= 1;
|
|
468
|
+
startPos -= $pos.parent.child(startIndex).nodeSize;
|
|
469
|
+
}
|
|
470
|
+
while (endIndex < $pos.parent.childCount && isMarkInSet([...$pos.parent.child(endIndex).marks], type, attributes)) {
|
|
471
|
+
endPos += $pos.parent.child(endIndex).nodeSize;
|
|
472
|
+
endIndex += 1;
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
from: startPos,
|
|
476
|
+
to: endPos
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
function getMarkType(nameOrType, schema) {
|
|
480
|
+
if (typeof nameOrType === "string") {
|
|
481
|
+
if (!schema.marks[nameOrType]) {
|
|
482
|
+
throw Error(`There is no mark type named '${nameOrType}'. Maybe you forgot to add the extension?`);
|
|
483
|
+
}
|
|
484
|
+
return schema.marks[nameOrType];
|
|
485
|
+
}
|
|
486
|
+
return nameOrType;
|
|
487
|
+
}
|
|
488
|
+
var extendMarkRange = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
|
489
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
490
|
+
const { doc, selection } = tr;
|
|
491
|
+
const { $from, from, to } = selection;
|
|
492
|
+
if (dispatch) {
|
|
493
|
+
const range = getMarkRange($from, type, attributes);
|
|
494
|
+
if (range && range.from <= from && range.to >= to) {
|
|
495
|
+
const newSelection = TextSelection2.create(doc, range.from, range.to);
|
|
496
|
+
tr.setSelection(newSelection);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return true;
|
|
500
|
+
};
|
|
501
|
+
var first = (commands) => (props) => {
|
|
502
|
+
const items = typeof commands === "function" ? commands(props) : commands;
|
|
503
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
504
|
+
if (items[i](props)) {
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return false;
|
|
509
|
+
};
|
|
510
|
+
function isTextSelection(value) {
|
|
511
|
+
return value instanceof TextSelection3;
|
|
512
|
+
}
|
|
513
|
+
function minMax(value = 0, min = 0, max = 0) {
|
|
514
|
+
return Math.min(Math.max(value, min), max);
|
|
515
|
+
}
|
|
516
|
+
function resolveFocusPosition(doc, position = null) {
|
|
517
|
+
if (!position) {
|
|
518
|
+
return null;
|
|
519
|
+
}
|
|
520
|
+
const selectionAtStart = Selection.atStart(doc);
|
|
521
|
+
const selectionAtEnd = Selection.atEnd(doc);
|
|
522
|
+
if (position === "start" || position === true) {
|
|
523
|
+
return selectionAtStart;
|
|
524
|
+
}
|
|
525
|
+
if (position === "end") {
|
|
526
|
+
return selectionAtEnd;
|
|
527
|
+
}
|
|
528
|
+
const minPos = selectionAtStart.from;
|
|
529
|
+
const maxPos = selectionAtEnd.to;
|
|
530
|
+
if (position === "all") {
|
|
531
|
+
return TextSelection4.create(doc, minMax(0, minPos, maxPos), minMax(doc.content.size, minPos, maxPos));
|
|
532
|
+
}
|
|
533
|
+
return TextSelection4.create(doc, minMax(position, minPos, maxPos), minMax(position, minPos, maxPos));
|
|
534
|
+
}
|
|
535
|
+
function isAndroid() {
|
|
536
|
+
return navigator.platform === "Android" || /android/i.test(navigator.userAgent);
|
|
537
|
+
}
|
|
538
|
+
function isiOS() {
|
|
539
|
+
return ["iPad Simulator", "iPhone Simulator", "iPod Simulator", "iPad", "iPhone", "iPod"].includes(navigator.platform) || // iPad on iOS 13 detection
|
|
540
|
+
navigator.userAgent.includes("Mac") && "ontouchend" in document;
|
|
541
|
+
}
|
|
542
|
+
function isSafari() {
|
|
543
|
+
return typeof navigator !== "undefined" ? /^((?!chrome|android).)*safari/i.test(navigator.userAgent) : false;
|
|
544
|
+
}
|
|
545
|
+
var focus = (position = null, options = {}) => ({ editor, view, tr, dispatch }) => {
|
|
546
|
+
options = {
|
|
547
|
+
scrollIntoView: true,
|
|
548
|
+
...options
|
|
549
|
+
};
|
|
550
|
+
const delayedFocus = () => {
|
|
551
|
+
if (isiOS() || isAndroid()) {
|
|
552
|
+
;
|
|
553
|
+
view.dom.focus();
|
|
554
|
+
}
|
|
555
|
+
if (isSafari() && !isiOS() && !isAndroid()) {
|
|
556
|
+
;
|
|
557
|
+
view.dom.focus({ preventScroll: true });
|
|
558
|
+
}
|
|
559
|
+
requestAnimationFrame(() => {
|
|
560
|
+
if (!editor.isDestroyed) {
|
|
561
|
+
view.focus();
|
|
562
|
+
if (options == null ? void 0 : options.scrollIntoView) {
|
|
563
|
+
editor.commands.scrollIntoView();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
if (view.hasFocus() && position === null || position === false) {
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
|
|
572
|
+
delayedFocus();
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
const selection = resolveFocusPosition(tr.doc, position) || editor.state.selection;
|
|
576
|
+
const isSameSelection = editor.state.selection.eq(selection);
|
|
577
|
+
if (dispatch) {
|
|
578
|
+
if (!isSameSelection) {
|
|
579
|
+
tr.setSelection(selection);
|
|
580
|
+
}
|
|
581
|
+
if (isSameSelection && tr.storedMarks) {
|
|
582
|
+
tr.setStoredMarks(tr.storedMarks);
|
|
583
|
+
}
|
|
584
|
+
delayedFocus();
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
};
|
|
588
|
+
var forEach = (items, fn) => (props) => {
|
|
589
|
+
return items.every((item, index) => fn(item, { ...props, index }));
|
|
590
|
+
};
|
|
591
|
+
var insertContent = (value, options) => ({ tr, commands }) => {
|
|
592
|
+
return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
|
|
593
|
+
};
|
|
594
|
+
var removeWhitespaces = (node) => {
|
|
595
|
+
const children = node.childNodes;
|
|
596
|
+
for (let i = children.length - 1; i >= 0; i -= 1) {
|
|
597
|
+
const child = children[i];
|
|
598
|
+
if (child.nodeType === 3 && child.nodeValue && /^(\n\s\s|\n)$/.test(child.nodeValue)) {
|
|
599
|
+
node.removeChild(child);
|
|
600
|
+
} else if (child.nodeType === 1) {
|
|
601
|
+
removeWhitespaces(child);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return node;
|
|
605
|
+
};
|
|
606
|
+
function elementFromString(value) {
|
|
607
|
+
if (typeof window === "undefined") {
|
|
608
|
+
throw new Error("[tiptap error]: there is no window object available, so this function cannot be used");
|
|
609
|
+
}
|
|
610
|
+
const wrappedValue = `<body>${value}</body>`;
|
|
611
|
+
const html = new window.DOMParser().parseFromString(wrappedValue, "text/html").body;
|
|
612
|
+
return removeWhitespaces(html);
|
|
613
|
+
}
|
|
614
|
+
function createNodeFromContent(content, schema, options) {
|
|
615
|
+
if (content instanceof ProseMirrorNode || content instanceof Fragment) {
|
|
616
|
+
return content;
|
|
617
|
+
}
|
|
618
|
+
options = {
|
|
619
|
+
slice: true,
|
|
620
|
+
parseOptions: {},
|
|
621
|
+
...options
|
|
622
|
+
};
|
|
623
|
+
const isJSONContent = typeof content === "object" && content !== null;
|
|
624
|
+
const isTextContent = typeof content === "string";
|
|
625
|
+
if (isJSONContent) {
|
|
626
|
+
try {
|
|
627
|
+
const isArrayContent = Array.isArray(content) && content.length > 0;
|
|
628
|
+
if (isArrayContent) {
|
|
629
|
+
return Fragment.fromArray(content.map((item) => schema.nodeFromJSON(item)));
|
|
630
|
+
}
|
|
631
|
+
const node = schema.nodeFromJSON(content);
|
|
632
|
+
if (options.errorOnInvalidContent) {
|
|
633
|
+
node.check();
|
|
634
|
+
}
|
|
635
|
+
return node;
|
|
636
|
+
} catch (error) {
|
|
637
|
+
if (options.errorOnInvalidContent) {
|
|
638
|
+
throw new Error("[tiptap error]: Invalid JSON content", { cause: error });
|
|
639
|
+
}
|
|
640
|
+
console.warn("[tiptap warn]: Invalid content.", "Passed value:", content, "Error:", error);
|
|
641
|
+
return createNodeFromContent("", schema, options);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (isTextContent) {
|
|
645
|
+
if (options.errorOnInvalidContent) {
|
|
646
|
+
let hasInvalidContent = false;
|
|
647
|
+
let invalidContent = "";
|
|
648
|
+
const contentCheckSchema = new Schema({
|
|
649
|
+
topNode: schema.spec.topNode,
|
|
650
|
+
marks: schema.spec.marks,
|
|
651
|
+
// Prosemirror's schemas are executed such that: the last to execute, matches last
|
|
652
|
+
// This means that we can add a catch-all node at the end of the schema to catch any content that we don't know how to handle
|
|
653
|
+
nodes: schema.spec.nodes.append({
|
|
654
|
+
__tiptap__private__unknown__catch__all__node: {
|
|
655
|
+
content: "inline*",
|
|
656
|
+
group: "block",
|
|
657
|
+
parseDOM: [
|
|
658
|
+
{
|
|
659
|
+
tag: "*",
|
|
660
|
+
getAttrs: (e) => {
|
|
661
|
+
hasInvalidContent = true;
|
|
662
|
+
invalidContent = typeof e === "string" ? e : e.outerHTML;
|
|
663
|
+
return null;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
]
|
|
667
|
+
}
|
|
668
|
+
})
|
|
669
|
+
});
|
|
670
|
+
if (options.slice) {
|
|
671
|
+
DOMParser.fromSchema(contentCheckSchema).parseSlice(elementFromString(content), options.parseOptions);
|
|
672
|
+
} else {
|
|
673
|
+
DOMParser.fromSchema(contentCheckSchema).parse(elementFromString(content), options.parseOptions);
|
|
674
|
+
}
|
|
675
|
+
if (options.errorOnInvalidContent && hasInvalidContent) {
|
|
676
|
+
throw new Error("[tiptap error]: Invalid HTML content", {
|
|
677
|
+
cause: new Error(`Invalid element found: ${invalidContent}`)
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const parser = DOMParser.fromSchema(schema);
|
|
682
|
+
if (options.slice) {
|
|
683
|
+
return parser.parseSlice(elementFromString(content), options.parseOptions).content;
|
|
684
|
+
}
|
|
685
|
+
return parser.parse(elementFromString(content), options.parseOptions);
|
|
686
|
+
}
|
|
687
|
+
return createNodeFromContent("", schema, options);
|
|
688
|
+
}
|
|
689
|
+
function selectionToInsertionEnd(tr, startLen, bias) {
|
|
690
|
+
const last = tr.steps.length - 1;
|
|
691
|
+
if (last < startLen) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const step = tr.steps[last];
|
|
695
|
+
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) {
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
const map = tr.mapping.maps[last];
|
|
699
|
+
let end = 0;
|
|
700
|
+
map.forEach((_from, _to, _newFrom, newTo) => {
|
|
701
|
+
if (end === 0) {
|
|
702
|
+
end = newTo;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
tr.setSelection(Selection2.near(tr.doc.resolve(end), bias));
|
|
706
|
+
}
|
|
707
|
+
var isFragment = (nodeOrFragment) => {
|
|
708
|
+
return !("type" in nodeOrFragment);
|
|
709
|
+
};
|
|
710
|
+
var insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) => {
|
|
711
|
+
var _a;
|
|
712
|
+
if (dispatch) {
|
|
713
|
+
options = {
|
|
714
|
+
parseOptions: editor.options.parseOptions,
|
|
715
|
+
updateSelection: true,
|
|
716
|
+
applyInputRules: false,
|
|
717
|
+
applyPasteRules: false,
|
|
718
|
+
...options
|
|
719
|
+
};
|
|
720
|
+
let content;
|
|
721
|
+
const emitContentError = (error) => {
|
|
722
|
+
editor.emit("contentError", {
|
|
723
|
+
editor,
|
|
724
|
+
error,
|
|
725
|
+
disableCollaboration: () => {
|
|
726
|
+
if ("collaboration" in editor.storage && typeof editor.storage.collaboration === "object" && editor.storage.collaboration) {
|
|
727
|
+
;
|
|
728
|
+
editor.storage.collaboration.isDisabled = true;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
};
|
|
733
|
+
const parseOptions = {
|
|
734
|
+
preserveWhitespace: "full",
|
|
735
|
+
...options.parseOptions
|
|
736
|
+
};
|
|
737
|
+
if (!options.errorOnInvalidContent && !editor.options.enableContentCheck && editor.options.emitContentError) {
|
|
738
|
+
try {
|
|
739
|
+
createNodeFromContent(value, editor.schema, {
|
|
740
|
+
parseOptions,
|
|
741
|
+
errorOnInvalidContent: true
|
|
742
|
+
});
|
|
743
|
+
} catch (e) {
|
|
744
|
+
emitContentError(e);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
try {
|
|
748
|
+
content = createNodeFromContent(value, editor.schema, {
|
|
749
|
+
parseOptions,
|
|
750
|
+
errorOnInvalidContent: (_a = options.errorOnInvalidContent) != null ? _a : editor.options.enableContentCheck
|
|
751
|
+
});
|
|
752
|
+
} catch (e) {
|
|
753
|
+
emitContentError(e);
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
let { from, to } = typeof position === "number" ? { from: position, to: position } : { from: position.from, to: position.to };
|
|
757
|
+
let isOnlyTextContent = true;
|
|
758
|
+
let isOnlyBlockContent = true;
|
|
759
|
+
const nodes = isFragment(content) ? content : [content];
|
|
760
|
+
nodes.forEach((node) => {
|
|
761
|
+
node.check();
|
|
762
|
+
isOnlyTextContent = isOnlyTextContent ? node.isText && node.marks.length === 0 : false;
|
|
763
|
+
isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false;
|
|
764
|
+
});
|
|
765
|
+
if (from === to && isOnlyBlockContent) {
|
|
766
|
+
const { parent } = tr.doc.resolve(from);
|
|
767
|
+
const isEmptyTextBlock = parent.isTextblock && !parent.type.spec.code && !parent.childCount;
|
|
768
|
+
if (isEmptyTextBlock) {
|
|
769
|
+
from -= 1;
|
|
770
|
+
to += 1;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
let newContent;
|
|
774
|
+
if (isOnlyTextContent) {
|
|
775
|
+
if (Array.isArray(value)) {
|
|
776
|
+
newContent = value.map((v) => v.text || "").join("");
|
|
777
|
+
} else if (value instanceof Fragment2) {
|
|
778
|
+
let text = "";
|
|
779
|
+
value.forEach((node) => {
|
|
780
|
+
if (node.text) {
|
|
781
|
+
text += node.text;
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
newContent = text;
|
|
785
|
+
} else if (typeof value === "object" && !!value && !!value.text) {
|
|
786
|
+
newContent = value.text;
|
|
787
|
+
} else {
|
|
788
|
+
newContent = value;
|
|
789
|
+
}
|
|
790
|
+
tr.insertText(newContent, from, to);
|
|
791
|
+
} else {
|
|
792
|
+
newContent = content;
|
|
793
|
+
const $from = tr.doc.resolve(from);
|
|
794
|
+
const $fromNode = $from.node();
|
|
795
|
+
const fromSelectionAtStart = $from.parentOffset === 0;
|
|
796
|
+
const isTextSelection2 = $fromNode.isText || $fromNode.isTextblock;
|
|
797
|
+
const hasContent = $fromNode.content.size > 0;
|
|
798
|
+
if (fromSelectionAtStart && isTextSelection2 && hasContent) {
|
|
799
|
+
from = Math.max(0, from - 1);
|
|
800
|
+
}
|
|
801
|
+
tr.replaceWith(from, to, newContent);
|
|
802
|
+
}
|
|
803
|
+
if (options.updateSelection) {
|
|
804
|
+
selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
|
|
805
|
+
}
|
|
806
|
+
if (options.applyInputRules) {
|
|
807
|
+
tr.setMeta("applyInputRules", { from, text: newContent });
|
|
808
|
+
}
|
|
809
|
+
if (options.applyPasteRules) {
|
|
810
|
+
tr.setMeta("applyPasteRules", { from, text: newContent });
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return true;
|
|
814
|
+
};
|
|
815
|
+
var joinUp = () => ({ state, dispatch }) => {
|
|
816
|
+
return originalJoinUp(state, dispatch);
|
|
817
|
+
};
|
|
818
|
+
var joinDown = () => ({ state, dispatch }) => {
|
|
819
|
+
return originalJoinDown(state, dispatch);
|
|
820
|
+
};
|
|
821
|
+
var joinBackward = () => ({ state, dispatch }) => {
|
|
822
|
+
return originalJoinBackward(state, dispatch);
|
|
823
|
+
};
|
|
824
|
+
var joinForward = () => ({ state, dispatch }) => {
|
|
825
|
+
return originalJoinForward(state, dispatch);
|
|
826
|
+
};
|
|
827
|
+
var joinItemBackward = () => ({ state, dispatch, tr }) => {
|
|
828
|
+
try {
|
|
829
|
+
const point = joinPoint(state.doc, state.selection.$from.pos, -1);
|
|
830
|
+
if (point === null || point === void 0) {
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
tr.join(point, 2);
|
|
834
|
+
if (dispatch) {
|
|
835
|
+
dispatch(tr);
|
|
836
|
+
}
|
|
837
|
+
return true;
|
|
838
|
+
} catch {
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
var joinItemForward = () => ({ state, dispatch, tr }) => {
|
|
843
|
+
try {
|
|
844
|
+
const point = joinPoint2(state.doc, state.selection.$from.pos, 1);
|
|
845
|
+
if (point === null || point === void 0) {
|
|
846
|
+
return false;
|
|
847
|
+
}
|
|
848
|
+
tr.join(point, 2);
|
|
849
|
+
if (dispatch) {
|
|
850
|
+
dispatch(tr);
|
|
851
|
+
}
|
|
852
|
+
return true;
|
|
853
|
+
} catch {
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
var joinTextblockBackward = () => ({ state, dispatch }) => {
|
|
858
|
+
return originalCommand(state, dispatch);
|
|
859
|
+
};
|
|
860
|
+
var joinTextblockForward = () => ({ state, dispatch }) => {
|
|
861
|
+
return originalCommand2(state, dispatch);
|
|
862
|
+
};
|
|
863
|
+
function isMacOS() {
|
|
864
|
+
return typeof navigator !== "undefined" ? /Mac/.test(navigator.platform) : false;
|
|
865
|
+
}
|
|
866
|
+
function normalizeKeyName(name) {
|
|
867
|
+
const parts = name.split(/-(?!$)/);
|
|
868
|
+
let result = parts[parts.length - 1];
|
|
869
|
+
if (result === "Space") {
|
|
870
|
+
result = " ";
|
|
871
|
+
}
|
|
872
|
+
let alt;
|
|
873
|
+
let ctrl;
|
|
874
|
+
let shift;
|
|
875
|
+
let meta;
|
|
876
|
+
for (let i = 0; i < parts.length - 1; i += 1) {
|
|
877
|
+
const mod = parts[i];
|
|
878
|
+
if (/^(cmd|meta|m)$/i.test(mod)) {
|
|
879
|
+
meta = true;
|
|
880
|
+
} else if (/^a(lt)?$/i.test(mod)) {
|
|
881
|
+
alt = true;
|
|
882
|
+
} else if (/^(c|ctrl|control)$/i.test(mod)) {
|
|
883
|
+
ctrl = true;
|
|
884
|
+
} else if (/^s(hift)?$/i.test(mod)) {
|
|
885
|
+
shift = true;
|
|
886
|
+
} else if (/^mod$/i.test(mod)) {
|
|
887
|
+
if (isiOS() || isMacOS()) {
|
|
888
|
+
meta = true;
|
|
889
|
+
} else {
|
|
890
|
+
ctrl = true;
|
|
891
|
+
}
|
|
892
|
+
} else {
|
|
893
|
+
throw new Error(`Unrecognized modifier name: ${mod}`);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (alt) {
|
|
897
|
+
result = `Alt-${result}`;
|
|
898
|
+
}
|
|
899
|
+
if (ctrl) {
|
|
900
|
+
result = `Ctrl-${result}`;
|
|
901
|
+
}
|
|
902
|
+
if (meta) {
|
|
903
|
+
result = `Meta-${result}`;
|
|
904
|
+
}
|
|
905
|
+
if (shift) {
|
|
906
|
+
result = `Shift-${result}`;
|
|
907
|
+
}
|
|
908
|
+
return result;
|
|
909
|
+
}
|
|
910
|
+
var keyboardShortcut = (name) => ({ editor, view, tr, dispatch }) => {
|
|
911
|
+
const keys = normalizeKeyName(name).split(/-(?!$)/);
|
|
912
|
+
const key = keys.find((item) => !["Alt", "Ctrl", "Meta", "Shift"].includes(item));
|
|
913
|
+
const event = new KeyboardEvent("keydown", {
|
|
914
|
+
key: key === "Space" ? " " : key,
|
|
915
|
+
altKey: keys.includes("Alt"),
|
|
916
|
+
ctrlKey: keys.includes("Ctrl"),
|
|
917
|
+
metaKey: keys.includes("Meta"),
|
|
918
|
+
shiftKey: keys.includes("Shift"),
|
|
919
|
+
bubbles: true,
|
|
920
|
+
cancelable: true
|
|
921
|
+
});
|
|
922
|
+
const capturedTransaction = editor.captureTransaction(() => {
|
|
923
|
+
view.someProp("handleKeyDown", (f) => f(view, event));
|
|
924
|
+
});
|
|
925
|
+
capturedTransaction == null ? void 0 : capturedTransaction.steps.forEach((step) => {
|
|
926
|
+
const newStep = step.map(tr.mapping);
|
|
927
|
+
if (newStep && dispatch) {
|
|
928
|
+
tr.maybeStep(newStep);
|
|
929
|
+
}
|
|
930
|
+
});
|
|
931
|
+
return true;
|
|
932
|
+
};
|
|
933
|
+
function isNodeActive(state, typeOrName, attributes = {}) {
|
|
934
|
+
const { from, to, empty } = state.selection;
|
|
935
|
+
const type = typeOrName ? getNodeType(typeOrName, state.schema) : null;
|
|
936
|
+
const nodeRanges = [];
|
|
937
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
938
|
+
if (node.isText) {
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
const relativeFrom = Math.max(from, pos);
|
|
942
|
+
const relativeTo = Math.min(to, pos + node.nodeSize);
|
|
943
|
+
nodeRanges.push({
|
|
944
|
+
node,
|
|
945
|
+
from: relativeFrom,
|
|
946
|
+
to: relativeTo
|
|
947
|
+
});
|
|
948
|
+
});
|
|
949
|
+
const selectionRange = to - from;
|
|
950
|
+
const matchedNodeRanges = nodeRanges.filter((nodeRange) => {
|
|
951
|
+
if (!type) {
|
|
952
|
+
return true;
|
|
953
|
+
}
|
|
954
|
+
return type.name === nodeRange.node.type.name;
|
|
955
|
+
}).filter((nodeRange) => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }));
|
|
956
|
+
if (empty) {
|
|
957
|
+
return !!matchedNodeRanges.length;
|
|
958
|
+
}
|
|
959
|
+
const range = matchedNodeRanges.reduce((sum, nodeRange) => sum + nodeRange.to - nodeRange.from, 0);
|
|
960
|
+
return range >= selectionRange;
|
|
961
|
+
}
|
|
962
|
+
var lift = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
963
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
964
|
+
const isActive2 = isNodeActive(state, type, attributes);
|
|
965
|
+
if (!isActive2) {
|
|
966
|
+
return false;
|
|
967
|
+
}
|
|
968
|
+
return originalLift(state, dispatch);
|
|
969
|
+
};
|
|
970
|
+
var liftEmptyBlock = () => ({ state, dispatch }) => {
|
|
971
|
+
return originalLiftEmptyBlock(state, dispatch);
|
|
972
|
+
};
|
|
973
|
+
var liftListItem = (typeOrName) => ({ state, dispatch }) => {
|
|
974
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
975
|
+
return originalLiftListItem(type)(state, dispatch);
|
|
976
|
+
};
|
|
977
|
+
var newlineInCode = () => ({ state, dispatch }) => {
|
|
978
|
+
return originalNewlineInCode(state, dispatch);
|
|
979
|
+
};
|
|
980
|
+
function getSchemaTypeNameByName(name, schema) {
|
|
981
|
+
if (schema.nodes[name]) {
|
|
982
|
+
return "node";
|
|
983
|
+
}
|
|
984
|
+
if (schema.marks[name]) {
|
|
985
|
+
return "mark";
|
|
986
|
+
}
|
|
987
|
+
return null;
|
|
988
|
+
}
|
|
989
|
+
function deleteProps(obj, propOrProps) {
|
|
990
|
+
const props = typeof propOrProps === "string" ? [propOrProps] : propOrProps;
|
|
991
|
+
return Object.keys(obj).reduce((newObj, prop) => {
|
|
992
|
+
if (!props.includes(prop)) {
|
|
993
|
+
newObj[prop] = obj[prop];
|
|
994
|
+
}
|
|
995
|
+
return newObj;
|
|
996
|
+
}, {});
|
|
997
|
+
}
|
|
998
|
+
var resetAttributes = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
|
|
999
|
+
let nodeType = null;
|
|
1000
|
+
let markType = null;
|
|
1001
|
+
const schemaType = getSchemaTypeNameByName(
|
|
1002
|
+
typeof typeOrName === "string" ? typeOrName : typeOrName.name,
|
|
1003
|
+
state.schema
|
|
1004
|
+
);
|
|
1005
|
+
if (!schemaType) {
|
|
1006
|
+
return false;
|
|
1007
|
+
}
|
|
1008
|
+
if (schemaType === "node") {
|
|
1009
|
+
nodeType = getNodeType(typeOrName, state.schema);
|
|
1010
|
+
}
|
|
1011
|
+
if (schemaType === "mark") {
|
|
1012
|
+
markType = getMarkType(typeOrName, state.schema);
|
|
1013
|
+
}
|
|
1014
|
+
let canReset = false;
|
|
1015
|
+
tr.selection.ranges.forEach((range) => {
|
|
1016
|
+
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
|
|
1017
|
+
if (nodeType && nodeType === node.type) {
|
|
1018
|
+
canReset = true;
|
|
1019
|
+
if (dispatch) {
|
|
1020
|
+
tr.setNodeMarkup(pos, void 0, deleteProps(node.attrs, attributes));
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
if (markType && node.marks.length) {
|
|
1024
|
+
node.marks.forEach((mark) => {
|
|
1025
|
+
if (markType === mark.type) {
|
|
1026
|
+
canReset = true;
|
|
1027
|
+
if (dispatch) {
|
|
1028
|
+
tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)));
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
});
|
|
1035
|
+
return canReset;
|
|
1036
|
+
};
|
|
1037
|
+
var scrollIntoView = () => ({ tr, dispatch }) => {
|
|
1038
|
+
if (dispatch) {
|
|
1039
|
+
tr.scrollIntoView();
|
|
1040
|
+
}
|
|
1041
|
+
return true;
|
|
1042
|
+
};
|
|
1043
|
+
var selectAll = () => ({ tr, dispatch }) => {
|
|
1044
|
+
if (dispatch) {
|
|
1045
|
+
const selection = new AllSelection(tr.doc);
|
|
1046
|
+
tr.setSelection(selection);
|
|
1047
|
+
}
|
|
1048
|
+
return true;
|
|
1049
|
+
};
|
|
1050
|
+
var selectNodeBackward = () => ({ state, dispatch }) => {
|
|
1051
|
+
return originalSelectNodeBackward(state, dispatch);
|
|
1052
|
+
};
|
|
1053
|
+
var selectNodeForward = () => ({ state, dispatch }) => {
|
|
1054
|
+
return originalSelectNodeForward(state, dispatch);
|
|
1055
|
+
};
|
|
1056
|
+
var selectParentNode = () => ({ state, dispatch }) => {
|
|
1057
|
+
return originalSelectParentNode(state, dispatch);
|
|
1058
|
+
};
|
|
1059
|
+
var selectTextblockEnd = () => ({ state, dispatch }) => {
|
|
1060
|
+
return originalSelectTextblockEnd(state, dispatch);
|
|
1061
|
+
};
|
|
1062
|
+
var selectTextblockStart = () => ({ state, dispatch }) => {
|
|
1063
|
+
return originalSelectTextblockStart(state, dispatch);
|
|
1064
|
+
};
|
|
1065
|
+
function createDocument(content, schema, parseOptions = {}, options = {}) {
|
|
1066
|
+
return createNodeFromContent(content, schema, {
|
|
1067
|
+
slice: false,
|
|
1068
|
+
parseOptions,
|
|
1069
|
+
errorOnInvalidContent: options.errorOnInvalidContent
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
var setContent = (content, { errorOnInvalidContent, emitUpdate = true, parseOptions = {} } = {}) => ({ editor, tr, dispatch, commands }) => {
|
|
1073
|
+
const { doc } = tr;
|
|
1074
|
+
if (parseOptions.preserveWhitespace !== "full") {
|
|
1075
|
+
const document2 = createDocument(content, editor.schema, parseOptions, {
|
|
1076
|
+
errorOnInvalidContent: errorOnInvalidContent != null ? errorOnInvalidContent : editor.options.enableContentCheck
|
|
1077
|
+
});
|
|
1078
|
+
if (dispatch) {
|
|
1079
|
+
tr.replaceWith(0, doc.content.size, document2).setMeta("preventUpdate", !emitUpdate);
|
|
1080
|
+
}
|
|
1081
|
+
return true;
|
|
1082
|
+
}
|
|
1083
|
+
if (dispatch) {
|
|
1084
|
+
tr.setMeta("preventUpdate", !emitUpdate);
|
|
1085
|
+
}
|
|
1086
|
+
return commands.insertContentAt({ from: 0, to: doc.content.size }, content, {
|
|
1087
|
+
parseOptions,
|
|
1088
|
+
errorOnInvalidContent: errorOnInvalidContent != null ? errorOnInvalidContent : editor.options.enableContentCheck
|
|
1089
|
+
});
|
|
1090
|
+
};
|
|
1091
|
+
function getMarkAttributes(state, typeOrName) {
|
|
1092
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
1093
|
+
const { from, to, empty } = state.selection;
|
|
1094
|
+
const marks = [];
|
|
1095
|
+
if (empty) {
|
|
1096
|
+
if (state.storedMarks) {
|
|
1097
|
+
marks.push(...state.storedMarks);
|
|
1098
|
+
}
|
|
1099
|
+
marks.push(...state.selection.$head.marks());
|
|
1100
|
+
} else {
|
|
1101
|
+
state.doc.nodesBetween(from, to, (node) => {
|
|
1102
|
+
marks.push(...node.marks);
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
const mark = marks.find((markItem) => markItem.type.name === type.name);
|
|
1106
|
+
if (!mark) {
|
|
1107
|
+
return {};
|
|
1108
|
+
}
|
|
1109
|
+
return { ...mark.attrs };
|
|
1110
|
+
}
|
|
1111
|
+
function combineTransactionSteps(oldDoc, transactions) {
|
|
1112
|
+
const transform = new Transform(oldDoc);
|
|
1113
|
+
transactions.forEach((transaction) => {
|
|
1114
|
+
transaction.steps.forEach((step) => {
|
|
1115
|
+
transform.step(step);
|
|
1116
|
+
});
|
|
1117
|
+
});
|
|
1118
|
+
return transform;
|
|
1119
|
+
}
|
|
1120
|
+
function defaultBlockAt(match) {
|
|
1121
|
+
for (let i = 0; i < match.edgeCount; i += 1) {
|
|
1122
|
+
const { type } = match.edge(i);
|
|
1123
|
+
if (type.isTextblock && !type.hasRequiredAttrs()) {
|
|
1124
|
+
return type;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
return null;
|
|
1128
|
+
}
|
|
1129
|
+
function findParentNodeClosestToPos($pos, predicate) {
|
|
1130
|
+
for (let i = $pos.depth; i > 0; i -= 1) {
|
|
1131
|
+
const node = $pos.node(i);
|
|
1132
|
+
if (predicate(node)) {
|
|
1133
|
+
return {
|
|
1134
|
+
pos: i > 0 ? $pos.before(i) : 0,
|
|
1135
|
+
start: $pos.start(i),
|
|
1136
|
+
depth: i,
|
|
1137
|
+
node
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
function findParentNode(predicate) {
|
|
1143
|
+
return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
|
|
1144
|
+
}
|
|
1145
|
+
function getExtensionField(extension, field, context) {
|
|
1146
|
+
if (extension.config[field] === void 0 && extension.parent) {
|
|
1147
|
+
return getExtensionField(extension.parent, field, context);
|
|
1148
|
+
}
|
|
1149
|
+
if (typeof extension.config[field] === "function") {
|
|
1150
|
+
const value = extension.config[field].bind({
|
|
1151
|
+
...context,
|
|
1152
|
+
parent: extension.parent ? getExtensionField(extension.parent, field, context) : null
|
|
1153
|
+
});
|
|
1154
|
+
return value;
|
|
1155
|
+
}
|
|
1156
|
+
return extension.config[field];
|
|
1157
|
+
}
|
|
1158
|
+
function flattenExtensions(extensions) {
|
|
1159
|
+
return extensions.map((extension) => {
|
|
1160
|
+
const context = {
|
|
1161
|
+
name: extension.name,
|
|
1162
|
+
options: extension.options,
|
|
1163
|
+
storage: extension.storage
|
|
1164
|
+
};
|
|
1165
|
+
const addExtensions = getExtensionField(extension, "addExtensions", context);
|
|
1166
|
+
if (addExtensions) {
|
|
1167
|
+
return [extension, ...flattenExtensions(addExtensions())];
|
|
1168
|
+
}
|
|
1169
|
+
return extension;
|
|
1170
|
+
}).flat(10);
|
|
1171
|
+
}
|
|
1172
|
+
function getHTMLFromFragment(fragment, schema) {
|
|
1173
|
+
const documentFragment = DOMSerializer.fromSchema(schema).serializeFragment(fragment);
|
|
1174
|
+
const temporaryDocument = document.implementation.createHTMLDocument();
|
|
1175
|
+
const container = temporaryDocument.createElement("div");
|
|
1176
|
+
container.appendChild(documentFragment);
|
|
1177
|
+
return container.innerHTML;
|
|
1178
|
+
}
|
|
1179
|
+
function isFunction(value) {
|
|
1180
|
+
return typeof value === "function";
|
|
1181
|
+
}
|
|
1182
|
+
function callOrReturn(value, context = void 0, ...props) {
|
|
1183
|
+
if (isFunction(value)) {
|
|
1184
|
+
if (context) {
|
|
1185
|
+
return value.bind(context)(...props);
|
|
1186
|
+
}
|
|
1187
|
+
return value(...props);
|
|
1188
|
+
}
|
|
1189
|
+
return value;
|
|
1190
|
+
}
|
|
1191
|
+
function isEmptyObject(value = {}) {
|
|
1192
|
+
return Object.keys(value).length === 0 && value.constructor === Object;
|
|
1193
|
+
}
|
|
1194
|
+
function splitExtensions(extensions) {
|
|
1195
|
+
const baseExtensions = extensions.filter((extension) => extension.type === "extension");
|
|
1196
|
+
const nodeExtensions = extensions.filter((extension) => extension.type === "node");
|
|
1197
|
+
const markExtensions = extensions.filter((extension) => extension.type === "mark");
|
|
1198
|
+
return {
|
|
1199
|
+
baseExtensions,
|
|
1200
|
+
nodeExtensions,
|
|
1201
|
+
markExtensions
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
function getAttributesFromExtensions(extensions) {
|
|
1205
|
+
const extensionAttributes = [];
|
|
1206
|
+
const { nodeExtensions, markExtensions } = splitExtensions(extensions);
|
|
1207
|
+
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions];
|
|
1208
|
+
const defaultAttribute = {
|
|
1209
|
+
default: null,
|
|
1210
|
+
validate: void 0,
|
|
1211
|
+
rendered: true,
|
|
1212
|
+
renderHTML: null,
|
|
1213
|
+
parseHTML: null,
|
|
1214
|
+
keepOnSplit: true,
|
|
1215
|
+
isRequired: false
|
|
1216
|
+
};
|
|
1217
|
+
extensions.forEach((extension) => {
|
|
1218
|
+
const context = {
|
|
1219
|
+
name: extension.name,
|
|
1220
|
+
options: extension.options,
|
|
1221
|
+
storage: extension.storage,
|
|
1222
|
+
extensions: nodeAndMarkExtensions
|
|
1223
|
+
};
|
|
1224
|
+
const addGlobalAttributes = getExtensionField(
|
|
1225
|
+
extension,
|
|
1226
|
+
"addGlobalAttributes",
|
|
1227
|
+
context
|
|
1228
|
+
);
|
|
1229
|
+
if (!addGlobalAttributes) {
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
const globalAttributes = addGlobalAttributes();
|
|
1233
|
+
globalAttributes.forEach((globalAttribute) => {
|
|
1234
|
+
globalAttribute.types.forEach((type) => {
|
|
1235
|
+
Object.entries(globalAttribute.attributes).forEach(([name, attribute]) => {
|
|
1236
|
+
extensionAttributes.push({
|
|
1237
|
+
type,
|
|
1238
|
+
name,
|
|
1239
|
+
attribute: {
|
|
1240
|
+
...defaultAttribute,
|
|
1241
|
+
...attribute
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
});
|
|
1245
|
+
});
|
|
1246
|
+
});
|
|
1247
|
+
});
|
|
1248
|
+
nodeAndMarkExtensions.forEach((extension) => {
|
|
1249
|
+
const context = {
|
|
1250
|
+
name: extension.name,
|
|
1251
|
+
options: extension.options,
|
|
1252
|
+
storage: extension.storage
|
|
1253
|
+
};
|
|
1254
|
+
const addAttributes = getExtensionField(
|
|
1255
|
+
extension,
|
|
1256
|
+
"addAttributes",
|
|
1257
|
+
context
|
|
1258
|
+
);
|
|
1259
|
+
if (!addAttributes) {
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const attributes = addAttributes();
|
|
1263
|
+
Object.entries(attributes).forEach(([name, attribute]) => {
|
|
1264
|
+
const mergedAttr = {
|
|
1265
|
+
...defaultAttribute,
|
|
1266
|
+
...attribute
|
|
1267
|
+
};
|
|
1268
|
+
if (typeof (mergedAttr == null ? void 0 : mergedAttr.default) === "function") {
|
|
1269
|
+
mergedAttr.default = mergedAttr.default();
|
|
1270
|
+
}
|
|
1271
|
+
if ((mergedAttr == null ? void 0 : mergedAttr.isRequired) && (mergedAttr == null ? void 0 : mergedAttr.default) === void 0) {
|
|
1272
|
+
delete mergedAttr.default;
|
|
1273
|
+
}
|
|
1274
|
+
extensionAttributes.push({
|
|
1275
|
+
type: extension.name,
|
|
1276
|
+
name,
|
|
1277
|
+
attribute: mergedAttr
|
|
1278
|
+
});
|
|
1279
|
+
});
|
|
1280
|
+
});
|
|
1281
|
+
return extensionAttributes;
|
|
1282
|
+
}
|
|
1283
|
+
function mergeAttributes(...objects) {
|
|
1284
|
+
return objects.filter((item) => !!item).reduce((items, item) => {
|
|
1285
|
+
const mergedAttributes = { ...items };
|
|
1286
|
+
Object.entries(item).forEach(([key, value]) => {
|
|
1287
|
+
const exists = mergedAttributes[key];
|
|
1288
|
+
if (!exists) {
|
|
1289
|
+
mergedAttributes[key] = value;
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
if (key === "class") {
|
|
1293
|
+
const valueClasses = value ? String(value).split(" ") : [];
|
|
1294
|
+
const existingClasses = mergedAttributes[key] ? mergedAttributes[key].split(" ") : [];
|
|
1295
|
+
const insertClasses = valueClasses.filter((valueClass) => !existingClasses.includes(valueClass));
|
|
1296
|
+
mergedAttributes[key] = [...existingClasses, ...insertClasses].join(" ");
|
|
1297
|
+
} else if (key === "style") {
|
|
1298
|
+
const newStyles = value ? value.split(";").map((style2) => style2.trim()).filter(Boolean) : [];
|
|
1299
|
+
const existingStyles = mergedAttributes[key] ? mergedAttributes[key].split(";").map((style2) => style2.trim()).filter(Boolean) : [];
|
|
1300
|
+
const styleMap = /* @__PURE__ */ new Map();
|
|
1301
|
+
existingStyles.forEach((style2) => {
|
|
1302
|
+
const [property, val] = style2.split(":").map((part) => part.trim());
|
|
1303
|
+
styleMap.set(property, val);
|
|
1304
|
+
});
|
|
1305
|
+
newStyles.forEach((style2) => {
|
|
1306
|
+
const [property, val] = style2.split(":").map((part) => part.trim());
|
|
1307
|
+
styleMap.set(property, val);
|
|
1308
|
+
});
|
|
1309
|
+
mergedAttributes[key] = Array.from(styleMap.entries()).map(([property, val]) => `${property}: ${val}`).join("; ");
|
|
1310
|
+
} else {
|
|
1311
|
+
mergedAttributes[key] = value;
|
|
1312
|
+
}
|
|
1313
|
+
});
|
|
1314
|
+
return mergedAttributes;
|
|
1315
|
+
}, {});
|
|
1316
|
+
}
|
|
1317
|
+
function getRenderedAttributes(nodeOrMark, extensionAttributes) {
|
|
1318
|
+
return extensionAttributes.filter((attribute) => attribute.type === nodeOrMark.type.name).filter((item) => item.attribute.rendered).map((item) => {
|
|
1319
|
+
if (!item.attribute.renderHTML) {
|
|
1320
|
+
return {
|
|
1321
|
+
[item.name]: nodeOrMark.attrs[item.name]
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
return item.attribute.renderHTML(nodeOrMark.attrs) || {};
|
|
1325
|
+
}).reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {});
|
|
1326
|
+
}
|
|
1327
|
+
function fromString(value) {
|
|
1328
|
+
if (typeof value !== "string") {
|
|
1329
|
+
return value;
|
|
1330
|
+
}
|
|
1331
|
+
if (value.match(/^[+-]?(?:\d*\.)?\d+$/)) {
|
|
1332
|
+
return Number(value);
|
|
1333
|
+
}
|
|
1334
|
+
if (value === "true") {
|
|
1335
|
+
return true;
|
|
1336
|
+
}
|
|
1337
|
+
if (value === "false") {
|
|
1338
|
+
return false;
|
|
1339
|
+
}
|
|
1340
|
+
return value;
|
|
1341
|
+
}
|
|
1342
|
+
function injectExtensionAttributesToParseRule(parseRule, extensionAttributes) {
|
|
1343
|
+
if ("style" in parseRule) {
|
|
1344
|
+
return parseRule;
|
|
1345
|
+
}
|
|
1346
|
+
return {
|
|
1347
|
+
...parseRule,
|
|
1348
|
+
getAttrs: (node) => {
|
|
1349
|
+
const oldAttributes = parseRule.getAttrs ? parseRule.getAttrs(node) : parseRule.attrs;
|
|
1350
|
+
if (oldAttributes === false) {
|
|
1351
|
+
return false;
|
|
1352
|
+
}
|
|
1353
|
+
const newAttributes = extensionAttributes.reduce((items, item) => {
|
|
1354
|
+
const value = item.attribute.parseHTML ? item.attribute.parseHTML(node) : fromString(node.getAttribute(item.name));
|
|
1355
|
+
if (value === null || value === void 0) {
|
|
1356
|
+
return items;
|
|
1357
|
+
}
|
|
1358
|
+
return {
|
|
1359
|
+
...items,
|
|
1360
|
+
[item.name]: value
|
|
1361
|
+
};
|
|
1362
|
+
}, {});
|
|
1363
|
+
return { ...oldAttributes, ...newAttributes };
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
1366
|
+
}
|
|
1367
|
+
function cleanUpSchemaItem(data) {
|
|
1368
|
+
return Object.fromEntries(
|
|
1369
|
+
// @ts-ignore
|
|
1370
|
+
Object.entries(data).filter(([key, value]) => {
|
|
1371
|
+
if (key === "attrs" && isEmptyObject(value)) {
|
|
1372
|
+
return false;
|
|
1373
|
+
}
|
|
1374
|
+
return value !== null && value !== void 0;
|
|
1375
|
+
})
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
function buildAttributeSpec(extensionAttribute) {
|
|
1379
|
+
var _a, _b;
|
|
1380
|
+
const spec = {};
|
|
1381
|
+
if (!((_a = extensionAttribute == null ? void 0 : extensionAttribute.attribute) == null ? void 0 : _a.isRequired) && "default" in ((extensionAttribute == null ? void 0 : extensionAttribute.attribute) || {})) {
|
|
1382
|
+
spec.default = extensionAttribute.attribute.default;
|
|
1383
|
+
}
|
|
1384
|
+
if (((_b = extensionAttribute == null ? void 0 : extensionAttribute.attribute) == null ? void 0 : _b.validate) !== void 0) {
|
|
1385
|
+
spec.validate = extensionAttribute.attribute.validate;
|
|
1386
|
+
}
|
|
1387
|
+
return [extensionAttribute.name, spec];
|
|
1388
|
+
}
|
|
1389
|
+
function getSchemaByResolvedExtensions(extensions, editor) {
|
|
1390
|
+
var _a;
|
|
1391
|
+
const allAttributes = getAttributesFromExtensions(extensions);
|
|
1392
|
+
const { nodeExtensions, markExtensions } = splitExtensions(extensions);
|
|
1393
|
+
const topNode = (_a = nodeExtensions.find((extension) => getExtensionField(extension, "topNode"))) == null ? void 0 : _a.name;
|
|
1394
|
+
const nodes = Object.fromEntries(
|
|
1395
|
+
nodeExtensions.map((extension) => {
|
|
1396
|
+
const extensionAttributes = allAttributes.filter((attribute) => attribute.type === extension.name);
|
|
1397
|
+
const context = {
|
|
1398
|
+
name: extension.name,
|
|
1399
|
+
options: extension.options,
|
|
1400
|
+
storage: extension.storage,
|
|
1401
|
+
editor
|
|
1402
|
+
};
|
|
1403
|
+
const extraNodeFields = extensions.reduce((fields, e) => {
|
|
1404
|
+
const extendNodeSchema = getExtensionField(e, "extendNodeSchema", context);
|
|
1405
|
+
return {
|
|
1406
|
+
...fields,
|
|
1407
|
+
...extendNodeSchema ? extendNodeSchema(extension) : {}
|
|
1408
|
+
};
|
|
1409
|
+
}, {});
|
|
1410
|
+
const schema = cleanUpSchemaItem({
|
|
1411
|
+
...extraNodeFields,
|
|
1412
|
+
content: callOrReturn(getExtensionField(extension, "content", context)),
|
|
1413
|
+
marks: callOrReturn(getExtensionField(extension, "marks", context)),
|
|
1414
|
+
group: callOrReturn(getExtensionField(extension, "group", context)),
|
|
1415
|
+
inline: callOrReturn(getExtensionField(extension, "inline", context)),
|
|
1416
|
+
atom: callOrReturn(getExtensionField(extension, "atom", context)),
|
|
1417
|
+
selectable: callOrReturn(getExtensionField(extension, "selectable", context)),
|
|
1418
|
+
draggable: callOrReturn(getExtensionField(extension, "draggable", context)),
|
|
1419
|
+
code: callOrReturn(getExtensionField(extension, "code", context)),
|
|
1420
|
+
whitespace: callOrReturn(getExtensionField(extension, "whitespace", context)),
|
|
1421
|
+
linebreakReplacement: callOrReturn(
|
|
1422
|
+
getExtensionField(extension, "linebreakReplacement", context)
|
|
1423
|
+
),
|
|
1424
|
+
defining: callOrReturn(getExtensionField(extension, "defining", context)),
|
|
1425
|
+
isolating: callOrReturn(getExtensionField(extension, "isolating", context)),
|
|
1426
|
+
attrs: Object.fromEntries(extensionAttributes.map(buildAttributeSpec))
|
|
1427
|
+
});
|
|
1428
|
+
const parseHTML = callOrReturn(getExtensionField(extension, "parseHTML", context));
|
|
1429
|
+
if (parseHTML) {
|
|
1430
|
+
schema.parseDOM = parseHTML.map(
|
|
1431
|
+
(parseRule) => injectExtensionAttributesToParseRule(parseRule, extensionAttributes)
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
const renderHTML = getExtensionField(extension, "renderHTML", context);
|
|
1435
|
+
if (renderHTML) {
|
|
1436
|
+
schema.toDOM = (node) => renderHTML({
|
|
1437
|
+
node,
|
|
1438
|
+
HTMLAttributes: getRenderedAttributes(node, extensionAttributes)
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
const renderText = getExtensionField(extension, "renderText", context);
|
|
1442
|
+
if (renderText) {
|
|
1443
|
+
schema.toText = renderText;
|
|
1444
|
+
}
|
|
1445
|
+
return [extension.name, schema];
|
|
1446
|
+
})
|
|
1447
|
+
);
|
|
1448
|
+
const marks = Object.fromEntries(
|
|
1449
|
+
markExtensions.map((extension) => {
|
|
1450
|
+
const extensionAttributes = allAttributes.filter((attribute) => attribute.type === extension.name);
|
|
1451
|
+
const context = {
|
|
1452
|
+
name: extension.name,
|
|
1453
|
+
options: extension.options,
|
|
1454
|
+
storage: extension.storage,
|
|
1455
|
+
editor
|
|
1456
|
+
};
|
|
1457
|
+
const extraMarkFields = extensions.reduce((fields, e) => {
|
|
1458
|
+
const extendMarkSchema = getExtensionField(e, "extendMarkSchema", context);
|
|
1459
|
+
return {
|
|
1460
|
+
...fields,
|
|
1461
|
+
...extendMarkSchema ? extendMarkSchema(extension) : {}
|
|
1462
|
+
};
|
|
1463
|
+
}, {});
|
|
1464
|
+
const schema = cleanUpSchemaItem({
|
|
1465
|
+
...extraMarkFields,
|
|
1466
|
+
inclusive: callOrReturn(getExtensionField(extension, "inclusive", context)),
|
|
1467
|
+
excludes: callOrReturn(getExtensionField(extension, "excludes", context)),
|
|
1468
|
+
group: callOrReturn(getExtensionField(extension, "group", context)),
|
|
1469
|
+
spanning: callOrReturn(getExtensionField(extension, "spanning", context)),
|
|
1470
|
+
code: callOrReturn(getExtensionField(extension, "code", context)),
|
|
1471
|
+
attrs: Object.fromEntries(extensionAttributes.map(buildAttributeSpec))
|
|
1472
|
+
});
|
|
1473
|
+
const parseHTML = callOrReturn(getExtensionField(extension, "parseHTML", context));
|
|
1474
|
+
if (parseHTML) {
|
|
1475
|
+
schema.parseDOM = parseHTML.map(
|
|
1476
|
+
(parseRule) => injectExtensionAttributesToParseRule(parseRule, extensionAttributes)
|
|
1477
|
+
);
|
|
1478
|
+
}
|
|
1479
|
+
const renderHTML = getExtensionField(extension, "renderHTML", context);
|
|
1480
|
+
if (renderHTML) {
|
|
1481
|
+
schema.toDOM = (mark) => renderHTML({
|
|
1482
|
+
mark,
|
|
1483
|
+
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes)
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
return [extension.name, schema];
|
|
1487
|
+
})
|
|
1488
|
+
);
|
|
1489
|
+
return new Schema2({
|
|
1490
|
+
topNode,
|
|
1491
|
+
nodes,
|
|
1492
|
+
marks
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
function findDuplicates(items) {
|
|
1496
|
+
const filtered = items.filter((el, index) => items.indexOf(el) !== index);
|
|
1497
|
+
return Array.from(new Set(filtered));
|
|
1498
|
+
}
|
|
1499
|
+
function sortExtensions(extensions) {
|
|
1500
|
+
const defaultPriority = 100;
|
|
1501
|
+
return extensions.sort((a, b) => {
|
|
1502
|
+
const priorityA = getExtensionField(a, "priority") || defaultPriority;
|
|
1503
|
+
const priorityB = getExtensionField(b, "priority") || defaultPriority;
|
|
1504
|
+
if (priorityA > priorityB) {
|
|
1505
|
+
return -1;
|
|
1506
|
+
}
|
|
1507
|
+
if (priorityA < priorityB) {
|
|
1508
|
+
return 1;
|
|
1509
|
+
}
|
|
1510
|
+
return 0;
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
function resolveExtensions(extensions) {
|
|
1514
|
+
const resolvedExtensions = sortExtensions(flattenExtensions(extensions));
|
|
1515
|
+
const duplicatedNames = findDuplicates(resolvedExtensions.map((extension) => extension.name));
|
|
1516
|
+
if (duplicatedNames.length) {
|
|
1517
|
+
console.warn(
|
|
1518
|
+
`[tiptap warn]: Duplicate extension names found: [${duplicatedNames.map((item) => `'${item}'`).join(", ")}]. This can lead to issues.`
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
return resolvedExtensions;
|
|
1522
|
+
}
|
|
1523
|
+
function getTextBetween(startNode, range, options) {
|
|
1524
|
+
const { from, to } = range;
|
|
1525
|
+
const { blockSeparator = "\n\n", textSerializers = {} } = options || {};
|
|
1526
|
+
let text = "";
|
|
1527
|
+
startNode.nodesBetween(from, to, (node, pos, parent, index) => {
|
|
1528
|
+
var _a;
|
|
1529
|
+
if (node.isBlock && pos > from) {
|
|
1530
|
+
text += blockSeparator;
|
|
1531
|
+
}
|
|
1532
|
+
const textSerializer = textSerializers == null ? void 0 : textSerializers[node.type.name];
|
|
1533
|
+
if (textSerializer) {
|
|
1534
|
+
if (parent) {
|
|
1535
|
+
text += textSerializer({
|
|
1536
|
+
node,
|
|
1537
|
+
pos,
|
|
1538
|
+
parent,
|
|
1539
|
+
index,
|
|
1540
|
+
range
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
return false;
|
|
1544
|
+
}
|
|
1545
|
+
if (node.isText) {
|
|
1546
|
+
text += (_a = node == null ? void 0 : node.text) == null ? void 0 : _a.slice(Math.max(from, pos) - pos, to - pos);
|
|
1547
|
+
}
|
|
1548
|
+
});
|
|
1549
|
+
return text;
|
|
1550
|
+
}
|
|
1551
|
+
function getTextSerializersFromSchema(schema) {
|
|
1552
|
+
return Object.fromEntries(
|
|
1553
|
+
Object.entries(schema.nodes).filter(([, node]) => node.spec.toText).map(([name, node]) => [name, node.spec.toText])
|
|
1554
|
+
);
|
|
1555
|
+
}
|
|
1556
|
+
function removeDuplicates(array, by = JSON.stringify) {
|
|
1557
|
+
const seen = {};
|
|
1558
|
+
return array.filter((item) => {
|
|
1559
|
+
const key = by(item);
|
|
1560
|
+
return Object.prototype.hasOwnProperty.call(seen, key) ? false : seen[key] = true;
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
function simplifyChangedRanges(changes) {
|
|
1564
|
+
const uniqueChanges = removeDuplicates(changes);
|
|
1565
|
+
return uniqueChanges.length === 1 ? uniqueChanges : uniqueChanges.filter((change, index) => {
|
|
1566
|
+
const rest = uniqueChanges.filter((_, i) => i !== index);
|
|
1567
|
+
return !rest.some((otherChange) => {
|
|
1568
|
+
return change.oldRange.from >= otherChange.oldRange.from && change.oldRange.to <= otherChange.oldRange.to && change.newRange.from >= otherChange.newRange.from && change.newRange.to <= otherChange.newRange.to;
|
|
1569
|
+
});
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
function getChangedRanges(transform) {
|
|
1573
|
+
const { mapping, steps } = transform;
|
|
1574
|
+
const changes = [];
|
|
1575
|
+
mapping.maps.forEach((stepMap, index) => {
|
|
1576
|
+
const ranges = [];
|
|
1577
|
+
if (!stepMap.ranges.length) {
|
|
1578
|
+
const { from, to } = steps[index];
|
|
1579
|
+
if (from === void 0 || to === void 0) {
|
|
1580
|
+
return;
|
|
1581
|
+
}
|
|
1582
|
+
ranges.push({ from, to });
|
|
1583
|
+
} else {
|
|
1584
|
+
stepMap.forEach((from, to) => {
|
|
1585
|
+
ranges.push({ from, to });
|
|
1586
|
+
});
|
|
1587
|
+
}
|
|
1588
|
+
ranges.forEach(({ from, to }) => {
|
|
1589
|
+
const newStart = mapping.slice(index).map(from, -1);
|
|
1590
|
+
const newEnd = mapping.slice(index).map(to);
|
|
1591
|
+
const oldStart = mapping.invert().map(newStart, -1);
|
|
1592
|
+
const oldEnd = mapping.invert().map(newEnd);
|
|
1593
|
+
changes.push({
|
|
1594
|
+
oldRange: {
|
|
1595
|
+
from: oldStart,
|
|
1596
|
+
to: oldEnd
|
|
1597
|
+
},
|
|
1598
|
+
newRange: {
|
|
1599
|
+
from: newStart,
|
|
1600
|
+
to: newEnd
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
});
|
|
1604
|
+
});
|
|
1605
|
+
return simplifyChangedRanges(changes);
|
|
1606
|
+
}
|
|
1607
|
+
function getSchemaTypeByName(name, schema) {
|
|
1608
|
+
return schema.nodes[name] || schema.marks[name] || null;
|
|
1609
|
+
}
|
|
1610
|
+
function getSplittedAttributes(extensionAttributes, typeName, attributes) {
|
|
1611
|
+
return Object.fromEntries(
|
|
1612
|
+
Object.entries(attributes).filter(([name]) => {
|
|
1613
|
+
const extensionAttribute = extensionAttributes.find((item) => {
|
|
1614
|
+
return item.type === typeName && item.name === name;
|
|
1615
|
+
});
|
|
1616
|
+
if (!extensionAttribute) {
|
|
1617
|
+
return false;
|
|
1618
|
+
}
|
|
1619
|
+
return extensionAttribute.attribute.keepOnSplit;
|
|
1620
|
+
})
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
var getTextContentFromNodes = ($from, maxMatch = 500) => {
|
|
1624
|
+
let textBefore = "";
|
|
1625
|
+
const sliceEndPos = $from.parentOffset;
|
|
1626
|
+
$from.parent.nodesBetween(Math.max(0, sliceEndPos - maxMatch), sliceEndPos, (node, pos, parent, index) => {
|
|
1627
|
+
var _a, _b;
|
|
1628
|
+
const chunk = ((_b = (_a = node.type.spec).toText) == null ? void 0 : _b.call(_a, {
|
|
1629
|
+
node,
|
|
1630
|
+
pos,
|
|
1631
|
+
parent,
|
|
1632
|
+
index
|
|
1633
|
+
})) || node.textContent || "%leaf%";
|
|
1634
|
+
textBefore += node.isAtom && !node.isText ? chunk : chunk.slice(0, Math.max(0, sliceEndPos - pos));
|
|
1635
|
+
});
|
|
1636
|
+
return textBefore;
|
|
1637
|
+
};
|
|
1638
|
+
function isMarkActive(state, typeOrName, attributes = {}) {
|
|
1639
|
+
const { empty, ranges } = state.selection;
|
|
1640
|
+
const type = typeOrName ? getMarkType(typeOrName, state.schema) : null;
|
|
1641
|
+
if (empty) {
|
|
1642
|
+
return !!(state.storedMarks || state.selection.$from.marks()).filter((mark) => {
|
|
1643
|
+
if (!type) {
|
|
1644
|
+
return true;
|
|
1645
|
+
}
|
|
1646
|
+
return type.name === mark.type.name;
|
|
1647
|
+
}).find((mark) => objectIncludes(mark.attrs, attributes, { strict: false }));
|
|
1648
|
+
}
|
|
1649
|
+
let selectionRange = 0;
|
|
1650
|
+
const markRanges = [];
|
|
1651
|
+
ranges.forEach(({ $from, $to }) => {
|
|
1652
|
+
const from = $from.pos;
|
|
1653
|
+
const to = $to.pos;
|
|
1654
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
1655
|
+
if (!node.isText && !node.marks.length) {
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1658
|
+
const relativeFrom = Math.max(from, pos);
|
|
1659
|
+
const relativeTo = Math.min(to, pos + node.nodeSize);
|
|
1660
|
+
const range2 = relativeTo - relativeFrom;
|
|
1661
|
+
selectionRange += range2;
|
|
1662
|
+
markRanges.push(
|
|
1663
|
+
...node.marks.map((mark) => ({
|
|
1664
|
+
mark,
|
|
1665
|
+
from: relativeFrom,
|
|
1666
|
+
to: relativeTo
|
|
1667
|
+
}))
|
|
1668
|
+
);
|
|
1669
|
+
});
|
|
1670
|
+
});
|
|
1671
|
+
if (selectionRange === 0) {
|
|
1672
|
+
return false;
|
|
1673
|
+
}
|
|
1674
|
+
const matchedRange = markRanges.filter((markRange) => {
|
|
1675
|
+
if (!type) {
|
|
1676
|
+
return true;
|
|
1677
|
+
}
|
|
1678
|
+
return type.name === markRange.mark.type.name;
|
|
1679
|
+
}).filter((markRange) => objectIncludes(markRange.mark.attrs, attributes, { strict: false })).reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
|
|
1680
|
+
const excludedRange = markRanges.filter((markRange) => {
|
|
1681
|
+
if (!type) {
|
|
1682
|
+
return true;
|
|
1683
|
+
}
|
|
1684
|
+
return markRange.mark.type !== type && markRange.mark.type.excludes(type);
|
|
1685
|
+
}).reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
|
|
1686
|
+
const range = matchedRange > 0 ? matchedRange + excludedRange : matchedRange;
|
|
1687
|
+
return range >= selectionRange;
|
|
1688
|
+
}
|
|
1689
|
+
function isExtensionRulesEnabled(extension, enabled) {
|
|
1690
|
+
if (Array.isArray(enabled)) {
|
|
1691
|
+
return enabled.some((enabledExtension) => {
|
|
1692
|
+
const name = typeof enabledExtension === "string" ? enabledExtension : enabledExtension.name;
|
|
1693
|
+
return name === extension.name;
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
return enabled;
|
|
1697
|
+
}
|
|
1698
|
+
function isList(name, extensions) {
|
|
1699
|
+
const { nodeExtensions } = splitExtensions(extensions);
|
|
1700
|
+
const extension = nodeExtensions.find((item) => item.name === name);
|
|
1701
|
+
if (!extension) {
|
|
1702
|
+
return false;
|
|
1703
|
+
}
|
|
1704
|
+
const context = {
|
|
1705
|
+
name: extension.name,
|
|
1706
|
+
options: extension.options,
|
|
1707
|
+
storage: extension.storage
|
|
1708
|
+
};
|
|
1709
|
+
const group = callOrReturn(getExtensionField(extension, "group", context));
|
|
1710
|
+
if (typeof group !== "string") {
|
|
1711
|
+
return false;
|
|
1712
|
+
}
|
|
1713
|
+
return group.split(" ").includes("list");
|
|
1714
|
+
}
|
|
1715
|
+
function isNodeEmpty(node, {
|
|
1716
|
+
checkChildren = true,
|
|
1717
|
+
ignoreWhitespace = false
|
|
1718
|
+
} = {}) {
|
|
1719
|
+
var _a;
|
|
1720
|
+
if (ignoreWhitespace) {
|
|
1721
|
+
if (node.type.name === "hardBreak") {
|
|
1722
|
+
return true;
|
|
1723
|
+
}
|
|
1724
|
+
if (node.isText) {
|
|
1725
|
+
return /^\s*$/m.test((_a = node.text) != null ? _a : "");
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
if (node.isText) {
|
|
1729
|
+
return !node.text;
|
|
1730
|
+
}
|
|
1731
|
+
if (node.isAtom || node.isLeaf) {
|
|
1732
|
+
return false;
|
|
1733
|
+
}
|
|
1734
|
+
if (node.content.childCount === 0) {
|
|
1735
|
+
return true;
|
|
1736
|
+
}
|
|
1737
|
+
if (checkChildren) {
|
|
1738
|
+
let isContentEmpty = true;
|
|
1739
|
+
node.content.forEach((childNode) => {
|
|
1740
|
+
if (isContentEmpty === false) {
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1743
|
+
if (!isNodeEmpty(childNode, { ignoreWhitespace, checkChildren })) {
|
|
1744
|
+
isContentEmpty = false;
|
|
1745
|
+
}
|
|
1746
|
+
});
|
|
1747
|
+
return isContentEmpty;
|
|
1748
|
+
}
|
|
1749
|
+
return false;
|
|
1750
|
+
}
|
|
1751
|
+
function canSetMark(state, tr, newMarkType) {
|
|
1752
|
+
var _a;
|
|
1753
|
+
const { selection } = tr;
|
|
1754
|
+
let cursor = null;
|
|
1755
|
+
if (isTextSelection(selection)) {
|
|
1756
|
+
cursor = selection.$cursor;
|
|
1757
|
+
}
|
|
1758
|
+
if (cursor) {
|
|
1759
|
+
const currentMarks = (_a = state.storedMarks) != null ? _a : cursor.marks();
|
|
1760
|
+
const parentAllowsMarkType = cursor.parent.type.allowsMarkType(newMarkType);
|
|
1761
|
+
return parentAllowsMarkType && (!!newMarkType.isInSet(currentMarks) || !currentMarks.some((mark) => mark.type.excludes(newMarkType)));
|
|
1762
|
+
}
|
|
1763
|
+
const { ranges } = selection;
|
|
1764
|
+
return ranges.some(({ $from, $to }) => {
|
|
1765
|
+
let someNodeSupportsMark = $from.depth === 0 ? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType) : false;
|
|
1766
|
+
state.doc.nodesBetween($from.pos, $to.pos, (node, _pos, parent) => {
|
|
1767
|
+
if (someNodeSupportsMark) {
|
|
1768
|
+
return false;
|
|
1769
|
+
}
|
|
1770
|
+
if (node.isInline) {
|
|
1771
|
+
const parentAllowsMarkType = !parent || parent.type.allowsMarkType(newMarkType);
|
|
1772
|
+
const currentMarksAllowMarkType = !!newMarkType.isInSet(node.marks) || !node.marks.some((otherMark) => otherMark.type.excludes(newMarkType));
|
|
1773
|
+
someNodeSupportsMark = parentAllowsMarkType && currentMarksAllowMarkType;
|
|
1774
|
+
}
|
|
1775
|
+
return !someNodeSupportsMark;
|
|
1776
|
+
});
|
|
1777
|
+
return someNodeSupportsMark;
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
var setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
|
1781
|
+
const { selection } = tr;
|
|
1782
|
+
const { empty, ranges } = selection;
|
|
1783
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
1784
|
+
if (dispatch) {
|
|
1785
|
+
if (empty) {
|
|
1786
|
+
const oldAttributes = getMarkAttributes(state, type);
|
|
1787
|
+
tr.addStoredMark(
|
|
1788
|
+
type.create({
|
|
1789
|
+
...oldAttributes,
|
|
1790
|
+
...attributes
|
|
1791
|
+
})
|
|
1792
|
+
);
|
|
1793
|
+
} else {
|
|
1794
|
+
ranges.forEach((range) => {
|
|
1795
|
+
const from = range.$from.pos;
|
|
1796
|
+
const to = range.$to.pos;
|
|
1797
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
1798
|
+
const trimmedFrom = Math.max(pos, from);
|
|
1799
|
+
const trimmedTo = Math.min(pos + node.nodeSize, to);
|
|
1800
|
+
const someHasMark = node.marks.find((mark) => mark.type === type);
|
|
1801
|
+
if (someHasMark) {
|
|
1802
|
+
node.marks.forEach((mark) => {
|
|
1803
|
+
if (type === mark.type) {
|
|
1804
|
+
tr.addMark(
|
|
1805
|
+
trimmedFrom,
|
|
1806
|
+
trimmedTo,
|
|
1807
|
+
type.create({
|
|
1808
|
+
...mark.attrs,
|
|
1809
|
+
...attributes
|
|
1810
|
+
})
|
|
1811
|
+
);
|
|
1812
|
+
}
|
|
1813
|
+
});
|
|
1814
|
+
} else {
|
|
1815
|
+
tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
|
|
1816
|
+
}
|
|
1817
|
+
});
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
return canSetMark(state, tr, type);
|
|
1822
|
+
};
|
|
1823
|
+
var setMeta = (key, value) => ({ tr }) => {
|
|
1824
|
+
tr.setMeta(key, value);
|
|
1825
|
+
return true;
|
|
1826
|
+
};
|
|
1827
|
+
var setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
|
|
1828
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1829
|
+
let attributesToCopy;
|
|
1830
|
+
if (state.selection.$anchor.sameParent(state.selection.$head)) {
|
|
1831
|
+
attributesToCopy = state.selection.$anchor.parent.attrs;
|
|
1832
|
+
}
|
|
1833
|
+
if (!type.isTextblock) {
|
|
1834
|
+
console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
|
|
1835
|
+
return false;
|
|
1836
|
+
}
|
|
1837
|
+
return chain().command(({ commands }) => {
|
|
1838
|
+
const canSetBlock = setBlockType(type, { ...attributesToCopy, ...attributes })(state);
|
|
1839
|
+
if (canSetBlock) {
|
|
1840
|
+
return true;
|
|
1841
|
+
}
|
|
1842
|
+
return commands.clearNodes();
|
|
1843
|
+
}).command(({ state: updatedState }) => {
|
|
1844
|
+
return setBlockType(type, { ...attributesToCopy, ...attributes })(updatedState, dispatch);
|
|
1845
|
+
}).run();
|
|
1846
|
+
};
|
|
1847
|
+
var setNodeSelection = (position) => ({ tr, dispatch }) => {
|
|
1848
|
+
if (dispatch) {
|
|
1849
|
+
const { doc } = tr;
|
|
1850
|
+
const from = minMax(position, 0, doc.content.size);
|
|
1851
|
+
const selection = NodeSelection2.create(doc, from);
|
|
1852
|
+
tr.setSelection(selection);
|
|
1853
|
+
}
|
|
1854
|
+
return true;
|
|
1855
|
+
};
|
|
1856
|
+
var setTextDirection = (direction, position) => ({ tr, state, dispatch }) => {
|
|
1857
|
+
const { selection } = state;
|
|
1858
|
+
let from;
|
|
1859
|
+
let to;
|
|
1860
|
+
if (typeof position === "number") {
|
|
1861
|
+
from = position;
|
|
1862
|
+
to = position;
|
|
1863
|
+
} else if (position && "from" in position && "to" in position) {
|
|
1864
|
+
from = position.from;
|
|
1865
|
+
to = position.to;
|
|
1866
|
+
} else {
|
|
1867
|
+
from = selection.from;
|
|
1868
|
+
to = selection.to;
|
|
1869
|
+
}
|
|
1870
|
+
if (dispatch) {
|
|
1871
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
1872
|
+
if (node.isText) {
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
tr.setNodeMarkup(pos, void 0, {
|
|
1876
|
+
...node.attrs,
|
|
1877
|
+
dir: direction
|
|
1878
|
+
});
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
return true;
|
|
1882
|
+
};
|
|
1883
|
+
var setTextSelection = (position) => ({ tr, dispatch }) => {
|
|
1884
|
+
if (dispatch) {
|
|
1885
|
+
const { doc } = tr;
|
|
1886
|
+
const { from, to } = typeof position === "number" ? { from: position, to: position } : position;
|
|
1887
|
+
const minPos = TextSelection5.atStart(doc).from;
|
|
1888
|
+
const maxPos = TextSelection5.atEnd(doc).to;
|
|
1889
|
+
const resolvedFrom = minMax(from, minPos, maxPos);
|
|
1890
|
+
const resolvedEnd = minMax(to, minPos, maxPos);
|
|
1891
|
+
const selection = TextSelection5.create(doc, resolvedFrom, resolvedEnd);
|
|
1892
|
+
tr.setSelection(selection);
|
|
1893
|
+
}
|
|
1894
|
+
return true;
|
|
1895
|
+
};
|
|
1896
|
+
var sinkListItem = (typeOrName) => ({ state, dispatch }) => {
|
|
1897
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1898
|
+
return originalSinkListItem(type)(state, dispatch);
|
|
1899
|
+
};
|
|
1900
|
+
function ensureMarks(state, splittableMarks) {
|
|
1901
|
+
const marks = state.storedMarks || state.selection.$to.parentOffset && state.selection.$from.marks();
|
|
1902
|
+
if (marks) {
|
|
1903
|
+
const filteredMarks = marks.filter((mark) => splittableMarks == null ? void 0 : splittableMarks.includes(mark.type.name));
|
|
1904
|
+
state.tr.ensureMarks(filteredMarks);
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
var splitBlock = ({ keepMarks = true } = {}) => ({ tr, state, dispatch, editor }) => {
|
|
1908
|
+
const { selection, doc } = tr;
|
|
1909
|
+
const { $from, $to } = selection;
|
|
1910
|
+
const extensionAttributes = editor.extensionManager.attributes;
|
|
1911
|
+
const newAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
|
|
1912
|
+
if (selection instanceof NodeSelection3 && selection.node.isBlock) {
|
|
1913
|
+
if (!$from.parentOffset || !canSplit(doc, $from.pos)) {
|
|
1914
|
+
return false;
|
|
1915
|
+
}
|
|
1916
|
+
if (dispatch) {
|
|
1917
|
+
if (keepMarks) {
|
|
1918
|
+
ensureMarks(state, editor.extensionManager.splittableMarks);
|
|
1919
|
+
}
|
|
1920
|
+
tr.split($from.pos).scrollIntoView();
|
|
1921
|
+
}
|
|
1922
|
+
return true;
|
|
1923
|
+
}
|
|
1924
|
+
if (!$from.parent.isBlock) {
|
|
1925
|
+
return false;
|
|
1926
|
+
}
|
|
1927
|
+
const atEnd = $to.parentOffset === $to.parent.content.size;
|
|
1928
|
+
const deflt = $from.depth === 0 ? void 0 : defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)));
|
|
1929
|
+
let types = atEnd && deflt ? [
|
|
1930
|
+
{
|
|
1931
|
+
type: deflt,
|
|
1932
|
+
attrs: newAttributes
|
|
1933
|
+
}
|
|
1934
|
+
] : void 0;
|
|
1935
|
+
let can = canSplit(tr.doc, tr.mapping.map($from.pos), 1, types);
|
|
1936
|
+
if (!types && !can && canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : void 0)) {
|
|
1937
|
+
can = true;
|
|
1938
|
+
types = deflt ? [
|
|
1939
|
+
{
|
|
1940
|
+
type: deflt,
|
|
1941
|
+
attrs: newAttributes
|
|
1942
|
+
}
|
|
1943
|
+
] : void 0;
|
|
1944
|
+
}
|
|
1945
|
+
if (dispatch) {
|
|
1946
|
+
if (can) {
|
|
1947
|
+
if (selection instanceof TextSelection6) {
|
|
1948
|
+
tr.deleteSelection();
|
|
1949
|
+
}
|
|
1950
|
+
tr.split(tr.mapping.map($from.pos), 1, types);
|
|
1951
|
+
if (deflt && !atEnd && !$from.parentOffset && $from.parent.type !== deflt) {
|
|
1952
|
+
const first2 = tr.mapping.map($from.before());
|
|
1953
|
+
const $first = tr.doc.resolve(first2);
|
|
1954
|
+
if ($from.node(-1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
|
|
1955
|
+
tr.setNodeMarkup(tr.mapping.map($from.before()), deflt);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
if (keepMarks) {
|
|
1960
|
+
ensureMarks(state, editor.extensionManager.splittableMarks);
|
|
1961
|
+
}
|
|
1962
|
+
tr.scrollIntoView();
|
|
1963
|
+
}
|
|
1964
|
+
return can;
|
|
1965
|
+
};
|
|
1966
|
+
var splitListItem = (typeOrName, overrideAttrs = {}) => ({ tr, state, dispatch, editor }) => {
|
|
1967
|
+
var _a;
|
|
1968
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
1969
|
+
const { $from, $to } = state.selection;
|
|
1970
|
+
const node = state.selection.node;
|
|
1971
|
+
if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) {
|
|
1972
|
+
return false;
|
|
1973
|
+
}
|
|
1974
|
+
const grandParent = $from.node(-1);
|
|
1975
|
+
if (grandParent.type !== type) {
|
|
1976
|
+
return false;
|
|
1977
|
+
}
|
|
1978
|
+
const extensionAttributes = editor.extensionManager.attributes;
|
|
1979
|
+
if ($from.parent.content.size === 0 && $from.node(-1).childCount === $from.indexAfter(-1)) {
|
|
1980
|
+
if ($from.depth === 2 || $from.node(-3).type !== type || $from.index(-2) !== $from.node(-2).childCount - 1) {
|
|
1981
|
+
return false;
|
|
1982
|
+
}
|
|
1983
|
+
if (dispatch) {
|
|
1984
|
+
let wrap = Fragment3.empty;
|
|
1985
|
+
const depthBefore = $from.index(-1) ? 1 : $from.index(-2) ? 2 : 3;
|
|
1986
|
+
for (let d = $from.depth - depthBefore; d >= $from.depth - 3; d -= 1) {
|
|
1987
|
+
wrap = Fragment3.from($from.node(d).copy(wrap));
|
|
1988
|
+
}
|
|
1989
|
+
const depthAfter = (
|
|
1990
|
+
// eslint-disable-next-line no-nested-ternary
|
|
1991
|
+
$from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3
|
|
1992
|
+
);
|
|
1993
|
+
const newNextTypeAttributes2 = {
|
|
1994
|
+
...getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs),
|
|
1995
|
+
...overrideAttrs
|
|
1996
|
+
};
|
|
1997
|
+
const nextType2 = ((_a = type.contentMatch.defaultType) == null ? void 0 : _a.createAndFill(newNextTypeAttributes2)) || void 0;
|
|
1998
|
+
wrap = wrap.append(Fragment3.from(type.createAndFill(null, nextType2) || void 0));
|
|
1999
|
+
const start = $from.before($from.depth - (depthBefore - 1));
|
|
2000
|
+
tr.replace(start, $from.after(-depthAfter), new Slice(wrap, 4 - depthBefore, 0));
|
|
2001
|
+
let sel = -1;
|
|
2002
|
+
tr.doc.nodesBetween(start, tr.doc.content.size, (n, pos) => {
|
|
2003
|
+
if (sel > -1) {
|
|
2004
|
+
return false;
|
|
2005
|
+
}
|
|
2006
|
+
if (n.isTextblock && n.content.size === 0) {
|
|
2007
|
+
sel = pos + 1;
|
|
2008
|
+
}
|
|
2009
|
+
});
|
|
2010
|
+
if (sel > -1) {
|
|
2011
|
+
tr.setSelection(TextSelection7.near(tr.doc.resolve(sel)));
|
|
2012
|
+
}
|
|
2013
|
+
tr.scrollIntoView();
|
|
2014
|
+
}
|
|
2015
|
+
return true;
|
|
2016
|
+
}
|
|
2017
|
+
const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null;
|
|
2018
|
+
const newTypeAttributes = {
|
|
2019
|
+
...getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs),
|
|
2020
|
+
...overrideAttrs
|
|
2021
|
+
};
|
|
2022
|
+
const newNextTypeAttributes = {
|
|
2023
|
+
...getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs),
|
|
2024
|
+
...overrideAttrs
|
|
2025
|
+
};
|
|
2026
|
+
tr.delete($from.pos, $to.pos);
|
|
2027
|
+
const types = nextType ? [
|
|
2028
|
+
{ type, attrs: newTypeAttributes },
|
|
2029
|
+
{ type: nextType, attrs: newNextTypeAttributes }
|
|
2030
|
+
] : [{ type, attrs: newTypeAttributes }];
|
|
2031
|
+
if (!canSplit2(tr.doc, $from.pos, 2)) {
|
|
2032
|
+
return false;
|
|
2033
|
+
}
|
|
2034
|
+
if (dispatch) {
|
|
2035
|
+
const { selection, storedMarks } = state;
|
|
2036
|
+
const { splittableMarks } = editor.extensionManager;
|
|
2037
|
+
const marks = storedMarks || selection.$to.parentOffset && selection.$from.marks();
|
|
2038
|
+
tr.split($from.pos, 2, types).scrollIntoView();
|
|
2039
|
+
if (!marks || !dispatch) {
|
|
2040
|
+
return true;
|
|
2041
|
+
}
|
|
2042
|
+
const filteredMarks = marks.filter((mark) => splittableMarks.includes(mark.type.name));
|
|
2043
|
+
tr.ensureMarks(filteredMarks);
|
|
2044
|
+
}
|
|
2045
|
+
return true;
|
|
2046
|
+
};
|
|
2047
|
+
var joinListBackwards = (tr, listType) => {
|
|
2048
|
+
const list = findParentNode((node) => node.type === listType)(tr.selection);
|
|
2049
|
+
if (!list) {
|
|
2050
|
+
return true;
|
|
2051
|
+
}
|
|
2052
|
+
const before = tr.doc.resolve(Math.max(0, list.pos - 1)).before(list.depth);
|
|
2053
|
+
if (before === void 0) {
|
|
2054
|
+
return true;
|
|
2055
|
+
}
|
|
2056
|
+
const nodeBefore = tr.doc.nodeAt(before);
|
|
2057
|
+
const canJoinBackwards = list.node.type === (nodeBefore == null ? void 0 : nodeBefore.type) && canJoin(tr.doc, list.pos);
|
|
2058
|
+
if (!canJoinBackwards) {
|
|
2059
|
+
return true;
|
|
2060
|
+
}
|
|
2061
|
+
tr.join(list.pos);
|
|
2062
|
+
return true;
|
|
2063
|
+
};
|
|
2064
|
+
var joinListForwards = (tr, listType) => {
|
|
2065
|
+
const list = findParentNode((node) => node.type === listType)(tr.selection);
|
|
2066
|
+
if (!list) {
|
|
2067
|
+
return true;
|
|
2068
|
+
}
|
|
2069
|
+
const after = tr.doc.resolve(list.start).after(list.depth);
|
|
2070
|
+
if (after === void 0) {
|
|
2071
|
+
return true;
|
|
2072
|
+
}
|
|
2073
|
+
const nodeAfter = tr.doc.nodeAt(after);
|
|
2074
|
+
const canJoinForwards = list.node.type === (nodeAfter == null ? void 0 : nodeAfter.type) && canJoin(tr.doc, after);
|
|
2075
|
+
if (!canJoinForwards) {
|
|
2076
|
+
return true;
|
|
2077
|
+
}
|
|
2078
|
+
tr.join(after);
|
|
2079
|
+
return true;
|
|
2080
|
+
};
|
|
2081
|
+
var toggleList = (listTypeOrName, itemTypeOrName, keepMarks, attributes = {}) => ({ editor, tr, state, dispatch, chain, commands, can }) => {
|
|
2082
|
+
const { extensions, splittableMarks } = editor.extensionManager;
|
|
2083
|
+
const listType = getNodeType(listTypeOrName, state.schema);
|
|
2084
|
+
const itemType = getNodeType(itemTypeOrName, state.schema);
|
|
2085
|
+
const { selection, storedMarks } = state;
|
|
2086
|
+
const { $from, $to } = selection;
|
|
2087
|
+
const range = $from.blockRange($to);
|
|
2088
|
+
const marks = storedMarks || selection.$to.parentOffset && selection.$from.marks();
|
|
2089
|
+
if (!range) {
|
|
2090
|
+
return false;
|
|
2091
|
+
}
|
|
2092
|
+
const parentList = findParentNode((node) => isList(node.type.name, extensions))(selection);
|
|
2093
|
+
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
|
|
2094
|
+
if (parentList.node.type === listType) {
|
|
2095
|
+
return commands.liftListItem(itemType);
|
|
2096
|
+
}
|
|
2097
|
+
if (isList(parentList.node.type.name, extensions) && listType.validContent(parentList.node.content) && dispatch) {
|
|
2098
|
+
return chain().command(() => {
|
|
2099
|
+
tr.setNodeMarkup(parentList.pos, listType);
|
|
2100
|
+
return true;
|
|
2101
|
+
}).command(() => joinListBackwards(tr, listType)).command(() => joinListForwards(tr, listType)).run();
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
if (!keepMarks || !marks || !dispatch) {
|
|
2105
|
+
return chain().command(() => {
|
|
2106
|
+
const canWrapInList = can().wrapInList(listType, attributes);
|
|
2107
|
+
if (canWrapInList) {
|
|
2108
|
+
return true;
|
|
2109
|
+
}
|
|
2110
|
+
return commands.clearNodes();
|
|
2111
|
+
}).wrapInList(listType, attributes).command(() => joinListBackwards(tr, listType)).command(() => joinListForwards(tr, listType)).run();
|
|
2112
|
+
}
|
|
2113
|
+
return chain().command(() => {
|
|
2114
|
+
const canWrapInList = can().wrapInList(listType, attributes);
|
|
2115
|
+
const filteredMarks = marks.filter((mark) => splittableMarks.includes(mark.type.name));
|
|
2116
|
+
tr.ensureMarks(filteredMarks);
|
|
2117
|
+
if (canWrapInList) {
|
|
2118
|
+
return true;
|
|
2119
|
+
}
|
|
2120
|
+
return commands.clearNodes();
|
|
2121
|
+
}).wrapInList(listType, attributes).command(() => joinListBackwards(tr, listType)).command(() => joinListForwards(tr, listType)).run();
|
|
2122
|
+
};
|
|
2123
|
+
var toggleMark = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
|
|
2124
|
+
const { extendEmptyMarkRange = false } = options;
|
|
2125
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2126
|
+
const isActive2 = isMarkActive(state, type, attributes);
|
|
2127
|
+
if (isActive2) {
|
|
2128
|
+
return commands.unsetMark(type, { extendEmptyMarkRange });
|
|
2129
|
+
}
|
|
2130
|
+
return commands.setMark(type, attributes);
|
|
2131
|
+
};
|
|
2132
|
+
var toggleNode = (typeOrName, toggleTypeOrName, attributes = {}) => ({ state, commands }) => {
|
|
2133
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2134
|
+
const toggleType = getNodeType(toggleTypeOrName, state.schema);
|
|
2135
|
+
const isActive2 = isNodeActive(state, type, attributes);
|
|
2136
|
+
let attributesToCopy;
|
|
2137
|
+
if (state.selection.$anchor.sameParent(state.selection.$head)) {
|
|
2138
|
+
attributesToCopy = state.selection.$anchor.parent.attrs;
|
|
2139
|
+
}
|
|
2140
|
+
if (isActive2) {
|
|
2141
|
+
return commands.setNode(toggleType, attributesToCopy);
|
|
2142
|
+
}
|
|
2143
|
+
return commands.setNode(type, { ...attributesToCopy, ...attributes });
|
|
2144
|
+
};
|
|
2145
|
+
var toggleWrap = (typeOrName, attributes = {}) => ({ state, commands }) => {
|
|
2146
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2147
|
+
const isActive2 = isNodeActive(state, type, attributes);
|
|
2148
|
+
if (isActive2) {
|
|
2149
|
+
return commands.lift(type);
|
|
2150
|
+
}
|
|
2151
|
+
return commands.wrapIn(type, attributes);
|
|
2152
|
+
};
|
|
2153
|
+
var undoInputRule = () => ({ state, dispatch }) => {
|
|
2154
|
+
const plugins = state.plugins;
|
|
2155
|
+
for (let i = 0; i < plugins.length; i += 1) {
|
|
2156
|
+
const plugin = plugins[i];
|
|
2157
|
+
let undoable;
|
|
2158
|
+
if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
|
|
2159
|
+
if (dispatch) {
|
|
2160
|
+
const tr = state.tr;
|
|
2161
|
+
const toUndo = undoable.transform;
|
|
2162
|
+
for (let j = toUndo.steps.length - 1; j >= 0; j -= 1) {
|
|
2163
|
+
tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
|
|
2164
|
+
}
|
|
2165
|
+
if (undoable.text) {
|
|
2166
|
+
const marks = tr.doc.resolve(undoable.from).marks();
|
|
2167
|
+
tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks));
|
|
2168
|
+
} else {
|
|
2169
|
+
tr.delete(undoable.from, undoable.to);
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
return true;
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
return false;
|
|
2176
|
+
};
|
|
2177
|
+
var unsetAllMarks = () => ({ tr, dispatch }) => {
|
|
2178
|
+
const { selection } = tr;
|
|
2179
|
+
const { empty, ranges } = selection;
|
|
2180
|
+
if (empty) {
|
|
2181
|
+
return true;
|
|
2182
|
+
}
|
|
2183
|
+
if (dispatch) {
|
|
2184
|
+
ranges.forEach((range) => {
|
|
2185
|
+
tr.removeMark(range.$from.pos, range.$to.pos);
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
return true;
|
|
2189
|
+
};
|
|
2190
|
+
var unsetMark = (typeOrName, options = {}) => ({ tr, state, dispatch }) => {
|
|
2191
|
+
var _a;
|
|
2192
|
+
const { extendEmptyMarkRange = false } = options;
|
|
2193
|
+
const { selection } = tr;
|
|
2194
|
+
const type = getMarkType(typeOrName, state.schema);
|
|
2195
|
+
const { $from, empty, ranges } = selection;
|
|
2196
|
+
if (!dispatch) {
|
|
2197
|
+
return true;
|
|
2198
|
+
}
|
|
2199
|
+
if (empty && extendEmptyMarkRange) {
|
|
2200
|
+
let { from, to } = selection;
|
|
2201
|
+
const attrs = (_a = $from.marks().find((mark) => mark.type === type)) == null ? void 0 : _a.attrs;
|
|
2202
|
+
const range = getMarkRange($from, type, attrs);
|
|
2203
|
+
if (range) {
|
|
2204
|
+
from = range.from;
|
|
2205
|
+
to = range.to;
|
|
2206
|
+
}
|
|
2207
|
+
tr.removeMark(from, to, type);
|
|
2208
|
+
} else {
|
|
2209
|
+
ranges.forEach((range) => {
|
|
2210
|
+
tr.removeMark(range.$from.pos, range.$to.pos, type);
|
|
2211
|
+
});
|
|
2212
|
+
}
|
|
2213
|
+
tr.removeStoredMark(type);
|
|
2214
|
+
return true;
|
|
2215
|
+
};
|
|
2216
|
+
var unsetTextDirection = (position) => ({ tr, state, dispatch }) => {
|
|
2217
|
+
const { selection } = state;
|
|
2218
|
+
let from;
|
|
2219
|
+
let to;
|
|
2220
|
+
if (typeof position === "number") {
|
|
2221
|
+
from = position;
|
|
2222
|
+
to = position;
|
|
2223
|
+
} else if (position && "from" in position && "to" in position) {
|
|
2224
|
+
from = position.from;
|
|
2225
|
+
to = position.to;
|
|
2226
|
+
} else {
|
|
2227
|
+
from = selection.from;
|
|
2228
|
+
to = selection.to;
|
|
2229
|
+
}
|
|
2230
|
+
if (dispatch) {
|
|
2231
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
2232
|
+
if (node.isText) {
|
|
2233
|
+
return;
|
|
2234
|
+
}
|
|
2235
|
+
const newAttrs = { ...node.attrs };
|
|
2236
|
+
delete newAttrs.dir;
|
|
2237
|
+
tr.setNodeMarkup(pos, void 0, newAttrs);
|
|
2238
|
+
});
|
|
2239
|
+
}
|
|
2240
|
+
return true;
|
|
2241
|
+
};
|
|
2242
|
+
var updateAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
|
2243
|
+
let nodeType = null;
|
|
2244
|
+
let markType = null;
|
|
2245
|
+
const schemaType = getSchemaTypeNameByName(
|
|
2246
|
+
typeof typeOrName === "string" ? typeOrName : typeOrName.name,
|
|
2247
|
+
state.schema
|
|
2248
|
+
);
|
|
2249
|
+
if (!schemaType) {
|
|
2250
|
+
return false;
|
|
2251
|
+
}
|
|
2252
|
+
if (schemaType === "node") {
|
|
2253
|
+
nodeType = getNodeType(typeOrName, state.schema);
|
|
2254
|
+
}
|
|
2255
|
+
if (schemaType === "mark") {
|
|
2256
|
+
markType = getMarkType(typeOrName, state.schema);
|
|
2257
|
+
}
|
|
2258
|
+
let canUpdate = false;
|
|
2259
|
+
tr.selection.ranges.forEach((range) => {
|
|
2260
|
+
const from = range.$from.pos;
|
|
2261
|
+
const to = range.$to.pos;
|
|
2262
|
+
let lastPos;
|
|
2263
|
+
let lastNode;
|
|
2264
|
+
let trimmedFrom;
|
|
2265
|
+
let trimmedTo;
|
|
2266
|
+
if (tr.selection.empty) {
|
|
2267
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2268
|
+
if (nodeType && nodeType === node.type) {
|
|
2269
|
+
canUpdate = true;
|
|
2270
|
+
trimmedFrom = Math.max(pos, from);
|
|
2271
|
+
trimmedTo = Math.min(pos + node.nodeSize, to);
|
|
2272
|
+
lastPos = pos;
|
|
2273
|
+
lastNode = node;
|
|
2274
|
+
}
|
|
2275
|
+
});
|
|
2276
|
+
} else {
|
|
2277
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2278
|
+
if (pos < from && nodeType && nodeType === node.type) {
|
|
2279
|
+
canUpdate = true;
|
|
2280
|
+
trimmedFrom = Math.max(pos, from);
|
|
2281
|
+
trimmedTo = Math.min(pos + node.nodeSize, to);
|
|
2282
|
+
lastPos = pos;
|
|
2283
|
+
lastNode = node;
|
|
2284
|
+
}
|
|
2285
|
+
if (pos >= from && pos <= to) {
|
|
2286
|
+
if (nodeType && nodeType === node.type) {
|
|
2287
|
+
canUpdate = true;
|
|
2288
|
+
if (dispatch) {
|
|
2289
|
+
tr.setNodeMarkup(pos, void 0, {
|
|
2290
|
+
...node.attrs,
|
|
2291
|
+
...attributes
|
|
2292
|
+
});
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
if (markType && node.marks.length) {
|
|
2296
|
+
node.marks.forEach((mark) => {
|
|
2297
|
+
if (markType === mark.type) {
|
|
2298
|
+
canUpdate = true;
|
|
2299
|
+
if (dispatch) {
|
|
2300
|
+
const trimmedFrom2 = Math.max(pos, from);
|
|
2301
|
+
const trimmedTo2 = Math.min(pos + node.nodeSize, to);
|
|
2302
|
+
tr.addMark(
|
|
2303
|
+
trimmedFrom2,
|
|
2304
|
+
trimmedTo2,
|
|
2305
|
+
markType.create({
|
|
2306
|
+
...mark.attrs,
|
|
2307
|
+
...attributes
|
|
2308
|
+
})
|
|
2309
|
+
);
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
});
|
|
2316
|
+
}
|
|
2317
|
+
if (lastNode) {
|
|
2318
|
+
if (lastPos !== void 0 && dispatch) {
|
|
2319
|
+
tr.setNodeMarkup(lastPos, void 0, {
|
|
2320
|
+
...lastNode.attrs,
|
|
2321
|
+
...attributes
|
|
2322
|
+
});
|
|
2323
|
+
}
|
|
2324
|
+
if (markType && lastNode.marks.length) {
|
|
2325
|
+
lastNode.marks.forEach((mark) => {
|
|
2326
|
+
if (markType === mark.type && dispatch) {
|
|
2327
|
+
tr.addMark(
|
|
2328
|
+
trimmedFrom,
|
|
2329
|
+
trimmedTo,
|
|
2330
|
+
markType.create({
|
|
2331
|
+
...mark.attrs,
|
|
2332
|
+
...attributes
|
|
2333
|
+
})
|
|
2334
|
+
);
|
|
2335
|
+
}
|
|
2336
|
+
});
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
});
|
|
2340
|
+
return canUpdate;
|
|
2341
|
+
};
|
|
2342
|
+
var wrapIn = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
2343
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2344
|
+
return originalWrapIn(type, attributes)(state, dispatch);
|
|
2345
|
+
};
|
|
2346
|
+
var wrapInList = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
|
|
2347
|
+
const type = getNodeType(typeOrName, state.schema);
|
|
2348
|
+
return originalWrapInList(type, attributes)(state, dispatch);
|
|
2349
|
+
};
|
|
2350
|
+
var inputRuleMatcherHandler = (text, find) => {
|
|
2351
|
+
if (isRegExp(find)) {
|
|
2352
|
+
return find.exec(text);
|
|
2353
|
+
}
|
|
2354
|
+
const inputRuleMatch = find(text);
|
|
2355
|
+
if (!inputRuleMatch) {
|
|
2356
|
+
return null;
|
|
2357
|
+
}
|
|
2358
|
+
const result = [inputRuleMatch.text];
|
|
2359
|
+
result.index = inputRuleMatch.index;
|
|
2360
|
+
result.input = text;
|
|
2361
|
+
result.data = inputRuleMatch.data;
|
|
2362
|
+
if (inputRuleMatch.replaceWith) {
|
|
2363
|
+
if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) {
|
|
2364
|
+
console.warn('[tiptap warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".');
|
|
2365
|
+
}
|
|
2366
|
+
result.push(inputRuleMatch.replaceWith);
|
|
2367
|
+
}
|
|
2368
|
+
return result;
|
|
2369
|
+
};
|
|
2370
|
+
function run(config) {
|
|
2371
|
+
var _a;
|
|
2372
|
+
const { editor, from, to, text, rules, plugin } = config;
|
|
2373
|
+
const { view } = editor;
|
|
2374
|
+
if (view.composing) {
|
|
2375
|
+
return false;
|
|
2376
|
+
}
|
|
2377
|
+
const $from = view.state.doc.resolve(from);
|
|
2378
|
+
if (
|
|
2379
|
+
// check for code node
|
|
2380
|
+
$from.parent.type.spec.code || // check for code mark
|
|
2381
|
+
!!((_a = $from.nodeBefore || $from.nodeAfter) == null ? void 0 : _a.marks.find((mark) => mark.type.spec.code))
|
|
2382
|
+
) {
|
|
2383
|
+
return false;
|
|
2384
|
+
}
|
|
2385
|
+
let matched = false;
|
|
2386
|
+
const textBefore = getTextContentFromNodes($from) + text;
|
|
2387
|
+
rules.forEach((rule) => {
|
|
2388
|
+
if (matched) {
|
|
2389
|
+
return;
|
|
2390
|
+
}
|
|
2391
|
+
const match = inputRuleMatcherHandler(textBefore, rule.find);
|
|
2392
|
+
if (!match) {
|
|
2393
|
+
return;
|
|
2394
|
+
}
|
|
2395
|
+
const tr = view.state.tr;
|
|
2396
|
+
const state = createChainableState({
|
|
2397
|
+
state: view.state,
|
|
2398
|
+
transaction: tr
|
|
2399
|
+
});
|
|
2400
|
+
const range = {
|
|
2401
|
+
from: from - (match[0].length - text.length),
|
|
2402
|
+
to
|
|
2403
|
+
};
|
|
2404
|
+
const { commands, chain, can } = new CommandManager({
|
|
2405
|
+
editor,
|
|
2406
|
+
state
|
|
2407
|
+
});
|
|
2408
|
+
const handler = rule.handler({
|
|
2409
|
+
state,
|
|
2410
|
+
range,
|
|
2411
|
+
match,
|
|
2412
|
+
commands,
|
|
2413
|
+
chain,
|
|
2414
|
+
can
|
|
2415
|
+
});
|
|
2416
|
+
if (handler === null || !tr.steps.length) {
|
|
2417
|
+
return;
|
|
2418
|
+
}
|
|
2419
|
+
if (rule.undoable) {
|
|
2420
|
+
tr.setMeta(plugin, {
|
|
2421
|
+
transform: tr,
|
|
2422
|
+
from,
|
|
2423
|
+
to,
|
|
2424
|
+
text
|
|
2425
|
+
});
|
|
2426
|
+
}
|
|
2427
|
+
view.dispatch(tr);
|
|
2428
|
+
matched = true;
|
|
2429
|
+
});
|
|
2430
|
+
return matched;
|
|
2431
|
+
}
|
|
2432
|
+
function inputRulesPlugin(props) {
|
|
2433
|
+
const { editor, rules } = props;
|
|
2434
|
+
const plugin = new Plugin({
|
|
2435
|
+
state: {
|
|
2436
|
+
init() {
|
|
2437
|
+
return null;
|
|
2438
|
+
},
|
|
2439
|
+
apply(tr, prev, state) {
|
|
2440
|
+
const stored = tr.getMeta(plugin);
|
|
2441
|
+
if (stored) {
|
|
2442
|
+
return stored;
|
|
2443
|
+
}
|
|
2444
|
+
const simulatedInputMeta = tr.getMeta("applyInputRules");
|
|
2445
|
+
const isSimulatedInput = !!simulatedInputMeta;
|
|
2446
|
+
if (isSimulatedInput) {
|
|
2447
|
+
setTimeout(() => {
|
|
2448
|
+
let { text } = simulatedInputMeta;
|
|
2449
|
+
if (typeof text === "string") {
|
|
2450
|
+
text = text;
|
|
2451
|
+
} else {
|
|
2452
|
+
text = getHTMLFromFragment(Fragment4.from(text), state.schema);
|
|
2453
|
+
}
|
|
2454
|
+
const { from } = simulatedInputMeta;
|
|
2455
|
+
const to = from + text.length;
|
|
2456
|
+
run({
|
|
2457
|
+
editor,
|
|
2458
|
+
from,
|
|
2459
|
+
to,
|
|
2460
|
+
text,
|
|
2461
|
+
rules,
|
|
2462
|
+
plugin
|
|
2463
|
+
});
|
|
2464
|
+
});
|
|
2465
|
+
}
|
|
2466
|
+
return tr.selectionSet || tr.docChanged ? null : prev;
|
|
2467
|
+
}
|
|
2468
|
+
},
|
|
2469
|
+
props: {
|
|
2470
|
+
handleTextInput(view, from, to, text) {
|
|
2471
|
+
return run({
|
|
2472
|
+
editor,
|
|
2473
|
+
from,
|
|
2474
|
+
to,
|
|
2475
|
+
text,
|
|
2476
|
+
rules,
|
|
2477
|
+
plugin
|
|
2478
|
+
});
|
|
2479
|
+
},
|
|
2480
|
+
handleDOMEvents: {
|
|
2481
|
+
compositionend: (view) => {
|
|
2482
|
+
setTimeout(() => {
|
|
2483
|
+
const { $cursor } = view.state.selection;
|
|
2484
|
+
if ($cursor) {
|
|
2485
|
+
run({
|
|
2486
|
+
editor,
|
|
2487
|
+
from: $cursor.pos,
|
|
2488
|
+
to: $cursor.pos,
|
|
2489
|
+
text: "",
|
|
2490
|
+
rules,
|
|
2491
|
+
plugin
|
|
2492
|
+
});
|
|
2493
|
+
}
|
|
2494
|
+
});
|
|
2495
|
+
return false;
|
|
2496
|
+
}
|
|
2497
|
+
},
|
|
2498
|
+
// add support for input rules to trigger on enter
|
|
2499
|
+
// this is useful for example for code blocks
|
|
2500
|
+
handleKeyDown(view, event) {
|
|
2501
|
+
if (event.key !== "Enter") {
|
|
2502
|
+
return false;
|
|
2503
|
+
}
|
|
2504
|
+
const { $cursor } = view.state.selection;
|
|
2505
|
+
if ($cursor) {
|
|
2506
|
+
return run({
|
|
2507
|
+
editor,
|
|
2508
|
+
from: $cursor.pos,
|
|
2509
|
+
to: $cursor.pos,
|
|
2510
|
+
text: "\n",
|
|
2511
|
+
rules,
|
|
2512
|
+
plugin
|
|
2513
|
+
});
|
|
2514
|
+
}
|
|
2515
|
+
return false;
|
|
2516
|
+
}
|
|
2517
|
+
},
|
|
2518
|
+
// @ts-ignore
|
|
2519
|
+
isInputRules: true
|
|
2520
|
+
});
|
|
2521
|
+
return plugin;
|
|
2522
|
+
}
|
|
2523
|
+
function getType(value) {
|
|
2524
|
+
return Object.prototype.toString.call(value).slice(8, -1);
|
|
2525
|
+
}
|
|
2526
|
+
function isPlainObject(value) {
|
|
2527
|
+
if (getType(value) !== "Object") {
|
|
2528
|
+
return false;
|
|
2529
|
+
}
|
|
2530
|
+
return value.constructor === Object && Object.getPrototypeOf(value) === Object.prototype;
|
|
2531
|
+
}
|
|
2532
|
+
function mergeDeep(target, source) {
|
|
2533
|
+
const output = { ...target };
|
|
2534
|
+
if (isPlainObject(target) && isPlainObject(source)) {
|
|
2535
|
+
Object.keys(source).forEach((key) => {
|
|
2536
|
+
if (isPlainObject(source[key]) && isPlainObject(target[key])) {
|
|
2537
|
+
output[key] = mergeDeep(target[key], source[key]);
|
|
2538
|
+
} else {
|
|
2539
|
+
output[key] = source[key];
|
|
2540
|
+
}
|
|
2541
|
+
});
|
|
2542
|
+
}
|
|
2543
|
+
return output;
|
|
2544
|
+
}
|
|
2545
|
+
var Extendable = class {
|
|
2546
|
+
constructor(config = {}) {
|
|
2547
|
+
this.type = "extendable";
|
|
2548
|
+
this.parent = null;
|
|
2549
|
+
this.child = null;
|
|
2550
|
+
this.name = "";
|
|
2551
|
+
this.config = {
|
|
2552
|
+
name: this.name
|
|
2553
|
+
};
|
|
2554
|
+
this.config = {
|
|
2555
|
+
...this.config,
|
|
2556
|
+
...config
|
|
2557
|
+
};
|
|
2558
|
+
this.name = this.config.name;
|
|
2559
|
+
}
|
|
2560
|
+
get options() {
|
|
2561
|
+
return {
|
|
2562
|
+
...callOrReturn(
|
|
2563
|
+
getExtensionField(this, "addOptions", {
|
|
2564
|
+
name: this.name
|
|
2565
|
+
})
|
|
2566
|
+
) || {}
|
|
2567
|
+
};
|
|
2568
|
+
}
|
|
2569
|
+
get storage() {
|
|
2570
|
+
return {
|
|
2571
|
+
...callOrReturn(
|
|
2572
|
+
getExtensionField(this, "addStorage", {
|
|
2573
|
+
name: this.name,
|
|
2574
|
+
options: this.options
|
|
2575
|
+
})
|
|
2576
|
+
) || {}
|
|
2577
|
+
};
|
|
2578
|
+
}
|
|
2579
|
+
configure(options = {}) {
|
|
2580
|
+
const extension = this.extend({
|
|
2581
|
+
...this.config,
|
|
2582
|
+
addOptions: () => {
|
|
2583
|
+
return mergeDeep(this.options, options);
|
|
2584
|
+
}
|
|
2585
|
+
});
|
|
2586
|
+
extension.name = this.name;
|
|
2587
|
+
extension.parent = this.parent;
|
|
2588
|
+
return extension;
|
|
2589
|
+
}
|
|
2590
|
+
extend(extendedConfig = {}) {
|
|
2591
|
+
const extension = new this.constructor({ ...this.config, ...extendedConfig });
|
|
2592
|
+
extension.parent = this;
|
|
2593
|
+
this.child = extension;
|
|
2594
|
+
extension.name = "name" in extendedConfig ? extendedConfig.name : extension.parent.name;
|
|
2595
|
+
return extension;
|
|
2596
|
+
}
|
|
2597
|
+
};
|
|
2598
|
+
var Mark = class _Mark extends Extendable {
|
|
2599
|
+
constructor() {
|
|
2600
|
+
super(...arguments);
|
|
2601
|
+
this.type = "mark";
|
|
2602
|
+
}
|
|
2603
|
+
/**
|
|
2604
|
+
* Create a new Mark instance
|
|
2605
|
+
* @param config - Mark configuration object or a function that returns a configuration object
|
|
2606
|
+
*/
|
|
2607
|
+
static create(config = {}) {
|
|
2608
|
+
const resolvedConfig = typeof config === "function" ? config() : config;
|
|
2609
|
+
return new _Mark(resolvedConfig);
|
|
2610
|
+
}
|
|
2611
|
+
static handleExit({ editor, mark }) {
|
|
2612
|
+
const { tr } = editor.state;
|
|
2613
|
+
const currentPos = editor.state.selection.$from;
|
|
2614
|
+
const isAtEnd = currentPos.pos === currentPos.end();
|
|
2615
|
+
if (isAtEnd) {
|
|
2616
|
+
const currentMarks = currentPos.marks();
|
|
2617
|
+
const isInMark = !!currentMarks.find((m) => (m == null ? void 0 : m.type.name) === mark.name);
|
|
2618
|
+
if (!isInMark) {
|
|
2619
|
+
return false;
|
|
2620
|
+
}
|
|
2621
|
+
const removeMark = currentMarks.find((m) => (m == null ? void 0 : m.type.name) === mark.name);
|
|
2622
|
+
if (removeMark) {
|
|
2623
|
+
tr.removeStoredMark(removeMark);
|
|
2624
|
+
}
|
|
2625
|
+
tr.insertText(" ", currentPos.pos);
|
|
2626
|
+
editor.view.dispatch(tr);
|
|
2627
|
+
return true;
|
|
2628
|
+
}
|
|
2629
|
+
return false;
|
|
2630
|
+
}
|
|
2631
|
+
configure(options) {
|
|
2632
|
+
return super.configure(options);
|
|
2633
|
+
}
|
|
2634
|
+
extend(extendedConfig) {
|
|
2635
|
+
const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
|
|
2636
|
+
return super.extend(resolvedConfig);
|
|
2637
|
+
}
|
|
2638
|
+
};
|
|
2639
|
+
function isNumber(value) {
|
|
2640
|
+
return typeof value === "number";
|
|
2641
|
+
}
|
|
2642
|
+
var pasteRuleMatcherHandler = (text, find, event) => {
|
|
2643
|
+
if (isRegExp(find)) {
|
|
2644
|
+
return [...text.matchAll(find)];
|
|
2645
|
+
}
|
|
2646
|
+
const matches = find(text, event);
|
|
2647
|
+
if (!matches) {
|
|
2648
|
+
return [];
|
|
2649
|
+
}
|
|
2650
|
+
return matches.map((pasteRuleMatch) => {
|
|
2651
|
+
const result = [pasteRuleMatch.text];
|
|
2652
|
+
result.index = pasteRuleMatch.index;
|
|
2653
|
+
result.input = text;
|
|
2654
|
+
result.data = pasteRuleMatch.data;
|
|
2655
|
+
if (pasteRuleMatch.replaceWith) {
|
|
2656
|
+
if (!pasteRuleMatch.text.includes(pasteRuleMatch.replaceWith)) {
|
|
2657
|
+
console.warn('[tiptap warn]: "pasteRuleMatch.replaceWith" must be part of "pasteRuleMatch.text".');
|
|
2658
|
+
}
|
|
2659
|
+
result.push(pasteRuleMatch.replaceWith);
|
|
2660
|
+
}
|
|
2661
|
+
return result;
|
|
2662
|
+
});
|
|
2663
|
+
};
|
|
2664
|
+
function run2(config) {
|
|
2665
|
+
const { editor, state, from, to, rule, pasteEvent, dropEvent } = config;
|
|
2666
|
+
const { commands, chain, can } = new CommandManager({
|
|
2667
|
+
editor,
|
|
2668
|
+
state
|
|
2669
|
+
});
|
|
2670
|
+
const handlers = [];
|
|
2671
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
2672
|
+
var _a, _b, _c, _d, _e;
|
|
2673
|
+
if (((_b = (_a = node.type) == null ? void 0 : _a.spec) == null ? void 0 : _b.code) || !(node.isText || node.isTextblock || node.isInline)) {
|
|
2674
|
+
return;
|
|
2675
|
+
}
|
|
2676
|
+
const contentSize = (_e = (_d = (_c = node.content) == null ? void 0 : _c.size) != null ? _d : node.nodeSize) != null ? _e : 0;
|
|
2677
|
+
const resolvedFrom = Math.max(from, pos);
|
|
2678
|
+
const resolvedTo = Math.min(to, pos + contentSize);
|
|
2679
|
+
if (resolvedFrom >= resolvedTo) {
|
|
2680
|
+
return;
|
|
2681
|
+
}
|
|
2682
|
+
const textToMatch = node.isText ? node.text || "" : node.textBetween(resolvedFrom - pos, resolvedTo - pos, void 0, "\uFFFC");
|
|
2683
|
+
const matches = pasteRuleMatcherHandler(textToMatch, rule.find, pasteEvent);
|
|
2684
|
+
matches.forEach((match) => {
|
|
2685
|
+
if (match.index === void 0) {
|
|
2686
|
+
return;
|
|
2687
|
+
}
|
|
2688
|
+
const start = resolvedFrom + match.index + 1;
|
|
2689
|
+
const end = start + match[0].length;
|
|
2690
|
+
const range = {
|
|
2691
|
+
from: state.tr.mapping.map(start),
|
|
2692
|
+
to: state.tr.mapping.map(end)
|
|
2693
|
+
};
|
|
2694
|
+
const handler = rule.handler({
|
|
2695
|
+
state,
|
|
2696
|
+
range,
|
|
2697
|
+
match,
|
|
2698
|
+
commands,
|
|
2699
|
+
chain,
|
|
2700
|
+
can,
|
|
2701
|
+
pasteEvent,
|
|
2702
|
+
dropEvent
|
|
2703
|
+
});
|
|
2704
|
+
handlers.push(handler);
|
|
2705
|
+
});
|
|
2706
|
+
});
|
|
2707
|
+
const success = handlers.every((handler) => handler !== null);
|
|
2708
|
+
return success;
|
|
2709
|
+
}
|
|
2710
|
+
var tiptapDragFromOtherEditor = null;
|
|
2711
|
+
var createClipboardPasteEvent = (text) => {
|
|
2712
|
+
var _a;
|
|
2713
|
+
const event = new ClipboardEvent("paste", {
|
|
2714
|
+
clipboardData: new DataTransfer()
|
|
2715
|
+
});
|
|
2716
|
+
(_a = event.clipboardData) == null ? void 0 : _a.setData("text/html", text);
|
|
2717
|
+
return event;
|
|
2718
|
+
};
|
|
2719
|
+
function pasteRulesPlugin(props) {
|
|
2720
|
+
const { editor, rules } = props;
|
|
2721
|
+
let dragSourceElement = null;
|
|
2722
|
+
let isPastedFromProseMirror = false;
|
|
2723
|
+
let isDroppedFromProseMirror = false;
|
|
2724
|
+
let pasteEvent = typeof ClipboardEvent !== "undefined" ? new ClipboardEvent("paste") : null;
|
|
2725
|
+
let dropEvent;
|
|
2726
|
+
try {
|
|
2727
|
+
dropEvent = typeof DragEvent !== "undefined" ? new DragEvent("drop") : null;
|
|
2728
|
+
} catch {
|
|
2729
|
+
dropEvent = null;
|
|
2730
|
+
}
|
|
2731
|
+
const processEvent = ({
|
|
2732
|
+
state,
|
|
2733
|
+
from,
|
|
2734
|
+
to,
|
|
2735
|
+
rule,
|
|
2736
|
+
pasteEvt
|
|
2737
|
+
}) => {
|
|
2738
|
+
const tr = state.tr;
|
|
2739
|
+
const chainableState = createChainableState({
|
|
2740
|
+
state,
|
|
2741
|
+
transaction: tr
|
|
2742
|
+
});
|
|
2743
|
+
const handler = run2({
|
|
2744
|
+
editor,
|
|
2745
|
+
state: chainableState,
|
|
2746
|
+
from: Math.max(from - 1, 0),
|
|
2747
|
+
to: to.b - 1,
|
|
2748
|
+
rule,
|
|
2749
|
+
pasteEvent: pasteEvt,
|
|
2750
|
+
dropEvent
|
|
2751
|
+
});
|
|
2752
|
+
if (!handler || !tr.steps.length) {
|
|
2753
|
+
return;
|
|
2754
|
+
}
|
|
2755
|
+
try {
|
|
2756
|
+
dropEvent = typeof DragEvent !== "undefined" ? new DragEvent("drop") : null;
|
|
2757
|
+
} catch {
|
|
2758
|
+
dropEvent = null;
|
|
2759
|
+
}
|
|
2760
|
+
pasteEvent = typeof ClipboardEvent !== "undefined" ? new ClipboardEvent("paste") : null;
|
|
2761
|
+
return tr;
|
|
2762
|
+
};
|
|
2763
|
+
const plugins = rules.map((rule) => {
|
|
2764
|
+
return new Plugin2({
|
|
2765
|
+
// we register a global drag handler to track the current drag source element
|
|
2766
|
+
view(view) {
|
|
2767
|
+
const handleDragstart = (event) => {
|
|
2768
|
+
var _a;
|
|
2769
|
+
dragSourceElement = ((_a = view.dom.parentElement) == null ? void 0 : _a.contains(event.target)) ? view.dom.parentElement : null;
|
|
2770
|
+
if (dragSourceElement) {
|
|
2771
|
+
tiptapDragFromOtherEditor = editor;
|
|
2772
|
+
}
|
|
2773
|
+
};
|
|
2774
|
+
const handleDragend = () => {
|
|
2775
|
+
if (tiptapDragFromOtherEditor) {
|
|
2776
|
+
tiptapDragFromOtherEditor = null;
|
|
2777
|
+
}
|
|
2778
|
+
};
|
|
2779
|
+
window.addEventListener("dragstart", handleDragstart);
|
|
2780
|
+
window.addEventListener("dragend", handleDragend);
|
|
2781
|
+
return {
|
|
2782
|
+
destroy() {
|
|
2783
|
+
window.removeEventListener("dragstart", handleDragstart);
|
|
2784
|
+
window.removeEventListener("dragend", handleDragend);
|
|
2785
|
+
}
|
|
2786
|
+
};
|
|
2787
|
+
},
|
|
2788
|
+
props: {
|
|
2789
|
+
handleDOMEvents: {
|
|
2790
|
+
drop: (view, event) => {
|
|
2791
|
+
isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement;
|
|
2792
|
+
dropEvent = event;
|
|
2793
|
+
if (!isDroppedFromProseMirror) {
|
|
2794
|
+
const dragFromOtherEditor = tiptapDragFromOtherEditor;
|
|
2795
|
+
if (dragFromOtherEditor == null ? void 0 : dragFromOtherEditor.isEditable) {
|
|
2796
|
+
setTimeout(() => {
|
|
2797
|
+
const selection = dragFromOtherEditor.state.selection;
|
|
2798
|
+
if (selection) {
|
|
2799
|
+
dragFromOtherEditor.commands.deleteRange({ from: selection.from, to: selection.to });
|
|
2800
|
+
}
|
|
2801
|
+
}, 10);
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
return false;
|
|
2805
|
+
},
|
|
2806
|
+
paste: (_view, event) => {
|
|
2807
|
+
var _a;
|
|
2808
|
+
const html = (_a = event.clipboardData) == null ? void 0 : _a.getData("text/html");
|
|
2809
|
+
pasteEvent = event;
|
|
2810
|
+
isPastedFromProseMirror = !!(html == null ? void 0 : html.includes("data-pm-slice"));
|
|
2811
|
+
return false;
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
},
|
|
2815
|
+
appendTransaction: (transactions, oldState, state) => {
|
|
2816
|
+
const transaction = transactions[0];
|
|
2817
|
+
const isPaste = transaction.getMeta("uiEvent") === "paste" && !isPastedFromProseMirror;
|
|
2818
|
+
const isDrop = transaction.getMeta("uiEvent") === "drop" && !isDroppedFromProseMirror;
|
|
2819
|
+
const simulatedPasteMeta = transaction.getMeta("applyPasteRules");
|
|
2820
|
+
const isSimulatedPaste = !!simulatedPasteMeta;
|
|
2821
|
+
if (!isPaste && !isDrop && !isSimulatedPaste) {
|
|
2822
|
+
return;
|
|
2823
|
+
}
|
|
2824
|
+
if (isSimulatedPaste) {
|
|
2825
|
+
let { text } = simulatedPasteMeta;
|
|
2826
|
+
if (typeof text === "string") {
|
|
2827
|
+
text = text;
|
|
2828
|
+
} else {
|
|
2829
|
+
text = getHTMLFromFragment(Fragment5.from(text), state.schema);
|
|
2830
|
+
}
|
|
2831
|
+
const { from: from2 } = simulatedPasteMeta;
|
|
2832
|
+
const to2 = from2 + text.length;
|
|
2833
|
+
const pasteEvt = createClipboardPasteEvent(text);
|
|
2834
|
+
return processEvent({
|
|
2835
|
+
rule,
|
|
2836
|
+
state,
|
|
2837
|
+
from: from2,
|
|
2838
|
+
to: { b: to2 },
|
|
2839
|
+
pasteEvt
|
|
2840
|
+
});
|
|
2841
|
+
}
|
|
2842
|
+
const from = oldState.doc.content.findDiffStart(state.doc.content);
|
|
2843
|
+
const to = oldState.doc.content.findDiffEnd(state.doc.content);
|
|
2844
|
+
if (!isNumber(from) || !to || from === to.b) {
|
|
2845
|
+
return;
|
|
2846
|
+
}
|
|
2847
|
+
return processEvent({
|
|
2848
|
+
rule,
|
|
2849
|
+
state,
|
|
2850
|
+
from,
|
|
2851
|
+
to,
|
|
2852
|
+
pasteEvt: pasteEvent
|
|
2853
|
+
});
|
|
2854
|
+
}
|
|
2855
|
+
});
|
|
2856
|
+
});
|
|
2857
|
+
return plugins;
|
|
2858
|
+
}
|
|
2859
|
+
var ExtensionManager = class {
|
|
2860
|
+
constructor(extensions, editor) {
|
|
2861
|
+
this.splittableMarks = [];
|
|
2862
|
+
this.editor = editor;
|
|
2863
|
+
this.baseExtensions = extensions;
|
|
2864
|
+
this.extensions = resolveExtensions(extensions);
|
|
2865
|
+
this.schema = getSchemaByResolvedExtensions(this.extensions, editor);
|
|
2866
|
+
this.setupExtensions();
|
|
2867
|
+
}
|
|
2868
|
+
/**
|
|
2869
|
+
* Get all commands from the extensions.
|
|
2870
|
+
* @returns An object with all commands where the key is the command name and the value is the command function
|
|
2871
|
+
*/
|
|
2872
|
+
get commands() {
|
|
2873
|
+
return this.extensions.reduce((commands, extension) => {
|
|
2874
|
+
const context = {
|
|
2875
|
+
name: extension.name,
|
|
2876
|
+
options: extension.options,
|
|
2877
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
2878
|
+
editor: this.editor,
|
|
2879
|
+
type: getSchemaTypeByName(extension.name, this.schema)
|
|
2880
|
+
};
|
|
2881
|
+
const addCommands = getExtensionField(extension, "addCommands", context);
|
|
2882
|
+
if (!addCommands) {
|
|
2883
|
+
return commands;
|
|
2884
|
+
}
|
|
2885
|
+
return {
|
|
2886
|
+
...commands,
|
|
2887
|
+
...addCommands()
|
|
2888
|
+
};
|
|
2889
|
+
}, {});
|
|
2890
|
+
}
|
|
2891
|
+
/**
|
|
2892
|
+
* Get all registered Prosemirror plugins from the extensions.
|
|
2893
|
+
* @returns An array of Prosemirror plugins
|
|
2894
|
+
*/
|
|
2895
|
+
get plugins() {
|
|
2896
|
+
const { editor } = this;
|
|
2897
|
+
const extensions = sortExtensions([...this.extensions].reverse());
|
|
2898
|
+
const allPlugins = extensions.flatMap((extension) => {
|
|
2899
|
+
const context = {
|
|
2900
|
+
name: extension.name,
|
|
2901
|
+
options: extension.options,
|
|
2902
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
2903
|
+
editor,
|
|
2904
|
+
type: getSchemaTypeByName(extension.name, this.schema)
|
|
2905
|
+
};
|
|
2906
|
+
const plugins = [];
|
|
2907
|
+
const addKeyboardShortcuts = getExtensionField(
|
|
2908
|
+
extension,
|
|
2909
|
+
"addKeyboardShortcuts",
|
|
2910
|
+
context
|
|
2911
|
+
);
|
|
2912
|
+
let defaultBindings = {};
|
|
2913
|
+
if (extension.type === "mark" && getExtensionField(extension, "exitable", context)) {
|
|
2914
|
+
defaultBindings.ArrowRight = () => Mark.handleExit({ editor, mark: extension });
|
|
2915
|
+
}
|
|
2916
|
+
if (addKeyboardShortcuts) {
|
|
2917
|
+
const bindings = Object.fromEntries(
|
|
2918
|
+
Object.entries(addKeyboardShortcuts()).map(([shortcut, method]) => {
|
|
2919
|
+
return [shortcut, () => method({ editor })];
|
|
2920
|
+
})
|
|
2921
|
+
);
|
|
2922
|
+
defaultBindings = { ...defaultBindings, ...bindings };
|
|
2923
|
+
}
|
|
2924
|
+
const keyMapPlugin = keymap(defaultBindings);
|
|
2925
|
+
plugins.push(keyMapPlugin);
|
|
2926
|
+
const addInputRules = getExtensionField(extension, "addInputRules", context);
|
|
2927
|
+
if (isExtensionRulesEnabled(extension, editor.options.enableInputRules) && addInputRules) {
|
|
2928
|
+
const rules = addInputRules();
|
|
2929
|
+
if (rules && rules.length) {
|
|
2930
|
+
const inputResult = inputRulesPlugin({
|
|
2931
|
+
editor,
|
|
2932
|
+
rules
|
|
2933
|
+
});
|
|
2934
|
+
const inputPlugins = Array.isArray(inputResult) ? inputResult : [inputResult];
|
|
2935
|
+
plugins.push(...inputPlugins);
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
const addPasteRules = getExtensionField(extension, "addPasteRules", context);
|
|
2939
|
+
if (isExtensionRulesEnabled(extension, editor.options.enablePasteRules) && addPasteRules) {
|
|
2940
|
+
const rules = addPasteRules();
|
|
2941
|
+
if (rules && rules.length) {
|
|
2942
|
+
const pasteRules = pasteRulesPlugin({ editor, rules });
|
|
2943
|
+
plugins.push(...pasteRules);
|
|
2944
|
+
}
|
|
2945
|
+
}
|
|
2946
|
+
const addProseMirrorPlugins = getExtensionField(
|
|
2947
|
+
extension,
|
|
2948
|
+
"addProseMirrorPlugins",
|
|
2949
|
+
context
|
|
2950
|
+
);
|
|
2951
|
+
if (addProseMirrorPlugins) {
|
|
2952
|
+
const proseMirrorPlugins = addProseMirrorPlugins();
|
|
2953
|
+
plugins.push(...proseMirrorPlugins);
|
|
2954
|
+
}
|
|
2955
|
+
return plugins;
|
|
2956
|
+
});
|
|
2957
|
+
return allPlugins;
|
|
2958
|
+
}
|
|
2959
|
+
/**
|
|
2960
|
+
* Get all attributes from the extensions.
|
|
2961
|
+
* @returns An array of attributes
|
|
2962
|
+
*/
|
|
2963
|
+
get attributes() {
|
|
2964
|
+
return getAttributesFromExtensions(this.extensions);
|
|
2965
|
+
}
|
|
2966
|
+
/**
|
|
2967
|
+
* Get all node views from the extensions.
|
|
2968
|
+
* @returns An object with all node views where the key is the node name and the value is the node view function
|
|
2969
|
+
*/
|
|
2970
|
+
get nodeViews() {
|
|
2971
|
+
const { editor } = this;
|
|
2972
|
+
const { nodeExtensions } = splitExtensions(this.extensions);
|
|
2973
|
+
return Object.fromEntries(
|
|
2974
|
+
nodeExtensions.filter((extension) => !!getExtensionField(extension, "addNodeView")).map((extension) => {
|
|
2975
|
+
const extensionAttributes = this.attributes.filter((attribute) => attribute.type === extension.name);
|
|
2976
|
+
const context = {
|
|
2977
|
+
name: extension.name,
|
|
2978
|
+
options: extension.options,
|
|
2979
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
2980
|
+
editor,
|
|
2981
|
+
type: getNodeType(extension.name, this.schema)
|
|
2982
|
+
};
|
|
2983
|
+
const addNodeView = getExtensionField(extension, "addNodeView", context);
|
|
2984
|
+
if (!addNodeView) {
|
|
2985
|
+
return [];
|
|
2986
|
+
}
|
|
2987
|
+
const nodeViewResult = addNodeView();
|
|
2988
|
+
if (!nodeViewResult) {
|
|
2989
|
+
return [];
|
|
2990
|
+
}
|
|
2991
|
+
const nodeview = (node, view, getPos, decorations, innerDecorations) => {
|
|
2992
|
+
const HTMLAttributes = getRenderedAttributes(node, extensionAttributes);
|
|
2993
|
+
return nodeViewResult({
|
|
2994
|
+
// pass-through
|
|
2995
|
+
node,
|
|
2996
|
+
view,
|
|
2997
|
+
getPos,
|
|
2998
|
+
decorations,
|
|
2999
|
+
innerDecorations,
|
|
3000
|
+
// tiptap-specific
|
|
3001
|
+
editor,
|
|
3002
|
+
extension,
|
|
3003
|
+
HTMLAttributes
|
|
3004
|
+
});
|
|
3005
|
+
};
|
|
3006
|
+
return [extension.name, nodeview];
|
|
3007
|
+
})
|
|
3008
|
+
);
|
|
3009
|
+
}
|
|
3010
|
+
/**
|
|
3011
|
+
* Get the composed dispatchTransaction function from all extensions.
|
|
3012
|
+
* @param baseDispatch The base dispatch function (e.g. from the editor or user props)
|
|
3013
|
+
* @returns A composed dispatch function
|
|
3014
|
+
*/
|
|
3015
|
+
dispatchTransaction(baseDispatch) {
|
|
3016
|
+
const { editor } = this;
|
|
3017
|
+
const extensions = sortExtensions([...this.extensions].reverse());
|
|
3018
|
+
return extensions.reduceRight((next, extension) => {
|
|
3019
|
+
const context = {
|
|
3020
|
+
name: extension.name,
|
|
3021
|
+
options: extension.options,
|
|
3022
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
3023
|
+
editor,
|
|
3024
|
+
type: getSchemaTypeByName(extension.name, this.schema)
|
|
3025
|
+
};
|
|
3026
|
+
const dispatchTransaction = getExtensionField(
|
|
3027
|
+
extension,
|
|
3028
|
+
"dispatchTransaction",
|
|
3029
|
+
context
|
|
3030
|
+
);
|
|
3031
|
+
if (!dispatchTransaction) {
|
|
3032
|
+
return next;
|
|
3033
|
+
}
|
|
3034
|
+
return (transaction) => {
|
|
3035
|
+
dispatchTransaction.call(context, { transaction, next });
|
|
3036
|
+
};
|
|
3037
|
+
}, baseDispatch);
|
|
3038
|
+
}
|
|
3039
|
+
get markViews() {
|
|
3040
|
+
const { editor } = this;
|
|
3041
|
+
const { markExtensions } = splitExtensions(this.extensions);
|
|
3042
|
+
return Object.fromEntries(
|
|
3043
|
+
markExtensions.filter((extension) => !!getExtensionField(extension, "addMarkView")).map((extension) => {
|
|
3044
|
+
const extensionAttributes = this.attributes.filter((attribute) => attribute.type === extension.name);
|
|
3045
|
+
const context = {
|
|
3046
|
+
name: extension.name,
|
|
3047
|
+
options: extension.options,
|
|
3048
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
3049
|
+
editor,
|
|
3050
|
+
type: getMarkType(extension.name, this.schema)
|
|
3051
|
+
};
|
|
3052
|
+
const addMarkView = getExtensionField(extension, "addMarkView", context);
|
|
3053
|
+
if (!addMarkView) {
|
|
3054
|
+
return [];
|
|
3055
|
+
}
|
|
3056
|
+
const markView = (mark, view, inline) => {
|
|
3057
|
+
const HTMLAttributes = getRenderedAttributes(mark, extensionAttributes);
|
|
3058
|
+
return addMarkView()({
|
|
3059
|
+
// pass-through
|
|
3060
|
+
mark,
|
|
3061
|
+
view,
|
|
3062
|
+
inline,
|
|
3063
|
+
// tiptap-specific
|
|
3064
|
+
editor,
|
|
3065
|
+
extension,
|
|
3066
|
+
HTMLAttributes,
|
|
3067
|
+
updateAttributes: (attrs) => {
|
|
3068
|
+
updateMarkViewAttributes(mark, editor, attrs);
|
|
3069
|
+
}
|
|
3070
|
+
});
|
|
3071
|
+
};
|
|
3072
|
+
return [extension.name, markView];
|
|
3073
|
+
})
|
|
3074
|
+
);
|
|
3075
|
+
}
|
|
3076
|
+
/**
|
|
3077
|
+
* Go through all extensions, create extension storages & setup marks
|
|
3078
|
+
* & bind editor event listener.
|
|
3079
|
+
*/
|
|
3080
|
+
setupExtensions() {
|
|
3081
|
+
const extensions = this.extensions;
|
|
3082
|
+
this.editor.extensionStorage = Object.fromEntries(
|
|
3083
|
+
extensions.map((extension) => [extension.name, extension.storage])
|
|
3084
|
+
);
|
|
3085
|
+
extensions.forEach((extension) => {
|
|
3086
|
+
var _a;
|
|
3087
|
+
const context = {
|
|
3088
|
+
name: extension.name,
|
|
3089
|
+
options: extension.options,
|
|
3090
|
+
storage: this.editor.extensionStorage[extension.name],
|
|
3091
|
+
editor: this.editor,
|
|
3092
|
+
type: getSchemaTypeByName(extension.name, this.schema)
|
|
3093
|
+
};
|
|
3094
|
+
if (extension.type === "mark") {
|
|
3095
|
+
const keepOnSplit = (_a = callOrReturn(getExtensionField(extension, "keepOnSplit", context))) != null ? _a : true;
|
|
3096
|
+
if (keepOnSplit) {
|
|
3097
|
+
this.splittableMarks.push(extension.name);
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
const onBeforeCreate = getExtensionField(extension, "onBeforeCreate", context);
|
|
3101
|
+
const onCreate = getExtensionField(extension, "onCreate", context);
|
|
3102
|
+
const onUpdate = getExtensionField(extension, "onUpdate", context);
|
|
3103
|
+
const onSelectionUpdate = getExtensionField(
|
|
3104
|
+
extension,
|
|
3105
|
+
"onSelectionUpdate",
|
|
3106
|
+
context
|
|
3107
|
+
);
|
|
3108
|
+
const onTransaction = getExtensionField(extension, "onTransaction", context);
|
|
3109
|
+
const onFocus = getExtensionField(extension, "onFocus", context);
|
|
3110
|
+
const onBlur = getExtensionField(extension, "onBlur", context);
|
|
3111
|
+
const onDestroy = getExtensionField(extension, "onDestroy", context);
|
|
3112
|
+
if (onBeforeCreate) {
|
|
3113
|
+
this.editor.on("beforeCreate", onBeforeCreate);
|
|
3114
|
+
}
|
|
3115
|
+
if (onCreate) {
|
|
3116
|
+
this.editor.on("create", onCreate);
|
|
3117
|
+
}
|
|
3118
|
+
if (onUpdate) {
|
|
3119
|
+
this.editor.on("update", onUpdate);
|
|
3120
|
+
}
|
|
3121
|
+
if (onSelectionUpdate) {
|
|
3122
|
+
this.editor.on("selectionUpdate", onSelectionUpdate);
|
|
3123
|
+
}
|
|
3124
|
+
if (onTransaction) {
|
|
3125
|
+
this.editor.on("transaction", onTransaction);
|
|
3126
|
+
}
|
|
3127
|
+
if (onFocus) {
|
|
3128
|
+
this.editor.on("focus", onFocus);
|
|
3129
|
+
}
|
|
3130
|
+
if (onBlur) {
|
|
3131
|
+
this.editor.on("blur", onBlur);
|
|
3132
|
+
}
|
|
3133
|
+
if (onDestroy) {
|
|
3134
|
+
this.editor.on("destroy", onDestroy);
|
|
3135
|
+
}
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
};
|
|
3139
|
+
ExtensionManager.resolve = resolveExtensions;
|
|
3140
|
+
ExtensionManager.sort = sortExtensions;
|
|
3141
|
+
ExtensionManager.flatten = flattenExtensions;
|
|
3142
|
+
var extensions_exports = {};
|
|
3143
|
+
__export(extensions_exports, {
|
|
3144
|
+
ClipboardTextSerializer: () => ClipboardTextSerializer,
|
|
3145
|
+
Commands: () => Commands,
|
|
3146
|
+
Delete: () => Delete,
|
|
3147
|
+
Drop: () => Drop,
|
|
3148
|
+
Editable: () => Editable,
|
|
3149
|
+
FocusEvents: () => FocusEvents,
|
|
3150
|
+
Keymap: () => Keymap,
|
|
3151
|
+
Paste: () => Paste,
|
|
3152
|
+
Tabindex: () => Tabindex,
|
|
3153
|
+
TextDirection: () => TextDirection,
|
|
3154
|
+
focusEventsPluginKey: () => focusEventsPluginKey
|
|
3155
|
+
});
|
|
3156
|
+
var Extension = class _Extension extends Extendable {
|
|
3157
|
+
constructor() {
|
|
3158
|
+
super(...arguments);
|
|
3159
|
+
this.type = "extension";
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Create a new Extension instance
|
|
3163
|
+
* @param config - Extension configuration object or a function that returns a configuration object
|
|
3164
|
+
*/
|
|
3165
|
+
static create(config = {}) {
|
|
3166
|
+
const resolvedConfig = typeof config === "function" ? config() : config;
|
|
3167
|
+
return new _Extension(resolvedConfig);
|
|
3168
|
+
}
|
|
3169
|
+
configure(options) {
|
|
3170
|
+
return super.configure(options);
|
|
3171
|
+
}
|
|
3172
|
+
extend(extendedConfig) {
|
|
3173
|
+
const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
|
|
3174
|
+
return super.extend(resolvedConfig);
|
|
3175
|
+
}
|
|
3176
|
+
};
|
|
3177
|
+
var ClipboardTextSerializer = Extension.create({
|
|
3178
|
+
name: "clipboardTextSerializer",
|
|
3179
|
+
addOptions() {
|
|
3180
|
+
return {
|
|
3181
|
+
blockSeparator: void 0
|
|
3182
|
+
};
|
|
3183
|
+
},
|
|
3184
|
+
addProseMirrorPlugins() {
|
|
3185
|
+
return [
|
|
3186
|
+
new Plugin3({
|
|
3187
|
+
key: new PluginKey("clipboardTextSerializer"),
|
|
3188
|
+
props: {
|
|
3189
|
+
clipboardTextSerializer: () => {
|
|
3190
|
+
const { editor } = this;
|
|
3191
|
+
const { state, schema } = editor;
|
|
3192
|
+
const { doc, selection } = state;
|
|
3193
|
+
const { ranges } = selection;
|
|
3194
|
+
const from = Math.min(...ranges.map((range2) => range2.$from.pos));
|
|
3195
|
+
const to = Math.max(...ranges.map((range2) => range2.$to.pos));
|
|
3196
|
+
const textSerializers = getTextSerializersFromSchema(schema);
|
|
3197
|
+
const range = { from, to };
|
|
3198
|
+
return getTextBetween(doc, range, {
|
|
3199
|
+
...this.options.blockSeparator !== void 0 ? { blockSeparator: this.options.blockSeparator } : {},
|
|
3200
|
+
textSerializers
|
|
3201
|
+
});
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
})
|
|
3205
|
+
];
|
|
3206
|
+
}
|
|
3207
|
+
});
|
|
3208
|
+
var Commands = Extension.create({
|
|
3209
|
+
name: "commands",
|
|
3210
|
+
addCommands() {
|
|
3211
|
+
return {
|
|
3212
|
+
...commands_exports
|
|
3213
|
+
};
|
|
3214
|
+
}
|
|
3215
|
+
});
|
|
3216
|
+
var Delete = Extension.create({
|
|
3217
|
+
name: "delete",
|
|
3218
|
+
onUpdate({ transaction, appendedTransactions }) {
|
|
3219
|
+
var _a, _b, _c;
|
|
3220
|
+
const callback = () => {
|
|
3221
|
+
var _a2, _b2, _c2, _d;
|
|
3222
|
+
if ((_d = (_c2 = (_b2 = (_a2 = this.editor.options.coreExtensionOptions) == null ? void 0 : _a2.delete) == null ? void 0 : _b2.filterTransaction) == null ? void 0 : _c2.call(_b2, transaction)) != null ? _d : transaction.getMeta("y-sync$")) {
|
|
3223
|
+
return;
|
|
3224
|
+
}
|
|
3225
|
+
const nextTransaction = combineTransactionSteps(transaction.before, [transaction, ...appendedTransactions]);
|
|
3226
|
+
const changes = getChangedRanges(nextTransaction);
|
|
3227
|
+
changes.forEach((change) => {
|
|
3228
|
+
if (nextTransaction.mapping.mapResult(change.oldRange.from).deletedAfter && nextTransaction.mapping.mapResult(change.oldRange.to).deletedBefore) {
|
|
3229
|
+
nextTransaction.before.nodesBetween(change.oldRange.from, change.oldRange.to, (node, from) => {
|
|
3230
|
+
const to = from + node.nodeSize - 2;
|
|
3231
|
+
const isFullyWithinRange = change.oldRange.from <= from && to <= change.oldRange.to;
|
|
3232
|
+
this.editor.emit("delete", {
|
|
3233
|
+
type: "node",
|
|
3234
|
+
node,
|
|
3235
|
+
from,
|
|
3236
|
+
to,
|
|
3237
|
+
newFrom: nextTransaction.mapping.map(from),
|
|
3238
|
+
newTo: nextTransaction.mapping.map(to),
|
|
3239
|
+
deletedRange: change.oldRange,
|
|
3240
|
+
newRange: change.newRange,
|
|
3241
|
+
partial: !isFullyWithinRange,
|
|
3242
|
+
editor: this.editor,
|
|
3243
|
+
transaction,
|
|
3244
|
+
combinedTransform: nextTransaction
|
|
3245
|
+
});
|
|
3246
|
+
});
|
|
3247
|
+
}
|
|
3248
|
+
});
|
|
3249
|
+
const mapping = nextTransaction.mapping;
|
|
3250
|
+
nextTransaction.steps.forEach((step, index) => {
|
|
3251
|
+
var _a3, _b3;
|
|
3252
|
+
if (step instanceof RemoveMarkStep) {
|
|
3253
|
+
const newStart = mapping.slice(index).map(step.from, -1);
|
|
3254
|
+
const newEnd = mapping.slice(index).map(step.to);
|
|
3255
|
+
const oldStart = mapping.invert().map(newStart, -1);
|
|
3256
|
+
const oldEnd = mapping.invert().map(newEnd);
|
|
3257
|
+
const foundBeforeMark = (_a3 = nextTransaction.doc.nodeAt(newStart - 1)) == null ? void 0 : _a3.marks.some((mark) => mark.eq(step.mark));
|
|
3258
|
+
const foundAfterMark = (_b3 = nextTransaction.doc.nodeAt(newEnd)) == null ? void 0 : _b3.marks.some((mark) => mark.eq(step.mark));
|
|
3259
|
+
this.editor.emit("delete", {
|
|
3260
|
+
type: "mark",
|
|
3261
|
+
mark: step.mark,
|
|
3262
|
+
from: step.from,
|
|
3263
|
+
to: step.to,
|
|
3264
|
+
deletedRange: {
|
|
3265
|
+
from: oldStart,
|
|
3266
|
+
to: oldEnd
|
|
3267
|
+
},
|
|
3268
|
+
newRange: {
|
|
3269
|
+
from: newStart,
|
|
3270
|
+
to: newEnd
|
|
3271
|
+
},
|
|
3272
|
+
partial: Boolean(foundAfterMark || foundBeforeMark),
|
|
3273
|
+
editor: this.editor,
|
|
3274
|
+
transaction,
|
|
3275
|
+
combinedTransform: nextTransaction
|
|
3276
|
+
});
|
|
3277
|
+
}
|
|
3278
|
+
});
|
|
3279
|
+
};
|
|
3280
|
+
if ((_c = (_b = (_a = this.editor.options.coreExtensionOptions) == null ? void 0 : _a.delete) == null ? void 0 : _b.async) != null ? _c : true) {
|
|
3281
|
+
setTimeout(callback, 0);
|
|
3282
|
+
} else {
|
|
3283
|
+
callback();
|
|
3284
|
+
}
|
|
3285
|
+
}
|
|
3286
|
+
});
|
|
3287
|
+
var Drop = Extension.create({
|
|
3288
|
+
name: "drop",
|
|
3289
|
+
addProseMirrorPlugins() {
|
|
3290
|
+
return [
|
|
3291
|
+
new Plugin4({
|
|
3292
|
+
key: new PluginKey2("tiptapDrop"),
|
|
3293
|
+
props: {
|
|
3294
|
+
handleDrop: (_, e, slice, moved) => {
|
|
3295
|
+
this.editor.emit("drop", {
|
|
3296
|
+
editor: this.editor,
|
|
3297
|
+
event: e,
|
|
3298
|
+
slice,
|
|
3299
|
+
moved
|
|
3300
|
+
});
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
})
|
|
3304
|
+
];
|
|
3305
|
+
}
|
|
3306
|
+
});
|
|
3307
|
+
var Editable = Extension.create({
|
|
3308
|
+
name: "editable",
|
|
3309
|
+
addProseMirrorPlugins() {
|
|
3310
|
+
return [
|
|
3311
|
+
new Plugin5({
|
|
3312
|
+
key: new PluginKey3("editable"),
|
|
3313
|
+
props: {
|
|
3314
|
+
editable: () => this.editor.options.editable
|
|
3315
|
+
}
|
|
3316
|
+
})
|
|
3317
|
+
];
|
|
3318
|
+
}
|
|
3319
|
+
});
|
|
3320
|
+
var focusEventsPluginKey = new PluginKey4("focusEvents");
|
|
3321
|
+
var FocusEvents = Extension.create({
|
|
3322
|
+
name: "focusEvents",
|
|
3323
|
+
addProseMirrorPlugins() {
|
|
3324
|
+
const { editor } = this;
|
|
3325
|
+
return [
|
|
3326
|
+
new Plugin6({
|
|
3327
|
+
key: focusEventsPluginKey,
|
|
3328
|
+
props: {
|
|
3329
|
+
handleDOMEvents: {
|
|
3330
|
+
focus: (view, event) => {
|
|
3331
|
+
editor.isFocused = true;
|
|
3332
|
+
const transaction = editor.state.tr.setMeta("focus", { event }).setMeta("addToHistory", false);
|
|
3333
|
+
view.dispatch(transaction);
|
|
3334
|
+
return false;
|
|
3335
|
+
},
|
|
3336
|
+
blur: (view, event) => {
|
|
3337
|
+
editor.isFocused = false;
|
|
3338
|
+
const transaction = editor.state.tr.setMeta("blur", { event }).setMeta("addToHistory", false);
|
|
3339
|
+
view.dispatch(transaction);
|
|
3340
|
+
return false;
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
}
|
|
3344
|
+
})
|
|
3345
|
+
];
|
|
3346
|
+
}
|
|
3347
|
+
});
|
|
3348
|
+
var Keymap = Extension.create({
|
|
3349
|
+
name: "keymap",
|
|
3350
|
+
addKeyboardShortcuts() {
|
|
3351
|
+
const handleBackspace = () => this.editor.commands.first(({ commands }) => [
|
|
3352
|
+
() => commands.undoInputRule(),
|
|
3353
|
+
// maybe convert first text block node to default node
|
|
3354
|
+
() => commands.command(({ tr }) => {
|
|
3355
|
+
const { selection, doc } = tr;
|
|
3356
|
+
const { empty, $anchor } = selection;
|
|
3357
|
+
const { pos, parent } = $anchor;
|
|
3358
|
+
const $parentPos = $anchor.parent.isTextblock && pos > 0 ? tr.doc.resolve(pos - 1) : $anchor;
|
|
3359
|
+
const parentIsIsolating = $parentPos.parent.type.spec.isolating;
|
|
3360
|
+
const parentPos = $anchor.pos - $anchor.parentOffset;
|
|
3361
|
+
const isAtStart = parentIsIsolating && $parentPos.parent.childCount === 1 ? parentPos === $anchor.pos : Selection3.atStart(doc).from === pos;
|
|
3362
|
+
if (!empty || !parent.type.isTextblock || parent.textContent.length || !isAtStart || isAtStart && $anchor.parent.type.name === "paragraph") {
|
|
3363
|
+
return false;
|
|
3364
|
+
}
|
|
3365
|
+
return commands.clearNodes();
|
|
3366
|
+
}),
|
|
3367
|
+
() => commands.deleteSelection(),
|
|
3368
|
+
() => commands.joinBackward(),
|
|
3369
|
+
() => commands.selectNodeBackward()
|
|
3370
|
+
]);
|
|
3371
|
+
const handleDelete = () => this.editor.commands.first(({ commands }) => [
|
|
3372
|
+
() => commands.deleteSelection(),
|
|
3373
|
+
() => commands.deleteCurrentNode(),
|
|
3374
|
+
() => commands.joinForward(),
|
|
3375
|
+
() => commands.selectNodeForward()
|
|
3376
|
+
]);
|
|
3377
|
+
const handleEnter = () => this.editor.commands.first(({ commands }) => [
|
|
3378
|
+
() => commands.newlineInCode(),
|
|
3379
|
+
() => commands.createParagraphNear(),
|
|
3380
|
+
() => commands.liftEmptyBlock(),
|
|
3381
|
+
() => commands.splitBlock()
|
|
3382
|
+
]);
|
|
3383
|
+
const baseKeymap = {
|
|
3384
|
+
Enter: handleEnter,
|
|
3385
|
+
"Mod-Enter": () => this.editor.commands.exitCode(),
|
|
3386
|
+
Backspace: handleBackspace,
|
|
3387
|
+
"Mod-Backspace": handleBackspace,
|
|
3388
|
+
"Shift-Backspace": handleBackspace,
|
|
3389
|
+
Delete: handleDelete,
|
|
3390
|
+
"Mod-Delete": handleDelete,
|
|
3391
|
+
"Mod-a": () => this.editor.commands.selectAll()
|
|
3392
|
+
};
|
|
3393
|
+
const pcKeymap = {
|
|
3394
|
+
...baseKeymap
|
|
3395
|
+
};
|
|
3396
|
+
const macKeymap = {
|
|
3397
|
+
...baseKeymap,
|
|
3398
|
+
"Ctrl-h": handleBackspace,
|
|
3399
|
+
"Alt-Backspace": handleBackspace,
|
|
3400
|
+
"Ctrl-d": handleDelete,
|
|
3401
|
+
"Ctrl-Alt-Backspace": handleDelete,
|
|
3402
|
+
"Alt-Delete": handleDelete,
|
|
3403
|
+
"Alt-d": handleDelete,
|
|
3404
|
+
"Ctrl-a": () => this.editor.commands.selectTextblockStart(),
|
|
3405
|
+
"Ctrl-e": () => this.editor.commands.selectTextblockEnd()
|
|
3406
|
+
};
|
|
3407
|
+
if (isiOS() || isMacOS()) {
|
|
3408
|
+
return macKeymap;
|
|
3409
|
+
}
|
|
3410
|
+
return pcKeymap;
|
|
3411
|
+
},
|
|
3412
|
+
addProseMirrorPlugins() {
|
|
3413
|
+
return [
|
|
3414
|
+
// With this plugin we check if the whole document was selected and deleted.
|
|
3415
|
+
// In this case we will additionally call `clearNodes()` to convert e.g. a heading
|
|
3416
|
+
// to a paragraph if necessary.
|
|
3417
|
+
// This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
|
|
3418
|
+
// with many other commands.
|
|
3419
|
+
new Plugin7({
|
|
3420
|
+
key: new PluginKey5("clearDocument"),
|
|
3421
|
+
appendTransaction: (transactions, oldState, newState) => {
|
|
3422
|
+
if (transactions.some((tr2) => tr2.getMeta("composition"))) {
|
|
3423
|
+
return;
|
|
3424
|
+
}
|
|
3425
|
+
const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
|
|
3426
|
+
const ignoreTr = transactions.some((transaction) => transaction.getMeta("preventClearDocument"));
|
|
3427
|
+
if (!docChanges || ignoreTr) {
|
|
3428
|
+
return;
|
|
3429
|
+
}
|
|
3430
|
+
const { empty, from, to } = oldState.selection;
|
|
3431
|
+
const allFrom = Selection3.atStart(oldState.doc).from;
|
|
3432
|
+
const allEnd = Selection3.atEnd(oldState.doc).to;
|
|
3433
|
+
const allWasSelected = from === allFrom && to === allEnd;
|
|
3434
|
+
if (empty || !allWasSelected) {
|
|
3435
|
+
return;
|
|
3436
|
+
}
|
|
3437
|
+
const isEmpty = isNodeEmpty(newState.doc);
|
|
3438
|
+
if (!isEmpty) {
|
|
3439
|
+
return;
|
|
3440
|
+
}
|
|
3441
|
+
const tr = newState.tr;
|
|
3442
|
+
const state = createChainableState({
|
|
3443
|
+
state: newState,
|
|
3444
|
+
transaction: tr
|
|
3445
|
+
});
|
|
3446
|
+
const { commands } = new CommandManager({
|
|
3447
|
+
editor: this.editor,
|
|
3448
|
+
state
|
|
3449
|
+
});
|
|
3450
|
+
commands.clearNodes();
|
|
3451
|
+
if (!tr.steps.length) {
|
|
3452
|
+
return;
|
|
3453
|
+
}
|
|
3454
|
+
return tr;
|
|
3455
|
+
}
|
|
3456
|
+
})
|
|
3457
|
+
];
|
|
3458
|
+
}
|
|
3459
|
+
});
|
|
3460
|
+
var Paste = Extension.create({
|
|
3461
|
+
name: "paste",
|
|
3462
|
+
addProseMirrorPlugins() {
|
|
3463
|
+
return [
|
|
3464
|
+
new Plugin8({
|
|
3465
|
+
key: new PluginKey6("tiptapPaste"),
|
|
3466
|
+
props: {
|
|
3467
|
+
handlePaste: (_view, e, slice) => {
|
|
3468
|
+
this.editor.emit("paste", {
|
|
3469
|
+
editor: this.editor,
|
|
3470
|
+
event: e,
|
|
3471
|
+
slice
|
|
3472
|
+
});
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
})
|
|
3476
|
+
];
|
|
3477
|
+
}
|
|
3478
|
+
});
|
|
3479
|
+
var Tabindex = Extension.create({
|
|
3480
|
+
name: "tabindex",
|
|
3481
|
+
addProseMirrorPlugins() {
|
|
3482
|
+
return [
|
|
3483
|
+
new Plugin9({
|
|
3484
|
+
key: new PluginKey7("tabindex"),
|
|
3485
|
+
props: {
|
|
3486
|
+
attributes: () => this.editor.isEditable ? { tabindex: "0" } : {}
|
|
3487
|
+
}
|
|
3488
|
+
})
|
|
3489
|
+
];
|
|
3490
|
+
}
|
|
3491
|
+
});
|
|
3492
|
+
var TextDirection = Extension.create({
|
|
3493
|
+
name: "textDirection",
|
|
3494
|
+
addOptions() {
|
|
3495
|
+
return {
|
|
3496
|
+
direction: void 0
|
|
3497
|
+
};
|
|
3498
|
+
},
|
|
3499
|
+
addGlobalAttributes() {
|
|
3500
|
+
if (!this.options.direction) {
|
|
3501
|
+
return [];
|
|
3502
|
+
}
|
|
3503
|
+
const { nodeExtensions } = splitExtensions(this.extensions);
|
|
3504
|
+
return [
|
|
3505
|
+
{
|
|
3506
|
+
types: nodeExtensions.filter((extension) => extension.name !== "text").map((extension) => extension.name),
|
|
3507
|
+
attributes: {
|
|
3508
|
+
dir: {
|
|
3509
|
+
default: this.options.direction,
|
|
3510
|
+
parseHTML: (element) => {
|
|
3511
|
+
const dir = element.getAttribute("dir");
|
|
3512
|
+
if (dir && (dir === "ltr" || dir === "rtl" || dir === "auto")) {
|
|
3513
|
+
return dir;
|
|
3514
|
+
}
|
|
3515
|
+
return this.options.direction;
|
|
3516
|
+
},
|
|
3517
|
+
renderHTML: (attributes) => {
|
|
3518
|
+
if (!attributes.dir) {
|
|
3519
|
+
return {};
|
|
3520
|
+
}
|
|
3521
|
+
return {
|
|
3522
|
+
dir: attributes.dir
|
|
3523
|
+
};
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
];
|
|
3529
|
+
},
|
|
3530
|
+
addProseMirrorPlugins() {
|
|
3531
|
+
return [
|
|
3532
|
+
new Plugin10({
|
|
3533
|
+
key: new PluginKey8("textDirection"),
|
|
3534
|
+
props: {
|
|
3535
|
+
attributes: () => {
|
|
3536
|
+
const direction = this.options.direction;
|
|
3537
|
+
if (!direction) {
|
|
3538
|
+
return {};
|
|
3539
|
+
}
|
|
3540
|
+
return {
|
|
3541
|
+
dir: direction
|
|
3542
|
+
};
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
3545
|
+
})
|
|
3546
|
+
];
|
|
3547
|
+
}
|
|
3548
|
+
});
|
|
3549
|
+
var markdown_exports = {};
|
|
3550
|
+
__export(markdown_exports, {
|
|
3551
|
+
createAtomBlockMarkdownSpec: () => createAtomBlockMarkdownSpec,
|
|
3552
|
+
createBlockMarkdownSpec: () => createBlockMarkdownSpec,
|
|
3553
|
+
createInlineMarkdownSpec: () => createInlineMarkdownSpec,
|
|
3554
|
+
parseAttributes: () => parseAttributes,
|
|
3555
|
+
parseIndentedBlocks: () => parseIndentedBlocks,
|
|
3556
|
+
renderNestedMarkdownContent: () => renderNestedMarkdownContent,
|
|
3557
|
+
serializeAttributes: () => serializeAttributes
|
|
3558
|
+
});
|
|
3559
|
+
function parseAttributes(attrString) {
|
|
3560
|
+
if (!(attrString == null ? void 0 : attrString.trim())) {
|
|
3561
|
+
return {};
|
|
3562
|
+
}
|
|
3563
|
+
const attributes = {};
|
|
3564
|
+
const quotedStrings = [];
|
|
3565
|
+
const tempString = attrString.replace(/["']([^"']*)["']/g, (match) => {
|
|
3566
|
+
quotedStrings.push(match);
|
|
3567
|
+
return `__QUOTED_${quotedStrings.length - 1}__`;
|
|
3568
|
+
});
|
|
3569
|
+
const classMatches = tempString.match(/(?:^|\s)\.([a-zA-Z][\w-]*)/g);
|
|
3570
|
+
if (classMatches) {
|
|
3571
|
+
const classes = classMatches.map((match) => match.trim().slice(1));
|
|
3572
|
+
attributes.class = classes.join(" ");
|
|
3573
|
+
}
|
|
3574
|
+
const idMatch = tempString.match(/(?:^|\s)#([a-zA-Z][\w-]*)/);
|
|
3575
|
+
if (idMatch) {
|
|
3576
|
+
attributes.id = idMatch[1];
|
|
3577
|
+
}
|
|
3578
|
+
const kvRegex = /([a-zA-Z][\w-]*)\s*=\s*(__QUOTED_\d+__)/g;
|
|
3579
|
+
const kvMatches = Array.from(tempString.matchAll(kvRegex));
|
|
3580
|
+
kvMatches.forEach(([, key, quotedRef]) => {
|
|
3581
|
+
var _a;
|
|
3582
|
+
const quotedIndex = parseInt(((_a = quotedRef.match(/__QUOTED_(\d+)__/)) == null ? void 0 : _a[1]) || "0", 10);
|
|
3583
|
+
const quotedValue = quotedStrings[quotedIndex];
|
|
3584
|
+
if (quotedValue) {
|
|
3585
|
+
attributes[key] = quotedValue.slice(1, -1);
|
|
3586
|
+
}
|
|
3587
|
+
});
|
|
3588
|
+
const cleanString = tempString.replace(/(?:^|\s)\.([a-zA-Z][\w-]*)/g, "").replace(/(?:^|\s)#([a-zA-Z][\w-]*)/g, "").replace(/([a-zA-Z][\w-]*)\s*=\s*__QUOTED_\d+__/g, "").trim();
|
|
3589
|
+
if (cleanString) {
|
|
3590
|
+
const booleanAttrs = cleanString.split(/\s+/).filter(Boolean);
|
|
3591
|
+
booleanAttrs.forEach((attr) => {
|
|
3592
|
+
if (attr.match(/^[a-zA-Z][\w-]*$/)) {
|
|
3593
|
+
attributes[attr] = true;
|
|
3594
|
+
}
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
return attributes;
|
|
3598
|
+
}
|
|
3599
|
+
function serializeAttributes(attributes) {
|
|
3600
|
+
if (!attributes || Object.keys(attributes).length === 0) {
|
|
3601
|
+
return "";
|
|
3602
|
+
}
|
|
3603
|
+
const parts = [];
|
|
3604
|
+
if (attributes.class) {
|
|
3605
|
+
const classes = String(attributes.class).split(/\s+/).filter(Boolean);
|
|
3606
|
+
classes.forEach((cls) => parts.push(`.${cls}`));
|
|
3607
|
+
}
|
|
3608
|
+
if (attributes.id) {
|
|
3609
|
+
parts.push(`#${attributes.id}`);
|
|
3610
|
+
}
|
|
3611
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
3612
|
+
if (key === "class" || key === "id") {
|
|
3613
|
+
return;
|
|
3614
|
+
}
|
|
3615
|
+
if (value === true) {
|
|
3616
|
+
parts.push(key);
|
|
3617
|
+
} else if (value !== false && value != null) {
|
|
3618
|
+
parts.push(`${key}="${String(value)}"`);
|
|
3619
|
+
}
|
|
3620
|
+
});
|
|
3621
|
+
return parts.join(" ");
|
|
3622
|
+
}
|
|
3623
|
+
function createAtomBlockMarkdownSpec(options) {
|
|
3624
|
+
const {
|
|
3625
|
+
nodeName,
|
|
3626
|
+
name: markdownName,
|
|
3627
|
+
parseAttributes: parseAttributes2 = parseAttributes,
|
|
3628
|
+
serializeAttributes: serializeAttributes2 = serializeAttributes,
|
|
3629
|
+
defaultAttributes = {},
|
|
3630
|
+
requiredAttributes = [],
|
|
3631
|
+
allowedAttributes
|
|
3632
|
+
} = options;
|
|
3633
|
+
const blockName = markdownName || nodeName;
|
|
3634
|
+
const filterAttributes = (attrs) => {
|
|
3635
|
+
if (!allowedAttributes) {
|
|
3636
|
+
return attrs;
|
|
3637
|
+
}
|
|
3638
|
+
const filtered = {};
|
|
3639
|
+
allowedAttributes.forEach((key) => {
|
|
3640
|
+
if (key in attrs) {
|
|
3641
|
+
filtered[key] = attrs[key];
|
|
3642
|
+
}
|
|
3643
|
+
});
|
|
3644
|
+
return filtered;
|
|
3645
|
+
};
|
|
3646
|
+
return {
|
|
3647
|
+
parseMarkdown: (token, h2) => {
|
|
3648
|
+
const attrs = { ...defaultAttributes, ...token.attributes };
|
|
3649
|
+
return h2.createNode(nodeName, attrs, []);
|
|
3650
|
+
},
|
|
3651
|
+
markdownTokenizer: {
|
|
3652
|
+
name: nodeName,
|
|
3653
|
+
level: "block",
|
|
3654
|
+
start(src) {
|
|
3655
|
+
var _a;
|
|
3656
|
+
const regex = new RegExp(`^:::${blockName}(?:\\s|$)`, "m");
|
|
3657
|
+
const index = (_a = src.match(regex)) == null ? void 0 : _a.index;
|
|
3658
|
+
return index !== void 0 ? index : -1;
|
|
3659
|
+
},
|
|
3660
|
+
tokenize(src, _tokens, _lexer) {
|
|
3661
|
+
const regex = new RegExp(`^:::${blockName}(?:\\s+\\{([^}]*)\\})?\\s*:::(?:\\n|$)`);
|
|
3662
|
+
const match = src.match(regex);
|
|
3663
|
+
if (!match) {
|
|
3664
|
+
return void 0;
|
|
3665
|
+
}
|
|
3666
|
+
const attrString = match[1] || "";
|
|
3667
|
+
const attributes = parseAttributes2(attrString);
|
|
3668
|
+
const missingRequired = requiredAttributes.find((required) => !(required in attributes));
|
|
3669
|
+
if (missingRequired) {
|
|
3670
|
+
return void 0;
|
|
3671
|
+
}
|
|
3672
|
+
return {
|
|
3673
|
+
type: nodeName,
|
|
3674
|
+
raw: match[0],
|
|
3675
|
+
attributes
|
|
3676
|
+
};
|
|
3677
|
+
}
|
|
3678
|
+
},
|
|
3679
|
+
renderMarkdown: (node) => {
|
|
3680
|
+
const filteredAttrs = filterAttributes(node.attrs || {});
|
|
3681
|
+
const attrs = serializeAttributes2(filteredAttrs);
|
|
3682
|
+
const attrString = attrs ? ` {${attrs}}` : "";
|
|
3683
|
+
return `:::${blockName}${attrString} :::`;
|
|
3684
|
+
}
|
|
3685
|
+
};
|
|
3686
|
+
}
|
|
3687
|
+
function createBlockMarkdownSpec(options) {
|
|
3688
|
+
const {
|
|
3689
|
+
nodeName,
|
|
3690
|
+
name: markdownName,
|
|
3691
|
+
getContent,
|
|
3692
|
+
parseAttributes: parseAttributes2 = parseAttributes,
|
|
3693
|
+
serializeAttributes: serializeAttributes2 = serializeAttributes,
|
|
3694
|
+
defaultAttributes = {},
|
|
3695
|
+
content = "block",
|
|
3696
|
+
allowedAttributes
|
|
3697
|
+
} = options;
|
|
3698
|
+
const blockName = markdownName || nodeName;
|
|
3699
|
+
const filterAttributes = (attrs) => {
|
|
3700
|
+
if (!allowedAttributes) {
|
|
3701
|
+
return attrs;
|
|
3702
|
+
}
|
|
3703
|
+
const filtered = {};
|
|
3704
|
+
allowedAttributes.forEach((key) => {
|
|
3705
|
+
if (key in attrs) {
|
|
3706
|
+
filtered[key] = attrs[key];
|
|
3707
|
+
}
|
|
3708
|
+
});
|
|
3709
|
+
return filtered;
|
|
3710
|
+
};
|
|
3711
|
+
return {
|
|
3712
|
+
parseMarkdown: (token, h2) => {
|
|
3713
|
+
let nodeContent;
|
|
3714
|
+
if (getContent) {
|
|
3715
|
+
const contentResult = getContent(token);
|
|
3716
|
+
nodeContent = typeof contentResult === "string" ? [{ type: "text", text: contentResult }] : contentResult;
|
|
3717
|
+
} else if (content === "block") {
|
|
3718
|
+
nodeContent = h2.parseChildren(token.tokens || []);
|
|
3719
|
+
} else {
|
|
3720
|
+
nodeContent = h2.parseInline(token.tokens || []);
|
|
3721
|
+
}
|
|
3722
|
+
const attrs = { ...defaultAttributes, ...token.attributes };
|
|
3723
|
+
return h2.createNode(nodeName, attrs, nodeContent);
|
|
3724
|
+
},
|
|
3725
|
+
markdownTokenizer: {
|
|
3726
|
+
name: nodeName,
|
|
3727
|
+
level: "block",
|
|
3728
|
+
start(src) {
|
|
3729
|
+
var _a;
|
|
3730
|
+
const regex = new RegExp(`^:::${blockName}`, "m");
|
|
3731
|
+
const index = (_a = src.match(regex)) == null ? void 0 : _a.index;
|
|
3732
|
+
return index !== void 0 ? index : -1;
|
|
3733
|
+
},
|
|
3734
|
+
tokenize(src, _tokens, lexer) {
|
|
3735
|
+
var _a;
|
|
3736
|
+
const openingRegex = new RegExp(`^:::${blockName}(?:\\s+\\{([^}]*)\\})?\\s*\\n`);
|
|
3737
|
+
const openingMatch = src.match(openingRegex);
|
|
3738
|
+
if (!openingMatch) {
|
|
3739
|
+
return void 0;
|
|
3740
|
+
}
|
|
3741
|
+
const [openingTag, attrString = ""] = openingMatch;
|
|
3742
|
+
const attributes = parseAttributes2(attrString);
|
|
3743
|
+
let level = 1;
|
|
3744
|
+
const position = openingTag.length;
|
|
3745
|
+
let matchedContent = "";
|
|
3746
|
+
const blockPattern = /^:::([\w-]*)(\s.*)?/gm;
|
|
3747
|
+
const remaining = src.slice(position);
|
|
3748
|
+
blockPattern.lastIndex = 0;
|
|
3749
|
+
for (; ; ) {
|
|
3750
|
+
const match = blockPattern.exec(remaining);
|
|
3751
|
+
if (match === null) {
|
|
3752
|
+
break;
|
|
3753
|
+
}
|
|
3754
|
+
const matchPos = match.index;
|
|
3755
|
+
const blockType = match[1];
|
|
3756
|
+
if ((_a = match[2]) == null ? void 0 : _a.endsWith(":::")) {
|
|
3757
|
+
continue;
|
|
3758
|
+
}
|
|
3759
|
+
if (blockType) {
|
|
3760
|
+
level += 1;
|
|
3761
|
+
} else {
|
|
3762
|
+
level -= 1;
|
|
3763
|
+
if (level === 0) {
|
|
3764
|
+
const rawContent = remaining.slice(0, matchPos);
|
|
3765
|
+
matchedContent = rawContent.trim();
|
|
3766
|
+
const fullMatch = src.slice(0, position + matchPos + match[0].length);
|
|
3767
|
+
let contentTokens = [];
|
|
3768
|
+
if (matchedContent) {
|
|
3769
|
+
if (content === "block") {
|
|
3770
|
+
contentTokens = lexer.blockTokens(rawContent);
|
|
3771
|
+
contentTokens.forEach((token) => {
|
|
3772
|
+
if (token.text && (!token.tokens || token.tokens.length === 0)) {
|
|
3773
|
+
token.tokens = lexer.inlineTokens(token.text);
|
|
3774
|
+
}
|
|
3775
|
+
});
|
|
3776
|
+
while (contentTokens.length > 0) {
|
|
3777
|
+
const lastToken = contentTokens[contentTokens.length - 1];
|
|
3778
|
+
if (lastToken.type === "paragraph" && (!lastToken.text || lastToken.text.trim() === "")) {
|
|
3779
|
+
contentTokens.pop();
|
|
3780
|
+
} else {
|
|
3781
|
+
break;
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
} else {
|
|
3785
|
+
contentTokens = lexer.inlineTokens(matchedContent);
|
|
3786
|
+
}
|
|
3787
|
+
}
|
|
3788
|
+
return {
|
|
3789
|
+
type: nodeName,
|
|
3790
|
+
raw: fullMatch,
|
|
3791
|
+
attributes,
|
|
3792
|
+
content: matchedContent,
|
|
3793
|
+
tokens: contentTokens
|
|
3794
|
+
};
|
|
3795
|
+
}
|
|
3796
|
+
}
|
|
3797
|
+
}
|
|
3798
|
+
return void 0;
|
|
3799
|
+
}
|
|
3800
|
+
},
|
|
3801
|
+
renderMarkdown: (node, h2) => {
|
|
3802
|
+
const filteredAttrs = filterAttributes(node.attrs || {});
|
|
3803
|
+
const attrs = serializeAttributes2(filteredAttrs);
|
|
3804
|
+
const attrString = attrs ? ` {${attrs}}` : "";
|
|
3805
|
+
const renderedContent = h2.renderChildren(node.content || [], "\n\n");
|
|
3806
|
+
return `:::${blockName}${attrString}
|
|
3807
|
+
|
|
3808
|
+
${renderedContent}
|
|
3809
|
+
|
|
3810
|
+
:::`;
|
|
3811
|
+
}
|
|
3812
|
+
};
|
|
3813
|
+
}
|
|
3814
|
+
function parseShortcodeAttributes(attrString) {
|
|
3815
|
+
if (!attrString.trim()) {
|
|
3816
|
+
return {};
|
|
3817
|
+
}
|
|
3818
|
+
const attributes = {};
|
|
3819
|
+
const regex = /(\w+)=(?:"([^"]*)"|'([^']*)')/g;
|
|
3820
|
+
let match = regex.exec(attrString);
|
|
3821
|
+
while (match !== null) {
|
|
3822
|
+
const [, key, doubleQuoted, singleQuoted] = match;
|
|
3823
|
+
attributes[key] = doubleQuoted || singleQuoted;
|
|
3824
|
+
match = regex.exec(attrString);
|
|
3825
|
+
}
|
|
3826
|
+
return attributes;
|
|
3827
|
+
}
|
|
3828
|
+
function serializeShortcodeAttributes(attrs) {
|
|
3829
|
+
return Object.entries(attrs).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}="${value}"`).join(" ");
|
|
3830
|
+
}
|
|
3831
|
+
function createInlineMarkdownSpec(options) {
|
|
3832
|
+
const {
|
|
3833
|
+
nodeName,
|
|
3834
|
+
name: shortcodeName,
|
|
3835
|
+
getContent,
|
|
3836
|
+
parseAttributes: parseAttributes2 = parseShortcodeAttributes,
|
|
3837
|
+
serializeAttributes: serializeAttributes2 = serializeShortcodeAttributes,
|
|
3838
|
+
defaultAttributes = {},
|
|
3839
|
+
selfClosing = false,
|
|
3840
|
+
allowedAttributes
|
|
3841
|
+
} = options;
|
|
3842
|
+
const shortcode = shortcodeName || nodeName;
|
|
3843
|
+
const filterAttributes = (attrs) => {
|
|
3844
|
+
if (!allowedAttributes) {
|
|
3845
|
+
return attrs;
|
|
3846
|
+
}
|
|
3847
|
+
const filtered = {};
|
|
3848
|
+
allowedAttributes.forEach((attr) => {
|
|
3849
|
+
const attrName = typeof attr === "string" ? attr : attr.name;
|
|
3850
|
+
const skipIfDefault = typeof attr === "string" ? void 0 : attr.skipIfDefault;
|
|
3851
|
+
if (attrName in attrs) {
|
|
3852
|
+
const value = attrs[attrName];
|
|
3853
|
+
if (skipIfDefault !== void 0 && value === skipIfDefault) {
|
|
3854
|
+
return;
|
|
3855
|
+
}
|
|
3856
|
+
filtered[attrName] = value;
|
|
3857
|
+
}
|
|
3858
|
+
});
|
|
3859
|
+
return filtered;
|
|
3860
|
+
};
|
|
3861
|
+
const escapedShortcode = shortcode.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3862
|
+
return {
|
|
3863
|
+
parseMarkdown: (token, h2) => {
|
|
3864
|
+
const attrs = { ...defaultAttributes, ...token.attributes };
|
|
3865
|
+
if (selfClosing) {
|
|
3866
|
+
return h2.createNode(nodeName, attrs);
|
|
3867
|
+
}
|
|
3868
|
+
const content = getContent ? getContent(token) : token.content || "";
|
|
3869
|
+
if (content) {
|
|
3870
|
+
return h2.createNode(nodeName, attrs, [h2.createTextNode(content)]);
|
|
3871
|
+
}
|
|
3872
|
+
return h2.createNode(nodeName, attrs, []);
|
|
3873
|
+
},
|
|
3874
|
+
markdownTokenizer: {
|
|
3875
|
+
name: nodeName,
|
|
3876
|
+
level: "inline",
|
|
3877
|
+
start(src) {
|
|
3878
|
+
const startPattern = selfClosing ? new RegExp(`\\[${escapedShortcode}\\s*[^\\]]*\\]`) : new RegExp(`\\[${escapedShortcode}\\s*[^\\]]*\\][\\s\\S]*?\\[\\/${escapedShortcode}\\]`);
|
|
3879
|
+
const match = src.match(startPattern);
|
|
3880
|
+
const index = match == null ? void 0 : match.index;
|
|
3881
|
+
return index !== void 0 ? index : -1;
|
|
3882
|
+
},
|
|
3883
|
+
tokenize(src, _tokens, _lexer) {
|
|
3884
|
+
const tokenPattern = selfClosing ? new RegExp(`^\\[${escapedShortcode}\\s*([^\\]]*)\\]`) : new RegExp(`^\\[${escapedShortcode}\\s*([^\\]]*)\\]([\\s\\S]*?)\\[\\/${escapedShortcode}\\]`);
|
|
3885
|
+
const match = src.match(tokenPattern);
|
|
3886
|
+
if (!match) {
|
|
3887
|
+
return void 0;
|
|
3888
|
+
}
|
|
3889
|
+
let content = "";
|
|
3890
|
+
let attrString = "";
|
|
3891
|
+
if (selfClosing) {
|
|
3892
|
+
const [, attrs] = match;
|
|
3893
|
+
attrString = attrs;
|
|
3894
|
+
} else {
|
|
3895
|
+
const [, attrs, contentMatch] = match;
|
|
3896
|
+
attrString = attrs;
|
|
3897
|
+
content = contentMatch || "";
|
|
3898
|
+
}
|
|
3899
|
+
const attributes = parseAttributes2(attrString.trim());
|
|
3900
|
+
return {
|
|
3901
|
+
type: nodeName,
|
|
3902
|
+
raw: match[0],
|
|
3903
|
+
content: content.trim(),
|
|
3904
|
+
attributes
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
},
|
|
3908
|
+
renderMarkdown: (node) => {
|
|
3909
|
+
let content = "";
|
|
3910
|
+
if (getContent) {
|
|
3911
|
+
content = getContent(node);
|
|
3912
|
+
} else if (node.content && node.content.length > 0) {
|
|
3913
|
+
content = node.content.filter((child) => child.type === "text").map((child) => child.text).join("");
|
|
3914
|
+
}
|
|
3915
|
+
const filteredAttrs = filterAttributes(node.attrs || {});
|
|
3916
|
+
const attrs = serializeAttributes2(filteredAttrs);
|
|
3917
|
+
const attrString = attrs ? ` ${attrs}` : "";
|
|
3918
|
+
if (selfClosing) {
|
|
3919
|
+
return `[${shortcode}${attrString}]`;
|
|
3920
|
+
}
|
|
3921
|
+
return `[${shortcode}${attrString}]${content}[/${shortcode}]`;
|
|
3922
|
+
}
|
|
3923
|
+
};
|
|
3924
|
+
}
|
|
3925
|
+
function parseIndentedBlocks(src, config, lexer) {
|
|
3926
|
+
var _a, _b, _c, _d;
|
|
3927
|
+
const lines = src.split("\n");
|
|
3928
|
+
const items = [];
|
|
3929
|
+
let totalRaw = "";
|
|
3930
|
+
let i = 0;
|
|
3931
|
+
const baseIndentSize = config.baseIndentSize || 2;
|
|
3932
|
+
while (i < lines.length) {
|
|
3933
|
+
const currentLine = lines[i];
|
|
3934
|
+
const itemMatch = currentLine.match(config.itemPattern);
|
|
3935
|
+
if (!itemMatch) {
|
|
3936
|
+
if (items.length > 0) {
|
|
3937
|
+
break;
|
|
3938
|
+
} else if (currentLine.trim() === "") {
|
|
3939
|
+
i += 1;
|
|
3940
|
+
totalRaw = `${totalRaw}${currentLine}
|
|
3941
|
+
`;
|
|
3942
|
+
continue;
|
|
3943
|
+
} else {
|
|
3944
|
+
return void 0;
|
|
3945
|
+
}
|
|
3946
|
+
}
|
|
3947
|
+
const itemData = config.extractItemData(itemMatch);
|
|
3948
|
+
const { indentLevel, mainContent } = itemData;
|
|
3949
|
+
totalRaw = `${totalRaw}${currentLine}
|
|
3950
|
+
`;
|
|
3951
|
+
const itemContent = [mainContent];
|
|
3952
|
+
i += 1;
|
|
3953
|
+
while (i < lines.length) {
|
|
3954
|
+
const nextLine = lines[i];
|
|
3955
|
+
if (nextLine.trim() === "") {
|
|
3956
|
+
const nextNonEmptyIndex = lines.slice(i + 1).findIndex((l) => l.trim() !== "");
|
|
3957
|
+
if (nextNonEmptyIndex === -1) {
|
|
3958
|
+
break;
|
|
3959
|
+
}
|
|
3960
|
+
const nextNonEmpty = lines[i + 1 + nextNonEmptyIndex];
|
|
3961
|
+
const nextIndent2 = ((_b = (_a = nextNonEmpty.match(/^(\s*)/)) == null ? void 0 : _a[1]) == null ? void 0 : _b.length) || 0;
|
|
3962
|
+
if (nextIndent2 > indentLevel) {
|
|
3963
|
+
itemContent.push(nextLine);
|
|
3964
|
+
totalRaw = `${totalRaw}${nextLine}
|
|
3965
|
+
`;
|
|
3966
|
+
i += 1;
|
|
3967
|
+
continue;
|
|
3968
|
+
} else {
|
|
3969
|
+
break;
|
|
3970
|
+
}
|
|
3971
|
+
}
|
|
3972
|
+
const nextIndent = ((_d = (_c = nextLine.match(/^(\s*)/)) == null ? void 0 : _c[1]) == null ? void 0 : _d.length) || 0;
|
|
3973
|
+
if (nextIndent > indentLevel) {
|
|
3974
|
+
itemContent.push(nextLine);
|
|
3975
|
+
totalRaw = `${totalRaw}${nextLine}
|
|
3976
|
+
`;
|
|
3977
|
+
i += 1;
|
|
3978
|
+
} else {
|
|
3979
|
+
break;
|
|
3980
|
+
}
|
|
3981
|
+
}
|
|
3982
|
+
let nestedTokens;
|
|
3983
|
+
const nestedContent = itemContent.slice(1);
|
|
3984
|
+
if (nestedContent.length > 0) {
|
|
3985
|
+
const dedentedNested = nestedContent.map((nestedLine) => nestedLine.slice(indentLevel + baseIndentSize)).join("\n");
|
|
3986
|
+
if (dedentedNested.trim()) {
|
|
3987
|
+
if (config.customNestedParser) {
|
|
3988
|
+
nestedTokens = config.customNestedParser(dedentedNested);
|
|
3989
|
+
} else {
|
|
3990
|
+
nestedTokens = lexer.blockTokens(dedentedNested);
|
|
3991
|
+
}
|
|
3992
|
+
}
|
|
3993
|
+
}
|
|
3994
|
+
const token = config.createToken(itemData, nestedTokens);
|
|
3995
|
+
items.push(token);
|
|
3996
|
+
}
|
|
3997
|
+
if (items.length === 0) {
|
|
3998
|
+
return void 0;
|
|
3999
|
+
}
|
|
4000
|
+
return {
|
|
4001
|
+
items,
|
|
4002
|
+
raw: totalRaw
|
|
4003
|
+
};
|
|
4004
|
+
}
|
|
4005
|
+
function renderNestedMarkdownContent(node, h2, prefixOrGenerator, ctx) {
|
|
4006
|
+
if (!node || !Array.isArray(node.content)) {
|
|
4007
|
+
return "";
|
|
4008
|
+
}
|
|
4009
|
+
const prefix = typeof prefixOrGenerator === "function" ? prefixOrGenerator(ctx) : prefixOrGenerator;
|
|
4010
|
+
const [content, ...children] = node.content;
|
|
4011
|
+
const mainContent = h2.renderChildren([content]);
|
|
4012
|
+
const output = [`${prefix}${mainContent}`];
|
|
4013
|
+
if (children && children.length > 0) {
|
|
4014
|
+
children.forEach((child) => {
|
|
4015
|
+
const childContent = h2.renderChildren([child]);
|
|
4016
|
+
if (childContent) {
|
|
4017
|
+
const indentedChild = childContent.split("\n").map((line) => line ? h2.indent(line) : "").join("\n");
|
|
4018
|
+
output.push(indentedChild);
|
|
4019
|
+
}
|
|
4020
|
+
});
|
|
4021
|
+
}
|
|
4022
|
+
return output.join("\n");
|
|
4023
|
+
}
|
|
4024
|
+
function updateMarkViewAttributes(checkMark, editor, attrs = {}) {
|
|
4025
|
+
const { state } = editor;
|
|
4026
|
+
const { doc, tr } = state;
|
|
4027
|
+
const thisMark = checkMark;
|
|
4028
|
+
doc.descendants((node, pos) => {
|
|
4029
|
+
const from = tr.mapping.map(pos);
|
|
4030
|
+
const to = tr.mapping.map(pos) + node.nodeSize;
|
|
4031
|
+
let foundMark = null;
|
|
4032
|
+
node.marks.forEach((mark) => {
|
|
4033
|
+
if (mark !== thisMark) {
|
|
4034
|
+
return false;
|
|
4035
|
+
}
|
|
4036
|
+
foundMark = mark;
|
|
4037
|
+
});
|
|
4038
|
+
if (!foundMark) {
|
|
4039
|
+
return;
|
|
4040
|
+
}
|
|
4041
|
+
let needsUpdate = false;
|
|
4042
|
+
Object.keys(attrs).forEach((k) => {
|
|
4043
|
+
if (attrs[k] !== foundMark.attrs[k]) {
|
|
4044
|
+
needsUpdate = true;
|
|
4045
|
+
}
|
|
4046
|
+
});
|
|
4047
|
+
if (needsUpdate) {
|
|
4048
|
+
const updatedMark = checkMark.type.create({
|
|
4049
|
+
...checkMark.attrs,
|
|
4050
|
+
...attrs
|
|
4051
|
+
});
|
|
4052
|
+
tr.removeMark(from, to, checkMark.type);
|
|
4053
|
+
tr.addMark(from, to, updatedMark);
|
|
4054
|
+
}
|
|
4055
|
+
});
|
|
4056
|
+
if (tr.docChanged) {
|
|
4057
|
+
editor.view.dispatch(tr);
|
|
4058
|
+
}
|
|
4059
|
+
}
|
|
4060
|
+
var Node3 = class _Node extends Extendable {
|
|
4061
|
+
constructor() {
|
|
4062
|
+
super(...arguments);
|
|
4063
|
+
this.type = "node";
|
|
4064
|
+
}
|
|
4065
|
+
/**
|
|
4066
|
+
* Create a new Node instance
|
|
4067
|
+
* @param config - Node configuration object or a function that returns a configuration object
|
|
4068
|
+
*/
|
|
4069
|
+
static create(config = {}) {
|
|
4070
|
+
const resolvedConfig = typeof config === "function" ? config() : config;
|
|
4071
|
+
return new _Node(resolvedConfig);
|
|
4072
|
+
}
|
|
4073
|
+
configure(options) {
|
|
4074
|
+
return super.configure(options);
|
|
4075
|
+
}
|
|
4076
|
+
extend(extendedConfig) {
|
|
4077
|
+
const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
|
|
4078
|
+
return super.extend(resolvedConfig);
|
|
4079
|
+
}
|
|
4080
|
+
};
|
|
4081
|
+
|
|
4082
|
+
// src/extensions/callout.ts
|
|
4083
|
+
var CALLOUT_ARIA_LABELS = {
|
|
4084
|
+
success: "\u6210\u529F\u63D0\u793A",
|
|
4085
|
+
warning: "\u8B66\u544A\u63D0\u793A",
|
|
4086
|
+
error: "\u932F\u8AA4\u63D0\u793A",
|
|
4087
|
+
info: "\u8CC7\u8A0A\u63D0\u793A"
|
|
4088
|
+
};
|
|
4089
|
+
var Callout = Node3.create({
|
|
4090
|
+
name: "callout",
|
|
4091
|
+
group: "block",
|
|
4092
|
+
content: "block+",
|
|
4093
|
+
defining: true,
|
|
4094
|
+
addOptions() {
|
|
4095
|
+
return {
|
|
4096
|
+
HTMLAttributes: {}
|
|
4097
|
+
};
|
|
4098
|
+
},
|
|
4099
|
+
addAttributes() {
|
|
4100
|
+
return {
|
|
4101
|
+
type: {
|
|
4102
|
+
default: "info",
|
|
4103
|
+
parseHTML: (element) => element.getAttribute("data-type"),
|
|
4104
|
+
renderHTML: (attributes) => ({
|
|
4105
|
+
"data-type": attributes.type
|
|
4106
|
+
})
|
|
4107
|
+
}
|
|
4108
|
+
};
|
|
4109
|
+
},
|
|
4110
|
+
parseHTML() {
|
|
4111
|
+
return [
|
|
4112
|
+
{
|
|
4113
|
+
tag: "div[data-callout]"
|
|
4114
|
+
}
|
|
4115
|
+
];
|
|
4116
|
+
},
|
|
4117
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
4118
|
+
const type = node.attrs.type || "info";
|
|
4119
|
+
return [
|
|
4120
|
+
"div",
|
|
4121
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
4122
|
+
"data-callout": "",
|
|
4123
|
+
"class": `callout callout-${type}`,
|
|
4124
|
+
"role": "note",
|
|
4125
|
+
"aria-label": CALLOUT_ARIA_LABELS[type]
|
|
4126
|
+
}),
|
|
4127
|
+
0
|
|
4128
|
+
];
|
|
4129
|
+
},
|
|
4130
|
+
addCommands() {
|
|
4131
|
+
return {
|
|
4132
|
+
setCallout: (attributes) => ({ commands }) => {
|
|
4133
|
+
return commands.wrapIn(this.name, attributes);
|
|
4134
|
+
},
|
|
4135
|
+
toggleCallout: (attributes) => ({ commands }) => {
|
|
4136
|
+
return commands.toggleWrap(this.name, attributes);
|
|
4137
|
+
},
|
|
4138
|
+
unsetCallout: () => ({ commands }) => {
|
|
4139
|
+
return commands.lift(this.name);
|
|
4140
|
+
}
|
|
4141
|
+
};
|
|
4142
|
+
}
|
|
4143
|
+
});
|
|
4144
|
+
|
|
4145
|
+
// src/components/Toolbar.tsx
|
|
4146
|
+
import { useRef } from "react";
|
|
4147
|
+
|
|
4148
|
+
// src/ui/button.tsx
|
|
4149
|
+
import * as React from "react";
|
|
4150
|
+
|
|
4151
|
+
// src/lib/utils.ts
|
|
4152
|
+
import { clsx } from "clsx";
|
|
4153
|
+
import { twMerge } from "tailwind-merge";
|
|
4154
|
+
function cn(...inputs) {
|
|
4155
|
+
return twMerge(clsx(inputs));
|
|
4156
|
+
}
|
|
4157
|
+
|
|
4158
|
+
// src/ui/button.tsx
|
|
4159
|
+
import { jsx } from "react/jsx-runtime";
|
|
4160
|
+
var Button = React.forwardRef(
|
|
4161
|
+
({ className, variant = "default", size = "default", ...props }, ref) => {
|
|
4162
|
+
const variantStyles = {
|
|
4163
|
+
default: "bg-[var(--background)] text-[var(--foreground)] hover:bg-[var(--accent)]",
|
|
4164
|
+
ghost: "hover:bg-[var(--accent)] hover:text-[var(--accent-foreground)]",
|
|
4165
|
+
outline: "border border-[var(--border)] bg-transparent hover:bg-[var(--accent)]"
|
|
4166
|
+
};
|
|
4167
|
+
const sizeStyles = {
|
|
4168
|
+
default: "h-9 px-4 py-2",
|
|
4169
|
+
sm: "h-8 px-3 text-xs",
|
|
4170
|
+
lg: "h-10 px-8"
|
|
4171
|
+
};
|
|
4172
|
+
return /* @__PURE__ */ jsx(
|
|
4173
|
+
"button",
|
|
4174
|
+
{
|
|
4175
|
+
className: cn(
|
|
4176
|
+
"inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors",
|
|
4177
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring)]",
|
|
4178
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
4179
|
+
variantStyles[variant],
|
|
4180
|
+
sizeStyles[size],
|
|
4181
|
+
className
|
|
4182
|
+
),
|
|
4183
|
+
ref,
|
|
4184
|
+
...props
|
|
4185
|
+
}
|
|
4186
|
+
);
|
|
4187
|
+
}
|
|
4188
|
+
);
|
|
4189
|
+
Button.displayName = "Button";
|
|
4190
|
+
|
|
4191
|
+
// src/ui/toggle.tsx
|
|
4192
|
+
import * as React2 from "react";
|
|
4193
|
+
import * as TogglePrimitive from "@radix-ui/react-toggle";
|
|
4194
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
4195
|
+
var Toggle = React2.forwardRef(
|
|
4196
|
+
({ className, size = "default", ...props }, ref) => {
|
|
4197
|
+
const sizeStyles = {
|
|
4198
|
+
default: "h-9 px-3",
|
|
4199
|
+
sm: "h-8 px-2"
|
|
4200
|
+
};
|
|
4201
|
+
return /* @__PURE__ */ jsx2(
|
|
4202
|
+
TogglePrimitive.Root,
|
|
4203
|
+
{
|
|
4204
|
+
ref,
|
|
4205
|
+
className: cn(
|
|
4206
|
+
"inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors",
|
|
4207
|
+
"hover:bg-[var(--accent)] hover:text-[var(--accent-foreground)]",
|
|
4208
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring)]",
|
|
4209
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
4210
|
+
"data-[state=on]:bg-[var(--accent)] data-[state=on]:text-[var(--accent-foreground)]",
|
|
4211
|
+
sizeStyles[size],
|
|
4212
|
+
className
|
|
4213
|
+
),
|
|
4214
|
+
...props
|
|
4215
|
+
}
|
|
4216
|
+
);
|
|
4217
|
+
}
|
|
4218
|
+
);
|
|
4219
|
+
Toggle.displayName = TogglePrimitive.Root.displayName;
|
|
4220
|
+
|
|
4221
|
+
// src/components/ToolbarButton.tsx
|
|
4222
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
4223
|
+
function ToolbarButton({
|
|
4224
|
+
pressed,
|
|
4225
|
+
onPressedChange,
|
|
4226
|
+
disabled,
|
|
4227
|
+
children,
|
|
4228
|
+
title
|
|
4229
|
+
}) {
|
|
4230
|
+
return /* @__PURE__ */ jsx3(
|
|
4231
|
+
Toggle,
|
|
4232
|
+
{
|
|
4233
|
+
size: "sm",
|
|
4234
|
+
pressed,
|
|
4235
|
+
onPressedChange,
|
|
4236
|
+
disabled,
|
|
4237
|
+
title,
|
|
4238
|
+
className: "h-9 w-9 p-0 border-0 bg-transparent rounded-lg hover:bg-muted data-[state=on]:bg-zinc-900 data-[state=on]:text-white dark:data-[state=on]:bg-zinc-100 dark:data-[state=on]:text-zinc-900 disabled:opacity-30",
|
|
4239
|
+
children
|
|
4240
|
+
}
|
|
4241
|
+
);
|
|
4242
|
+
}
|
|
4243
|
+
|
|
4244
|
+
// src/components/ToolbarDivider.tsx
|
|
4245
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
4246
|
+
function ToolbarDivider() {
|
|
4247
|
+
return /* @__PURE__ */ jsx4("div", { className: "w-px h-5 bg-border mx-1.5" });
|
|
4248
|
+
}
|
|
4249
|
+
|
|
4250
|
+
// src/ui/dropdown-menu.tsx
|
|
4251
|
+
import * as React3 from "react";
|
|
4252
|
+
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
|
|
4253
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
4254
|
+
var DropdownMenu = DropdownMenuPrimitive.Root;
|
|
4255
|
+
var DropdownMenuTrigger = DropdownMenuPrimitive.Trigger;
|
|
4256
|
+
var DropdownMenuContent = React3.forwardRef(({ className, sideOffset = 4, ...props }, ref) => /* @__PURE__ */ jsx5(DropdownMenuPrimitive.Portal, { children: /* @__PURE__ */ jsx5(
|
|
4257
|
+
DropdownMenuPrimitive.Content,
|
|
4258
|
+
{
|
|
4259
|
+
ref,
|
|
4260
|
+
sideOffset,
|
|
4261
|
+
className: cn(
|
|
4262
|
+
"z-50 min-w-[8rem] overflow-hidden rounded-md border border-[var(--border)] bg-[var(--popover)] p-1 text-[var(--popover-foreground)] shadow-md",
|
|
4263
|
+
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
|
|
4264
|
+
"data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
|
|
4265
|
+
className
|
|
4266
|
+
),
|
|
4267
|
+
...props
|
|
4268
|
+
}
|
|
4269
|
+
) }));
|
|
4270
|
+
DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName;
|
|
4271
|
+
var DropdownMenuItem = React3.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx5(
|
|
4272
|
+
DropdownMenuPrimitive.Item,
|
|
4273
|
+
{
|
|
4274
|
+
ref,
|
|
4275
|
+
className: cn(
|
|
4276
|
+
"relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors",
|
|
4277
|
+
"focus:bg-[var(--accent)] focus:text-[var(--accent-foreground)]",
|
|
4278
|
+
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
4279
|
+
className
|
|
4280
|
+
),
|
|
4281
|
+
...props
|
|
4282
|
+
}
|
|
4283
|
+
));
|
|
4284
|
+
DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName;
|
|
4285
|
+
var DropdownMenuSeparator = React3.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx5(
|
|
4286
|
+
DropdownMenuPrimitive.Separator,
|
|
4287
|
+
{
|
|
4288
|
+
ref,
|
|
4289
|
+
className: cn("-mx-1 my-1 h-px bg-[var(--border)]", className),
|
|
4290
|
+
...props
|
|
4291
|
+
}
|
|
4292
|
+
));
|
|
4293
|
+
DropdownMenuSeparator.displayName = DropdownMenuPrimitive.Separator.displayName;
|
|
4294
|
+
|
|
4295
|
+
// src/components/HeadingDropdown.tsx
|
|
4296
|
+
import { ChevronDown, Heading1, Heading2, Heading3 } from "lucide-react";
|
|
4297
|
+
import { jsx as jsx6, jsxs } from "react/jsx-runtime";
|
|
4298
|
+
function HeadingDropdown({ editor, iconSize = "h-4 w-4" }) {
|
|
4299
|
+
return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
|
|
4300
|
+
/* @__PURE__ */ jsx6(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button, { variant: "ghost", size: "sm", className: "h-8 px-2 gap-1 font-normal", children: [
|
|
4301
|
+
/* @__PURE__ */ jsx6("span", { className: "text-sm font-semibold", children: "H" }),
|
|
4302
|
+
/* @__PURE__ */ jsx6(ChevronDown, { className: "h-3 w-3" })
|
|
4303
|
+
] }) }),
|
|
4304
|
+
/* @__PURE__ */ jsxs(DropdownMenuContent, { align: "start", children: [
|
|
4305
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleHeading({ level: 1 }).run(), children: [
|
|
4306
|
+
/* @__PURE__ */ jsx6(Heading1, { className: cn(iconSize, "mr-2") }),
|
|
4307
|
+
"\u6A19\u984C 1"
|
|
4308
|
+
] }),
|
|
4309
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleHeading({ level: 2 }).run(), children: [
|
|
4310
|
+
/* @__PURE__ */ jsx6(Heading2, { className: cn(iconSize, "mr-2") }),
|
|
4311
|
+
"\u6A19\u984C 2"
|
|
4312
|
+
] }),
|
|
4313
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleHeading({ level: 3 }).run(), children: [
|
|
4314
|
+
/* @__PURE__ */ jsx6(Heading3, { className: cn(iconSize, "mr-2") }),
|
|
4315
|
+
"\u6A19\u984C 3"
|
|
4316
|
+
] }),
|
|
4317
|
+
/* @__PURE__ */ jsx6(DropdownMenuItem, { onClick: () => editor.chain().focus().setParagraph().run(), children: "\u5167\u6587" })
|
|
4318
|
+
] })
|
|
4319
|
+
] });
|
|
4320
|
+
}
|
|
4321
|
+
|
|
4322
|
+
// src/components/ListDropdown.tsx
|
|
4323
|
+
import { ChevronDown as ChevronDown2, List, ListOrdered, ListTodo } from "lucide-react";
|
|
4324
|
+
import { jsx as jsx7, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
4325
|
+
function ListDropdown({ editor, iconSize = "h-4 w-4" }) {
|
|
4326
|
+
return /* @__PURE__ */ jsxs2(DropdownMenu, { children: [
|
|
4327
|
+
/* @__PURE__ */ jsx7(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs2(Button, { variant: "ghost", size: "sm", className: "h-8 px-2 gap-1", children: [
|
|
4328
|
+
/* @__PURE__ */ jsx7(List, { className: iconSize }),
|
|
4329
|
+
/* @__PURE__ */ jsx7(ChevronDown2, { className: "h-3 w-3" })
|
|
4330
|
+
] }) }),
|
|
4331
|
+
/* @__PURE__ */ jsxs2(DropdownMenuContent, { align: "start", children: [
|
|
4332
|
+
/* @__PURE__ */ jsxs2(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleBulletList().run(), children: [
|
|
4333
|
+
/* @__PURE__ */ jsx7(List, { className: cn(iconSize, "mr-2") }),
|
|
4334
|
+
"\u9805\u76EE\u6E05\u55AE"
|
|
4335
|
+
] }),
|
|
4336
|
+
/* @__PURE__ */ jsxs2(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleOrderedList().run(), children: [
|
|
4337
|
+
/* @__PURE__ */ jsx7(ListOrdered, { className: cn(iconSize, "mr-2") }),
|
|
4338
|
+
"\u7DE8\u865F\u6E05\u55AE"
|
|
4339
|
+
] }),
|
|
4340
|
+
/* @__PURE__ */ jsxs2(DropdownMenuItem, { onClick: () => editor.chain().focus().toggleTaskList().run(), children: [
|
|
4341
|
+
/* @__PURE__ */ jsx7(ListTodo, { className: cn(iconSize, "mr-2") }),
|
|
4342
|
+
"\u4EFB\u52D9\u6E05\u55AE"
|
|
4343
|
+
] })
|
|
4344
|
+
] })
|
|
4345
|
+
] });
|
|
4346
|
+
}
|
|
4347
|
+
|
|
4348
|
+
// src/components/TableDropdown.tsx
|
|
4349
|
+
import { ChevronDown as ChevronDown3, Plus, Trash2, Table as TableIcon } from "lucide-react";
|
|
4350
|
+
import { jsx as jsx8, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
4351
|
+
function TableDropdown({ editor, iconSize = "h-4 w-4" }) {
|
|
4352
|
+
return /* @__PURE__ */ jsxs3(DropdownMenu, { children: [
|
|
4353
|
+
/* @__PURE__ */ jsx8(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs3(Button, { variant: "ghost", size: "sm", className: "h-8 px-2 gap-1", children: [
|
|
4354
|
+
/* @__PURE__ */ jsx8(TableIcon, { className: iconSize }),
|
|
4355
|
+
/* @__PURE__ */ jsx8(ChevronDown3, { className: "h-3 w-3" })
|
|
4356
|
+
] }) }),
|
|
4357
|
+
/* @__PURE__ */ jsxs3(DropdownMenuContent, { align: "start", children: [
|
|
4358
|
+
/* @__PURE__ */ jsxs3(
|
|
4359
|
+
DropdownMenuItem,
|
|
4360
|
+
{
|
|
4361
|
+
onClick: () => editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
|
|
4362
|
+
children: [
|
|
4363
|
+
/* @__PURE__ */ jsx8(Plus, { className: cn(iconSize, "mr-2") }),
|
|
4364
|
+
"\u63D2\u5165\u8868\u683C (3x3)"
|
|
4365
|
+
]
|
|
4366
|
+
}
|
|
4367
|
+
),
|
|
4368
|
+
/* @__PURE__ */ jsx8(DropdownMenuSeparator, {}),
|
|
4369
|
+
/* @__PURE__ */ jsx8(
|
|
4370
|
+
DropdownMenuItem,
|
|
4371
|
+
{
|
|
4372
|
+
onClick: () => editor.chain().focus().addColumnAfter().run(),
|
|
4373
|
+
disabled: !editor.can().addColumnAfter(),
|
|
4374
|
+
children: "\u65B0\u589E\u6B04"
|
|
4375
|
+
}
|
|
4376
|
+
),
|
|
4377
|
+
/* @__PURE__ */ jsx8(
|
|
4378
|
+
DropdownMenuItem,
|
|
4379
|
+
{
|
|
4380
|
+
onClick: () => editor.chain().focus().addRowAfter().run(),
|
|
4381
|
+
disabled: !editor.can().addRowAfter(),
|
|
4382
|
+
children: "\u65B0\u589E\u5217"
|
|
4383
|
+
}
|
|
4384
|
+
),
|
|
4385
|
+
/* @__PURE__ */ jsx8(DropdownMenuSeparator, {}),
|
|
4386
|
+
/* @__PURE__ */ jsx8(
|
|
4387
|
+
DropdownMenuItem,
|
|
4388
|
+
{
|
|
4389
|
+
onClick: () => editor.chain().focus().deleteColumn().run(),
|
|
4390
|
+
disabled: !editor.can().deleteColumn(),
|
|
4391
|
+
children: "\u522A\u9664\u6B04"
|
|
4392
|
+
}
|
|
4393
|
+
),
|
|
4394
|
+
/* @__PURE__ */ jsx8(
|
|
4395
|
+
DropdownMenuItem,
|
|
4396
|
+
{
|
|
4397
|
+
onClick: () => editor.chain().focus().deleteRow().run(),
|
|
4398
|
+
disabled: !editor.can().deleteRow(),
|
|
4399
|
+
children: "\u522A\u9664\u5217"
|
|
4400
|
+
}
|
|
4401
|
+
),
|
|
4402
|
+
/* @__PURE__ */ jsx8(DropdownMenuSeparator, {}),
|
|
4403
|
+
/* @__PURE__ */ jsxs3(
|
|
4404
|
+
DropdownMenuItem,
|
|
4405
|
+
{
|
|
4406
|
+
onClick: () => editor.chain().focus().deleteTable().run(),
|
|
4407
|
+
disabled: !editor.can().deleteTable(),
|
|
4408
|
+
className: "text-destructive",
|
|
4409
|
+
children: [
|
|
4410
|
+
/* @__PURE__ */ jsx8(Trash2, { className: cn(iconSize, "mr-2") }),
|
|
4411
|
+
"\u522A\u9664\u8868\u683C"
|
|
4412
|
+
]
|
|
4413
|
+
}
|
|
4414
|
+
)
|
|
4415
|
+
] })
|
|
4416
|
+
] });
|
|
4417
|
+
}
|
|
4418
|
+
|
|
4419
|
+
// src/components/CalloutDropdown.tsx
|
|
4420
|
+
import { useState } from "react";
|
|
4421
|
+
import { ChevronDown as ChevronDown4, MessageSquare } from "lucide-react";
|
|
4422
|
+
import { jsx as jsx9, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
4423
|
+
function CalloutDropdown({ editor, iconSize = "h-4 w-4" }) {
|
|
4424
|
+
const [open, setOpen] = useState(false);
|
|
4425
|
+
return /* @__PURE__ */ jsxs4(DropdownMenu, { open, onOpenChange: setOpen, modal: false, children: [
|
|
4426
|
+
/* @__PURE__ */ jsx9(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs4(Button, { variant: "ghost", size: "sm", className: "h-8 px-2 gap-1", children: [
|
|
4427
|
+
/* @__PURE__ */ jsx9(MessageSquare, { className: iconSize }),
|
|
4428
|
+
/* @__PURE__ */ jsx9(ChevronDown4, { className: "h-3 w-3" })
|
|
4429
|
+
] }) }),
|
|
4430
|
+
/* @__PURE__ */ jsx9(DropdownMenuContent, { align: "start", className: "min-w-0 p-1.5", children: /* @__PURE__ */ jsxs4("div", { className: "flex gap-1", children: [
|
|
4431
|
+
/* @__PURE__ */ jsx9(
|
|
4432
|
+
"button",
|
|
4433
|
+
{
|
|
4434
|
+
type: "button",
|
|
4435
|
+
className: "w-6 h-6 rounded bg-emerald-100 border border-emerald-300 hover:scale-110 transition-transform",
|
|
4436
|
+
onClick: () => {
|
|
4437
|
+
setOpen(false);
|
|
4438
|
+
setTimeout(() => {
|
|
4439
|
+
editor.chain().focus().setCallout({ type: "success" }).run();
|
|
4440
|
+
}, 0);
|
|
4441
|
+
},
|
|
4442
|
+
title: "\u6210\u529F\u63D0\u793A",
|
|
4443
|
+
"aria-label": "\u6210\u529F\u63D0\u793A"
|
|
4444
|
+
}
|
|
4445
|
+
),
|
|
4446
|
+
/* @__PURE__ */ jsx9(
|
|
4447
|
+
"button",
|
|
4448
|
+
{
|
|
4449
|
+
type: "button",
|
|
4450
|
+
className: "w-6 h-6 rounded bg-amber-100 border border-amber-300 hover:scale-110 transition-transform",
|
|
4451
|
+
onClick: () => {
|
|
4452
|
+
setOpen(false);
|
|
4453
|
+
setTimeout(() => {
|
|
4454
|
+
editor.chain().focus().setCallout({ type: "warning" }).run();
|
|
4455
|
+
}, 0);
|
|
4456
|
+
},
|
|
4457
|
+
title: "\u8B66\u544A\u63D0\u793A",
|
|
4458
|
+
"aria-label": "\u8B66\u544A\u63D0\u793A"
|
|
4459
|
+
}
|
|
4460
|
+
),
|
|
4461
|
+
/* @__PURE__ */ jsx9(
|
|
4462
|
+
"button",
|
|
4463
|
+
{
|
|
4464
|
+
type: "button",
|
|
4465
|
+
className: "w-6 h-6 rounded bg-red-100 border border-red-300 hover:scale-110 transition-transform",
|
|
4466
|
+
onClick: () => {
|
|
4467
|
+
setOpen(false);
|
|
4468
|
+
setTimeout(() => {
|
|
4469
|
+
editor.chain().focus().setCallout({ type: "error" }).run();
|
|
4470
|
+
}, 0);
|
|
4471
|
+
},
|
|
4472
|
+
title: "\u932F\u8AA4\u63D0\u793A",
|
|
4473
|
+
"aria-label": "\u932F\u8AA4\u63D0\u793A"
|
|
4474
|
+
}
|
|
4475
|
+
),
|
|
4476
|
+
/* @__PURE__ */ jsx9(
|
|
4477
|
+
"button",
|
|
4478
|
+
{
|
|
4479
|
+
type: "button",
|
|
4480
|
+
className: "w-6 h-6 rounded bg-sky-100 border border-sky-300 hover:scale-110 transition-transform",
|
|
4481
|
+
onClick: () => {
|
|
4482
|
+
setOpen(false);
|
|
4483
|
+
setTimeout(() => {
|
|
4484
|
+
editor.chain().focus().setCallout({ type: "info" }).run();
|
|
4485
|
+
}, 0);
|
|
4486
|
+
},
|
|
4487
|
+
title: "\u8CC7\u8A0A\u63D0\u793A",
|
|
4488
|
+
"aria-label": "\u8CC7\u8A0A\u63D0\u793A"
|
|
4489
|
+
}
|
|
4490
|
+
)
|
|
4491
|
+
] }) })
|
|
4492
|
+
] });
|
|
4493
|
+
}
|
|
4494
|
+
|
|
4495
|
+
// src/components/ColorPicker.tsx
|
|
4496
|
+
import { Palette, X } from "lucide-react";
|
|
4497
|
+
import { jsx as jsx10, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
4498
|
+
var COLOR_PALETTE = [
|
|
4499
|
+
{ name: "\u9ED1\u8272", value: "#000000" },
|
|
4500
|
+
{ name: "\u7070\u8272", value: "#6B7280" },
|
|
4501
|
+
{ name: "\u7D05\u8272", value: "#EF4444" },
|
|
4502
|
+
{ name: "\u6A59\u8272", value: "#F97316" },
|
|
4503
|
+
{ name: "\u9EC3\u8272", value: "#EAB308" },
|
|
4504
|
+
{ name: "\u7DA0\u8272", value: "#22C55E" },
|
|
4505
|
+
{ name: "\u85CD\u8272", value: "#3B82F6" },
|
|
4506
|
+
{ name: "\u7D2B\u8272", value: "#8B5CF6" }
|
|
4507
|
+
];
|
|
4508
|
+
function ColorPicker({ editor, iconSize = "h-4 w-4" }) {
|
|
4509
|
+
return /* @__PURE__ */ jsxs5(DropdownMenu, { children: [
|
|
4510
|
+
/* @__PURE__ */ jsx10(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx10(Button, { variant: "ghost", size: "sm", className: "h-8 px-2 gap-1", title: "\u984F\u8272", children: /* @__PURE__ */ jsx10(Palette, { className: iconSize }) }) }),
|
|
4511
|
+
/* @__PURE__ */ jsx10(DropdownMenuContent, { align: "start", className: "w-72", children: /* @__PURE__ */ jsxs5("div", { className: "p-2", children: [
|
|
4512
|
+
/* @__PURE__ */ jsx10("div", { className: "text-xs font-medium text-muted-foreground mb-2", children: "\u6587\u5B57\u984F\u8272" }),
|
|
4513
|
+
/* @__PURE__ */ jsxs5("div", { className: "flex gap-1.5 mb-3", children: [
|
|
4514
|
+
COLOR_PALETTE.map((color) => /* @__PURE__ */ jsx10(
|
|
4515
|
+
"button",
|
|
4516
|
+
{
|
|
4517
|
+
type: "button",
|
|
4518
|
+
className: "w-6 h-6 rounded border border-border dark:border-zinc-600 hover:scale-110 transition-transform",
|
|
4519
|
+
style: { backgroundColor: color.value },
|
|
4520
|
+
onClick: () => editor.chain().focus().setColor(color.value).run(),
|
|
4521
|
+
title: color.name,
|
|
4522
|
+
"aria-label": `\u6587\u5B57\u984F\u8272\uFF1A${color.name}`
|
|
4523
|
+
},
|
|
4524
|
+
`text-${color.value}`
|
|
4525
|
+
)),
|
|
4526
|
+
/* @__PURE__ */ jsx10(
|
|
4527
|
+
"button",
|
|
4528
|
+
{
|
|
4529
|
+
type: "button",
|
|
4530
|
+
className: "w-6 h-6 rounded border border-border dark:border-zinc-600 hover:scale-110 transition-transform flex items-center justify-center bg-background",
|
|
4531
|
+
onClick: () => editor.chain().focus().unsetColor().run(),
|
|
4532
|
+
title: "\u6E05\u9664\u6587\u5B57\u984F\u8272",
|
|
4533
|
+
"aria-label": "\u6E05\u9664\u6587\u5B57\u984F\u8272",
|
|
4534
|
+
children: /* @__PURE__ */ jsx10(X, { className: "h-3 w-3" })
|
|
4535
|
+
}
|
|
4536
|
+
)
|
|
4537
|
+
] }),
|
|
4538
|
+
/* @__PURE__ */ jsx10(DropdownMenuSeparator, {}),
|
|
4539
|
+
/* @__PURE__ */ jsx10("div", { className: "text-xs font-medium text-muted-foreground mb-2 mt-2", children: "\u80CC\u666F\u8272" }),
|
|
4540
|
+
/* @__PURE__ */ jsxs5("div", { className: "flex gap-1.5", children: [
|
|
4541
|
+
COLOR_PALETTE.map((color) => /* @__PURE__ */ jsx10(
|
|
4542
|
+
"button",
|
|
4543
|
+
{
|
|
4544
|
+
type: "button",
|
|
4545
|
+
className: "w-6 h-6 rounded border border-border dark:border-zinc-600 hover:scale-110 transition-transform",
|
|
4546
|
+
style: { backgroundColor: color.value },
|
|
4547
|
+
onClick: () => editor.chain().focus().toggleHighlight({ color: color.value }).run(),
|
|
4548
|
+
title: color.name,
|
|
4549
|
+
"aria-label": `\u80CC\u666F\u8272\uFF1A${color.name}`
|
|
4550
|
+
},
|
|
4551
|
+
`bg-${color.value}`
|
|
4552
|
+
)),
|
|
4553
|
+
/* @__PURE__ */ jsx10(
|
|
4554
|
+
"button",
|
|
4555
|
+
{
|
|
4556
|
+
type: "button",
|
|
4557
|
+
className: "w-6 h-6 rounded border border-border dark:border-zinc-600 hover:scale-110 transition-transform flex items-center justify-center bg-background",
|
|
4558
|
+
onClick: () => editor.chain().focus().unsetHighlight().run(),
|
|
4559
|
+
title: "\u6E05\u9664\u80CC\u666F\u8272",
|
|
4560
|
+
"aria-label": "\u6E05\u9664\u80CC\u666F\u8272",
|
|
4561
|
+
children: /* @__PURE__ */ jsx10(X, { className: "h-3 w-3" })
|
|
4562
|
+
}
|
|
4563
|
+
)
|
|
4564
|
+
] })
|
|
4565
|
+
] }) })
|
|
4566
|
+
] });
|
|
4567
|
+
}
|
|
4568
|
+
|
|
4569
|
+
// src/components/Toolbar.tsx
|
|
4570
|
+
import {
|
|
4571
|
+
Bold,
|
|
4572
|
+
Italic,
|
|
4573
|
+
Code,
|
|
4574
|
+
Underline as UnderlineIcon,
|
|
4575
|
+
Link as LinkIcon,
|
|
4576
|
+
Superscript as SuperscriptIcon,
|
|
4577
|
+
Subscript as SubscriptIcon,
|
|
4578
|
+
AlignLeft,
|
|
4579
|
+
AlignCenter,
|
|
4580
|
+
AlignRight,
|
|
4581
|
+
ImagePlus,
|
|
4582
|
+
Sun,
|
|
4583
|
+
Moon
|
|
4584
|
+
} from "lucide-react";
|
|
4585
|
+
import { Fragment as Fragment6, jsx as jsx11, jsxs as jsxs6 } from "react/jsx-runtime";
|
|
4586
|
+
var ICON_SIZE = "h-4 w-4";
|
|
4587
|
+
var TEXT_FORMAT_BUTTONS = [
|
|
4588
|
+
{ key: "bold", icon: Bold, title: "\u7C97\u9AD4", isActive: (e) => e.isActive("bold"), action: (e) => e.chain().focus().toggleBold().run() },
|
|
4589
|
+
{ key: "italic", icon: Italic, title: "\u659C\u9AD4", isActive: (e) => e.isActive("italic"), action: (e) => e.chain().focus().toggleItalic().run() },
|
|
4590
|
+
{ key: "code", icon: Code, title: "\u884C\u5167\u7A0B\u5F0F\u78BC", isActive: (e) => e.isActive("code"), action: (e) => e.chain().focus().toggleCode().run() },
|
|
4591
|
+
{ key: "underline", icon: UnderlineIcon, title: "\u5E95\u7DDA", isActive: (e) => e.isActive("underline"), action: (e) => e.chain().focus().toggleUnderline().run() }
|
|
4592
|
+
];
|
|
4593
|
+
var SCRIPT_BUTTONS = [
|
|
4594
|
+
{ key: "superscript", icon: SuperscriptIcon, title: "\u4E0A\u6A19", isActive: (e) => e.isActive("superscript"), action: (e) => e.chain().focus().toggleSuperscript().run() },
|
|
4595
|
+
{ key: "subscript", icon: SubscriptIcon, title: "\u4E0B\u6A19", isActive: (e) => e.isActive("subscript"), action: (e) => e.chain().focus().toggleSubscript().run() }
|
|
4596
|
+
];
|
|
4597
|
+
var ALIGN_BUTTONS = [
|
|
4598
|
+
{ key: "left", icon: AlignLeft, title: "\u9760\u5DE6\u5C0D\u9F4A", isActive: (e) => e.isActive({ textAlign: "left" }), action: (e) => e.chain().focus().setTextAlign("left").run() },
|
|
4599
|
+
{ key: "center", icon: AlignCenter, title: "\u7F6E\u4E2D\u5C0D\u9F4A", isActive: (e) => e.isActive({ textAlign: "center" }), action: (e) => e.chain().focus().setTextAlign("center").run() },
|
|
4600
|
+
{ key: "right", icon: AlignRight, title: "\u9760\u53F3\u5C0D\u9F4A", isActive: (e) => e.isActive({ textAlign: "right" }), action: (e) => e.chain().focus().setTextAlign("right").run() }
|
|
4601
|
+
];
|
|
4602
|
+
function ToolbarButtonGroup({ buttons, editor }) {
|
|
4603
|
+
return /* @__PURE__ */ jsx11(Fragment6, { children: buttons.map(({ key, icon: Icon, title, isActive, action }) => /* @__PURE__ */ jsx11(
|
|
4604
|
+
ToolbarButton,
|
|
4605
|
+
{
|
|
4606
|
+
pressed: isActive(editor),
|
|
4607
|
+
onPressedChange: () => action(editor),
|
|
4608
|
+
title,
|
|
4609
|
+
children: /* @__PURE__ */ jsx11(Icon, { className: ICON_SIZE })
|
|
4610
|
+
},
|
|
4611
|
+
key
|
|
4612
|
+
)) });
|
|
4613
|
+
}
|
|
4614
|
+
function normalizeConfig(config) {
|
|
4615
|
+
if (config === "none") return [];
|
|
4616
|
+
if (config === "all" || !config) {
|
|
4617
|
+
return [
|
|
4618
|
+
["heading", "list", "table", "callout"],
|
|
4619
|
+
["format", "link", "color"],
|
|
4620
|
+
["script"],
|
|
4621
|
+
["align"],
|
|
4622
|
+
["image"],
|
|
4623
|
+
["themeToggle"]
|
|
4624
|
+
];
|
|
4625
|
+
}
|
|
4626
|
+
if (Array.isArray(config)) {
|
|
4627
|
+
if (config.length > 0 && Array.isArray(config[0])) {
|
|
4628
|
+
return config;
|
|
4629
|
+
}
|
|
4630
|
+
return [config];
|
|
4631
|
+
}
|
|
4632
|
+
return [];
|
|
4633
|
+
}
|
|
4634
|
+
function Toolbar({
|
|
4635
|
+
editor,
|
|
4636
|
+
config,
|
|
4637
|
+
toolbarStart,
|
|
4638
|
+
toolbarEnd,
|
|
4639
|
+
theme,
|
|
4640
|
+
onThemeToggle,
|
|
4641
|
+
onImageUpload
|
|
4642
|
+
}) {
|
|
4643
|
+
const fileInputRef = useRef(null);
|
|
4644
|
+
if (!editor) return null;
|
|
4645
|
+
const groups = normalizeConfig(config);
|
|
4646
|
+
const setLink = () => {
|
|
4647
|
+
const previousUrl = editor.getAttributes("link").href;
|
|
4648
|
+
const url = window.prompt("\u8F38\u5165\u9023\u7D50\u7DB2\u5740", previousUrl);
|
|
4649
|
+
if (url === null) return;
|
|
4650
|
+
if (url === "") {
|
|
4651
|
+
editor.chain().focus().extendMarkRange("link").unsetLink().run();
|
|
4652
|
+
return;
|
|
4653
|
+
}
|
|
4654
|
+
editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
|
|
4655
|
+
};
|
|
4656
|
+
const handleImageUpload = (e) => {
|
|
4657
|
+
const file = e.target.files?.[0];
|
|
4658
|
+
if (!file) return;
|
|
4659
|
+
const MAX_SIZE = 5 * 1024 * 1024;
|
|
4660
|
+
if (file.size > MAX_SIZE) {
|
|
4661
|
+
alert("\u5716\u7247\u5927\u5C0F\u4E0D\u53EF\u8D85\u904E 5MB");
|
|
4662
|
+
e.target.value = "";
|
|
4663
|
+
return;
|
|
4664
|
+
}
|
|
4665
|
+
if (onImageUpload) {
|
|
4666
|
+
onImageUpload(file);
|
|
4667
|
+
} else {
|
|
4668
|
+
const reader = new FileReader();
|
|
4669
|
+
reader.onload = () => {
|
|
4670
|
+
const base64 = reader.result;
|
|
4671
|
+
editor.chain().focus().setImage({ src: base64 }).run();
|
|
4672
|
+
};
|
|
4673
|
+
reader.onerror = () => {
|
|
4674
|
+
console.error("\u5716\u7247\u8B80\u53D6\u5931\u6557");
|
|
4675
|
+
};
|
|
4676
|
+
reader.readAsDataURL(file);
|
|
4677
|
+
}
|
|
4678
|
+
e.target.value = "";
|
|
4679
|
+
};
|
|
4680
|
+
const renderToolbarItem = (item) => {
|
|
4681
|
+
switch (item) {
|
|
4682
|
+
case "heading":
|
|
4683
|
+
return /* @__PURE__ */ jsx11(HeadingDropdown, { editor, iconSize: ICON_SIZE }, "heading");
|
|
4684
|
+
case "list":
|
|
4685
|
+
return /* @__PURE__ */ jsx11(ListDropdown, { editor, iconSize: ICON_SIZE }, "list");
|
|
4686
|
+
case "table":
|
|
4687
|
+
return /* @__PURE__ */ jsx11(TableDropdown, { editor, iconSize: ICON_SIZE }, "table");
|
|
4688
|
+
case "callout":
|
|
4689
|
+
return /* @__PURE__ */ jsx11(CalloutDropdown, { editor, iconSize: ICON_SIZE }, "callout");
|
|
4690
|
+
case "format":
|
|
4691
|
+
return /* @__PURE__ */ jsx11(ToolbarButtonGroup, { buttons: TEXT_FORMAT_BUTTONS, editor }, "format");
|
|
4692
|
+
case "link":
|
|
4693
|
+
return /* @__PURE__ */ jsx11(
|
|
4694
|
+
ToolbarButton,
|
|
4695
|
+
{
|
|
4696
|
+
pressed: editor.isActive("link"),
|
|
4697
|
+
onPressedChange: setLink,
|
|
4698
|
+
title: "\u9023\u7D50",
|
|
4699
|
+
children: /* @__PURE__ */ jsx11(LinkIcon, { className: ICON_SIZE })
|
|
4700
|
+
},
|
|
4701
|
+
"link"
|
|
4702
|
+
);
|
|
4703
|
+
case "color":
|
|
4704
|
+
return /* @__PURE__ */ jsx11(ColorPicker, { editor, iconSize: ICON_SIZE }, "color");
|
|
4705
|
+
case "script":
|
|
4706
|
+
return /* @__PURE__ */ jsx11(ToolbarButtonGroup, { buttons: SCRIPT_BUTTONS, editor }, "script");
|
|
4707
|
+
case "align":
|
|
4708
|
+
return /* @__PURE__ */ jsx11(ToolbarButtonGroup, { buttons: ALIGN_BUTTONS, editor }, "align");
|
|
4709
|
+
case "image":
|
|
4710
|
+
return /* @__PURE__ */ jsx11(
|
|
4711
|
+
Button,
|
|
4712
|
+
{
|
|
4713
|
+
variant: "ghost",
|
|
4714
|
+
size: "sm",
|
|
4715
|
+
className: "h-8 px-2 gap-1",
|
|
4716
|
+
onClick: () => fileInputRef.current?.click(),
|
|
4717
|
+
title: "\u63D2\u5165\u5716\u7247",
|
|
4718
|
+
children: /* @__PURE__ */ jsx11(ImagePlus, { className: ICON_SIZE })
|
|
4719
|
+
},
|
|
4720
|
+
"image"
|
|
4721
|
+
);
|
|
4722
|
+
case "themeToggle":
|
|
4723
|
+
return onThemeToggle ? /* @__PURE__ */ jsx11(
|
|
4724
|
+
Button,
|
|
4725
|
+
{
|
|
4726
|
+
variant: "ghost",
|
|
4727
|
+
size: "sm",
|
|
4728
|
+
className: "h-8 w-8 p-0",
|
|
4729
|
+
onClick: onThemeToggle,
|
|
4730
|
+
title: theme === "dark" ? "\u5207\u63DB\u6DFA\u8272\u6A21\u5F0F" : "\u5207\u63DB\u6DF1\u8272\u6A21\u5F0F",
|
|
4731
|
+
children: theme === "dark" ? /* @__PURE__ */ jsx11(Moon, { className: ICON_SIZE }) : /* @__PURE__ */ jsx11(Sun, { className: ICON_SIZE })
|
|
4732
|
+
},
|
|
4733
|
+
"themeToggle"
|
|
4734
|
+
) : null;
|
|
4735
|
+
default:
|
|
4736
|
+
return null;
|
|
4737
|
+
}
|
|
4738
|
+
};
|
|
4739
|
+
return /* @__PURE__ */ jsx11("div", { className: "flex items-center justify-center mb-2 relative", children: /* @__PURE__ */ jsxs6("div", { className: "flex items-center justify-between px-2 py-1.5 bg-white dark:bg-zinc-900 rounded-lg shadow-sm w-full max-w-7xl mx-auto", children: [
|
|
4740
|
+
/* @__PURE__ */ jsxs6("div", { className: "flex items-center gap-0.5", children: [
|
|
4741
|
+
/* @__PURE__ */ jsx11(
|
|
4742
|
+
"input",
|
|
4743
|
+
{
|
|
4744
|
+
ref: fileInputRef,
|
|
4745
|
+
type: "file",
|
|
4746
|
+
accept: "image/*",
|
|
4747
|
+
className: "hidden",
|
|
4748
|
+
onChange: handleImageUpload,
|
|
4749
|
+
"aria-label": "\u4E0A\u50B3\u5716\u7247"
|
|
4750
|
+
}
|
|
4751
|
+
),
|
|
4752
|
+
toolbarStart,
|
|
4753
|
+
groups.map((group, groupIndex) => /* @__PURE__ */ jsxs6("div", { className: "flex items-center gap-0.5", children: [
|
|
4754
|
+
groupIndex > 0 && /* @__PURE__ */ jsx11(ToolbarDivider, {}),
|
|
4755
|
+
group.map((item) => renderToolbarItem(item))
|
|
4756
|
+
] }, groupIndex))
|
|
4757
|
+
] }),
|
|
4758
|
+
/* @__PURE__ */ jsx11("div", { className: "flex items-center gap-0.5", children: toolbarEnd })
|
|
4759
|
+
] }) });
|
|
4760
|
+
}
|
|
4761
|
+
|
|
4762
|
+
// src/EliaEditor.tsx
|
|
4763
|
+
import { jsx as jsx12, jsxs as jsxs7 } from "react/jsx-runtime";
|
|
4764
|
+
function EliaEditor({
|
|
4765
|
+
content = "",
|
|
4766
|
+
onChange,
|
|
4767
|
+
placeholder = "\u958B\u59CB\u5BEB\u4F5C...",
|
|
4768
|
+
toolbar = "all",
|
|
4769
|
+
header,
|
|
4770
|
+
toolbarStart,
|
|
4771
|
+
toolbarEnd,
|
|
4772
|
+
footer,
|
|
4773
|
+
className,
|
|
4774
|
+
editorClassName,
|
|
4775
|
+
autofocus = false,
|
|
4776
|
+
readOnly = false,
|
|
4777
|
+
theme,
|
|
4778
|
+
onThemeToggle,
|
|
4779
|
+
onImageUpload
|
|
4780
|
+
}) {
|
|
4781
|
+
const isInternalUpdate = useRef2(false);
|
|
4782
|
+
const handleUpdate = useCallback(
|
|
4783
|
+
({ editor: editor2 }) => {
|
|
4784
|
+
isInternalUpdate.current = true;
|
|
4785
|
+
onChange?.(editor2.getHTML());
|
|
4786
|
+
},
|
|
4787
|
+
[onChange]
|
|
4788
|
+
);
|
|
4789
|
+
const editor = useEditor({
|
|
4790
|
+
extensions: [
|
|
4791
|
+
StarterKit.configure({
|
|
4792
|
+
heading: {
|
|
4793
|
+
levels: [1, 2, 3]
|
|
4794
|
+
}
|
|
4795
|
+
}),
|
|
4796
|
+
Placeholder.configure({
|
|
4797
|
+
placeholder
|
|
4798
|
+
}),
|
|
4799
|
+
Underline,
|
|
4800
|
+
TextAlign.configure({
|
|
4801
|
+
types: ["heading", "paragraph"]
|
|
4802
|
+
}),
|
|
4803
|
+
Link.configure({
|
|
4804
|
+
openOnClick: false,
|
|
4805
|
+
HTMLAttributes: {
|
|
4806
|
+
class: "text-primary underline underline-offset-4"
|
|
4807
|
+
}
|
|
4808
|
+
}),
|
|
4809
|
+
TextStyle,
|
|
4810
|
+
Color,
|
|
4811
|
+
Highlight.configure({
|
|
4812
|
+
multicolor: true
|
|
4813
|
+
}),
|
|
4814
|
+
Subscript,
|
|
4815
|
+
Superscript,
|
|
4816
|
+
TaskList,
|
|
4817
|
+
TaskItem.configure({
|
|
4818
|
+
nested: true
|
|
4819
|
+
}),
|
|
4820
|
+
Table.configure({
|
|
4821
|
+
resizable: true
|
|
4822
|
+
}),
|
|
4823
|
+
TableRow,
|
|
4824
|
+
TableHeader,
|
|
4825
|
+
TableCell,
|
|
4826
|
+
ImageResize.configure({
|
|
4827
|
+
allowBase64: true
|
|
4828
|
+
}),
|
|
4829
|
+
Callout
|
|
4830
|
+
],
|
|
4831
|
+
content,
|
|
4832
|
+
autofocus,
|
|
4833
|
+
editable: !readOnly,
|
|
4834
|
+
editorProps: {
|
|
4835
|
+
attributes: {
|
|
4836
|
+
class: cn(
|
|
4837
|
+
"tiptap-editor min-h-[300px] p-4 focus:outline-none",
|
|
4838
|
+
editorClassName
|
|
4839
|
+
)
|
|
4840
|
+
}
|
|
4841
|
+
},
|
|
4842
|
+
onUpdate: handleUpdate
|
|
4843
|
+
});
|
|
4844
|
+
useEffect(() => {
|
|
4845
|
+
if (editor && !isInternalUpdate.current && content !== editor.getHTML()) {
|
|
4846
|
+
editor.commands.setContent(content);
|
|
4847
|
+
}
|
|
4848
|
+
isInternalUpdate.current = false;
|
|
4849
|
+
}, [content, editor]);
|
|
4850
|
+
useEffect(() => {
|
|
4851
|
+
if (editor) {
|
|
4852
|
+
editor.setEditable(!readOnly);
|
|
4853
|
+
}
|
|
4854
|
+
}, [readOnly, editor]);
|
|
4855
|
+
return /* @__PURE__ */ jsxs7("div", { className: cn("elia-editor", className), children: [
|
|
4856
|
+
header && /* @__PURE__ */ jsx12("div", { className: "elia-editor-header", children: header }),
|
|
4857
|
+
/* @__PURE__ */ jsx12(
|
|
4858
|
+
Toolbar,
|
|
4859
|
+
{
|
|
4860
|
+
editor,
|
|
4861
|
+
config: toolbar,
|
|
4862
|
+
toolbarStart,
|
|
4863
|
+
toolbarEnd,
|
|
4864
|
+
theme,
|
|
4865
|
+
onThemeToggle,
|
|
4866
|
+
onImageUpload
|
|
4867
|
+
}
|
|
4868
|
+
),
|
|
4869
|
+
/* @__PURE__ */ jsx12(EditorContent, { editor }),
|
|
4870
|
+
footer && /* @__PURE__ */ jsx12("div", { className: "elia-editor-footer", children: footer })
|
|
4871
|
+
] });
|
|
4872
|
+
}
|
|
4873
|
+
export {
|
|
4874
|
+
Button,
|
|
4875
|
+
Callout,
|
|
4876
|
+
CalloutDropdown,
|
|
4877
|
+
ColorPicker,
|
|
4878
|
+
DropdownMenu,
|
|
4879
|
+
DropdownMenuContent,
|
|
4880
|
+
DropdownMenuItem,
|
|
4881
|
+
DropdownMenuSeparator,
|
|
4882
|
+
DropdownMenuTrigger,
|
|
4883
|
+
EliaEditor,
|
|
4884
|
+
HeadingDropdown,
|
|
4885
|
+
ListDropdown,
|
|
4886
|
+
TableDropdown,
|
|
4887
|
+
Toggle,
|
|
4888
|
+
Toolbar,
|
|
4889
|
+
ToolbarButton,
|
|
4890
|
+
ToolbarDivider,
|
|
4891
|
+
cn
|
|
4892
|
+
};
|
|
4893
|
+
//# sourceMappingURL=index.js.map
|