@tiptap/core 2.2.0 → 2.2.2

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
@@ -3334,18 +3334,26 @@
3334
3334
  });
3335
3335
 
3336
3336
  class NodePos {
3337
- constructor(pos, editor) {
3337
+ constructor(pos, editor, isBlock = false, node = null) {
3338
+ this.currentNode = null;
3339
+ this.actualDepth = null;
3340
+ this.isBlock = isBlock;
3338
3341
  this.resolvedPos = pos;
3339
3342
  this.editor = editor;
3343
+ this.currentNode = node;
3344
+ }
3345
+ get name() {
3346
+ return this.node.type.name;
3340
3347
  }
3341
3348
  get node() {
3342
- return this.resolvedPos.node();
3349
+ return this.currentNode || this.resolvedPos.node();
3343
3350
  }
3344
3351
  get element() {
3345
3352
  return this.editor.view.domAtPos(this.pos).node;
3346
3353
  }
3347
3354
  get depth() {
3348
- return this.resolvedPos.depth;
3355
+ var _a;
3356
+ return (_a = this.actualDepth) !== null && _a !== void 0 ? _a : this.resolvedPos.depth;
3349
3357
  }
3350
3358
  get pos() {
3351
3359
  return this.resolvedPos.pos;
@@ -3354,7 +3362,17 @@
3354
3362
  return this.node.content;
3355
3363
  }
3356
3364
  set content(content) {
3357
- 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);
3358
3376
  }
3359
3377
  get attributes() {
3360
3378
  return this.node.attrs;
@@ -3366,6 +3384,9 @@
3366
3384
  return this.node.nodeSize;
3367
3385
  }
3368
3386
  get from() {
3387
+ if (this.isBlock) {
3388
+ return this.pos;
3389
+ }
3369
3390
  return this.resolvedPos.start(this.resolvedPos.depth);
3370
3391
  }
3371
3392
  get range() {
@@ -3375,6 +3396,9 @@
3375
3396
  };
3376
3397
  }
3377
3398
  get to() {
3399
+ if (this.isBlock) {
3400
+ return this.pos + this.size;
3401
+ }
3378
3402
  return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1);
3379
3403
  }
3380
3404
  get parent() {
@@ -3386,14 +3410,14 @@
3386
3410
  return new NodePos($pos, this.editor);
3387
3411
  }
3388
3412
  get before() {
3389
- let $pos = this.resolvedPos.doc.resolve(this.from - 2);
3413
+ let $pos = this.resolvedPos.doc.resolve(this.from - (this.isBlock ? 1 : 2));
3390
3414
  if ($pos.depth !== this.depth) {
3391
3415
  $pos = this.resolvedPos.doc.resolve(this.from - 3);
3392
3416
  }
3393
3417
  return new NodePos($pos, this.editor);
3394
3418
  }
3395
3419
  get after() {
3396
- let $pos = this.resolvedPos.doc.resolve(this.to + 2);
3420
+ let $pos = this.resolvedPos.doc.resolve(this.to + (this.isBlock ? 2 : 1));
3397
3421
  if ($pos.depth !== this.depth) {
3398
3422
  $pos = this.resolvedPos.doc.resolve(this.to + 3);
3399
3423
  }
@@ -3402,12 +3426,17 @@
3402
3426
  get children() {
3403
3427
  const children = [];
3404
3428
  this.node.content.forEach((node, offset) => {
3405
- const targetPos = this.pos + offset + 1;
3429
+ const isBlock = node.isBlock && !node.isTextblock;
3430
+ const targetPos = this.pos + offset + (isBlock ? 0 : 1);
3406
3431
  const $pos = this.resolvedPos.doc.resolve(targetPos);
3407
- if ($pos.depth === this.depth) {
3432
+ if (!isBlock && $pos.depth <= this.depth) {
3408
3433
  return;
3409
3434
  }
3410
- 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));
3411
3440
  });
3412
3441
  return children;
3413
3442
  }
@@ -3447,13 +3476,13 @@
3447
3476
  querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3448
3477
  let nodes = [];
3449
3478
  // iterate through children recursively finding all nodes which match the selector with the node name
3450
- if (!this.children || this.children.length === 0) {
3479
+ if (this.isBlock || !this.children || this.children.length === 0) {
3451
3480
  return nodes;
3452
3481
  }
3453
- this.children.forEach(node => {
3454
- if (node.node.type.name === selector) {
3482
+ this.children.forEach(childPos => {
3483
+ if (childPos.node.type.name === selector) {
3455
3484
  if (Object.keys(attributes).length > 0) {
3456
- const nodeAttributes = node.node.attrs;
3485
+ const nodeAttributes = childPos.node.attrs;
3457
3486
  const attrKeys = Object.keys(attributes);
3458
3487
  for (let index = 0; index < attrKeys.length; index += 1) {
3459
3488
  const key = attrKeys[index];
@@ -3462,12 +3491,12 @@
3462
3491
  }
3463
3492
  }
3464
3493
  }
3465
- nodes.push(node);
3494
+ nodes.push(childPos);
3466
3495
  if (firstItemOnly) {
3467
3496
  return;
3468
3497
  }
3469
3498
  }
3470
- nodes = nodes.concat(node.querySelectorAll(selector));
3499
+ nodes = nodes.concat(childPos.querySelectorAll(selector));
3471
3500
  });
3472
3501
  return nodes;
3473
3502
  }