Weird behavior of Column drag in prosemirror-tables

Hi @marijn How to resolve this weird behavior of the column dragging?

I have modified the schema and other part of pm-tables such that now my table has an attr called columnsWidth: number , due to performance issues.

function handleMouseDown(
    view: EditorView,
    event: MouseEvent,
    cellMinWidth: number,
    defaultCellMinWidth: number,
): boolean {
    const win = view.dom.ownerDocument.defaultView ?? window;

    const pluginState = columnResizingPluginKey.getState(view.state);
    if (!pluginState || pluginState.activeHandle == -1 || pluginState.dragging) return false;

    const $cell = view.state.doc.resolve(pluginState.activeHandle);
    const table = $cell.node(-1);

    const width = currentColWidth(view, pluginState.activeHandle, table.attrs.columnsWidth);
    view.dispatch(
        view.state.tr.setMeta(columnResizingPluginKey, {
            setDragging: {startX: event.clientX, startWidth: width},
        }),
    );

    // Finalizes the resizing process when the mouse is released
    function finish(event: MouseEvent) {
        win.removeEventListener("mouseup", finish);
        win.removeEventListener("mousemove", move);
        const pluginState = columnResizingPluginKey.getState(view.state);
        if (pluginState?.dragging) {
            updateColumnWidth(
                view,
                pluginState.activeHandle,
                draggedWidth(pluginState.dragging, event, cellMinWidth),
            );
            view.dispatch(view.state.tr.setMeta(columnResizingPluginKey, {setDragging: null}));
        }
    }

    // Updates the column width as the mouse is moved while dragging
    function move(event: MouseEvent): void {
        if (!event.which) return finish(event);
        const pluginState = columnResizingPluginKey.getState(view.state);
        if (!pluginState) return;
        console.log("move", pluginState);
        if (pluginState.dragging) {
            const dragged = draggedWidth(pluginState.dragging, event, cellMinWidth);
            displayColumnWidth(view, pluginState.activeHandle, dragged, defaultCellMinWidth);
        }
    }

    displayColumnWidth(view, pluginState.activeHandle, width, defaultCellMinWidth);

    win.addEventListener("mouseup", finish);
    win.addEventListener("mousemove", move);
    event.preventDefault();
    return true;
}

CleanShot 2024-12-09 at 19.38.18

I don’t maintain prosemirror-tables, and I’m definitely not available to debug issues caused by your own modifications of the library.

1 Like

I’m not sure what went wrong with your code. Your code snippet doesn’t container enough information (the first one I noticed is your didn’t provide your own currentColWidth() implementation`).

It seems that you modified the lib a lot to address “performance issues”, but you didn’t mention what’s exactly the issues with the current prosemirror-tables lib.

1 Like