@prosekit/core 0.0.17 → 0.0.19

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,3 +1,4 @@
1
+ import { AllSelection } from '@prosekit/pm/state';
1
2
  import { Attrs } from '@prosekit/pm/model';
2
3
  import { Attrs as Attrs_2 } from 'prosemirror-model';
3
4
  import { Command } from '@prosekit/pm/state';
@@ -12,7 +13,9 @@ import type { IsEqual } from 'type-fest';
12
13
  import { Mark } from '@prosekit/pm/model';
13
14
  import type { MarkSpec } from '@prosekit/pm/model';
14
15
  import { MarkType } from '@prosekit/pm/model';
16
+ import { MarkType as MarkType_2 } from 'prosemirror-model';
15
17
  import { Node as Node_2 } from 'prosemirror-model';
18
+ import { NodeSelection } from '@prosekit/pm/state';
16
19
  import type { NodeSpec } from '@prosekit/pm/model';
17
20
  import { NodeType } from '@prosekit/pm/model';
18
21
  import { NodeType as NodeType_2 } from 'prosemirror-model';
@@ -23,8 +26,10 @@ import { ProseMirrorFragment } from '@prosekit/pm/model';
23
26
  import { ProseMirrorNode } from '@prosekit/pm/model';
24
27
  import { Schema } from '@prosekit/pm/model';
25
28
  import type { SchemaSpec } from '@prosekit/pm/model';
26
- import { Selection as Selection_2 } from 'prosemirror-state';
29
+ import { Selection as Selection_2 } from '@prosekit/pm/state';
30
+ import { Selection as Selection_3 } from 'prosemirror-state';
27
31
  import type { Simplify } from 'type-fest';
32
+ import { TextSelection } from '@prosekit/pm/state';
28
33
  import { Transaction } from '@prosekit/pm/state';
29
34
  import type { UnionToIntersection } from 'type-fest';
30
35
  import { UserProjectConfigExport } from 'vitest/dist/config.js';
@@ -34,15 +39,24 @@ declare type Action = (options: {
34
39
  view?: EditorView;
35
40
  }) => boolean;
36
41
 
42
+ /**
43
+ * Add the given mark to the inline content.
44
+ */
37
45
  declare function addMark(options: {
46
+ /**
47
+ * The type of the mark to add.
48
+ */
38
49
  type: string | MarkType;
50
+ /**
51
+ * The attributes of the mark to add.
52
+ */
39
53
  attrs?: Attrs | null;
40
54
  /**
41
- * The start position of the mark. By default it will be the start position of current selection.
55
+ * The start position of the document. By default it will be the start position of current selection.
42
56
  */
43
57
  from?: number;
44
58
  /**
45
- * The end position of the mark. By default it will be the end position of current selection.
59
+ * The end position of the document. By default it will be the end position of current selection.
46
60
  */
47
61
  to?: number;
48
62
  }): Command;
@@ -58,6 +72,28 @@ export declare function assertTypeEqual<T, U>(_val: IsEqual<T, U>): void;
58
72
 
59
73
  export declare function attrsMatch(nodeOrMark: ProseMirrorNode | Mark, attrs: Attrs): boolean;
60
74
 
75
+ export declare abstract class BaseExtension<T extends ExtensionTyping = ExtensionTyping> implements Extension<T> {
76
+ extension: Extension | Extension[];
77
+ priority?: Priority;
78
+ _type?: T;
79
+ /**
80
+ * @internal
81
+ *
82
+ * Whether this extension has payload that can be converted to a schema.
83
+ *
84
+ * Notice that this does not mean that the extension has a schema. For
85
+ * example, a `FacetExtension` with a `schemaFacet` will return `true` for
86
+ * this property, but will return `null` for `schema`.
87
+ */
88
+ abstract hasSchema: boolean;
89
+ /**
90
+ * @internal
91
+ *
92
+ * The schema that this extension represents.
93
+ */
94
+ abstract schema: Schema | null;
95
+ }
96
+
61
97
  export declare function collectNodes(content: NodeContent): ProseMirrorNode[];
62
98
 
63
99
  export declare interface CommandApplier<Args extends any[] = any[]> {
@@ -91,7 +127,7 @@ declare type ConverterTuple = Tuple5<FacetConverter | undefined>;
91
127
  /**
92
128
  * @public
93
129
  */
94
- declare function createEditor<E extends Extension>({ extension, defaultDoc, defaultSelection, }: EditorOptions<E>): Editor<E>;
130
+ declare function createEditor<E extends Extension>({ extension, defaultDoc, defaultHTML, defaultSelection, }: EditorOptions<E>): Editor<E>;
95
131
  export { createEditor }
96
132
  export { createEditor as createEditor_alias_1 }
97
133
 
@@ -105,13 +141,20 @@ export declare const default_alias_1: UserProjectConfigExport;
105
141
 
106
142
  declare interface DefaultStateOptions {
107
143
  /**
108
- * A JSON representation of a ProseMirror document.
144
+ * A JSON object representing the starting document to use when creating the
145
+ * editor.
146
+ */
147
+ defaultDoc?: NodeJson;
148
+ /**
149
+ * A HTML string representing the starting document to use when creating the
150
+ * editor.
109
151
  */
110
- doc?: NodeJson;
152
+ defaultHTML?: string;
111
153
  /**
112
- * A JSON representation of a ProseMirror selection.
154
+ * A JSON object representing the starting selection to use when creating the
155
+ * editor. It's only used when `defaultDoc` or `defaultHTML` is also provided.
113
156
  */
114
- selection?: SelectionJson;
157
+ defaultSelection?: SelectionJson;
115
158
  }
116
159
  export { DefaultStateOptions }
117
160
  export { DefaultStateOptions as DefaultStateOptions_alias_1 }
@@ -143,6 +186,18 @@ declare function defineBaseCommands(): Extension<{
143
186
  to?: number | undefined;
144
187
  }];
145
188
  selectAll: [];
189
+ addMark: [options: {
190
+ type: string | MarkType_2;
191
+ attrs?: Attrs_2 | null | undefined;
192
+ from?: number | undefined;
193
+ to?: number | undefined;
194
+ }];
195
+ removeMark: [options: {
196
+ type: string | MarkType_2;
197
+ attrs?: Attrs_2 | null | undefined;
198
+ from?: number | undefined;
199
+ to?: number | undefined;
200
+ }];
146
201
  };
147
202
  }>;
148
203
  export { defineBaseCommands }
@@ -163,7 +218,7 @@ declare function defineCommands<T extends Record<string, CommandCreator> = Recor
163
218
  export { defineCommands }
164
219
  export { defineCommands as defineCommands_alias_1 }
165
220
 
166
- declare function defineDefaultState(options: DefaultStateOptions): Extension;
221
+ declare function defineDefaultState({ defaultDoc, defaultHTML, defaultSelection, }: DefaultStateOptions): Extension;
167
222
  export { defineDefaultState }
168
223
  export { defineDefaultState as defineDefaultState_alias_1 }
169
224
 
@@ -181,7 +236,7 @@ export { defineDoc as defineDoc_alias_1 }
181
236
  */
182
237
  declare function defineEventHandler(options: {
183
238
  update?: VoidFunction;
184
- }): FacetExtension<VoidFunction, PluginPayload>;
239
+ }): Extension<ExtensionTyping<string, string, CommandArgs>>;
185
240
  export { defineEventHandler }
186
241
  export { defineEventHandler as defineEventHandler_alias_1 }
187
242
 
@@ -327,15 +382,34 @@ declare interface EditorOptions<E extends Extension> {
327
382
  * editor.
328
383
  */
329
384
  defaultDoc?: NodeJson;
385
+ /**
386
+ * A HTML string representing the starting document to use when creating the
387
+ * editor.
388
+ */
389
+ defaultHTML?: string;
330
390
  /**
331
391
  * A JSON object representing the starting selection to use when creating the
332
- * editor. It's only used when `defaultDoc` is also provided.
392
+ * editor. It's only used when `defaultDoc` or `defaultHTML` is also provided.
333
393
  */
334
394
  defaultSelection?: SelectionJson;
335
395
  }
336
396
  export { EditorOptions }
337
397
  export { EditorOptions as EditorOptions_alias_1 }
338
398
 
399
+ /**
400
+ * Parse a HTML element to a ProseMirror document JSON.
401
+ */
402
+ declare function elementToJSON(element: HTMLElement, schema: Schema): NodeJson;
403
+ export { elementToJSON }
404
+ export { elementToJSON as elementToJSON_alias_1 }
405
+
406
+ /**
407
+ * Parse a HTML element to a ProseMirror node.
408
+ */
409
+ declare function elementToNode(element: HTMLElement, schema: Schema): ProseMirrorNode;
410
+ export { elementToNode }
411
+ export { elementToNode as elementToNode_alias_1 }
412
+
339
413
  declare type EmptyValue = undefined | null | EmptyObject;
340
414
 
341
415
  export declare type ExceptEmptyValue<T> = ConditionalExcept<T, EmptyValue>;
@@ -347,6 +421,12 @@ declare interface Extension<T extends ExtensionTyping = ExtensionTyping> {
347
421
  extension: Extension | Extension[];
348
422
  priority?: Priority;
349
423
  _type?: T;
424
+ /**
425
+ * @public
426
+ *
427
+ * The schema that this extension represents.
428
+ */
429
+ schema: Schema | null;
350
430
  }
351
431
  export { Extension }
352
432
  export { Extension as Extension_alias_1 }
@@ -431,13 +511,17 @@ declare class Facet<Input, Output> {
431
511
  * @internal
432
512
  */
433
513
  readonly singleton: boolean;
514
+ /**
515
+ * @internal
516
+ */
517
+ isSchema: boolean;
434
518
  private constructor();
435
- static define<Input, Output>({ converter: converter, convert: convert, next, singleton, }: FacetOptions<Input, Output>): Facet<Input, Output>;
519
+ static define<Input, Output>({ converter, convert, next, singleton, }: FacetOptions<Input, Output>): Facet<Input, Output>;
436
520
  /**
437
521
  * @internal
438
522
  */
439
523
  static defineRootFacet<Input>(options: Omit<FacetOptions<Input, Input>, 'next'>): Facet<Input, Input>;
440
- extension(payloads: Input[]): FacetExtension<Input, Output>;
524
+ extension(payloads: Input[]): Extension;
441
525
  }
442
526
  export { Facet }
443
527
  export { Facet as Facet_alias_1 }
@@ -453,14 +537,14 @@ export declare interface FacetConverter<Input = any, Output = any> {
453
537
  /**
454
538
  * @public
455
539
  */
456
- declare class FacetExtension<Input, Output> {
540
+ export declare class FacetExtensionImpl<Input, Output> extends BaseExtension {
457
541
  readonly facet: Facet<Input, Output>;
458
542
  readonly payloads: Input[];
459
543
  extension: Extension;
544
+ hasSchema: boolean;
545
+ schema: null;
460
546
  constructor(facet: Facet<Input, Output>, payloads: Input[]);
461
547
  }
462
- export { FacetExtension }
463
- export { FacetExtension as FacetExtension_alias_1 }
464
548
 
465
549
  /**
466
550
  * @public
@@ -474,7 +558,11 @@ declare interface FacetOptions<Input, Output> {
474
558
  export { FacetOptions }
475
559
  export { FacetOptions as FacetOptions_alias_1 }
476
560
 
477
- export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_2;
561
+ export declare function getBrowserDocument(): Document | undefined;
562
+
563
+ export declare function getBrowserWindow(): (Window & typeof globalThis) | null | undefined;
564
+
565
+ export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_3;
478
566
 
479
567
  export declare function getFacetCount(): number;
480
568
 
@@ -492,6 +580,20 @@ declare function getNodeType(schema: Schema, type: string | NodeType): NodeType;
492
580
  export { getNodeType }
493
581
  export { getNodeType as getNodeType_alias_1 }
494
582
 
583
+ /**
584
+ * Parse a HTML element to a ProseMirror document JSON.
585
+ */
586
+ declare function htmlToJSON(html: string, schema: Schema): NodeJson;
587
+ export { htmlToJSON }
588
+ export { htmlToJSON as htmlToJSON_alias_1 }
589
+
590
+ /**
591
+ * Parse a HTML string to a ProseMirror node.
592
+ */
593
+ declare function htmlToNode(html: string, schema: Schema): ProseMirrorNode;
594
+ export { htmlToNode }
595
+ export { htmlToNode as htmlToNode_alias_1 }
596
+
495
597
  declare function insertNode(options: {
496
598
  node: ProseMirrorNode;
497
599
  pos?: number;
@@ -516,15 +618,46 @@ export declare function insertText({ text, from, to, }: {
516
618
  to?: number;
517
619
  }): Command;
518
620
 
621
+ /**
622
+ * @internal
623
+ */
624
+ declare function isAllSelection(sel: Selection_2): sel is AllSelection;
625
+ export { isAllSelection }
626
+ export { isAllSelection as isAllSelection_alias_1 }
627
+
519
628
  export declare const isMac: boolean;
520
629
 
521
- export declare function isMark(mark: unknown): mark is Mark;
630
+ /**
631
+ * @internal
632
+ */
633
+ declare function isMark(mark: unknown): mark is Mark;
634
+ export { isMark }
635
+ export { isMark as isMark_alias_1 }
522
636
 
523
637
  export declare function isMarkActive(state: EditorState, type: string | MarkType, attrs?: Attrs | null): boolean;
524
638
 
525
639
  export declare function isNodeActive(state: EditorState, type: string | NodeType, attrs?: Attrs | null): boolean;
526
640
 
527
- export declare function isProseMirrorNode(node: unknown): node is ProseMirrorNode;
641
+ /**
642
+ * @internal
643
+ */
644
+ declare function isNodeSelection(sel: Selection_2): sel is NodeSelection;
645
+ export { isNodeSelection }
646
+ export { isNodeSelection as isNodeSelection_alias_1 }
647
+
648
+ /**
649
+ * @internal
650
+ */
651
+ declare function isProseMirrorNode(node: unknown): node is ProseMirrorNode;
652
+ export { isProseMirrorNode }
653
+ export { isProseMirrorNode as isProseMirrorNode_alias_1 }
654
+
655
+ /**
656
+ * @internal
657
+ */
658
+ declare function isTextSelection(sel: Selection_2): sel is TextSelection;
659
+ export { isTextSelection }
660
+ export { isTextSelection as isTextSelection_alias_1 }
528
661
 
529
662
  /**
530
663
  * @public
@@ -604,6 +737,8 @@ declare interface NodeViewOptions {
604
737
  export { NodeViewOptions }
605
738
  export { NodeViewOptions as NodeViewOptions_alias_1 }
606
739
 
740
+ export declare function notNull<T>(value: T | null | undefined): value is T;
741
+
607
742
  export declare function objectEqual<T>(a: T, b: T): boolean;
608
743
 
609
744
  declare type Payload = unknown;
@@ -649,6 +784,30 @@ declare class ProseKitError extends Error {
649
784
  export { ProseKitError }
650
785
  export { ProseKitError as ProseKitError_alias_1 }
651
786
 
787
+ /**
788
+ * Remove the given mark from the inline content.
789
+ */
790
+ declare function removeMark(options: {
791
+ /**
792
+ * The type of the mark to remove.
793
+ */
794
+ type: string | MarkType;
795
+ /**
796
+ * If attrs is given, remove precisely the mark with the given attrs. Otherwise, remove all marks of the given type.
797
+ */
798
+ attrs?: Attrs | null;
799
+ /**
800
+ * The start position of the document. By default it will be the start position of current selection.
801
+ */
802
+ from?: number;
803
+ /**
804
+ * The end position of the document. By default it will be the end position of current selection.
805
+ */
806
+ to?: number;
807
+ }): Command;
808
+ export { removeMark }
809
+ export { removeMark as removeMark_alias_1 }
810
+
652
811
  export declare const schemaFacet: Facet<SchemaPayload, SchemaPayload>;
653
812
 
654
813
  export declare type SchemaPayload = SchemaSpec;
@@ -661,8 +820,8 @@ export declare function selectAll(): Command;
661
820
  * @public
662
821
  */
663
822
  declare interface SelectionJson {
664
- from: number;
665
- to: number;
823
+ anchor: number;
824
+ head: number;
666
825
  type: string;
667
826
  }
668
827
  export { SelectionJson }
@@ -755,6 +914,15 @@ declare function union<E extends Extension | Extension[]>(extension: E): Simplif
755
914
  export { union }
756
915
  export { union as union_alias_1 }
757
916
 
917
+ export declare class UnionExtensionImpl<T extends ExtensionTyping = ExtensionTyping> extends BaseExtension<T> implements Extension<T> {
918
+ extension: BaseExtension[];
919
+ private _schema;
920
+ private hasSchemaCount;
921
+ constructor(extension?: BaseExtension[]);
922
+ get hasSchema(): boolean;
923
+ get schema(): Schema | null;
924
+ }
925
+
758
926
  export declare function uniqPush<T>(prev: readonly T[], next: readonly T[]): T[];
759
927
 
760
928
  export declare function uniqRemove<T>(prev: T[], next: T[]): T[];
@@ -1,5 +1,6 @@
1
1
  export { addMark } from './_tsup-dts-rollup';
2
2
  export { insertNode } from './_tsup-dts-rollup';
3
+ export { removeMark } from './_tsup-dts-rollup';
3
4
  export { setBlockType } from './_tsup-dts-rollup';
4
5
  export { toggleMark } from './_tsup-dts-rollup';
5
6
  export { toggleNode } from './_tsup-dts-rollup';
@@ -34,7 +35,6 @@ export { pluginFacet } from './_tsup-dts-rollup';
34
35
  export { PluginPayload } from './_tsup-dts-rollup';
35
36
  export { defineText } from './_tsup-dts-rollup';
36
37
  export { Facet } from './_tsup-dts-rollup';
37
- export { FacetExtension } from './_tsup-dts-rollup';
38
38
  export { FacetOptions } from './_tsup-dts-rollup';
39
39
  export { CommandArgs } from './_tsup-dts-rollup';
40
40
  export { Extension } from './_tsup-dts-rollup';
@@ -51,3 +51,12 @@ export { Priority } from './_tsup-dts-rollup';
51
51
  export { SimplifyUnion } from './_tsup-dts-rollup';
52
52
  export { getMarkType } from './_tsup-dts-rollup';
53
53
  export { getNodeType } from './_tsup-dts-rollup';
54
+ export { elementToJSON } from './_tsup-dts-rollup';
55
+ export { elementToNode } from './_tsup-dts-rollup';
56
+ export { htmlToJSON } from './_tsup-dts-rollup';
57
+ export { htmlToNode } from './_tsup-dts-rollup';
58
+ export { isAllSelection } from './_tsup-dts-rollup';
59
+ export { isMark } from './_tsup-dts-rollup';
60
+ export { isNodeSelection } from './_tsup-dts-rollup';
61
+ export { isProseMirrorNode } from './_tsup-dts-rollup';
62
+ export { isTextSelection } from './_tsup-dts-rollup';
@@ -75,6 +75,24 @@ function insertNode(options) {
75
75
  };
76
76
  }
77
77
 
78
+ // src/commands/remove-mark.ts
79
+ import "@prosekit/pm/model";
80
+ import "@prosekit/pm/state";
81
+ function removeMark(options) {
82
+ return (state, dispatch) => {
83
+ var _a, _b;
84
+ const markType = getMarkType(state.schema, options.type);
85
+ const mark = options.attrs ? markType.create(options.attrs) : markType;
86
+ const from = (_a = options.from) != null ? _a : state.selection.from;
87
+ const to = (_b = options.to) != null ? _b : state.selection.to;
88
+ if (from > to) {
89
+ return false;
90
+ }
91
+ dispatch == null ? void 0 : dispatch(state.tr.removeMark(from, to, mark));
92
+ return true;
93
+ };
94
+ }
95
+
78
96
  // src/commands/set-block-type.ts
79
97
  import "@prosekit/pm/state";
80
98
 
@@ -198,7 +216,7 @@ function toggleNode({
198
216
  }
199
217
 
200
218
  // src/editor/editor.ts
201
- import { Schema as Schema4 } from "@prosekit/pm/model";
219
+ import { Schema as Schema6 } from "@prosekit/pm/model";
202
220
  import { EditorState } from "@prosekit/pm/state";
203
221
  import { EditorView } from "@prosekit/pm/view";
204
222
 
@@ -226,6 +244,14 @@ function uniqRemove(prev, next) {
226
244
  return result;
227
245
  }
228
246
 
247
+ // src/facets/base-extension.ts
248
+ import "@prosekit/pm/model";
249
+ var BaseExtension = class {
250
+ constructor() {
251
+ this.extension = [];
252
+ }
253
+ };
254
+
229
255
  // src/facets/facet.ts
230
256
  var facetCount = 0;
231
257
  function getFacetCount() {
@@ -237,6 +263,10 @@ var Facet = class _Facet {
237
263
  * @internal
238
264
  */
239
265
  this.index = facetCount++;
266
+ /**
267
+ * @internal
268
+ */
269
+ this.isSchema = false;
240
270
  this.converter = converter;
241
271
  this.next = next;
242
272
  this.singleton = singleton;
@@ -263,13 +293,17 @@ var Facet = class _Facet {
263
293
  return _Facet.define(options);
264
294
  }
265
295
  extension(payloads) {
266
- return new FacetExtension(this, payloads);
296
+ return new FacetExtensionImpl(this, payloads);
267
297
  }
268
298
  };
269
- var FacetExtension = class {
299
+ var FacetExtensionImpl = class extends BaseExtension {
270
300
  constructor(facet, payloads) {
301
+ var _a;
302
+ super();
271
303
  this.facet = facet;
272
304
  this.payloads = payloads;
305
+ this.schema = null;
306
+ this.hasSchema = !!(facet.isSchema || ((_a = facet.next) == null ? void 0 : _a.isSchema));
273
307
  }
274
308
  };
275
309
 
@@ -304,118 +338,97 @@ var stateFacet = Facet.defineRootFacet({
304
338
  }
305
339
  });
306
340
 
307
- // src/extensions/default-state.ts
308
- function defineDefaultState(options) {
309
- return stateFacet.extension([
310
- ({ schema }) => {
311
- const config = {};
312
- if (options.doc) {
313
- config.doc = schema.nodeFromJSON(options.doc);
314
- if (options.selection) {
315
- config.selection = Selection.fromJSON(config.doc, options.selection);
316
- }
317
- }
318
- return config;
319
- }
320
- ]);
321
- }
341
+ // src/utils/parse.ts
342
+ import { DOMParser } from "@prosekit/pm/model";
322
343
 
323
- // src/utils/type-assertion.ts
324
- import { Mark, ProseMirrorNode } from "@prosekit/pm/model";
325
- function isProseMirrorNode(node) {
326
- return node instanceof ProseMirrorNode;
344
+ // src/utils/get-dom-api.ts
345
+ function getGlobalBrowserDocument() {
346
+ if (typeof document !== "undefined") {
347
+ return document;
348
+ }
349
+ if (typeof globalThis !== "undefined" && globalThis.document) {
350
+ return globalThis.document;
351
+ }
327
352
  }
328
- function isMark(mark) {
329
- return mark instanceof Mark;
353
+ function getGlobalBrowserWindow() {
354
+ if (typeof window !== "undefined") {
355
+ return window;
356
+ }
357
+ if (typeof globalThis !== "undefined" && globalThis.window) {
358
+ return globalThis.window;
359
+ }
330
360
  }
331
-
332
- // src/utils/is-mark-active.ts
333
- function isMarkActive(state, type, attrs) {
361
+ function getBrowserWindow() {
334
362
  var _a;
335
- const markType = getMarkType(state.schema, type);
336
- const mark = attrs ? markType.create(attrs) : markType;
337
- const { from, to, $from, $to } = state.selection;
338
- return state.doc.rangeHasMark(from, to, mark) || hasMark(
339
- [...$from.marks(), ...$to.marks(), ...(_a = state.storedMarks) != null ? _a : []],
340
- mark
341
- );
342
- }
343
- function hasMark(marks, mark) {
344
- if (marks.length === 0) {
345
- return false;
346
- }
347
- if (isMark(mark)) {
348
- return marks.some((m) => m.eq(mark));
349
- } else {
350
- return marks.some((m) => m.type === mark);
363
+ const win = getGlobalBrowserWindow();
364
+ if (win) {
365
+ return win;
351
366
  }
367
+ return (_a = getGlobalBrowserDocument()) == null ? void 0 : _a.defaultView;
352
368
  }
353
369
 
354
- // src/editor/builder.ts
355
- import "@prosekit/pm/model";
356
- function createNodeBuilder(getState, type) {
357
- const builder = (...args) => buildNode(type, args);
358
- builder.isActive = (attrs) => {
359
- const state = getState();
360
- return state ? isNodeActive(state, type, attrs) : false;
361
- };
362
- return builder;
370
+ // src/utils/parse.ts
371
+ function elementToNode(element, schema) {
372
+ return DOMParser.fromSchema(schema).parse(element);
363
373
  }
364
- function createMarkBuilder(getState, type) {
365
- const builder = (...args) => buildMark(type, args);
366
- builder.isActive = (attrs) => {
367
- const state = getState();
368
- return state ? isMarkActive(state, type, attrs) : false;
369
- };
370
- return builder;
374
+ function elementToJSON(element, schema) {
375
+ return elementToNode(element, schema).toJSON();
371
376
  }
372
- function buildMark(type, args) {
373
- const [attrs, children] = normalizeArgs(args);
374
- return flattenChildren(type.schema, children, type.create(attrs));
377
+ function htmlToNode(html, schema) {
378
+ return elementToNode(htmlToElement(html), schema);
375
379
  }
376
- function buildNode(type, args) {
377
- const [attrs, children] = normalizeArgs(args);
378
- const node = type.createAndFill(attrs, flattenChildren(type.schema, children));
379
- if (!node) {
380
- throw new ProseKitError(`Couldn't create node ${type.name}`);
381
- }
382
- return node;
380
+ function htmlToJSON(html, schema) {
381
+ return elementToJSON(htmlToElement(html), schema);
383
382
  }
384
- function flattenChildren(schema, children, mark) {
385
- const nodes = [];
386
- for (const child of children) {
387
- if (typeof child === "string") {
388
- if (child) {
389
- nodes.push(schema.text(child, mark ? [mark] : null));
390
- }
391
- } else if (Array.isArray(child)) {
392
- nodes.push(...flattenChildren(schema, child, mark));
393
- } else if (isProseMirrorNode(child)) {
394
- nodes.push(mark ? child.mark(mark.addToSet(child.marks)) : child);
395
- } else {
396
- throw new ProseKitError(`Invalid node child: ${typeof child}`);
397
- }
398
- }
399
- return nodes;
400
- }
401
- function normalizeArgs(args) {
402
- const [attrs, ...children] = args;
403
- if (isNodeChild(args)) {
404
- children.unshift(args);
405
- return [null, children];
406
- } else if (typeof attrs === "object") {
407
- return [attrs, children];
408
- } else {
409
- return [null, children];
383
+ function htmlToElement(html) {
384
+ const win = getBrowserWindow();
385
+ if (!win) {
386
+ throw new ProseKitError(
387
+ "No Browser Document Found. You can only parse a HTML string in the browser environment."
388
+ );
410
389
  }
390
+ const parser = new win.DOMParser();
391
+ return parser.parseFromString(`<body>${html}</body>`, "text/html").body;
411
392
  }
412
- function isNodeChild(value) {
413
- if (!value) {
414
- return false;
393
+
394
+ // src/extensions/default-state.ts
395
+ function defineDefaultState({
396
+ defaultDoc,
397
+ defaultHTML,
398
+ defaultSelection
399
+ }) {
400
+ if (defaultHTML && defaultDoc) {
401
+ throw new ProseKitError(
402
+ "Only one of defaultHTML and defaultDoc can be provided"
403
+ );
415
404
  }
416
- return typeof value === "string" || Array.isArray(value) || isProseMirrorNode(value);
405
+ return stateFacet.extension([
406
+ ({ schema }) => {
407
+ const config = {};
408
+ if (defaultHTML) {
409
+ defaultDoc = htmlToJSON(defaultHTML, schema);
410
+ }
411
+ if (defaultDoc) {
412
+ config.doc = schema.nodeFromJSON(defaultDoc);
413
+ if (defaultSelection) {
414
+ config.selection = Selection.fromJSON(config.doc, defaultSelection);
415
+ }
416
+ }
417
+ return config;
418
+ }
419
+ ]);
417
420
  }
418
421
 
422
+ // src/types/priority.ts
423
+ var Priority = /* @__PURE__ */ ((Priority2) => {
424
+ Priority2[Priority2["lowest"] = 4] = "lowest";
425
+ Priority2[Priority2["low"] = 3] = "low";
426
+ Priority2[Priority2["default"] = 2] = "default";
427
+ Priority2[Priority2["high"] = 1] = "high";
428
+ Priority2[Priority2["highest"] = 0] = "highest";
429
+ return Priority2;
430
+ })(Priority || {});
431
+
419
432
  // src/facets/command.ts
420
433
  var commandFacet = Facet.defineRootFacet({
421
434
  convert: (inputs) => {
@@ -439,6 +452,7 @@ var schemaFacet = Facet.defineRootFacet({
439
452
  return { nodes, marks, topNode };
440
453
  }
441
454
  });
455
+ schemaFacet.isSchema = true;
442
456
 
443
457
  // src/facets/view.ts
444
458
  var viewFacet = Facet.defineRootFacet({
@@ -447,17 +461,7 @@ var viewFacet = Facet.defineRootFacet({
447
461
  }
448
462
  });
449
463
 
450
- // src/types/priority.ts
451
- var Priority = /* @__PURE__ */ ((Priority2) => {
452
- Priority2[Priority2["lowest"] = 4] = "lowest";
453
- Priority2[Priority2["low"] = 3] = "low";
454
- Priority2[Priority2["default"] = 2] = "default";
455
- Priority2[Priority2["high"] = 1] = "high";
456
- Priority2[Priority2["highest"] = 0] = "highest";
457
- return Priority2;
458
- })(Priority || {});
459
-
460
- // src/editor/flatten.ts
464
+ // src/facets/flatten.ts
461
465
  function flattenInputTuple(inputTuple) {
462
466
  return [
463
467
  ...inputTuple[0],
@@ -506,7 +510,7 @@ function extractFacets(root) {
506
510
  while (extensions.length > 0) {
507
511
  const ext = extensions.pop();
508
512
  const pri = priorities.pop();
509
- if (ext instanceof FacetExtension) {
513
+ if (ext instanceof FacetExtensionImpl) {
510
514
  const facet = ext.facet;
511
515
  if (!facets[facet.index]) {
512
516
  facets[facet.index] = facet;
@@ -631,26 +635,179 @@ function updateExtension(prevInputs, prevConverters, extension, mode) {
631
635
  return { schemaInput, stateInput, viewInput, commandInput };
632
636
  }
633
637
 
634
- // src/editor/type-utils.ts
635
- function union(extension) {
636
- if (extension && Array.isArray(extension)) {
637
- return { extension };
638
+ // src/utils/type-assertion.ts
639
+ import { Mark, ProseMirrorNode } from "@prosekit/pm/model";
640
+ import {
641
+ AllSelection,
642
+ NodeSelection,
643
+ TextSelection as TextSelection2
644
+ } from "@prosekit/pm/state";
645
+ function isProseMirrorNode(node) {
646
+ return node instanceof ProseMirrorNode;
647
+ }
648
+ function isMark(mark) {
649
+ return mark instanceof Mark;
650
+ }
651
+ function isTextSelection(sel) {
652
+ return sel instanceof TextSelection2;
653
+ }
654
+ function isNodeSelection(sel) {
655
+ return sel instanceof NodeSelection;
656
+ }
657
+ function isAllSelection(sel) {
658
+ return sel instanceof AllSelection;
659
+ }
660
+
661
+ // src/utils/is-mark-active.ts
662
+ function isMarkActive(state, type, attrs) {
663
+ const markType = getMarkType(state.schema, type);
664
+ const mark = attrs ? markType.create(attrs) : markType;
665
+ const { from, $from, to, empty } = state.selection;
666
+ if (empty) {
667
+ return hasMark(state.storedMarks || $from.marks(), mark);
668
+ } else {
669
+ return state.doc.rangeHasMark(from, to, mark);
670
+ }
671
+ }
672
+ function hasMark(marks, mark) {
673
+ if (marks.length === 0) {
674
+ return false;
675
+ }
676
+ if (isMark(mark)) {
677
+ return marks.some((m) => m.eq(mark));
678
+ } else {
679
+ return marks.some((m) => m.type === mark);
680
+ }
681
+ }
682
+
683
+ // src/editor/builder.ts
684
+ import "@prosekit/pm/model";
685
+ function createNodeBuilder(getState, type) {
686
+ const builder = (...args) => buildNode(type, args);
687
+ builder.isActive = (attrs) => {
688
+ const state = getState();
689
+ return state ? isNodeActive(state, type, attrs) : false;
690
+ };
691
+ return builder;
692
+ }
693
+ function createMarkBuilder(getState, type) {
694
+ const builder = (...args) => buildMark(type, args);
695
+ builder.isActive = (attrs) => {
696
+ const state = getState();
697
+ return state ? isMarkActive(state, type, attrs) : false;
698
+ };
699
+ return builder;
700
+ }
701
+ function buildMark(type, args) {
702
+ const [attrs, children] = normalizeArgs(args);
703
+ return flattenChildren(type.schema, children, type.create(attrs));
704
+ }
705
+ function buildNode(type, args) {
706
+ const [attrs, children] = normalizeArgs(args);
707
+ const node = type.createAndFill(attrs, flattenChildren(type.schema, children));
708
+ if (!node) {
709
+ throw new ProseKitError(`Couldn't create node ${type.name}`);
638
710
  }
639
- return extension;
711
+ return node;
712
+ }
713
+ function flattenChildren(schema, children, mark) {
714
+ const nodes = [];
715
+ for (const child of children) {
716
+ if (typeof child === "string") {
717
+ if (child) {
718
+ nodes.push(schema.text(child, mark ? [mark] : null));
719
+ }
720
+ } else if (Array.isArray(child)) {
721
+ nodes.push(...flattenChildren(schema, child, mark));
722
+ } else if (isProseMirrorNode(child)) {
723
+ nodes.push(mark ? child.mark(mark.addToSet(child.marks)) : child);
724
+ } else {
725
+ throw new ProseKitError(`Invalid node child: ${typeof child}`);
726
+ }
727
+ }
728
+ return nodes;
729
+ }
730
+ function normalizeArgs(args) {
731
+ const [attrs, ...children] = args;
732
+ if (isNodeChild(args)) {
733
+ children.unshift(args);
734
+ return [null, children];
735
+ } else if (typeof attrs === "object") {
736
+ return [attrs, children];
737
+ } else {
738
+ return [null, children];
739
+ }
740
+ }
741
+ function isNodeChild(value) {
742
+ if (!value) {
743
+ return false;
744
+ }
745
+ return typeof value === "string" || Array.isArray(value) || isProseMirrorNode(value);
746
+ }
747
+
748
+ // src/facets/union-extension.ts
749
+ import { Schema as Schema5 } from "@prosekit/pm/model";
750
+ var UnionExtensionImpl = class extends BaseExtension {
751
+ constructor(extension = []) {
752
+ super();
753
+ this.extension = extension;
754
+ this._schema = void 0;
755
+ this.hasSchemaCount = 0;
756
+ for (const e of extension) {
757
+ if (e instanceof BaseExtension) {
758
+ this.hasSchemaCount += e.hasSchema ? 1 : 0;
759
+ } else {
760
+ throw new ProseKitError("Invalid extension");
761
+ }
762
+ }
763
+ }
764
+ get hasSchema() {
765
+ return this.hasSchemaCount > 0;
766
+ }
767
+ get schema() {
768
+ var _a;
769
+ if (this._schema !== void 0) {
770
+ return this._schema;
771
+ }
772
+ if (this.hasSchemaCount === 0) {
773
+ this._schema = null;
774
+ return this._schema;
775
+ }
776
+ if (this.hasSchemaCount === 1) {
777
+ const schema = (_a = this.extension.find((e) => e.hasSchema)) == null ? void 0 : _a.schema;
778
+ if (schema) {
779
+ this._schema = schema;
780
+ return this._schema;
781
+ }
782
+ }
783
+ const { schemaInput } = updateExtension([], [], this, "add");
784
+ this._schema = schemaInput ? new Schema5(schemaInput) : null;
785
+ return this._schema;
786
+ }
787
+ };
788
+
789
+ // src/editor/union.ts
790
+ function union(extension) {
791
+ const array = Array.isArray(extension) ? extension : [extension];
792
+ return new UnionExtensionImpl(
793
+ array
794
+ );
640
795
  }
641
796
 
642
797
  // src/editor/editor.ts
643
798
  function createEditor({
644
799
  extension,
645
800
  defaultDoc,
801
+ defaultHTML,
646
802
  defaultSelection
647
803
  }) {
648
- if (defaultDoc) {
804
+ if (defaultDoc || defaultHTML) {
649
805
  extension = union([
650
806
  extension,
651
807
  defineDefaultState({
652
- doc: defaultDoc,
653
- selection: defaultSelection
808
+ defaultDoc,
809
+ defaultHTML,
810
+ defaultSelection
654
811
  })
655
812
  ]);
656
813
  }
@@ -668,7 +825,7 @@ var EditorInstance = class {
668
825
  if (!schemaInput) {
669
826
  throw new ProseKitError("Schema must be defined");
670
827
  }
671
- const schema = new Schema4(schemaInput);
828
+ const schema = new Schema6(schemaInput);
672
829
  const stateConfig = stateInput ? stateInput({ schema }) : { schema };
673
830
  const state = EditorState.create(stateConfig);
674
831
  if (commandInput) {
@@ -859,7 +1016,9 @@ var Editor = class _Editor {
859
1016
 
860
1017
  // src/editor/with-priority.ts
861
1018
  function withPriority(extension, priority) {
862
- return { extension, priority };
1019
+ const result = union(extension);
1020
+ result.priority = priority;
1021
+ return result;
863
1022
  }
864
1023
 
865
1024
  // src/commands/insert-text.ts
@@ -877,10 +1036,10 @@ function insertText({
877
1036
  }
878
1037
 
879
1038
  // src/commands/select-all.ts
880
- import { AllSelection } from "@prosekit/pm/state";
1039
+ import { AllSelection as AllSelection2 } from "@prosekit/pm/state";
881
1040
  function selectAll() {
882
1041
  return (state, dispatch) => {
883
- dispatch == null ? void 0 : dispatch(state.tr.setSelection(new AllSelection(state.doc)));
1042
+ dispatch == null ? void 0 : dispatch(state.tr.setSelection(new AllSelection2(state.doc)));
884
1043
  return true;
885
1044
  };
886
1045
  }
@@ -936,7 +1095,9 @@ function defineBaseCommands() {
936
1095
  insertNode: insertNode2,
937
1096
  wrap,
938
1097
  setBlockType,
939
- selectAll
1098
+ selectAll,
1099
+ addMark,
1100
+ removeMark
940
1101
  });
941
1102
  }
942
1103
 
@@ -1233,7 +1394,6 @@ function defineText() {
1233
1394
  export {
1234
1395
  Editor,
1235
1396
  Facet,
1236
- FacetExtension,
1237
1397
  Priority,
1238
1398
  ProseKitError,
1239
1399
  addMark,
@@ -1254,10 +1414,20 @@ export {
1254
1414
  defineParagraph,
1255
1415
  definePlugin,
1256
1416
  defineText,
1417
+ elementToJSON,
1418
+ elementToNode,
1257
1419
  getMarkType,
1258
1420
  getNodeType,
1421
+ htmlToJSON,
1422
+ htmlToNode,
1259
1423
  insertNode,
1424
+ isAllSelection,
1425
+ isMark,
1426
+ isNodeSelection,
1427
+ isProseMirrorNode,
1428
+ isTextSelection,
1260
1429
  pluginFacet,
1430
+ removeMark,
1261
1431
  setBlockType,
1262
1432
  toggleMark,
1263
1433
  toggleNode,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.0.17",
4
+ "version": "0.0.19",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",