@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.cjs CHANGED
@@ -19,7 +19,6 @@ function createChainableState(config) {
19
19
  ...state,
20
20
  apply: state.apply.bind(state),
21
21
  applyTransaction: state.applyTransaction.bind(state),
22
- filterTransaction: state.filterTransaction,
23
22
  plugins: state.plugins,
24
23
  schema: state.schema,
25
24
  reconfigure: state.reconfigure.bind(state),
@@ -734,11 +733,11 @@ class PasteRule {
734
733
  this.handler = config.handler;
735
734
  }
736
735
  }
737
- const pasteRuleMatcherHandler = (text, find) => {
736
+ const pasteRuleMatcherHandler = (text, find, event) => {
738
737
  if (isRegExp(find)) {
739
738
  return [...text.matchAll(find)];
740
739
  }
741
- const matches = find(text);
740
+ const matches = find(text, event);
742
741
  if (!matches) {
743
742
  return [];
744
743
  }
@@ -770,7 +769,7 @@ function run(config) {
770
769
  const resolvedFrom = Math.max(from, pos);
771
770
  const resolvedTo = Math.min(to, pos + node.content.size);
772
771
  const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc');
773
- const matches = pasteRuleMatcherHandler(textToMatch, rule.find);
772
+ const matches = pasteRuleMatcherHandler(textToMatch, rule.find, pasteEvent);
774
773
  matches.forEach(match => {
775
774
  if (match.index === undefined) {
776
775
  return;
@@ -1144,14 +1143,14 @@ class Extension {
1144
1143
  this.child = null;
1145
1144
  this.config = {
1146
1145
  name: this.name,
1147
- defaultOptions: undefined,
1146
+ defaultOptions: {},
1148
1147
  };
1149
1148
  this.config = {
1150
1149
  ...this.config,
1151
1150
  ...config,
1152
1151
  };
1153
1152
  this.name = this.config.name;
1154
- if (config.defaultOptions) {
1153
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
1155
1154
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
1156
1155
  }
1157
1156
  // TODO: remove `addOptions` fallback
@@ -1588,10 +1587,24 @@ const insertContent = (value, options) => ({ tr, commands }) => {
1588
1587
  return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
1589
1588
  };
1590
1589
 
1590
+ const removeWhitespaces = (node) => {
1591
+ const children = node.childNodes;
1592
+ for (let i = children.length - 1; i >= 0; i -= 1) {
1593
+ const child = children[i];
1594
+ if (child.nodeType === 3 && child.nodeValue && /^(\n\s\s|\n)$/.test(child.nodeValue)) {
1595
+ node.removeChild(child);
1596
+ }
1597
+ else if (child.nodeType === 1) {
1598
+ removeWhitespaces(child);
1599
+ }
1600
+ }
1601
+ return node;
1602
+ };
1591
1603
  function elementFromString(value) {
1592
1604
  // add a wrapper to preserve leading and trailing whitespace
1593
1605
  const wrappedValue = `<body>${value}</body>`;
1594
- return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1606
+ const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1607
+ return removeWhitespaces(html);
1595
1608
  }
1596
1609
 
1597
1610
  function createNodeFromContent(content, schema, options) {
@@ -1757,6 +1770,14 @@ const joinItemForward = () => ({ state, dispatch, tr, }) => {
1757
1770
  }
1758
1771
  };
1759
1772
 
1773
+ const joinTextblockBackward = () => ({ state, dispatch }) => {
1774
+ return commands$1.joinTextblockBackward(state, dispatch);
1775
+ };
1776
+
1777
+ const joinTextblockForward = () => ({ state, dispatch }) => {
1778
+ return commands$1.joinTextblockForward(state, dispatch);
1779
+ };
1780
+
1760
1781
  function isMacOS() {
1761
1782
  return typeof navigator !== 'undefined'
1762
1783
  ? /Mac/.test(navigator.platform)
@@ -2311,6 +2332,9 @@ function getMarksBetween(from, to, doc) {
2311
2332
  }
2312
2333
  else {
2313
2334
  doc.nodesBetween(from, to, (node, pos) => {
2335
+ if (!node || (node === null || node === void 0 ? void 0 : node.nodeSize) === undefined) {
2336
+ return;
2337
+ }
2314
2338
  marks.push(...node.marks.map(mark => ({
2315
2339
  from: pos,
2316
2340
  to: pos + node.nodeSize,
@@ -3088,6 +3112,8 @@ var commands = /*#__PURE__*/Object.freeze({
3088
3112
  joinForward: joinForward,
3089
3113
  joinItemBackward: joinItemBackward,
3090
3114
  joinItemForward: joinItemForward,
3115
+ joinTextblockBackward: joinTextblockBackward,
3116
+ joinTextblockForward: joinTextblockForward,
3091
3117
  keyboardShortcut: keyboardShortcut,
3092
3118
  lift: lift,
3093
3119
  liftEmptyBlock: liftEmptyBlock,
@@ -3313,6 +3339,151 @@ var extensions = /*#__PURE__*/Object.freeze({
3313
3339
  Tabindex: Tabindex
3314
3340
  });
3315
3341
 
3342
+ class NodePos {
3343
+ constructor(pos, editor) {
3344
+ this.resolvedPos = pos;
3345
+ this.editor = editor;
3346
+ }
3347
+ get node() {
3348
+ return this.resolvedPos.node();
3349
+ }
3350
+ get element() {
3351
+ return this.editor.view.domAtPos(this.pos).node;
3352
+ }
3353
+ get depth() {
3354
+ return this.resolvedPos.depth;
3355
+ }
3356
+ get pos() {
3357
+ return this.resolvedPos.pos;
3358
+ }
3359
+ get content() {
3360
+ return this.node.content;
3361
+ }
3362
+ set content(content) {
3363
+ this.editor.commands.insertContentAt({ from: this.from, to: this.to }, content);
3364
+ }
3365
+ get attributes() {
3366
+ return this.node.attrs;
3367
+ }
3368
+ get textContent() {
3369
+ return this.node.textContent;
3370
+ }
3371
+ get size() {
3372
+ return this.node.nodeSize;
3373
+ }
3374
+ get from() {
3375
+ return this.resolvedPos.start(this.resolvedPos.depth);
3376
+ }
3377
+ get range() {
3378
+ return {
3379
+ from: this.from,
3380
+ to: this.to,
3381
+ };
3382
+ }
3383
+ get to() {
3384
+ return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1);
3385
+ }
3386
+ get parent() {
3387
+ if (this.depth === 0) {
3388
+ return null;
3389
+ }
3390
+ const parentPos = this.resolvedPos.start(this.resolvedPos.depth - 1);
3391
+ const $pos = this.resolvedPos.doc.resolve(parentPos);
3392
+ return new NodePos($pos, this.editor);
3393
+ }
3394
+ get before() {
3395
+ let $pos = this.resolvedPos.doc.resolve(this.from - 2);
3396
+ if ($pos.depth !== this.depth) {
3397
+ $pos = this.resolvedPos.doc.resolve(this.from - 3);
3398
+ }
3399
+ return new NodePos($pos, this.editor);
3400
+ }
3401
+ get after() {
3402
+ let $pos = this.resolvedPos.doc.resolve(this.to + 2);
3403
+ if ($pos.depth !== this.depth) {
3404
+ $pos = this.resolvedPos.doc.resolve(this.to + 3);
3405
+ }
3406
+ return new NodePos($pos, this.editor);
3407
+ }
3408
+ get children() {
3409
+ const children = [];
3410
+ this.node.content.forEach((node, offset) => {
3411
+ const targetPos = this.pos + offset + 1;
3412
+ const $pos = this.resolvedPos.doc.resolve(targetPos);
3413
+ if ($pos.depth === this.depth) {
3414
+ return;
3415
+ }
3416
+ children.push(new NodePos($pos, this.editor));
3417
+ });
3418
+ return children;
3419
+ }
3420
+ get firstChild() {
3421
+ return this.children[0] || null;
3422
+ }
3423
+ get lastChild() {
3424
+ const children = this.children;
3425
+ return children[children.length - 1] || null;
3426
+ }
3427
+ closest(selector, attributes = {}) {
3428
+ let node = null;
3429
+ let currentNode = this.parent;
3430
+ while (currentNode && !node) {
3431
+ if (currentNode.node.type.name === selector) {
3432
+ if (Object.keys(attributes).length > 0) {
3433
+ const nodeAttributes = currentNode.node.attrs;
3434
+ const attrKeys = Object.keys(attributes);
3435
+ for (let index = 0; index < attrKeys.length; index += 1) {
3436
+ const key = attrKeys[index];
3437
+ if (nodeAttributes[key] !== attributes[key]) {
3438
+ break;
3439
+ }
3440
+ }
3441
+ }
3442
+ else {
3443
+ node = currentNode;
3444
+ }
3445
+ }
3446
+ currentNode = currentNode.parent;
3447
+ }
3448
+ return node;
3449
+ }
3450
+ querySelector(selector, attributes = {}) {
3451
+ return this.querySelectorAll(selector, attributes, true)[0] || null;
3452
+ }
3453
+ querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3454
+ let nodes = [];
3455
+ // iterate through children recursively finding all nodes which match the selector with the node name
3456
+ if (!this.children || this.children.length === 0) {
3457
+ return nodes;
3458
+ }
3459
+ this.children.forEach(node => {
3460
+ if (node.node.type.name === selector) {
3461
+ if (Object.keys(attributes).length > 0) {
3462
+ const nodeAttributes = node.node.attrs;
3463
+ const attrKeys = Object.keys(attributes);
3464
+ for (let index = 0; index < attrKeys.length; index += 1) {
3465
+ const key = attrKeys[index];
3466
+ if (nodeAttributes[key] !== attributes[key]) {
3467
+ return;
3468
+ }
3469
+ }
3470
+ }
3471
+ nodes.push(node);
3472
+ if (firstItemOnly) {
3473
+ return;
3474
+ }
3475
+ }
3476
+ nodes = nodes.concat(node.querySelectorAll(selector));
3477
+ });
3478
+ return nodes;
3479
+ }
3480
+ setAttribute(attributes) {
3481
+ const oldSelection = this.editor.state.selection;
3482
+ this.editor.chain().setTextSelection(this.from).updateAttributes(this.node.type.name, attributes).setTextSelection(oldSelection.from)
3483
+ .run();
3484
+ }
3485
+ }
3486
+
3316
3487
  const style = `.ProseMirror {
3317
3488
  position: relative;
3318
3489
  }
@@ -3758,6 +3929,21 @@ class Editor extends EventEmitter {
3758
3929
  // @ts-ignore
3759
3930
  return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3760
3931
  }
3932
+ $node(selector, attributes) {
3933
+ var _a;
3934
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelector(selector, attributes)) || null;
3935
+ }
3936
+ $nodes(selector, attributes) {
3937
+ var _a;
3938
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selector, attributes)) || null;
3939
+ }
3940
+ $pos(pos) {
3941
+ const $pos = this.state.doc.resolve(pos);
3942
+ return new NodePos($pos, this);
3943
+ }
3944
+ get $doc() {
3945
+ return this.$pos(0);
3946
+ }
3761
3947
  }
3762
3948
 
3763
3949
  /**
@@ -3947,14 +4133,14 @@ class Mark {
3947
4133
  this.child = null;
3948
4134
  this.config = {
3949
4135
  name: this.name,
3950
- defaultOptions: undefined,
4136
+ defaultOptions: {},
3951
4137
  };
3952
4138
  this.config = {
3953
4139
  ...this.config,
3954
4140
  ...config,
3955
4141
  };
3956
4142
  this.name = this.config.name;
3957
- if (config.defaultOptions) {
4143
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
3958
4144
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
3959
4145
  }
3960
4146
  // TODO: remove `addOptions` fallback
@@ -4030,14 +4216,14 @@ class Node {
4030
4216
  this.child = null;
4031
4217
  this.config = {
4032
4218
  name: this.name,
4033
- defaultOptions: undefined,
4219
+ defaultOptions: {},
4034
4220
  };
4035
4221
  this.config = {
4036
4222
  ...this.config,
4037
4223
  ...config,
4038
4224
  };
4039
4225
  this.name = this.config.name;
4040
- if (config.defaultOptions) {
4226
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
4041
4227
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
4042
4228
  }
4043
4229
  // TODO: remove `addOptions` fallback
@@ -4406,6 +4592,7 @@ exports.Extension = Extension;
4406
4592
  exports.InputRule = InputRule;
4407
4593
  exports.Mark = Mark;
4408
4594
  exports.Node = Node;
4595
+ exports.NodePos = NodePos;
4409
4596
  exports.NodeView = NodeView;
4410
4597
  exports.PasteRule = PasteRule;
4411
4598
  exports.Tracker = Tracker;