Skip to content

Commit a63e6ff

Browse files
authored
fix: database block can not get editorDom and scrollContainer (#36)
1 parent 8745104 commit a63e6ff

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/components/editor/components/blocks/database/DatabaseBlock.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ export const DatabaseBlock = memo(
2727
const [doc, setDoc] = useState<YDoc | null>(null);
2828

2929
useEffect(() => {
30-
if (!viewId) return;
31-
void (async () => {
30+
if(!viewId) return;
31+
void (async() => {
3232
try {
3333
const view = await loadView?.(viewId);
3434

35-
if (!view) {
35+
if(!view) {
3636
throw new Error('View not found');
3737
}
3838

3939
setDoc(view);
40-
} catch (e) {
40+
} catch(e) {
4141
setNotFound(true);
4242
}
4343
})();
@@ -48,16 +48,16 @@ export const DatabaseBlock = memo(
4848
const [iidName, setIidName] = useState<string>('');
4949

5050
useEffect(() => {
51-
const updateVisibleViewIds = async (meta: View | null) => {
52-
if (!meta) {
51+
const updateVisibleViewIds = async(meta: View | null) => {
52+
if(!meta) {
5353
return;
5454
}
5555

5656
const viewIds = meta.children.map((v) => v.view_id) || [];
5757

5858
viewIds.unshift(meta.view_id);
5959

60-
if (!viewIds.includes(viewId)) {
60+
if(!viewIds.includes(viewId)) {
6161
setSelectedViewId(viewIds[0]);
6262
} else {
6363
setSelectedViewId(viewId);
@@ -67,22 +67,22 @@ export const DatabaseBlock = memo(
6767
setVisibleViewIds(viewIds);
6868
};
6969

70-
void (async () => {
70+
void (async() => {
7171
try {
7272
const meta = await loadViewMeta?.(viewId, updateVisibleViewIds);
7373

74-
if (meta) {
74+
if(meta) {
7575
await updateVisibleViewIds(meta);
7676
}
77-
} catch (e) {
77+
} catch(e) {
7878
setNotFound(true);
7979
}
8080
})();
8181
}, [loadViewMeta, viewId]);
8282

8383
const handleNavigateToRow = useCallback(
84-
async (rowId: string) => {
85-
if (!viewId) return;
84+
async(rowId: string) => {
85+
if(!viewId) return;
8686
await navigateToView?.(viewId, rowId);
8787
},
8888
[navigateToView, viewId],
@@ -96,17 +96,17 @@ export const DatabaseBlock = memo(
9696

9797
return database?.get(YjsDatabaseKey.views)?.get(selectedViewId);
9898
}, [doc, selectedViewId]);
99-
const handleRendered = useCallback(async (height: number) => {
99+
const handleRendered = useCallback(async(height: number) => {
100100
const container = containerRef.current;
101101

102-
if (!container) return;
103-
if (height > 0) {
102+
if(!container) return;
103+
if(height > 0) {
104104
container.style.height = `${height}px`;
105105
}
106106

107107
const layout = Number(selectedView?.get(YjsDatabaseKey.layout));
108108

109-
if (layout !== DatabaseViewLayout.Calendar) {
109+
if(layout !== DatabaseViewLayout.Calendar) {
110110
container.style.maxHeight = '550px';
111111
}
112112

@@ -115,11 +115,15 @@ export const DatabaseBlock = memo(
115115
const [scrollLeft, setScrollLeft] = useState(0);
116116

117117
useEffect(() => {
118+
119+
const layout = Number(selectedView?.get(YjsDatabaseKey.layout));
118120
const editorDom = ReactEditor.toDOMNode(editor, editor);
119121
const scrollContainer = getScrollParent(editorDom) as HTMLElement;
120-
const layout = Number(selectedView?.get(YjsDatabaseKey.layout));
122+
123+
if(!editorDom || !scrollContainer) return;
121124

122125
const onResize = () => {
126+
123127
const scrollRect = scrollContainer.getBoundingClientRect();
124128

125129
setScrollLeft(Math.max(editorDom.getBoundingClientRect().left - scrollRect.left, layout === DatabaseViewLayout.Grid ? 64 : 0));
@@ -142,7 +146,7 @@ export const DatabaseBlock = memo(
142146
contentEditable={readOnly ? false : undefined}
143147
className={`relative w-full cursor-pointer`}
144148
onMouseEnter={() => {
145-
if (variant === UIVariant.App) {
149+
if(variant === UIVariant.App) {
146150

147151
setShowActions(true);
148152
}

0 commit comments

Comments
 (0)