@tiptap/core 3.0.0-next.8 → 3.0.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.
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  PasteRule: () => PasteRule,
34
34
  Tracker: () => Tracker,
35
35
  callOrReturn: () => callOrReturn,
36
+ canInsertNode: () => canInsertNode,
36
37
  combineTransactionSteps: () => combineTransactionSteps,
37
38
  createChainableState: () => createChainableState,
38
39
  createDocument: () => createDocument,
@@ -119,6 +120,7 @@ __export(index_exports, {
119
120
  textInputRule: () => textInputRule,
120
121
  textPasteRule: () => textPasteRule,
121
122
  textblockTypeInputRule: () => textblockTypeInputRule,
123
+ updateMarkViewAttributes: () => updateMarkViewAttributes,
122
124
  wrappingInputRule: () => wrappingInputRule
123
125
  });
124
126
  module.exports = __toCommonJS(index_exports);
@@ -1950,8 +1952,13 @@ var Mark = class _Mark extends Extendable {
1950
1952
  super(...arguments);
1951
1953
  this.type = "mark";
1952
1954
  }
1955
+ /**
1956
+ * Create a new Mark instance
1957
+ * @param config - Mark configuration object or a function that returns a configuration object
1958
+ */
1953
1959
  static create(config = {}) {
1954
- return new _Mark(config);
1960
+ const resolvedConfig = typeof config === "function" ? config() : config;
1961
+ return new _Mark(resolvedConfig);
1955
1962
  }
1956
1963
  static handleExit({ editor, mark }) {
1957
1964
  const { tr } = editor.state;
@@ -1977,7 +1984,8 @@ var Mark = class _Mark extends Extendable {
1977
1984
  return super.configure(options);
1978
1985
  }
1979
1986
  extend(extendedConfig) {
1980
- return super.extend(extendedConfig);
1987
+ const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
1988
+ return super.extend(resolvedConfig);
1981
1989
  }
1982
1990
  };
1983
1991
 
@@ -2145,7 +2153,7 @@ function pasteRulesPlugin(props) {
2145
2153
  dropEvent = event;
2146
2154
  if (!isDroppedFromProseMirror) {
2147
2155
  const dragFromOtherEditor = tiptapDragFromOtherEditor;
2148
- if (dragFromOtherEditor) {
2156
+ if (dragFromOtherEditor == null ? void 0 : dragFromOtherEditor.isEditable) {
2149
2157
  setTimeout(() => {
2150
2158
  const selection = dragFromOtherEditor.state.selection;
2151
2159
  if (selection) {
@@ -2384,7 +2392,10 @@ var ExtensionManager = class {
2384
2392
  // tiptap-specific
2385
2393
  editor,
2386
2394
  extension,
2387
- HTMLAttributes
2395
+ HTMLAttributes,
2396
+ updateAttributes: (attrs) => {
2397
+ updateMarkViewAttributes(mark, editor, attrs);
2398
+ }
2388
2399
  });
2389
2400
  };
2390
2401
  return [extension.name, markView];
@@ -2482,14 +2493,20 @@ var Extension = class _Extension extends Extendable {
2482
2493
  super(...arguments);
2483
2494
  this.type = "extension";
2484
2495
  }
2496
+ /**
2497
+ * Create a new Extension instance
2498
+ * @param config - Extension configuration object or a function that returns a configuration object
2499
+ */
2485
2500
  static create(config = {}) {
2486
- return new _Extension(config);
2501
+ const resolvedConfig = typeof config === "function" ? config() : config;
2502
+ return new _Extension(resolvedConfig);
2487
2503
  }
2488
2504
  configure(options) {
2489
2505
  return super.configure(options);
2490
2506
  }
2491
2507
  extend(extendedConfig) {
2492
- return super.extend(extendedConfig);
2508
+ const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
2509
+ return super.extend(resolvedConfig);
2493
2510
  }
2494
2511
  };
2495
2512
 
@@ -2659,7 +2676,7 @@ var cut = (originRange, targetPos) => ({ editor, tr }) => {
2659
2676
  tr.deleteRange(originRange.from, originRange.to);
2660
2677
  const newPos = tr.mapping.map(targetPos);
2661
2678
  tr.insert(newPos, contentSlice.content);
2662
- tr.setSelection(new import_state8.TextSelection(tr.doc.resolve(newPos - 1)));
2679
+ tr.setSelection(new import_state8.TextSelection(tr.doc.resolve(Math.max(newPos - 1, 0))));
2663
2680
  return true;
2664
2681
  };
2665
2682
 
@@ -2835,18 +2852,10 @@ var insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) =
2835
2852
  };
2836
2853
  let content;
2837
2854
  const { selection } = editor.state;
2838
- try {
2839
- content = createNodeFromContent(value, editor.schema, {
2840
- parseOptions: {
2841
- preserveWhitespace: "full",
2842
- ...options.parseOptions
2843
- },
2844
- errorOnInvalidContent: (_a = options.errorOnInvalidContent) != null ? _a : editor.options.enableContentCheck
2845
- });
2846
- } catch (e) {
2855
+ const emitContentError = (error) => {
2847
2856
  editor.emit("contentError", {
2848
2857
  editor,
2849
- error: e,
2858
+ error,
2850
2859
  disableCollaboration: () => {
2851
2860
  if ("collaboration" in editor.storage && typeof editor.storage.collaboration === "object" && editor.storage.collaboration) {
2852
2861
  ;
@@ -2854,6 +2863,28 @@ var insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) =
2854
2863
  }
2855
2864
  }
2856
2865
  });
2866
+ };
2867
+ const parseOptions = {
2868
+ preserveWhitespace: "full",
2869
+ ...options.parseOptions
2870
+ };
2871
+ if (!options.errorOnInvalidContent && !editor.options.enableContentCheck && editor.options.emitContentError) {
2872
+ try {
2873
+ createNodeFromContent(value, editor.schema, {
2874
+ parseOptions,
2875
+ errorOnInvalidContent: true
2876
+ });
2877
+ } catch (e) {
2878
+ emitContentError(e);
2879
+ }
2880
+ }
2881
+ try {
2882
+ content = createNodeFromContent(value, editor.schema, {
2883
+ parseOptions,
2884
+ errorOnInvalidContent: (_a = options.errorOnInvalidContent) != null ? _a : editor.options.enableContentCheck
2885
+ });
2886
+ } catch (e) {
2887
+ emitContentError(e);
2857
2888
  return false;
2858
2889
  }
2859
2890
  let { from, to } = typeof position === "number" ? { from: position, to: position } : { from: position.from, to: position.to };
@@ -2895,7 +2926,8 @@ var insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) =
2895
2926
  newContent = content;
2896
2927
  const fromSelectionAtStart = selection.$from.parentOffset === 0;
2897
2928
  const isTextSelection2 = selection.$from.node().isText || selection.$from.node().isTextblock;
2898
- if (fromSelectionAtStart && isTextSelection2) {
2929
+ const hasContent = selection.$from.node().content.size > 0;
2930
+ if (fromSelectionAtStart && isTextSelection2 && hasContent) {
2899
2931
  from = Math.max(0, from - 1);
2900
2932
  }
2901
2933
  tr.replaceWith(from, to, newContent);
@@ -4186,6 +4218,9 @@ var NodePos = class _NodePos {
4186
4218
  const isBlock = node.isBlock && !node.isTextblock;
4187
4219
  const isNonTextAtom = node.isAtom && !node.isText;
4188
4220
  const targetPos = this.pos + offset + (isNonTextAtom ? 0 : 1);
4221
+ if (targetPos < 0 || targetPos > this.resolvedPos.doc.nodeSize - 2) {
4222
+ return;
4223
+ }
4189
4224
  const $pos = this.resolvedPos.doc.resolve(targetPos);
4190
4225
  if (!isBlock && $pos.depth <= this.depth) {
4191
4226
  return;
@@ -4383,6 +4418,7 @@ var Editor = class extends EventEmitter {
4383
4418
  enablePasteRules: true,
4384
4419
  enableCoreExtensions: true,
4385
4420
  enableContentCheck: false,
4421
+ emitContentError: false,
4386
4422
  onBeforeCreate: () => null,
4387
4423
  onCreate: () => null,
4388
4424
  onUpdate: () => null,
@@ -4548,7 +4584,8 @@ var Editor = class extends EventEmitter {
4548
4584
  // Stub some commonly accessed properties to prevent errors
4549
4585
  composing: false,
4550
4586
  dragging: null,
4551
- editable: true
4587
+ editable: true,
4588
+ isDestroyed: false
4552
4589
  },
4553
4590
  {
4554
4591
  get: (obj, key) => {
@@ -4601,7 +4638,7 @@ var Editor = class extends EventEmitter {
4601
4638
  let plugins = prevPlugins;
4602
4639
  [].concat(nameOrPluginKeyToRemove).forEach((nameOrPluginKey) => {
4603
4640
  const name = typeof nameOrPluginKey === "string" ? `${nameOrPluginKey}$` : nameOrPluginKey.key;
4604
- plugins = prevPlugins.filter((plugin) => !plugin.key.startsWith(name));
4641
+ plugins = plugins.filter((plugin) => !plugin.key.startsWith(name));
4605
4642
  });
4606
4643
  if (prevPlugins.length === plugins.length) {
4607
4644
  return void 0;
@@ -4862,8 +4899,8 @@ var Editor = class extends EventEmitter {
4862
4899
  * Check if the editor is already destroyed.
4863
4900
  */
4864
4901
  get isDestroyed() {
4865
- var _a;
4866
- return !((_a = this.view) == null ? void 0 : _a.docView);
4902
+ var _a, _b;
4903
+ return (_b = (_a = this.editorView) == null ? void 0 : _a.isDestroyed) != null ? _b : true;
4867
4904
  }
4868
4905
  $node(selector, attributes) {
4869
4906
  var _a;
@@ -5041,6 +5078,29 @@ var h = (tag, attributes) => {
5041
5078
  return [tag, rest, children];
5042
5079
  };
5043
5080
 
5081
+ // src/utilities/canInsertNode.ts
5082
+ var import_state22 = require("@tiptap/pm/state");
5083
+ function canInsertNode(state, nodeType) {
5084
+ const { selection } = state;
5085
+ const { $from } = selection;
5086
+ if (selection instanceof import_state22.NodeSelection) {
5087
+ const index = $from.index();
5088
+ const parent = $from.parent;
5089
+ return parent.canReplaceWith(index, index + 1, nodeType);
5090
+ }
5091
+ let depth = $from.depth;
5092
+ while (depth >= 0) {
5093
+ const index = $from.index(depth);
5094
+ const parent = $from.node(depth);
5095
+ const match = parent.contentMatchAt(index);
5096
+ if (match.matchType(nodeType)) {
5097
+ return true;
5098
+ }
5099
+ depth -= 1;
5100
+ }
5101
+ return false;
5102
+ }
5103
+
5044
5104
  // src/utilities/escapeForRegEx.ts
5045
5105
  function escapeForRegEx(string) {
5046
5106
  return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
@@ -5052,6 +5112,42 @@ function isString(value) {
5052
5112
  }
5053
5113
 
5054
5114
  // src/MarkView.ts
5115
+ function updateMarkViewAttributes(checkMark, editor, attrs = {}) {
5116
+ const { state } = editor;
5117
+ const { doc, tr } = state;
5118
+ const thisMark = checkMark;
5119
+ doc.descendants((node, pos) => {
5120
+ const from = tr.mapping.map(pos);
5121
+ const to = tr.mapping.map(pos) + node.nodeSize;
5122
+ let foundMark = null;
5123
+ node.marks.forEach((mark) => {
5124
+ if (mark !== thisMark) {
5125
+ return false;
5126
+ }
5127
+ foundMark = mark;
5128
+ });
5129
+ if (!foundMark) {
5130
+ return;
5131
+ }
5132
+ let needsUpdate = false;
5133
+ Object.keys(attrs).forEach((k) => {
5134
+ if (attrs[k] !== foundMark.attrs[k]) {
5135
+ needsUpdate = true;
5136
+ }
5137
+ });
5138
+ if (needsUpdate) {
5139
+ const updatedMark = checkMark.type.create({
5140
+ ...checkMark.attrs,
5141
+ ...attrs
5142
+ });
5143
+ tr.removeMark(from, to, checkMark.type);
5144
+ tr.addMark(from, to, updatedMark);
5145
+ }
5146
+ });
5147
+ if (tr.docChanged) {
5148
+ editor.view.dispatch(tr);
5149
+ }
5150
+ }
5055
5151
  var MarkView = class {
5056
5152
  constructor(component, props, options) {
5057
5153
  this.component = component;
@@ -5066,6 +5162,13 @@ var MarkView = class {
5066
5162
  get contentDOM() {
5067
5163
  return null;
5068
5164
  }
5165
+ /**
5166
+ * Update the attributes of the mark in the document.
5167
+ * @param attrs The attributes to update.
5168
+ */
5169
+ updateAttributes(attrs, checkMark) {
5170
+ updateMarkViewAttributes(checkMark || this.mark, this.editor, attrs);
5171
+ }
5069
5172
  ignoreMutation(mutation) {
5070
5173
  if (!this.dom || !this.contentDOM) {
5071
5174
  return true;
@@ -5098,19 +5201,25 @@ var Node3 = class _Node extends Extendable {
5098
5201
  super(...arguments);
5099
5202
  this.type = "node";
5100
5203
  }
5204
+ /**
5205
+ * Create a new Node instance
5206
+ * @param config - Node configuration object or a function that returns a configuration object
5207
+ */
5101
5208
  static create(config = {}) {
5102
- return new _Node(config);
5209
+ const resolvedConfig = typeof config === "function" ? config() : config;
5210
+ return new _Node(resolvedConfig);
5103
5211
  }
5104
5212
  configure(options) {
5105
5213
  return super.configure(options);
5106
5214
  }
5107
5215
  extend(extendedConfig) {
5108
- return super.extend(extendedConfig);
5216
+ const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
5217
+ return super.extend(resolvedConfig);
5109
5218
  }
5110
5219
  };
5111
5220
 
5112
5221
  // src/NodeView.ts
5113
- var import_state22 = require("@tiptap/pm/state");
5222
+ var import_state23 = require("@tiptap/pm/state");
5114
5223
  var NodeView = class {
5115
5224
  constructor(component, props, options) {
5116
5225
  this.isDragging = false;
@@ -5157,12 +5266,13 @@ var NodeView = class {
5157
5266
  x = handleBox.x - domBox.x + offsetX;
5158
5267
  y = handleBox.y - domBox.y + offsetY;
5159
5268
  }
5160
- (_g = event.dataTransfer) == null ? void 0 : _g.setDragImage(this.dom, x, y);
5269
+ const clonedNode = this.dom.cloneNode(true);
5270
+ (_g = event.dataTransfer) == null ? void 0 : _g.setDragImage(clonedNode, x, y);
5161
5271
  const pos = this.getPos();
5162
5272
  if (typeof pos !== "number") {
5163
5273
  return;
5164
5274
  }
5165
- const selection = import_state22.NodeSelection.create(view.state.doc, pos);
5275
+ const selection = import_state23.NodeSelection.create(view.state.doc, pos);
5166
5276
  const transaction = view.state.tr.setSelection(selection);
5167
5277
  view.dispatch(transaction);
5168
5278
  }
@@ -5188,7 +5298,7 @@ var NodeView = class {
5188
5298
  const { isEditable } = this.editor;
5189
5299
  const { isDragging } = this;
5190
5300
  const isDraggable = !!this.node.type.spec.draggable;
5191
- const isSelectable = import_state22.NodeSelection.isSelectable(this.node);
5301
+ const isSelectable = import_state23.NodeSelection.isSelectable(this.node);
5192
5302
  const isCopyEvent = event.type === "copy";
5193
5303
  const isPasteEvent = event.type === "paste";
5194
5304
  const isCutEvent = event.type === "cut";
@@ -5412,6 +5522,7 @@ var Tracker = class {
5412
5522
  PasteRule,
5413
5523
  Tracker,
5414
5524
  callOrReturn,
5525
+ canInsertNode,
5415
5526
  combineTransactionSteps,
5416
5527
  createChainableState,
5417
5528
  createDocument,
@@ -5498,6 +5609,7 @@ var Tracker = class {
5498
5609
  textInputRule,
5499
5610
  textPasteRule,
5500
5611
  textblockTypeInputRule,
5612
+ updateMarkViewAttributes,
5501
5613
  wrappingInputRule
5502
5614
  });
5503
5615
  //# sourceMappingURL=index.cjs.map