@tiptap/core 2.5.8 → 2.6.0

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
@@ -966,7 +966,7 @@ function pasteRulesPlugin(props) {
966
966
 
967
967
  function findDuplicates(items) {
968
968
  const filtered = items.filter((el, index) => items.indexOf(el) !== index);
969
- return [...new Set(filtered)];
969
+ return Array.from(new Set(filtered));
970
970
  }
971
971
 
972
972
  class ExtensionManager {
@@ -2636,7 +2636,7 @@ function getMarksBetween(from, to, doc) {
2636
2636
  .resolve(from)
2637
2637
  .marks()
2638
2638
  .forEach(mark => {
2639
- const $pos = doc.resolve(from - 1);
2639
+ const $pos = doc.resolve(from);
2640
2640
  const range = getMarkRange($pos, mark.type);
2641
2641
  if (!range) {
2642
2642
  return;
@@ -2829,31 +2829,40 @@ function isList(name, extensions) {
2829
2829
  }
2830
2830
 
2831
2831
  /**
2832
- * Returns true if the given node is empty.
2833
- * When `checkChildren` is true (default), it will also check if all children are empty.
2832
+ * Returns true if the given prosemirror node is empty.
2834
2833
  */
2835
- function isNodeEmpty(node, { checkChildren } = { checkChildren: true }) {
2834
+ function isNodeEmpty(node, { checkChildren = true, ignoreWhitespace = false, } = {}) {
2835
+ var _a;
2836
+ if (ignoreWhitespace) {
2837
+ if (node.type.name === 'hardBreak') {
2838
+ // Hard breaks are considered empty
2839
+ return true;
2840
+ }
2841
+ if (node.isText) {
2842
+ return /^\s*$/m.test((_a = node.text) !== null && _a !== void 0 ? _a : '');
2843
+ }
2844
+ }
2836
2845
  if (node.isText) {
2837
2846
  return !node.text;
2838
2847
  }
2848
+ if (node.isAtom || node.isLeaf) {
2849
+ return false;
2850
+ }
2839
2851
  if (node.content.childCount === 0) {
2840
2852
  return true;
2841
2853
  }
2842
- if (node.isLeaf) {
2843
- return false;
2844
- }
2845
2854
  if (checkChildren) {
2846
- let hasSameContent = true;
2855
+ let isContentEmpty = true;
2847
2856
  node.content.forEach(childNode => {
2848
- if (hasSameContent === false) {
2857
+ if (isContentEmpty === false) {
2849
2858
  // Exit early for perf
2850
2859
  return;
2851
2860
  }
2852
- if (!isNodeEmpty(childNode)) {
2853
- hasSameContent = false;
2861
+ if (!isNodeEmpty(childNode, { ignoreWhitespace, checkChildren })) {
2862
+ isContentEmpty = false;
2854
2863
  }
2855
2864
  });
2856
- return hasSameContent;
2865
+ return isContentEmpty;
2857
2866
  }
2858
2867
  return false;
2859
2868
  }
@@ -3101,7 +3110,7 @@ const splitBlock = ({ keepMarks = true } = {}) => ({ tr, state: state$1, dispatc
3101
3110
  return can;
3102
3111
  };
3103
3112
 
3104
- const splitListItem = typeOrName => ({ tr, state: state$1, dispatch, editor, }) => {
3113
+ const splitListItem = (typeOrName, overrideAttrs = {}) => ({ tr, state: state$1, dispatch, editor, }) => {
3105
3114
  var _a;
3106
3115
  const type = getNodeType(typeOrName, state$1.schema);
3107
3116
  const { $from, $to } = state$1.selection;
@@ -3137,7 +3146,10 @@ const splitListItem = typeOrName => ({ tr, state: state$1, dispatch, editor, })
3137
3146
  // eslint-disable-next-line
3138
3147
  const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3;
3139
3148
  // Add a second list item with an empty default start node
3140
- const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
3149
+ const newNextTypeAttributes = {
3150
+ ...getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs),
3151
+ ...overrideAttrs,
3152
+ };
3141
3153
  const nextType = ((_a = type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.createAndFill(newNextTypeAttributes)) || undefined;
3142
3154
  wrap = wrap.append(model.Fragment.from(type.createAndFill(null, nextType) || undefined));
3143
3155
  const start = $from.before($from.depth - (depthBefore - 1));
@@ -3159,8 +3171,14 @@ const splitListItem = typeOrName => ({ tr, state: state$1, dispatch, editor, })
3159
3171
  return true;
3160
3172
  }
3161
3173
  const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null;
3162
- const newTypeAttributes = getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs);
3163
- const newNextTypeAttributes = getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs);
3174
+ const newTypeAttributes = {
3175
+ ...getSplittedAttributes(extensionAttributes, grandParent.type.name, grandParent.attrs),
3176
+ ...overrideAttrs,
3177
+ };
3178
+ const newNextTypeAttributes = {
3179
+ ...getSplittedAttributes(extensionAttributes, $from.node().type.name, $from.node().attrs),
3180
+ ...overrideAttrs,
3181
+ };
3164
3182
  tr.delete($from.pos, $to.pos);
3165
3183
  const types = nextType
3166
3184
  ? [
@@ -3962,6 +3980,10 @@ class Editor extends EventEmitter {
3962
3980
  constructor(options = {}) {
3963
3981
  super();
3964
3982
  this.isFocused = false;
3983
+ /**
3984
+ * The editor is considered initialized after the `create` event has been emitted.
3985
+ */
3986
+ this.isInitialized = false;
3965
3987
  this.extensionStorage = {};
3966
3988
  this.options = {
3967
3989
  element: document.createElement('div'),
@@ -4012,6 +4034,7 @@ class Editor extends EventEmitter {
4012
4034
  }
4013
4035
  this.commands.focus(this.options.autofocus);
4014
4036
  this.emit('create', { editor: this });
4037
+ this.isInitialized = true;
4015
4038
  }, 0);
4016
4039
  }
4017
4040
  /**
@@ -4979,14 +5002,16 @@ function nodePasteRule(config) {
4979
5002
  find: config.find,
4980
5003
  handler({ match, chain, range, pasteEvent, }) {
4981
5004
  const attributes = callOrReturn(config.getAttributes, undefined, match, pasteEvent);
5005
+ const content = callOrReturn(config.getContent, undefined, attributes);
4982
5006
  if (attributes === false || attributes === null) {
4983
5007
  return null;
4984
5008
  }
5009
+ const node = { type: config.type.name, attrs: attributes };
5010
+ if (content) {
5011
+ node.content = content;
5012
+ }
4985
5013
  if (match.input) {
4986
- chain().deleteRange(range).insertContentAt(range.from, {
4987
- type: config.type.name,
4988
- attrs: attributes,
4989
- });
5014
+ chain().deleteRange(range).insertContentAt(range.from, node);
4990
5015
  }
4991
5016
  },
4992
5017
  });