Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from "react"; import { observer } from "mobx-react";
import { useState } from "react";
import { observer } from "mobx-react";
import { AlertTriangle } from "lucide-react";
// ui
import { useTranslation } from "@plane/i18n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@floating-ui/react";
import type { Editor } from "@tiptap/core";
import { Ellipsis } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
// plane imports
import { cn } from "@plane/utils";
// constants
Expand Down Expand Up @@ -49,6 +49,25 @@ export function ColumnDragHandle(props: ColumnDragHandleProps) {
const { col, editor } = props;
// states
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
// Track active event listeners for cleanup
const activeListenersRef = useRef<{
mouseup?: (e: MouseEvent) => void;
mousemove?: (e: MouseEvent) => void;
}>({});

// Cleanup window event listeners on unmount
useEffect(() => {
const listenersRef = activeListenersRef.current;
return () => {
// Remove any lingering window event listeners when component unmounts
if (listenersRef.mouseup) {
window.removeEventListener("mouseup", listenersRef.mouseup);
}
if (listenersRef.mousemove) {
window.removeEventListener("mousemove", listenersRef.mousemove);
}
};
}, []);
// floating ui
const { refs, floatingStyles, context } = useFloating({
placement: "bottom-start",
Expand Down Expand Up @@ -94,6 +113,17 @@ export function ColumnDragHandle(props: ColumnDragHandleProps) {
e.stopPropagation();
e.preventDefault();

// Prevent multiple simultaneous drag operations
// If there are already listeners attached, remove them first
if (activeListenersRef.current.mouseup) {
window.removeEventListener("mouseup", activeListenersRef.current.mouseup);
}
if (activeListenersRef.current.mousemove) {
window.removeEventListener("mousemove", activeListenersRef.current.mousemove);
}
activeListenersRef.current.mouseup = undefined;
activeListenersRef.current.mousemove = undefined;

const table = findTable(editor.state.selection);
if (!table) return;

Expand Down Expand Up @@ -133,6 +163,9 @@ export function ColumnDragHandle(props: ColumnDragHandleProps) {
}
window.removeEventListener("mouseup", handleFinish);
window.removeEventListener("mousemove", handleMove);
// Clear the ref
activeListenersRef.current.mouseup = undefined;
activeListenersRef.current.mousemove = undefined;
};

let pseudoColumn: HTMLElement | undefined;
Expand Down Expand Up @@ -169,6 +202,9 @@ export function ColumnDragHandle(props: ColumnDragHandleProps) {
};

try {
// Store references for cleanup
activeListenersRef.current.mouseup = handleFinish;
activeListenersRef.current.mousemove = handleMove;
window.addEventListener("mouseup", handleFinish);
window.addEventListener("mousemove", handleMove);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type TableColumnDragHandlePluginState = {
// track table structure to detect changes
tableWidth?: number;
tableNodePos?: number;
// track renderers for cleanup
renderers?: ReactRenderer[];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renderers not destroyed when navigating away from table

When a user navigates away from a table (so findTable returns undefined), the apply function returns {} without destroying the renderers tracked in prev.renderers. This causes a memory leak because the ReactRenderer instances and their associated event listeners are never cleaned up. The PR adds renderer tracking for cleanup but misses this code path where prev has renderers that need destruction before returning an empty state.

Additional Locations (1)

Fix in Cursor Fix in Web

};

const TABLE_COLUMN_DRAG_HANDLE_PLUGIN_KEY = new PluginKey("tableColumnHandlerDecorationPlugin");
Expand Down Expand Up @@ -58,11 +60,22 @@ export const TableColumnDragHandlePlugin = (editor: Editor): Plugin<TableColumnD
decorations: mapped,
tableWidth: tableMap.width,
tableNodePos: table.pos,
renderers: prev.renderers,
};
}

// Clean up old renderers before creating new ones
prev.renderers?.forEach((renderer) => {
try {
renderer.destroy();
} catch (error) {
console.error("Error destroying renderer:", error);
}
});

// recreate all decorations
const decorations: Decoration[] = [];
const renderers: ReactRenderer[] = [];

for (let col = 0; col < tableMap.width; col++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, col);
Expand All @@ -75,19 +88,35 @@ export const TableColumnDragHandlePlugin = (editor: Editor): Plugin<TableColumnD
editor,
});

renderers.push(dragHandleComponent);
decorations.push(Decoration.widget(pos, () => dragHandleComponent.element));
}

return {
decorations: DecorationSet.create(newState.doc, decorations),
tableWidth: tableMap.width,
tableNodePos: table.pos,
renderers,
};
},
},
props: {
decorations(state) {
return TABLE_COLUMN_DRAG_HANDLE_PLUGIN_KEY.getState(state).decorations;
return (TABLE_COLUMN_DRAG_HANDLE_PLUGIN_KEY.getState(state) as TableColumnDragHandlePluginState | undefined)
?.decorations;
},
},
destroy() {
// Clean up all renderers when plugin is destroyed
const state =
editor.state &&
(TABLE_COLUMN_DRAG_HANDLE_PLUGIN_KEY.getState(editor.state) as TableColumnDragHandlePluginState | undefined);
state?.renderers?.forEach((renderer: ReactRenderer) => {
try {
renderer.destroy();
} catch (error) {
console.error("Error destroying renderer:", error);
}
});
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@floating-ui/react";
import type { Editor } from "@tiptap/core";
import { Ellipsis } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
// plane imports
import { cn } from "@plane/utils";
// constants
Expand Down Expand Up @@ -49,6 +49,25 @@ export function RowDragHandle(props: RowDragHandleProps) {
const { editor, row } = props;
// states
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
// Track active event listeners for cleanup
const activeListenersRef = useRef<{
mouseup?: (e: MouseEvent) => void;
mousemove?: (e: MouseEvent) => void;
}>({});

// Cleanup window event listeners on unmount
useEffect(() => {
const listenersRef = activeListenersRef.current;
return () => {
// Remove any lingering window event listeners when component unmounts
if (listenersRef.mouseup) {
window.removeEventListener("mouseup", listenersRef.mouseup);
}
if (listenersRef.mousemove) {
window.removeEventListener("mousemove", listenersRef.mousemove);
}
};
}, []);
// floating ui
const { refs, floatingStyles, context } = useFloating({
placement: "bottom-start",
Expand Down Expand Up @@ -94,6 +113,17 @@ export function RowDragHandle(props: RowDragHandleProps) {
e.stopPropagation();
e.preventDefault();

// Prevent multiple simultaneous drag operations
// If there are already listeners attached, remove them first
if (activeListenersRef.current.mouseup) {
window.removeEventListener("mouseup", activeListenersRef.current.mouseup);
}
if (activeListenersRef.current.mousemove) {
window.removeEventListener("mousemove", activeListenersRef.current.mousemove);
}
activeListenersRef.current.mouseup = undefined;
activeListenersRef.current.mousemove = undefined;

const table = findTable(editor.state.selection);
if (!table) return;

Expand Down Expand Up @@ -133,6 +163,9 @@ export function RowDragHandle(props: RowDragHandleProps) {
}
window.removeEventListener("mouseup", handleFinish);
window.removeEventListener("mousemove", handleMove);
// Clear the ref
activeListenersRef.current.mouseup = undefined;
activeListenersRef.current.mousemove = undefined;
};

let pseudoRow: HTMLElement | undefined;
Expand Down Expand Up @@ -168,6 +201,9 @@ export function RowDragHandle(props: RowDragHandleProps) {
};

try {
// Store references for cleanup
activeListenersRef.current.mouseup = handleFinish;
activeListenersRef.current.mousemove = handleMove;
window.addEventListener("mouseup", handleFinish);
window.addEventListener("mousemove", handleMove);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type TableRowDragHandlePluginState = {
// track table structure to detect changes
tableHeight?: number;
tableNodePos?: number;
// track renderers for cleanup
renderers?: ReactRenderer[];
};

const TABLE_ROW_DRAG_HANDLE_PLUGIN_KEY = new PluginKey("tableRowDragHandlePlugin");
Expand Down Expand Up @@ -58,11 +60,22 @@ export const TableRowDragHandlePlugin = (editor: Editor): Plugin<TableRowDragHan
decorations: mapped,
tableHeight: tableMap.height,
tableNodePos: table.pos,
renderers: prev.renderers,
};
}

// Clean up old renderers before creating new ones
prev.renderers?.forEach((renderer) => {
try {
renderer.destroy();
} catch (error) {
console.error("Error destroying renderer:", error);
}
});

// recreate all decorations
const decorations: Decoration[] = [];
const renderers: ReactRenderer[] = [];

for (let row = 0; row < tableMap.height; row++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, row * tableMap.width);
Expand All @@ -75,19 +88,35 @@ export const TableRowDragHandlePlugin = (editor: Editor): Plugin<TableRowDragHan
editor,
});

renderers.push(dragHandleComponent);
decorations.push(Decoration.widget(pos, () => dragHandleComponent.element));
}

return {
decorations: DecorationSet.create(newState.doc, decorations),
tableHeight: tableMap.height,
tableNodePos: table.pos,
renderers,
};
},
},
props: {
decorations(state) {
return TABLE_ROW_DRAG_HANDLE_PLUGIN_KEY.getState(state).decorations;
return (TABLE_ROW_DRAG_HANDLE_PLUGIN_KEY.getState(state) as TableRowDragHandlePluginState | undefined)
?.decorations;
},
},
destroy() {
// Clean up all renderers when plugin is destroyed
const state =
editor.state &&
(TABLE_ROW_DRAG_HANDLE_PLUGIN_KEY.getState(editor.state) as TableRowDragHandlePluginState | undefined);
state?.renderers?.forEach((renderer: ReactRenderer) => {
try {
renderer.destroy();
} catch (error) {
console.error("Error destroying renderer:", error);
}
});
},
});
Loading