@prosekit/core 0.0.4 → 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 */
@@ -120,6 +126,8 @@ declare class Editor<E extends Extension = any> {
120
126
  mount(place: HTMLElement | null | undefined | void): void;
121
127
  unmount(): void;
122
128
  use(extension: Extension): VoidFunction;
129
+ isNodeActive(nodeType: string | NodeType, attrs?: Attrs): boolean;
130
+ isMarkActive(markType: string | MarkType, attrs?: Attrs): boolean;
123
131
  }
124
132
 
125
133
  /** @public */
@@ -286,4 +294,4 @@ declare function getMarkType(schema: Schema, type: string | MarkType): MarkType;
286
294
  /** @internal */
287
295
  declare function getNodeType(schema: Schema, type: string | NodeType): NodeType;
288
296
 
289
- export { CommandArgs, Editor, EditorOptions, Extension, ExtensionTyping, ExtractCommandCreators, ExtractCommandDispatchers, ExtractMarks, ExtractNodes, Facet, FacetExtension, FacetOptions, Keymap, MarkSpecOptions, NodeSpecOptions, NodeViewOptions, PluginFacetInput, PluginOptions, Priority, ProseKitError, SimplifyUnion, StateConfigCallback, StateConfigContext, ViewProps, addBaseCommands, addBaseKeymap, addCommands, addDoc, addInputRule, addKeymap, addMarkSpec, addNodeSpec, addNodeView, addParagraph, addPlugin, addText, createEditor, defineExtension, getMarkType, getNodeType, pluginFacet, 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 };
@@ -32,8 +32,85 @@ function toggleMark(options) {
32
32
  };
33
33
  }
34
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
+
35
112
  // src/editor/editor.ts
36
- import { Schema as Schema2 } from "@prosekit/pm/model";
113
+ import { Schema as Schema3 } from "@prosekit/pm/model";
37
114
  import { EditorState } from "@prosekit/pm/state";
38
115
  import { EditorView } from "@prosekit/pm/view";
39
116
 
@@ -41,6 +118,14 @@ import { EditorView } from "@prosekit/pm/view";
41
118
  function voidFunction() {
42
119
  }
43
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
+
44
129
  // src/types/priority.ts
45
130
  var Priority = /* @__PURE__ */ ((Priority2) => {
46
131
  Priority2[Priority2["lowest"] = 4] = "lowest";
@@ -268,7 +353,7 @@ function createEditor({
268
353
  if (!schemaInput) {
269
354
  throw new Error("Schema must be defined");
270
355
  }
271
- const schema = new Schema2(schemaInput);
356
+ const schema = new Schema3(schemaInput);
272
357
  const stateConfig = stateInput ? stateInput({ schema }) : { schema };
273
358
  const state = EditorState.create(stateConfig);
274
359
  const directEditorProps = { state, ...viewInput };
@@ -416,6 +501,12 @@ var Editor = class _Editor {
416
501
  }
417
502
  return voidFunction;
418
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
+ }
419
510
  };
420
511
 
421
512
  // src/editor/type-utils.ts
@@ -677,19 +768,6 @@ function addText() {
677
768
  }
678
769
  });
679
770
  }
680
-
681
- // src/utils/get-node-type.ts
682
- import "@prosekit/pm/model";
683
- function getNodeType(schema, type) {
684
- if (typeof type === "string") {
685
- const nodeType = schema.nodes[type];
686
- if (!nodeType) {
687
- throw new ProseKitError(`Cannot find ProseMirror node type "${type}"`);
688
- }
689
- return nodeType;
690
- }
691
- return type;
692
- }
693
771
  export {
694
772
  Editor,
695
773
  Facet,
@@ -714,5 +792,6 @@ export {
714
792
  getNodeType,
715
793
  pluginFacet,
716
794
  toggleMark,
795
+ toggleNode,
717
796
  withPriority
718
797
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.0.4",
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.13.1"
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'