@viveeditor/ext-table 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/index.d.ts +34 -0
- package/dist/index.js +187 -0
- package/dist/index.js.map +1 -0
- package/package.json +84 -3
- package/style.css +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 viveEditor contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @viveeditor/ext-table
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Table extension for viveEditor: merged cells, column widths, keyboard navigation.
|
|
4
|
+
|
|
5
|
+
Part of [viveEditor](https://github.com/mtvrkan/viveeditor#readme), a free, open-source rich text editor.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @viveeditor/ext-table @viveeditor/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Editor } from '@viveeditor/core'
|
|
17
|
+
import '@viveeditor/core/style.css'
|
|
18
|
+
import { Table } from '@viveeditor/ext-table'
|
|
19
|
+
import '@viveeditor/ext-table/style.css'
|
|
20
|
+
|
|
21
|
+
const editor = await Editor.create(document.querySelector('#editor'), {
|
|
22
|
+
extensions: [Table],
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
editor.commands.insertTable({ rows: 3, cols: 3, headerRow: true })
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Merged cells, header rows, column widths by dragging, and Tab to the next cell. Saved HTML keeps column widths on any site.
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Extension } from "@viveeditor/core";
|
|
2
|
+
import { Command } from "prosemirror-state";
|
|
3
|
+
export interface TableOptions {
|
|
4
|
+
resizable: boolean;
|
|
5
|
+
cellMinWidth: number;
|
|
6
|
+
maxSize: number;
|
|
7
|
+
}
|
|
8
|
+
export interface InsertTableOptions {
|
|
9
|
+
rows?: number;
|
|
10
|
+
cols?: number;
|
|
11
|
+
headerRow?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare module "@viveeditor/core" {
|
|
14
|
+
interface CommandRegistry {
|
|
15
|
+
insertTable: (options?: InsertTableOptions) => Command;
|
|
16
|
+
deleteTable: () => Command;
|
|
17
|
+
addRowBefore: () => Command;
|
|
18
|
+
addRowAfter: () => Command;
|
|
19
|
+
deleteRow: () => Command;
|
|
20
|
+
addColumnBefore: () => Command;
|
|
21
|
+
addColumnAfter: () => Command;
|
|
22
|
+
deleteColumn: () => Command;
|
|
23
|
+
mergeCells: () => Command;
|
|
24
|
+
splitCell: () => Command;
|
|
25
|
+
toggleHeaderRow: () => Command;
|
|
26
|
+
toggleHeaderColumn: () => Command;
|
|
27
|
+
resizeColumn: (delta: number) => Command;
|
|
28
|
+
}
|
|
29
|
+
interface QueryRegistry {
|
|
30
|
+
inTable: () => boolean;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export declare const Table: Extension<TableOptions>;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { defineExtension } from "@viveeditor/core";
|
|
2
|
+
import { Plugin, TextSelection } from "prosemirror-state";
|
|
3
|
+
import { TableMap, TableView, addColumnAfter, addColumnBefore, addRow, addRowAfter, addRowBefore, columnResizing, deleteColumn, deleteRow, deleteTable, goToNextCell, isInTable, mergeCells, selectedRect, splitCell, tableEditing, tableNodeTypes, tableNodes, toggleHeaderColumn, toggleHeaderRow } from "prosemirror-tables";
|
|
4
|
+
// src/index.ts
|
|
5
|
+
const FALLBACK_WIDTH = 120;
|
|
6
|
+
function count(value, fallback, max) {
|
|
7
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 1 ? Math.min(value, max) : fallback;
|
|
8
|
+
}
|
|
9
|
+
function columnWidths(table) {
|
|
10
|
+
const widths = [];
|
|
11
|
+
table.firstChild?.forEach((cell) => {
|
|
12
|
+
const colwidth = cell.attrs.colwidth;
|
|
13
|
+
for (let index = 0; index < Number(cell.attrs.colspan); index++) {
|
|
14
|
+
const width = Array.isArray(colwidth) ? colwidth[index] : null;
|
|
15
|
+
widths.push(typeof width === "number" && width > 0 ? Math.round(width) : null);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return widths;
|
|
19
|
+
}
|
|
20
|
+
function renderTable(node, cellMinWidth) {
|
|
21
|
+
const widths = columnWidths(node);
|
|
22
|
+
if (widths.every((width) => width === null)) return ["table", ["tbody", 0]];
|
|
23
|
+
const total = widths.reduce((sum, width) => sum + (width ?? cellMinWidth), 0);
|
|
24
|
+
return [
|
|
25
|
+
"table",
|
|
26
|
+
{ style: `${widths.includes(null) ? "min-width" : "width"}: ${total}px` },
|
|
27
|
+
["colgroup", ...widths.map((width) => ["col", width ? { style: `width: ${width}px` } : {}])],
|
|
28
|
+
["tbody", 0]
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
function createTable(schema, rows, cols, headerRow) {
|
|
32
|
+
const types = tableNodeTypes(schema);
|
|
33
|
+
const cell = (header) => {
|
|
34
|
+
const node = (header ? types.header_cell : types.cell).createAndFill();
|
|
35
|
+
if (!node) throw new Error("Table cells must accept an empty paragraph.");
|
|
36
|
+
return node;
|
|
37
|
+
};
|
|
38
|
+
const rowNodes = Array.from({ length: rows }, (_row, index) => types.row.create(null, Array.from({ length: cols }, () => cell(headerRow && index === 0))));
|
|
39
|
+
return types.table.create(null, rowNodes);
|
|
40
|
+
}
|
|
41
|
+
function insertTable(schema, options, max) {
|
|
42
|
+
return (state, dispatch) => {
|
|
43
|
+
const { $from } = state.selection;
|
|
44
|
+
if (isInTable(state) || $from.depth === 0) return false;
|
|
45
|
+
if (!$from.parent.isTextblock || $from.parent.type.spec.code) return false;
|
|
46
|
+
const container = $from.node(-1);
|
|
47
|
+
const index = $from.index(-1);
|
|
48
|
+
const tableType = tableNodeTypes(schema).table;
|
|
49
|
+
const replace = $from.parent.content.size === 0 && container.canReplaceWith(index, index + 1, tableType);
|
|
50
|
+
if (!replace && !container.canReplaceWith(index + 1, index + 1, tableType)) return false;
|
|
51
|
+
if (dispatch) {
|
|
52
|
+
const nodes = [createTable(schema, count(options.rows, 3, max), count(options.cols, 3, max), options.headerRow ?? true)];
|
|
53
|
+
const paragraph = schema.nodes.paragraph;
|
|
54
|
+
if (index + 1 === container.childCount && paragraph) nodes.push(paragraph.create());
|
|
55
|
+
const from = replace ? $from.before() : $from.after();
|
|
56
|
+
const transaction = state.tr.replaceWith(from, replace ? $from.after() : from, nodes);
|
|
57
|
+
transaction.setSelection(TextSelection.create(transaction.doc, from + 4));
|
|
58
|
+
dispatch(transaction.scrollIntoView());
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const appendRow = (state, dispatch) => {
|
|
64
|
+
if (!isInTable(state)) return false;
|
|
65
|
+
if (dispatch) {
|
|
66
|
+
const rect = selectedRect(state);
|
|
67
|
+
const transaction = addRow(state.tr, rect, rect.map.height);
|
|
68
|
+
const table = transaction.doc.nodeAt(rect.tableStart - 1);
|
|
69
|
+
if (!table) return false;
|
|
70
|
+
const map = TableMap.get(table);
|
|
71
|
+
const first = map.map[(map.height - 1) * map.width] ?? 0;
|
|
72
|
+
transaction.setSelection(TextSelection.near(transaction.doc.resolve(rect.tableStart + first + 1)));
|
|
73
|
+
dispatch(transaction.scrollIntoView());
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
function resizeColumn(delta, cellMinWidth) {
|
|
78
|
+
return (state, dispatch, view) => {
|
|
79
|
+
if (!isInTable(state) || !Number.isFinite(delta) || delta === 0) return false;
|
|
80
|
+
if (!dispatch) return true;
|
|
81
|
+
const { map, table, tableStart, left } = selectedRect(state);
|
|
82
|
+
const cells = /* @__PURE__ */ new Set();
|
|
83
|
+
for (let row = 0; row < map.height; row++) {
|
|
84
|
+
const position = map.map[row * map.width + left];
|
|
85
|
+
if (position !== void 0) cells.add(position);
|
|
86
|
+
}
|
|
87
|
+
const known = [...cells].map((position) => {
|
|
88
|
+
const colwidth = table.nodeAt(position)?.attrs.colwidth;
|
|
89
|
+
return Array.isArray(colwidth) ? colwidth[left - map.colCount(position)] : null;
|
|
90
|
+
}).find((width) => typeof width === "number" && width > 0);
|
|
91
|
+
const first = [...cells][0];
|
|
92
|
+
const dom = first === void 0 ? null : view?.nodeDOM(tableStart + first);
|
|
93
|
+
const measured = dom instanceof HTMLElement ? dom.offsetWidth : 0;
|
|
94
|
+
const width = Math.max(cellMinWidth, Math.round((known ?? (measured || FALLBACK_WIDTH)) + delta));
|
|
95
|
+
const transaction = state.tr;
|
|
96
|
+
for (const position of cells) {
|
|
97
|
+
const cell = table.nodeAt(position);
|
|
98
|
+
if (!cell) continue;
|
|
99
|
+
const colspan = Number(cell.attrs.colspan);
|
|
100
|
+
const colwidth = cell.attrs.colwidth;
|
|
101
|
+
const widths = Array.isArray(colwidth) ? colwidth.map((value) => typeof value === "number" ? value : 0) : Array.from({ length: colspan }, () => 0);
|
|
102
|
+
widths[left - map.colCount(position)] = width;
|
|
103
|
+
transaction.setNodeMarkup(tableStart + position, void 0, {
|
|
104
|
+
...cell.attrs,
|
|
105
|
+
colwidth: widths
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
dispatch(transaction);
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function tableSpecs(cellMinWidth) {
|
|
113
|
+
const specs = tableNodes({
|
|
114
|
+
tableGroup: "block",
|
|
115
|
+
cellContent: "block+",
|
|
116
|
+
cellAttributes: {}
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
table: {
|
|
120
|
+
...specs.table,
|
|
121
|
+
content: "tableRow+",
|
|
122
|
+
toDOM: (node) => renderTable(node, cellMinWidth)
|
|
123
|
+
},
|
|
124
|
+
tableRow: {
|
|
125
|
+
...specs.table_row,
|
|
126
|
+
content: "(tableCell | tableHeader)*"
|
|
127
|
+
},
|
|
128
|
+
tableCell: specs.table_cell,
|
|
129
|
+
tableHeader: specs.table_header
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const Table = defineExtension({
|
|
133
|
+
name: "table",
|
|
134
|
+
priority: 90,
|
|
135
|
+
defaultOptions: {
|
|
136
|
+
resizable: true,
|
|
137
|
+
cellMinWidth: 48,
|
|
138
|
+
maxSize: 20
|
|
139
|
+
},
|
|
140
|
+
nodes: (options) => tableSpecs(options.cellMinWidth),
|
|
141
|
+
commands: ({ schema, options }) => ({
|
|
142
|
+
insertTable: (insert) => insertTable(schema, insert ?? {}, options.maxSize),
|
|
143
|
+
deleteTable: () => deleteTable,
|
|
144
|
+
addRowBefore: () => addRowBefore,
|
|
145
|
+
addRowAfter: () => addRowAfter,
|
|
146
|
+
deleteRow: () => deleteRow,
|
|
147
|
+
addColumnBefore: () => addColumnBefore,
|
|
148
|
+
addColumnAfter: () => addColumnAfter,
|
|
149
|
+
deleteColumn: () => deleteColumn,
|
|
150
|
+
mergeCells: () => mergeCells,
|
|
151
|
+
splitCell: () => splitCell,
|
|
152
|
+
toggleHeaderRow: () => toggleHeaderRow,
|
|
153
|
+
toggleHeaderColumn: () => toggleHeaderColumn,
|
|
154
|
+
resizeColumn: (delta) => resizeColumn(delta, options.cellMinWidth)
|
|
155
|
+
}),
|
|
156
|
+
queries: () => ({ inTable: (state) => isInTable(state) }),
|
|
157
|
+
keymap: () => ({
|
|
158
|
+
Tab: (state, dispatch) => goToNextCell(1)(state, dispatch) || appendRow(state, dispatch),
|
|
159
|
+
"Shift-Tab": goToNextCell(-1)
|
|
160
|
+
}),
|
|
161
|
+
plugins: ({ options }) => {
|
|
162
|
+
let escaped = false;
|
|
163
|
+
const view = (node) => new TableView(node, options.cellMinWidth);
|
|
164
|
+
return [
|
|
165
|
+
options.resizable ? columnResizing({
|
|
166
|
+
cellMinWidth: options.cellMinWidth,
|
|
167
|
+
View: TableView
|
|
168
|
+
}) : new Plugin({ props: { nodeViews: { table: view } } }),
|
|
169
|
+
tableEditing(),
|
|
170
|
+
new Plugin({ props: { handleDOMEvents: { keydown: (editorView, event) => {
|
|
171
|
+
if (event.key === "Escape") {
|
|
172
|
+
escaped = isInTable(editorView.state);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (event.key === "Tab" && escaped) {
|
|
176
|
+
escaped = false;
|
|
177
|
+
return isInTable(editorView.state);
|
|
178
|
+
}
|
|
179
|
+
if (event.key !== "Shift") escaped = false;
|
|
180
|
+
return false;
|
|
181
|
+
} } } })
|
|
182
|
+
];
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
export { Table };
|
|
186
|
+
|
|
187
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { defineExtension, type Extension } from '@viveeditor/core'\nimport type { DOMOutputSpec, NodeSpec, Node as ProseMirrorNode, Schema } from 'prosemirror-model'\nimport { type Command, Plugin, TextSelection } from 'prosemirror-state'\nimport {\n addColumnAfter,\n addColumnBefore,\n addRow,\n addRowAfter,\n addRowBefore,\n columnResizing,\n deleteColumn,\n deleteRow,\n deleteTable,\n goToNextCell,\n isInTable,\n mergeCells,\n selectedRect,\n splitCell,\n TableMap,\n TableView,\n tableEditing,\n tableNodes,\n tableNodeTypes,\n toggleHeaderColumn,\n toggleHeaderRow,\n} from 'prosemirror-tables'\n\nexport interface TableOptions {\n resizable: boolean\n cellMinWidth: number\n maxSize: number\n}\n\nexport interface InsertTableOptions {\n rows?: number\n cols?: number\n headerRow?: boolean\n}\n\ndeclare module '@viveeditor/core' {\n interface CommandRegistry {\n insertTable: (options?: InsertTableOptions) => Command\n deleteTable: () => Command\n addRowBefore: () => Command\n addRowAfter: () => Command\n deleteRow: () => Command\n addColumnBefore: () => Command\n addColumnAfter: () => Command\n deleteColumn: () => Command\n mergeCells: () => Command\n splitCell: () => Command\n toggleHeaderRow: () => Command\n toggleHeaderColumn: () => Command\n resizeColumn: (delta: number) => Command\n }\n interface QueryRegistry {\n inTable: () => boolean\n }\n}\n\nconst FALLBACK_WIDTH = 120\n\nfunction count(value: unknown, fallback: number, max: number): number {\n return typeof value === 'number' && Number.isInteger(value) && value >= 1\n ? Math.min(value, max)\n : fallback\n}\n\nfunction columnWidths(table: ProseMirrorNode): Array<number | null> {\n const widths: Array<number | null> = []\n table.firstChild?.forEach((cell) => {\n const colwidth: unknown = cell.attrs.colwidth\n for (let index = 0; index < Number(cell.attrs.colspan); index++) {\n const width = Array.isArray(colwidth) ? colwidth[index] : null\n widths.push(typeof width === 'number' && width > 0 ? Math.round(width) : null)\n }\n })\n return widths\n}\n\nfunction renderTable(node: ProseMirrorNode, cellMinWidth: number): DOMOutputSpec {\n const widths = columnWidths(node)\n if (widths.every((width) => width === null)) return ['table', ['tbody', 0]]\n const total = widths.reduce<number>((sum, width) => sum + (width ?? cellMinWidth), 0)\n const property = widths.includes(null) ? 'min-width' : 'width'\n return [\n 'table',\n { style: `${property}: ${total}px` },\n ['colgroup', ...widths.map((width) => ['col', width ? { style: `width: ${width}px` } : {}])],\n ['tbody', 0],\n ]\n}\n\nfunction createTable(schema: Schema, rows: number, cols: number, headerRow: boolean) {\n const types = tableNodeTypes(schema)\n const cell = (header: boolean): ProseMirrorNode => {\n const node = (header ? types.header_cell : types.cell).createAndFill()\n if (!node) throw new Error('Table cells must accept an empty paragraph.')\n return node\n }\n const rowNodes = Array.from({ length: rows }, (_row, index) =>\n types.row.create(\n null,\n Array.from({ length: cols }, () => cell(headerRow && index === 0)),\n ),\n )\n return types.table.create(null, rowNodes)\n}\n\nfunction insertTable(schema: Schema, options: InsertTableOptions, max: number): Command {\n return (state, dispatch) => {\n const { $from } = state.selection\n if (isInTable(state) || $from.depth === 0) return false\n if (!$from.parent.isTextblock || $from.parent.type.spec.code) return false\n const container = $from.node(-1)\n const index = $from.index(-1)\n const tableType = tableNodeTypes(schema).table\n const replace =\n $from.parent.content.size === 0 && container.canReplaceWith(index, index + 1, tableType)\n if (!replace && !container.canReplaceWith(index + 1, index + 1, tableType)) return false\n if (dispatch) {\n const table = createTable(\n schema,\n count(options.rows, 3, max),\n count(options.cols, 3, max),\n options.headerRow ?? true,\n )\n const nodes = [table]\n const paragraph = schema.nodes.paragraph\n if (index + 1 === container.childCount && paragraph) nodes.push(paragraph.create())\n const from = replace ? $from.before() : $from.after()\n const transaction = state.tr.replaceWith(from, replace ? $from.after() : from, nodes)\n transaction.setSelection(TextSelection.create(transaction.doc, from + 4))\n dispatch(transaction.scrollIntoView())\n }\n return true\n }\n}\n\nconst appendRow: Command = (state, dispatch) => {\n if (!isInTable(state)) return false\n if (dispatch) {\n const rect = selectedRect(state)\n const transaction = addRow(state.tr, rect, rect.map.height)\n const table = transaction.doc.nodeAt(rect.tableStart - 1)\n if (!table) return false\n const map = TableMap.get(table)\n const first = map.map[(map.height - 1) * map.width] ?? 0\n transaction.setSelection(\n TextSelection.near(transaction.doc.resolve(rect.tableStart + first + 1)),\n )\n dispatch(transaction.scrollIntoView())\n }\n return true\n}\n\nfunction resizeColumn(delta: number, cellMinWidth: number): Command {\n return (state, dispatch, view) => {\n if (!isInTable(state) || !Number.isFinite(delta) || delta === 0) return false\n if (!dispatch) return true\n const { map, table, tableStart, left } = selectedRect(state)\n const cells = new Set<number>()\n for (let row = 0; row < map.height; row++) {\n const position = map.map[row * map.width + left]\n if (position !== undefined) cells.add(position)\n }\n const known = [...cells]\n .map((position) => {\n const cell = table.nodeAt(position)\n const colwidth: unknown = cell?.attrs.colwidth\n return Array.isArray(colwidth) ? colwidth[left - map.colCount(position)] : null\n })\n .find((width): width is number => typeof width === 'number' && width > 0)\n const first = [...cells][0]\n const dom = first === undefined ? null : view?.nodeDOM(tableStart + first)\n const measured = dom instanceof HTMLElement ? dom.offsetWidth : 0\n const width = Math.max(\n cellMinWidth,\n Math.round((known ?? (measured || FALLBACK_WIDTH)) + delta),\n )\n const transaction = state.tr\n for (const position of cells) {\n const cell = table.nodeAt(position)\n if (!cell) continue\n const colspan = Number(cell.attrs.colspan)\n const colwidth: unknown = cell.attrs.colwidth\n const widths: number[] = Array.isArray(colwidth)\n ? colwidth.map((value) => (typeof value === 'number' ? value : 0))\n : Array.from({ length: colspan }, () => 0)\n widths[left - map.colCount(position)] = width\n transaction.setNodeMarkup(tableStart + position, undefined, {\n ...cell.attrs,\n colwidth: widths,\n })\n }\n dispatch(transaction)\n return true\n }\n}\n\nfunction tableSpecs(cellMinWidth: number): Record<string, NodeSpec> {\n const specs = tableNodes({ tableGroup: 'block', cellContent: 'block+', cellAttributes: {} })\n return {\n table: {\n ...specs.table,\n content: 'tableRow+',\n toDOM: (node) => renderTable(node, cellMinWidth),\n },\n tableRow: { ...specs.table_row, content: '(tableCell | tableHeader)*' },\n tableCell: specs.table_cell,\n tableHeader: specs.table_header,\n }\n}\n\nexport const Table: Extension<TableOptions> = defineExtension({\n name: 'table',\n priority: 90,\n defaultOptions: { resizable: true, cellMinWidth: 48, maxSize: 20 },\n nodes: (options) => tableSpecs(options.cellMinWidth),\n commands: ({ schema, options }) => ({\n insertTable: (insert?: InsertTableOptions) =>\n insertTable(schema, insert ?? {}, options.maxSize),\n deleteTable: () => deleteTable,\n addRowBefore: () => addRowBefore,\n addRowAfter: () => addRowAfter,\n deleteRow: () => deleteRow,\n addColumnBefore: () => addColumnBefore,\n addColumnAfter: () => addColumnAfter,\n deleteColumn: () => deleteColumn,\n mergeCells: () => mergeCells,\n splitCell: () => splitCell,\n toggleHeaderRow: () => toggleHeaderRow,\n toggleHeaderColumn: () => toggleHeaderColumn,\n resizeColumn: (delta: number) => resizeColumn(delta, options.cellMinWidth),\n }),\n queries: () => ({\n inTable: (state) => isInTable(state),\n }),\n keymap: () => ({\n Tab: (state, dispatch) => goToNextCell(1)(state, dispatch) || appendRow(state, dispatch),\n 'Shift-Tab': goToNextCell(-1),\n }),\n plugins: ({ options }) => {\n let escaped = false\n const view = (node: ProseMirrorNode) => new TableView(node, options.cellMinWidth)\n return [\n options.resizable\n ? columnResizing({ cellMinWidth: options.cellMinWidth, View: TableView })\n : new Plugin({ props: { nodeViews: { table: view } } }),\n tableEditing(),\n new Plugin({\n props: {\n handleDOMEvents: {\n keydown: (editorView, event) => {\n if (event.key === 'Escape') {\n escaped = isInTable(editorView.state)\n return false\n }\n if (event.key === 'Tab' && escaped) {\n escaped = false\n return isInTable(editorView.state)\n }\n if (event.key !== 'Shift') escaped = false\n return false\n },\n },\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;AA4DA,MAAM,iBAAiB;AAEvB,SAAS,MAAM,OAAgB,UAAkB,KAAqB;CACpE,OAAO,OAAO,UAAU,YAAY,OAAO,UAAU,KAAK,KAAK,SAAS,IACpE,KAAK,IAAI,OAAO,GAAG,IACnB;AACN;AAEA,SAAS,aAAa,OAA8C;CAClE,MAAM,SAA+B,CAAC;CACtC,MAAM,YAAY,SAAS,SAAS;EAClC,MAAM,WAAoB,KAAK,MAAM;EACrC,KAAK,IAAI,QAAQ,GAAG,QAAQ,OAAO,KAAK,MAAM,OAAO,GAAG,SAAS;GAC/D,MAAM,QAAQ,MAAM,QAAQ,QAAQ,IAAI,SAAS,SAAS;GAC1D,OAAO,KAAK,OAAO,UAAU,YAAY,QAAQ,IAAI,KAAK,MAAM,KAAK,IAAI,IAAI;EAC/E;CACF,CAAC;CACD,OAAO;AACT;AAEA,SAAS,YAAY,MAAuB,cAAqC;CAC/E,MAAM,SAAS,aAAa,IAAI;CAChC,IAAI,OAAO,OAAO,UAAU,UAAU,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;CAC1E,MAAM,QAAQ,OAAO,QAAgB,KAAK,UAAU,OAAO,SAAS,eAAe,CAAC;CAEpF,OAAO;EACL;EACA,EAAE,OAAO,GAHM,OAAO,SAAS,IAAI,IAAI,cAAc,QAGhC,IAAI,MAAM,IAAI;EACnC,CAAC,YAAY,GAAG,OAAO,KAAK,UAAU,CAAC,OAAO,QAAQ,EAAE,OAAO,UAAU,MAAM,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;EAC3F,CAAC,SAAS,CAAC;CACb;AACF;AAEA,SAAS,YAAY,QAAgB,MAAc,MAAc,WAAoB;CACnF,MAAM,QAAQ,eAAe,MAAM;CACnC,MAAM,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
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Table extension for viveEditor: merged cells, column widths, keyboard navigation.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"viveeditor",
|
|
8
|
+
"rich-text-editor",
|
|
9
|
+
"wysiwyg",
|
|
10
|
+
"editor",
|
|
11
|
+
"prosemirror",
|
|
12
|
+
"table",
|
|
13
|
+
"merge-cells",
|
|
14
|
+
"column-resize"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/mtvrkan/viveeditor/tree/main/packages/ext-table#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/mtvrkan/viveeditor/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/mtvrkan/viveeditor.git",
|
|
23
|
+
"directory": "packages/ext-table"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": [
|
|
27
|
+
"*.css"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./style.css": "./style.css"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"style.css"
|
|
39
|
+
],
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@viveeditor/core": "^0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"prosemirror-model": "^1.25.12",
|
|
45
|
+
"prosemirror-state": "^1.4.4",
|
|
46
|
+
"prosemirror-tables": "^1.8.5",
|
|
47
|
+
"prosemirror-view": "^1.42.7"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@size-limit/preset-small-lib": "^14.3.0",
|
|
51
|
+
"jsdom": "^30.1.3",
|
|
52
|
+
"size-limit": "^14.3.0",
|
|
53
|
+
"tsdown": "0.23.1",
|
|
54
|
+
"vitest": "^5.0.5",
|
|
55
|
+
"@viveeditor/core": "0.1.0",
|
|
56
|
+
"@viveeditor/ext-list": "0.1.0",
|
|
57
|
+
"@viveeditor/ext-code-block": "0.1.0"
|
|
58
|
+
},
|
|
59
|
+
"size-limit": [
|
|
60
|
+
{
|
|
61
|
+
"name": "@viveeditor/ext-table",
|
|
62
|
+
"path": "dist/index.js",
|
|
63
|
+
"ignore": [
|
|
64
|
+
"@viveeditor/core",
|
|
65
|
+
"prosemirror-keymap",
|
|
66
|
+
"prosemirror-model",
|
|
67
|
+
"prosemirror-state",
|
|
68
|
+
"prosemirror-transform",
|
|
69
|
+
"prosemirror-view"
|
|
70
|
+
],
|
|
71
|
+
"limit": "12 KB"
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=22.12"
|
|
76
|
+
},
|
|
77
|
+
"publishConfig": {
|
|
78
|
+
"access": "public",
|
|
79
|
+
"provenance": true
|
|
80
|
+
},
|
|
81
|
+
"scripts": {
|
|
82
|
+
"build": "tsdown",
|
|
83
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
84
|
+
"test": "vitest run",
|
|
85
|
+
"size": "size-limit"
|
|
86
|
+
}
|
|
6
87
|
}
|
package/style.css
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
.vive-content .tableWrapper {
|
|
2
|
+
margin: 0 0 0.9em;
|
|
3
|
+
overflow-x: auto;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.vive-content table {
|
|
7
|
+
width: 100%;
|
|
8
|
+
border-collapse: collapse;
|
|
9
|
+
table-layout: fixed;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.vive-content td,
|
|
13
|
+
.vive-content th {
|
|
14
|
+
position: relative;
|
|
15
|
+
padding: 6px 10px;
|
|
16
|
+
border: 1px solid var(--ve-border, hsl(220 13% 85%));
|
|
17
|
+
text-align: start;
|
|
18
|
+
vertical-align: top;
|
|
19
|
+
overflow-wrap: anywhere;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.vive-content th {
|
|
23
|
+
background: var(--ve-muted, hsl(220 14% 95%));
|
|
24
|
+
font-weight: 600;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.vive-content :is(td, th) > :first-child {
|
|
28
|
+
margin-top: 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.vive-content :is(td, th) > :last-child {
|
|
32
|
+
margin-bottom: 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.vive-content .selectedCell::after {
|
|
36
|
+
position: absolute;
|
|
37
|
+
inset: 0;
|
|
38
|
+
background: color-mix(in oklab, var(--ve-primary, hsl(202 80% 36%)) 16%, transparent);
|
|
39
|
+
content: "";
|
|
40
|
+
pointer-events: none;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.vive-content .column-resize-handle {
|
|
44
|
+
position: absolute;
|
|
45
|
+
top: 0;
|
|
46
|
+
right: -2px;
|
|
47
|
+
bottom: -1px;
|
|
48
|
+
width: 4px;
|
|
49
|
+
background: var(--ve-primary, hsl(202 80% 36%));
|
|
50
|
+
pointer-events: none;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.vive-content.resize-cursor,
|
|
54
|
+
.resize-cursor .vive-content {
|
|
55
|
+
cursor: col-resize;
|
|
56
|
+
}
|