@prosekit/core 0.2.4 → 0.2.6

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.
@@ -41,7 +41,9 @@ declare type Action = (options: {
41
41
  }) => boolean;
42
42
 
43
43
  /**
44
- * Add the given mark to the inline content.
44
+ * Returns a command that adds the given mark with the given attributes.
45
+ *
46
+ * @public
45
47
  */
46
48
  declare function addMark(options: {
47
49
  /**
@@ -165,7 +167,7 @@ declare type ConverterTuple = Tuple5<FacetConverter | undefined>;
165
167
  /**
166
168
  * @public
167
169
  */
168
- declare function createEditor<E extends Extension>({ extension, defaultDoc, defaultHTML, defaultSelection, }: EditorOptions<E>): Editor<E>;
170
+ declare function createEditor<E extends Extension>(options: EditorOptions<E>): Editor<E>;
169
171
  export { createEditor }
170
172
  export { createEditor as createEditor_alias_1 }
171
173
 
@@ -241,6 +243,11 @@ declare function defineBaseCommands(): Extension<{
241
243
  from?: number | undefined;
242
244
  to?: number | undefined;
243
245
  }];
246
+ setNodeAttrs: [options: {
247
+ type: string | NodeType_2;
248
+ attrs: Attrs_2;
249
+ pos?: number | undefined;
250
+ }];
244
251
  selectAll: [];
245
252
  addMark: [options: {
246
253
  type: string | MarkType_2;
@@ -517,6 +524,7 @@ declare class Editor<E extends Extension = any> {
517
524
  */
518
525
  blur(): void;
519
526
  use(extension: Extension): VoidFunction;
527
+ get state(): EditorState;
520
528
  get nodes(): Record<ExtractNodes<E>, NodeBuilder>;
521
529
  get marks(): Record<ExtractMarks<E>, MarkBuilder>;
522
530
  }
@@ -747,6 +755,12 @@ declare function getNodeType(schema: Schema, type: string | NodeType): NodeType;
747
755
  export { getNodeType }
748
756
  export { getNodeType as getNodeType_alias_1 }
749
757
 
758
+ /**
759
+ * Returns a command that inserts the given node at the current selection or at
760
+ * the given position.
761
+ *
762
+ * @public
763
+ */
750
764
  declare function insertNode(options: {
751
765
  node: ProseMirrorNode;
752
766
  pos?: number;
@@ -766,6 +780,11 @@ export declare function insertNodeAction({ node, pos, }: {
766
780
  pos?: number;
767
781
  }): Action;
768
782
 
783
+ /**
784
+ * Returns a command that inserts the given text.
785
+ *
786
+ * @public
787
+ */
769
788
  export declare function insertText({ text, from, to, }: {
770
789
  text: string;
771
790
  from?: number;
@@ -781,6 +800,15 @@ export { isAllSelection as isAllSelection_alias_1 }
781
800
 
782
801
  export declare function isElement(value: unknown): value is Element;
783
802
 
803
+ /**
804
+ * Check if the selection is in a code block.
805
+ *
806
+ * @internal
807
+ */
808
+ declare function isInCodeBlock(selection: Selection_2): boolean | undefined;
809
+ export { isInCodeBlock }
810
+ export { isInCodeBlock as isInCodeBlock_alias_1 }
811
+
784
812
  export declare const isMac: boolean;
785
813
 
786
814
  /**
@@ -1101,7 +1129,9 @@ export { ProseKitError }
1101
1129
  export { ProseKitError as ProseKitError_alias_1 }
1102
1130
 
1103
1131
  /**
1104
- * Remove the given mark from the inline content.
1132
+ * Returns a command that removes the given mark.
1133
+ *
1134
+ * @public
1105
1135
  */
1106
1136
  declare function removeMark(options: {
1107
1137
  /**
@@ -1128,6 +1158,11 @@ export declare const schemaFacet: Facet<SchemaPayload, SchemaPayload>;
1128
1158
 
1129
1159
  export declare type SchemaPayload = SchemaSpec;
1130
1160
 
1161
+ /**
1162
+ * Returns a command that selects the whole document.
1163
+ *
1164
+ * @public
1165
+ */
1131
1166
  export declare function selectAll(): Command;
1132
1167
 
1133
1168
  /**
@@ -1146,6 +1181,8 @@ export { SelectionJSON as SelectionJSON_alias_1 }
1146
1181
  /**
1147
1182
  * Returns a command that tries to set the selected textblocks to the given node
1148
1183
  * type with the given attributes.
1184
+ *
1185
+ * @public
1149
1186
  */
1150
1187
  declare function setBlockType(options: {
1151
1188
  type: NodeType | string;
@@ -1156,6 +1193,31 @@ declare function setBlockType(options: {
1156
1193
  export { setBlockType }
1157
1194
  export { setBlockType as setBlockType_alias_1 }
1158
1195
 
1196
+ /**
1197
+ * Returns a command that set the attributes of the current node.
1198
+ *
1199
+ * @public
1200
+ */
1201
+ declare function setNodeAttrs(options: {
1202
+ /**
1203
+ * The type of node to set the attributes of.
1204
+ *
1205
+ * If current node is not of this type, the command will do nothing.
1206
+ */
1207
+ type: string | NodeType;
1208
+ /**
1209
+ * The attributes to set.
1210
+ */
1211
+ attrs: Attrs;
1212
+ /**
1213
+ * The position of the node. Defaults to the position of the wrapping node
1214
+ * containing the current selection.
1215
+ */
1216
+ pos?: number;
1217
+ }): Command;
1218
+ export { setNodeAttrs }
1219
+ export { setNodeAttrs as setNodeAttrs_alias_1 }
1220
+
1159
1221
  export declare function setSelectionAround(tr: Transaction, pos: number): void;
1160
1222
 
1161
1223
  /**
@@ -1221,6 +1283,11 @@ export declare type ToCommandCreators<T extends CommandArgs> = {
1221
1283
  [K in keyof T]: CommandCreator<T[K]>;
1222
1284
  };
1223
1285
 
1286
+ /**
1287
+ * Returns a command that toggles the given mark with the given attributes.
1288
+ *
1289
+ * @public
1290
+ */
1224
1291
  declare function toggleMark({ type, attrs, }: {
1225
1292
  type: string | MarkType;
1226
1293
  attrs?: Attrs | null;
@@ -1228,6 +1295,12 @@ declare function toggleMark({ type, attrs, }: {
1228
1295
  export { toggleMark }
1229
1296
  export { toggleMark as toggleMark_alias_1 }
1230
1297
 
1298
+ /**
1299
+ * Returns a command that set the selected textblocks to the given node type
1300
+ * with the given attributes.
1301
+ *
1302
+ * @public
1303
+ */
1231
1304
  declare function toggleNode({ type, attrs, }: {
1232
1305
  type: string | NodeType;
1233
1306
  attrs?: Attrs | null;
@@ -1237,6 +1310,11 @@ export { toggleNode as toggleNode_alias_1 }
1237
1310
 
1238
1311
  declare type Tuple5<T> = [T, T, T, T, T];
1239
1312
 
1313
+ /**
1314
+ * Merge multiple extensions into one.
1315
+ *
1316
+ * @public
1317
+ */
1240
1318
  declare function union<E extends Extension | Extension[]>(extension: E): SimplifyExtension<E>;
1241
1319
  export { union }
1242
1320
  export { union as union_alias_1 }
@@ -1302,6 +1380,12 @@ declare function withSkipCodeBlock(command: Command): Command;
1302
1380
  export { withSkipCodeBlock }
1303
1381
  export { withSkipCodeBlock as withSkipCodeBlock_alias_1 }
1304
1382
 
1383
+ /**
1384
+ * Returns a command that wraps the selected textblock with the given node type
1385
+ * with the given attributes.
1386
+ *
1387
+ * @public
1388
+ */
1305
1389
  export declare function wrap({ nodeType, attrs, }: {
1306
1390
  nodeType: NodeType;
1307
1391
  attrs?: Attrs | null;
@@ -2,6 +2,7 @@ export { addMark } from './_tsup-dts-rollup';
2
2
  export { insertNode } from './_tsup-dts-rollup';
3
3
  export { removeMark } from './_tsup-dts-rollup';
4
4
  export { setBlockType } from './_tsup-dts-rollup';
5
+ export { setNodeAttrs } from './_tsup-dts-rollup';
5
6
  export { toggleMark } from './_tsup-dts-rollup';
6
7
  export { toggleNode } from './_tsup-dts-rollup';
7
8
  export { Editor } from './_tsup-dts-rollup';
@@ -70,6 +71,7 @@ export { defaultBlockAt } from './_tsup-dts-rollup';
70
71
  export { _getId } from './_tsup-dts-rollup';
71
72
  export { getMarkType } from './_tsup-dts-rollup';
72
73
  export { getNodeType } from './_tsup-dts-rollup';
74
+ export { isInCodeBlock } from './_tsup-dts-rollup';
73
75
  export { jsonFromElement } from './_tsup-dts-rollup';
74
76
  export { jsonFromHTML } from './_tsup-dts-rollup';
75
77
  export { jsonFromNode } from './_tsup-dts-rollup';
@@ -161,6 +161,29 @@ function setBlockType(options) {
161
161
  };
162
162
  }
163
163
 
164
+ // src/commands/set-node-attrs.ts
165
+ function setNodeAttrs(options) {
166
+ return (state, dispatch) => {
167
+ var _a;
168
+ const nodeType = getNodeType(state.schema, options.type);
169
+ const pos = (_a = options.pos) != null ? _a : state.selection.$from.before();
170
+ const node = state.doc.nodeAt(pos);
171
+ if (!node || node.type !== nodeType) {
172
+ return false;
173
+ }
174
+ if (dispatch) {
175
+ const { tr } = state;
176
+ for (const [key, value] of Object.entries(options.attrs)) {
177
+ if (value !== void 0) {
178
+ tr.setNodeAttribute(pos, key, value);
179
+ }
180
+ }
181
+ dispatch(tr);
182
+ }
183
+ return true;
184
+ };
185
+ }
186
+
164
187
  // src/commands/toggle-mark.ts
165
188
  import { toggleMark as baseToggleMark } from "@prosekit/pm/commands";
166
189
  import "@prosekit/pm/model";
@@ -825,12 +848,9 @@ function union(extension) {
825
848
  }
826
849
 
827
850
  // src/editor/editor.ts
828
- function createEditor({
829
- extension,
830
- defaultDoc,
831
- defaultHTML,
832
- defaultSelection
833
- }) {
851
+ function createEditor(options) {
852
+ const { defaultDoc, defaultHTML, defaultSelection } = options;
853
+ let extension = options.extension;
834
854
  if (defaultDoc || defaultHTML) {
835
855
  extension = union([
836
856
  extension,
@@ -858,6 +878,7 @@ var EditorInstance = class {
858
878
  const schema = new Schema6(schemaInput);
859
879
  const stateConfig = stateInput ? stateInput({ schema }) : { schema };
860
880
  const state = EditorState2.create(stateConfig);
881
+ this.cachedState = state;
861
882
  if (commandInput) {
862
883
  for (const [name, commandCreator] of Object.entries(commandInput)) {
863
884
  this.defineCommand(name, commandCreator);
@@ -865,10 +886,7 @@ var EditorInstance = class {
865
886
  }
866
887
  this.directEditorProps = { state, ...viewInput };
867
888
  this.schema = this.directEditorProps.state.schema;
868
- const getState = () => {
869
- var _a;
870
- return (_a = this.view) == null ? void 0 : _a.state;
871
- };
889
+ const getState = () => this.getState();
872
890
  this.nodeBuilders = Object.fromEntries(
873
891
  Object.values(this.schema.nodes).map((type) => [
874
892
  type.name,
@@ -882,6 +900,12 @@ var EditorInstance = class {
882
900
  ])
883
901
  );
884
902
  }
903
+ getState() {
904
+ if (this.view) {
905
+ this.cachedState = this.view.state;
906
+ }
907
+ return this.cachedState;
908
+ }
885
909
  updateExtension(extension, mode) {
886
910
  var _a;
887
911
  const { schemaInput, stateInput, viewInput, commandInput } = updateExtension(this.payloads, this.converters, extension, mode);
@@ -1054,6 +1078,9 @@ var Editor = class _Editor {
1054
1078
  this.instance.updateExtension(extension, "add");
1055
1079
  return () => this.instance.updateExtension(extension, "remove");
1056
1080
  }
1081
+ get state() {
1082
+ return this.instance.getState();
1083
+ }
1057
1084
  get nodes() {
1058
1085
  return this.instance.nodeBuilders;
1059
1086
  }
@@ -1123,6 +1150,7 @@ function defineBaseCommands() {
1123
1150
  insertNode,
1124
1151
  wrap,
1125
1152
  setBlockType,
1153
+ setNodeAttrs,
1126
1154
  selectAll,
1127
1155
  addMark,
1128
1156
  removeMark
@@ -1674,16 +1702,18 @@ function getId() {
1674
1702
  return `id:${id}`;
1675
1703
  }
1676
1704
 
1677
- // src/utils/unicode.ts
1678
- var OBJECT_REPLACEMENT_CHARACTER = "\uFFFC";
1679
-
1680
- // src/utils/with-skip-code-block.ts
1705
+ // src/utils/is-in-code-block.ts
1681
1706
  function isCodeBlockType(type) {
1682
1707
  return type.spec.code && type.isBlock;
1683
1708
  }
1684
1709
  function isInCodeBlock(selection) {
1685
1710
  return isCodeBlockType(selection.$from.parent.type) || isCodeBlockType(selection.$to.parent.type);
1686
1711
  }
1712
+
1713
+ // src/utils/unicode.ts
1714
+ var OBJECT_REPLACEMENT_CHARACTER = "\uFFFC";
1715
+
1716
+ // src/utils/with-skip-code-block.ts
1687
1717
  function withSkipCodeBlock(command) {
1688
1718
  return (state, dispatch, view) => {
1689
1719
  if (isInCodeBlock(state.selection)) {
@@ -1729,6 +1759,7 @@ export {
1729
1759
  getNodeType,
1730
1760
  insertNode,
1731
1761
  isAllSelection,
1762
+ isInCodeBlock,
1732
1763
  isMark,
1733
1764
  isNodeSelection,
1734
1765
  isProseMirrorNode,
@@ -1744,6 +1775,7 @@ export {
1744
1775
  pluginFacet,
1745
1776
  removeMark,
1746
1777
  setBlockType,
1778
+ setNodeAttrs,
1747
1779
  stateFromJSON,
1748
1780
  toggleMark,
1749
1781
  toggleNode,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.2.4",
4
+ "version": "0.2.6",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -44,7 +44,7 @@
44
44
  "@prosekit/dev": "*",
45
45
  "tsup": "^8.0.1",
46
46
  "typescript": "^5.3.3",
47
- "vitest": "^1.1.3"
47
+ "vitest": "^1.2.0"
48
48
  },
49
49
  "scripts": {
50
50
  "build:tsup": "tsup",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export { addMark } from './commands/add-mark'
2
2
  export { insertNode } from './commands/insert-node'
3
3
  export { removeMark } from './commands/remove-mark'
4
4
  export { setBlockType } from './commands/set-block-type'
5
+ export { setNodeAttrs } from './commands/set-node-attrs'
5
6
  export { toggleMark } from './commands/toggle-mark'
6
7
  export { toggleNode } from './commands/toggle-node'
7
8
  export { Editor, createEditor, type EditorOptions } from './editor/editor'
@@ -83,6 +84,7 @@ export { defaultBlockAt } from './utils/default-block-at'
83
84
  export { getId as _getId } from './utils/get-id'
84
85
  export { getMarkType } from './utils/get-mark-type'
85
86
  export { getNodeType } from './utils/get-node-type'
87
+ export { isInCodeBlock } from './utils/is-in-code-block'
86
88
  export {
87
89
  jsonFromElement,
88
90
  jsonFromHTML,