@prosekit/core 0.7.7 → 0.7.9

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.
@@ -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
- import "@prosekit/pm/model";
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(isNotNull).flat();
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 { DOMParser, DOMSerializer } from "@prosekit/pm/model";
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
@@ -440,33 +469,6 @@ function htmlFromJSON(json, options) {
440
469
  return htmlFromElement(elementFromJSON(json, options));
441
470
  }
442
471
 
443
- // src/utils/type-assertion.ts
444
- import { Mark, ProseMirrorNode } from "@prosekit/pm/model";
445
- import {
446
- AllSelection,
447
- NodeSelection,
448
- TextSelection,
449
- Selection
450
- } from "@prosekit/pm/state";
451
- function isProseMirrorNode(node) {
452
- return node instanceof ProseMirrorNode;
453
- }
454
- function isMark(mark) {
455
- return mark instanceof Mark;
456
- }
457
- function isSelection(sel) {
458
- return sel instanceof Selection;
459
- }
460
- function isTextSelection(sel) {
461
- return sel instanceof TextSelection;
462
- }
463
- function isNodeSelection(sel) {
464
- return sel instanceof NodeSelection;
465
- }
466
- function isAllSelection(sel) {
467
- return sel instanceof AllSelection;
468
- }
469
-
470
472
  // src/extensions/default-state.ts
471
473
  import { Selection as Selection3 } from "@prosekit/pm/state";
472
474
 
@@ -564,8 +566,12 @@ function getEditorSelection(doc, selection) {
564
566
  }
565
567
 
566
568
  // src/extensions/default-state.ts
567
- function defineDefaultState(options) {
568
- const { defaultSelection, defaultContent, defaultDoc, defaultHTML } = options;
569
+ function defineDefaultState({
570
+ defaultSelection,
571
+ defaultContent,
572
+ defaultDoc,
573
+ defaultHTML
574
+ }) {
569
575
  const defaultDocContent = defaultContent || defaultDoc || defaultHTML;
570
576
  return defineFacetPayload(stateFacet, [
571
577
  ({ schema }) => {
@@ -664,8 +670,9 @@ function union(...exts) {
664
670
  }
665
671
 
666
672
  // src/editor/editor.ts
667
- import "@prosekit/pm/model";
668
- import { EditorState as EditorState2 } from "@prosekit/pm/state";
673
+ import {
674
+ EditorState as EditorState2
675
+ } from "@prosekit/pm/state";
669
676
  import { EditorView } from "@prosekit/pm/view";
670
677
 
671
678
  // src/utils/deep-equals.ts
@@ -692,7 +699,6 @@ function deepEquals(a, b) {
692
699
  }
693
700
 
694
701
  // src/editor/action.ts
695
- import "@prosekit/pm/model";
696
702
  import mapValues from "just-map-values";
697
703
 
698
704
  // src/utils/attrs-match.ts
@@ -803,10 +809,10 @@ function isNodeChild(value) {
803
809
  // src/editor/editor.ts
804
810
  function setupEditorExtension(options) {
805
811
  if (options.defaultContent || options.defaultDoc || options.defaultHTML) {
806
- return union([
812
+ return union(
807
813
  options.extension,
808
814
  defineDefaultState(options)
809
- ]);
815
+ );
810
816
  }
811
817
  return options.extension;
812
818
  }
@@ -824,6 +830,13 @@ var EditorInstance = class {
824
830
  var _a;
825
831
  return ((_a = this.view) == null ? void 0 : _a.state) || this.directEditorProps.state;
826
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
+ };
827
840
  this.tree = extension.getTree();
828
841
  const payload = this.tree.getRootOutput();
829
842
  const schema = payload.schema;
@@ -948,21 +961,27 @@ var EditorInstance = class {
948
961
  const newState = state.reconfigure({ plugins: newPlugins });
949
962
  view.setProps({ state: newState });
950
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
+ }
951
974
  defineCommand(name, commandCreator) {
952
975
  const action = (...args) => {
953
- const view = this.view;
954
- assert(view, `Cannot call command "${name}" before the editor is mounted`);
955
976
  const command = commandCreator(...args);
956
- return command(view.state, view.dispatch.bind(view), view);
977
+ return this.exec(command);
957
978
  };
958
- action.canApply = (...args) => {
959
- const view = this.view;
960
- if (!view) {
961
- return false;
962
- }
979
+ const canExec = (...args) => {
963
980
  const command = commandCreator(...args);
964
- return command(view.state, void 0, view);
981
+ return this.canExec(command);
965
982
  };
983
+ action.canApply = canExec;
984
+ action.canExec = canExec;
966
985
  this.commands[name] = action;
967
986
  }
968
987
  removeCommand(name) {
@@ -1040,6 +1059,20 @@ var Editor = class {
1040
1059
  this.setContent = (content, selection) => {
1041
1060
  return this.instance.setContent(content, selection);
1042
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
+ };
1043
1076
  if (!(instance instanceof EditorInstance)) {
1044
1077
  throw new TypeError("Invalid EditorInstance");
1045
1078
  }
@@ -1104,11 +1137,19 @@ export {
1104
1137
  getNodeType,
1105
1138
  isNodeActive,
1106
1139
  Priority,
1107
- toReversed,
1108
- isNotNull,
1109
1140
  defineFacet,
1110
1141
  rootFacet,
1111
1142
  schemaFacet,
1143
+ toReversed,
1144
+ isProseMirrorNode,
1145
+ isMark,
1146
+ isFragment,
1147
+ isSlice,
1148
+ isSelection,
1149
+ isTextSelection,
1150
+ isNodeSelection,
1151
+ isAllSelection,
1152
+ isNotNullish,
1112
1153
  defineFacetPayload,
1113
1154
  stateFacet,
1114
1155
  isElement,
@@ -1123,12 +1164,6 @@ export {
1123
1164
  elementFromJSON,
1124
1165
  jsonFromHTML,
1125
1166
  htmlFromJSON,
1126
- isProseMirrorNode,
1127
- isMark,
1128
- isSelection,
1129
- isTextSelection,
1130
- isNodeSelection,
1131
- isAllSelection,
1132
1167
  defineDefaultState,
1133
1168
  isMarkAbsent,
1134
1169
  isMarkActive,
@@ -6,10 +6,13 @@ import {
6
6
  createNodeActions,
7
7
  isProseMirrorNode,
8
8
  setupEditorExtension
9
- } from "./chunk-MDJ2B4IL.js";
9
+ } from "./chunk-RNBMCB5M.js";
10
10
 
11
11
  // src/test/test-editor.ts
12
- import { NodeSelection, TextSelection } from "@prosekit/pm/state";
12
+ import {
13
+ NodeSelection,
14
+ TextSelection
15
+ } from "@prosekit/pm/state";
13
16
 
14
17
  // src/test/test-builder.ts
15
18
  var createNodeForTest = (type, attrs, children) => {
@@ -142,11 +142,15 @@ export { SimplifyUnion } from './_tsup-dts-rollup';
142
142
  export { assert } from './_tsup-dts-rollup';
143
143
  export { canUseRegexLookbehind } from './_tsup-dts-rollup';
144
144
  export { clsx } from './_tsup-dts-rollup';
145
+ export { collectChildren } from './_tsup-dts-rollup';
145
146
  export { collectNodes } from './_tsup-dts-rollup';
146
147
  export { NodeContent } from './_tsup-dts-rollup';
147
148
  export { containsInlineNode } from './_tsup-dts-rollup';
148
149
  export { defaultBlockAt } from './_tsup-dts-rollup';
149
150
  export { isApple } from './_tsup-dts-rollup';
151
+ export { findParentNode } from './_tsup-dts-rollup';
152
+ export { FindParentNodeResult } from './_tsup-dts-rollup';
153
+ export { findParentNodeOfType } from './_tsup-dts-rollup';
150
154
  export { _getId } from './_tsup-dts-rollup';
151
155
  export { getMarkType } from './_tsup-dts-rollup';
152
156
  export { getNodeType } from './_tsup-dts-rollup';
@@ -171,10 +175,12 @@ export { DOMParserOptions } from './_tsup-dts-rollup';
171
175
  export { DOMSerializerOptions } from './_tsup-dts-rollup';
172
176
  export { JSONParserOptions } from './_tsup-dts-rollup';
173
177
  export { isAllSelection } from './_tsup-dts-rollup';
178
+ export { isFragment } from './_tsup-dts-rollup';
174
179
  export { isMark } from './_tsup-dts-rollup';
175
180
  export { isNodeSelection } from './_tsup-dts-rollup';
176
181
  export { isProseMirrorNode } from './_tsup-dts-rollup';
177
182
  export { isSelection } from './_tsup-dts-rollup';
183
+ export { isSlice } from './_tsup-dts-rollup';
178
184
  export { isTextSelection } from './_tsup-dts-rollup';
179
185
  export { withSkipCodeBlock } from './_tsup-dts-rollup';
180
186
  export { OBJECT_REPLACEMENT_CHARACTER } from './_tsup-dts-rollup';