Skip to content

Commit 12e11aa

Browse files
committed
fix: improve getBlock() guards with proper state cleanup and async safety
- SideMenu: hide menu and clear hoveredBlock when getBlock returns undefined, preventing stale visible state - EmbedTab: re-check block existence before each updateBlock call - UploadTab: re-check block existence after async upload resolves - TableHandles: use typed local variable instead of as any cast
1 parent 9dd7fd9 commit 12e11aa

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎packages/core/src/extensions/SideMenu/SideMenu.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ export class SideMenuView<
245245
this.hoveredBlock!.getAttribute("data-id")!,
246246
);
247247
if (!sideMenuBlock) {
248+
if (this.state?.show) {
249+
this.state.show = false;
250+
this.hoveredBlock = undefined;
251+
this.emitUpdate(this.state);
252+
}
248253
return;
249254
}
250255
this.state = {

‎packages/core/src/extensions/TableHandles/TableHandles.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,10 @@ export class TableHandlesView implements PluginView {
533533
}
534534

535535
// Hide handles if the table block has been removed.
536-
this.state.block = this.editor.getBlock(this.state.block.id) as any;
536+
const refreshedBlock = this.editor.getBlock(this.state.block.id);
537537
if (
538-
!this.state.block ||
539-
this.state.block.type !== "table" ||
538+
!refreshedBlock ||
539+
refreshedBlock.type !== "table" ||
540540
// when collaborating, the table element might be replaced and out of date
541541
// because yjs replaces the element when for example you change the color via the side menu
542542
!this.tableElement?.isConnected
@@ -548,6 +548,7 @@ export class TableHandlesView implements PluginView {
548548

549549
return;
550550
}
551+
this.state.block = refreshedBlock as typeof this.state.block;
551552

552553
const { height: rowCount, width: colCount } = getDimensionsOfTable(
553554
this.state.block,

‎packages/react/src/components/FilePanel/DefaultTabs/EmbedTab.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export const EmbedTab = <
4141
(event: KeyboardEvent) => {
4242
if (event.key === "Enter" && !event.nativeEvent.isComposing) {
4343
event.preventDefault();
44+
if (!editor.getBlock(props.blockId)) {
45+
return;
46+
}
4447
editor.updateBlock(props.blockId, {
4548
props: {
4649
name: filenameFromURL(currentURL),
@@ -53,6 +56,9 @@ export const EmbedTab = <
5356
);
5457

5558
const handleURLClick = useCallback(() => {
59+
if (!editor.getBlock(props.blockId)) {
60+
return;
61+
}
5662
editor.updateBlock(props.blockId, {
5763
props: {
5864
name: filenameFromURL(currentURL),

‎packages/react/src/components/FilePanel/DefaultTabs/UploadTab.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export const UploadTab = <
5353
if (editor.uploadFile !== undefined) {
5454
try {
5555
let updateData = await editor.uploadFile(file, props.blockId);
56+
if (!editor.getBlock(props.blockId)) {
57+
return;
58+
}
5659
if (typeof updateData === "string") {
5760
// received a url
5861
updateData = {

0 commit comments

Comments
 (0)