Skip to content

Commit 1117a1c

Browse files
committed
chg: move code & add cellAround test
1 parent 0dae0b2 commit 1117a1c

File tree

3 files changed

+88
-29
lines changed

3 files changed

+88
-29
lines changed

src/input.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -288,33 +288,5 @@ function cellUnderMouse(
288288
});
289289
if (!mousePos) return null;
290290
const $pos = view.state.doc.resolve(mousePos.pos);
291-
const cell = cellAround($pos);
292-
if (cell) return cell;
293-
// search for entire merge cells
294-
for (let d = $pos.depth; d > 0; d--) {
295-
if ($pos.node(d).type.spec.tableRole === 'table') {
296-
const table = $pos.node(d);
297-
const tableStart = $pos.start(d);
298-
const map = TableMap.get(table);
299-
300-
for (let i = 0; i < map.map.length; i++) {
301-
const cellPos = map.map[i];
302-
const cellRect = map.findCell(cellPos);
303-
304-
const row = Math.floor(i / map.width);
305-
const col = i % map.width;
306-
if (
307-
row >= cellRect.top &&
308-
row < cellRect.bottom &&
309-
col >= cellRect.left &&
310-
col < cellRect.right
311-
) {
312-
return view.state.doc.resolve(tableStart + cellPos);
313-
}
314-
}
315-
break;
316-
}
317-
}
318-
319-
return null;
291+
return cellAround($pos);
320292
}

src/util.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ export function cellAround($pos: ResolvedPos): ResolvedPos | null {
3333
for (let d = $pos.depth - 1; d > 0; d--)
3434
if ($pos.node(d).type.spec.tableRole == 'row')
3535
return $pos.node(0).resolve($pos.before(d + 1));
36+
37+
// search the entire mergeCells
38+
for (let d = $pos.depth; d > 0; d--) {
39+
const node = $pos.node(d);
40+
if (node.type.spec.tableRole === 'table') {
41+
const table = TableMap.get(node);
42+
for (let i = 0; i < table.map.length; i++) {
43+
const cellPos = table.map[i];
44+
try {
45+
const cellRect = table.findCell(cellPos);
46+
const row = Math.floor(i / table.width);
47+
const col = i % table.width;
48+
49+
if (
50+
row >= cellRect.top &&
51+
row < cellRect.bottom &&
52+
col >= cellRect.left &&
53+
col < cellRect.right
54+
) {
55+
return $pos.node(0).resolve($pos.start(d) + cellPos);
56+
}
57+
} catch (e) {
58+
continue;
59+
}
60+
}
61+
break;
62+
}
63+
}
64+
3665
return null;
3766
}
3867

test/cellAround.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { EditorState } from 'prosemirror-state';
2+
import { describe, expect, it } from 'vitest';
3+
import { cellAround } from '../src/util';
4+
import { c, c11, cCursor, p, table, tr, selectionFor } from './build';
5+
6+
describe('cellAround', () => {
7+
it('cursor is in text', () => {
8+
const doc = table(
9+
tr(cCursor, c11, c11),
10+
);
11+
const state = EditorState.create({ doc, selection: selectionFor(doc) });
12+
13+
expect(cellAround(state.doc.resolve(state.selection.anchor))).not.toBeNull();
14+
expect(cellAround(state.doc.resolve(doc.content.size))).toBeNull();
15+
});
16+
// Test finding a cell in a regular cell
17+
it('finds a regular cell', () => {
18+
// Create a simple table with cursor in the middle cell
19+
const doc = table(
20+
tr(c11, c11, c11),
21+
tr(c11, cCursor, c11),
22+
tr(c11, c11, c11),
23+
);
24+
const state = EditorState.create({ doc, selection: selectionFor(doc) });
25+
const $pos = state.doc.resolve(state.selection.anchor);
26+
const $cell = cellAround($pos);
27+
28+
// Verify that the cell was found
29+
expect($cell).not.toBeNull();
30+
// Verify that the found cell is the expected merged cell
31+
const cellNode = $cell!.nodeAfter;
32+
expect(cellNode!.attrs.colspan).toBe(1);
33+
expect(cellNode!.attrs.rowspan).toBe(1);
34+
});
35+
36+
// Test finding a cell when cursor is inside a merged cell
37+
it('finds a merged cell when cursor is inside', () => {
38+
// Create a table with a merged cell, cursor inside the merged cell
39+
const doc = table(
40+
tr(c11, c(2, 2, 'x<cursor>'), c11),
41+
tr(c11, c11),
42+
tr(c11, c11, c11),
43+
);
44+
45+
const state = EditorState.create({ doc, selection: selectionFor(doc) });
46+
const $pos = state.doc.resolve(state.selection.anchor);
47+
const $cell = cellAround($pos);
48+
49+
// Verify that the cell was found
50+
expect($cell).not.toBeNull();
51+
// Verify that the found cell is a merged cell
52+
const cellNode = $cell!.nodeAfter;
53+
expect(cellNode!.attrs.colspan).toBe(2);
54+
expect(cellNode!.attrs.rowspan).toBe(2);
55+
});
56+
57+
58+
});

0 commit comments

Comments
 (0)