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