tiptap-essentials 2.0.0-beta.6 → 2.0.0-beta.8
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/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/samples.d.ts +1 -0
- package/dist/samples.js +10 -2
- package/dist/table-cell.d.ts +2 -0
- package/dist/table-cell.js +94 -0
- package/dist/table-sizing.d.ts +73 -0
- package/dist/table-sizing.js +482 -0
- package/dist/table.d.ts +37 -3
- package/dist/table.js +140 -8
- package/dist/tiptap-essentials.d.ts +1 -1
- package/dist/tiptap-essentials.js +14 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -17,5 +17,7 @@ export { Subscript } from './subscript.js';
|
|
|
17
17
|
export { Superscript } from './superscript.js';
|
|
18
18
|
export { Table } from './table.js';
|
|
19
19
|
export type { TableOptions } from './table.js';
|
|
20
|
+
export { TableCell, TableHeader } from './table-cell.js';
|
|
21
|
+
export { columnShares } from './table-sizing.js';
|
|
20
22
|
export { TextStyle } from './text-style.js';
|
|
21
23
|
export { Youtube } from './youtube.js';
|
package/dist/index.js
CHANGED
|
@@ -9,5 +9,7 @@ export { PlainSpaces } from './plain-spaces.js';
|
|
|
9
9
|
export { Subscript } from './subscript.js';
|
|
10
10
|
export { Superscript } from './superscript.js';
|
|
11
11
|
export { Table } from './table.js';
|
|
12
|
+
export { TableCell, TableHeader } from './table-cell.js';
|
|
13
|
+
export { columnShares } from './table-sizing.js';
|
|
12
14
|
export { TextStyle } from './text-style.js';
|
|
13
15
|
export { Youtube } from './youtube.js';
|
package/dist/samples.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
export declare const picture: string;
|
|
7
7
|
export declare const table: string;
|
|
8
8
|
export declare const captionedTable: string;
|
|
9
|
+
export declare const styledTable: string;
|
|
9
10
|
export declare const video: string;
|
|
10
11
|
export declare const size = "<p><span style=\"font-size: var(--lg-font-size);\">Large</span> text</p>";
|
|
11
12
|
export declare const color = "<p><span style=\"color: var(--tiptap-text-red);\">Red</span> text</p>";
|
package/dist/samples.js
CHANGED
|
@@ -7,16 +7,24 @@ export const picture = '<picture><source type="image/jxl" srcset="https://static
|
|
|
7
7
|
'<img src="https://static.example/uploads/store/photo-1200.jpg" alt="A harbour" title="A harbour" ' +
|
|
8
8
|
'srcset="https://static.example/uploads/store/photo-300.jpg 300w" sizes="auto" loading="lazy" ' +
|
|
9
9
|
'style="width: 100%; aspect-ratio: 3 / 2;"></picture>';
|
|
10
|
+
// A table no line has moved, its columns at even shares.
|
|
10
11
|
export const table = '<div class="table-responsive" role="region" tabindex="0" aria-label="Table">' +
|
|
11
|
-
'<table class="tiptap-table"><tbody>' +
|
|
12
|
+
'<table class="tiptap-table"><colgroup><col style="--tiptap-column-width: 50%;"><col style="--tiptap-column-width: 50%;"></colgroup><tbody>' +
|
|
12
13
|
'<tr><th><p>Port</p></th><th><p>Ships</p></th></tr>' +
|
|
13
14
|
'<tr><td><p>Chattogram</p></td><td><p>12</p></td></tr>' +
|
|
14
15
|
'</tbody></table></div>';
|
|
15
16
|
export const captionedTable = '<div class="table-responsive" role="region" tabindex="0" aria-label="Ships by port">' +
|
|
16
|
-
'<table class="tiptap-table"><caption>Ships by port</caption
|
|
17
|
+
'<table class="tiptap-table"><caption>Ships by port</caption>' +
|
|
18
|
+
'<colgroup><col style="--tiptap-column-width: 50%;"><col style="--tiptap-column-width: 50%;"></colgroup><tbody>' +
|
|
17
19
|
'<tr><th><p>Port</p></th><th><p>Ships</p></th></tr>' +
|
|
18
20
|
'<tr><td><p>Chattogram</p></td><td><p>12</p></td></tr>' +
|
|
19
21
|
'</tbody></table></div>';
|
|
22
|
+
// A table whose lines have moved, a row given a height and a cell a colour.
|
|
23
|
+
export const styledTable = '<div class="table-responsive" role="region" tabindex="0" aria-label="Table">' +
|
|
24
|
+
'<table class="tiptap-table"><colgroup><col style="--tiptap-column-width: 30.5%;"><col style="--tiptap-column-width: 69.5%;"></colgroup><tbody>' +
|
|
25
|
+
'<tr><th><p>Port</p></th><th><p>Ships</p></th></tr>' +
|
|
26
|
+
'<tr style="--tiptap-row-height: 2.5em;"><td style="background-color: var(--tiptap-highlight-yellow);"><p>Chattogram</p></td><td><p>12</p></td></tr>' +
|
|
27
|
+
'</tbody></table></div>';
|
|
20
28
|
export const video = '<div data-youtube-video=""><iframe width="640" height="480" allowfullscreen="true" ' +
|
|
21
29
|
'src="https://www.youtube.com/embed/M7lc1UVf-VE?start=90&rel=1" title="A talk"></iframe></div>';
|
|
22
30
|
export const size = '<p><span style="font-size: var(--lg-font-size);">Large</span> text</p>';
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { getStyleProperty } from '@tiptap/core';
|
|
2
|
+
import { TableCell as TiptapTableCell, TableHeader as TiptapTableHeader } from '@tiptap/extension-table';
|
|
3
|
+
/**
|
|
4
|
+
* A cell's `colwidth` holds the shares of its table's width the columns it
|
|
5
|
+
* spans take, in percent, rather than the pixels prosemirror-tables has it
|
|
6
|
+
* hold: prosemirror-tables keeps it in step as columns are added, deleted,
|
|
7
|
+
* merged and split. The table writes the shares in its `colgroup`, where
|
|
8
|
+
* they are read back from; a cell writes none of its own.
|
|
9
|
+
*/
|
|
10
|
+
const shares = {
|
|
11
|
+
colwidth: {
|
|
12
|
+
default: null,
|
|
13
|
+
parseHTML: (element) => sharesOf(element),
|
|
14
|
+
rendered: false
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const TableCell = TiptapTableCell.extend({
|
|
18
|
+
addAttributes() {
|
|
19
|
+
return { ...this.parent?.(), ...shares };
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
export const TableHeader = TiptapTableHeader.extend({
|
|
23
|
+
addAttributes() {
|
|
24
|
+
return { ...this.parent?.(), ...shares };
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
// A column's share as a body writes it, `--tiptap-column-width: 25%`.
|
|
28
|
+
const SHARE = /^(\d{1,3}(?:\.\d+)?)%$/;
|
|
29
|
+
// The shares of the columns a cell spans, from its table's `col`s, or none
|
|
30
|
+
// where its table gives none of them.
|
|
31
|
+
function sharesOf(cell) {
|
|
32
|
+
const table = cell.closest('table');
|
|
33
|
+
if (!table) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const columns = columnsOf(table);
|
|
37
|
+
const start = startOf(cell, table);
|
|
38
|
+
const span = spanOf(cell, 'colspan');
|
|
39
|
+
const spanned = columns.slice(start, start + span);
|
|
40
|
+
return start >= 0 && spanned.some((share) => share > 0)
|
|
41
|
+
? Array.from({ length: span }, (_, at) => spanned[at] ?? 0)
|
|
42
|
+
: null;
|
|
43
|
+
}
|
|
44
|
+
// Each column's share, a `col`'s `span` counted as that many columns, and 0
|
|
45
|
+
// for one written without a share.
|
|
46
|
+
function columnsOf(table) {
|
|
47
|
+
const group = Array.from(table.children).find((child) => child.localName === 'colgroup');
|
|
48
|
+
return Array.from(group?.children ?? []).flatMap((col) => {
|
|
49
|
+
const value = getStyleProperty(col, '--tiptap-column-width');
|
|
50
|
+
const share = value && SHARE.test(value) ? parseFloat(value) : 0;
|
|
51
|
+
return Array(spanOf(col, 'span')).fill(share <= 100 ? share : 0);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// The column a cell starts at, past the cells of rows above reaching down
|
|
55
|
+
// into its own.
|
|
56
|
+
function startOf(cell, table) {
|
|
57
|
+
const taken = [];
|
|
58
|
+
for (const [index, row] of rowsOf(table).entries()) {
|
|
59
|
+
let column = 0;
|
|
60
|
+
for (const each of cellsOf(row)) {
|
|
61
|
+
while (taken[index]?.has(column)) {
|
|
62
|
+
column += 1;
|
|
63
|
+
}
|
|
64
|
+
if (each === cell) {
|
|
65
|
+
return column;
|
|
66
|
+
}
|
|
67
|
+
const span = spanOf(each, 'colspan');
|
|
68
|
+
for (let below = 0; below < spanOf(each, 'rowspan'); below += 1) {
|
|
69
|
+
const set = (taken[index + below] ??= new Set());
|
|
70
|
+
for (let across = 0; across < span; across += 1) {
|
|
71
|
+
set.add(column + across);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
column += span;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
// A table's own rows, in its sections or in it, and none of a table inside
|
|
80
|
+
// one of its cells.
|
|
81
|
+
function rowsOf(table) {
|
|
82
|
+
return Array.from(table.children).flatMap((child) => child.localName === 'tr'
|
|
83
|
+
? [child]
|
|
84
|
+
: ['thead', 'tbody', 'tfoot'].includes(child.localName)
|
|
85
|
+
? Array.from(child.children).filter((row) => row.localName === 'tr')
|
|
86
|
+
: []);
|
|
87
|
+
}
|
|
88
|
+
function cellsOf(row) {
|
|
89
|
+
return Array.from(row.children).filter((cell) => ['td', 'th'].includes(cell.localName));
|
|
90
|
+
}
|
|
91
|
+
function spanOf(element, name) {
|
|
92
|
+
const span = parseInt(element.getAttribute(name) ?? '', 10);
|
|
93
|
+
return span > 0 ? span : 1;
|
|
94
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Node as ProseMirrorNode } from '@tiptap/pm/model';
|
|
2
|
+
import { Plugin, PluginKey, type Transaction } from '@tiptap/pm/state';
|
|
3
|
+
import { type EditorView } from '@tiptap/pm/view';
|
|
4
|
+
/**
|
|
5
|
+
* Lines of the table at `table` picked by a press: the line after the column
|
|
6
|
+
* `column` and the line under the row `row`, either none. `columns` and
|
|
7
|
+
* `rows` are the table's size as they were picked, beyond which a change to
|
|
8
|
+
* the table lets them go.
|
|
9
|
+
*/
|
|
10
|
+
interface Lines {
|
|
11
|
+
table: number;
|
|
12
|
+
columns: number;
|
|
13
|
+
rows: number;
|
|
14
|
+
column: number | null;
|
|
15
|
+
row: number | null;
|
|
16
|
+
}
|
|
17
|
+
interface Drawn {
|
|
18
|
+
shares?: number[];
|
|
19
|
+
height?: number | null;
|
|
20
|
+
}
|
|
21
|
+
interface SizingState {
|
|
22
|
+
over: {
|
|
23
|
+
column: boolean;
|
|
24
|
+
row: boolean;
|
|
25
|
+
} | null;
|
|
26
|
+
picked: Lines | null;
|
|
27
|
+
drawn: Drawn | null;
|
|
28
|
+
}
|
|
29
|
+
export declare const tableSizingKey: PluginKey<SizingState>;
|
|
30
|
+
export declare function tableSizing(): Plugin<SizingState>;
|
|
31
|
+
/**
|
|
32
|
+
* The share of a table's width each of its columns is drawn at, in percent
|
|
33
|
+
* at one decimal: what its cells give, and to a column none of them gives a
|
|
34
|
+
* width, the even share of the rest's. A table no line has moved has even
|
|
35
|
+
* shares.
|
|
36
|
+
*/
|
|
37
|
+
export declare function columnShares(table: ProseMirrorNode): number[];
|
|
38
|
+
/**
|
|
39
|
+
* The shares once the line after the column `line` moves by `by`, the
|
|
40
|
+
* columns either side trading what it moves, neither under its least; as
|
|
41
|
+
* they were where the two need more than they have between them.
|
|
42
|
+
*/
|
|
43
|
+
export declare function movedShares(shares: number[], line: number, by: number, least: number[]): number[];
|
|
44
|
+
export declare function setShares(tr: Transaction, table: ProseMirrorNode, tablePos: number, shares: number[]): Transaction;
|
|
45
|
+
/**
|
|
46
|
+
* The least share of its width each column of a drawn table can take: the
|
|
47
|
+
* table drawn as narrow as its words and padding let it, and put back before
|
|
48
|
+
* the page is painted.
|
|
49
|
+
*/
|
|
50
|
+
export declare function leastShares(table: HTMLTableElement): number[];
|
|
51
|
+
/**
|
|
52
|
+
* The least height a drawn row can take on the screen: its tallest cell's
|
|
53
|
+
* blocks with the cell's padding, and half the border under it, which a
|
|
54
|
+
* collapsed border gives each side. A cell reaching into rows below is left
|
|
55
|
+
* out.
|
|
56
|
+
*/
|
|
57
|
+
export declare function leastRowHeight(row: HTMLTableRowElement): number;
|
|
58
|
+
/**
|
|
59
|
+
* A row's height in em once drawn `height` pixels tall on the screen, in
|
|
60
|
+
* text `em` pixels high there: none where its text already fills that, and
|
|
61
|
+
* at most the highest a body writes.
|
|
62
|
+
*/
|
|
63
|
+
export declare function rowHeight(height: number, least: number, em: number): number | null;
|
|
64
|
+
/**
|
|
65
|
+
* How many pixels of the screen an element's own pixel takes, which a
|
|
66
|
+
* transform on a box around it changes: the screen's pixels are what a
|
|
67
|
+
* pointer moves by, and its own what its styles are written in.
|
|
68
|
+
*/
|
|
69
|
+
export declare function scaleOf(element: HTMLElement): number;
|
|
70
|
+
export declare function emOf(row: HTMLTableRowElement): number;
|
|
71
|
+
export declare function rowPosition(table: ProseMirrorNode, tablePos: number, index: number): number;
|
|
72
|
+
export declare function tableElement(view: EditorView, pos: number): HTMLTableElement | null;
|
|
73
|
+
export {};
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
2
|
+
import { CellSelection, TableMap } from '@tiptap/pm/tables';
|
|
3
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
4
|
+
/**
|
|
5
|
+
* A table's lines, picked and dragged to size its columns and its rows. A
|
|
6
|
+
* press within `REACH` of the line after a column, or of the line under a
|
|
7
|
+
* row, picks it, and a drag in the same press moves it: the columns either
|
|
8
|
+
* side trade their shares of the table's width, and the row takes a height in
|
|
9
|
+
* em of its text. A press where the two meet picks both. A column stops at
|
|
10
|
+
* the least its words and padding take, and a row at its text, where it keeps
|
|
11
|
+
* no height of its own. The table's left, right and top edges are no lines.
|
|
12
|
+
*
|
|
13
|
+
* The classes it gives, which the page's stylesheet draws, are
|
|
14
|
+
* `tiptap-cursor-cell` on the cell the cursor is in; `tiptap-handle-right`
|
|
15
|
+
* and `tiptap-handle-bottom` on the cells along a picked line, and a
|
|
16
|
+
* `tiptap-handle-dot` where two picked lines meet; and
|
|
17
|
+
* `tiptap-over-column-line` and `tiptap-over-row-line` on the editor while
|
|
18
|
+
* the pointer is over a line.
|
|
19
|
+
*/
|
|
20
|
+
// How near a line, either side of it, a press picks it.
|
|
21
|
+
const REACH = 4;
|
|
22
|
+
// How far a press moves before it is a drag rather than a click.
|
|
23
|
+
const SLOP = 3;
|
|
24
|
+
// The highest a row's height goes, in em, as a body writes it: two digits.
|
|
25
|
+
const HIGHEST = 99.9;
|
|
26
|
+
export const tableSizingKey = new PluginKey('tableSizing');
|
|
27
|
+
// A drag in progress in an editor, which Escape cancels.
|
|
28
|
+
const drags = new WeakMap();
|
|
29
|
+
export function tableSizing() {
|
|
30
|
+
return new Plugin({
|
|
31
|
+
key: tableSizingKey,
|
|
32
|
+
state: {
|
|
33
|
+
init: () => ({ over: null, picked: null, drawn: null }),
|
|
34
|
+
apply(tr, value, _, state) {
|
|
35
|
+
const meta = tr.getMeta(tableSizingKey);
|
|
36
|
+
const next = meta ? { ...value, ...meta } : value;
|
|
37
|
+
return tr.docChanged && next.picked
|
|
38
|
+
? { ...next, picked: mapped(next.picked, tr, state.doc) }
|
|
39
|
+
: next;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
props: {
|
|
43
|
+
attributes(state) {
|
|
44
|
+
const over = tableSizingKey.getState(state)?.over;
|
|
45
|
+
const classes = [
|
|
46
|
+
over?.column ? 'tiptap-over-column-line' : '',
|
|
47
|
+
over?.row ? 'tiptap-over-row-line' : ''
|
|
48
|
+
].filter(Boolean);
|
|
49
|
+
return classes.length ? { class: classes.join(' ') } : {};
|
|
50
|
+
},
|
|
51
|
+
decorations: (state) => decorationsOf(state),
|
|
52
|
+
handleKeyDown(view, event) {
|
|
53
|
+
if (event.key !== 'Escape') {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const drag = drags.get(view);
|
|
57
|
+
if (drag) {
|
|
58
|
+
drag.cancel();
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (tableSizingKey.getState(view.state)?.picked) {
|
|
62
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { picked: null }));
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
},
|
|
67
|
+
handleDOMEvents: {
|
|
68
|
+
mousemove(view, event) {
|
|
69
|
+
if (!drags.has(view)) {
|
|
70
|
+
hover(view, linesAt(view, event));
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
},
|
|
74
|
+
mouseleave(view) {
|
|
75
|
+
if (!drags.has(view)) {
|
|
76
|
+
hover(view, null);
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
},
|
|
80
|
+
mousedown: (view, event) => press(view, event)
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
view: (view) => ({ destroy: () => drags.get(view)?.cancel() })
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The share of a table's width each of its columns is drawn at, in percent
|
|
88
|
+
* at one decimal: what its cells give, and to a column none of them gives a
|
|
89
|
+
* width, the even share of the rest's. A table no line has moved has even
|
|
90
|
+
* shares.
|
|
91
|
+
*/
|
|
92
|
+
export function columnShares(table) {
|
|
93
|
+
const map = TableMap.get(table);
|
|
94
|
+
const given = Array.from({ length: map.width }, (_, column) => {
|
|
95
|
+
for (let row = 0; row < map.height; row += 1) {
|
|
96
|
+
const pos = map.map[row * map.width + column];
|
|
97
|
+
const share = table.nodeAt(pos)?.attrs['colwidth']?.[column - map.colCount(pos)];
|
|
98
|
+
if (share) {
|
|
99
|
+
return share;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
});
|
|
104
|
+
const known = given.filter((share) => share !== null);
|
|
105
|
+
const even = known.length ? sum(known) / known.length : 1;
|
|
106
|
+
const weights = given.map((share) => share ?? even);
|
|
107
|
+
const total = sum(weights);
|
|
108
|
+
return weights.map((weight) => tenth((weight / total) * 100));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The shares once the line after the column `line` moves by `by`, the
|
|
112
|
+
* columns either side trading what it moves, neither under its least; as
|
|
113
|
+
* they were where the two need more than they have between them.
|
|
114
|
+
*/
|
|
115
|
+
export function movedShares(shares, line, by, least) {
|
|
116
|
+
const total = shares[line] + shares[line + 1];
|
|
117
|
+
const low = Math.ceil((least[line] ?? 0) * 10) / 10;
|
|
118
|
+
const high = Math.floor((total - (least[line + 1] ?? 0)) * 10) / 10;
|
|
119
|
+
if (low > high) {
|
|
120
|
+
return shares;
|
|
121
|
+
}
|
|
122
|
+
const left = Math.min(high, Math.max(low, tenth(shares[line] + by)));
|
|
123
|
+
return shares.map((share, at) => at === line ? left : at === line + 1 ? tenth(total - left) : share);
|
|
124
|
+
}
|
|
125
|
+
// Gives every cell of the table at `tablePos` the shares of the columns it
|
|
126
|
+
// spans.
|
|
127
|
+
export function setShares(tr, table, tablePos, shares) {
|
|
128
|
+
const map = TableMap.get(table);
|
|
129
|
+
for (const pos of new Set(map.map)) {
|
|
130
|
+
const cell = table.nodeAt(pos);
|
|
131
|
+
const start = map.colCount(pos);
|
|
132
|
+
tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + pos), undefined, {
|
|
133
|
+
...cell.attrs,
|
|
134
|
+
colwidth: shares.slice(start, start + cell.attrs['colspan'])
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return tr;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The least share of its width each column of a drawn table can take: the
|
|
141
|
+
* table drawn as narrow as its words and padding let it, and put back before
|
|
142
|
+
* the page is painted.
|
|
143
|
+
*/
|
|
144
|
+
export function leastShares(table) {
|
|
145
|
+
const width = table.getBoundingClientRect().width;
|
|
146
|
+
if (!width) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
const before = table.style.width;
|
|
150
|
+
table.style.width = '0px';
|
|
151
|
+
const least = Array.from(table.querySelectorAll(':scope > colgroup > col'), (col) => (col.getBoundingClientRect().width / width) * 100);
|
|
152
|
+
table.style.width = before;
|
|
153
|
+
return least;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The least height a drawn row can take on the screen: its tallest cell's
|
|
157
|
+
* blocks with the cell's padding, and half the border under it, which a
|
|
158
|
+
* collapsed border gives each side. A cell reaching into rows below is left
|
|
159
|
+
* out.
|
|
160
|
+
*/
|
|
161
|
+
export function leastRowHeight(row) {
|
|
162
|
+
const top = row.getBoundingClientRect().top;
|
|
163
|
+
const scale = scaleOf(row);
|
|
164
|
+
let least = 0;
|
|
165
|
+
for (const cell of Array.from(row.cells)) {
|
|
166
|
+
if (cell.rowSpan !== 1) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const style = getComputedStyle(cell);
|
|
170
|
+
const blocks = Array.from(cell.children).filter((child) => getComputedStyle(child).position !== 'absolute');
|
|
171
|
+
const last = blocks.at(-1);
|
|
172
|
+
const bottom = last
|
|
173
|
+
? last.getBoundingClientRect().bottom +
|
|
174
|
+
parseFloat(getComputedStyle(last).marginBottom)
|
|
175
|
+
: cell.getBoundingClientRect().top + parseFloat(style.paddingTop);
|
|
176
|
+
least = Math.max(least, bottom -
|
|
177
|
+
top +
|
|
178
|
+
(parseFloat(style.paddingBottom) +
|
|
179
|
+
parseFloat(style.borderBottomWidth) / 2) *
|
|
180
|
+
scale);
|
|
181
|
+
}
|
|
182
|
+
return least;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* A row's height in em once drawn `height` pixels tall on the screen, in
|
|
186
|
+
* text `em` pixels high there: none where its text already fills that, and
|
|
187
|
+
* at most the highest a body writes.
|
|
188
|
+
*/
|
|
189
|
+
export function rowHeight(height, least, em) {
|
|
190
|
+
return height <= least + 0.5 ? null : Math.min(tenth(height / em), HIGHEST);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* How many pixels of the screen an element's own pixel takes, which a
|
|
194
|
+
* transform on a box around it changes: the screen's pixels are what a
|
|
195
|
+
* pointer moves by, and its own what its styles are written in.
|
|
196
|
+
*/
|
|
197
|
+
export function scaleOf(element) {
|
|
198
|
+
return element.offsetHeight
|
|
199
|
+
? element.getBoundingClientRect().height / element.offsetHeight
|
|
200
|
+
: 1;
|
|
201
|
+
}
|
|
202
|
+
// The height of the text in a row, in the screen's pixels.
|
|
203
|
+
export function emOf(row) {
|
|
204
|
+
return parseFloat(getComputedStyle(row).fontSize) * scaleOf(row);
|
|
205
|
+
}
|
|
206
|
+
// Where the row `index` of the table at `tablePos` starts.
|
|
207
|
+
export function rowPosition(table, tablePos, index) {
|
|
208
|
+
let pos = tablePos + 1;
|
|
209
|
+
for (let row = 0; row < index; row += 1) {
|
|
210
|
+
pos += table.child(row).nodeSize;
|
|
211
|
+
}
|
|
212
|
+
return pos;
|
|
213
|
+
}
|
|
214
|
+
// The `table` element the editor draws for the table at `pos`, inside the
|
|
215
|
+
// box it scrolls in.
|
|
216
|
+
export function tableElement(view, pos) {
|
|
217
|
+
const box = view.nodeDOM(pos);
|
|
218
|
+
return box instanceof HTMLElement
|
|
219
|
+
? box.querySelector(':scope > table')
|
|
220
|
+
: null;
|
|
221
|
+
}
|
|
222
|
+
function hover(view, lines) {
|
|
223
|
+
const over = lines && { column: lines.column !== null, row: lines.row !== null };
|
|
224
|
+
const before = tableSizingKey.getState(view.state)?.over ?? null;
|
|
225
|
+
if (over?.column !== before?.column || over?.row !== before?.row) {
|
|
226
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { over }));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function press(view, event) {
|
|
230
|
+
const modified = event.shiftKey || event.ctrlKey || event.metaKey || event.altKey;
|
|
231
|
+
const lines = event.button === 0 && !modified && !drags.has(view)
|
|
232
|
+
? linesAt(view, event)
|
|
233
|
+
: null;
|
|
234
|
+
if (!lines) {
|
|
235
|
+
if (tableSizingKey.getState(view.state)?.picked) {
|
|
236
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { picked: null }));
|
|
237
|
+
}
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
view.focus();
|
|
242
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { picked: lines }));
|
|
243
|
+
drag(view, event, lines);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
// Follows the pointer until it is released, drawing the lines where it takes
|
|
247
|
+
// them, and writes them there as one change.
|
|
248
|
+
function drag(view, start, lines) {
|
|
249
|
+
const table = view.state.doc.nodeAt(lines.table);
|
|
250
|
+
const element = tableElement(view, lines.table);
|
|
251
|
+
const width = element?.getBoundingClientRect().width ?? 0;
|
|
252
|
+
const shares = columnShares(table);
|
|
253
|
+
const least = lines.column !== null && element ? leastShares(element) : [];
|
|
254
|
+
const row = lines.row === null
|
|
255
|
+
? null
|
|
256
|
+
: view.nodeDOM(rowPosition(table, lines.table, lines.row));
|
|
257
|
+
const height = row?.getBoundingClientRect().height ?? 0;
|
|
258
|
+
const leastHeight = row ? leastRowHeight(row) : 0;
|
|
259
|
+
const em = row ? emOf(row) : 0;
|
|
260
|
+
const window = view.dom.ownerDocument.defaultView;
|
|
261
|
+
let drawn = null;
|
|
262
|
+
const move = (event) => {
|
|
263
|
+
const x = event.clientX - start.clientX;
|
|
264
|
+
const y = event.clientY - start.clientY;
|
|
265
|
+
if (!drawn && Math.hypot(x, y) < SLOP) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
drawn = {
|
|
269
|
+
...(lines.column !== null &&
|
|
270
|
+
width && {
|
|
271
|
+
shares: movedShares(shares, lines.column, (x / width) * 100, least)
|
|
272
|
+
}),
|
|
273
|
+
...(row && em && { height: rowHeight(height + y, leastHeight, em) })
|
|
274
|
+
};
|
|
275
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { drawn }));
|
|
276
|
+
};
|
|
277
|
+
const stop = () => {
|
|
278
|
+
window.removeEventListener('mousemove', move);
|
|
279
|
+
window.removeEventListener('mouseup', release);
|
|
280
|
+
drags.delete(view);
|
|
281
|
+
};
|
|
282
|
+
const release = () => {
|
|
283
|
+
stop();
|
|
284
|
+
if (drawn) {
|
|
285
|
+
view.dispatch(written(view.state, lines, drawn).setMeta(tableSizingKey, {
|
|
286
|
+
drawn: null
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const cancel = () => {
|
|
291
|
+
stop();
|
|
292
|
+
if (drawn) {
|
|
293
|
+
view.dispatch(view.state.tr.setMeta(tableSizingKey, { drawn: null }));
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
window.addEventListener('mousemove', move);
|
|
297
|
+
window.addEventListener('mouseup', release);
|
|
298
|
+
drags.set(view, { cancel });
|
|
299
|
+
}
|
|
300
|
+
function written(state, lines, drawn) {
|
|
301
|
+
const tr = state.tr;
|
|
302
|
+
const table = state.doc.nodeAt(lines.table);
|
|
303
|
+
if (!table) {
|
|
304
|
+
return tr;
|
|
305
|
+
}
|
|
306
|
+
if (drawn.shares) {
|
|
307
|
+
setShares(tr, table, lines.table, drawn.shares);
|
|
308
|
+
}
|
|
309
|
+
if (drawn.height !== undefined && lines.row !== null) {
|
|
310
|
+
tr.setNodeMarkup(tr.mapping.map(rowPosition(table, lines.table, lines.row)), undefined, { ...table.child(lines.row).attrs, height: drawn.height });
|
|
311
|
+
}
|
|
312
|
+
return tr;
|
|
313
|
+
}
|
|
314
|
+
// The lines a pointer is within reach of, along the edges of the cell it is
|
|
315
|
+
// over: none of the table's left, right or top edges.
|
|
316
|
+
function linesAt(view, event) {
|
|
317
|
+
if (!view.editable || !(event.target instanceof Element)) {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
const element = event.target.closest('td, th');
|
|
321
|
+
if (!element || !view.dom.contains(element)) {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
let $pos;
|
|
325
|
+
try {
|
|
326
|
+
$pos = view.state.doc.resolve(view.posAtDOM(element, 0));
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
let depth = $pos.depth;
|
|
332
|
+
while (depth > 0 && !isCell($pos.node(depth))) {
|
|
333
|
+
depth -= 1;
|
|
334
|
+
}
|
|
335
|
+
if (depth < 3) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
const tablePos = $pos.before(depth - 2);
|
|
339
|
+
const table = $pos.node(depth - 2);
|
|
340
|
+
const map = TableMap.get(table);
|
|
341
|
+
const cell = map.findCell($pos.before(depth) - tablePos - 1);
|
|
342
|
+
const box = element.getBoundingClientRect();
|
|
343
|
+
const { clientX: x, clientY: y } = event;
|
|
344
|
+
const column = Math.abs(x - box.right) <= REACH && cell.right < map.width
|
|
345
|
+
? cell.right - 1
|
|
346
|
+
: Math.abs(x - box.left) <= REACH && cell.left > 0
|
|
347
|
+
? cell.left - 1
|
|
348
|
+
: null;
|
|
349
|
+
const row = Math.abs(y - box.bottom) <= REACH
|
|
350
|
+
? cell.bottom - 1
|
|
351
|
+
: Math.abs(y - box.top) <= REACH && cell.top > 0
|
|
352
|
+
? cell.top - 1
|
|
353
|
+
: null;
|
|
354
|
+
return column === null && row === null
|
|
355
|
+
? null
|
|
356
|
+
: { table: tablePos, columns: map.width, rows: map.height, column, row };
|
|
357
|
+
}
|
|
358
|
+
// The lines picked, where the table they are in is still there and still of
|
|
359
|
+
// the size they were picked in.
|
|
360
|
+
function mapped(lines, tr, doc) {
|
|
361
|
+
const { pos, deleted } = tr.mapping.mapResult(lines.table, 1);
|
|
362
|
+
const table = deleted ? null : doc.nodeAt(pos);
|
|
363
|
+
if (table?.type.spec['tableRole'] !== 'table') {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
const map = TableMap.get(table);
|
|
367
|
+
return map.width === lines.columns && map.height === lines.rows
|
|
368
|
+
? { ...lines, table: pos }
|
|
369
|
+
: null;
|
|
370
|
+
}
|
|
371
|
+
function decorationsOf(state) {
|
|
372
|
+
const { picked, drawn } = tableSizingKey.getState(state);
|
|
373
|
+
const decorations = [];
|
|
374
|
+
const cursor = cursorCell(state);
|
|
375
|
+
if (cursor) {
|
|
376
|
+
decorations.push(Decoration.node(cursor.from, cursor.to, { class: 'tiptap-cursor-cell' }));
|
|
377
|
+
}
|
|
378
|
+
const table = picked && state.doc.nodeAt(picked.table);
|
|
379
|
+
if (!picked || !table) {
|
|
380
|
+
return DecorationSet.create(state.doc, decorations);
|
|
381
|
+
}
|
|
382
|
+
const map = TableMap.get(table);
|
|
383
|
+
const start = picked.table + 1;
|
|
384
|
+
for (const pos of new Set(map.map)) {
|
|
385
|
+
const cell = map.findCell(pos);
|
|
386
|
+
const classes = [
|
|
387
|
+
cell.right - 1 === picked.column ? 'tiptap-handle-right' : '',
|
|
388
|
+
cell.bottom - 1 === picked.row ? 'tiptap-handle-bottom' : ''
|
|
389
|
+
].filter(Boolean);
|
|
390
|
+
if (classes.length) {
|
|
391
|
+
decorations.push(Decoration.node(start + pos, start + pos + table.nodeAt(pos).nodeSize, {
|
|
392
|
+
class: classes.join(' ')
|
|
393
|
+
}));
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (picked.column !== null && picked.row !== null) {
|
|
397
|
+
const dot = meeting(map, picked.column, picked.row);
|
|
398
|
+
if (dot) {
|
|
399
|
+
decorations.push(Decoration.widget(start + dot.pos + 1, () => dotElement(dot.corner), {
|
|
400
|
+
side: -1,
|
|
401
|
+
key: `tiptap-handle-dot-${dot.corner}`,
|
|
402
|
+
ignoreSelection: true
|
|
403
|
+
}));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (drawn?.shares) {
|
|
407
|
+
decorations.push(Decoration.node(picked.table, picked.table + table.nodeSize, {}, {
|
|
408
|
+
shares: drawn.shares
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
if (drawn && drawn.height !== undefined && picked.row !== null) {
|
|
412
|
+
const pos = rowPosition(table, picked.table, picked.row);
|
|
413
|
+
decorations.push(Decoration.node(pos, pos + table.child(picked.row).nodeSize, {
|
|
414
|
+
style: `height: ${drawn.height === null ? 'auto' : `${drawn.height}em`}`
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
return DecorationSet.create(state.doc, decorations);
|
|
418
|
+
}
|
|
419
|
+
// The cell the cursor is in, the innermost where a table is in a cell, and
|
|
420
|
+
// none while cells are selected.
|
|
421
|
+
function cursorCell(state) {
|
|
422
|
+
const { selection } = state;
|
|
423
|
+
if (selection instanceof CellSelection) {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
const { $head } = selection;
|
|
427
|
+
for (let depth = $head.depth; depth > 0; depth -= 1) {
|
|
428
|
+
if (isCell($head.node(depth))) {
|
|
429
|
+
return { from: $head.before(depth), to: $head.after(depth) };
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
// A cell with a corner where the line after `column` meets the line under
|
|
435
|
+
// `row`, and which corner: where a cell spans one of the lines, another of
|
|
436
|
+
// the four around the point has it.
|
|
437
|
+
function meeting(map, column, row) {
|
|
438
|
+
const corners = [
|
|
439
|
+
{ row, column, corner: 'bottom-right' },
|
|
440
|
+
{ row, column: column + 1, corner: 'bottom-left' },
|
|
441
|
+
{ row: row + 1, column, corner: 'top-right' },
|
|
442
|
+
{ row: row + 1, column: column + 1, corner: 'top-left' }
|
|
443
|
+
];
|
|
444
|
+
for (const each of corners) {
|
|
445
|
+
if (each.row >= map.height || each.column >= map.width) {
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const pos = map.map[each.row * map.width + each.column];
|
|
449
|
+
const cell = map.findCell(pos);
|
|
450
|
+
const [vertical, horizontal] = each.corner.split('-');
|
|
451
|
+
const atRow = vertical === 'bottom' ? cell.bottom - 1 === row : cell.top === row + 1;
|
|
452
|
+
const atColumn = horizontal === 'right'
|
|
453
|
+
? cell.right - 1 === column
|
|
454
|
+
: cell.left === column + 1;
|
|
455
|
+
if (atRow && atColumn) {
|
|
456
|
+
return { pos, corner: each.corner };
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
// The dot where picked lines meet, on the corner of the cell it is drawn in.
|
|
462
|
+
function dotElement(corner) {
|
|
463
|
+
const dot = document.createElement('span');
|
|
464
|
+
const [vertical, horizontal] = corner.split('-');
|
|
465
|
+
dot.className = 'tiptap-handle-dot';
|
|
466
|
+
dot.contentEditable = 'false';
|
|
467
|
+
dot.setAttribute('aria-hidden', 'true');
|
|
468
|
+
dot.style.setProperty(vertical, '0');
|
|
469
|
+
dot.style.setProperty(horizontal, '0');
|
|
470
|
+
dot.style.translate = `${horizontal === 'right' ? '50%' : '-50%'} ${vertical === 'bottom' ? '50%' : '-50%'}`;
|
|
471
|
+
return dot;
|
|
472
|
+
}
|
|
473
|
+
function isCell(node) {
|
|
474
|
+
const role = node.type.spec['tableRole'];
|
|
475
|
+
return role === 'cell' || role === 'header_cell';
|
|
476
|
+
}
|
|
477
|
+
function sum(values) {
|
|
478
|
+
return values.reduce((total, value) => total + value, 0);
|
|
479
|
+
}
|
|
480
|
+
function tenth(value) {
|
|
481
|
+
return Math.round(value * 10) / 10;
|
|
482
|
+
}
|
package/dist/table.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ export interface TableOptions extends TiptapTableOptions {
|
|
|
6
6
|
* caption to call it by. Without one, the table scrolls in a plain box.
|
|
7
7
|
*/
|
|
8
8
|
region: string | null;
|
|
9
|
+
/**
|
|
10
|
+
* The colours a cell's background may take, as a body writes them:
|
|
11
|
+
* `var(--tiptap-highlight-yellow)`. Any colour where null.
|
|
12
|
+
*/
|
|
13
|
+
cellColors: string[] | null;
|
|
9
14
|
}
|
|
10
15
|
declare module '@tiptap/core' {
|
|
11
16
|
interface Commands<ReturnType> {
|
|
@@ -17,16 +22,45 @@ declare module '@tiptap/core' {
|
|
|
17
22
|
setTableCaption: (caption: string) => ReturnType;
|
|
18
23
|
unsetTableCaption: () => ReturnType;
|
|
19
24
|
};
|
|
25
|
+
tableSizes: {
|
|
26
|
+
/**
|
|
27
|
+
* Gives the cell the cursor is in, or the cells selected, `width`
|
|
28
|
+
* percent of the table's width, the column after them — before them,
|
|
29
|
+
* for the last — taking the difference. A width either column cannot
|
|
30
|
+
* take, narrower than its words or its padding, is set to the nearest
|
|
31
|
+
* it can.
|
|
32
|
+
*/
|
|
33
|
+
setColumnWidth: (width: number) => ReturnType;
|
|
34
|
+
/**
|
|
35
|
+
* Gives the row under the cursor's cell a height of `height` em. None,
|
|
36
|
+
* or a height its text already fills, takes its height off.
|
|
37
|
+
*/
|
|
38
|
+
setRowHeight: (height: number | null) => ReturnType;
|
|
39
|
+
};
|
|
40
|
+
cellBackground: {
|
|
41
|
+
/**
|
|
42
|
+
* Colours the cell the cursor is in, or every cell selected, with one of
|
|
43
|
+
* the table's `cellColors`.
|
|
44
|
+
*/
|
|
45
|
+
setCellBackground: (color: string) => ReturnType;
|
|
46
|
+
unsetCellBackground: () => ReturnType;
|
|
47
|
+
};
|
|
20
48
|
}
|
|
21
49
|
}
|
|
22
50
|
/**
|
|
23
51
|
* Tiptap's table, written inside a box it scrolls in when it is too wide for
|
|
24
|
-
* the page
|
|
25
|
-
*
|
|
26
|
-
*
|
|
52
|
+
* the page. The editor draws its own copy without the region's tab stop:
|
|
53
|
+
* inside the editable area, one would take the focus from the editor whenever
|
|
54
|
+
* a cell is clicked.
|
|
27
55
|
*
|
|
28
56
|
* A table's caption is a line of plain text the table holds, set by a command
|
|
29
57
|
* rather than typed in place: ProseMirror's tables read every child of a table
|
|
30
58
|
* as a row. It is written first in the table, and names the region.
|
|
59
|
+
*
|
|
60
|
+
* A column's width is its share of the table's, written in the table's
|
|
61
|
+
* `colgroup` as `--tiptap-column-width`, and a row's height is in em of its
|
|
62
|
+
* text, written as `--tiptap-row-height`: the page's stylesheet reads both.
|
|
63
|
+
* Its lines are dragged to set them (`table-sizing.ts`), and the commands
|
|
64
|
+
* set them too.
|
|
31
65
|
*/
|
|
32
66
|
export declare const Table: import("@tiptap/core").Node<TableOptions, any>;
|
package/dist/table.js
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
import { findParentNode, mergeAttributes } from '@tiptap/core';
|
|
1
|
+
import { findParentNode, getStyleProperty, mergeAttributes } from '@tiptap/core';
|
|
2
|
+
import { isInTable, selectedRect, setCellAttr } from '@tiptap/pm/tables';
|
|
2
3
|
import { Table as TiptapTable } from '@tiptap/extension-table';
|
|
4
|
+
import { ifListed } from './listed.js';
|
|
5
|
+
import { columnShares, emOf, leastRowHeight, leastShares, movedShares, rowHeight, rowPosition, setShares, tableElement, tableSizing } from './table-sizing.js';
|
|
3
6
|
/**
|
|
4
7
|
* Tiptap's table, written inside a box it scrolls in when it is too wide for
|
|
5
|
-
* the page
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
+
* the page. The editor draws its own copy without the region's tab stop:
|
|
9
|
+
* inside the editable area, one would take the focus from the editor whenever
|
|
10
|
+
* a cell is clicked.
|
|
8
11
|
*
|
|
9
12
|
* A table's caption is a line of plain text the table holds, set by a command
|
|
10
13
|
* rather than typed in place: ProseMirror's tables read every child of a table
|
|
11
14
|
* as a row. It is written first in the table, and names the region.
|
|
15
|
+
*
|
|
16
|
+
* A column's width is its share of the table's, written in the table's
|
|
17
|
+
* `colgroup` as `--tiptap-column-width`, and a row's height is in em of its
|
|
18
|
+
* text, written as `--tiptap-row-height`: the page's stylesheet reads both.
|
|
19
|
+
* Its lines are dragged to set them (`table-sizing.ts`), and the commands
|
|
20
|
+
* set them too.
|
|
12
21
|
*/
|
|
13
22
|
export const Table = TiptapTable.extend({
|
|
14
23
|
addOptions() {
|
|
15
24
|
return {
|
|
16
25
|
...this.parent(),
|
|
17
26
|
HTMLAttributes: { class: 'tiptap-table' },
|
|
18
|
-
region: null
|
|
27
|
+
region: null,
|
|
28
|
+
cellColors: null
|
|
19
29
|
};
|
|
20
30
|
},
|
|
21
31
|
addAttributes() {
|
|
@@ -28,6 +38,36 @@ export const Table = TiptapTable.extend({
|
|
|
28
38
|
}
|
|
29
39
|
};
|
|
30
40
|
},
|
|
41
|
+
addGlobalAttributes() {
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
types: ['tableCell', 'tableHeader'],
|
|
45
|
+
attributes: {
|
|
46
|
+
backgroundColor: {
|
|
47
|
+
default: null,
|
|
48
|
+
parseHTML: (element) => ifListed(getStyleProperty(element, 'background-color'), this.options.cellColors),
|
|
49
|
+
renderHTML: (attributes) => attributes['backgroundColor']
|
|
50
|
+
? {
|
|
51
|
+
style: `background-color: ${attributes['backgroundColor']}`
|
|
52
|
+
}
|
|
53
|
+
: {}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
types: ['tableRow'],
|
|
59
|
+
attributes: {
|
|
60
|
+
height: {
|
|
61
|
+
default: null,
|
|
62
|
+
parseHTML: (element) => heightOf(element),
|
|
63
|
+
renderHTML: (attributes) => attributes['height']
|
|
64
|
+
? { style: `--tiptap-row-height: ${attributes['height']}em` }
|
|
65
|
+
: {}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
},
|
|
31
71
|
// A caption is read as the table's, and read as content would make a row.
|
|
32
72
|
parseHTML() {
|
|
33
73
|
return [...(this.parent?.() ?? []), { tag: 'caption', ignore: true }];
|
|
@@ -45,12 +85,20 @@ export const Table = TiptapTable.extend({
|
|
|
45
85
|
'table',
|
|
46
86
|
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
|
47
87
|
...(caption ? [['caption', caption]] : []),
|
|
88
|
+
[
|
|
89
|
+
'colgroup',
|
|
90
|
+
...columnShares(node).map((share) => [
|
|
91
|
+
'col',
|
|
92
|
+
{ style: `--tiptap-column-width: ${share}%;` }
|
|
93
|
+
])
|
|
94
|
+
],
|
|
48
95
|
['tbody', 0]
|
|
49
96
|
]
|
|
50
97
|
];
|
|
51
98
|
},
|
|
52
99
|
addNodeView() {
|
|
53
|
-
return ({ node, HTMLAttributes }) => {
|
|
100
|
+
return ({ node, HTMLAttributes, decorations }) => {
|
|
101
|
+
let drawn = node;
|
|
54
102
|
const dom = document.createElement('div');
|
|
55
103
|
dom.className = 'table-responsive';
|
|
56
104
|
const table = dom.appendChild(document.createElement('table'));
|
|
@@ -64,8 +112,38 @@ export const Table = TiptapTable.extend({
|
|
|
64
112
|
element.textContent = caption;
|
|
65
113
|
element.setAttribute('contenteditable', 'false');
|
|
66
114
|
}
|
|
115
|
+
const columns = table.appendChild(document.createElement('colgroup'));
|
|
67
116
|
const contentDOM = table.appendChild(document.createElement('tbody'));
|
|
68
|
-
|
|
117
|
+
// The shares a drag draws ahead of the table's own.
|
|
118
|
+
const draw = (shown, decorations) => {
|
|
119
|
+
const shares = decorations.find((decoration) => decoration.spec['shares'])?.spec['shares'] ?? columnShares(shown);
|
|
120
|
+
while (columns.children.length > shares.length) {
|
|
121
|
+
columns.lastElementChild.remove();
|
|
122
|
+
}
|
|
123
|
+
shares.forEach((share, at) => {
|
|
124
|
+
const col = columns.children[at] ??
|
|
125
|
+
columns.appendChild(document.createElement('col'));
|
|
126
|
+
col.style.setProperty('--tiptap-column-width', `${share}%`);
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
draw(node, decorations);
|
|
130
|
+
return {
|
|
131
|
+
dom,
|
|
132
|
+
contentDOM,
|
|
133
|
+
update: (updated, decorations) => {
|
|
134
|
+
if (updated.type !== drawn.type ||
|
|
135
|
+
updated.attrs['caption'] !== drawn.attrs['caption']) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
drawn = updated;
|
|
139
|
+
draw(updated, decorations);
|
|
140
|
+
return true;
|
|
141
|
+
},
|
|
142
|
+
// The box, the table, its caption and its columns are drawn here, and
|
|
143
|
+
// what changes them, a drag's shares among it, is none of the text's.
|
|
144
|
+
ignoreMutation: (mutation) => mutation.type !== 'selection' &&
|
|
145
|
+
!contentDOM.contains(mutation.target)
|
|
146
|
+
};
|
|
69
147
|
};
|
|
70
148
|
},
|
|
71
149
|
addCommands() {
|
|
@@ -85,8 +163,56 @@ export const Table = TiptapTable.extend({
|
|
|
85
163
|
return {
|
|
86
164
|
...this.parent?.(),
|
|
87
165
|
setTableCaption: (text) => caption(captionText(text)),
|
|
88
|
-
unsetTableCaption: () => caption(null)
|
|
166
|
+
unsetTableCaption: () => caption(null),
|
|
167
|
+
setColumnWidth: (width) => ({ state, tr, dispatch, view }) => {
|
|
168
|
+
if (!isInTable(state)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const { map, table, tableStart, left, right } = selectedRect(state);
|
|
172
|
+
if (map.width < 2) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
const shares = columnShares(table);
|
|
176
|
+
const last = right === map.width;
|
|
177
|
+
const now = shares
|
|
178
|
+
.slice(left, right)
|
|
179
|
+
.reduce((total, share) => total + share, 0);
|
|
180
|
+
const element = view ? tableElement(view, tableStart - 1) : null;
|
|
181
|
+
if (dispatch) {
|
|
182
|
+
setShares(tr, table, tableStart - 1, movedShares(shares, last ? left - 1 : right - 1, last ? now - width : width - now, element ? leastShares(element) : []));
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
},
|
|
186
|
+
setRowHeight: (height) => ({ state, tr, dispatch, view }) => {
|
|
187
|
+
if (!isInTable(state)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
const { table, tableStart, bottom } = selectedRect(state);
|
|
191
|
+
const pos = rowPosition(table, tableStart - 1, bottom - 1);
|
|
192
|
+
const row = view?.nodeDOM(pos);
|
|
193
|
+
const drawn = row instanceof HTMLTableRowElement && row.isConnected;
|
|
194
|
+
const em = drawn ? emOf(row) : 0;
|
|
195
|
+
const least = drawn && em ? leastRowHeight(row) : 0;
|
|
196
|
+
if (dispatch) {
|
|
197
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
198
|
+
...table.child(bottom - 1).attrs,
|
|
199
|
+
height: height && height > 0
|
|
200
|
+
? rowHeight(height * (em || 1), least, em || 1)
|
|
201
|
+
: null
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
},
|
|
206
|
+
setCellBackground: (color) => ({ state, dispatch }) => ifListed(color, this.options.cellColors) !== null &&
|
|
207
|
+
setCellAttr('backgroundColor', color)(state, dispatch),
|
|
208
|
+
unsetCellBackground: () => ({ state, dispatch }) => setCellAttr('backgroundColor', null)(state, dispatch)
|
|
89
209
|
};
|
|
210
|
+
},
|
|
211
|
+
addProseMirrorPlugins() {
|
|
212
|
+
return [
|
|
213
|
+
...(this.parent?.() ?? []),
|
|
214
|
+
...(this.editor.isEditable ? [tableSizing()] : [])
|
|
215
|
+
];
|
|
90
216
|
}
|
|
91
217
|
});
|
|
92
218
|
// A caption as a table holds it: each run of spaces and line breaks one
|
|
@@ -94,3 +220,9 @@ export const Table = TiptapTable.extend({
|
|
|
94
220
|
function captionText(text) {
|
|
95
221
|
return text?.replace(/\s+/g, ' ').trim() || null;
|
|
96
222
|
}
|
|
223
|
+
// A row's height as a body writes it, `--tiptap-row-height: 2.5em`, in em.
|
|
224
|
+
function heightOf(element) {
|
|
225
|
+
const value = /^(\d{1,2}(?:\.\d+)?)em$/.exec(getStyleProperty(element, '--tiptap-row-height') ?? '');
|
|
226
|
+
const height = value ? Math.round(parseFloat(value[1]) * 10) / 10 : 0;
|
|
227
|
+
return height > 0 ? height : null;
|
|
228
|
+
}
|
|
@@ -16,7 +16,7 @@ import { type TableOptions } from './table.js';
|
|
|
16
16
|
type Part<Options> = Partial<Options> | false;
|
|
17
17
|
export interface TiptapEssentialsOptions {
|
|
18
18
|
starterKit: Part<StarterKitOptions>;
|
|
19
|
-
/** TableKit's options, its table being the library's. */
|
|
19
|
+
/** TableKit's options, its table, cell and header being the library's. */
|
|
20
20
|
tableKit: (Partial<Omit<TableKitOptions, 'table'>> & {
|
|
21
21
|
table?: Part<TableOptions>;
|
|
22
22
|
}) | false;
|
|
@@ -13,6 +13,7 @@ import { PlainSpaces } from './plain-spaces.js';
|
|
|
13
13
|
import { Subscript } from './subscript.js';
|
|
14
14
|
import { Superscript } from './superscript.js';
|
|
15
15
|
import { Table } from './table.js';
|
|
16
|
+
import { TableCell, TableHeader } from './table-cell.js';
|
|
16
17
|
import { TextStyle } from './text-style.js';
|
|
17
18
|
import { Youtube } from './youtube.js';
|
|
18
19
|
/**
|
|
@@ -46,11 +47,22 @@ export const TiptapEssentials = Extension.create({
|
|
|
46
47
|
extensions.push(StarterKit.configure(starterKit));
|
|
47
48
|
}
|
|
48
49
|
if (tableKit !== false) {
|
|
49
|
-
const { table = {}, ...rest } = tableKit;
|
|
50
|
-
extensions.push(TableKit.configure({
|
|
50
|
+
const { table = {}, tableCell = {}, tableHeader = {}, ...rest } = tableKit;
|
|
51
|
+
extensions.push(TableKit.configure({
|
|
52
|
+
...rest,
|
|
53
|
+
table: false,
|
|
54
|
+
tableCell: false,
|
|
55
|
+
tableHeader: false
|
|
56
|
+
}));
|
|
51
57
|
if (table !== false) {
|
|
52
58
|
extensions.push(Table.configure(table));
|
|
53
59
|
}
|
|
60
|
+
if (tableCell !== false) {
|
|
61
|
+
extensions.push(TableCell.configure(tableCell));
|
|
62
|
+
}
|
|
63
|
+
if (tableHeader !== false) {
|
|
64
|
+
extensions.push(TableHeader.configure(tableHeader));
|
|
65
|
+
}
|
|
54
66
|
}
|
|
55
67
|
if (textStyleKit !== false) {
|
|
56
68
|
const { fontSize = {}, color = {}, lineHeight = {}, textStyle = {}, ...rest } = textStyleKit;
|