@prosekit/core 0.7.14 → 0.8.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.
@@ -76,15 +76,13 @@ var Facet = class {
76
76
  this.path = parent ? [...parent.path, this.index] : [];
77
77
  }
78
78
  get reducer() {
79
- var _a, _b;
80
- return (_b = this._reducer) != null ? _b : (_a = this._reduce) == null ? void 0 : _a.call(this);
79
+ return this._reducer ?? this._reduce?.();
81
80
  }
82
81
  };
83
82
  function defineFacet(options) {
84
- var _a;
85
83
  return new Facet(
86
84
  options.parent,
87
- (_a = options.singleton) != null ? _a : false,
85
+ options.singleton ?? false,
88
86
  options.reducer,
89
87
  options.reduce
90
88
  );
@@ -138,7 +136,6 @@ import {
138
136
 
139
137
  // src/facets/root.ts
140
138
  function rootReducer(inputs) {
141
- var _a;
142
139
  let schema;
143
140
  let commands;
144
141
  let stateFunc;
@@ -149,7 +146,7 @@ function rootReducer(inputs) {
149
146
  stateFunc = input.state || stateFunc;
150
147
  view = input.view || view;
151
148
  }
152
- const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
149
+ const state = schema && (stateFunc?.({ schema }) ?? { schema });
153
150
  return { schema, state, commands, view };
154
151
  }
155
152
  var rootFacet = new Facet(null, true, rootReducer);
@@ -169,32 +166,29 @@ var schemaFacet = defineFacet({
169
166
  // src/facets/base-extension.ts
170
167
  var BaseExtension = class {
171
168
  constructor() {
172
- this.extension = [];
173
169
  this.trees = [null, null, null, null, null];
174
170
  }
175
171
  /**
176
172
  * @internal
177
173
  */
178
174
  getTree(priority) {
179
- var _a, _b;
180
- const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
181
- return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
175
+ var _a;
176
+ const pri = priority ?? this.priority ?? 2 /* default */;
177
+ return (_a = this.trees)[pri] || (_a[pri] = this.createTree(pri));
182
178
  }
183
179
  /**
184
180
  * @internal
185
181
  */
186
182
  findFacetOutput(facet) {
187
- var _a;
188
183
  let node = this.getTree();
189
184
  for (const index of facet.path) {
190
- node = node == null ? void 0 : node.children.get(index);
185
+ node = node?.children.get(index);
191
186
  }
192
- return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
187
+ return node?.getOutput() ?? null;
193
188
  }
194
189
  get schema() {
195
- var _a, _b;
196
190
  const output = this.findFacetOutput(schemaFacet);
197
- return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
191
+ return output?.find(Boolean)?.schema ?? null;
198
192
  }
199
193
  };
200
194
 
@@ -212,8 +206,7 @@ function arraySubstract(a, b) {
212
206
  return a.filter((x) => !b.includes(x));
213
207
  }
214
208
  function toReversed(arr) {
215
- var _a, _b;
216
- return (_b = (_a = arr.toReversed) == null ? void 0 : _a.call(arr)) != null ? _b : [...arr].reverse();
209
+ return arr.toReversed?.() ?? [...arr].reverse();
217
210
  }
218
211
 
219
212
  // src/facets/facet-node.ts
@@ -228,7 +221,7 @@ function zip5(a, b, mapper) {
228
221
  }
229
222
  function unionInput(a, b) {
230
223
  if (!a && !b) return null;
231
- return uniqPush(a != null ? a : [], b != null ? b : []);
224
+ return uniqPush(a ?? [], b ?? []);
232
225
  }
233
226
  function subtractInput(a, b) {
234
227
  if (!a) return null;
@@ -354,8 +347,7 @@ var FacetExtensionImpl = class extends BaseExtension {
354
347
  * @internal
355
348
  */
356
349
  createTree(priority) {
357
- var _a;
358
- const pri = (_a = this.priority) != null ? _a : priority;
350
+ const pri = this.priority ?? priority;
359
351
  const inputs = [null, null, null, null, null];
360
352
  inputs[pri] = [...this.payloads];
361
353
  let node = new FacetNode(this.facet, inputs);
@@ -370,6 +362,17 @@ function defineFacetPayload(facet, payloads) {
370
362
  return new FacetExtensionImpl(facet, payloads);
371
363
  }
372
364
 
365
+ // src/utils/is-object.ts
366
+ function isObject(v) {
367
+ return typeof v === "object" && v != null;
368
+ }
369
+
370
+ // src/utils/is-element.ts
371
+ var ELEMENT_NODE = 1;
372
+ function isElement(el) {
373
+ return isObject(el) && el.nodeType === ELEMENT_NODE && typeof el.nodeName === "string";
374
+ }
375
+
373
376
  // src/utils/parse.ts
374
377
  import {
375
378
  DOMParser,
@@ -395,12 +398,10 @@ function findGlobalBrowserWindow() {
395
398
  }
396
399
  }
397
400
  function findBrowserDocument(options) {
398
- var _a, _b, _c;
399
- return (_c = (_a = options == null ? void 0 : options.document) != null ? _a : findGlobalBrowserDocument()) != null ? _c : (_b = findGlobalBrowserWindow()) == null ? void 0 : _b.document;
401
+ return options?.document ?? findGlobalBrowserDocument() ?? findGlobalBrowserWindow()?.document;
400
402
  }
401
403
  function findBrowserWindow(options) {
402
- var _a, _b, _c, _d, _e;
403
- return (_e = (_d = (_b = (_a = options == null ? void 0 : options.document) == null ? void 0 : _a.defaultView) != null ? _b : findGlobalBrowserWindow()) != null ? _d : (_c = findBrowserDocument(options)) == null ? void 0 : _c.defaultView) != null ? _e : void 0;
404
+ return options?.document?.defaultView ?? findGlobalBrowserWindow() ?? findBrowserDocument(options)?.defaultView ?? void 0;
404
405
  }
405
406
  function getBrowserDocument(options) {
406
407
  const doc = findBrowserDocument(options);
@@ -431,7 +432,7 @@ function nodeFromElement(element, options) {
431
432
  return (CustomDOMParser || DOMParser).fromSchema(schema).parse(element, parseOptions);
432
433
  }
433
434
  function elementFromNode(node, options) {
434
- const Serializer = (options == null ? void 0 : options.DOMSerializer) || DOMSerializer;
435
+ const Serializer = options?.DOMSerializer || DOMSerializer;
435
436
  const document2 = getBrowserDocument(options);
436
437
  const schema = node.type.schema;
437
438
  const serializer = Serializer.fromSchema(schema);
@@ -482,7 +483,6 @@ var stateFacet = defineFacet({
482
483
  reduce: () => {
483
484
  let callbacks = [];
484
485
  const state = (ctx) => {
485
- var _a, _b, _c, _d, _e, _f;
486
486
  const configs = callbacks.map((cb) => cb(ctx));
487
487
  const config = {
488
488
  schema: ctx.schema,
@@ -490,11 +490,11 @@ var stateFacet = defineFacet({
490
490
  plugins: []
491
491
  };
492
492
  for (const c of configs) {
493
- config.schema = (_a = config.schema) != null ? _a : c.schema;
494
- config.doc = (_b = config.doc) != null ? _b : c.doc;
495
- config.selection = (_c = config.selection) != null ? _c : c.selection;
496
- config.storedMarks = [...config.storedMarks, ...(_d = c.storedMarks) != null ? _d : []];
497
- config.plugins = uniqPush((_e = config.plugins) != null ? _e : [], (_f = c.plugins) != null ? _f : []);
493
+ config.schema = config.schema ?? c.schema;
494
+ config.doc = config.doc ?? c.doc;
495
+ config.selection = config.selection ?? c.selection;
496
+ config.storedMarks = [...config.storedMarks, ...c.storedMarks ?? []];
497
+ config.plugins = uniqPush(config.plugins ?? [], c.plugins ?? []);
498
498
  }
499
499
  assert(
500
500
  config.doc || config.schema,
@@ -516,19 +516,6 @@ var stateFacet = defineFacet({
516
516
 
517
517
  // src/utils/editor-content.ts
518
518
  import { Selection as Selection2 } from "@prosekit/pm/state";
519
-
520
- // src/utils/is-object.ts
521
- function isObject(v) {
522
- return typeof v === "object" && v != null;
523
- }
524
-
525
- // src/utils/is-element.ts
526
- var ELEMENT_NODE = 1;
527
- function isElement(el) {
528
- return isObject(el) && el.nodeType === ELEMENT_NODE && typeof el.nodeName === "string";
529
- }
530
-
531
- // src/utils/editor-content.ts
532
519
  function getEditorContentJSON(schema, content) {
533
520
  if (typeof content === "string") {
534
521
  return jsonFromHTML(content, { schema });
@@ -614,7 +601,7 @@ function isMarkAbsent(node, from, to, markType, attrs) {
614
601
  if (missing) {
615
602
  return false;
616
603
  }
617
- const allowed = (parent == null ? void 0 : parent.type.allowsMarkType(markType)) && !node2.marks.some((m) => m.type !== markType && m.type.excludes(markType));
604
+ const allowed = parent?.type.allowsMarkType(markType) && !node2.marks.some((m) => m.type !== markType && m.type.excludes(markType));
618
605
  if (allowed) {
619
606
  available = true;
620
607
  if (!includesMark(node2.marks, markType, attrs)) {
@@ -650,13 +637,9 @@ var UnionExtensionImpl = class extends BaseExtension {
650
637
  * @internal
651
638
  */
652
639
  createTree(priority) {
653
- var _a;
654
- const pri = (_a = this.priority) != null ? _a : priority;
640
+ const pri = this.priority ?? priority;
655
641
  const extensions = [...this.extension];
656
- extensions.sort((a, b) => {
657
- var _a2, _b;
658
- return ((_a2 = a.priority) != null ? _a2 : pri) - ((_b = b.priority) != null ? _b : pri);
659
- });
642
+ extensions.sort((a, b) => (a.priority ?? pri) - (b.priority ?? pri));
660
643
  const children = extensions.map((ext) => ext.getTree(pri));
661
644
  assert(children.length > 0);
662
645
  let node = children[0];
@@ -828,8 +811,7 @@ var EditorInstance = class {
828
811
  this.commands = {};
829
812
  this.afterMounted = [];
830
813
  this.getState = () => {
831
- var _a;
832
- return ((_a = this.view) == null ? void 0 : _a.state) || this.directEditorProps.state;
814
+ return this.view?.state || this.directEditorProps.state;
833
815
  };
834
816
  this.dispatch = (tr) => {
835
817
  if (this.view) {
@@ -838,6 +820,22 @@ var EditorInstance = class {
838
820
  this.directEditorProps.state = this.directEditorProps.state.apply(tr);
839
821
  }
840
822
  };
823
+ /**
824
+ * Return a JSON object representing the editor's current document.
825
+ */
826
+ this.getDocJSON = () => {
827
+ const state = this.getState();
828
+ return jsonFromNode(state.doc);
829
+ };
830
+ /**
831
+ * Return a HTML string representing the editor's current document.
832
+ */
833
+ this.getDocHTML = (options) => {
834
+ const serializer = this.getProp("clipboardSerializer");
835
+ const DOMSerializer2 = serializer ? { fromSchema: () => serializer } : void 0;
836
+ const doc = this.getDoc();
837
+ return htmlFromNode(doc, { ...options, DOMSerializer: DOMSerializer2 });
838
+ };
841
839
  this.tree = extension.getTree();
842
840
  const payload = this.tree.getRootOutput();
843
841
  const schema = payload.schema;
@@ -854,6 +852,12 @@ var EditorInstance = class {
854
852
  this.schema = state.schema;
855
853
  this.directEditorProps = { state, ...payload.view };
856
854
  }
855
+ getDoc() {
856
+ return this.getState().doc;
857
+ }
858
+ getProp(propName) {
859
+ return this.view?.someProp(propName) ?? this.directEditorProps[propName];
860
+ }
857
861
  updateState(state) {
858
862
  if (this.view) {
859
863
  this.view.updateState(state);
@@ -877,29 +881,28 @@ var EditorInstance = class {
877
881
  this.updateState(newState);
878
882
  }
879
883
  updateExtension(extension, add) {
880
- var _a, _b, _c, _d;
881
884
  const view = this.view;
882
885
  if (!view || view.isDestroyed) {
883
886
  return;
884
887
  }
885
888
  const tree = extension.getTree();
886
889
  const payload = tree.getRootOutput();
887
- if (payload == null ? void 0 : payload.schema) {
890
+ if (payload?.schema) {
888
891
  throw new ProseKitError("Schema cannot be changed");
889
892
  }
890
- if (payload == null ? void 0 : payload.view) {
893
+ if (payload?.view) {
891
894
  throw new ProseKitError("View cannot be changed");
892
895
  }
893
896
  const oldPayload = this.tree.getRootOutput();
894
- const oldPlugins = [...(_b = (_a = view.state) == null ? void 0 : _a.plugins) != null ? _b : []];
897
+ const oldPlugins = [...view.state?.plugins ?? []];
895
898
  this.tree = add ? unionFacetNode(this.tree, tree) : subtractFacetNode(this.tree, tree);
896
899
  const newPayload = this.tree.getRootOutput();
897
- const newPlugins = [...(_d = (_c = newPayload == null ? void 0 : newPayload.state) == null ? void 0 : _c.plugins) != null ? _d : []];
900
+ const newPlugins = [...newPayload?.state?.plugins ?? []];
898
901
  if (!deepEquals(oldPlugins, newPlugins)) {
899
902
  const state = view.state.reconfigure({ plugins: newPlugins });
900
903
  view.updateState(state);
901
904
  }
902
- if ((newPayload == null ? void 0 : newPayload.commands) && !deepEquals(oldPayload == null ? void 0 : oldPayload.commands, newPayload == null ? void 0 : newPayload.commands)) {
905
+ if (newPayload?.commands && !deepEquals(oldPayload?.commands, newPayload?.commands)) {
903
906
  const commands = newPayload.commands;
904
907
  const names = Object.keys(commands);
905
908
  for (const name of names) {
@@ -919,7 +922,7 @@ var EditorInstance = class {
919
922
  this.afterMounted.push(lazyCreate);
920
923
  return () => {
921
924
  canceled = true;
922
- lazyRemove == null ? void 0 : lazyRemove();
925
+ lazyRemove?.();
923
926
  };
924
927
  }
925
928
  this.updateExtension(extension, true);
@@ -963,14 +966,12 @@ var EditorInstance = class {
963
966
  view.setProps({ state: newState });
964
967
  }
965
968
  exec(command) {
966
- var _a;
967
969
  const state = this.getState();
968
- return command(state, this.dispatch, (_a = this.view) != null ? _a : void 0);
970
+ return command(state, this.dispatch, this.view ?? void 0);
969
971
  }
970
972
  canExec(command) {
971
- var _a;
972
973
  const state = this.getState();
973
- return command(state, void 0, (_a = this.view) != null ? _a : void 0);
974
+ return command(state, void 0, this.view ?? void 0);
974
975
  }
975
976
  defineCommand(name, commandCreator) {
976
977
  const action = (...args) => {
@@ -1015,15 +1016,13 @@ var Editor = class {
1015
1016
  * Focus the editor.
1016
1017
  */
1017
1018
  this.focus = () => {
1018
- var _a;
1019
- (_a = this.instance.view) == null ? void 0 : _a.focus();
1019
+ this.instance.view?.focus();
1020
1020
  };
1021
1021
  /**
1022
1022
  * Blur the editor.
1023
1023
  */
1024
1024
  this.blur = () => {
1025
- var _a;
1026
- (_a = this.instance.view) == null ? void 0 : _a.dom.blur();
1025
+ this.instance.view?.dom.blur();
1027
1026
  };
1028
1027
  /**
1029
1028
  * Register an extension to the editor. Return a function to unregister the
@@ -1060,6 +1059,18 @@ var Editor = class {
1060
1059
  this.setContent = (content, selection) => {
1061
1060
  return this.instance.setContent(content, selection);
1062
1061
  };
1062
+ /**
1063
+ * Return a JSON object representing the editor's current document.
1064
+ */
1065
+ this.getDocJSON = () => {
1066
+ return this.instance.getDocJSON();
1067
+ };
1068
+ /**
1069
+ * Return a HTML string representing the editor's current document.
1070
+ */
1071
+ this.getDocHTML = (options) => {
1072
+ return this.instance.getDocHTML(options);
1073
+ };
1063
1074
  /**
1064
1075
  * Execute the given command. Return `true` if the command was successfully
1065
1076
  * executed, otherwise `false`.
@@ -1107,8 +1118,7 @@ var Editor = class {
1107
1118
  * Whether the editor is focused.
1108
1119
  */
1109
1120
  get focused() {
1110
- var _a, _b;
1111
- return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : false;
1121
+ return this.instance.view?.hasFocus() ?? false;
1112
1122
  }
1113
1123
  /**
1114
1124
  * All {@link CommandAction}s defined by the editor.
@@ -6,7 +6,7 @@ import {
6
6
  createNodeActions,
7
7
  isProseMirrorNode,
8
8
  setupEditorExtension
9
- } from "./chunk-6DIFWJEG.js";
9
+ } from "./chunk-B3WEP4DD.js";
10
10
 
11
11
  // src/test/test-editor.ts
12
12
  import {
@@ -72,8 +72,8 @@ function maybeResolve(doc, pos) {
72
72
  }
73
73
  function getSelection(doc) {
74
74
  const tags = doc.tags;
75
- const $a = maybeResolve(doc, tags == null ? void 0 : tags.a);
76
- const $b = maybeResolve(doc, tags == null ? void 0 : tags.b);
75
+ const $a = maybeResolve(doc, tags?.a);
76
+ const $b = maybeResolve(doc, tags?.b);
77
77
  if ($a) {
78
78
  if ($a.parent.inlineContent) {
79
79
  return new TextSelection($a, $b);
@@ -38,6 +38,8 @@ export { union } from './_tsup-dts-rollup.js';
38
38
  export { withPriority } from './_tsup-dts-rollup.js';
39
39
  export { EditorNotFoundError_alias_1 as EditorNotFoundError } from './_tsup-dts-rollup.js';
40
40
  export { ProseKitError_alias_1 as ProseKitError } from './_tsup-dts-rollup.js';
41
+ export { defineClipboardSerializer } from './_tsup-dts-rollup.js';
42
+ export { ClipboardSerializerOptions } from './_tsup-dts-rollup.js';
41
43
  export { defineBaseCommands } from './_tsup-dts-rollup.js';
42
44
  export { defineCommands } from './_tsup-dts-rollup.js';
43
45
  export { BaseCommandsExtension } from './_tsup-dts-rollup.js';
@@ -168,6 +170,7 @@ export { _getId } from './_tsup-dts-rollup.js';
168
170
  export { getMarkType } from './_tsup-dts-rollup.js';
169
171
  export { getNodeType } from './_tsup-dts-rollup.js';
170
172
  export { isAtBlockStart } from './_tsup-dts-rollup.js';
173
+ export { isElement } from './_tsup-dts-rollup.js';
171
174
  export { isInCodeBlock } from './_tsup-dts-rollup.js';
172
175
  export { isMarkAbsent } from './_tsup-dts-rollup.js';
173
176
  export { isMarkActive } from './_tsup-dts-rollup.js';