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