@tiptap/core 2.2.0-rc.7 → 2.2.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.umd.js CHANGED
@@ -13,7 +13,6 @@
13
13
  ...state,
14
14
  apply: state.apply.bind(state),
15
15
  applyTransaction: state.applyTransaction.bind(state),
16
- filterTransaction: state.filterTransaction,
17
16
  plugins: state.plugins,
18
17
  schema: state.schema,
19
18
  reconfigure: state.reconfigure.bind(state),
@@ -728,11 +727,11 @@
728
727
  this.handler = config.handler;
729
728
  }
730
729
  }
731
- const pasteRuleMatcherHandler = (text, find) => {
730
+ const pasteRuleMatcherHandler = (text, find, event) => {
732
731
  if (isRegExp(find)) {
733
732
  return [...text.matchAll(find)];
734
733
  }
735
- const matches = find(text);
734
+ const matches = find(text, event);
736
735
  if (!matches) {
737
736
  return [];
738
737
  }
@@ -764,7 +763,7 @@
764
763
  const resolvedFrom = Math.max(from, pos);
765
764
  const resolvedTo = Math.min(to, pos + node.content.size);
766
765
  const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc');
767
- const matches = pasteRuleMatcherHandler(textToMatch, rule.find);
766
+ const matches = pasteRuleMatcherHandler(textToMatch, rule.find, pasteEvent);
768
767
  matches.forEach(match => {
769
768
  if (match.index === undefined) {
770
769
  return;
@@ -1138,14 +1137,14 @@
1138
1137
  this.child = null;
1139
1138
  this.config = {
1140
1139
  name: this.name,
1141
- defaultOptions: undefined,
1140
+ defaultOptions: {},
1142
1141
  };
1143
1142
  this.config = {
1144
1143
  ...this.config,
1145
1144
  ...config,
1146
1145
  };
1147
1146
  this.name = this.config.name;
1148
- if (config.defaultOptions) {
1147
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
1149
1148
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
1150
1149
  }
1151
1150
  // TODO: remove `addOptions` fallback
@@ -1582,10 +1581,24 @@
1582
1581
  return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
1583
1582
  };
1584
1583
 
1584
+ const removeWhitespaces = (node) => {
1585
+ const children = node.childNodes;
1586
+ for (let i = children.length - 1; i >= 0; i -= 1) {
1587
+ const child = children[i];
1588
+ if (child.nodeType === 3 && child.nodeValue && /^(\n\s\s|\n)$/.test(child.nodeValue)) {
1589
+ node.removeChild(child);
1590
+ }
1591
+ else if (child.nodeType === 1) {
1592
+ removeWhitespaces(child);
1593
+ }
1594
+ }
1595
+ return node;
1596
+ };
1585
1597
  function elementFromString(value) {
1586
1598
  // add a wrapper to preserve leading and trailing whitespace
1587
1599
  const wrappedValue = `<body>${value}</body>`;
1588
- return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1600
+ const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1601
+ return removeWhitespaces(html);
1589
1602
  }
1590
1603
 
1591
1604
  function createNodeFromContent(content, schema, options) {
@@ -1751,6 +1764,14 @@
1751
1764
  }
1752
1765
  };
1753
1766
 
1767
+ const joinTextblockBackward = () => ({ state, dispatch }) => {
1768
+ return commands$1.joinTextblockBackward(state, dispatch);
1769
+ };
1770
+
1771
+ const joinTextblockForward = () => ({ state, dispatch }) => {
1772
+ return commands$1.joinTextblockForward(state, dispatch);
1773
+ };
1774
+
1754
1775
  function isMacOS() {
1755
1776
  return typeof navigator !== 'undefined'
1756
1777
  ? /Mac/.test(navigator.platform)
@@ -2305,6 +2326,9 @@
2305
2326
  }
2306
2327
  else {
2307
2328
  doc.nodesBetween(from, to, (node, pos) => {
2329
+ if (!node || (node === null || node === void 0 ? void 0 : node.nodeSize) === undefined) {
2330
+ return;
2331
+ }
2308
2332
  marks.push(...node.marks.map(mark => ({
2309
2333
  from: pos,
2310
2334
  to: pos + node.nodeSize,
@@ -3082,6 +3106,8 @@
3082
3106
  joinForward: joinForward,
3083
3107
  joinItemBackward: joinItemBackward,
3084
3108
  joinItemForward: joinItemForward,
3109
+ joinTextblockBackward: joinTextblockBackward,
3110
+ joinTextblockForward: joinTextblockForward,
3085
3111
  keyboardShortcut: keyboardShortcut,
3086
3112
  lift: lift,
3087
3113
  liftEmptyBlock: liftEmptyBlock,
@@ -3307,6 +3333,151 @@
3307
3333
  Tabindex: Tabindex
3308
3334
  });
3309
3335
 
3336
+ class NodePos {
3337
+ constructor(pos, editor) {
3338
+ this.resolvedPos = pos;
3339
+ this.editor = editor;
3340
+ }
3341
+ get node() {
3342
+ return this.resolvedPos.node();
3343
+ }
3344
+ get element() {
3345
+ return this.editor.view.domAtPos(this.pos).node;
3346
+ }
3347
+ get depth() {
3348
+ return this.resolvedPos.depth;
3349
+ }
3350
+ get pos() {
3351
+ return this.resolvedPos.pos;
3352
+ }
3353
+ get content() {
3354
+ return this.node.content;
3355
+ }
3356
+ set content(content) {
3357
+ this.editor.commands.insertContentAt({ from: this.from, to: this.to }, content);
3358
+ }
3359
+ get attributes() {
3360
+ return this.node.attrs;
3361
+ }
3362
+ get textContent() {
3363
+ return this.node.textContent;
3364
+ }
3365
+ get size() {
3366
+ return this.node.nodeSize;
3367
+ }
3368
+ get from() {
3369
+ return this.resolvedPos.start(this.resolvedPos.depth);
3370
+ }
3371
+ get range() {
3372
+ return {
3373
+ from: this.from,
3374
+ to: this.to,
3375
+ };
3376
+ }
3377
+ get to() {
3378
+ return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1);
3379
+ }
3380
+ get parent() {
3381
+ if (this.depth === 0) {
3382
+ return null;
3383
+ }
3384
+ const parentPos = this.resolvedPos.start(this.resolvedPos.depth - 1);
3385
+ const $pos = this.resolvedPos.doc.resolve(parentPos);
3386
+ return new NodePos($pos, this.editor);
3387
+ }
3388
+ get before() {
3389
+ let $pos = this.resolvedPos.doc.resolve(this.from - 2);
3390
+ if ($pos.depth !== this.depth) {
3391
+ $pos = this.resolvedPos.doc.resolve(this.from - 3);
3392
+ }
3393
+ return new NodePos($pos, this.editor);
3394
+ }
3395
+ get after() {
3396
+ let $pos = this.resolvedPos.doc.resolve(this.to + 2);
3397
+ if ($pos.depth !== this.depth) {
3398
+ $pos = this.resolvedPos.doc.resolve(this.to + 3);
3399
+ }
3400
+ return new NodePos($pos, this.editor);
3401
+ }
3402
+ get children() {
3403
+ const children = [];
3404
+ this.node.content.forEach((node, offset) => {
3405
+ const targetPos = this.pos + offset + 1;
3406
+ const $pos = this.resolvedPos.doc.resolve(targetPos);
3407
+ if ($pos.depth === this.depth) {
3408
+ return;
3409
+ }
3410
+ children.push(new NodePos($pos, this.editor));
3411
+ });
3412
+ return children;
3413
+ }
3414
+ get firstChild() {
3415
+ return this.children[0] || null;
3416
+ }
3417
+ get lastChild() {
3418
+ const children = this.children;
3419
+ return children[children.length - 1] || null;
3420
+ }
3421
+ closest(selector, attributes = {}) {
3422
+ let node = null;
3423
+ let currentNode = this.parent;
3424
+ while (currentNode && !node) {
3425
+ if (currentNode.node.type.name === selector) {
3426
+ if (Object.keys(attributes).length > 0) {
3427
+ const nodeAttributes = currentNode.node.attrs;
3428
+ const attrKeys = Object.keys(attributes);
3429
+ for (let index = 0; index < attrKeys.length; index += 1) {
3430
+ const key = attrKeys[index];
3431
+ if (nodeAttributes[key] !== attributes[key]) {
3432
+ break;
3433
+ }
3434
+ }
3435
+ }
3436
+ else {
3437
+ node = currentNode;
3438
+ }
3439
+ }
3440
+ currentNode = currentNode.parent;
3441
+ }
3442
+ return node;
3443
+ }
3444
+ querySelector(selector, attributes = {}) {
3445
+ return this.querySelectorAll(selector, attributes, true)[0] || null;
3446
+ }
3447
+ querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3448
+ let nodes = [];
3449
+ // iterate through children recursively finding all nodes which match the selector with the node name
3450
+ if (!this.children || this.children.length === 0) {
3451
+ return nodes;
3452
+ }
3453
+ this.children.forEach(node => {
3454
+ if (node.node.type.name === selector) {
3455
+ if (Object.keys(attributes).length > 0) {
3456
+ const nodeAttributes = node.node.attrs;
3457
+ const attrKeys = Object.keys(attributes);
3458
+ for (let index = 0; index < attrKeys.length; index += 1) {
3459
+ const key = attrKeys[index];
3460
+ if (nodeAttributes[key] !== attributes[key]) {
3461
+ return;
3462
+ }
3463
+ }
3464
+ }
3465
+ nodes.push(node);
3466
+ if (firstItemOnly) {
3467
+ return;
3468
+ }
3469
+ }
3470
+ nodes = nodes.concat(node.querySelectorAll(selector));
3471
+ });
3472
+ return nodes;
3473
+ }
3474
+ setAttribute(attributes) {
3475
+ const oldSelection = this.editor.state.selection;
3476
+ this.editor.chain().setTextSelection(this.from).updateAttributes(this.node.type.name, attributes).setTextSelection(oldSelection.from)
3477
+ .run();
3478
+ }
3479
+ }
3480
+
3310
3481
  const style = `.ProseMirror {
3311
3482
  position: relative;
3312
3483
  }
@@ -3752,6 +3923,21 @@ img.ProseMirror-separator {
3752
3923
  // @ts-ignore
3753
3924
  return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3754
3925
  }
3926
+ $node(selector, attributes) {
3927
+ var _a;
3928
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelector(selector, attributes)) || null;
3929
+ }
3930
+ $nodes(selector, attributes) {
3931
+ var _a;
3932
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selector, attributes)) || null;
3933
+ }
3934
+ $pos(pos) {
3935
+ const $pos = this.state.doc.resolve(pos);
3936
+ return new NodePos($pos, this);
3937
+ }
3938
+ get $doc() {
3939
+ return this.$pos(0);
3940
+ }
3755
3941
  }
3756
3942
 
3757
3943
  /**
@@ -3941,14 +4127,14 @@ img.ProseMirror-separator {
3941
4127
  this.child = null;
3942
4128
  this.config = {
3943
4129
  name: this.name,
3944
- defaultOptions: undefined,
4130
+ defaultOptions: {},
3945
4131
  };
3946
4132
  this.config = {
3947
4133
  ...this.config,
3948
4134
  ...config,
3949
4135
  };
3950
4136
  this.name = this.config.name;
3951
- if (config.defaultOptions) {
4137
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
3952
4138
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
3953
4139
  }
3954
4140
  // TODO: remove `addOptions` fallback
@@ -4024,14 +4210,14 @@ img.ProseMirror-separator {
4024
4210
  this.child = null;
4025
4211
  this.config = {
4026
4212
  name: this.name,
4027
- defaultOptions: undefined,
4213
+ defaultOptions: {},
4028
4214
  };
4029
4215
  this.config = {
4030
4216
  ...this.config,
4031
4217
  ...config,
4032
4218
  };
4033
4219
  this.name = this.config.name;
4034
- if (config.defaultOptions) {
4220
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
4035
4221
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
4036
4222
  }
4037
4223
  // TODO: remove `addOptions` fallback
@@ -4400,6 +4586,7 @@ img.ProseMirror-separator {
4400
4586
  exports.InputRule = InputRule;
4401
4587
  exports.Mark = Mark;
4402
4588
  exports.Node = Node;
4589
+ exports.NodePos = NodePos;
4403
4590
  exports.NodeView = NodeView;
4404
4591
  exports.PasteRule = PasteRule;
4405
4592
  exports.Tracker = Tracker;