@tiptap/core
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -6,6 +6,10 @@ import { Fragment, Node, ResolvedPos } from '@tiptap/pm/model'; | ||
private resolvedPos; | ||
private isBlock; | ||
private editor; | ||
constructor(pos: ResolvedPos, editor: Editor); | ||
private get name(); | ||
constructor(pos: ResolvedPos, editor: Editor, isBlock?: boolean, node?: Node | null); | ||
private currentNode; | ||
get node(): Node; | ||
get element(): HTMLElement; | ||
actualDepth: number | null; | ||
get depth(): number; | ||
@@ -12,0 +16,0 @@ get pos(): number; |
{ | ||
"name": "@tiptap/core", | ||
"description": "headless rich text editor", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"homepage": "https://tiptap.dev", | ||
@@ -35,3 +35,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@tiptap/pm": "^2.2.0" | ||
"@tiptap/pm": "^2.2.1" | ||
}, | ||
@@ -38,0 +38,0 @@ "peerDependencies": { |
@@ -11,11 +11,21 @@ import { | ||
private isBlock: boolean | ||
private editor: Editor | ||
constructor(pos: ResolvedPos, editor: Editor) { | ||
private get name(): string { | ||
return this.node.type.name | ||
} | ||
constructor(pos: ResolvedPos, editor: Editor, isBlock = false, node: Node | null = null) { | ||
this.isBlock = isBlock | ||
this.resolvedPos = pos | ||
this.editor = editor | ||
this.currentNode = node | ||
} | ||
private currentNode: Node | null = null | ||
get node(): Node { | ||
return this.resolvedPos.node() | ||
return this.currentNode || this.resolvedPos.node() | ||
} | ||
@@ -27,4 +37,6 @@ | ||
public actualDepth: number | null = null | ||
get depth(): number { | ||
return this.resolvedPos.depth | ||
return this.actualDepth ?? this.resolvedPos.depth | ||
} | ||
@@ -41,3 +53,16 @@ | ||
set content(content: Content) { | ||
this.editor.commands.insertContentAt({ from: this.from, to: this.to }, content) | ||
let from = this.from | ||
let to = this.to | ||
if (this.isBlock) { | ||
if (this.content.size === 0) { | ||
console.error(`You can’t set content on a block node. Tried to set content on ${this.name} at ${this.pos}`) | ||
return | ||
} | ||
from = this.from + 1 | ||
to = this.to - 1 | ||
} | ||
this.editor.commands.insertContentAt({ from, to }, content) | ||
} | ||
@@ -58,2 +83,6 @@ | ||
get from(): number { | ||
if (this.isBlock) { | ||
return this.pos | ||
} | ||
return this.resolvedPos.start(this.resolvedPos.depth) | ||
@@ -70,2 +99,6 @@ } | ||
get to(): number { | ||
if (this.isBlock) { | ||
return this.pos + this.size | ||
} | ||
return this.resolvedPos.end(this.resolvedPos.depth) + (this.node.isText ? 0 : 1) | ||
@@ -86,3 +119,3 @@ } | ||
get before(): NodePos | null { | ||
let $pos = this.resolvedPos.doc.resolve(this.from - 2) | ||
let $pos = this.resolvedPos.doc.resolve(this.from - (this.isBlock ? 1 : 2)) | ||
@@ -97,3 +130,3 @@ if ($pos.depth !== this.depth) { | ||
get after(): NodePos | null { | ||
let $pos = this.resolvedPos.doc.resolve(this.to + 2) | ||
let $pos = this.resolvedPos.doc.resolve(this.to + (this.isBlock ? 2 : 1)) | ||
@@ -111,10 +144,18 @@ if ($pos.depth !== this.depth) { | ||
this.node.content.forEach((node, offset) => { | ||
const targetPos = this.pos + offset + 1 | ||
const isBlock = node.isBlock && !node.isTextblock | ||
const targetPos = this.pos + offset + (isBlock ? 0 : 1) | ||
const $pos = this.resolvedPos.doc.resolve(targetPos) | ||
if ($pos.depth === this.depth) { | ||
if (!isBlock && $pos.depth <= this.depth) { | ||
return | ||
} | ||
children.push(new NodePos($pos, this.editor)) | ||
const childNodePos = new NodePos($pos, this.editor, isBlock, isBlock ? node : null) | ||
if (isBlock) { | ||
childNodePos.actualDepth = this.depth + 1 | ||
} | ||
children.push(new NodePos($pos, this.editor, isBlock, isBlock ? node : null)) | ||
}) | ||
@@ -171,10 +212,10 @@ | ||
// iterate through children recursively finding all nodes which match the selector with the node name | ||
if (!this.children || this.children.length === 0) { | ||
if (this.isBlock || !this.children || this.children.length === 0) { | ||
return nodes | ||
} | ||
this.children.forEach(node => { | ||
if (node.node.type.name === selector) { | ||
this.children.forEach(childPos => { | ||
if (childPos.node.type.name === selector) { | ||
if (Object.keys(attributes).length > 0) { | ||
const nodeAttributes = node.node.attrs | ||
const nodeAttributes = childPos.node.attrs | ||
const attrKeys = Object.keys(attributes) | ||
@@ -191,3 +232,3 @@ | ||
nodes.push(node) | ||
nodes.push(childPos) | ||
@@ -199,3 +240,3 @@ if (firstItemOnly) { | ||
nodes = nodes.concat(node.querySelectorAll(selector)) | ||
nodes = nodes.concat(childPos.querySelectorAll(selector)) | ||
}) | ||
@@ -202,0 +243,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
2041632
23904