@vibe-tiptap/editor-ui 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/table-context-target.d.ts +11 -0
- package/dist/table-context-target.js +27 -0
- package/dist/table-grips.d.ts +23 -0
- package/dist/table-grips.js +56 -0
- package/package.json +3 -2
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CoreEditorContext } from '@vibe-tiptap/core';
|
|
2
|
+
type EditorState = CoreEditorContext['editor']['state'];
|
|
3
|
+
export interface TableContextTarget {
|
|
4
|
+
cellPos: number;
|
|
5
|
+
columnOffset: number;
|
|
6
|
+
doc: EditorState['doc'];
|
|
7
|
+
clientX: number;
|
|
8
|
+
clientY: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function mapTableContextTarget(target: TableContextTarget, transactions: readonly EditorState['tr'][]): TableContextTarget | undefined;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function mapTableContextTarget(target, transactions) {
|
|
2
|
+
let { cellPos, doc } = target;
|
|
3
|
+
for (const transaction of transactions) {
|
|
4
|
+
if (transaction.before !== doc)
|
|
5
|
+
return;
|
|
6
|
+
const mapped = transaction.mapping.mapResult(cellPos, 1);
|
|
7
|
+
doc = transaction.doc;
|
|
8
|
+
if (!mapped.deleted) {
|
|
9
|
+
cellPos = mapped.pos;
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
const inside = transaction.mapping.mapResult(cellPos + 1, 1);
|
|
13
|
+
if (inside.deleted)
|
|
14
|
+
return;
|
|
15
|
+
const position = doc.resolve(inside.pos);
|
|
16
|
+
const role = position.parent.type.spec.tableRole;
|
|
17
|
+
if (role !== 'cell' && role !== 'header_cell')
|
|
18
|
+
return;
|
|
19
|
+
cellPos = position.before();
|
|
20
|
+
}
|
|
21
|
+
if (doc === target.doc)
|
|
22
|
+
return target;
|
|
23
|
+
const role = doc.nodeAt(cellPos)?.type.spec.tableRole;
|
|
24
|
+
if (role !== 'cell' && role !== 'header_cell')
|
|
25
|
+
return;
|
|
26
|
+
return { ...target, cellPos, doc };
|
|
27
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Editor } from '@vibe-tiptap/core';
|
|
2
|
+
export interface TableGripTarget {
|
|
3
|
+
cell: HTMLTableCellElement;
|
|
4
|
+
table: HTMLTableElement;
|
|
5
|
+
row: number;
|
|
6
|
+
column: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function getTableGripCellStart(editor: Editor, cell: HTMLTableCellElement): import("@tiptap/pm/model").ResolvedPos | null | undefined;
|
|
9
|
+
export declare function getTableGripTarget(editor: Editor, cell: HTMLTableCellElement, clientX: number): TableGripTarget | undefined;
|
|
10
|
+
/** 返回按钮的挂载容器及相对于该容器的坐标。 */
|
|
11
|
+
export declare function getTableGripLayout(editor: Editor, { table, cell, row, column }: TableGripTarget): {
|
|
12
|
+
wrapper: HTMLElement;
|
|
13
|
+
positions: {
|
|
14
|
+
row: {
|
|
15
|
+
left: number;
|
|
16
|
+
top: number;
|
|
17
|
+
};
|
|
18
|
+
column: {
|
|
19
|
+
left: number;
|
|
20
|
+
top: number;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
} | undefined;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { cellAround, TableMap } from '@tiptap/pm/tables';
|
|
2
|
+
export function getTableGripCellStart(editor, cell) {
|
|
3
|
+
if (editor.isDestroyed || !editor.view.dom.contains(cell))
|
|
4
|
+
return;
|
|
5
|
+
return cellAround(editor.state.doc.resolve(editor.view.posAtDOM(cell, 0)));
|
|
6
|
+
}
|
|
7
|
+
export function getTableGripTarget(editor, cell, clientX) {
|
|
8
|
+
const table = cell.closest('table');
|
|
9
|
+
const start = getTableGripCellStart(editor, cell);
|
|
10
|
+
if (!table || !start || editor.view.nodeDOM(start.pos) !== cell)
|
|
11
|
+
return;
|
|
12
|
+
const { left, right, top } = TableMap.get(start.node(-1)).findCell(start.pos - start.start(-1));
|
|
13
|
+
const columns = table.querySelectorAll(':scope > colgroup > col');
|
|
14
|
+
for (let column = left; column < right; column += 1) {
|
|
15
|
+
const bounds = columns[column]?.getBoundingClientRect();
|
|
16
|
+
if (bounds && bounds.width > 0 && clientX < bounds.right) {
|
|
17
|
+
return { table, cell, row: top, column };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const bounds = cell.getBoundingClientRect();
|
|
21
|
+
const fraction = bounds.width > 0 ? (clientX - bounds.left) / bounds.width : 0;
|
|
22
|
+
const column = Math.min(right - 1, Math.max(left, left + Math.floor(fraction * (right - left))));
|
|
23
|
+
return { table, cell, row: top, column };
|
|
24
|
+
}
|
|
25
|
+
/** 返回按钮的挂载容器及相对于该容器的坐标。 */
|
|
26
|
+
export function getTableGripLayout(editor, { table, cell, row, column }) {
|
|
27
|
+
const wrapper = table.parentElement;
|
|
28
|
+
if (!wrapper?.classList.contains('vibe-tiptap-scroll-block'))
|
|
29
|
+
return;
|
|
30
|
+
const rowElement = table.rows[row];
|
|
31
|
+
const start = getTableGripCellStart(editor, cell);
|
|
32
|
+
if (!rowElement || !start)
|
|
33
|
+
return;
|
|
34
|
+
const wrapperBounds = wrapper.getBoundingClientRect();
|
|
35
|
+
const originX = wrapperBounds.left + wrapper.clientLeft - wrapper.scrollLeft;
|
|
36
|
+
const originY = wrapperBounds.top + wrapper.clientTop - wrapper.scrollTop;
|
|
37
|
+
const tableBounds = table.getBoundingClientRect();
|
|
38
|
+
const rowBounds = rowElement.getBoundingClientRect();
|
|
39
|
+
const columnBounds = table
|
|
40
|
+
.querySelectorAll(':scope > colgroup > col')[column]?.getBoundingClientRect();
|
|
41
|
+
const cellBounds = cell.getBoundingClientRect();
|
|
42
|
+
const { left, right } = TableMap.get(start.node(-1)).findCell(start.pos - start.start(-1));
|
|
43
|
+
const columnCenter = columnBounds && columnBounds.width > 0
|
|
44
|
+
? columnBounds.left + columnBounds.width / 2
|
|
45
|
+
: cellBounds.left + ((column - left + 0.5) * cellBounds.width) / (right - left);
|
|
46
|
+
return {
|
|
47
|
+
wrapper,
|
|
48
|
+
positions: {
|
|
49
|
+
row: {
|
|
50
|
+
left: tableBounds.left - originX,
|
|
51
|
+
top: (rowBounds.top + rowBounds.bottom) / 2 - originY,
|
|
52
|
+
},
|
|
53
|
+
column: { left: columnCenter - originX, top: tableBounds.top - originY },
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibe-tiptap/editor-ui",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Vibe Tiptap UI 实现共享的工具栏、主题和文件辅助模块",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@excalidraw/excalidraw": "0.18.0-dabd83c",
|
|
21
|
-
"@
|
|
21
|
+
"@tiptap/pm": "3.31.3",
|
|
22
|
+
"@vibe-tiptap/core": "1.1.4"
|
|
22
23
|
},
|
|
23
24
|
"peerDependencies": {
|
|
24
25
|
"react": "^18.0.0 || ^19.0.0",
|