Improve key compare in rendering (#1027)

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
Tilman Vatteroth 2021-02-10 16:10:57 +01:00 committed by GitHub
parent ae38ab007a
commit 37234f9c09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 71 additions and 78 deletions

View file

@ -36,16 +36,18 @@ export const calculateKeyFromLineMarker = (node: DomElement, lineKeys?: LineKeys
return
}
const startLine = Number(startLineInMarkdown)
const endLine = Number(endLineInMarkdown)
if (lineKeys[startLine] === undefined || lineKeys[endLine] === undefined) {
const startLineIndex = Number(startLineInMarkdown)
const endLineIndex = Number(endLineInMarkdown)
const startLine = lineKeys[startLineIndex - 1]
const endLine = lineKeys[endLineIndex - 2]
if (startLine === undefined || endLine === undefined) {
return
}
return `${ lineKeys[startLine].id }_${ lineKeys[endLine].id }`
return `${ startLine.id }_${ endLine.id }`
}
export const findNodeReplacement = (node: DomElement, key: string, allReplacers: ComponentReplacer[], subNodeTransform: SubNodeTransform, nativeRenderer: NativeRenderer): ReactElement | null | undefined => {
export const findNodeReplacement = (node: DomElement, allReplacers: ComponentReplacer[], subNodeTransform: SubNodeTransform, nativeRenderer: NativeRenderer): ReactElement | null | undefined => {
return allReplacers
.map((componentReplacer) => componentReplacer.getReplacement(node, subNodeTransform, nativeRenderer))
.find((replacement) => replacement !== undefined)
@ -66,7 +68,7 @@ export const buildTransformer = (lineKeys: (LineKeys[] | undefined), allReplacer
const subNodeTransform: SubNodeTransform = (subNode, subIndex) => transform(subNode, subIndex, transform)
const key = calculateKeyFromLineMarker(node, lineKeys) ?? (-index).toString()
const tryReplacement = findNodeReplacement(node, key, allReplacers, subNodeTransform, nativeRenderer)
const tryReplacement = findNodeReplacement(node, allReplacers, subNodeTransform, nativeRenderer)
if (tryReplacement === null) {
return null
} else if (tryReplacement === undefined) {