@prosekit/core 0.7.15 → 0.8.1

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.
@@ -1,2 +1,37 @@
1
- export { createTestEditor } from './_tsup-dts-rollup.js';
2
- export { TestEditor } from './_tsup-dts-rollup.js';
1
+ import { Editor$1 as Editor, EditorInstance$1 as EditorInstance, EditorOptions, Extension } from "./editor-BP9kgR6R.js";
2
+ import { ProseMirrorNode } from "@prosekit/pm/model";
3
+
4
+ //#region src/test/test-editor.d.ts
5
+ /**
6
+ * An editor for testing purposes.
7
+ * @public
8
+ */
9
+ /**
10
+ * An editor for testing purposes.
11
+ * @public
12
+ */
13
+ declare class TestEditor<E extends Extension = Extension> extends Editor<E> {
14
+ constructor(instance: EditorInstance);
15
+ /**
16
+ * Set the editor state to the given document. You can use special tokens
17
+ * `<a>` and `<b>` to set the anchor and head positions of the selection.
18
+ *
19
+ * @example
20
+ *
21
+ * ```ts
22
+ * const editor = createTestEditor({ extension })
23
+ * const n = editor.nodes
24
+ * const doc = n.doc(n.paragraph('<a>Hello<b> world!'))
25
+ * editor.set(doc) // "Hello" is selected.
26
+ * ```
27
+ */
28
+ set(doc: ProseMirrorNode): void;
29
+ dispatchEvent(event: Event): void;
30
+ }
31
+ /**
32
+ * @public
33
+ */
34
+ declare function createTestEditor<E extends Extension>(options: EditorOptions<E>): TestEditor<E>;
35
+
36
+ //#endregion
37
+ export { TestEditor, createTestEditor };
@@ -1,130 +1,108 @@
1
- import {
2
- Editor,
3
- EditorInstance,
4
- assert,
5
- createMarkActions,
6
- createNodeActions,
7
- isProseMirrorNode,
8
- setupEditorExtension
9
- } from "./chunk-B3WEP4DD.js";
1
+ import { Editor, EditorInstance, assert, createMarkActions, createNodeActions, isProseMirrorNode, setupEditorExtension } from "./editor-BOiNwODb.js";
2
+ import { NodeSelection, TextSelection } from "@prosekit/pm/state";
10
3
 
11
- // src/test/test-editor.ts
12
- import {
13
- NodeSelection,
14
- TextSelection
15
- } from "@prosekit/pm/state";
16
-
17
- // src/test/test-builder.ts
18
- var createNodeForTest = (type, attrs, children) => {
19
- const tags = {};
20
- const isTopNode = type === type.schema.topNodeType;
21
- let pos = isTopNode ? 0 : 1;
22
- const normalizedChildren = [];
23
- for (const child of children) {
24
- if (child.tags) {
25
- for (const [key, value] of Object.entries(child.tags)) {
26
- tags[key] = pos + value;
27
- }
28
- normalizedChildren.push(child);
29
- pos += child.nodeSize;
30
- } else if (child.isText) {
31
- const text = child.text;
32
- const re = /<(a|b)>/g;
33
- let i = 0;
34
- let out = "";
35
- for (const match of text.matchAll(re)) {
36
- out += text.slice(i, match.index);
37
- tags[match[1]] = pos + out.length;
38
- i = match.index + match[0].length;
39
- }
40
- out += text.slice(i);
41
- if (out) {
42
- normalizedChildren.push(child.type.schema.text(out).mark(child.marks));
43
- pos += out.length;
44
- }
45
- } else {
46
- normalizedChildren.push(child);
47
- pos += child.nodeSize;
48
- }
49
- }
50
- const node = type.createAndFill(
51
- attrs,
52
- normalizedChildren
53
- );
54
- assert(node, `Failed to create node ${type.name}`);
55
- node.tags = tags;
56
- return node;
4
+ //#region src/test/test-builder.ts
5
+ const createNodeForTest = (type, attrs, children) => {
6
+ const tags = {};
7
+ const isTopNode = type === type.schema.topNodeType;
8
+ let pos = isTopNode ? 0 : 1;
9
+ const normalizedChildren = [];
10
+ for (const child of children) if (child.tags) {
11
+ for (const [key, value] of Object.entries(child.tags)) tags[key] = pos + value;
12
+ normalizedChildren.push(child);
13
+ pos += child.nodeSize;
14
+ } else if (child.isText) {
15
+ const text = child.text;
16
+ const re = /<(a|b)>/g;
17
+ let i = 0;
18
+ let out = "";
19
+ for (const match of text.matchAll(re)) {
20
+ out += text.slice(i, match.index);
21
+ tags[match[1]] = pos + out.length;
22
+ i = match.index + match[0].length;
23
+ }
24
+ out += text.slice(i);
25
+ if (out) {
26
+ normalizedChildren.push(child.type.schema.text(out).mark(child.marks));
27
+ pos += out.length;
28
+ }
29
+ } else {
30
+ normalizedChildren.push(child);
31
+ pos += child.nodeSize;
32
+ }
33
+ const node = type.createAndFill(attrs, normalizedChildren);
34
+ assert(node, `Failed to create node ${type.name}`);
35
+ node.tags = tags;
36
+ return node;
57
37
  };
58
- var applyMarkForTest = (mark, children) => {
59
- return children.map((node) => {
60
- const newNode = node.mark(mark.addToSet(node.marks));
61
- newNode.tags = node.tags;
62
- return newNode;
63
- });
38
+ const applyMarkForTest = (mark, children) => {
39
+ return children.map((node) => {
40
+ const newNode = node.mark(mark.addToSet(node.marks));
41
+ newNode.tags = node.tags;
42
+ return newNode;
43
+ });
64
44
  };
65
45
 
66
- // src/test/test-editor.ts
46
+ //#endregion
47
+ //#region src/test/test-editor.ts
67
48
  function maybeResolve(doc, pos) {
68
- if (pos != null) {
69
- return doc.resolve(pos);
70
- }
71
- return void 0;
49
+ if (pos != null) return doc.resolve(pos);
50
+ return void 0;
72
51
  }
73
52
  function getSelection(doc) {
74
- const tags = doc.tags;
75
- const $a = maybeResolve(doc, tags?.a);
76
- const $b = maybeResolve(doc, tags?.b);
77
- if ($a) {
78
- if ($a.parent.inlineContent) {
79
- return new TextSelection($a, $b);
80
- } else {
81
- return new NodeSelection($a);
82
- }
83
- }
84
- return TextSelection.atStart(doc);
53
+ const tags = doc.tags;
54
+ const $a = maybeResolve(doc, tags?.a);
55
+ const $b = maybeResolve(doc, tags?.b);
56
+ if ($a) if ($a.parent.inlineContent) return new TextSelection($a, $b);
57
+ else return new NodeSelection($a);
58
+ return TextSelection.atStart(doc);
85
59
  }
86
60
  var TestEditorInstance = class extends EditorInstance {
87
- constructor(extension) {
88
- super(extension);
89
- this.nodes = createNodeActions(this.schema, this.getState, createNodeForTest);
90
- this.marks = createMarkActions(this.schema, this.getState, applyMarkForTest);
91
- }
92
- setContent(content, selection) {
93
- return super.setContent(
94
- content,
95
- isProseMirrorNode(content) && !selection ? getSelection(content) : selection
96
- );
97
- }
61
+ constructor(extension) {
62
+ super(extension);
63
+ this.nodes = createNodeActions(this.schema, this.getState, createNodeForTest);
64
+ this.marks = createMarkActions(this.schema, this.getState, applyMarkForTest);
65
+ }
66
+ setContent(content, selection) {
67
+ return super.setContent(content, isProseMirrorNode(content) && !selection ? getSelection(content) : selection);
68
+ }
98
69
  };
70
+ /**
71
+ * An editor for testing purposes.
72
+ * @public
73
+ */
99
74
  var TestEditor = class extends Editor {
100
- constructor(instance) {
101
- super(instance);
102
- }
103
- /**
104
- * Set the editor state to the given document. You can use special tokens
105
- * `<a>` and `<b>` to set the anchor and head positions of the selection.
106
- *
107
- * @example
108
- *
109
- * ```ts
110
- * const editor = createTestEditor({ extension })
111
- * const n = editor.nodes
112
- * const doc = n.doc(n.paragraph('<a>Hello<b> world!'))
113
- * editor.set(doc) // "Hello" is selected.
114
- * ```
115
- */
116
- set(doc) {
117
- return this.setContent(doc);
118
- }
119
- dispatchEvent(event) {
120
- this.view.dispatchEvent(event);
121
- }
75
+ constructor(instance) {
76
+ super(instance);
77
+ }
78
+ /**
79
+ * Set the editor state to the given document. You can use special tokens
80
+ * `<a>` and `<b>` to set the anchor and head positions of the selection.
81
+ *
82
+ * @example
83
+ *
84
+ * ```ts
85
+ * const editor = createTestEditor({ extension })
86
+ * const n = editor.nodes
87
+ * const doc = n.doc(n.paragraph('<a>Hello<b> world!'))
88
+ * editor.set(doc) // "Hello" is selected.
89
+ * ```
90
+ */
91
+ set(doc) {
92
+ return this.setContent(doc);
93
+ }
94
+ dispatchEvent(event) {
95
+ this.view.dispatchEvent(event);
96
+ }
122
97
  };
98
+ /**
99
+ * @public
100
+ */
123
101
  function createTestEditor(options) {
124
- const extension = setupEditorExtension(options);
125
- const instance = new TestEditorInstance(extension);
126
- return new TestEditor(instance);
102
+ const extension = setupEditorExtension(options);
103
+ const instance = new TestEditorInstance(extension);
104
+ return new TestEditor(instance);
127
105
  }
128
- export {
129
- createTestEditor
130
- };
106
+
107
+ //#endregion
108
+ export { createTestEditor };