Skip to content

Commit 195930a

Browse files
committed
Fix(table): alignment is a ticked group, and a commit can't vanish (#485)
Two defects behind a report I could not reproduce: choosing a column alignment appearing to do nothing until it is chosen a second time. The alignment entries were a hidden toggle — choosing the alignment a column already had cleared it back to none. A column with no alignment renders identically to a left-aligned one, so on a fresh column that reads as: choose Align left, nothing changes; choose it again, still nothing, because the second choice quietly removed what the first one set. The three entries are now a ticked group with a fourth, Align default. A new column shows `Align default ✓`, choosing an alignment only ever sets it, and clearing is a deliberate pick rather than the side effect of choosing twice. The second defect is a silent drop. `commitTable` looks the table up in the syntax tree, and finding no Table node it returned — throwing the edit away with no feedback. Markdown parsing is incremental, so a table created or restructured moments earlier can still be outside the parsed tree, which makes this exactly a "that did nothing; do it again and it works" bug, and one whose likelihood depends on machine speed and document size rather than on anything the user can see. It now falls back to `tableBlockAt`, which reads the table block out of the document — the run of `|` rows around the position, plus a trailing `zen:cols` marker — and refuses a lone pipe line, since rewriting prose as a table would be a worse failure than none. Not claimed as a reproduction of the report. Driving the packaged app, the first selection applied every time: fresh `/table` table, keystrokes 60ms apart, and a 4000-paragraph note to starve the parser. What is fixed here is the behaviour that produces the described symptom, and the path by which an action can disappear without a trace. Verified in the app: first pick writes `| :------- |`, a second pick leaves it alone where it used to revert, and Align default clears it. Five tests cover the document-scan fallback. Reported by @uNyanda.
1 parent 13c9487 commit 195930a

3 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎packages/app-core/src/lib/cm-table-menu.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export function openTableContextMenu(req: TableMenuRequest): void {
131131
alignItem('Align left', 'left', col, model, apply, row),
132132
alignItem('Align center', 'center', col, model, apply, row),
133133
alignItem('Align right', 'right', col, model, apply, row),
134+
alignItem('Align default', 'none', col, model, apply, row),
134135
{ kind: 'sep' },
135136
{
136137
kind: 'item',
@@ -337,6 +338,18 @@ export function openTableContextMenu(req: TableMenuRequest): void {
337338
}
338339
}
339340

341+
/**
342+
* One entry of the alignment radio group, ticked when the column already has
343+
* it. Choosing an alignment sets it — including "Align default", which is how a
344+
* column goes back to none.
345+
*
346+
* Picking the active alignment used to clear it, an invisible toggle: a column
347+
* with no alignment renders identically to a left-aligned one, so choosing
348+
* "Align left" twice looked like "nothing happened, then nothing happened",
349+
* and choosing it once on a fresh column looked like the first pick was
350+
* ignored. A ticked group with an explicit default says what the state is and
351+
* only ever moves it where you point. (#485)
352+
*/
340353
function alignItem(
341354
label: string,
342355
align: ColumnAlign,
@@ -345,11 +358,10 @@ function alignItem(
345358
apply: TableMenuRequest['apply'],
346359
row: number
347360
): MenuItem {
348-
const active = model.aligns[col] === align
361+
const current = model.aligns[col] ?? 'none'
349362
return {
350363
kind: 'item',
351-
label: active ? `${label} ✓` : label,
352-
run: () =>
353-
apply(setColumnAlign(model, col, active ? 'none' : align), { row, col })
364+
label: current === align ? `${label} ✓` : label,
365+
run: () => apply(setColumnAlign(model, col, align), { row, col })
354366
}
355367
}

‎packages/app-core/src/lib/cm-table.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { history } from '@codemirror/commands'
66
import { EditorState } from '@codemirror/state'
77
import { EditorView } from '@codemirror/view'
88
import { afterEach, describe, expect, it, vi } from 'vitest'
9+
import { Text } from '@codemirror/state'
910
import {
11+
tableBlockAt,
1012
tablePlugin,
1113
nextWordStart,
1214
prevWordStart,
@@ -431,3 +433,40 @@ describe('table cell link following (#445)', () => {
431433
view.destroy()
432434
})
433435
})
436+
437+
describe('tableBlockAt — the fallback when the parse has not caught up (#485)', () => {
438+
const doc = (text: string) => Text.of(text.split('\n'))
439+
440+
it('finds the table around a position, header row to last row', () => {
441+
const text = 'intro\n\n| A | B |\n| --- | --- |\n| 1 | 2 |\n\nafter'
442+
const t = doc(text)
443+
const inside = text.indexOf('| 1 | 2 |') + 2
444+
const range = tableBlockAt(t, inside)
445+
expect(range).not.toBeNull()
446+
expect(text.slice(range!.from, range!.to)).toBe('| A | B |\n| --- | --- |\n| 1 | 2 |')
447+
})
448+
449+
it('takes the trailing zen:cols marker with it, like the tree path does', () => {
450+
const text = '| A | B |\n| --- | --- |\n| 1 | 2 |\n<!-- zen:cols=273,227 -->\ntail'
451+
const t = doc(text)
452+
const range = tableBlockAt(t, 3)
453+
expect(text.slice(range!.from, range!.to)).toContain('zen:cols=273,227')
454+
})
455+
456+
it('refuses a lone pipe line — that is prose, not a table', () => {
457+
const t = doc('a | b is not a table\n| stray |\nplain')
458+
expect(tableBlockAt(t, t.line(2).from + 2)).toBeNull()
459+
})
460+
461+
it('returns null off the table', () => {
462+
const t = doc('prose here\n\n| A |\n| --- |')
463+
expect(tableBlockAt(t, 2)).toBeNull()
464+
})
465+
466+
it('handles a table at the very start and end of the document', () => {
467+
const text = '| A | B |\n| --- | --- |'
468+
const t = doc(text)
469+
const range = tableBlockAt(t, 0)
470+
expect(range).toEqual({ from: 0, to: text.length })
471+
})
472+
})

‎packages/app-core/src/lib/cm-table.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ function colWidthsAfter(
9595
return widths ? { widths, to: next.to } : null
9696
}
9797

98+
/**
99+
* The table block containing `pos`, found by reading the document rather than
100+
* the syntax tree: the run of `|` rows around that line, plus a trailing
101+
* `zen:cols` marker.
102+
*
103+
* Backs up `tableRangeAt`. Markdown parsing is incremental, so a table that was
104+
* just created or restructured can still be outside the parsed tree when the
105+
* next action commits — and a commit that can't find its range throws the
106+
* user's edit away without a word. Reported as an alignment that only takes
107+
* effect the second time you choose it. (#485)
108+
*/
109+
export function tableBlockAt(doc: Text, pos: number): { from: number; to: number } | null {
110+
const isRow = (text: string): boolean => text.trimStart().startsWith('|')
111+
const line = doc.lineAt(Math.max(0, Math.min(pos, doc.length)))
112+
if (!isRow(line.text)) return null
113+
let first = line.number
114+
while (first > 1 && isRow(doc.line(first - 1).text)) first--
115+
let last = line.number
116+
while (last < doc.lines && isRow(doc.line(last + 1).text)) last++
117+
// A table is at least a header and its delimiter row; one `|` line on its own
118+
// is prose, and rewriting it as a table would be a worse failure than none.
119+
if (last - first < 1) return null
120+
const ext = colWidthsAfter(doc, doc.line(last).to)
121+
return { from: doc.line(first).from, to: ext ? ext.to : doc.line(last).to }
122+
}
123+
98124
/** Find the enclosing `Table` node range for a doc position, or null. The range
99125
* is extended over a trailing `zen:cols` width marker so re-serialization
100126
* replaces both (no duplicate markers, no leaked raw comment). */
@@ -108,7 +134,7 @@ function tableRangeAt(view: EditorView, pos: number): { from: number; to: number
108134
if (!node.parent) break
109135
node = node.parent
110136
}
111-
return null
137+
return tableBlockAt(view.state.doc, pos)
112138
}
113139

114140
/**

0 commit comments

Comments
 (0)