-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[WIKI-491] [WIKI-496] [WIKI-499] refactor: tables width and selection UI #7274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a414ae2
refactor: tables width and selection UI
aaryan610 fdd4f80
fix: drag handle position
aaryan610 a0d6fa0
refactor: selection decorator logic
aaryan610 ec7cbd4
refactor: adjacent cells logic
aaryan610 167e53f
refactor: folder structure
aaryan610 13c7ac8
chore: default column width for new columns
aaryan610 e32807d
refactor: plugin location
aaryan610 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/editor/src/core/extensions/table/plugins/table-selection-outline/plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { findParentNode, type Editor } from "@tiptap/core"; | ||
| import { Plugin, PluginKey } from "@tiptap/pm/state"; | ||
| import { CellSelection, TableMap } from "@tiptap/pm/tables"; | ||
| import { Decoration, DecorationSet } from "@tiptap/pm/view"; | ||
| // local imports | ||
| import { getCellBorderClasses } from "./utils"; | ||
|
|
||
| type TableCellSelectionOutlinePluginState = { | ||
| decorations?: DecorationSet; | ||
| }; | ||
|
|
||
| const TABLE_SELECTION_OUTLINE_PLUGIN_KEY = new PluginKey("table-cell-selection-outline"); | ||
|
|
||
| export const TableCellSelectionOutlinePlugin = (editor: Editor): Plugin<TableCellSelectionOutlinePluginState> => | ||
| new Plugin<TableCellSelectionOutlinePluginState>({ | ||
| key: TABLE_SELECTION_OUTLINE_PLUGIN_KEY, | ||
| state: { | ||
| init: () => ({}), | ||
| apply(tr, prev, oldState, newState) { | ||
| if (!editor.isEditable) return {}; | ||
| const table = findParentNode((node) => node.type.spec.tableRole === "table")(newState.selection); | ||
| const hasDocChanged = tr.docChanged || !newState.selection.eq(oldState.selection); | ||
| if (!table || !hasDocChanged) { | ||
| return table === undefined ? {} : prev; | ||
| } | ||
|
|
||
| const { selection } = newState; | ||
| if (!(selection instanceof CellSelection)) return {}; | ||
|
|
||
| const decorations: Decoration[] = []; | ||
| const tableMap = TableMap.get(table.node); | ||
| const selectedCells: number[] = []; | ||
|
|
||
| // First, collect all selected cell positions | ||
| selection.forEachCell((_node, pos) => { | ||
| const start = pos - table.pos - 1; | ||
| selectedCells.push(start); | ||
| }); | ||
|
|
||
| // Then, add decorations with appropriate border classes | ||
| selection.forEachCell((node, pos) => { | ||
| const start = pos - table.pos - 1; | ||
| const classes = getCellBorderClasses(start, selectedCells, tableMap); | ||
|
|
||
| decorations.push(Decoration.node(pos, pos + node.nodeSize, { class: classes.join(" ") })); | ||
| }); | ||
|
|
||
| return { | ||
| decorations: DecorationSet.create(newState.doc, decorations), | ||
| }; | ||
| }, | ||
| }, | ||
| props: { | ||
| decorations(state) { | ||
| return TABLE_SELECTION_OUTLINE_PLUGIN_KEY.getState(state).decorations; | ||
| }, | ||
| }, | ||
| }); |
75 changes: 75 additions & 0 deletions
75
packages/editor/src/core/extensions/table/plugins/table-selection-outline/utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { TableMap } from "@tiptap/pm/tables"; | ||
|
|
||
| /** | ||
| * Calculates the positions of cells adjacent to a given cell in a table | ||
| * @param cellStart - The start position of the current cell in the document | ||
| * @param tableMap - ProseMirror's table mapping structure containing cell positions and dimensions | ||
| * @returns Object with positions of adjacent cells (undefined if cell doesn't exist at table edge) | ||
| */ | ||
| const getAdjacentCellPositions = ( | ||
| cellStart: number, | ||
| tableMap: TableMap | ||
| ): { top?: number; bottom?: number; left?: number; right?: number } => { | ||
| // Extract table dimensions | ||
| // width -> number of columns in the table | ||
| // height -> number of rows in the table | ||
| const { width, height } = tableMap; | ||
|
|
||
| // Find the index of our cell in the flat tableMap.map array | ||
| // tableMap.map contains start positions of all cells in row-by-row order | ||
| const cellIndex = tableMap.map.indexOf(cellStart); | ||
|
|
||
| // Safety check: if cell position not found in table map, return empty object | ||
| if (cellIndex === -1) return {}; | ||
|
|
||
| // Convert flat array index to 2D grid coordinates | ||
| // row = which row the cell is in (0-based from top) | ||
| // col = which column the cell is in (0-based from left) | ||
| const row = Math.floor(cellIndex / width); // Integer division gives row number | ||
| const col = cellIndex % width; // Remainder gives column number | ||
|
|
||
| return { | ||
| // Top cell: same column, one row up | ||
| // Check if we're not in the first row (row > 0) before calculating | ||
| top: row > 0 ? tableMap.map[(row - 1) * width + col] : undefined, | ||
|
|
||
| // Bottom cell: same column, one row down | ||
| // Check if we're not in the last row (row < height - 1) before calculating | ||
| bottom: row < height - 1 ? tableMap.map[(row + 1) * width + col] : undefined, | ||
|
|
||
| // Left cell: same row, one column left | ||
| // Check if we're not in the first column (col > 0) before calculating | ||
| left: col > 0 ? tableMap.map[row * width + (col - 1)] : undefined, | ||
|
|
||
| // Right cell: same row, one column right | ||
| // Check if we're not in the last column (col < width - 1) before calculating | ||
| right: col < width - 1 ? tableMap.map[row * width + (col + 1)] : undefined, | ||
| }; | ||
| }; | ||
|
|
||
| export const getCellBorderClasses = (cellStart: number, selectedCells: number[], tableMap: TableMap): string[] => { | ||
| const adjacent = getAdjacentCellPositions(cellStart, tableMap); | ||
| const classes: string[] = []; | ||
|
|
||
| // Add border-right if right cell is not selected or doesn't exist | ||
| if (adjacent.right === undefined || !selectedCells.includes(adjacent.right)) { | ||
| classes.push("selectedCell-border-right"); | ||
| } | ||
|
|
||
| // Add border-left if left cell is not selected or doesn't exist | ||
| if (adjacent.left === undefined || !selectedCells.includes(adjacent.left)) { | ||
| classes.push("selectedCell-border-left"); | ||
| } | ||
|
|
||
| // Add border-top if top cell is not selected or doesn't exist | ||
| if (adjacent.top === undefined || !selectedCells.includes(adjacent.top)) { | ||
| classes.push("selectedCell-border-top"); | ||
| } | ||
|
|
||
| // Add border-bottom if bottom cell is not selected or doesn't exist | ||
| if (adjacent.bottom === undefined || !selectedCells.includes(adjacent.bottom)) { | ||
| classes.push("selectedCell-border-bottom"); | ||
| } | ||
|
|
||
| return classes; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export { Table } from "./table"; | ||
|
|
||
| export const DEFAULT_COLUMN_WIDTH = 150; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.