Skip to content

Commit 6fc29db

Browse files
satorencursoragent
andcommitted
fix(TableHandles): guard stale block state after doc updates
Optional chaining on `this.state?.block.id` did not protect when `block` was undefined after `update()` refreshed a removed table block. Clear internal PluginView state instead of only hiding handles, and allow emitUpdate to sync undefined state to the extension store. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ec9c151 commit 6fc29db

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,9 @@ export class TableHandlesView implements PluginView {
163163
any
164164
>,
165165
private readonly pmView: EditorView,
166-
emitUpdate: (state: TableHandlesState) => void,
166+
emitUpdate: (state: TableHandlesState | undefined) => void,
167167
) {
168168
this.emitUpdate = () => {
169-
if (!this.state) {
170-
throw new Error("Attempting to update uninitialized image toolbar");
171-
}
172-
173169
emitUpdate(this.state);
174170
};
175171

@@ -298,7 +294,7 @@ export class TableHandlesView implements PluginView {
298294

299295
const hideHandles =
300296
// always hide handles when the actively hovered table changed
301-
this.state?.block.id !== tableBlock.id ||
297+
this.state?.block?.id !== tableBlock.id ||
302298
// make sure we don't hide existing handles (keep col / row index) when
303299
// we're hovering just above or to the right of a table
304300
event.clientX > tableRect.right ||
@@ -543,9 +539,9 @@ export class TableHandlesView implements PluginView {
543539
// because yjs replaces the element when for example you change the color via the side menu
544540
!this.tableElement?.isConnected
545541
) {
546-
this.state.show = false;
547-
this.state.showAddOrRemoveRowsButton = false;
548-
this.state.showAddOrRemoveColumnsButton = false;
542+
this.state = undefined;
543+
this.tableId = undefined;
544+
this.tableElement = undefined;
549545
this.emitUpdate();
550546

551547
return;
@@ -628,7 +624,7 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
628624
view: (editorView) => {
629625
view = new TableHandlesView(editor as any, editorView, (state) => {
630626
store.setState(
631-
state.block
627+
state?.block
632628
? {
633629
...state,
634630
draggingState: state.draggingState

0 commit comments

Comments
 (0)