@prosekit/core 0.0.0-next-20240724172520 → 0.0.0-next-20240901092634
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/_tsup-dts-rollup.d.ts +413 -120
- package/dist/{chunk-MOSGJZHV.js → chunk-RNBMCB5M.js} +363 -258
- package/dist/prosekit-core-test.js +16 -19
- package/dist/prosekit-core.d.ts +10 -1
- package/dist/prosekit-core.js +321 -305
- package/package.json +9 -6
@@ -17,7 +17,6 @@ var DOMDocumentNotFoundError = class extends ProseKitError {
|
|
17
17
|
};
|
18
18
|
|
19
19
|
// src/utils/get-mark-type.ts
|
20
|
-
import "@prosekit/pm/model";
|
21
20
|
function getMarkType(schema, type) {
|
22
21
|
if (typeof type === "string") {
|
23
22
|
const markType = schema.marks[type];
|
@@ -37,7 +36,6 @@ function assert(condition, message = "Assertion failed") {
|
|
37
36
|
}
|
38
37
|
|
39
38
|
// src/utils/get-node-type.ts
|
40
|
-
import "@prosekit/pm/model";
|
41
39
|
function getNodeType(schema, type) {
|
42
40
|
if (typeof type === "string") {
|
43
41
|
const nodeType = schema.nodes[type];
|
@@ -92,8 +90,110 @@ function defineFacet(options) {
|
|
92
90
|
);
|
93
91
|
}
|
94
92
|
|
93
|
+
// src/utils/type-assertion.ts
|
94
|
+
import { Fragment, Mark, ProseMirrorNode, Slice } from "@prosekit/pm/model";
|
95
|
+
import {
|
96
|
+
AllSelection,
|
97
|
+
NodeSelection,
|
98
|
+
Selection,
|
99
|
+
TextSelection
|
100
|
+
} from "@prosekit/pm/state";
|
101
|
+
function isProseMirrorNode(node) {
|
102
|
+
return node instanceof ProseMirrorNode;
|
103
|
+
}
|
104
|
+
function isMark(mark) {
|
105
|
+
return mark instanceof Mark;
|
106
|
+
}
|
107
|
+
function isFragment(fragment) {
|
108
|
+
return fragment instanceof Fragment;
|
109
|
+
}
|
110
|
+
function isSlice(slice) {
|
111
|
+
return slice instanceof Slice;
|
112
|
+
}
|
113
|
+
function isSelection(sel) {
|
114
|
+
return sel instanceof Selection;
|
115
|
+
}
|
116
|
+
function isTextSelection(sel) {
|
117
|
+
return sel instanceof TextSelection;
|
118
|
+
}
|
119
|
+
function isNodeSelection(sel) {
|
120
|
+
return sel instanceof NodeSelection;
|
121
|
+
}
|
122
|
+
function isAllSelection(sel) {
|
123
|
+
return sel instanceof AllSelection;
|
124
|
+
}
|
125
|
+
function isNotNullish(value) {
|
126
|
+
return value != null;
|
127
|
+
}
|
128
|
+
|
129
|
+
// src/facets/schema.ts
|
130
|
+
import { Schema } from "@prosekit/pm/model";
|
131
|
+
|
132
|
+
// src/facets/root.ts
|
133
|
+
function rootReducer(inputs) {
|
134
|
+
var _a;
|
135
|
+
let schema;
|
136
|
+
let commands;
|
137
|
+
let stateFunc;
|
138
|
+
let view;
|
139
|
+
for (const input of inputs) {
|
140
|
+
schema = input.schema || schema;
|
141
|
+
commands = input.commands || commands;
|
142
|
+
stateFunc = input.state || stateFunc;
|
143
|
+
view = input.view || view;
|
144
|
+
}
|
145
|
+
const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
|
146
|
+
return { schema, state, commands, view };
|
147
|
+
}
|
148
|
+
var rootFacet = new Facet(
|
149
|
+
null,
|
150
|
+
true,
|
151
|
+
rootReducer
|
152
|
+
);
|
153
|
+
|
154
|
+
// src/facets/schema.ts
|
155
|
+
var schemaFacet = defineFacet({
|
156
|
+
reducer: (specs) => {
|
157
|
+
assert(specs.length <= 1);
|
158
|
+
const spec = specs[0];
|
159
|
+
const schema = spec ? new Schema(spec) : null;
|
160
|
+
return { schema };
|
161
|
+
},
|
162
|
+
parent: rootFacet,
|
163
|
+
singleton: true
|
164
|
+
});
|
165
|
+
|
95
166
|
// src/facets/base-extension.ts
|
96
|
-
|
167
|
+
var BaseExtension = class {
|
168
|
+
constructor() {
|
169
|
+
this.extension = [];
|
170
|
+
this.trees = [null, null, null, null, null];
|
171
|
+
}
|
172
|
+
/**
|
173
|
+
* @internal
|
174
|
+
*/
|
175
|
+
getTree(priority) {
|
176
|
+
var _a, _b;
|
177
|
+
const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
|
178
|
+
return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
|
179
|
+
}
|
180
|
+
/**
|
181
|
+
* @internal
|
182
|
+
*/
|
183
|
+
findFacetOutput(facet) {
|
184
|
+
var _a;
|
185
|
+
let node = this.getTree();
|
186
|
+
for (const index of facet.path) {
|
187
|
+
node = node == null ? void 0 : node.children.get(index);
|
188
|
+
}
|
189
|
+
return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
|
190
|
+
}
|
191
|
+
get schema() {
|
192
|
+
var _a, _b;
|
193
|
+
const output = this.findFacetOutput(schemaFacet);
|
194
|
+
return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
|
195
|
+
}
|
196
|
+
};
|
97
197
|
|
98
198
|
// src/utils/array.ts
|
99
199
|
function uniqPush(prev, next) {
|
@@ -113,11 +213,6 @@ function toReversed(arr) {
|
|
113
213
|
return (_b = (_a = arr.toReversed) == null ? void 0 : _a.call(arr)) != null ? _b : [...arr].reverse();
|
114
214
|
}
|
115
215
|
|
116
|
-
// src/utils/is-not-null.ts
|
117
|
-
function isNotNull(value) {
|
118
|
-
return value != null;
|
119
|
-
}
|
120
|
-
|
121
216
|
// src/facets/facet-node.ts
|
122
217
|
function zip5(a, b, mapper) {
|
123
218
|
return [
|
@@ -208,7 +303,7 @@ var FacetNode = class {
|
|
208
303
|
}
|
209
304
|
if (this.facet.singleton) {
|
210
305
|
const reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer);
|
211
|
-
const input = inputs.filter(
|
306
|
+
const input = inputs.filter(isNotNullish).flat();
|
212
307
|
output[2 /* default */] = reducer(input);
|
213
308
|
} else {
|
214
309
|
for (let pri = 0; pri < 5; pri++) {
|
@@ -242,75 +337,6 @@ var FacetNode = class {
|
|
242
337
|
}
|
243
338
|
};
|
244
339
|
|
245
|
-
// src/facets/schema.ts
|
246
|
-
import { Schema as Schema3 } from "@prosekit/pm/model";
|
247
|
-
|
248
|
-
// src/facets/root.ts
|
249
|
-
function rootReducer(inputs) {
|
250
|
-
var _a;
|
251
|
-
let schema;
|
252
|
-
let commands;
|
253
|
-
let stateFunc;
|
254
|
-
let view;
|
255
|
-
for (const input of inputs) {
|
256
|
-
schema = input.schema || schema;
|
257
|
-
commands = input.commands || commands;
|
258
|
-
stateFunc = input.state || stateFunc;
|
259
|
-
view = input.view || view;
|
260
|
-
}
|
261
|
-
const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
|
262
|
-
return { schema, state, commands, view };
|
263
|
-
}
|
264
|
-
var rootFacet = new Facet(
|
265
|
-
null,
|
266
|
-
true,
|
267
|
-
rootReducer
|
268
|
-
);
|
269
|
-
|
270
|
-
// src/facets/schema.ts
|
271
|
-
var schemaFacet = defineFacet({
|
272
|
-
reducer: (specs) => {
|
273
|
-
assert(specs.length <= 1);
|
274
|
-
const spec = specs[0];
|
275
|
-
const schema = spec ? new Schema3(spec) : null;
|
276
|
-
return { schema };
|
277
|
-
},
|
278
|
-
parent: rootFacet,
|
279
|
-
singleton: true
|
280
|
-
});
|
281
|
-
|
282
|
-
// src/facets/base-extension.ts
|
283
|
-
var BaseExtension = class {
|
284
|
-
constructor() {
|
285
|
-
this.extension = [];
|
286
|
-
this.trees = [null, null, null, null, null];
|
287
|
-
}
|
288
|
-
/**
|
289
|
-
* @internal
|
290
|
-
*/
|
291
|
-
getTree(priority) {
|
292
|
-
var _a, _b;
|
293
|
-
const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
|
294
|
-
return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
|
295
|
-
}
|
296
|
-
/**
|
297
|
-
* @internal
|
298
|
-
*/
|
299
|
-
findFacetOutput(facet) {
|
300
|
-
var _a;
|
301
|
-
let node = this.getTree();
|
302
|
-
for (const index of facet.path) {
|
303
|
-
node = node == null ? void 0 : node.children.get(index);
|
304
|
-
}
|
305
|
-
return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
|
306
|
-
}
|
307
|
-
get schema() {
|
308
|
-
var _a, _b;
|
309
|
-
const output = this.findFacetOutput(schemaFacet);
|
310
|
-
return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
|
311
|
-
}
|
312
|
-
};
|
313
|
-
|
314
340
|
// src/facets/facet-extension.ts
|
315
341
|
var FacetExtensionImpl = class extends BaseExtension {
|
316
342
|
/**
|
@@ -342,7 +368,10 @@ function defineFacetPayload(facet, payloads) {
|
|
342
368
|
}
|
343
369
|
|
344
370
|
// src/utils/parse.ts
|
345
|
-
import {
|
371
|
+
import {
|
372
|
+
DOMParser,
|
373
|
+
DOMSerializer
|
374
|
+
} from "@prosekit/pm/model";
|
346
375
|
import { EditorState } from "@prosekit/pm/state";
|
347
376
|
|
348
377
|
// src/utils/get-dom-api.ts
|
@@ -441,7 +470,7 @@ function htmlFromJSON(json, options) {
|
|
441
470
|
}
|
442
471
|
|
443
472
|
// src/extensions/default-state.ts
|
444
|
-
import { Selection } from "@prosekit/pm/state";
|
473
|
+
import { Selection as Selection3 } from "@prosekit/pm/state";
|
445
474
|
|
446
475
|
// src/facets/state.ts
|
447
476
|
var stateFacet = defineFacet({
|
@@ -480,31 +509,78 @@ var stateFacet = defineFacet({
|
|
480
509
|
parent: rootFacet
|
481
510
|
});
|
482
511
|
|
512
|
+
// src/utils/editor-content.ts
|
513
|
+
import { Selection as Selection2 } from "@prosekit/pm/state";
|
514
|
+
|
515
|
+
// src/utils/is-object.ts
|
516
|
+
function isObject(v) {
|
517
|
+
return typeof v === "object" && v != null;
|
518
|
+
}
|
519
|
+
|
520
|
+
// src/utils/is-element.ts
|
521
|
+
var ELEMENT_NODE = 1;
|
522
|
+
function isElement(el) {
|
523
|
+
return isObject(el) && el.nodeType === ELEMENT_NODE && typeof el.nodeName === "string";
|
524
|
+
}
|
525
|
+
|
526
|
+
// src/utils/editor-content.ts
|
527
|
+
function getEditorContentJSON(schema, content) {
|
528
|
+
if (typeof content === "string") {
|
529
|
+
return jsonFromHTML(content, { schema });
|
530
|
+
} else if (isElement(content)) {
|
531
|
+
return jsonFromElement(content, { schema });
|
532
|
+
} else {
|
533
|
+
return content;
|
534
|
+
}
|
535
|
+
}
|
536
|
+
function getEditorContentNode(schema, content) {
|
537
|
+
if (isProseMirrorNode(content)) {
|
538
|
+
return content;
|
539
|
+
}
|
540
|
+
return schema.nodeFromJSON(getEditorContentJSON(schema, content));
|
541
|
+
}
|
542
|
+
function getEditorContentDoc(schema, content) {
|
543
|
+
const doc = getEditorContentNode(schema, content);
|
544
|
+
assert(
|
545
|
+
doc.type.schema === schema,
|
546
|
+
"Document schema does not match editor schema"
|
547
|
+
);
|
548
|
+
assert(
|
549
|
+
doc.type === schema.topNodeType,
|
550
|
+
`Document type does not match editor top node type. Expected ${schema.topNodeType.name}, got ${doc.type.name}`
|
551
|
+
);
|
552
|
+
return doc;
|
553
|
+
}
|
554
|
+
function getEditorSelection(doc, selection) {
|
555
|
+
if (isSelection(selection)) {
|
556
|
+
assert(selection.$head.doc === doc, "Selection and doc do not match");
|
557
|
+
return selection;
|
558
|
+
}
|
559
|
+
if (selection === "start") {
|
560
|
+
return Selection2.atStart(doc);
|
561
|
+
}
|
562
|
+
if (selection === "end") {
|
563
|
+
return Selection2.atEnd(doc);
|
564
|
+
}
|
565
|
+
return Selection2.fromJSON(doc, selection);
|
566
|
+
}
|
567
|
+
|
483
568
|
// src/extensions/default-state.ts
|
484
569
|
function defineDefaultState({
|
570
|
+
defaultSelection,
|
571
|
+
defaultContent,
|
485
572
|
defaultDoc,
|
486
|
-
defaultHTML
|
487
|
-
defaultSelection
|
573
|
+
defaultHTML
|
488
574
|
}) {
|
489
|
-
|
490
|
-
throw new ProseKitError(
|
491
|
-
"Only one of defaultHTML and defaultDoc can be provided"
|
492
|
-
);
|
493
|
-
}
|
575
|
+
const defaultDocContent = defaultContent || defaultDoc || defaultHTML;
|
494
576
|
return defineFacetPayload(stateFacet, [
|
495
577
|
({ schema }) => {
|
496
578
|
const config = {};
|
497
|
-
if (
|
498
|
-
|
499
|
-
|
500
|
-
} else {
|
501
|
-
defaultDoc = jsonFromElement(defaultHTML, { schema });
|
502
|
-
}
|
503
|
-
}
|
504
|
-
if (defaultDoc) {
|
505
|
-
config.doc = schema.nodeFromJSON(defaultDoc);
|
579
|
+
if (defaultDocContent) {
|
580
|
+
const json = getEditorContentJSON(schema, defaultDocContent);
|
581
|
+
config.doc = schema.nodeFromJSON(json);
|
506
582
|
if (defaultSelection) {
|
507
|
-
config.selection =
|
583
|
+
config.selection = Selection3.fromJSON(config.doc, defaultSelection);
|
508
584
|
}
|
509
585
|
}
|
510
586
|
return config;
|
@@ -556,29 +632,6 @@ function isMarkActive(state, type, attrs) {
|
|
556
632
|
}
|
557
633
|
}
|
558
634
|
|
559
|
-
// src/utils/type-assertion.ts
|
560
|
-
import { Mark, ProseMirrorNode } from "@prosekit/pm/model";
|
561
|
-
import {
|
562
|
-
AllSelection,
|
563
|
-
NodeSelection,
|
564
|
-
TextSelection
|
565
|
-
} from "@prosekit/pm/state";
|
566
|
-
function isProseMirrorNode(node) {
|
567
|
-
return node instanceof ProseMirrorNode;
|
568
|
-
}
|
569
|
-
function isMark(mark) {
|
570
|
-
return mark instanceof Mark;
|
571
|
-
}
|
572
|
-
function isTextSelection(sel) {
|
573
|
-
return sel instanceof TextSelection;
|
574
|
-
}
|
575
|
-
function isNodeSelection(sel) {
|
576
|
-
return sel instanceof NodeSelection;
|
577
|
-
}
|
578
|
-
function isAllSelection(sel) {
|
579
|
-
return sel instanceof AllSelection;
|
580
|
-
}
|
581
|
-
|
582
635
|
// src/facets/union-extension.ts
|
583
636
|
var UnionExtensionImpl = class extends BaseExtension {
|
584
637
|
/**
|
@@ -617,8 +670,9 @@ function union(...exts) {
|
|
617
670
|
}
|
618
671
|
|
619
672
|
// src/editor/editor.ts
|
620
|
-
import
|
621
|
-
|
673
|
+
import {
|
674
|
+
EditorState as EditorState2
|
675
|
+
} from "@prosekit/pm/state";
|
622
676
|
import { EditorView } from "@prosekit/pm/view";
|
623
677
|
|
624
678
|
// src/utils/deep-equals.ts
|
@@ -645,7 +699,7 @@ function deepEquals(a, b) {
|
|
645
699
|
}
|
646
700
|
|
647
701
|
// src/editor/action.ts
|
648
|
-
import "
|
702
|
+
import mapValues from "just-map-values";
|
649
703
|
|
650
704
|
// src/utils/attrs-match.ts
|
651
705
|
function attrsMatch(nodeOrMark, attrs) {
|
@@ -673,28 +727,32 @@ function isNodeActive(state, type, attrs) {
|
|
673
727
|
|
674
728
|
// src/editor/action.ts
|
675
729
|
function createNodeActions(schema, getState, createNode = defaultCreateNode) {
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
730
|
+
return mapValues(
|
731
|
+
schema.nodes,
|
732
|
+
(type) => createNodeAction(type, getState, createNode)
|
733
|
+
);
|
734
|
+
}
|
735
|
+
function createNodeAction(type, getState, createNode) {
|
736
|
+
const action = (...args) => buildNode(type, args, createNode);
|
737
|
+
action.isActive = (attrs) => {
|
738
|
+
const state = getState();
|
739
|
+
return state ? isNodeActive(state, type, attrs) : false;
|
740
|
+
};
|
741
|
+
return action;
|
686
742
|
}
|
687
743
|
function createMarkActions(schema, getState, applyMark = defaultApplyMark) {
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
744
|
+
return mapValues(
|
745
|
+
schema.marks,
|
746
|
+
(type) => createMarkAction(type, getState, applyMark)
|
747
|
+
);
|
748
|
+
}
|
749
|
+
function createMarkAction(type, getState, applyMark) {
|
750
|
+
const action = (...args) => buildMark(type, args, applyMark);
|
751
|
+
action.isActive = (attrs) => {
|
752
|
+
const state = getState();
|
753
|
+
return state ? isMarkActive(state, type, attrs) : false;
|
754
|
+
};
|
755
|
+
return action;
|
698
756
|
}
|
699
757
|
function buildMark(type, args, applyMark) {
|
700
758
|
const [attrs, children] = normalizeArgs(args);
|
@@ -702,7 +760,7 @@ function buildMark(type, args, applyMark) {
|
|
702
760
|
return applyMark(mark, flattenChildren(type.schema, children));
|
703
761
|
}
|
704
762
|
var defaultApplyMark = (mark, children) => {
|
705
|
-
return children.map((
|
763
|
+
return children.map((node) => node.mark(mark.addToSet(node.marks)));
|
706
764
|
};
|
707
765
|
function buildNode(type, args, createNode) {
|
708
766
|
const [attrs, children] = normalizeArgs(args);
|
@@ -750,31 +808,35 @@ function isNodeChild(value) {
|
|
750
808
|
|
751
809
|
// src/editor/editor.ts
|
752
810
|
function setupEditorExtension(options) {
|
753
|
-
|
754
|
-
|
755
|
-
return union([
|
811
|
+
if (options.defaultContent || options.defaultDoc || options.defaultHTML) {
|
812
|
+
return union(
|
756
813
|
options.extension,
|
757
|
-
defineDefaultState(
|
758
|
-
|
759
|
-
defaultHTML,
|
760
|
-
defaultSelection
|
761
|
-
})
|
762
|
-
]);
|
814
|
+
defineDefaultState(options)
|
815
|
+
);
|
763
816
|
}
|
764
817
|
return options.extension;
|
765
818
|
}
|
766
819
|
function createEditor(options) {
|
767
820
|
const extension = setupEditorExtension(options);
|
768
|
-
|
821
|
+
const instance = new EditorInstance(extension);
|
822
|
+
return new Editor(instance);
|
769
823
|
}
|
770
824
|
var EditorInstance = class {
|
771
825
|
constructor(extension) {
|
772
826
|
this.view = null;
|
773
827
|
this.commands = {};
|
828
|
+
this.afterMounted = [];
|
774
829
|
this.getState = () => {
|
775
830
|
var _a;
|
776
831
|
return ((_a = this.view) == null ? void 0 : _a.state) || this.directEditorProps.state;
|
777
832
|
};
|
833
|
+
this.dispatch = (tr) => {
|
834
|
+
if (this.view) {
|
835
|
+
this.view.dispatch(tr);
|
836
|
+
} else {
|
837
|
+
this.directEditorProps.state = this.directEditorProps.state.apply(tr);
|
838
|
+
}
|
839
|
+
};
|
778
840
|
this.tree = extension.getTree();
|
779
841
|
const payload = this.tree.getRootOutput();
|
780
842
|
const schema = payload.schema;
|
@@ -798,6 +860,21 @@ var EditorInstance = class {
|
|
798
860
|
this.directEditorProps.state = state;
|
799
861
|
}
|
800
862
|
}
|
863
|
+
setContent(content, selection) {
|
864
|
+
const doc = getEditorContentDoc(this.schema, content);
|
865
|
+
doc.check();
|
866
|
+
const sel = getEditorSelection(doc, selection || "start");
|
867
|
+
const oldState = this.getState();
|
868
|
+
if (doc.eq(oldState.doc) && (!selection || sel.eq(oldState.selection))) {
|
869
|
+
return;
|
870
|
+
}
|
871
|
+
const newState = EditorState2.create({
|
872
|
+
doc,
|
873
|
+
selection: sel,
|
874
|
+
plugins: oldState.plugins
|
875
|
+
});
|
876
|
+
this.updateState(newState);
|
877
|
+
}
|
801
878
|
updateExtension(extension, add) {
|
802
879
|
var _a, _b, _c, _d;
|
803
880
|
const view = this.view;
|
@@ -829,19 +906,33 @@ var EditorInstance = class {
|
|
829
906
|
}
|
830
907
|
}
|
831
908
|
}
|
909
|
+
use(extension) {
|
910
|
+
if (!this.mounted) {
|
911
|
+
let canceled = false;
|
912
|
+
let lazyRemove = null;
|
913
|
+
const lazyCreate = () => {
|
914
|
+
if (!canceled) {
|
915
|
+
lazyRemove = this.use(extension);
|
916
|
+
}
|
917
|
+
};
|
918
|
+
this.afterMounted.push(lazyCreate);
|
919
|
+
return () => {
|
920
|
+
canceled = true;
|
921
|
+
lazyRemove == null ? void 0 : lazyRemove();
|
922
|
+
};
|
923
|
+
}
|
924
|
+
this.updateExtension(extension, true);
|
925
|
+
return () => this.updateExtension(extension, false);
|
926
|
+
}
|
832
927
|
mount(place) {
|
833
928
|
if (this.view) {
|
834
929
|
throw new ProseKitError("Editor is already mounted");
|
835
930
|
}
|
836
|
-
if (!place) {
|
837
|
-
throw new ProseKitError("Can't mount editor without a place");
|
838
|
-
}
|
839
931
|
this.view = new EditorView({ mount: place }, this.directEditorProps);
|
932
|
+
this.afterMounted.forEach((callback) => callback());
|
840
933
|
}
|
841
934
|
unmount() {
|
842
|
-
if (!this.view)
|
843
|
-
throw new ProseKitError("Editor is not mounted yet");
|
844
|
-
}
|
935
|
+
if (!this.view) return;
|
845
936
|
this.directEditorProps.state = this.view.state;
|
846
937
|
this.view.destroy();
|
847
938
|
this.view = null;
|
@@ -870,46 +961,122 @@ var EditorInstance = class {
|
|
870
961
|
const newState = state.reconfigure({ plugins: newPlugins });
|
871
962
|
view.setProps({ state: newState });
|
872
963
|
}
|
964
|
+
exec(command) {
|
965
|
+
var _a;
|
966
|
+
const state = this.getState();
|
967
|
+
return command(state, this.dispatch, (_a = this.view) != null ? _a : void 0);
|
968
|
+
}
|
969
|
+
canExec(command) {
|
970
|
+
var _a;
|
971
|
+
const state = this.getState();
|
972
|
+
return command(state, void 0, (_a = this.view) != null ? _a : void 0);
|
973
|
+
}
|
873
974
|
defineCommand(name, commandCreator) {
|
874
975
|
const action = (...args) => {
|
875
|
-
const view = this.view;
|
876
|
-
assert(view, `Cannot call command "${name}" before the editor is mounted`);
|
877
976
|
const command = commandCreator(...args);
|
878
|
-
return
|
977
|
+
return this.exec(command);
|
879
978
|
};
|
880
|
-
|
881
|
-
const view = this.view;
|
882
|
-
if (!view) {
|
883
|
-
return false;
|
884
|
-
}
|
979
|
+
const canExec = (...args) => {
|
885
980
|
const command = commandCreator(...args);
|
886
|
-
return command
|
981
|
+
return this.canExec(command);
|
887
982
|
};
|
983
|
+
action.canApply = canExec;
|
984
|
+
action.canExec = canExec;
|
888
985
|
this.commands[name] = action;
|
889
986
|
}
|
890
987
|
removeCommand(name) {
|
891
988
|
delete this.commands[name];
|
892
989
|
}
|
893
990
|
};
|
894
|
-
var Editor = class
|
991
|
+
var Editor = class {
|
895
992
|
/**
|
896
993
|
* @internal
|
897
994
|
*/
|
898
995
|
constructor(instance) {
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
this.
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
996
|
+
/**
|
997
|
+
* Mount the editor to the given HTML element.
|
998
|
+
* Pass `null` or `undefined` to unmount the editor.
|
999
|
+
*/
|
1000
|
+
this.mount = (place) => {
|
1001
|
+
if (place) {
|
1002
|
+
this.instance.mount(place);
|
1003
|
+
} else {
|
1004
|
+
this.instance.unmount();
|
1005
|
+
}
|
1006
|
+
};
|
1007
|
+
/**
|
1008
|
+
* Unmount the editor. This is equivalent to `mount(null)`.
|
1009
|
+
*/
|
1010
|
+
this.unmount = () => {
|
1011
|
+
this.instance.unmount();
|
1012
|
+
};
|
1013
|
+
/**
|
1014
|
+
* Focus the editor.
|
1015
|
+
*/
|
1016
|
+
this.focus = () => {
|
1017
|
+
var _a;
|
1018
|
+
(_a = this.instance.view) == null ? void 0 : _a.focus();
|
1019
|
+
};
|
1020
|
+
/**
|
1021
|
+
* Blur the editor.
|
1022
|
+
*/
|
1023
|
+
this.blur = () => {
|
1024
|
+
var _a;
|
1025
|
+
(_a = this.instance.view) == null ? void 0 : _a.dom.blur();
|
1026
|
+
};
|
1027
|
+
/**
|
1028
|
+
* Register an extension to the editor. Return a function to unregister the
|
1029
|
+
* extension.
|
1030
|
+
*/
|
1031
|
+
this.use = (extension) => {
|
1032
|
+
return this.instance.use(extension);
|
1033
|
+
};
|
1034
|
+
/**
|
1035
|
+
* Update the editor's state.
|
1036
|
+
*
|
1037
|
+
* @remarks
|
1038
|
+
*
|
1039
|
+
* This is an advanced method. Use it only if you have a specific reason to
|
1040
|
+
* directly manipulate the editor's state.
|
1041
|
+
*/
|
1042
|
+
this.updateState = (state) => {
|
1043
|
+
this.instance.updateState(state);
|
1044
|
+
};
|
1045
|
+
/**
|
1046
|
+
* Update the editor's document and selection.
|
1047
|
+
*
|
1048
|
+
* @param content - The new document to set. It can be one of the following:
|
1049
|
+
* - A ProseMirror node instance
|
1050
|
+
* - A ProseMirror node JSON object
|
1051
|
+
* - An HTML string
|
1052
|
+
* - An HTML element instance
|
1053
|
+
* @param selection - Optional. Specifies the new selection. It can be one of the following:
|
1054
|
+
* - A ProseMirror selection instance
|
1055
|
+
* - A ProseMirror selection JSON object
|
1056
|
+
* - The string "start" (to set selection at the beginning, default value)
|
1057
|
+
* - The string "end" (to set selection at the end)
|
1058
|
+
*/
|
1059
|
+
this.setContent = (content, selection) => {
|
1060
|
+
return this.instance.setContent(content, selection);
|
1061
|
+
};
|
1062
|
+
/**
|
1063
|
+
* Execute the given command. Return `true` if the command was successfully
|
1064
|
+
* executed, otherwise `false`.
|
1065
|
+
*/
|
1066
|
+
this.exec = (command) => {
|
1067
|
+
return this.instance.exec(command);
|
1068
|
+
};
|
1069
|
+
/**
|
1070
|
+
* Check if the given command can be executed. Return `true` if the command
|
1071
|
+
* can be executed, otherwise `false`.
|
1072
|
+
*/
|
1073
|
+
this.canExec = (command) => {
|
1074
|
+
return this.instance.canExec(command);
|
1075
|
+
};
|
909
1076
|
if (!(instance instanceof EditorInstance)) {
|
910
1077
|
throw new TypeError("Invalid EditorInstance");
|
911
1078
|
}
|
912
|
-
|
1079
|
+
this.instance = instance;
|
913
1080
|
}
|
914
1081
|
/**
|
915
1082
|
* Whether the editor is mounted.
|
@@ -929,68 +1096,6 @@ var Editor = class _Editor {
|
|
929
1096
|
get schema() {
|
930
1097
|
return this.instance.schema;
|
931
1098
|
}
|
932
|
-
/**
|
933
|
-
* Whether the editor is focused.
|
934
|
-
*/
|
935
|
-
get focused() {
|
936
|
-
var _a, _b;
|
937
|
-
return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : false;
|
938
|
-
}
|
939
|
-
/**
|
940
|
-
* Mount the editor to the given HTML element.
|
941
|
-
* Pass `null` or `undefined` to unmount the editor.
|
942
|
-
*/
|
943
|
-
mount(place) {
|
944
|
-
if (!place) {
|
945
|
-
return this.unmount();
|
946
|
-
}
|
947
|
-
this.instance.mount(place);
|
948
|
-
this.afterMounted.forEach((callback) => callback());
|
949
|
-
}
|
950
|
-
/**
|
951
|
-
* Unmount the editor. This is equivalent to `mount(null)`.
|
952
|
-
*/
|
953
|
-
unmount() {
|
954
|
-
if (this.mounted) {
|
955
|
-
this.instance.unmount();
|
956
|
-
}
|
957
|
-
}
|
958
|
-
/**
|
959
|
-
* Focus the editor.
|
960
|
-
*/
|
961
|
-
focus() {
|
962
|
-
var _a;
|
963
|
-
(_a = this.instance.view) == null ? void 0 : _a.focus();
|
964
|
-
}
|
965
|
-
/**
|
966
|
-
* Blur the editor.
|
967
|
-
*/
|
968
|
-
blur() {
|
969
|
-
var _a;
|
970
|
-
(_a = this.instance.view) == null ? void 0 : _a.dom.blur();
|
971
|
-
}
|
972
|
-
/**
|
973
|
-
* Register an extension to the editor. Return a function to unregister the
|
974
|
-
* extension.
|
975
|
-
*/
|
976
|
-
use(extension) {
|
977
|
-
if (!this.mounted) {
|
978
|
-
let canceled = false;
|
979
|
-
let lazyRemove = null;
|
980
|
-
const lazyCreate = () => {
|
981
|
-
if (!canceled) {
|
982
|
-
lazyRemove = this.use(extension);
|
983
|
-
}
|
984
|
-
};
|
985
|
-
this.afterMounted.push(lazyCreate);
|
986
|
-
return () => {
|
987
|
-
canceled = true;
|
988
|
-
lazyRemove == null ? void 0 : lazyRemove();
|
989
|
-
};
|
990
|
-
}
|
991
|
-
this.instance.updateExtension(extension, true);
|
992
|
-
return () => this.instance.updateExtension(extension, false);
|
993
|
-
}
|
994
1099
|
/**
|
995
1100
|
* The editor's current state.
|
996
1101
|
*/
|
@@ -998,15 +1103,11 @@ var Editor = class _Editor {
|
|
998
1103
|
return this.instance.getState();
|
999
1104
|
}
|
1000
1105
|
/**
|
1001
|
-
*
|
1002
|
-
*
|
1003
|
-
* @remarks
|
1004
|
-
*
|
1005
|
-
* This is an advanced method. Use it only if you have a specific reason to
|
1006
|
-
* directly manipulate the editor's state.
|
1106
|
+
* Whether the editor is focused.
|
1007
1107
|
*/
|
1008
|
-
|
1009
|
-
|
1108
|
+
get focused() {
|
1109
|
+
var _a, _b;
|
1110
|
+
return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : false;
|
1010
1111
|
}
|
1011
1112
|
/**
|
1012
1113
|
* All {@link CommandAction}s defined by the editor.
|
@@ -1036,13 +1137,22 @@ export {
|
|
1036
1137
|
getNodeType,
|
1037
1138
|
isNodeActive,
|
1038
1139
|
Priority,
|
1039
|
-
toReversed,
|
1040
|
-
isNotNull,
|
1041
1140
|
defineFacet,
|
1042
1141
|
rootFacet,
|
1043
1142
|
schemaFacet,
|
1143
|
+
toReversed,
|
1144
|
+
isProseMirrorNode,
|
1145
|
+
isMark,
|
1146
|
+
isFragment,
|
1147
|
+
isSlice,
|
1148
|
+
isSelection,
|
1149
|
+
isTextSelection,
|
1150
|
+
isNodeSelection,
|
1151
|
+
isAllSelection,
|
1152
|
+
isNotNullish,
|
1044
1153
|
defineFacetPayload,
|
1045
1154
|
stateFacet,
|
1155
|
+
isElement,
|
1046
1156
|
jsonFromState,
|
1047
1157
|
stateFromJSON,
|
1048
1158
|
jsonFromNode,
|
@@ -1057,11 +1167,6 @@ export {
|
|
1057
1167
|
defineDefaultState,
|
1058
1168
|
isMarkAbsent,
|
1059
1169
|
isMarkActive,
|
1060
|
-
isProseMirrorNode,
|
1061
|
-
isMark,
|
1062
|
-
isTextSelection,
|
1063
|
-
isNodeSelection,
|
1064
|
-
isAllSelection,
|
1065
1170
|
createNodeActions,
|
1066
1171
|
createMarkActions,
|
1067
1172
|
union,
|