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