@mdbase-dev/ui 0.1.0-beta.163 → 0.1.0-beta.165
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/README.md +41 -0
- package/css/live-preview.css +387 -0
- package/css/palette.css +2 -1
- package/dist/apps.d.ts +6 -1
- package/dist/apps.js +25 -0
- package/dist/collections/CollectionGate.js +14 -3
- package/dist/collections/model.d.ts +4 -1
- package/dist/collections/model.js +19 -2
- package/dist/live-preview/blocks.d.ts +3 -0
- package/dist/live-preview/blocks.js +381 -0
- package/dist/live-preview/inline.d.ts +20 -0
- package/dist/live-preview/inline.js +418 -0
- package/dist/live-preview/keys.d.ts +7 -0
- package/dist/live-preview/keys.js +143 -0
- package/dist/live-preview/reveal.d.ts +29 -0
- package/dist/live-preview/reveal.js +97 -0
- package/dist/live-preview/table-controls.d.ts +75 -0
- package/dist/live-preview/table-controls.js +734 -0
- package/dist/live-preview/table-model.d.ts +113 -0
- package/dist/live-preview/table-model.js +510 -0
- package/dist/live-preview/table.d.ts +2 -0
- package/dist/live-preview/table.js +832 -0
- package/dist/live-preview.d.ts +16 -0
- package/dist/live-preview.js +47 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/package.json +30 -4
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
import { ViewPlugin } from "@codemirror/view";
|
|
2
|
+
import { i18n as _i18n } from "@lingui/core";
|
|
3
|
+
import { alignColumn, cellClipboard, cellRange, clearBlock, deleteColumns, deleteRows, dispatchTableEdit, insertColumn, insertRow, moveColumn, moveRow, rangeCells, sortRows, tableAt, tableContext, wholeTable } from "./table-model.js";
|
|
4
|
+
const SVG = "http://www.w3.org/2000/svg";
|
|
5
|
+
function icon(kind) {
|
|
6
|
+
const svg = document.createElementNS(SVG, "svg");
|
|
7
|
+
svg.setAttribute("viewBox", "0 0 16 16");
|
|
8
|
+
svg.setAttribute("aria-hidden", "true");
|
|
9
|
+
const dots = kind === "grip-x" ? [[4, 6], [8, 6], [12, 6], [4, 10], [8, 10], [12, 10]] : [[6, 4], [10, 4], [6, 8], [10, 8], [6, 12], [10, 12]];
|
|
10
|
+
if (kind === "plus") {
|
|
11
|
+
const path = document.createElementNS(SVG, "path");
|
|
12
|
+
path.setAttribute("d", "M8 3.5v9M3.5 8h9");
|
|
13
|
+
svg.append(path);
|
|
14
|
+
} else for (const [cx, cy] of dots) {
|
|
15
|
+
const dot = document.createElementNS(SVG, "circle");
|
|
16
|
+
dot.setAttribute("cx", String(cx));
|
|
17
|
+
dot.setAttribute("cy", String(cy));
|
|
18
|
+
dot.setAttribute("r", "1.15");
|
|
19
|
+
svg.append(dot);
|
|
20
|
+
}
|
|
21
|
+
return svg;
|
|
22
|
+
}
|
|
23
|
+
/** The table cell an element is in, read from the layout: empty and missing cells have no text to hit. */
|
|
24
|
+
export function cellTarget(view, element) {
|
|
25
|
+
const cell = element?.closest(".cm-lp-td");
|
|
26
|
+
const line = cell?.closest(".cm-line");
|
|
27
|
+
if (!cell || !line || !view.contentDOM.contains(line)) return null;
|
|
28
|
+
const position = view.posAtDOM(line, 0);
|
|
29
|
+
const table = tableAt(view.state, position);
|
|
30
|
+
if (!table) return null;
|
|
31
|
+
const row = table.rows.findIndex(row => row.lineFrom === position);
|
|
32
|
+
const column = [...line.querySelectorAll(":scope > .cm-lp-td")].indexOf(cell);
|
|
33
|
+
return row < 0 || column < 0 ? null : {
|
|
34
|
+
tableFrom: table.from,
|
|
35
|
+
at: {
|
|
36
|
+
row,
|
|
37
|
+
column
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Puts cells on the clipboard: Markdown for text, an HTML table for spreadsheets and documents. */
|
|
42
|
+
function writeCells(view, range) {
|
|
43
|
+
const {
|
|
44
|
+
markdown,
|
|
45
|
+
html
|
|
46
|
+
} = cellClipboard(view.state, range);
|
|
47
|
+
void navigator.clipboard.write([new ClipboardItem({
|
|
48
|
+
"text/plain": new Blob([markdown], {
|
|
49
|
+
type: "text/plain"
|
|
50
|
+
}),
|
|
51
|
+
"text/html": new Blob([html], {
|
|
52
|
+
type: "text/html"
|
|
53
|
+
})
|
|
54
|
+
})]);
|
|
55
|
+
}
|
|
56
|
+
const copyTable = view => ({
|
|
57
|
+
label: _i18n._(
|
|
58
|
+
/** i18n */
|
|
59
|
+
{
|
|
60
|
+
id: "t9RpQT",
|
|
61
|
+
message: "Copy table",
|
|
62
|
+
context: "table"
|
|
63
|
+
}),
|
|
64
|
+
run: table => {
|
|
65
|
+
writeCells(view, wholeTable(table));
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
/** The selected block's own actions, as its keyboard shortcuts do them. */
|
|
70
|
+
function blockItems(view) {
|
|
71
|
+
const act = (copy, clear) => () => {
|
|
72
|
+
const range = cellRange(view.state);
|
|
73
|
+
if (!range) return null;
|
|
74
|
+
if (copy) writeCells(view, range);
|
|
75
|
+
const edit = clear ? clearBlock(view.state, range, copy ? "delete.cut" : "delete") : null;
|
|
76
|
+
if (edit) view.dispatch(edit);
|
|
77
|
+
return null;
|
|
78
|
+
};
|
|
79
|
+
return [{
|
|
80
|
+
label: _i18n._(
|
|
81
|
+
/** i18n */
|
|
82
|
+
{
|
|
83
|
+
id: "b9F7ib",
|
|
84
|
+
message: "Copy cells",
|
|
85
|
+
context: "table"
|
|
86
|
+
}),
|
|
87
|
+
run: act(true, false)
|
|
88
|
+
}, {
|
|
89
|
+
label: _i18n._(
|
|
90
|
+
/** i18n */
|
|
91
|
+
{
|
|
92
|
+
id: "EGmzo2",
|
|
93
|
+
message: "Cut cells",
|
|
94
|
+
context: "table"
|
|
95
|
+
}),
|
|
96
|
+
run: act(true, true)
|
|
97
|
+
}, {
|
|
98
|
+
label: _i18n._(
|
|
99
|
+
/** i18n */
|
|
100
|
+
{
|
|
101
|
+
id: "_Amut3",
|
|
102
|
+
message: "Clear cells",
|
|
103
|
+
context: "table"
|
|
104
|
+
}),
|
|
105
|
+
run: act(false, true)
|
|
106
|
+
}];
|
|
107
|
+
}
|
|
108
|
+
function rowMoves(view, table, at) {
|
|
109
|
+
if (table.rows[at.row].kind !== "body") return [{
|
|
110
|
+
label: _i18n._(
|
|
111
|
+
/** i18n */
|
|
112
|
+
{
|
|
113
|
+
id: "D32DpX",
|
|
114
|
+
message: "Insert row below",
|
|
115
|
+
context: "table"
|
|
116
|
+
}),
|
|
117
|
+
run: (table, at) => insertRow(table, at.row)
|
|
118
|
+
}];
|
|
119
|
+
return [{
|
|
120
|
+
label: _i18n._(
|
|
121
|
+
/** i18n */
|
|
122
|
+
{
|
|
123
|
+
id: "6I67ow",
|
|
124
|
+
message: "Insert row above",
|
|
125
|
+
context: "table"
|
|
126
|
+
}),
|
|
127
|
+
run: (table, at) => insertRow(table, at.row - 1)
|
|
128
|
+
}, {
|
|
129
|
+
label: _i18n._(
|
|
130
|
+
/** i18n */
|
|
131
|
+
{
|
|
132
|
+
id: "D32DpX",
|
|
133
|
+
message: "Insert row below",
|
|
134
|
+
context: "table"
|
|
135
|
+
}),
|
|
136
|
+
run: (table, at) => insertRow(table, at.row)
|
|
137
|
+
}, {
|
|
138
|
+
label: _i18n._(
|
|
139
|
+
/** i18n */
|
|
140
|
+
{
|
|
141
|
+
id: "8VzvS0",
|
|
142
|
+
message: "Move row up",
|
|
143
|
+
context: "table"
|
|
144
|
+
}),
|
|
145
|
+
run: (table, at) => moveRowAt(view, table, at, at.row - 1),
|
|
146
|
+
disabled: table.rows[at.row - 1]?.kind !== "body"
|
|
147
|
+
}, {
|
|
148
|
+
label: _i18n._(
|
|
149
|
+
/** i18n */
|
|
150
|
+
{
|
|
151
|
+
id: "XfOTYg",
|
|
152
|
+
message: "Move row down",
|
|
153
|
+
context: "table"
|
|
154
|
+
}),
|
|
155
|
+
run: (table, at) => moveRowAt(view, table, at, at.row + 1),
|
|
156
|
+
disabled: table.rows[at.row + 1]?.kind !== "body"
|
|
157
|
+
}];
|
|
158
|
+
}
|
|
159
|
+
function deleteRowsItem(table, rows) {
|
|
160
|
+
const body = rows.filter(row => table.rows[row].kind === "body");
|
|
161
|
+
if (!body.length) return [];
|
|
162
|
+
const label = body.length > 1 ? _i18n._(
|
|
163
|
+
/** i18n */
|
|
164
|
+
{
|
|
165
|
+
id: "7x7cmB",
|
|
166
|
+
message: "Delete rows",
|
|
167
|
+
context: "table"
|
|
168
|
+
}) : _i18n._(
|
|
169
|
+
/** i18n */
|
|
170
|
+
{
|
|
171
|
+
id: "ZWroEK",
|
|
172
|
+
message: "Delete row",
|
|
173
|
+
context: "table"
|
|
174
|
+
});
|
|
175
|
+
return [{
|
|
176
|
+
label,
|
|
177
|
+
run: table => deleteRows(table, body),
|
|
178
|
+
danger: true
|
|
179
|
+
}];
|
|
180
|
+
}
|
|
181
|
+
function moveRowAt(view, table, at, target) {
|
|
182
|
+
const cell = table.rows[at.row].cells[at.column];
|
|
183
|
+
// The caret stays on its row, so put it in the row being moved.
|
|
184
|
+
const state = view.state.update({
|
|
185
|
+
selection: {
|
|
186
|
+
anchor: cell?.contentTo ?? table.rows[at.row].to
|
|
187
|
+
}
|
|
188
|
+
}).state;
|
|
189
|
+
return moveRow(state, table, at.row, target);
|
|
190
|
+
}
|
|
191
|
+
function columnMoves(view, table, at) {
|
|
192
|
+
return [{
|
|
193
|
+
label: _i18n._(
|
|
194
|
+
/** i18n */
|
|
195
|
+
{
|
|
196
|
+
id: "jySKa9",
|
|
197
|
+
message: "Insert column left",
|
|
198
|
+
context: "table"
|
|
199
|
+
}),
|
|
200
|
+
run: (table, at) => insertColumn(view.state, table, at, at.column)
|
|
201
|
+
}, {
|
|
202
|
+
label: _i18n._(
|
|
203
|
+
/** i18n */
|
|
204
|
+
{
|
|
205
|
+
id: "D5MX-a",
|
|
206
|
+
message: "Insert column right",
|
|
207
|
+
context: "table"
|
|
208
|
+
}),
|
|
209
|
+
run: (table, at) => insertColumn(view.state, table, at, at.column + 1)
|
|
210
|
+
}, {
|
|
211
|
+
label: _i18n._(
|
|
212
|
+
/** i18n */
|
|
213
|
+
{
|
|
214
|
+
id: "Vlym2A",
|
|
215
|
+
message: "Move column left",
|
|
216
|
+
context: "table"
|
|
217
|
+
}),
|
|
218
|
+
run: (table, at) => moveColumn(view.state, table, at, at.column - 1),
|
|
219
|
+
disabled: at.column === 0
|
|
220
|
+
}, {
|
|
221
|
+
label: _i18n._(
|
|
222
|
+
/** i18n */
|
|
223
|
+
{
|
|
224
|
+
id: "Nj85n-",
|
|
225
|
+
message: "Move column right",
|
|
226
|
+
context: "table"
|
|
227
|
+
}),
|
|
228
|
+
run: (table, at) => moveColumn(view.state, table, at, at.column + 1),
|
|
229
|
+
disabled: at.column >= table.columns - 1
|
|
230
|
+
}];
|
|
231
|
+
}
|
|
232
|
+
function columnFormat(view, table, at) {
|
|
233
|
+
const align = (alignment, label) => ({
|
|
234
|
+
label,
|
|
235
|
+
checked: (table.alignments[at.column] ?? "left") === alignment,
|
|
236
|
+
run: (table, at) => alignColumn(view.state, table, at, alignment)
|
|
237
|
+
});
|
|
238
|
+
return [align("left", _i18n._(
|
|
239
|
+
/** i18n */
|
|
240
|
+
{
|
|
241
|
+
id: "CwPBSU",
|
|
242
|
+
message: "Align left",
|
|
243
|
+
context: "table"
|
|
244
|
+
})), align("center", _i18n._(
|
|
245
|
+
/** i18n */
|
|
246
|
+
{
|
|
247
|
+
id: "bys5pN",
|
|
248
|
+
message: "Align center",
|
|
249
|
+
context: "table"
|
|
250
|
+
})), align("right", _i18n._(
|
|
251
|
+
/** i18n */
|
|
252
|
+
{
|
|
253
|
+
id: "z2aN_0",
|
|
254
|
+
message: "Align right",
|
|
255
|
+
context: "table"
|
|
256
|
+
})), "separator", {
|
|
257
|
+
label: _i18n._(
|
|
258
|
+
/** i18n */
|
|
259
|
+
{
|
|
260
|
+
id: "xOEzUG",
|
|
261
|
+
message: "Sort ascending",
|
|
262
|
+
context: "table"
|
|
263
|
+
}),
|
|
264
|
+
run: (table, at) => sortRows(view.state, table, at.column, 1),
|
|
265
|
+
disabled: table.rows.length < 4
|
|
266
|
+
}, {
|
|
267
|
+
label: _i18n._(
|
|
268
|
+
/** i18n */
|
|
269
|
+
{
|
|
270
|
+
id: "LJNBRF",
|
|
271
|
+
message: "Sort descending",
|
|
272
|
+
context: "table"
|
|
273
|
+
}),
|
|
274
|
+
run: (table, at) => sortRows(view.state, table, at.column, -1),
|
|
275
|
+
disabled: table.rows.length < 4
|
|
276
|
+
}];
|
|
277
|
+
}
|
|
278
|
+
function deleteColumnsItem(view, table, columns) {
|
|
279
|
+
const label = columns.length > 1 ? _i18n._(
|
|
280
|
+
/** i18n */
|
|
281
|
+
{
|
|
282
|
+
id: "1mVduw",
|
|
283
|
+
message: "Delete columns",
|
|
284
|
+
context: "table"
|
|
285
|
+
}) : _i18n._(
|
|
286
|
+
/** i18n */
|
|
287
|
+
{
|
|
288
|
+
id: "MEEyhe",
|
|
289
|
+
message: "Delete column",
|
|
290
|
+
context: "table"
|
|
291
|
+
});
|
|
292
|
+
return {
|
|
293
|
+
label,
|
|
294
|
+
run: (table, at) => deleteColumns(view.state, table, at, columns),
|
|
295
|
+
danger: true,
|
|
296
|
+
disabled: columns.length >= table.columns
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function menuItems(view, kind, table, at) {
|
|
300
|
+
const end = deletes => ["separator", copyTable(view), ...(deletes.length ? ["separator", ...deletes] : [])];
|
|
301
|
+
if (kind === "row") return [...rowMoves(view, table, at), ...end(deleteRowsItem(table, [at.row]))];
|
|
302
|
+
if (kind === "column") return [...columnMoves(view, table, at), "separator", ...columnFormat(view, table, at), ...end([deleteColumnsItem(view, table, [at.column])])];
|
|
303
|
+
// Inside a selected block, the block's actions lead and deleting takes all its rows or columns.
|
|
304
|
+
const range = cellRange(view.state);
|
|
305
|
+
const block = range && range.table.from === table.from ? rangeCells(range) : null;
|
|
306
|
+
const inBlock = !!block && block.rows.includes(at.row) && block.columns.includes(at.column);
|
|
307
|
+
return [...(inBlock ? [...blockItems(view), "separator"] : []), ...rowMoves(view, table, at), "separator", ...columnMoves(view, table, at), "separator", ...columnFormat(view, table, at), ...end([...deleteRowsItem(table, inBlock ? block.rows : [at.row]), deleteColumnsItem(view, table, inBlock ? block.columns : [at.column])])];
|
|
308
|
+
}
|
|
309
|
+
/** One open menu per editor. Built from plain buttons so it reads as a menu to assistive tech. */
|
|
310
|
+
class Menu {
|
|
311
|
+
view;
|
|
312
|
+
target;
|
|
313
|
+
onClose;
|
|
314
|
+
dom;
|
|
315
|
+
close;
|
|
316
|
+
constructor(view, target, items, x, y, onClose) {
|
|
317
|
+
this.view = view;
|
|
318
|
+
this.target = target;
|
|
319
|
+
this.onClose = onClose;
|
|
320
|
+
const menu = this.dom = document.createElement("div");
|
|
321
|
+
menu.className = "mdbase-menu cm-lp-table-menu";
|
|
322
|
+
menu.setAttribute("role", "menu");
|
|
323
|
+
menu.setAttribute("aria-label", _i18n._(
|
|
324
|
+
/** i18n */
|
|
325
|
+
{
|
|
326
|
+
id: "0R_qze",
|
|
327
|
+
message: "Table options",
|
|
328
|
+
context: "table"
|
|
329
|
+
}));
|
|
330
|
+
for (const item of items) {
|
|
331
|
+
if (item === "separator") {
|
|
332
|
+
const rule = document.createElement("div");
|
|
333
|
+
rule.className = "cm-lp-table-menu-separator";
|
|
334
|
+
rule.setAttribute("role", "separator");
|
|
335
|
+
menu.append(rule);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const button = document.createElement("button");
|
|
339
|
+
button.type = "button";
|
|
340
|
+
button.className = "mdbase-menu-item";
|
|
341
|
+
button.setAttribute("role", item.checked === undefined ? "menuitem" : "menuitemradio");
|
|
342
|
+
if (item.checked !== undefined) button.setAttribute("aria-checked", String(item.checked));
|
|
343
|
+
if (item.danger) button.classList.add("is-danger");
|
|
344
|
+
button.disabled = item.disabled ?? false;
|
|
345
|
+
button.textContent = item.label;
|
|
346
|
+
button.addEventListener("click", () => this.run(item.run));
|
|
347
|
+
menu.append(button);
|
|
348
|
+
}
|
|
349
|
+
menu.addEventListener("keydown", event => this.key(event));
|
|
350
|
+
const document_ = view.dom.ownerDocument;
|
|
351
|
+
document_.body.append(menu);
|
|
352
|
+
// Keep the menu on screen, flipping above its anchor when needed.
|
|
353
|
+
const rect = menu.getBoundingClientRect();
|
|
354
|
+
const viewport = document_.documentElement;
|
|
355
|
+
menu.style.left = `${Math.max(8, Math.min(x, viewport.clientWidth - rect.width - 8))}px`;
|
|
356
|
+
menu.style.top = `${y + rect.height > viewport.clientHeight - 8 ? Math.max(8, y - rect.height - 8) : y}px`;
|
|
357
|
+
this.close = event => {
|
|
358
|
+
if (event.type === "keydown" || !(event.target instanceof Node && menu.contains(event.target))) this.destroy(event.type === "keydown");
|
|
359
|
+
};
|
|
360
|
+
document_.addEventListener("pointerdown", this.close, true);
|
|
361
|
+
document_.addEventListener("scroll", this.close, true);
|
|
362
|
+
window.addEventListener("resize", this.close);
|
|
363
|
+
menu.querySelector("button:not(:disabled)")?.focus();
|
|
364
|
+
}
|
|
365
|
+
buttons() {
|
|
366
|
+
return [...this.dom.querySelectorAll("button:not(:disabled)")];
|
|
367
|
+
}
|
|
368
|
+
key(event) {
|
|
369
|
+
const buttons = this.buttons();
|
|
370
|
+
const index = buttons.indexOf(event.target);
|
|
371
|
+
const move = to => {
|
|
372
|
+
event.preventDefault();
|
|
373
|
+
buttons[(to + buttons.length) % buttons.length]?.focus();
|
|
374
|
+
};
|
|
375
|
+
if (event.key === "ArrowDown") move(index + 1);else if (event.key === "ArrowUp") move(index - 1);else if (event.key === "Home") move(0);else if (event.key === "End") move(buttons.length - 1);else if (event.key === "Escape" || event.key === "Tab") {
|
|
376
|
+
event.preventDefault();
|
|
377
|
+
this.destroy(true);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
run(run) {
|
|
381
|
+
const table = tableAt(this.view.state, this.target.tableFrom);
|
|
382
|
+
this.destroy(true);
|
|
383
|
+
if (table) dispatchTableEdit(this.view, run(table, this.target.at));
|
|
384
|
+
}
|
|
385
|
+
destroy(refocus) {
|
|
386
|
+
const document_ = this.view.dom.ownerDocument;
|
|
387
|
+
document_.removeEventListener("pointerdown", this.close, true);
|
|
388
|
+
document_.removeEventListener("scroll", this.close, true);
|
|
389
|
+
window.removeEventListener("resize", this.close);
|
|
390
|
+
this.dom.remove();
|
|
391
|
+
this.onClose();
|
|
392
|
+
if (refocus) this.view.focus();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/** Row and column grips plus add buttons, drawn over the editor rather than inside the document. */
|
|
396
|
+
class Controls {
|
|
397
|
+
view;
|
|
398
|
+
layer;
|
|
399
|
+
column;
|
|
400
|
+
row;
|
|
401
|
+
addRow;
|
|
402
|
+
addColumn;
|
|
403
|
+
source;
|
|
404
|
+
drop;
|
|
405
|
+
hover = null;
|
|
406
|
+
target = null;
|
|
407
|
+
menu = null;
|
|
408
|
+
drag = null;
|
|
409
|
+
/** A drag ends with a click on its grip, which must not open the menu. */
|
|
410
|
+
dragged = false;
|
|
411
|
+
constructor(view) {
|
|
412
|
+
this.view = view;
|
|
413
|
+
this.layer = document.createElement("div");
|
|
414
|
+
this.layer.className = "cm-lp-table-controls";
|
|
415
|
+
this.layer.setAttribute("aria-hidden", "true");
|
|
416
|
+
const button = (className, kind, label, click) => {
|
|
417
|
+
const element = document.createElement("button");
|
|
418
|
+
element.type = "button";
|
|
419
|
+
element.tabIndex = -1;
|
|
420
|
+
element.className = className;
|
|
421
|
+
element.title = label;
|
|
422
|
+
element.append(icon(kind));
|
|
423
|
+
// Keep the caret and focus in the editor.
|
|
424
|
+
element.addEventListener("mousedown", event => event.preventDefault());
|
|
425
|
+
element.addEventListener("click", click);
|
|
426
|
+
this.layer.append(element);
|
|
427
|
+
return element;
|
|
428
|
+
};
|
|
429
|
+
this.column = button("cm-lp-table-grip cm-lp-table-grip-column", "grip-x", _i18n._(
|
|
430
|
+
/** i18n */
|
|
431
|
+
{
|
|
432
|
+
id: "6U-3rD",
|
|
433
|
+
message: "Column options",
|
|
434
|
+
context: "table"
|
|
435
|
+
}), event => this.dragged || this.open("column", event));
|
|
436
|
+
this.row = button("cm-lp-table-grip cm-lp-table-grip-row", "grip-y", _i18n._(
|
|
437
|
+
/** i18n */
|
|
438
|
+
{
|
|
439
|
+
id: "JnyajL",
|
|
440
|
+
message: "Row options",
|
|
441
|
+
context: "table"
|
|
442
|
+
}), event => this.dragged || this.open("row", event));
|
|
443
|
+
this.addRow = button("cm-lp-table-add cm-lp-table-add-row", "plus", _i18n._(
|
|
444
|
+
/** i18n */
|
|
445
|
+
{
|
|
446
|
+
id: "FvfStt",
|
|
447
|
+
message: "Add row",
|
|
448
|
+
context: "table"
|
|
449
|
+
}), () => this.add("row"));
|
|
450
|
+
this.addColumn = button("cm-lp-table-add cm-lp-table-add-column", "plus", _i18n._(
|
|
451
|
+
/** i18n */
|
|
452
|
+
{
|
|
453
|
+
id: "plippA",
|
|
454
|
+
message: "Add column",
|
|
455
|
+
context: "table"
|
|
456
|
+
}), () => this.add("column"));
|
|
457
|
+
this.source = document.createElement("div");
|
|
458
|
+
this.source.className = "cm-lp-table-drag-source";
|
|
459
|
+
this.drop = document.createElement("div");
|
|
460
|
+
this.drop.className = "cm-lp-table-drop";
|
|
461
|
+
this.source.hidden = this.drop.hidden = true;
|
|
462
|
+
this.layer.append(this.source, this.drop);
|
|
463
|
+
this.column.addEventListener("pointerdown", event => this.startDrag("column", event));
|
|
464
|
+
this.row.addEventListener("pointerdown", event => this.startDrag("row", event));
|
|
465
|
+
view.scrollDOM.append(this.layer);
|
|
466
|
+
this.schedule();
|
|
467
|
+
}
|
|
468
|
+
current() {
|
|
469
|
+
if (this.menu) return this.menu.target;
|
|
470
|
+
if (this.hover) return this.hover;
|
|
471
|
+
if (!this.view.hasFocus) return null;
|
|
472
|
+
const context = tableContext(this.view.state);
|
|
473
|
+
return context ? {
|
|
474
|
+
tableFrom: context.table.from,
|
|
475
|
+
at: context.at
|
|
476
|
+
} : null;
|
|
477
|
+
}
|
|
478
|
+
update(update) {
|
|
479
|
+
if (update.docChanged && this.hover) this.hover = null;
|
|
480
|
+
if (update.docChanged || update.selectionSet || update.focusChanged || update.geometryChanged || update.viewportChanged) this.schedule();
|
|
481
|
+
}
|
|
482
|
+
schedule() {
|
|
483
|
+
this.view.requestMeasure({
|
|
484
|
+
key: this,
|
|
485
|
+
read: view => this.read(view),
|
|
486
|
+
write: layout => this.write(layout)
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
cellElements(target) {
|
|
490
|
+
const table = tableAt(this.view.state, target.tableFrom);
|
|
491
|
+
if (!table || target.at.row >= table.rows.length) return null;
|
|
492
|
+
const line = row => {
|
|
493
|
+
const {
|
|
494
|
+
node
|
|
495
|
+
} = this.view.domAtPos(table.rows[row].lineFrom);
|
|
496
|
+
return (node instanceof Element ? node : node.parentElement)?.closest(".cm-line") ?? null;
|
|
497
|
+
};
|
|
498
|
+
const row = line(target.at.row),
|
|
499
|
+
header = line(0);
|
|
500
|
+
const column = header?.querySelectorAll(":scope > .cm-lp-td")[target.at.column];
|
|
501
|
+
const wrapper = header?.closest(".cm-lp-table");
|
|
502
|
+
return row && column && wrapper ? {
|
|
503
|
+
row,
|
|
504
|
+
column,
|
|
505
|
+
wrapper
|
|
506
|
+
} : null;
|
|
507
|
+
}
|
|
508
|
+
read(view) {
|
|
509
|
+
this.target = this.current();
|
|
510
|
+
if (!this.target || view.state.readOnly) return null;
|
|
511
|
+
const elements = this.cellElements(this.target);
|
|
512
|
+
if (!elements) return null;
|
|
513
|
+
return {
|
|
514
|
+
table: elements.wrapper.getBoundingClientRect(),
|
|
515
|
+
column: elements.column.getBoundingClientRect(),
|
|
516
|
+
row: elements.row.getBoundingClientRect(),
|
|
517
|
+
scroller: view.scrollDOM.getBoundingClientRect(),
|
|
518
|
+
scroll: {
|
|
519
|
+
top: view.scrollDOM.scrollTop,
|
|
520
|
+
left: view.scrollDOM.scrollLeft
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
write(layout) {
|
|
525
|
+
this.layer.classList.toggle("cm-lp-table-controls-visible", layout !== null);
|
|
526
|
+
if (!layout) return;
|
|
527
|
+
const x = value => `${value - layout.scroller.left + layout.scroll.left}px`;
|
|
528
|
+
const y = value => `${value - layout.scroller.top + layout.scroll.top}px`;
|
|
529
|
+
const place = (element, left, top, width, height) => {
|
|
530
|
+
element.style.left = left;
|
|
531
|
+
element.style.top = top;
|
|
532
|
+
element.style.width = width ?? "";
|
|
533
|
+
element.style.height = height ?? "";
|
|
534
|
+
};
|
|
535
|
+
place(this.column, x(layout.column.left + layout.column.width / 2), y(layout.table.top));
|
|
536
|
+
place(this.row, x(layout.table.left), y(layout.row.top + layout.row.height / 2));
|
|
537
|
+
place(this.addRow, x(layout.table.left), y(layout.table.bottom), `${layout.table.width}px`);
|
|
538
|
+
place(this.addColumn, x(layout.table.right), y(layout.table.top), undefined, `${layout.table.height}px`);
|
|
539
|
+
}
|
|
540
|
+
open(kind, event, at) {
|
|
541
|
+
const target = this.current();
|
|
542
|
+
this.menu?.destroy(false);
|
|
543
|
+
if (!target) return;
|
|
544
|
+
const table = tableAt(this.view.state, target.tableFrom);
|
|
545
|
+
if (!table) return;
|
|
546
|
+
const items = menuItems(this.view, kind === "both" ? "cell" : kind, table, target.at);
|
|
547
|
+
const anchor = event?.currentTarget instanceof HTMLElement ? event.currentTarget.getBoundingClientRect() : null;
|
|
548
|
+
const x = at?.x ?? (anchor ? anchor.left : 0),
|
|
549
|
+
y = at?.y ?? (anchor ? anchor.bottom + 4 : 0);
|
|
550
|
+
this.menu = new Menu(this.view, target, items, x, y, () => {
|
|
551
|
+
this.menu = null;
|
|
552
|
+
this.schedule();
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
/** Opens both sections at a point, for right-click, long-press and the keyboard. */
|
|
556
|
+
openAt(target, x, y) {
|
|
557
|
+
this.hover = target;
|
|
558
|
+
this.open("both", null, {
|
|
559
|
+
x,
|
|
560
|
+
y
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
add(kind) {
|
|
564
|
+
const target = this.current();
|
|
565
|
+
const table = target && tableAt(this.view.state, target.tableFrom);
|
|
566
|
+
if (!target || !table) return;
|
|
567
|
+
this.hover = null;
|
|
568
|
+
if (kind === "row") dispatchTableEdit(this.view, insertRow(table, table.rows.length - 1));
|
|
569
|
+
// A new column starts at its header, which is the first thing it needs.
|
|
570
|
+
else dispatchTableEdit(this.view, insertColumn(this.view.state, table, {
|
|
571
|
+
row: 0,
|
|
572
|
+
column: table.columns - 1
|
|
573
|
+
}, table.columns));
|
|
574
|
+
this.view.focus();
|
|
575
|
+
}
|
|
576
|
+
/** Rows and columns move by dragging their grip; a press without movement stays a click. */
|
|
577
|
+
startDrag(kind, event) {
|
|
578
|
+
this.dragged = false;
|
|
579
|
+
const target = this.current();
|
|
580
|
+
if (event.button !== 0 || !target || this.view.state.readOnly) return;
|
|
581
|
+
const grip = event.currentTarget;
|
|
582
|
+
grip.setPointerCapture(event.pointerId);
|
|
583
|
+
this.drag = {
|
|
584
|
+
kind,
|
|
585
|
+
target,
|
|
586
|
+
x: event.clientX,
|
|
587
|
+
y: event.clientY,
|
|
588
|
+
to: null
|
|
589
|
+
};
|
|
590
|
+
const move = moved => this.moveDrag(moved);
|
|
591
|
+
const end = ended => {
|
|
592
|
+
if (ended instanceof KeyboardEvent) {
|
|
593
|
+
if (ended.key !== "Escape") return;
|
|
594
|
+
// Escape drops the drag wherever focus is.
|
|
595
|
+
ended.preventDefault();
|
|
596
|
+
ended.stopPropagation();
|
|
597
|
+
}
|
|
598
|
+
grip.removeEventListener("pointermove", move);
|
|
599
|
+
grip.removeEventListener("pointerup", end);
|
|
600
|
+
grip.removeEventListener("pointercancel", end);
|
|
601
|
+
window.removeEventListener("keydown", end, true);
|
|
602
|
+
this.endDrag(ended.type === "pointerup");
|
|
603
|
+
};
|
|
604
|
+
grip.addEventListener("pointermove", move);
|
|
605
|
+
grip.addEventListener("pointerup", end);
|
|
606
|
+
grip.addEventListener("pointercancel", end);
|
|
607
|
+
window.addEventListener("keydown", end, true);
|
|
608
|
+
}
|
|
609
|
+
moveDrag(event) {
|
|
610
|
+
const drag = this.drag;
|
|
611
|
+
const table = drag && tableAt(this.view.state, drag.target.tableFrom);
|
|
612
|
+
if (!drag || !table) return;
|
|
613
|
+
if (drag.to === null && Math.hypot(event.clientX - drag.x, event.clientY - drag.y) < 4) return;
|
|
614
|
+
const slots = this.slots(table, drag.kind);
|
|
615
|
+
const wrapper = slots[0]?.element.closest(".cm-lp-table");
|
|
616
|
+
if (!wrapper) return;
|
|
617
|
+
const row = drag.kind === "row";
|
|
618
|
+
const point = row ? event.clientY : event.clientX;
|
|
619
|
+
const from = row ? drag.target.at.row : drag.target.at.column;
|
|
620
|
+
const slot = slots.find(slot => point < (row ? slot.rect.bottom : slot.rect.right)) ?? slots.at(-1);
|
|
621
|
+
const source = slots.find(slot => slot.index === from);
|
|
622
|
+
drag.to = slot.index;
|
|
623
|
+
this.layer.classList.add("cm-lp-table-dragging");
|
|
624
|
+
const scroller = this.view.scrollDOM.getBoundingClientRect();
|
|
625
|
+
const frame = wrapper.getBoundingClientRect();
|
|
626
|
+
const place = (element, left, top, width, height) => {
|
|
627
|
+
element.hidden = false;
|
|
628
|
+
element.style.left = `${left - scroller.left + this.view.scrollDOM.scrollLeft}px`;
|
|
629
|
+
element.style.top = `${top - scroller.top + this.view.scrollDOM.scrollTop}px`;
|
|
630
|
+
element.style.width = `${width}px`;
|
|
631
|
+
element.style.height = `${height}px`;
|
|
632
|
+
};
|
|
633
|
+
if (source) {
|
|
634
|
+
if (row) place(this.source, frame.left, source.rect.top, frame.width, source.rect.height);else place(this.source, source.rect.left, frame.top, source.rect.width, frame.height);
|
|
635
|
+
}
|
|
636
|
+
// The line marks the edge the moved row or column will land against.
|
|
637
|
+
if (slot.index === from) this.drop.hidden = true;else if (row) place(this.drop, frame.left, slot.index > from ? slot.rect.bottom : slot.rect.top, frame.width, 0);else place(this.drop, slot.index > from ? slot.rect.right : slot.rect.left, frame.top, 0, frame.height);
|
|
638
|
+
}
|
|
639
|
+
endDrag(commit) {
|
|
640
|
+
const drag = this.drag;
|
|
641
|
+
this.drag = null;
|
|
642
|
+
this.layer.classList.remove("cm-lp-table-dragging");
|
|
643
|
+
this.source.hidden = this.drop.hidden = true;
|
|
644
|
+
if (!drag || drag.to === null) return;
|
|
645
|
+
this.dragged = true;
|
|
646
|
+
const table = tableAt(this.view.state, drag.target.tableFrom);
|
|
647
|
+
if (!commit || !table) return;
|
|
648
|
+
const {
|
|
649
|
+
at
|
|
650
|
+
} = drag.target;
|
|
651
|
+
const edit = drag.kind === "row" ? moveRowAt(this.view, table, at, drag.to) : moveColumn(this.view.state, table, at, drag.to);
|
|
652
|
+
this.hover = null;
|
|
653
|
+
if (edit) dispatchTableEdit(this.view, edit, "move.table");
|
|
654
|
+
this.view.focus();
|
|
655
|
+
}
|
|
656
|
+
/** The rows or columns a drag can land on, by their rendered rectangles. */
|
|
657
|
+
slots(table, kind) {
|
|
658
|
+
const line = row => {
|
|
659
|
+
const {
|
|
660
|
+
node
|
|
661
|
+
} = this.view.domAtPos(table.rows[row].lineFrom);
|
|
662
|
+
return (node instanceof Element ? node : node.parentElement)?.closest(".cm-line") ?? null;
|
|
663
|
+
};
|
|
664
|
+
const slots = [];
|
|
665
|
+
if (kind === "row") {
|
|
666
|
+
table.rows.forEach((row, index) => {
|
|
667
|
+
const element = row.kind === "body" ? line(index) : null;
|
|
668
|
+
if (element) slots.push({
|
|
669
|
+
index,
|
|
670
|
+
element,
|
|
671
|
+
rect: element.getBoundingClientRect()
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
} else {
|
|
675
|
+
line(0)?.querySelectorAll(":scope > .cm-lp-td").forEach((element, index) => slots.push({
|
|
676
|
+
index,
|
|
677
|
+
element,
|
|
678
|
+
rect: element.getBoundingClientRect()
|
|
679
|
+
}));
|
|
680
|
+
}
|
|
681
|
+
return slots;
|
|
682
|
+
}
|
|
683
|
+
pointer(event) {
|
|
684
|
+
if (this.menu || this.drag) return;
|
|
685
|
+
const element = event.target instanceof Element ? event.target : null;
|
|
686
|
+
if (element?.closest(".cm-lp-table-controls")) return;
|
|
687
|
+
const next = this.targetAt(element);
|
|
688
|
+
if (next?.tableFrom === this.hover?.tableFrom && next?.at.row === this.hover?.at.row && next?.at.column === this.hover?.at.column) return;
|
|
689
|
+
this.hover = next;
|
|
690
|
+
this.schedule();
|
|
691
|
+
}
|
|
692
|
+
targetAt(element) {
|
|
693
|
+
return cellTarget(this.view, element);
|
|
694
|
+
}
|
|
695
|
+
destroy() {
|
|
696
|
+
this.menu?.destroy(false);
|
|
697
|
+
this.layer.remove();
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const controls = ViewPlugin.fromClass(Controls, {
|
|
701
|
+
eventHandlers: {
|
|
702
|
+
mousemove(event) {
|
|
703
|
+
this.pointer(event);
|
|
704
|
+
},
|
|
705
|
+
mouseleave() {
|
|
706
|
+
if (this.hover && !this.menu) {
|
|
707
|
+
this.hover = null;
|
|
708
|
+
this.schedule();
|
|
709
|
+
}
|
|
710
|
+
},
|
|
711
|
+
contextmenu(event, view) {
|
|
712
|
+
const target = this.targetAt(event.target instanceof Element ? event.target : null);
|
|
713
|
+
if (!target || view.state.readOnly) return false;
|
|
714
|
+
event.preventDefault();
|
|
715
|
+
this.openAt(target, event.clientX, event.clientY);
|
|
716
|
+
return true;
|
|
717
|
+
},
|
|
718
|
+
keydown(event, view) {
|
|
719
|
+
if (!(event.key === "ContextMenu" || event.key === "F10" && event.shiftKey)) return false;
|
|
720
|
+
const context = tableContext(view.state);
|
|
721
|
+
if (!context || view.state.readOnly) return false;
|
|
722
|
+
const coords = view.coordsAtPos(view.state.selection.main.head);
|
|
723
|
+
event.preventDefault();
|
|
724
|
+
this.openAt({
|
|
725
|
+
tableFrom: context.table.from,
|
|
726
|
+
at: context.at
|
|
727
|
+
}, coords?.left ?? 0, coords?.bottom ?? 0);
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
export function tableControls() {
|
|
733
|
+
return controls;
|
|
734
|
+
}
|