Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(editor): preserve table widths when inserting beside selected cells (#35162)

OrcaWina0b8181bf3d8
diff --git a/src/renderer/src/components/editor/rich-markdown-table-actions.test.ts b/src/renderer/src/components/editor/rich-markdown-table-actions.test.ts
index b0e3dfb58ff..0ffab112d91 100644
--- a/src/renderer/src/components/editor/rich-markdown-table-actions.test.ts
+++ b/src/renderer/src/components/editor/rich-markdown-table-actions.test.ts
@@ -164,6 +164,36 @@ describe('rich markdown table actions', () => {
}
})
+ it.each([
+ ['insert-column-left', 'b2', [100, 133, 67]],
+ ['insert-column-right', 'a1', [133, 67, 100]]
+ ] as const)(
+ 'rebalances %s at the selected rectangle edge, not clicked cell %s',
+ (action, clickedCell, expectedWidths) => {
+ const editor = createEditor()
+ try {
+ setFirstRowColumnWidths(editor, [200, 100])
+ editor.view.dispatch(
+ editor.state.tr.setSelection(
+ CellSelection.create(
+ editor.state.doc,
+ cellAtText(editor, 'a1'),
+ cellAtText(editor, 'b2')
+ )
+ )
+ )
+ expect(
+ runRichMarkdownTableAction(editor, action, {
+ cellPosition: cellAtText(editor, clickedCell)
+ })
+ ).toBe(true)
+ expect(firstRowColumnWidths(editor)).toEqual(expectedWidths)
+ } finally {
+ editor.destroy()
+ }
+ }
+ )
+
it('does not throw when a cached cell position outlives the document', () => {
const editor = createEditor()
try {
diff --git a/src/renderer/src/components/editor/rich-markdown-table-actions.ts b/src/renderer/src/components/editor/rich-markdown-table-actions.ts
index 57e7715f0ad..60bb25c8799 100644
--- a/src/renderer/src/components/editor/rich-markdown-table-actions.ts
+++ b/src/renderer/src/components/editor/rich-markdown-table-actions.ts
@@ -1,6 +1,6 @@
import type { Editor } from '@tiptap/react'
import { TextSelection, type Transaction } from '@tiptap/pm/state'
-import { CellSelection, selectionCell, TableMap } from '@tiptap/pm/tables'
+import { CellSelection, selectedRect, selectionCell, TableMap } from '@tiptap/pm/tables'
export type RichMarkdownTableAction =
| 'insert-row-above'
@@ -152,12 +152,6 @@ function tableContext(editor: Editor, targetCellPosition: number): TableContext
}
}
-function columnIndexAtCellPosition(editor: Editor, cellPosition: number): number | null {
- const $cell = editor.state.doc.resolve(cellPosition)
- const role = $cell.nodeAfter?.type.spec.tableRole
- return role === 'cell' || role === 'header_cell' ? $cell.index($cell.depth) : null
-}
-
function selectedTableCoverage(
editor: Editor,
context: TableContext
@@ -250,7 +244,6 @@ export function runRichMarkdownTableAction(
if (!context) {
return false
}
- const targetColumnIndex = columnIndexAtCellPosition(editor, targetCellPosition)
const coverage = selectedTableCoverage(editor, context)
const chain = editor.chain().focus()
@@ -270,28 +263,26 @@ export function runRichMarkdownTableAction(
return false
}
return chain.deleteRow().run()
- case 'insert-column-left':
- if (targetColumnIndex === null) {
- return false
- }
+ case 'insert-column-left': {
+ const insertedColumnIndex = selectedRect(editor.state).left
return chain
.addColumnBefore()
.command(({ tr }) => {
- rebalanceAddedColumn(tr, context.tablePosition, targetColumnIndex)
+ rebalanceAddedColumn(tr, context.tablePosition, insertedColumnIndex)
return true
})
.run()
- case 'insert-column-right':
- if (targetColumnIndex === null) {
- return false
- }
+ }
+ case 'insert-column-right': {
+ const insertedColumnIndex = selectedRect(editor.state).right
return chain
.addColumnAfter()
.command(({ tr }) => {
- rebalanceAddedColumn(tr, context.tablePosition, targetColumnIndex + 1)
+ rebalanceAddedColumn(tr, context.tablePosition, insertedColumnIndex)
return true
})
.run()
+ }
case 'delete-column':
if (coverage.columns.size >= context.columnCount) {
return chain.deleteTable().run()