@prosekit/core 0.0.3 → 0.0.5

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,4 +1,4 @@
1
- import { MarkType, Attrs, Schema, ProseMirrorNode, NodeType, MarkSpec, NodeSpec } from '@prosekit/pm/model';
1
+ import { MarkType, Attrs, NodeType, Schema, ProseMirrorNode, MarkSpec, NodeSpec } from '@prosekit/pm/model';
2
2
  import { Command, EditorStateConfig, Plugin } from '@prosekit/pm/state';
3
3
  import { EditorView, NodeViewConstructor, DirectEditorProps } from '@prosekit/pm/view';
4
4
  import { ConditionalExcept, EmptyObject, Simplify, UnionToIntersection } from 'type-fest';
@@ -10,6 +10,12 @@ interface ToggleMarkOptions {
10
10
  }
11
11
  declare function toggleMark(options: ToggleMarkOptions): Command;
12
12
 
13
+ interface ToggleNodeOptions {
14
+ type: string | NodeType;
15
+ attrs?: Attrs | null;
16
+ }
17
+ declare function toggleNode(options: ToggleNodeOptions): Command;
18
+
13
19
  type CommandDispatcher<Args extends any[] = any[]> = (...arg: Args) => boolean;
14
20
  type CommandCreator<Args extends any[] = any[]> = (...arg: Args) => Command;
15
21
  /** @internal */
@@ -54,6 +60,11 @@ declare const enum Priority {
54
60
  highest = 0
55
61
  }
56
62
 
63
+ /**
64
+ * @intneral
65
+ */
66
+ type SimplifyUnion<T> = Simplify<UnionToIntersection<T>>;
67
+
57
68
  /**
58
69
  * @public
59
70
  */
@@ -115,6 +126,8 @@ declare class Editor<E extends Extension = any> {
115
126
  mount(place: HTMLElement | null | undefined | void): void;
116
127
  unmount(): void;
117
128
  use(extension: Extension): VoidFunction;
129
+ isNodeActive(nodeType: string | NodeType, attrs?: Attrs): boolean;
130
+ isMarkActive(markType: string | MarkType, attrs?: Attrs): boolean;
118
131
  }
119
132
 
120
133
  /** @public */
@@ -263,21 +276,22 @@ interface PluginOptions {
263
276
  }
264
277
  /** @public */
265
278
  declare function addPlugin({ plugins }: PluginOptions): Extension;
279
+ /** @internal */
280
+ type PluginFacetInput = (context: {
281
+ schema: Schema;
282
+ }) => Plugin[];
283
+ /** @internal */
284
+ declare const pluginFacet: Facet<PluginFacetInput, StateConfigCallback>;
266
285
 
267
286
  /** @public */
268
287
  declare function addText(): Extension<{
269
288
  NODES: "text";
270
289
  }>;
271
290
 
272
- /**
273
- * @intneral
274
- */
275
- type SimplifyUnion<T> = Simplify<UnionToIntersection<T>>;
276
-
277
291
  /** @internal */
278
292
  declare function getMarkType(schema: Schema, type: string | MarkType): MarkType;
279
293
 
280
294
  /** @internal */
281
295
  declare function getNodeType(schema: Schema, type: string | NodeType): NodeType;
282
296
 
283
- export { CommandArgs, Editor, EditorOptions, Extension, ExtensionTyping, ExtractCommandCreators, ExtractCommandDispatchers, ExtractMarks, ExtractNodes, Facet, FacetExtension, FacetOptions, Keymap, MarkSpecOptions, NodeSpecOptions, NodeViewOptions, PluginOptions, Priority, ProseKitError, SimplifyUnion, StateConfigCallback, StateConfigContext, ViewProps, addBaseCommands, addBaseKeymap, addCommands, addDoc, addInputRule, addKeymap, addMarkSpec, addNodeSpec, addNodeView, addParagraph, addPlugin, addText, createEditor, defineExtension, getMarkType, getNodeType, toggleMark, withPriority };
297
+ export { CommandArgs, Editor, EditorOptions, Extension, ExtensionTyping, ExtractCommandCreators, ExtractCommandDispatchers, ExtractMarks, ExtractNodes, Facet, FacetExtension, FacetOptions, Keymap, MarkSpecOptions, NodeSpecOptions, NodeViewOptions, PluginFacetInput, PluginOptions, Priority, ProseKitError, SimplifyUnion, StateConfigCallback, StateConfigContext, ToggleMarkOptions, ToggleNodeOptions, ViewProps, addBaseCommands, addBaseKeymap, addCommands, addDoc, addInputRule, addKeymap, addMarkSpec, addNodeSpec, addNodeView, addParagraph, addPlugin, addText, createEditor, defineExtension, getMarkType, getNodeType, pluginFacet, toggleMark, toggleNode, withPriority };
@@ -1,5 +1,10 @@
1
1
  // src/commands/toggle-mark.ts
2
2
  import { toggleMark as baseToggleMark } from "@prosekit/pm/commands";
3
+ import "@prosekit/pm/model";
4
+ import "@prosekit/pm/state";
5
+
6
+ // src/utils/get-mark-type.ts
7
+ import "@prosekit/pm/model";
3
8
 
4
9
  // src/error.ts
5
10
  var ProseKitError = class extends Error {
@@ -27,8 +32,85 @@ function toggleMark(options) {
27
32
  };
28
33
  }
29
34
 
35
+ // src/commands/toggle-node.ts
36
+ import { setBlockType } from "@prosekit/pm/commands";
37
+ import "@prosekit/pm/model";
38
+ import "@prosekit/pm/state";
39
+
40
+ // src/utils/get-node-type.ts
41
+ import "@prosekit/pm/model";
42
+ function getNodeType(schema, type) {
43
+ if (typeof type === "string") {
44
+ const nodeType = schema.nodes[type];
45
+ if (!nodeType) {
46
+ throw new ProseKitError(`Cannot find ProseMirror node type "${type}"`);
47
+ }
48
+ return nodeType;
49
+ }
50
+ return type;
51
+ }
52
+
53
+ // src/utils/object-euqal.ts
54
+ function objectEqual(a, b) {
55
+ if (a === b) {
56
+ return true;
57
+ }
58
+ if (typeof a !== "object" || typeof b !== "object") {
59
+ return false;
60
+ }
61
+ if (a === null || b === null) {
62
+ return false;
63
+ }
64
+ if (Array.isArray(a) || Array.isArray(b)) {
65
+ return false;
66
+ }
67
+ const aKeys = Object.keys(a);
68
+ const bKeys = Object.keys(b);
69
+ if (aKeys.length !== bKeys.length) {
70
+ return false;
71
+ }
72
+ for (const key of aKeys) {
73
+ if (!bKeys.includes(key)) {
74
+ return false;
75
+ }
76
+ if (!objectEqual(a[key], b[key])) {
77
+ return false;
78
+ }
79
+ }
80
+ return true;
81
+ }
82
+
83
+ // src/utils/is-node-active.ts
84
+ function isNodeActive(state, type, attrs) {
85
+ const $pos = state.selection.$from;
86
+ const nodeType = getNodeType(state.schema, type);
87
+ for (let depth = $pos.depth; depth >= 0; depth--) {
88
+ const node = $pos.node(depth);
89
+ if (node.type === nodeType && (!attrs || objectEqual(attrs, node.attrs))) {
90
+ return true;
91
+ }
92
+ }
93
+ return false;
94
+ }
95
+
96
+ // src/commands/toggle-node.ts
97
+ function toggleNode(options) {
98
+ return (state, dispatch, view) => {
99
+ if (isNodeActive(state, options.type, options.attrs)) {
100
+ const defaultType = state.schema.topNodeType.contentMatch.defaultType;
101
+ if (!defaultType) {
102
+ return false;
103
+ }
104
+ return setBlockType(defaultType)(state, dispatch, view);
105
+ } else {
106
+ const nodeType = getNodeType(state.schema, options.type);
107
+ return setBlockType(nodeType, options.attrs)(state, dispatch, view);
108
+ }
109
+ };
110
+ }
111
+
30
112
  // src/editor/editor.ts
31
- import { Schema } from "@prosekit/pm/model";
113
+ import { Schema as Schema3 } from "@prosekit/pm/model";
32
114
  import { EditorState } from "@prosekit/pm/state";
33
115
  import { EditorView } from "@prosekit/pm/view";
34
116
 
@@ -36,6 +118,14 @@ import { EditorView } from "@prosekit/pm/view";
36
118
  function voidFunction() {
37
119
  }
38
120
 
121
+ // src/utils/is-mark-active.ts
122
+ function isMarkActive(state, type, attrs) {
123
+ const markType = getMarkType(state.schema, type);
124
+ const mark = attrs ? markType.create(attrs) : markType;
125
+ const { from, to } = state.selection;
126
+ return state.doc.rangeHasMark(from, to, mark);
127
+ }
128
+
39
129
  // src/types/priority.ts
40
130
  var Priority = /* @__PURE__ */ ((Priority2) => {
41
131
  Priority2[Priority2["lowest"] = 4] = "lowest";
@@ -263,7 +353,7 @@ function createEditor({
263
353
  if (!schemaInput) {
264
354
  throw new Error("Schema must be defined");
265
355
  }
266
- const schema = new Schema(schemaInput);
356
+ const schema = new Schema3(schemaInput);
267
357
  const stateConfig = stateInput ? stateInput({ schema }) : { schema };
268
358
  const state = EditorState.create(stateConfig);
269
359
  const directEditorProps = { state, ...viewInput };
@@ -411,6 +501,12 @@ var Editor = class _Editor {
411
501
  }
412
502
  return voidFunction;
413
503
  }
504
+ isNodeActive(nodeType, attrs) {
505
+ return isNodeActive(this.view.state, nodeType, attrs);
506
+ }
507
+ isMarkActive(markType, attrs) {
508
+ return isMarkActive(this.view.state, markType, attrs);
509
+ }
414
510
  };
415
511
 
416
512
  // src/editor/type-utils.ts
@@ -427,6 +523,7 @@ function withPriority(extension, priority) {
427
523
  }
428
524
 
429
525
  // src/extensions/command.ts
526
+ import "@prosekit/pm/model";
430
527
  import { AllSelection, Selection } from "@prosekit/pm/state";
431
528
  import { findWrapping, insertPoint } from "@prosekit/pm/transform";
432
529
  function addCommands(commands) {
@@ -536,8 +633,12 @@ function addDoc() {
536
633
 
537
634
  // src/extensions/input-rules.ts
538
635
  import { inputRules } from "@prosekit/pm/inputrules";
636
+ import "@prosekit/pm/model";
637
+ import "@prosekit/pm/state";
539
638
 
540
639
  // src/extensions/plugin.ts
640
+ import "@prosekit/pm/model";
641
+ import "@prosekit/pm/state";
541
642
  function addPlugin({ plugins }) {
542
643
  if (typeof plugins === "function") {
543
644
  return pluginFacet.extension([plugins]);
@@ -574,6 +675,7 @@ var inputRuleFacet = Facet.define({
574
675
  // src/extensions/keymap.ts
575
676
  import { baseKeymap, chainCommands } from "@prosekit/pm/commands";
576
677
  import { keymap as createKeymapPlugin } from "@prosekit/pm/keymap";
678
+ import "@prosekit/pm/state";
577
679
  function addKeymap(keymap) {
578
680
  return keymapFacet.extension([keymap]);
579
681
  }
@@ -624,7 +726,8 @@ var markSpecFacet = Facet.define({
624
726
  });
625
727
 
626
728
  // src/extensions/node-view.ts
627
- import { Plugin as Plugin2 } from "@prosekit/pm/state";
729
+ import { Plugin as Plugin4 } from "@prosekit/pm/state";
730
+ import "@prosekit/pm/view";
628
731
  function addNodeView(options) {
629
732
  return nodeViewFacet.extension([options]);
630
733
  }
@@ -636,7 +739,7 @@ var nodeViewFacet = Facet.define({
636
739
  nodeViews[input.name] = input.constructor;
637
740
  }
638
741
  }
639
- return () => [new Plugin2({ props: { nodeViews } })];
742
+ return () => [new Plugin4({ props: { nodeViews } })];
640
743
  },
641
744
  next: pluginFacet
642
745
  });
@@ -665,18 +768,6 @@ function addText() {
665
768
  }
666
769
  });
667
770
  }
668
-
669
- // src/utils/get-node-type.ts
670
- function getNodeType(schema, type) {
671
- if (typeof type === "string") {
672
- const nodeType = schema.nodes[type];
673
- if (!nodeType) {
674
- throw new ProseKitError(`Cannot find ProseMirror node type "${type}"`);
675
- }
676
- return nodeType;
677
- }
678
- return type;
679
- }
680
771
  export {
681
772
  Editor,
682
773
  Facet,
@@ -699,6 +790,8 @@ export {
699
790
  defineExtension,
700
791
  getMarkType,
701
792
  getNodeType,
793
+ pluginFacet,
702
794
  toggleMark,
795
+ toggleNode,
703
796
  withPriority
704
797
  };
package/dist/style.css CHANGED
@@ -1,4 +1,4 @@
1
- /* ../../node_modules/.pnpm/prosemirror-view@1.31.5/node_modules/prosemirror-view/style/prosemirror.css */
1
+ /* ../../node_modules/.pnpm/prosemirror-view@1.31.6/node_modules/prosemirror-view/style/prosemirror.css */
2
2
  .ProseMirror {
3
3
  position: relative;
4
4
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "@prosekit/pm": "^0.0.3",
42
42
  "orderedmap": "^2.1.1",
43
- "type-fest": "^3.12.0"
43
+ "type-fest": "^4.0.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@prosekit/dev": "*",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { toggleMark } from './commands/toggle-mark'
1
+ export { toggleMark, type ToggleMarkOptions } from './commands/toggle-mark'
2
+ export { toggleNode, type ToggleNodeOptions } from './commands/toggle-node'
2
3
  export { Editor, createEditor, type EditorOptions } from './editor/editor'
3
4
  export { Facet, FacetExtension, type FacetOptions } from './editor/facet'
4
5
  export { defineExtension } from './editor/type-utils'
@@ -12,7 +13,12 @@ export { addMarkSpec, type MarkSpecOptions } from './extensions/mark-spec'
12
13
  export { addNodeSpec, type NodeSpecOptions } from './extensions/node-spec'
13
14
  export { addNodeView, type NodeViewOptions } from './extensions/node-view'
14
15
  export { addParagraph } from './extensions/paragraph'
15
- export { addPlugin, type PluginOptions } from './extensions/plugin'
16
+ export {
17
+ addPlugin,
18
+ type PluginOptions,
19
+ type PluginFacetInput,
20
+ pluginFacet,
21
+ } from './extensions/plugin'
16
22
  export { addText } from './extensions/text'
17
23
  export { type CommandArgs as CommandArgs } from './types/command'
18
24
  export * from './types/editor'