fix: add topNode to getDebugJSON

This commit is contained in:
Philipp Kühn
2021-08-24 17:11:13 +02:00
parent fc74deb6f3
commit f65e1b32a1

View File

@@ -6,23 +6,18 @@ interface DebugJSONContent extends JSONContent {
to: number, to: number,
} }
/** export default function getDebugJSON(node: ProseMirrorNode, startOffset = 0): DebugJSONContent {
* Returns a node tree with node positions. const isTopNode = node.type === node.type.schema.topNodeType
*/ const increment = isTopNode ? 0 : 1
export default function getDebugJSON(node: ProseMirrorNode, startOffset = 0) { const from = startOffset // + offset
const nodes: DebugJSONContent[] = [] const to = from + node.nodeSize
const marks = node.marks.map(mark => ({
node.forEach((n, offset) => {
const from = startOffset + offset
const to = from + n.nodeSize
const marks = n.marks.map(mark => ({
type: mark.type.name, type: mark.type.name,
attrs: { ...mark.attrs }, attrs: { ...mark.attrs },
})) }))
const attrs = { ...n.attrs } const attrs = { ...node.attrs }
const content = getDebugJSON(n, from + 1)
const output: DebugJSONContent = { const output: DebugJSONContent = {
type: n.type.name, type: node.type.name,
from, from,
to, to,
} }
@@ -35,16 +30,17 @@ export default function getDebugJSON(node: ProseMirrorNode, startOffset = 0) {
output.marks = marks output.marks = marks
} }
if (content.length) { if (node.content.childCount) {
output.content = content output.content = []
}
if (n.text) { node.forEach((child, offset) => {
output.text = n.text output.content?.push(getDebugJSON(child, startOffset + offset + increment))
}
nodes.push(output)
}) })
}
return nodes
if (node.text) {
output.text = node.text
}
return output
} }