Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(chrome-ext): preserve sentences across Google Docs pages (#4756)
Elijah Potter99e5ac24f66e
diff --git a/packages/chrome-plugin/src/contentScript/googleDocs.ts b/packages/chrome-plugin/src/contentScript/googleDocs.tsindex 7b6a04e94..50ba8060b 100644--- a/packages/chrome-plugin/src/contentScript/googleDocs.ts+++ b/packages/chrome-plugin/src/contentScript/googleDocs.ts@@ -31,6 +31,7 @@ type LayoutRefreshFramework = LintFramework & {type GoogleDocsRectSegment = {rectNode: SVGRectElement;+ page: Element | null;text: string;rect: GoogleDocsRectLayout;};@@ -269,6 +270,7 @@ export function createGoogleDocsBridgeSync(fw: LintFramework): () => Promise<voisegments.push({rectNode,+ page: rectNode.closest('.kix-page-paginated'),text,rect: {top,@@ -431,14 +433,94 @@ export function createGoogleDocsBridgeSync(fw: LintFramework): () => Promise<voi};}+ function getPageBoundarySeparator(+ previous: GoogleDocsRectSegment,+ current: GoogleDocsRectSegment,+ logicalText: string,+ ): string {+ // A failed full-document alignment need not discard the logical gap here.+ // Repeated text is safe only when all occurrences are adjacent and agree.+ const currentContent = current.text.trimStart();+ const currentPrefix = current.text+ .slice(0, current.text.length - currentContent.length)+ .replace(/\u00a0/gu, ' ');+ let separator: string | null = null;+ let conflictingSeparators = false;+ let previousOccurrences = 0;+ let adjacentPairs = 0;+ let omittedProse = false;+ let cursor = 0;+ while (cursor < logicalText.length) {+ const start = logicalText.indexOf(previous.text, cursor);+ if (start < 0) break;+ previousOccurrences += 1;+ const end = start + previous.text.length;+ const nextStart = logicalText.indexOf(currentContent, end);+ if (nextStart >= 0 && !canIgnoreLogicalGap(logicalText.slice(end, nextStart))) {+ omittedProse = true;+ }+ const whitespace = /^\s*/u.exec(logicalText.slice(end))?.[0] ?? '';+ if (logicalText.startsWith(currentContent, end + whitespace.length)) {+ adjacentPairs += 1;+ // The rectangle already owns its prefix, even if Docs used regular spaces.+ const gap = whitespace.replace(/\u00a0/gu, ' ').endsWith(currentPrefix)+ ? whitespace.slice(0, whitespace.length - currentPrefix.length)+ : whitespace;+ const normalized = normalizeLogicalGap(+ gap,+ previous.text,+ current.text,+ false,+ current.rect,+ null,+ );+ if (separator != null && separator !== normalized) {+ conflictingSeparators = true;+ }+ separator = normalized;+ }+ cursor = start + 1;+ }+ let currentOccurrences = 0;+ cursor = 0;+ while (currentContent.length > 0 && cursor < logicalText.length) {+ const start = logicalText.indexOf(currentContent, cursor);+ if (start < 0) break;+ currentOccurrences += 1;+ cursor = start + 1;+ }+ // Known missing prose is not an unknown gap. Isolate the visible regions+ // instead of manufacturing grammatical adjacency or choosing a repeated match.+ if (+ omittedProse ||+ (adjacentPairs > 0 &&+ currentContent.length > 0 &&+ (adjacentPairs !== previousOccurrences || adjacentPairs !== currentOccurrences))+ ) {+ return '\n\n';+ }+ if (separator != null && !conflictingSeparators) return separator;++ // Page margins are layout, not paragraph semantics. Without a usable+ // logical gap, join conservatively rather than inventing a paragraph.+ if (/\s$/u.test(previous.text) || /^\s/u.test(current.text)) return '';+ // Straight quotes and apostrophes are ambiguous, not evidence for a tight join.+ if (/[([{“‘/_—–-]$/u.test(previous.text) || /^[,./:;!?%)\]}”’—–-]/u.test(current.text)) {+ return '';+ }+ return ' ';+ }+function buildRectSnapshot(editor: HTMLElement,segments: GoogleDocsRectSegment[],+ logicalText: string,): GoogleDocsCloneSnapshot {const fragment = document.createDocumentFragment();const parts: string[] = [];let previousText = '';let previousRect: GoogleDocsRectLayout | null = null;+ let previousSegment: GoogleDocsRectSegment | null = null;let currentLineBand: GoogleDocsLineBand | null = null;for (const segment of segments) {@@ -453,16 +535,23 @@ export function createGoogleDocsBridgeSync(fw: LintFramework): () => Promise<voigetGoogleDocsParagraphBreakThreshold(currentLineBand, segment.rect),Math.min(currentLineBand.height, segment.rect.height) * GOOGLE_DOCS_PARAGRAPH_BREAK_RATIO,);- const breakText = shouldInsertSoftWrapSpace(- previousText,- segment.text,- lineGap,- paragraphBreakThreshold,- )- ? ' '- : lineGap >= paragraphBreakThreshold- ? '\n\n'- : '\n';+ const changesPage =+ previousSegment?.page != null &&+ segment.page != null &&+ previousSegment.page !== segment.page;+ const breakText =+ changesPage && previousSegment != null+ ? getPageBoundarySeparator(previousSegment, segment, logicalText)+ : shouldInsertSoftWrapSpace(+ previousText,+ segment.text,+ lineGap,+ paragraphBreakThreshold,+ )+ ? ' '+ : lineGap >= paragraphBreakThreshold+ ? '\n\n'+ : '\n';appendText(fragment, parts, breakText);} else if (previousRect != null &&@@ -476,6 +565,7 @@ export function createGoogleDocsBridgeSync(fw: LintFramework): () => Promise<voiparts.push(segment.text);previousText = segment.text;previousRect = segment.rect;+ previousSegment = segment;currentLineBand =currentLineBand == null || startsNewLine? createGoogleDocsLineBand(segment.rect)@@ -500,8 +590,20 @@ export function createGoogleDocsBridgeSync(fw: LintFramework): () => Promise<voi.getElementById(GOOGLE_DOCS_MAIN_WORLD_BRIDGE_ID)?.textContent?.replace(/\r\n?/gu, '\n') ?? '';+ // Display normalization is lossy: it can turn a raw label into an exact+ // match for a different native paragraph, or hide known omitted prose.+ // Keep raw source labels throughout ordinary native matching/projection.+ // Without native text, retain the legacy display-only normalization.+ const ordinarySegments =+ logicalText.length > 0+ ? segments.map((segment) => ({+ ...segment,+ text: segment.rectNode.getAttribute('aria-label')!,+ }))+ : segments;return (- buildLogicalSnapshot(editor, segments, logicalText) ?? buildRectSnapshot(editor, segments)+ buildLogicalSnapshot(editor, ordinarySegments, logicalText) ??+ buildRectSnapshot(editor, ordinarySegments, logicalText));}