-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove empty attributes in getDebugJSON
- Loading branch information
1 parent
61b06e7
commit 940fd69
Showing
1 changed file
with
44 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
import { Node as ProseMirrorNode } from 'prosemirror-model' | ||
import { JSONContent } from '../types' | ||
|
||
interface DebugJSONContent extends JSONContent { | ||
from: number, | ||
to: number, | ||
} | ||
|
||
/** | ||
* Returns a node tree with node positions. | ||
*/ | ||
export default function getDebugJSON(node: ProseMirrorNode) { | ||
const debug = (startNode: ProseMirrorNode, startOffset = 0) => { | ||
const nodes: any[] = [] | ||
|
||
startNode.forEach((n, offset) => { | ||
const from = startOffset + offset | ||
const to = from + n.nodeSize | ||
|
||
nodes.push({ | ||
type: n.type.name, | ||
attrs: { ...n.attrs }, | ||
from, | ||
to, | ||
marks: n.marks.map(mark => ({ | ||
type: mark.type.name, | ||
attrs: { ...mark.attrs }, | ||
})), | ||
content: debug(n, from + 1), | ||
}) | ||
}) | ||
|
||
return nodes | ||
} | ||
|
||
return debug(node) | ||
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 => ({ | ||
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) { | ||
output.attrs = attrs | ||
} | ||
|
||
if (marks.length) { | ||
output.marks = marks | ||
} | ||
|
||
if (content.length) { | ||
output.content = content | ||
} | ||
|
||
if (n.text) { | ||
output.text = n.text | ||
} | ||
|
||
nodes.push(output) | ||
}) | ||
|
||
return nodes | ||
} |