Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(chrome-ext): properly support the Slate editor. (#1275)

Elijah Potter2611d1ac3e48
diff --git a/packages/chrome-plugin/src/Highlights.ts b/packages/chrome-plugin/src/Highlights.ts
index 21eb9b955..37f08cff7 100644
--- a/packages/chrome-plugin/src/Highlights.ts
+++ b/packages/chrome-plugin/src/Highlights.ts
@@ -5,6 +5,15 @@ import h from 'virtual-dom/h';
import patch from 'virtual-dom/patch';
import { type LintBox, isBoxInScreen } from './Box';
import RenderBox from './RenderBox';
+import {
+ getLexicalRoot,
+ getMediumRoot,
+ getNotionRoot,
+ getQuillJsRoot,
+ getShredditComposerRoot,
+ getSlateRoot,
+ getTrixRoot,
+} from './editorUtils';
import lintKindColor from './lintKindColor';
/** A class that renders highlights to a page and nothing else. Uses a virtual DOM to minimize jitter. */
@@ -143,71 +152,6 @@ function getInitialContainingRect(el: HTMLElement): DOMRect | null {
return null;
}
-function findAncestor(
- el: HTMLElement,
- predicate: (el: HTMLElement) => boolean,
-): HTMLElement | null {
- let node = el.parentElement;
-
- while (node != null) {
- if (predicate(node)) {
- return node;
- }
-
- node = node.parentElement;
- }
-
- return null;
-}
-
-/** Determines if a given node is a child of a Lexical editor instance.
- * If so, returns the root node of that instance. */
-function getLexicalRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(
- el,
- (node: HTMLElement) => node.getAttribute('data-lexical-editor') == 'true',
- );
-}
-
-/** Determines if a given node is a child of a Trix editor instance.
- * If so, returns the root node of that instance. */
-function getTrixRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(el, (node: HTMLElement) => node.nodeName == 'TRIX-EDITOR');
-}
-
-/** Determines if a given node is a child of a Reddit composer instance.
- * If so, returns the root node of that instance. */
-function getShredditComposerRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(el, (node: HTMLElement) => node.nodeName == 'SHREDDIT-COMPOSER');
-}
-
-/** Determines if a given node is a child of a Quill.js editor instance.
- * If so, returns the root node of that instance. */
-function getQuillJsRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(el, (node: HTMLElement) => node.classList.contains('ql-container'));
-}
-
-/** Determines if a given node is a child of a Slate editor instance.
- * If so, returns the root node of that instance. */
-function getSlateRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(el, (node: HTMLElement) => node.getAttribute('data-slate-editor') == 'true');
-}
-
-/** Determines if a given node is a child of a Medium.com editor instance.
- * If so, returns the root node of that instance. */
-function getMediumRoot(el: HTMLElement): HTMLElement | null {
- return findAncestor(
- el,
- (node: HTMLElement) => node.nodeName == 'MAIN' && location.hostname == 'medium.com',
- );
-}
-
-/** Determines if a given node is a child of a Notion editor instance.
- * If so, returns the root node of that instance. */
-function getNotionRoot(el: HTMLElement): HTMLElement | null {
- return document.getElementById('notion-app');
-}
-
/**
* Determines whether a given element would form the containing block
* for a descendant with `position: fixed`, based on CSS transforms,
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index c1c3065d9..06b86fae7 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -62,6 +62,7 @@ async function enableDefaultDomains() {
'x.com',
'www.notion.so',
'hashnode.com',
+ 'www.slatejs.org',
];
for (const item of defaultEnabledDomains) {
diff --git a/packages/chrome-plugin/src/computeLintBoxes.ts b/packages/chrome-plugin/src/computeLintBoxes.ts
index fec45224e..e256a41ff 100644
--- a/packages/chrome-plugin/src/computeLintBoxes.ts
+++ b/packages/chrome-plugin/src/computeLintBoxes.ts
@@ -2,6 +2,7 @@ import { type IgnorableLintBox, type LintBox, domRectToBox, isBottomEdgeInBox }
import ProtocolClient from './ProtocolClient';
import TextFieldRange from './TextFieldRange';
import { getRangeForTextSpan } from './domUtils';
+import { getSlateRoot } from './editorUtils';
import { type UnpackedLint, type UnpackedSuggestion, applySuggestion } from './unpackLint';
function isFormEl(el: HTMLElement): el is HTMLTextAreaElement | HTMLInputElement {
@@ -66,15 +67,65 @@ export default function computeLintBoxes(el: HTMLElement, lint: UnpackedLint): I
}
function replaceValue(el: HTMLElement, value: string) {
+ const slateRoot = getSlateRoot(el);
+
if (isFormEl(el)) {
el.value = value;
+ } else if (slateRoot != null) {
+ replaceValueSlate(el, value);
} else {
el.textContent = value;
el.dispatchEvent(new InputEvent('beforeinput', { bubbles: true, data: value }));
el.dispatchEvent(new InputEvent('input', { bubbles: true }));
- console.log(document.contains(el));
}
el.dispatchEvent(new Event('change', { bubbles: true }));
}
+
+/** Replace the content of a Slate editor node. */
+function replaceValueSlate(el: HTMLElement, value: string) {
+ slateSelectAllText(el);
+ slateInsertText(el, value);
+}
+
+function slateSelectAllText(target: Node): Range {
+ const range = target.ownerDocument!.createRange();
+ if (target.nodeType === Node.TEXT_NODE) {
+ const len = (target as Text).data.length;
+ range.setStart(target, 0);
+ range.setEnd(target, len);
+ } else {
+ range.selectNodeContents(target);
+ }
+ const sel = target.ownerDocument!.defaultView!.getSelection();
+ sel?.removeAllRanges();
+ sel?.addRange(range);
+ return range;
+}
+
+function slateInsertText(el: HTMLElement, raw: string): void {
+ const inputType = 'insertText';
+
+ const evInit: InputEventInit = {
+ bubbles: true,
+ cancelable: true,
+ inputType,
+ data: raw,
+ };
+
+ if ('StaticRange' in self && 'getTargetRanges' in InputEvent.prototype) {
+ const sel = el.ownerDocument.defaultView!.getSelection();
+ if (sel?.rangeCount) evInit.targetRanges = [new StaticRange(sel.getRangeAt(0))];
+ }
+
+ const beforeEvt = new InputEvent('beforeinput', evInit);
+ const biSuccess: boolean = el.dispatchEvent(beforeEvt);
+
+ const textEvt = new InputEvent('textInput', evInit);
+ const teSuccess = el.dispatchEvent(textEvt);
+
+ if (biSuccess && teSuccess) {
+ el.ownerDocument.execCommand(inputType, false, raw);
+ }
+}
diff --git a/packages/chrome-plugin/src/editorUtils.ts b/packages/chrome-plugin/src/editorUtils.ts
new file mode 100644
index 000000000..1485a7c10
--- /dev/null
+++ b/packages/chrome-plugin/src/editorUtils.ts
@@ -0,0 +1,64 @@
+export function findAncestor(
+ el: HTMLElement,
+ predicate: (el: HTMLElement) => boolean,
+): HTMLElement | null {
+ let node = el.parentElement;
+
+ while (node != null) {
+ if (predicate(node)) {
+ return node;
+ }
+
+ node = node.parentElement;
+ }
+
+ return null;
+}
+
+/** Determines if a given node is a child of a Lexical editor instance.
+ * If so, returns the root node of that instance. */
+export function getLexicalRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(
+ el,
+ (node: HTMLElement) => node.getAttribute('data-lexical-editor') == 'true',
+ );
+}
+
+/** Determines if a given node is a child of a Slate.js editor instance.
+ * If so, returns the root node of that instance. */
+export function getSlateRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(el, (node: HTMLElement) => node.getAttribute('data-slate-editor') == 'true');
+}
+
+/** Determines if a given node is a child of a Trix editor instance.
+ * If so, returns the root node of that instance. */
+export function getTrixRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(el, (node: HTMLElement) => node.nodeName == 'TRIX-EDITOR');
+}
+
+/** Determines if a given node is a child of a Reddit composer instance.
+ * If so, returns the root node of that instance. */
+export function getShredditComposerRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(el, (node: HTMLElement) => node.nodeName == 'SHREDDIT-COMPOSER');
+}
+
+/** Determines if a given node is a child of a Quill.js editor instance.
+ * If so, returns the root node of that instance. */
+export function getQuillJsRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(el, (node: HTMLElement) => node.classList.contains('ql-container'));
+}
+
+/** Determines if a given node is a child of a Medium.com editor instance.
+ * If so, returns the root node of that instance. */
+export function getMediumRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(
+ el,
+ (node: HTMLElement) => node.nodeName == 'MAIN' && location.hostname == 'medium.com',
+ );
+}
+
+/** Determines if a given node is a child of a Notion editor instance.
+ * If so, returns the root node of that instance. */
+export function getNotionRoot(el: HTMLElement): HTMLElement | null {
+ return document.getElementById('notion-app');
+}