@tiptap/core 2.2.0-rc.8 → 2.2.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
@@ -1587,10 +1587,24 @@ const insertContent = (value, options) => ({ tr, commands }) => {
1587
1587
  return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options);
1588
1588
  };
1589
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
+ };
1590
1603
  function elementFromString(value) {
1591
1604
  // add a wrapper to preserve leading and trailing whitespace
1592
1605
  const wrappedValue = `<body>${value}</body>`;
1593
- return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1606
+ const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body;
1607
+ return removeWhitespaces(html);
1594
1608
  }
1595
1609
 
1596
1610
  function createNodeFromContent(content, schema, options) {
@@ -2318,7 +2332,7 @@ function getMarksBetween(from, to, doc) {
2318
2332
  }
2319
2333
  else {
2320
2334
  doc.nodesBetween(from, to, (node, pos) => {
2321
- if (!node || node.nodeSize === undefined) {
2335
+ if (!node || (node === null || node === void 0 ? void 0 : node.nodeSize) === undefined) {
2322
2336
  return;
2323
2337
  }
2324
2338
  marks.push(...node.marks.map(mark => ({
@@ -3326,18 +3340,26 @@ var extensions = /*#__PURE__*/Object.freeze({
3326
3340
  });
3327
3341
 
3328
3342
  class NodePos {
3329
- constructor(pos, editor) {
3343
+ constructor(pos, editor, isBlock = false, node = null) {
3344
+ this.currentNode = null;
3345
+ this.actualDepth = null;
3346
+ this.isBlock = isBlock;
3330
3347
  this.resolvedPos = pos;
3331
3348
  this.editor = editor;
3349
+ this.currentNode = node;
3350
+ }
3351
+ get name() {
3352
+ return this.node.type.name;
3332
3353
  }
3333
3354
  get node() {
3334
- return this.resolvedPos.node();
3355
+ return this.currentNode || this.resolvedPos.node();
3335
3356
  }
3336
3357
  get element() {
3337
3358
  return this.editor.view.domAtPos(this.pos).node;
3338
3359
  }
3339
3360
  get depth() {
3340
- return this.resolvedPos.depth;
3361
+ var _a;
3362
+ return (_a = this.actualDepth) !== null && _a !== void 0 ? _a : this.resolvedPos.depth;
3341
3363
  }
3342
3364
  get pos() {
3343
3365
  return this.resolvedPos.pos;
@@ -3346,7 +3368,17 @@ class NodePos {
3346
3368
  return this.node.content;
3347
3369
  }
3348
3370
  set content(content) {
3349
- this.editor.commands.insertContentAt({ from: this.from, to: this.to }, content);
3371
+ let from = this.from;
3372
+ let to = this.to;
3373
+ if (this.isBlock) {
3374
+ if (this.content.size === 0) {
3375
+ console.error(`You can’t set content on a block node. Tried to set content on ${this.name} at ${this.pos}`);
3376
+ return;
3377
+ }
3378
+ from = this.from + 1;
3379
+ to = this.to - 1;
3380
+ }
3381
+ this.editor.commands.insertContentAt({ from, to }, content);
3350
3382
  }
3351
3383
  get attributes() {
3352
3384
  return this.node.attrs;
@@ -3358,6 +3390,9 @@ class NodePos {
3358
3390
  return this.node.nodeSize;
3359
3391
  }
3360
3392
  get from() {
3393
+ if (this.isBlock) {
3394
+ return this.pos;
3395
+ }
3361
3396
  return this.resolvedPos.start(this.resolvedPos.depth);
3362
3397
  }
3363
3398
  get range() {
@@ -3367,6 +3402,9 @@ class NodePos {
3367
3402
  };
3368
3403
  }
3369
3404
  get to() {
3405
+ if (this.isBlock) {
3406
+ return this.pos + this.size;
3407
+ }
3370
3408
  return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1);
3371
3409
  }
3372
3410
  get parent() {
@@ -3378,14 +3416,14 @@ class NodePos {
3378
3416
  return new NodePos($pos, this.editor);
3379
3417
  }
3380
3418
  get before() {
3381
- let $pos = this.resolvedPos.doc.resolve(this.from - 2);
3419
+ let $pos = this.resolvedPos.doc.resolve(this.from - (this.isBlock ? 1 : 2));
3382
3420
  if ($pos.depth !== this.depth) {
3383
3421
  $pos = this.resolvedPos.doc.resolve(this.from - 3);
3384
3422
  }
3385
3423
  return new NodePos($pos, this.editor);
3386
3424
  }
3387
3425
  get after() {
3388
- let $pos = this.resolvedPos.doc.resolve(this.to + 2);
3426
+ let $pos = this.resolvedPos.doc.resolve(this.to + (this.isBlock ? 2 : 1));
3389
3427
  if ($pos.depth !== this.depth) {
3390
3428
  $pos = this.resolvedPos.doc.resolve(this.to + 3);
3391
3429
  }
@@ -3394,12 +3432,17 @@ class NodePos {
3394
3432
  get children() {
3395
3433
  const children = [];
3396
3434
  this.node.content.forEach((node, offset) => {
3397
- const targetPos = this.pos + offset + 1;
3435
+ const isBlock = node.isBlock && !node.isTextblock;
3436
+ const targetPos = this.pos + offset + (isBlock ? 0 : 1);
3398
3437
  const $pos = this.resolvedPos.doc.resolve(targetPos);
3399
- if ($pos.depth === this.depth) {
3438
+ if (!isBlock && $pos.depth <= this.depth) {
3400
3439
  return;
3401
3440
  }
3402
- children.push(new NodePos($pos, this.editor));
3441
+ const childNodePos = new NodePos($pos, this.editor, isBlock, isBlock ? node : null);
3442
+ if (isBlock) {
3443
+ childNodePos.actualDepth = this.depth + 1;
3444
+ }
3445
+ children.push(new NodePos($pos, this.editor, isBlock, isBlock ? node : null));
3403
3446
  });
3404
3447
  return children;
3405
3448
  }
@@ -3439,13 +3482,13 @@ class NodePos {
3439
3482
  querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3440
3483
  let nodes = [];
3441
3484
  // iterate through children recursively finding all nodes which match the selector with the node name
3442
- if (!this.children || this.children.length === 0) {
3485
+ if (this.isBlock || !this.children || this.children.length === 0) {
3443
3486
  return nodes;
3444
3487
  }
3445
- this.children.forEach(node => {
3446
- if (node.node.type.name === selector) {
3488
+ this.children.forEach(childPos => {
3489
+ if (childPos.node.type.name === selector) {
3447
3490
  if (Object.keys(attributes).length > 0) {
3448
- const nodeAttributes = node.node.attrs;
3491
+ const nodeAttributes = childPos.node.attrs;
3449
3492
  const attrKeys = Object.keys(attributes);
3450
3493
  for (let index = 0; index < attrKeys.length; index += 1) {
3451
3494
  const key = attrKeys[index];
@@ -3454,12 +3497,12 @@ class NodePos {
3454
3497
  }
3455
3498
  }
3456
3499
  }
3457
- nodes.push(node);
3500
+ nodes.push(childPos);
3458
3501
  if (firstItemOnly) {
3459
3502
  return;
3460
3503
  }
3461
3504
  }
3462
- nodes = nodes.concat(node.querySelectorAll(selector));
3505
+ nodes = nodes.concat(childPos.querySelectorAll(selector));
3463
3506
  });
3464
3507
  return nodes;
3465
3508
  }