Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Merge pull request #1418 from ecency/bugfix/editor-preview-dup-lines
Browse files Browse the repository at this point in the history
Editor preview: fixed duplicated lines
  • Loading branch information
feruzm authored Jul 22, 2023
2 parents ca32012 + b05de70 commit ab46309
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/common/components/post-body-lazy-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ interface Props {
className?: string;
}

interface Line {
element: Element;
hash: string;
}

export function PostBodyLazyRenderer({ rawBody, className }: Props) {
const [result, setResult] = useState<Element[]>([]);
const [hashes, setHashes] = useState<string[]>([]);
const [lines, setLines] = useState<Line[]>([]);
const { global } = useMappedStore();

useEffect(() => {
Expand All @@ -25,32 +29,23 @@ export function PostBodyLazyRenderer({ rawBody, className }: Props) {
const tree = document.createElement("div");
tree.innerHTML = renderedBody;

const nextHashes: string[] = [];
const linesToRender: number[] = [];
const nextLines: Element[] = [];
const nextLines: Line[] = [];

for (let i = 0; i < tree.children.length; i++) {
const child = tree.children.item(i)!!;

const hash = md5(child.innerHTML);
const existingHash = hashes[i];
const element = tree.children.item(i)!!;

if (hash !== existingHash) {
linesToRender.push(i);
}
const hash = md5(element.outerHTML + `index-${i}`);

nextHashes.push(hash);
nextLines.push(child);
nextLines.push({ element, hash });
}

setHashes(nextHashes);
setResult(nextLines);
setLines(nextLines);
};

return (
<div className={className}>
{result.map((line, i) => (
<div key={hashes[i]} dangerouslySetInnerHTML={{ __html: line.outerHTML }} />
{lines.map(({ element, hash }) => (
<div key={hash} dangerouslySetInnerHTML={{ __html: element.outerHTML }} />
))}
</div>
);
Expand Down

0 comments on commit ab46309

Please sign in to comment.