mirror of
https://github.com/block/buzz.git
synced 2026-11-29 04:32:08 +00:00
fix(composer): guard formatting tray before editor mount
Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz> Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
This commit is contained in:
@@ -161,7 +161,9 @@ export function SelectionFormattingTray({
|
||||
return;
|
||||
}
|
||||
|
||||
const editorDom = editor.view.dom;
|
||||
let disposed = false;
|
||||
let attachFrame: number | null = null;
|
||||
let editorDom: HTMLElement | null = null;
|
||||
const hide = () => setPosition(null);
|
||||
const handleContextMenu = () => {
|
||||
suppressRightClickUpdatesRef.current = true;
|
||||
@@ -175,27 +177,42 @@ export function SelectionFormattingTray({
|
||||
const handlePointerDown = (event: PointerEvent) => {
|
||||
if (event.button === 0) clearSuppression();
|
||||
};
|
||||
const attachToEditorView = () => {
|
||||
if (disposed || editor.isDestroyed) return;
|
||||
try {
|
||||
editorDom = editor.view.dom;
|
||||
} catch {
|
||||
// Tiptap creates the Editor before EditorContent mounts its view.
|
||||
// A sibling state update can run this effect inside that brief gap.
|
||||
attachFrame = window.requestAnimationFrame(attachToEditorView);
|
||||
return;
|
||||
}
|
||||
|
||||
scheduleUpdate();
|
||||
editor.on("selectionUpdate", scheduleUpdate);
|
||||
editor.on("transaction", scheduleUpdate);
|
||||
editor.on("focus", scheduleUpdate);
|
||||
editor.on("blur", hide);
|
||||
editorDom.addEventListener("contextmenu", handleContextMenu);
|
||||
editorDom.addEventListener("pointerdown", handlePointerDown);
|
||||
editorDom.addEventListener("keydown", clearSuppression);
|
||||
window.addEventListener("resize", scheduleUpdate);
|
||||
window.addEventListener("scroll", scheduleUpdate, true);
|
||||
scheduleUpdate();
|
||||
editor.on("selectionUpdate", scheduleUpdate);
|
||||
editor.on("transaction", scheduleUpdate);
|
||||
editor.on("focus", scheduleUpdate);
|
||||
editor.on("blur", hide);
|
||||
editorDom.addEventListener("contextmenu", handleContextMenu);
|
||||
editorDom.addEventListener("pointerdown", handlePointerDown);
|
||||
editorDom.addEventListener("keydown", clearSuppression);
|
||||
window.addEventListener("resize", scheduleUpdate);
|
||||
window.addEventListener("scroll", scheduleUpdate, true);
|
||||
};
|
||||
|
||||
attachToEditorView();
|
||||
|
||||
return () => {
|
||||
disposed = true;
|
||||
if (attachFrame !== null) window.cancelAnimationFrame(attachFrame);
|
||||
cancelScheduledUpdate();
|
||||
editor.off("selectionUpdate", scheduleUpdate);
|
||||
editor.off("transaction", scheduleUpdate);
|
||||
editor.off("focus", scheduleUpdate);
|
||||
editor.off("blur", hide);
|
||||
editorDom.removeEventListener("contextmenu", handleContextMenu);
|
||||
editorDom.removeEventListener("pointerdown", handlePointerDown);
|
||||
editorDom.removeEventListener("keydown", clearSuppression);
|
||||
editorDom?.removeEventListener("contextmenu", handleContextMenu);
|
||||
editorDom?.removeEventListener("pointerdown", handlePointerDown);
|
||||
editorDom?.removeEventListener("keydown", clearSuppression);
|
||||
window.removeEventListener("resize", scheduleUpdate);
|
||||
window.removeEventListener("scroll", scheduleUpdate, true);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user