@prosekit/core 0.3.1 → 0.3.2

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.
@@ -598,6 +598,13 @@ declare type DocChangeHandler = (view: EditorView, prevState: EditorState) => vo
598
598
  export { DocChangeHandler }
599
599
  export { DocChangeHandler as DocChangeHandler_alias_1 }
600
600
 
601
+ /**
602
+ * @internal
603
+ */
604
+ export declare class DOMDocumentNotFoundError extends ProseKitError {
605
+ constructor();
606
+ }
607
+
601
608
  /**
602
609
  * @internal
603
610
  */
@@ -620,6 +627,8 @@ export { DOMEventHandler as DOMEventHandler_alias_1 }
620
627
  */
621
628
  export declare type DOMEventPayload = [event: string, handler: DOMEventHandler];
622
629
 
630
+ export declare type DOMNode = InstanceType<typeof window.Node>;
631
+
623
632
  declare type DoubleClickHandler = (view: EditorView, pos: number, event: MouseEvent) => boolean | void;
624
633
  export { DoubleClickHandler }
625
634
  export { DoubleClickHandler as DoubleClickHandler_alias_1 }
@@ -726,7 +735,31 @@ export { EditorOptions as EditorOptions_alias_1 }
726
735
  *
727
736
  * @internal
728
737
  */
729
- export declare function elementFromHTML(html: string): HTMLElement;
738
+ export declare function elementFromHTML(html: string, options?: {
739
+ document?: Document;
740
+ }): HTMLElement;
741
+
742
+ /**
743
+ * Parse a ProseMirror document JSON object to a HTML element.
744
+ *
745
+ * @public
746
+ */
747
+ declare function elementFromJSON(json: NodeJSON, schema: Schema, options?: {
748
+ document?: Document;
749
+ }): HTMLElement;
750
+ export { elementFromJSON }
751
+ export { elementFromJSON as elementFromJSON_alias_1 }
752
+
753
+ /**
754
+ * Serialize a ProseMirror node to a HTML element.
755
+ *
756
+ * @public
757
+ */
758
+ declare function elementFromNode(node: ProseMirrorNode, options?: {
759
+ document?: Document;
760
+ }): HTMLElement;
761
+ export { elementFromNode }
762
+ export { elementFromNode as elementFromNode_alias_1 }
730
763
 
731
764
  declare type EmptyValue = undefined | null | EmptyObject;
732
765
 
@@ -890,6 +923,14 @@ declare interface FacetOptions<Input, Output> {
890
923
  export { FacetOptions }
891
924
  export { FacetOptions as FacetOptions_alias_1 }
892
925
 
926
+ export declare function findBrowserDocument(options?: {
927
+ document?: Document;
928
+ }): Document | undefined;
929
+
930
+ export declare function findBrowserWindow(options?: {
931
+ document?: Document;
932
+ }): (Window & typeof globalThis) | null | undefined;
933
+
893
934
  /**
894
935
  * A function that is called when the editor gains or loses focus.
895
936
  *
@@ -901,9 +942,13 @@ declare type FocusChangeHandler = (hasFocus: boolean) => void;
901
942
  export { FocusChangeHandler }
902
943
  export { FocusChangeHandler as FocusChangeHandler_alias_1 }
903
944
 
904
- export declare function getBrowserDocument(): Document | undefined;
945
+ export declare function getBrowserDocument(options?: {
946
+ document?: Document;
947
+ }): Document;
905
948
 
906
- export declare function getBrowserWindow(): (Window & typeof globalThis) | null | undefined;
949
+ export declare function getBrowserWindow(options?: {
950
+ document?: Document;
951
+ }): Window & typeof globalThis;
907
952
 
908
953
  export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_3;
909
954
 
@@ -938,6 +983,31 @@ export declare type GroupedEntries<T extends Record<string, any>> = {
938
983
 
939
984
  export declare function groupEntries<T extends Record<string, any>>(entries: ObjectEntries<T>[]): GroupedEntries<T>;
940
985
 
986
+ /**
987
+ * @internal
988
+ */
989
+ export declare function htmlFromElement(element: HTMLElement): string;
990
+
991
+ /**
992
+ * Parse a ProseMirror document JSON object to a HTML string.
993
+ *
994
+ * @public
995
+ */
996
+ declare function htmlFromJSON(json: NodeJSON, schema: Schema, options?: {
997
+ document?: Document;
998
+ }): string;
999
+ export { htmlFromJSON }
1000
+ export { htmlFromJSON as htmlFromJSON_alias_1 }
1001
+
1002
+ /**
1003
+ * Serialize a ProseMirror node to a HTML string
1004
+ *
1005
+ * @public
1006
+ */
1007
+ declare function htmlFromNode(node: ProseMirrorNode): string;
1008
+ export { htmlFromNode }
1009
+ export { htmlFromNode as htmlFromNode_alias_1 }
1010
+
941
1011
  /**
942
1012
  * Returns a command that inserts the given node at the current selection or at
943
1013
  * the given position.
@@ -1032,20 +1102,20 @@ export { isTextSelection }
1032
1102
  export { isTextSelection as isTextSelection_alias_1 }
1033
1103
 
1034
1104
  /**
1035
- * Parse a HTML element to a ProseMirror document JSON.
1105
+ * Serialize a HTML element to a ProseMirror document JSON object.
1036
1106
  *
1037
1107
  * @public
1038
1108
  */
1039
- declare function jsonFromElement(element: HTMLElement, schema: Schema): NodeJSON;
1040
- export { jsonFromElement }
1041
- export { jsonFromElement as jsonFromElement_alias_1 }
1109
+ export declare function jsonFromElement(element: DOMNode, schema: Schema): NodeJSON;
1042
1110
 
1043
1111
  /**
1044
- * Parse a HTML string to a ProseMirror document JSON.
1112
+ * Parse a HTML string to a ProseMirror document JSON object.
1045
1113
  *
1046
1114
  * @public
1047
1115
  */
1048
- declare function jsonFromHTML(html: string, schema: Schema): NodeJSON;
1116
+ declare function jsonFromHTML(html: string, schema: Schema, options?: {
1117
+ document?: Document;
1118
+ }): NodeJSON;
1049
1119
  export { jsonFromHTML }
1050
1120
  export { jsonFromHTML as jsonFromHTML_alias_1 }
1051
1121
 
@@ -1199,7 +1269,7 @@ export declare type NodeContent = ProseMirrorNode | ProseMirrorFragment | NodeCo
1199
1269
  *
1200
1270
  * @public
1201
1271
  */
1202
- declare function nodeFromElement(element: HTMLElement, schema: Schema): ProseMirrorNode;
1272
+ declare function nodeFromElement(element: DOMNode, schema: Schema): ProseMirrorNode;
1203
1273
  export { nodeFromElement }
1204
1274
  export { nodeFromElement as nodeFromElement_alias_1 }
1205
1275
 
@@ -1208,7 +1278,9 @@ export { nodeFromElement as nodeFromElement_alias_1 }
1208
1278
  *
1209
1279
  * @public
1210
1280
  */
1211
- declare function nodeFromHTML(html: string, schema: Schema): ProseMirrorNode;
1281
+ declare function nodeFromHTML(html: string, schema: Schema, options?: {
1282
+ document?: Document;
1283
+ }): ProseMirrorNode;
1212
1284
  export { nodeFromHTML }
1213
1285
  export { nodeFromHTML as nodeFromHTML_alias_1 }
1214
1286
 
@@ -99,7 +99,10 @@ export { _getId } from './_tsup-dts-rollup';
99
99
  export { getMarkType } from './_tsup-dts-rollup';
100
100
  export { getNodeType } from './_tsup-dts-rollup';
101
101
  export { isInCodeBlock } from './_tsup-dts-rollup';
102
- export { jsonFromElement } from './_tsup-dts-rollup';
102
+ export { elementFromJSON } from './_tsup-dts-rollup';
103
+ export { elementFromNode } from './_tsup-dts-rollup';
104
+ export { htmlFromJSON } from './_tsup-dts-rollup';
105
+ export { htmlFromNode } from './_tsup-dts-rollup';
103
106
  export { jsonFromHTML } from './_tsup-dts-rollup';
104
107
  export { jsonFromNode } from './_tsup-dts-rollup';
105
108
  export { jsonFromState } from './_tsup-dts-rollup';
@@ -15,6 +15,13 @@ var EditorNotFoundError = class extends ProseKitError {
15
15
  );
16
16
  }
17
17
  };
18
+ var DOMDocumentNotFoundError = class extends ProseKitError {
19
+ constructor() {
20
+ super(
21
+ "Unable to find browser Document. When not in the browser environment, you need to pass a DOM Document."
22
+ );
23
+ }
24
+ };
18
25
 
19
26
  // src/utils/get-mark-type.ts
20
27
  function getMarkType(schema, type) {
@@ -433,11 +440,11 @@ var stateFacet = Facet.defineRootFacet({
433
440
  });
434
441
 
435
442
  // src/utils/parse.ts
436
- import { DOMParser } from "@prosekit/pm/model";
443
+ import { DOMParser, DOMSerializer } from "@prosekit/pm/model";
437
444
  import { EditorState } from "@prosekit/pm/state";
438
445
 
439
446
  // src/utils/get-dom-api.ts
440
- function getGlobalBrowserDocument() {
447
+ function findGlobalBrowserDocument() {
441
448
  if (typeof document !== "undefined") {
442
449
  return document;
443
450
  }
@@ -445,7 +452,7 @@ function getGlobalBrowserDocument() {
445
452
  return globalThis.document;
446
453
  }
447
454
  }
448
- function getGlobalBrowserWindow() {
455
+ function findGlobalBrowserWindow() {
449
456
  if (typeof window !== "undefined") {
450
457
  return window;
451
458
  }
@@ -453,49 +460,78 @@ function getGlobalBrowserWindow() {
453
460
  return globalThis.window;
454
461
  }
455
462
  }
456
- function getBrowserWindow() {
457
- var _a;
458
- const win = getGlobalBrowserWindow();
459
- if (win) {
463
+ function findBrowserDocument(options) {
464
+ var _a, _b, _c;
465
+ return (_c = (_a = options == null ? void 0 : options.document) != null ? _a : findGlobalBrowserDocument()) != null ? _c : (_b = findGlobalBrowserWindow()) == null ? void 0 : _b.document;
466
+ }
467
+ function findBrowserWindow(options) {
468
+ var _a, _b, _c, _d;
469
+ return (_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;
470
+ }
471
+ function getBrowserDocument(options) {
472
+ const doc = findBrowserDocument(options);
473
+ if (doc)
474
+ return doc;
475
+ throw new DOMDocumentNotFoundError();
476
+ }
477
+ function getBrowserWindow(options) {
478
+ const win = findBrowserWindow(options);
479
+ if (win)
460
480
  return win;
461
- }
462
- return (_a = getGlobalBrowserDocument()) == null ? void 0 : _a.defaultView;
481
+ throw new DOMDocumentNotFoundError();
463
482
  }
464
483
 
465
484
  // src/utils/parse.ts
466
- function nodeFromElement(element, schema) {
467
- return DOMParser.fromSchema(schema).parse(element);
485
+ function jsonFromState(state) {
486
+ return state.toJSON();
468
487
  }
469
- function jsonFromElement(element, schema) {
470
- return jsonFromNode(nodeFromElement(element, schema));
488
+ function stateFromJSON(json, schema) {
489
+ return EditorState.fromJSON({ schema }, json);
471
490
  }
472
- function nodeFromHTML(html, schema) {
473
- return nodeFromElement(elementFromHTML(html), schema);
491
+ function jsonFromNode(node) {
492
+ return node.toJSON();
474
493
  }
475
- function jsonFromHTML(html, schema) {
476
- return jsonFromElement(elementFromHTML(html), schema);
494
+ function nodeFromJSON(json, schema) {
495
+ return schema.nodeFromJSON(json);
477
496
  }
478
- function elementFromHTML(html) {
479
- const win = getBrowserWindow();
480
- if (!win) {
481
- throw new ProseKitError(
482
- "No Browser Document Found. You can only parse a HTML string in the browser environment."
483
- );
497
+ function nodeFromElement(element, schema) {
498
+ return DOMParser.fromSchema(schema).parse(element);
499
+ }
500
+ function elementFromNode(node, options) {
501
+ const schema = node.type.schema;
502
+ const serializer = DOMSerializer.fromSchema(schema);
503
+ if (schema.topNodeType !== node.type) {
504
+ return serializer.serializeNode(node, options);
484
505
  }
506
+ const doc = getBrowserDocument();
507
+ const div = doc.createElement("div");
508
+ return serializer.serializeFragment(node.content, options, div);
509
+ }
510
+ function elementFromHTML(html, options) {
511
+ const win = getBrowserWindow(options);
485
512
  const parser = new win.DOMParser();
486
- return parser.parseFromString(`<body>${html}</body>`, "text/html").body;
513
+ return parser.parseFromString(`<body><div>${html}</div></body>`, "text/html").body.firstElementChild;
487
514
  }
488
- function jsonFromState(state) {
489
- return state.toJSON();
515
+ function htmlFromElement(element) {
516
+ return element.outerHTML;
490
517
  }
491
- function jsonFromNode(node) {
492
- return node.toJSON();
518
+ function nodeFromHTML(html, schema, options) {
519
+ return nodeFromElement(elementFromHTML(html, options), schema);
493
520
  }
494
- function nodeFromJSON(json, schema) {
495
- return schema.nodeFromJSON(json);
521
+ function htmlFromNode(node) {
522
+ return elementFromNode(node).outerHTML;
496
523
  }
497
- function stateFromJSON(json, schema) {
498
- return EditorState.fromJSON({ schema }, json);
524
+ function jsonFromElement(element, schema) {
525
+ return jsonFromNode(nodeFromElement(element, schema));
526
+ }
527
+ function elementFromJSON(json, schema, options) {
528
+ return elementFromNode(nodeFromJSON(json, schema), options);
529
+ }
530
+ function jsonFromHTML(html, schema, options) {
531
+ return jsonFromElement(elementFromHTML(html, options), schema);
532
+ }
533
+ function htmlFromJSON(json, schema, options) {
534
+ return htmlFromElement(elementFromJSON(json, schema, options));
499
535
  }
500
536
 
501
537
  // src/extensions/default-state.ts
@@ -1975,9 +2011,13 @@ export {
1975
2011
  defineTripleClickOnHandler,
1976
2012
  defineUnmountHandler,
1977
2013
  defineUpdateHandler,
2014
+ elementFromJSON,
2015
+ elementFromNode,
1978
2016
  expandMark,
1979
2017
  getMarkType,
1980
2018
  getNodeType,
2019
+ htmlFromJSON,
2020
+ htmlFromNode,
1981
2021
  insertNode,
1982
2022
  isAllSelection,
1983
2023
  isInCodeBlock,
@@ -1985,7 +2025,6 @@ export {
1985
2025
  isNodeSelection,
1986
2026
  isProseMirrorNode,
1987
2027
  isTextSelection,
1988
- jsonFromElement,
1989
2028
  jsonFromHTML,
1990
2029
  jsonFromNode,
1991
2030
  jsonFromState,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.3.2",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",