@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.js CHANGED
@@ -3,7 +3,7 @@ import { EditorView } from '@tiptap/pm/view';
3
3
  import { keymap } from '@tiptap/pm/keymap';
4
4
  import { Schema, Fragment, DOMParser, DOMSerializer, Node as Node$1, Slice } from '@tiptap/pm/model';
5
5
  import { liftTarget, ReplaceStep, ReplaceAroundStep, joinPoint, Transform, canSplit, canJoin, findWrapping } from '@tiptap/pm/transform';
6
- import { createParagraphNear as createParagraphNear$1, deleteSelection as deleteSelection$1, exitCode as exitCode$1, joinUp as joinUp$1, joinDown as joinDown$1, joinBackward as joinBackward$1, joinForward as joinForward$1, lift as lift$1, liftEmptyBlock as liftEmptyBlock$1, newlineInCode as newlineInCode$1, selectNodeBackward as selectNodeBackward$1, selectNodeForward as selectNodeForward$1, selectParentNode as selectParentNode$1, selectTextblockEnd as selectTextblockEnd$1, selectTextblockStart as selectTextblockStart$1, setBlockType, wrapIn as wrapIn$1 } from '@tiptap/pm/commands';
6
+ import { createParagraphNear as createParagraphNear$1, deleteSelection as deleteSelection$1, exitCode as exitCode$1, joinUp as joinUp$1, joinDown as joinDown$1, joinBackward as joinBackward$1, joinForward as joinForward$1, joinTextblockBackward as joinTextblockBackward$1, joinTextblockForward as joinTextblockForward$1, lift as lift$1, liftEmptyBlock as liftEmptyBlock$1, newlineInCode as newlineInCode$1, selectNodeBackward as selectNodeBackward$1, selectNodeForward as selectNodeForward$1, selectParentNode as selectParentNode$1, selectTextblockEnd as selectTextblockEnd$1, selectTextblockStart as selectTextblockStart$1, setBlockType, wrapIn as wrapIn$1 } from '@tiptap/pm/commands';
7
7
  import { liftListItem as liftListItem$1, sinkListItem as sinkListItem$1, wrapInList as wrapInList$1 } from '@tiptap/pm/schema-list';
8
8
 
9
9
  function createChainableState(config) {
@@ -15,7 +15,6 @@ function createChainableState(config) {
15
15
  ...state,
16
16
  apply: state.apply.bind(state),
17
17
  applyTransaction: state.applyTransaction.bind(state),
18
- filterTransaction: state.filterTransaction,
19
18
  plugins: state.plugins,
20
19
  schema: state.schema,
21
20
  reconfigure: state.reconfigure.bind(state),
@@ -730,11 +729,11 @@ class PasteRule {
730
729
  this.handler = config.handler;
731
730
  }
732
731
  }
733
- const pasteRuleMatcherHandler = (text, find) => {
732
+ const pasteRuleMatcherHandler = (text, find, event) => {
734
733
  if (isRegExp(find)) {
735
734
  return [...text.matchAll(find)];
736
735
  }
737
- const matches = find(text);
736
+ const matches = find(text, event);
738
737
  if (!matches) {
739
738
  return [];
740
739
  }
@@ -766,7 +765,7 @@ function run(config) {
766
765
  const resolvedFrom = Math.max(from, pos);
767
766
  const resolvedTo = Math.min(to, pos + node.content.size);
768
767
  const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc');
769
- const matches = pasteRuleMatcherHandler(textToMatch, rule.find);
768
+ const matches = pasteRuleMatcherHandler(textToMatch, rule.find, pasteEvent);
770
769
  matches.forEach(match => {
771
770
  if (match.index === undefined) {
772
771
  return;
@@ -1140,14 +1139,14 @@ class Extension {
1140
1139
  this.child = null;
1141
1140
  this.config = {
1142
1141
  name: this.name,
1143
- defaultOptions: undefined,
1142
+ defaultOptions: {},
1144
1143
  };
1145
1144
  this.config = {
1146
1145
  ...this.config,
1147
1146
  ...config,
1148
1147
  };
1149
1148
  this.name = this.config.name;
1150
- if (config.defaultOptions) {
1149
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
1151
1150
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
1152
1151
  }
1153
1152
  // TODO: remove `addOptions` fallback
@@ -1584,10 +1583,24 @@ const insertContent = (value, options) => ({ tr, commands }) => {
1584
1583
  return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
1585
1584
  };
1586
1585
 
1586
+ const removeWhitespaces = (node) => {
1587
+ const children = node.childNodes;
1588
+ for (let i = children.length - 1; i >= 0; i -= 1) {
1589
+ const child = children[i];
1590
+ if (child.nodeType === 3 && child.nodeValue && /^(\n\s\s|\n)$/.test(child.nodeValue)) {
1591
+ node.removeChild(child);
1592
+ }
1593
+ else if (child.nodeType === 1) {
1594
+ removeWhitespaces(child);
1595
+ }
1596
+ }
1597
+ return node;
1598
+ };
1587
1599
  function elementFromString(value) {
1588
1600
  // add a wrapper to preserve leading and trailing whitespace
1589
1601
  const wrappedValue = `<body>${value}</body>`;
1590
- return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1602
+ const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1603
+ return removeWhitespaces(html);
1591
1604
  }
1592
1605
 
1593
1606
  function createNodeFromContent(content, schema, options) {
@@ -1753,6 +1766,14 @@ const joinItemForward = () => ({ state, dispatch, tr, }) => {
1753
1766
  }
1754
1767
  };
1755
1768
 
1769
+ const joinTextblockBackward = () => ({ state, dispatch }) => {
1770
+ return joinTextblockBackward$1(state, dispatch);
1771
+ };
1772
+
1773
+ const joinTextblockForward = () => ({ state, dispatch }) => {
1774
+ return joinTextblockForward$1(state, dispatch);
1775
+ };
1776
+
1756
1777
  function isMacOS() {
1757
1778
  return typeof navigator !== 'undefined'
1758
1779
  ? /Mac/.test(navigator.platform)
@@ -2307,6 +2328,9 @@ function getMarksBetween(from, to, doc) {
2307
2328
  }
2308
2329
  else {
2309
2330
  doc.nodesBetween(from, to, (node, pos) => {
2331
+ if (!node || (node === null || node === void 0 ? void 0 : node.nodeSize) === undefined) {
2332
+ return;
2333
+ }
2310
2334
  marks.push(...node.marks.map(mark => ({
2311
2335
  from: pos,
2312
2336
  to: pos + node.nodeSize,
@@ -3084,6 +3108,8 @@ var commands = /*#__PURE__*/Object.freeze({
3084
3108
  joinForward: joinForward,
3085
3109
  joinItemBackward: joinItemBackward,
3086
3110
  joinItemForward: joinItemForward,
3111
+ joinTextblockBackward: joinTextblockBackward,
3112
+ joinTextblockForward: joinTextblockForward,
3087
3113
  keyboardShortcut: keyboardShortcut,
3088
3114
  lift: lift,
3089
3115
  liftEmptyBlock: liftEmptyBlock,
@@ -3309,6 +3335,151 @@ var extensions = /*#__PURE__*/Object.freeze({
3309
3335
  Tabindex: Tabindex
3310
3336
  });
3311
3337
 
3338
+ class NodePos {
3339
+ constructor(pos, editor) {
3340
+ this.resolvedPos = pos;
3341
+ this.editor = editor;
3342
+ }
3343
+ get node() {
3344
+ return this.resolvedPos.node();
3345
+ }
3346
+ get element() {
3347
+ return this.editor.view.domAtPos(this.pos).node;
3348
+ }
3349
+ get depth() {
3350
+ return this.resolvedPos.depth;
3351
+ }
3352
+ get pos() {
3353
+ return this.resolvedPos.pos;
3354
+ }
3355
+ get content() {
3356
+ return this.node.content;
3357
+ }
3358
+ set content(content) {
3359
+ this.editor.commands.insertContentAt({ from: this.from, to: this.to }, content);
3360
+ }
3361
+ get attributes() {
3362
+ return this.node.attrs;
3363
+ }
3364
+ get textContent() {
3365
+ return this.node.textContent;
3366
+ }
3367
+ get size() {
3368
+ return this.node.nodeSize;
3369
+ }
3370
+ get from() {
3371
+ return this.resolvedPos.start(this.resolvedPos.depth);
3372
+ }
3373
+ get range() {
3374
+ return {
3375
+ from: this.from,
3376
+ to: this.to,
3377
+ };
3378
+ }
3379
+ get to() {
3380
+ return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1);
3381
+ }
3382
+ get parent() {
3383
+ if (this.depth === 0) {
3384
+ return null;
3385
+ }
3386
+ const parentPos = this.resolvedPos.start(this.resolvedPos.depth - 1);
3387
+ const $pos = this.resolvedPos.doc.resolve(parentPos);
3388
+ return new NodePos($pos, this.editor);
3389
+ }
3390
+ get before() {
3391
+ let $pos = this.resolvedPos.doc.resolve(this.from - 2);
3392
+ if ($pos.depth !== this.depth) {
3393
+ $pos = this.resolvedPos.doc.resolve(this.from - 3);
3394
+ }
3395
+ return new NodePos($pos, this.editor);
3396
+ }
3397
+ get after() {
3398
+ let $pos = this.resolvedPos.doc.resolve(this.to + 2);
3399
+ if ($pos.depth !== this.depth) {
3400
+ $pos = this.resolvedPos.doc.resolve(this.to + 3);
3401
+ }
3402
+ return new NodePos($pos, this.editor);
3403
+ }
3404
+ get children() {
3405
+ const children = [];
3406
+ this.node.content.forEach((node, offset) => {
3407
+ const targetPos = this.pos + offset + 1;
3408
+ const $pos = this.resolvedPos.doc.resolve(targetPos);
3409
+ if ($pos.depth === this.depth) {
3410
+ return;
3411
+ }
3412
+ children.push(new NodePos($pos, this.editor));
3413
+ });
3414
+ return children;
3415
+ }
3416
+ get firstChild() {
3417
+ return this.children[0] || null;
3418
+ }
3419
+ get lastChild() {
3420
+ const children = this.children;
3421
+ return children[children.length - 1] || null;
3422
+ }
3423
+ closest(selector, attributes = {}) {
3424
+ let node = null;
3425
+ let currentNode = this.parent;
3426
+ while (currentNode && !node) {
3427
+ if (currentNode.node.type.name === selector) {
3428
+ if (Object.keys(attributes).length > 0) {
3429
+ const nodeAttributes = currentNode.node.attrs;
3430
+ const attrKeys = Object.keys(attributes);
3431
+ for (let index = 0; index < attrKeys.length; index += 1) {
3432
+ const key = attrKeys[index];
3433
+ if (nodeAttributes[key] !== attributes[key]) {
3434
+ break;
3435
+ }
3436
+ }
3437
+ }
3438
+ else {
3439
+ node = currentNode;
3440
+ }
3441
+ }
3442
+ currentNode = currentNode.parent;
3443
+ }
3444
+ return node;
3445
+ }
3446
+ querySelector(selector, attributes = {}) {
3447
+ return this.querySelectorAll(selector, attributes, true)[0] || null;
3448
+ }
3449
+ querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3450
+ let nodes = [];
3451
+ // iterate through children recursively finding all nodes which match the selector with the node name
3452
+ if (!this.children || this.children.length === 0) {
3453
+ return nodes;
3454
+ }
3455
+ this.children.forEach(node => {
3456
+ if (node.node.type.name === selector) {
3457
+ if (Object.keys(attributes).length > 0) {
3458
+ const nodeAttributes = node.node.attrs;
3459
+ const attrKeys = Object.keys(attributes);
3460
+ for (let index = 0; index < attrKeys.length; index += 1) {
3461
+ const key = attrKeys[index];
3462
+ if (nodeAttributes[key] !== attributes[key]) {
3463
+ return;
3464
+ }
3465
+ }
3466
+ }
3467
+ nodes.push(node);
3468
+ if (firstItemOnly) {
3469
+ return;
3470
+ }
3471
+ }
3472
+ nodes = nodes.concat(node.querySelectorAll(selector));
3473
+ });
3474
+ return nodes;
3475
+ }
3476
+ setAttribute(attributes) {
3477
+ const oldSelection = this.editor.state.selection;
3478
+ this.editor.chain().setTextSelection(this.from).updateAttributes(this.node.type.name, attributes).setTextSelection(oldSelection.from)
3479
+ .run();
3480
+ }
3481
+ }
3482
+
3312
3483
  const style = `.ProseMirror {
3313
3484
  position: relative;
3314
3485
  }
@@ -3754,6 +3925,21 @@ class Editor extends EventEmitter {
3754
3925
  // @ts-ignore
3755
3926
  return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3756
3927
  }
3928
+ $node(selector, attributes) {
3929
+ var _a;
3930
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelector(selector, attributes)) || null;
3931
+ }
3932
+ $nodes(selector, attributes) {
3933
+ var _a;
3934
+ return ((_a = this.$doc) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selector, attributes)) || null;
3935
+ }
3936
+ $pos(pos) {
3937
+ const $pos = this.state.doc.resolve(pos);
3938
+ return new NodePos($pos, this);
3939
+ }
3940
+ get $doc() {
3941
+ return this.$pos(0);
3942
+ }
3757
3943
  }
3758
3944
 
3759
3945
  /**
@@ -3943,14 +4129,14 @@ class Mark {
3943
4129
  this.child = null;
3944
4130
  this.config = {
3945
4131
  name: this.name,
3946
- defaultOptions: undefined,
4132
+ defaultOptions: {},
3947
4133
  };
3948
4134
  this.config = {
3949
4135
  ...this.config,
3950
4136
  ...config,
3951
4137
  };
3952
4138
  this.name = this.config.name;
3953
- if (config.defaultOptions) {
4139
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
3954
4140
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
3955
4141
  }
3956
4142
  // TODO: remove `addOptions` fallback
@@ -4026,14 +4212,14 @@ class Node {
4026
4212
  this.child = null;
4027
4213
  this.config = {
4028
4214
  name: this.name,
4029
- defaultOptions: undefined,
4215
+ defaultOptions: {},
4030
4216
  };
4031
4217
  this.config = {
4032
4218
  ...this.config,
4033
4219
  ...config,
4034
4220
  };
4035
4221
  this.name = this.config.name;
4036
- if (config.defaultOptions) {
4222
+ if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
4037
4223
  console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`);
4038
4224
  }
4039
4225
  // TODO: remove `addOptions` fallback
@@ -4396,5 +4582,5 @@ class Tracker {
4396
4582
  }
4397
4583
  }
4398
4584
 
4399
- export { CommandManager, Editor, Extension, InputRule, Mark, Node, NodeView, PasteRule, Tracker, callOrReturn, combineTransactionSteps, createChainableState, createDocument, createNodeFromContent, createStyleTag, defaultBlockAt, deleteProps, elementFromString, escapeForRegEx, extensions, findChildren, findChildrenInRange, findDuplicates, findParentNode, findParentNodeClosestToPos, fromString, generateHTML, generateJSON, generateText, getAttributes, getAttributesFromExtensions, getChangedRanges, getDebugJSON, getExtensionField, getHTMLFromFragment, getMarkAttributes, getMarkRange, getMarkType, getMarksBetween, getNodeAtPosition, getNodeAttributes, getNodeType, getRenderedAttributes, getSchema, getSchemaByResolvedExtensions, getSchemaTypeByName, getSchemaTypeNameByName, getSplittedAttributes, getText, getTextBetween, getTextContentFromNodes, getTextSerializersFromSchema, injectExtensionAttributesToParseRule, inputRulesPlugin, isActive, isAtEndOfNode, isAtStartOfNode, isEmptyObject, isExtensionRulesEnabled, isFunction, isList, isMacOS, isMarkActive, isNodeActive, isNodeEmpty, isNodeSelection, isNumber, isPlainObject, isRegExp, isString, isTextSelection, isiOS, markInputRule, markPasteRule, mergeAttributes, mergeDeep, minMax, nodeInputRule, nodePasteRule, objectIncludes, pasteRulesPlugin, posToDOMRect, removeDuplicates, resolveFocusPosition, selectionToInsertionEnd, splitExtensions, textInputRule, textPasteRule, textblockTypeInputRule, wrappingInputRule };
4585
+ export { CommandManager, Editor, Extension, InputRule, Mark, Node, NodePos, NodeView, PasteRule, Tracker, callOrReturn, combineTransactionSteps, createChainableState, createDocument, createNodeFromContent, createStyleTag, defaultBlockAt, deleteProps, elementFromString, escapeForRegEx, extensions, findChildren, findChildrenInRange, findDuplicates, findParentNode, findParentNodeClosestToPos, fromString, generateHTML, generateJSON, generateText, getAttributes, getAttributesFromExtensions, getChangedRanges, getDebugJSON, getExtensionField, getHTMLFromFragment, getMarkAttributes, getMarkRange, getMarkType, getMarksBetween, getNodeAtPosition, getNodeAttributes, getNodeType, getRenderedAttributes, getSchema, getSchemaByResolvedExtensions, getSchemaTypeByName, getSchemaTypeNameByName, getSplittedAttributes, getText, getTextBetween, getTextContentFromNodes, getTextSerializersFromSchema, injectExtensionAttributesToParseRule, inputRulesPlugin, isActive, isAtEndOfNode, isAtStartOfNode, isEmptyObject, isExtensionRulesEnabled, isFunction, isList, isMacOS, isMarkActive, isNodeActive, isNodeEmpty, isNodeSelection, isNumber, isPlainObject, isRegExp, isString, isTextSelection, isiOS, markInputRule, markPasteRule, mergeAttributes, mergeDeep, minMax, nodeInputRule, nodePasteRule, objectIncludes, pasteRulesPlugin, posToDOMRect, removeDuplicates, resolveFocusPosition, selectionToInsertionEnd, splitExtensions, textInputRule, textPasteRule, textblockTypeInputRule, wrappingInputRule };
4400
4586
  //# sourceMappingURL=index.js.map