-
Notifications
You must be signed in to change notification settings - Fork 178
feat: support rectangular selection when selecting a merged cell #271
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,13 @@ import { | |
|
|
||
| type Axis = 'horiz' | 'vert'; | ||
|
|
||
| interface Rect { | ||
| left: number; | ||
| top: number; | ||
| right: number; | ||
| bottom: number; | ||
| } | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
|
|
@@ -127,6 +134,68 @@ export function handleTripleClick(view: EditorView, pos: number): boolean { | |
| return true; | ||
| } | ||
|
|
||
| // judge rect is rectangle | ||
| export function judgeRectangle(tableMap: TableMap, rect: Rect) { | ||
| const { width, map, height } = tableMap; | ||
| const mergedCellsIndices = []; | ||
| let indexTop = rect.top * width + rect.left; | ||
| let indexLeft = indexTop; | ||
| let indexBottom = (rect.bottom - 1) * width + rect.left; | ||
| let indexRight = indexTop + (rect.right - rect.left - 1); | ||
| for (let i = rect.top; i < rect.bottom; i++) { | ||
| if ( | ||
| (rect.left > 0 && map[indexLeft] === map[indexLeft - 1]) || | ||
| (rect.right < width && map[indexRight] === map[indexRight + 1]) | ||
| ) { | ||
| if (map[indexLeft] === map[indexLeft - 1]) | ||
| mergedCellsIndices.push(indexLeft - 1); | ||
| if (map[indexRight] === map[indexRight + 1]) | ||
| mergedCellsIndices.push(indexRight + 1); | ||
| } | ||
| indexLeft += width; | ||
| indexRight += width; | ||
| } | ||
| for (let i = rect.left; i < rect.right; i++) { | ||
| if ( | ||
| (rect.top > 0 && map[indexTop] === map[indexTop - width]) || | ||
| (rect.bottom < height && map[indexBottom] === map[indexBottom + width]) | ||
| ) { | ||
| if (map[indexTop] === map[indexTop - width]) | ||
| mergedCellsIndices.push(indexTop - width); | ||
| if (map[indexBottom] === map[indexBottom + width]) | ||
| mergedCellsIndices.push(indexBottom + width); | ||
| } | ||
| indexTop++; | ||
| indexBottom++; | ||
| } | ||
| return Array.from(new Set(mergedCellsIndices)); | ||
| } | ||
|
|
||
| // get rectangular | ||
| export function getRectangularRect(rect: Rect, tableMap: TableMap) { | ||
| let mergedCellsIndices = []; | ||
| const rectangle = JSON.parse(JSON.stringify(rect)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's copy the properties directly. |
||
| while ((mergedCellsIndices = judgeRectangle(tableMap, rectangle)).length) { | ||
| let maxRow = 0, | ||
| minRow = Infinity, | ||
| maxCol = 0, | ||
| minCol = Infinity; | ||
| mergedCellsIndices.forEach((index: number) => { | ||
| const rowIndex = Math.floor(index / tableMap.width); | ||
| const colIndex = index % tableMap.width; | ||
| maxRow = Math.max(rowIndex, rectangle.bottom - 1, maxRow); | ||
| minRow = Math.min(rowIndex, rectangle.top, minRow); | ||
| maxCol = Math.max(colIndex, rectangle.right - 1, maxCol); | ||
| minCol = Math.min(colIndex, rectangle.left, minCol); | ||
| }); | ||
| rectangle.left = minCol; | ||
| rectangle.right = maxCol + 1; | ||
| rectangle.top = minRow; | ||
| rectangle.bottom = maxRow + 1; | ||
| } | ||
| return rectangle; | ||
| } | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
|
|
@@ -177,6 +246,7 @@ export function handlePaste( | |
| export function handleMouseDown( | ||
| view: EditorView, | ||
| startEvent: MouseEvent, | ||
| supportRectangularSelection?: boolean, | ||
| ): void { | ||
| if (startEvent.ctrlKey || startEvent.metaKey) return; | ||
|
|
||
|
|
@@ -210,14 +280,37 @@ export function handleMouseDown( | |
| if (starting) $head = $anchor; | ||
| else return; | ||
| } | ||
| const selection = new CellSelection($anchor, $head); | ||
| const selection = supportRectangularSelection | ||
| ? getRectangularSelection($anchor, $head) | ||
| : new CellSelection($anchor, $head); | ||
|
|
||
| if (starting || !view.state.selection.eq(selection)) { | ||
| const tr = view.state.tr.setSelection(selection); | ||
| if (starting) tr.setMeta(tableEditingKey, $anchor.pos); | ||
| view.dispatch(tr); | ||
| } | ||
| } | ||
|
|
||
| // get Rectangular cell Selection | ||
| function getRectangularSelection($anchor: ResolvedPos, $head: ResolvedPos) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this function out of |
||
| const tableNode = $anchor.node(-1); | ||
| const tableMap = TableMap.get(tableNode); | ||
| const tableStart = $anchor.start(-1); | ||
| const rect = tableMap.rectBetween( | ||
| $anchor.pos - tableStart, | ||
| $head.pos - tableStart, | ||
| ); | ||
| const rectangle = getRectangularRect(rect, tableMap); | ||
| const { left, right, top, bottom } = rectangle; | ||
| const { map, width } = tableMap; | ||
| const { tr } = view.state; | ||
| const $anchorCell = tr.doc.resolve(map[top * width + left] + tableStart); | ||
| const $headCell = tr.doc.resolve( | ||
| map[(bottom - 1) * width + right - 1] + tableStart, | ||
| ); | ||
|
Comment on lines
+306
to
+310
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you don't need |
||
| return new CellSelection($anchorCell, $headCell); | ||
| } | ||
|
|
||
| // Stop listening to mouse motion events. | ||
| function stop(): void { | ||
| view.root.removeEventListener('mouseup', stop); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just
import type {Rect} from './tablemap'.