mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 19:53:48 +08:00
fix(editor): render position of repeated sentence (#9948)
This commit is contained in:
@@ -42,6 +42,9 @@ class CanvasWorkerManager {
|
|||||||
const { canvas, ctx } = this;
|
const { canvas, ctx } = this;
|
||||||
if (!canvas || !ctx) return;
|
if (!canvas || !ctx) return;
|
||||||
|
|
||||||
|
// Track rendered positions to avoid duplicate rendering across all paragraphs and sentences
|
||||||
|
const renderedPositions = new Set<string>();
|
||||||
|
|
||||||
paragraphs.forEach(paragraph => {
|
paragraphs.forEach(paragraph => {
|
||||||
const scale = paragraph.scale ?? 1;
|
const scale = paragraph.scale ?? 1;
|
||||||
const fontSize = 15 * scale;
|
const fontSize = 15 * scale;
|
||||||
@@ -53,14 +56,16 @@ class CanvasWorkerManager {
|
|||||||
sentence.rects.forEach(textRect => {
|
sentence.rects.forEach(textRect => {
|
||||||
const x = textRect.rect.left - hostRect.left;
|
const x = textRect.rect.left - hostRect.left;
|
||||||
const y = textRect.rect.top - hostRect.top;
|
const y = textRect.rect.top - hostRect.top;
|
||||||
ctx.strokeRect(x, y, textRect.rect.width, textRect.rect.height);
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.fillStyle = 'black';
|
const posKey = `${x},${y}`;
|
||||||
sentence.rects.forEach(textRect => {
|
// Only render if we haven't rendered at this position before
|
||||||
const x = textRect.rect.left - hostRect.left;
|
if (renderedPositions.has(posKey)) return;
|
||||||
const y = textRect.rect.top - hostRect.top;
|
|
||||||
|
ctx.strokeRect(x, y, textRect.rect.width, textRect.rect.height);
|
||||||
|
ctx.fillStyle = 'black';
|
||||||
ctx.fillText(textRect.text, x, y + baselineY);
|
ctx.fillText(textRect.text, x, y + baselineY);
|
||||||
|
|
||||||
|
renderedPositions.add(posKey);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ export function getSentenceRects(
|
|||||||
element: Element,
|
element: Element,
|
||||||
sentence: string
|
sentence: string
|
||||||
): TextRect[] {
|
): TextRect[] {
|
||||||
const range = document.createRange();
|
|
||||||
const textNode = Array.from(element.childNodes).find(
|
const textNode = Array.from(element.childNodes).find(
|
||||||
node => node.nodeType === Node.TEXT_NODE
|
node => node.nodeType === Node.TEXT_NODE
|
||||||
);
|
);
|
||||||
@@ -97,13 +96,20 @@ export function getSentenceRects(
|
|||||||
if (!textNode) return [];
|
if (!textNode) return [];
|
||||||
|
|
||||||
const text = textNode.textContent || '';
|
const text = textNode.textContent || '';
|
||||||
const startIndex = text.indexOf(sentence);
|
let rects: TextRect[] = [];
|
||||||
if (startIndex === -1) return [];
|
let startIndex = 0;
|
||||||
|
|
||||||
range.setStart(textNode, startIndex);
|
// Find all occurrences of the sentence
|
||||||
range.setEnd(textNode, startIndex + sentence.length);
|
while ((startIndex = text.indexOf(sentence, startIndex)) !== -1) {
|
||||||
|
const range = document.createRange();
|
||||||
|
range.setStart(textNode, startIndex);
|
||||||
|
range.setEnd(textNode, startIndex + sentence.length);
|
||||||
|
|
||||||
return getRangeRects(range, sentence);
|
rects = rects.concat(getRangeRects(range, sentence));
|
||||||
|
startIndex += sentence.length; // Move to next potential occurrence
|
||||||
|
}
|
||||||
|
|
||||||
|
return rects;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function segmentSentences(text: string): string[] {
|
export function segmentSentences(text: string): string[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user