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,45 +6,41 @@ 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 => ({
type: mark.type.name,
attrs: { ...mark.attrs },
}))
const attrs = { ...node.attrs }
const output: DebugJSONContent = {
type: node.type.name,
from,
to,
}
node.forEach((n, offset) => { if (Object.keys(attrs).length) {
const from = startOffset + offset output.attrs = attrs
const to = from + n.nodeSize }
const marks = n.marks.map(mark => ({
type: mark.type.name,
attrs: { ...mark.attrs },
}))
const attrs = { ...n.attrs }
const content = getDebugJSON(n, from + 1)
const output: DebugJSONContent = {
type: n.type.name,
from,
to,
}
if (Object.keys(attrs).length) { if (marks.length) {
output.attrs = attrs output.marks = marks
} }
if (marks.length) { if (node.content.childCount) {
output.marks = marks output.content = []
}
if (content.length) { node.forEach((child, offset) => {
output.content = content output.content?.push(getDebugJSON(child, startOffset + offset + increment))
} })
}
if (n.text) { if (node.text) {
output.text = n.text output.text = node.text
} }
nodes.push(output) return output
})
return nodes
} }