-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathgoogleDocsAdapter.ts
More file actions
154 lines (137 loc) · 4.5 KB
/
Copy pathgoogleDocsAdapter.ts
File metadata and controls
154 lines (137 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { type Span, SuggestionKind } from 'harper.js';
import { domRectToBox, type IgnorableLintBox, isBottomEdgeInBox, shrinkBoxToFit } from './Box';
import {
getGoogleDocsHighlightRects,
isGoogleDocsTarget,
replaceGoogleDocsValue,
resolveGoogleDocsSpan,
} from './computeLintBoxes/googleDocsUtilities';
import type SourceElement from './SourceElement';
import type { UnpackedLint, UnpackedSpan, UnpackedSuggestion } from './unpackLint';
const GOOGLE_DOCS_EDITOR_SELECTOR = '.kix-appview-editor';
const GOOGLE_DOCS_SYNCING_ATTR = 'data-harper-gdocs-syncing';
/**
* Returns Google Docs-specific lint boxes for the hidden bridge target.
*
* Non-Google Docs targets return `null` so callers can continue down the generic
* lint-box pipeline without duplicating source checks.
*/
export function maybeComputeGoogleDocsLintBoxes(
target: HTMLElement,
lint: UnpackedLint,
rule: string,
opts: { ignoreLint?: (hash: string) => Promise<void> },
): IgnorableLintBox[] | null {
if (!isGoogleDocsTarget(target)) {
return null;
}
try {
const editor = document.querySelector(GOOGLE_DOCS_EDITOR_SELECTOR) as HTMLElement | null;
const source = target.textContent ?? '';
const resolvedSpan = resolveGoogleDocsSpan(source, lint);
if (!editor || resolvedSpan == null) {
return [];
}
const targetRects = getGoogleDocsHighlightRects(target, resolvedSpan as Span);
const editorBox = domRectToBox(editor.getBoundingClientRect());
if (targetRects.length === 0) {
return [];
}
const boxes: IgnorableLintBox[] = [];
for (const targetRect of targetRects as DOMRect[]) {
if (!isBottomEdgeInBox(targetRect, editorBox)) {
continue;
}
const shrunkBox = shrinkBoxToFit(targetRect, editorBox);
boxes.push({
x: shrunkBox.x,
y: shrunkBox.y,
width: shrunkBox.width,
height: shrunkBox.height,
lint,
source: editor,
rule,
applySuggestion: async (suggestion: UnpackedSuggestion) => {
const replacementText = suggestionToReplacementText(suggestion, resolvedSpan, source);
await replaceGoogleDocsValue(resolvedSpan, replacementText, source);
},
ignoreLint: opts.ignoreLint ? () => opts.ignoreLint!(lint.context_hash) : undefined,
});
}
return boxes;
} catch {
return [];
}
}
/**
* Narrows a render or lint source to the live Google Docs editor container.
*/
export function isGoogleDocsSource(source: SourceElement): source is HTMLElement {
return (
source instanceof HTMLElement &&
(source.classList.contains('kix-appview-editor') ||
source.closest(GOOGLE_DOCS_EDITOR_SELECTOR) != null)
);
}
/**
* Reports whether Google Docs is in the middle of a bridge sync and highlights
* should preserve their current rendered state.
*/
export function isGoogleDocsSourceSyncing(source: SourceElement): boolean {
return isGoogleDocsSource(source) && source.getAttribute(GOOGLE_DOCS_SYNCING_ATTR) === 'true';
}
/**
* Returns the element that should own rendered highlight boxes for a Google Docs source.
*/
export function getGoogleDocsRenderTarget(source: SourceElement): HTMLElement | null {
return isGoogleDocsSource(source) ? source : null;
}
/**
* Computes the absolute render offset for highlight boxes anchored to the
* scrolling Google Docs editor surface.
*/
export function getGoogleDocsRenderOffset(source: SourceElement): { x: number; y: number } | null {
if (!isGoogleDocsSource(source)) {
return null;
}
const editorRect = source.getBoundingClientRect();
return {
x: editorRect.x - source.scrollLeft,
y: editorRect.y - source.scrollTop,
};
}
/**
* Applies the host positioning required for Google Docs highlights.
*
* Returns `true` when the source was recognized as Google Docs and the caller
* should skip its generic host styling path.
*/
export function applyGoogleDocsRenderHostStyle(host: HTMLElement, source: SourceElement): boolean {
if (!isGoogleDocsSource(source)) {
return false;
}
host.style.position = 'absolute';
host.style.top = '0px';
host.style.left = '0px';
host.style.pointerEvents = 'none';
host.style.width = '0px';
host.style.height = '0px';
host.style.contain = 'none';
host.style.transform = 'none';
host.style.zIndex = '2147483647';
return true;
}
function suggestionToReplacementText(
suggestion: UnpackedSuggestion,
span: UnpackedSpan,
source: string,
): string {
switch (suggestion.kind) {
case SuggestionKind.Replace:
return suggestion.replacement_text;
case SuggestionKind.Remove:
return '';
case SuggestionKind.InsertAfter:
return source.slice(span.start, span.end) + suggestion.replacement_text;
}
}