From 4811fb9f4d3985ccc922d5823e8a092125dc65ea Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 May 2025 09:54:58 -0400 Subject: [PATCH] fix: Invisible JS error --- shared/editor/queries/table.ts | 81 ++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 19 deletions(-) diff --git a/shared/editor/queries/table.ts b/shared/editor/queries/table.ts index a6692795b5..7bad794269 100644 --- a/shared/editor/queries/table.ts +++ b/shared/editor/queries/table.ts @@ -6,6 +6,30 @@ import { selectedRect, } from "prosemirror-tables"; +/** + * Checks if the current selection is a column selection. + * @param state The editor state. + * @returns True if the selection is a column selection, false otherwise. + */ +export function isColSelection(state: EditorState): boolean { + if (state.selection instanceof CellSelection) { + return state.selection.isColSelection(); + } + return false; +} + +/** + * Checks if the current selection is a row selection. + * @param state The editor state. + * @returns True if the selection is a row selection, false otherwise. + */ +export function isRowSelection(state: EditorState): boolean { + if (state.selection instanceof CellSelection) { + return state.selection.isRowSelection(); + } + return false; +} + export function getColumnIndex(state: EditorState): number | undefined { if (state.selection instanceof CellSelection) { if (state.selection.isColSelection()) { @@ -62,13 +86,18 @@ export function getCellsInRow(index: number) { }; } +/** + * Check if a specific column is selected in the editor. + * + * @param state The editor state + * @param index The index of the column to check + * @returns Boolean indicating if the column is selected + */ export function isColumnSelected(index: number) { return (state: EditorState): boolean => { - if (state.selection instanceof CellSelection) { - if (state.selection.isColSelection()) { - const rect = selectedRect(state); - return rect.left <= index && rect.right > index; - } + if (isColSelection(state)) { + const rect = selectedRect(state); + return rect.left <= index && rect.right > index; } return false; @@ -106,28 +135,42 @@ export function isHeaderEnabled( return true; } +/** + * Check if a specific row is selected in the editor. + * + * @param state The editor state + * @param index The index of the row to check + * @returns Boolean indicating if the row is selected + */ export function isRowSelected(index: number) { return (state: EditorState): boolean => { - if (state.selection instanceof CellSelection) { - if (state.selection.isRowSelection()) { - const rect = selectedRect(state); - return rect.top <= index && rect.bottom > index; - } + if (isRowSelection(state)) { + const rect = selectedRect(state); + return rect.top <= index && rect.bottom > index; } return false; }; } +/** + * Check if an entire table is selected in the editor. + * + * @param state The editor state + * @returns Boolean indicating if the table is selected + */ export function isTableSelected(state: EditorState): boolean { - const rect = selectedRect(state); + if (state.selection instanceof CellSelection) { + const rect = selectedRect(state); - return ( - rect.top === 0 && - rect.left === 0 && - rect.bottom === rect.map.height && - rect.right === rect.map.width && - !state.selection.empty && - state.selection instanceof CellSelection - ); + return ( + rect.top === 0 && + rect.left === 0 && + rect.bottom === rect.map.height && + rect.right === rect.map.width && + !state.selection.empty + ); + } + + return false; }