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