Skip to content

Commit 0ff64ae

Browse files
committed
🐛(frontend) check tiptap editor in dom
When zooming in and out quickly, the editor instance may not be fully mounted, leading to errors when accessing its document. This commit adds checks to ensure the editor and its view are mounted before attempting to access the document, preventing potential runtime errors.
1 parent 90651a8 commit 0ff64ae

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useHeadings.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const useHeadings = (editor: DocsBlockNoteEditor) => {
77
const { setHeadings, resetHeadings } = useHeadingStore();
88

99
useEffect(() => {
10+
// Check if editor and its view are mounted before accessing document
11+
if (!editor || !editor._tiptapEditor?.view?.dom) {
12+
return;
13+
}
14+
1015
setHeadings(editor);
1116

1217
let timeoutId: NodeJS.Timeout;

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useShortcuts.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const useShortcuts = (
77
el: HTMLDivElement | null,
88
) => {
99
useEffect(() => {
10+
// Check if editor and its view are mounted
11+
if (!editor || !editor._tiptapEditor?.view?.dom || !el) {
12+
return;
13+
}
14+
1015
const handleKeyDown = (event: KeyboardEvent) => {
1116
if (event.key === '@' && editor?.isFocused()) {
1217
const selection = window.getSelection();
@@ -32,10 +37,6 @@ export const useShortcuts = (
3237
}
3338
};
3439

35-
if (!el) {
36-
return;
37-
}
38-
3940
el.addEventListener('keydown', handleKeyDown);
4041

4142
return () => {

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useUploadFile.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export const useUploadStatus = (editor: DocsBlockNoteEditor) => {
9595
);
9696

9797
useEffect(() => {
98+
// Check if editor and its view are mounted before accessing document
99+
if (!editor || !editor._tiptapEditor?.view?.dom) {
100+
return;
101+
}
102+
98103
const imagesBlocks = editor?.document.filter(
99104
(block) =>
100105
block.type === 'image' && block.props.url.includes(ANALYZE_URL),
@@ -110,6 +115,11 @@ export const useUploadStatus = (editor: DocsBlockNoteEditor) => {
110115
* block to show analyzing status
111116
*/
112117
useEffect(() => {
118+
// Check if editor and its view are mounted before setting up handlers
119+
if (!editor || !editor._tiptapEditor?.view?.dom) {
120+
return;
121+
}
122+
113123
editor.onUploadEnd((blockId) => {
114124
if (!blockId) {
115125
return;

src/frontend/apps/impress/src/features/docs/doc-editor/stores/useHeadingStore.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export interface UseHeadingStore {
2828
export const useHeadingStore = create<UseHeadingStore>((set, get) => ({
2929
headings: [],
3030
setHeadings: (editor) => {
31+
// Check if editor and its view are mounted before accessing document
32+
if (!editor || !editor._tiptapEditor?.view?.dom) {
33+
return;
34+
}
35+
3136
const headingBlocks = editor?.document
3237
.filter(
3338
(block) =>

0 commit comments

Comments
 (0)