@avlcodemonkey/nilla-components 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/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/css/alert.css +48 -0
- package/css/autocomplete.css +38 -0
- package/css/dialog.css +61 -0
- package/css/file-to-textarea.css +8 -0
- package/css/html-editor.css +172 -0
- package/css/json.css +44 -0
- package/css/list.css +37 -0
- package/css/nav-toggle.css +92 -0
- package/css/nilla.css +16 -0
- package/css/pjax.css +58 -0
- package/css/slider-input.css +16 -0
- package/css/table.css +99 -0
- package/css/tokens.css +44 -0
- package/css/unsaved-changes.css +10 -0
- package/css/vendor/wysi.css +406 -0
- package/docs/llms.txt +319 -0
- package/package.json +121 -5
- package/src/components/Alert.js +24 -0
- package/src/components/Autocomplete.js +127 -0
- package/src/components/BaseComponent.js +78 -0
- package/src/components/BaseDialog.js +151 -0
- package/src/components/ConfirmDialog.js +34 -0
- package/src/components/CopyToClipboard.js +66 -0
- package/src/components/DateFormatter.js +51 -0
- package/src/components/DateTimeInput.js +63 -0
- package/src/components/DisplayToggle.js +53 -0
- package/src/components/FileToTextarea.js +81 -0
- package/src/components/HtmlEditor.js +309 -0
- package/src/components/InfoDialog.js +38 -0
- package/src/components/JsonFormatter.js +23 -0
- package/src/components/List.js +237 -0
- package/src/components/ListSortable.js +8 -0
- package/src/components/NavToggle.js +119 -0
- package/src/components/PJax.js +888 -0
- package/src/components/Share.js +41 -0
- package/src/components/SliderInput.js +26 -0
- package/src/components/Table.js +758 -0
- package/src/components/TimeOffsetSelect.js +29 -0
- package/src/components/UnsavedChanges.js +187 -0
- package/src/constants/Fetch.js +7 -0
- package/src/constants/HttpHeaders.js +19 -0
- package/src/constants/HttpMethods.js +14 -0
- package/src/constants/SortDirection.js +11 -0
- package/src/constants/TableElements.js +25 -0
- package/src/constants/TableSettings.js +14 -0
- package/src/errors/FetchError.js +7 -0
- package/src/events/ListReorderEvent.js +19 -0
- package/src/events/PJaxErrorEvent.js +38 -0
- package/src/events/PJaxNavigatingEvent.js +46 -0
- package/src/index.js +26 -0
- package/src/locales/en.json +55 -0
- package/src/locales/es.js +9 -0
- package/src/locales/es.json +55 -0
- package/src/models/TableSort.js +20 -0
- package/src/utils/ajaxHeaders.js +10 -0
- package/src/utils/announce.js +40 -0
- package/src/utils/defineElement.js +13 -0
- package/src/utils/editor/blockTools.js +256 -0
- package/src/utils/editor/elements.js +18 -0
- package/src/utils/editor/input.js +350 -0
- package/src/utils/editor/selection.js +125 -0
- package/src/utils/editor/tableTools.js +497 -0
- package/src/utils/escape.js +20 -0
- package/src/utils/fetchJsonArray.js +29 -0
- package/src/utils/filterArray.js +18 -0
- package/src/utils/formActionUrl.js +10 -0
- package/src/utils/formData.js +118 -0
- package/src/utils/index.js +18 -0
- package/src/utils/indexedCompare.js +18 -0
- package/src/utils/isSameOrigin.js +12 -0
- package/src/utils/locale.js +36 -0
- package/src/utils/notifyChange.js +10 -0
- package/src/utils/numberSetting.js +14 -0
- package/src/utils/pageLanguage.js +13 -0
- package/src/utils/parseUtcDate.js +22 -0
- package/src/utils/randomId.js +17 -0
- package/src/utils/registerTabTrap.js +60 -0
- package/src/utils/renderTemplate.js +44 -0
- package/src/utils/requestTimeout.js +12 -0
- package/src/utils/response.js +28 -0
- package/src/utils/sortedCompare.js +67 -0
- package/src/utils/storage.js +40 -0
- package/src/vendor/autocomplete.js +515 -0
- package/src/vendor/formatDate.js +236 -0
- package/src/vendor/licenses/autocomplete.LICENSE.md +21 -0
- package/src/vendor/licenses/fecha.LICENSE.md +21 -0
- package/src/vendor/licenses/pretty-print-json.LICENSE.md +21 -0
- package/src/vendor/licenses/wysi.LICENSE.md +21 -0
- package/src/vendor/prettyPrintJson.js +108 -0
- package/src/vendor/wyzi.js +2024 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selection helpers shared by the HTML editor tools.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Find the closest element matching a selector, without leaving the editable region.
|
|
7
|
+
* @param {Node} node Node to start from.
|
|
8
|
+
* @param {string} selector Selector to match.
|
|
9
|
+
* @param {HTMLElement} editor Editable region.
|
|
10
|
+
* @returns {HTMLElement|null} Matching element, or null.
|
|
11
|
+
*/
|
|
12
|
+
export function closestInEditor(node, selector, editor) {
|
|
13
|
+
const element = node.nodeType === 1 ? node : node.parentElement;
|
|
14
|
+
const match = element?.closest(selector);
|
|
15
|
+
return match && match !== editor && editor.contains(match) ? match : null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the selected range if it's inside the editable region.
|
|
20
|
+
* @param {HTMLElement} editor Editable region.
|
|
21
|
+
* @returns {Range|null} Selected range, or null.
|
|
22
|
+
*/
|
|
23
|
+
export function getEditorRange(editor) {
|
|
24
|
+
const selection = document.getSelection();
|
|
25
|
+
if (!editor || !selection?.rangeCount) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const range = selection.getRangeAt(0);
|
|
29
|
+
return editor.contains(range.startContainer) && editor.contains(range.endContainer) ? range : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Replace the current selection.
|
|
34
|
+
* @param {Range} range Range to select.
|
|
35
|
+
*/
|
|
36
|
+
export function setRange(range) {
|
|
37
|
+
const selection = document.getSelection();
|
|
38
|
+
selection.removeAllRanges();
|
|
39
|
+
selection.addRange(range);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Place the caret.
|
|
44
|
+
* @param {Node} node Node to place the caret in.
|
|
45
|
+
* @param {number} offset Position within the node.
|
|
46
|
+
*/
|
|
47
|
+
export function placeCaret(node, offset) {
|
|
48
|
+
const range = document.createRange();
|
|
49
|
+
range.setStart(node, offset);
|
|
50
|
+
range.collapse(true);
|
|
51
|
+
setRange(range);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Place the caret after the last thing in an element, but before a trailing line break.
|
|
56
|
+
* @param {Node} node Element to place the caret in.
|
|
57
|
+
*/
|
|
58
|
+
export function placeCaretAtEnd(node) {
|
|
59
|
+
// these can't hold the caret, so it goes next to them instead
|
|
60
|
+
const isEmptyElement = x => !!x?.matches?.('br, img, input, hr');
|
|
61
|
+
if (isEmptyElement(node)) {
|
|
62
|
+
placeCaret(node.parentNode, Array.prototype.indexOf.call(node.parentNode.childNodes, node) + 1);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let target = node;
|
|
67
|
+
while (target.lastChild && !isEmptyElement(target.lastChild)) {
|
|
68
|
+
target = target.lastChild;
|
|
69
|
+
}
|
|
70
|
+
if (target.nodeType === 3) {
|
|
71
|
+
placeCaret(target, target.nodeValue.length);
|
|
72
|
+
} else {
|
|
73
|
+
placeCaret(target, target.childNodes.length - (target.lastChild?.nodeName === 'BR' ? 1 : 0));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Replace the selection with a node and place the caret after it.
|
|
79
|
+
* @param {Range} range Selected range.
|
|
80
|
+
* @param {Node} node Node to insert, which may be a fragment.
|
|
81
|
+
*/
|
|
82
|
+
export function insertAtRange(range, node) {
|
|
83
|
+
const last = node.nodeType === 11 ? node.lastChild : node;
|
|
84
|
+
range.deleteContents();
|
|
85
|
+
range.insertNode(node);
|
|
86
|
+
if (last) {
|
|
87
|
+
const caret = document.createRange();
|
|
88
|
+
caret.setStartAfter(last);
|
|
89
|
+
caret.collapse(true);
|
|
90
|
+
setRange(caret);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Check whether a selection starts in one table cell and ends in another. Changing it would break the table.
|
|
96
|
+
* @param {HTMLElement} editor Editable region.
|
|
97
|
+
* @param {Range|null} range Selected range.
|
|
98
|
+
* @returns {boolean} True if the selection goes from one cell to another.
|
|
99
|
+
*/
|
|
100
|
+
export function crossesCells(editor, range) {
|
|
101
|
+
if (!range) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
const start = closestInEditor(range.startContainer, 'td, th', editor);
|
|
105
|
+
const end = closestInEditor(range.endContainer, 'td, th', editor);
|
|
106
|
+
return !!start && !!end && start !== end;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Tell the editor its content changed, so the textarea gets updated.
|
|
111
|
+
* @param {HTMLElement} editor Editable region.
|
|
112
|
+
*/
|
|
113
|
+
export function contentChanged(editor) {
|
|
114
|
+
editor.dispatchEvent(new Event('input', { bubbles: true }));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Change the content directly, then tell the editor about it.
|
|
119
|
+
* @param {HTMLElement} editor Editable region.
|
|
120
|
+
* @param {() => void} change Makes the change.
|
|
121
|
+
*/
|
|
122
|
+
export function applyEdit(editor, change) {
|
|
123
|
+
change();
|
|
124
|
+
contentChanged(editor);
|
|
125
|
+
}
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor tools for creating and changing tables, and the toolbar menu that runs them.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { getText } from '../locale.js';
|
|
6
|
+
import { getSelectedBlocks } from './blockTools.js';
|
|
7
|
+
import { applyEdit, closestInEditor, getEditorRange, placeCaret, placeCaretAtEnd, setRange } from './selection.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Largest number of rows or columns a new table can have.
|
|
11
|
+
* @type {number}
|
|
12
|
+
*/
|
|
13
|
+
const MaxSize = 20;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Number of rows and columns a new table starts with.
|
|
17
|
+
* @type {number}
|
|
18
|
+
*/
|
|
19
|
+
const DefaultSize = 3;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Actions in the table menu that need every row to have the same cells, in the order they're shown.
|
|
23
|
+
* @type {string[]}
|
|
24
|
+
*/
|
|
25
|
+
const GridActions = ['rowAbove', 'rowBelow', 'columnLeft', 'columnRight', 'alignLeft', 'alignCenter', 'alignRight', 'deleteRow', 'deleteColumn'];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Actions in the table menu that change a table, in the order they're shown.
|
|
29
|
+
* @type {string[]}
|
|
30
|
+
*/
|
|
31
|
+
const Actions = [...GridActions, 'deleteTable'];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Selection each menu was opened with, since using the menu's inputs moves the selection out of the editor.
|
|
35
|
+
* @type {WeakMap<HTMLElement, Range>}
|
|
36
|
+
*/
|
|
37
|
+
const savedRanges = new WeakMap();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create an empty cell.
|
|
41
|
+
* @param {string} tag Either td or th.
|
|
42
|
+
* @param {string|null} [align] Alignment of the column.
|
|
43
|
+
* @returns {HTMLTableCellElement} New cell.
|
|
44
|
+
*/
|
|
45
|
+
function createCell(tag, align = null) {
|
|
46
|
+
const cell = document.createElement(tag);
|
|
47
|
+
if (align) {
|
|
48
|
+
cell.setAttribute('align', align);
|
|
49
|
+
}
|
|
50
|
+
cell.appendChild(document.createElement('br'));
|
|
51
|
+
return cell;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check whether a row is the header row of its table.
|
|
56
|
+
* @param {HTMLTableRowElement} row Row to check.
|
|
57
|
+
* @returns {boolean} True for the header row.
|
|
58
|
+
*/
|
|
59
|
+
function isHeaderRow(row) {
|
|
60
|
+
return row === row.closest('table').rows[0]
|
|
61
|
+
&& (row.parentElement.nodeName === 'THEAD' || [...row.cells].every(cell => cell.nodeName === 'TH'));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check whether any cells in a table span more than one row or column.
|
|
66
|
+
* @param {HTMLTableElement} table Table to check.
|
|
67
|
+
* @returns {boolean} True if there are merged cells.
|
|
68
|
+
*/
|
|
69
|
+
function hasMergedCells(table) {
|
|
70
|
+
return [...table.querySelectorAll('td, th')].some(cell => cell.colSpan > 1 || cell.rowSpan > 1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Find the cell, row and table the range starts in.
|
|
75
|
+
* @param {HTMLElement} editor Editable region.
|
|
76
|
+
* @param {Range|null} range Selected range.
|
|
77
|
+
* @returns {{cell: HTMLTableCellElement, row: HTMLTableRowElement, table: HTMLTableElement}|null} Position in a table, or null.
|
|
78
|
+
*/
|
|
79
|
+
function getPosition(editor, range) {
|
|
80
|
+
const cell = range && closestInEditor(range.startContainer, 'td, th', editor);
|
|
81
|
+
return cell ? { cell, row: cell.parentElement, table: cell.closest('table') } : null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Insert a new table after the current block. The first row is the header.
|
|
86
|
+
* @param {HTMLElement} editor Editable region.
|
|
87
|
+
* @param {number} rowCount Number of rows.
|
|
88
|
+
* @param {number} columnCount Number of columns.
|
|
89
|
+
*/
|
|
90
|
+
export function insertTable(editor, rowCount, columnCount) {
|
|
91
|
+
const range = getEditorRange(editor);
|
|
92
|
+
if (!range || closestInEditor(range.startContainer, 'table, pre', editor)) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const clamp = value => Math.min(Math.max(Math.trunc(value) || DefaultSize, 1), MaxSize);
|
|
96
|
+
const columns = clamp(columnCount);
|
|
97
|
+
const createRow = (tag) => {
|
|
98
|
+
const row = document.createElement('tr');
|
|
99
|
+
row.append(...Array.from({ length: columns }, () => createCell(tag)));
|
|
100
|
+
return row;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
applyEdit(editor, () => {
|
|
104
|
+
const table = document.createElement('table');
|
|
105
|
+
table.createTHead().appendChild(createRow('th'));
|
|
106
|
+
const bodyRows = Array.from({ length: clamp(rowCount) - 1 }, () => createRow('td'));
|
|
107
|
+
if (bodyRows.length) {
|
|
108
|
+
table.appendChild(document.createElement('tbody')).append(...bodyRows);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const quote = closestInEditor(range.startContainer, 'blockquote', editor);
|
|
112
|
+
const block = getSelectedBlocks(range, quote?.contains(range.endContainer) ? quote : editor)[0];
|
|
113
|
+
if (!block) {
|
|
114
|
+
editor.appendChild(table);
|
|
115
|
+
} else if (block.nodeName === 'P' && !block.textContent.trim() && !block.querySelector('img, input')) {
|
|
116
|
+
block.replaceWith(table);
|
|
117
|
+
} else {
|
|
118
|
+
block.after(table);
|
|
119
|
+
}
|
|
120
|
+
placeCaret(table.rows[0].cells[0], 0);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Add a row above or below the current one. Nothing can go above the header row.
|
|
126
|
+
* @param {HTMLElement} editor Editable region.
|
|
127
|
+
* @param {boolean} below Add the row below the current one.
|
|
128
|
+
*/
|
|
129
|
+
function insertRow(editor, below) {
|
|
130
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
131
|
+
if (!position || (!below && isHeaderRow(position.row))) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const { cell, row, table } = position;
|
|
135
|
+
|
|
136
|
+
applyEdit(editor, () => {
|
|
137
|
+
const newRow = document.createElement('tr');
|
|
138
|
+
newRow.append(...[...row.cells].map(x => createCell('td', x.getAttribute('align'))));
|
|
139
|
+
if (row.parentElement.nodeName === 'THEAD') {
|
|
140
|
+
const body = table.tBodies[0] ?? table.appendChild(document.createElement('tbody'));
|
|
141
|
+
body.prepend(newRow);
|
|
142
|
+
} else if (below) {
|
|
143
|
+
row.after(newRow);
|
|
144
|
+
} else {
|
|
145
|
+
row.before(newRow);
|
|
146
|
+
}
|
|
147
|
+
placeCaret(newRow.cells[Math.min(cell.cellIndex, newRow.cells.length - 1)], 0);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a column to the left or right of the current one.
|
|
153
|
+
* @param {HTMLElement} editor Editable region.
|
|
154
|
+
* @param {boolean} right Add the column to the right of the current one.
|
|
155
|
+
*/
|
|
156
|
+
function insertColumn(editor, right) {
|
|
157
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
158
|
+
if (!position) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const { cell, row, table } = position;
|
|
162
|
+
const index = cell.cellIndex;
|
|
163
|
+
|
|
164
|
+
applyEdit(editor, () => {
|
|
165
|
+
for (const x of table.rows) {
|
|
166
|
+
const reference = x.cells[index];
|
|
167
|
+
if (!reference) {
|
|
168
|
+
x.appendChild(createCell(isHeaderRow(x) ? 'th' : 'td'));
|
|
169
|
+
} else if (right) {
|
|
170
|
+
reference.after(createCell(reference.nodeName.toLowerCase()));
|
|
171
|
+
} else {
|
|
172
|
+
reference.before(createCell(reference.nodeName.toLowerCase()));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
placeCaret(row.cells[right ? index + 1 : index], 0);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Set the alignment of the current column.
|
|
181
|
+
* @param {HTMLElement} editor Editable region.
|
|
182
|
+
* @param {string} align Either left, center or right.
|
|
183
|
+
*/
|
|
184
|
+
function alignColumn(editor, align) {
|
|
185
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
186
|
+
if (!position) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
applyEdit(editor, () => {
|
|
191
|
+
// uses the align attribute because the content security policy blocks inline styles
|
|
192
|
+
for (const row of position.table.rows) {
|
|
193
|
+
row.cells[position.cell.cellIndex]?.setAttribute('align', align);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Remove a table, leaving an empty paragraph to type in.
|
|
200
|
+
* @param {HTMLTableElement} table Table to remove.
|
|
201
|
+
*/
|
|
202
|
+
function removeTable(table) {
|
|
203
|
+
const paragraph = document.createElement('p');
|
|
204
|
+
paragraph.appendChild(document.createElement('br'));
|
|
205
|
+
table.replaceWith(paragraph);
|
|
206
|
+
placeCaret(paragraph, 0);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Remove table sections that no longer have any rows.
|
|
211
|
+
* @param {HTMLTableElement} table Table to clean up.
|
|
212
|
+
*/
|
|
213
|
+
function removeEmptySections(table) {
|
|
214
|
+
table.querySelectorAll('thead, tbody, tfoot').forEach((section) => {
|
|
215
|
+
if (!section.querySelector('tr')) {
|
|
216
|
+
section.remove();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Delete the current row. Deleting the header row makes the next row the header.
|
|
223
|
+
* @param {HTMLElement} editor Editable region.
|
|
224
|
+
*/
|
|
225
|
+
function deleteRow(editor) {
|
|
226
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
227
|
+
if (!position) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const { row, table } = position;
|
|
231
|
+
|
|
232
|
+
applyEdit(editor, () => {
|
|
233
|
+
if (table.rows.length === 1) {
|
|
234
|
+
removeTable(table);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const index = row.rowIndex;
|
|
239
|
+
const wasHeader = isHeaderRow(row);
|
|
240
|
+
row.remove();
|
|
241
|
+
if (wasHeader) {
|
|
242
|
+
const header = document.createElement('tr');
|
|
243
|
+
header.append(...[...table.rows[0].cells].map((x) => {
|
|
244
|
+
const cell = document.createElement('th');
|
|
245
|
+
[...x.attributes].forEach(attribute => cell.setAttribute(attribute.name, attribute.value));
|
|
246
|
+
cell.append(...x.childNodes);
|
|
247
|
+
return cell;
|
|
248
|
+
}));
|
|
249
|
+
table.rows[0].remove();
|
|
250
|
+
table.createTHead().appendChild(header);
|
|
251
|
+
}
|
|
252
|
+
removeEmptySections(table);
|
|
253
|
+
placeCaretAtEnd(table.rows[Math.min(index, table.rows.length - 1)].cells[0]);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Delete the current column.
|
|
259
|
+
* @param {HTMLElement} editor Editable region.
|
|
260
|
+
*/
|
|
261
|
+
function deleteColumn(editor) {
|
|
262
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
263
|
+
if (!position) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const { cell, row, table } = position;
|
|
267
|
+
const index = cell.cellIndex;
|
|
268
|
+
|
|
269
|
+
applyEdit(editor, () => {
|
|
270
|
+
if ([...table.rows].every(x => x.cells.length <= 1)) {
|
|
271
|
+
removeTable(table);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
for (const x of [...table.rows]) {
|
|
275
|
+
x.cells[index]?.remove();
|
|
276
|
+
// uneven tables can have rows with nothing left in them
|
|
277
|
+
if (!x.cells.length) {
|
|
278
|
+
x.remove();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
removeEmptySections(table);
|
|
282
|
+
const target = row.isConnected ? row : table.rows[0];
|
|
283
|
+
placeCaretAtEnd(target.cells[Math.min(index, target.cells.length - 1)]);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Delete the current table.
|
|
289
|
+
* @param {HTMLElement} editor Editable region.
|
|
290
|
+
*/
|
|
291
|
+
function deleteTable(editor) {
|
|
292
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
293
|
+
if (position) {
|
|
294
|
+
applyEdit(editor, () => removeTable(position.table));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Run a table action on the editor's selection.
|
|
300
|
+
* @param {HTMLElement} editor Editable region.
|
|
301
|
+
* @param {string} action Name of the action.
|
|
302
|
+
*/
|
|
303
|
+
export function runTableAction(editor, action) {
|
|
304
|
+
const position = getPosition(editor, getEditorRange(editor));
|
|
305
|
+
if (position && GridActions.includes(action) && hasMergedCells(position.table)) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const handlers = {
|
|
310
|
+
rowAbove: () => insertRow(editor, false),
|
|
311
|
+
rowBelow: () => insertRow(editor, true),
|
|
312
|
+
columnLeft: () => insertColumn(editor, false),
|
|
313
|
+
columnRight: () => insertColumn(editor, true),
|
|
314
|
+
alignLeft: () => alignColumn(editor, 'left'),
|
|
315
|
+
alignCenter: () => alignColumn(editor, 'center'),
|
|
316
|
+
alignRight: () => alignColumn(editor, 'right'),
|
|
317
|
+
deleteRow: () => deleteRow(editor),
|
|
318
|
+
deleteColumn: () => deleteColumn(editor),
|
|
319
|
+
deleteTable: () => deleteTable(editor),
|
|
320
|
+
};
|
|
321
|
+
handlers[action]?.();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Build the toolbar button and menu for tables. Wysi copies the toolbar into each editor, so events are handled on the document.
|
|
326
|
+
* @param {(key: string) => string} [translate] Gets the text for a key.
|
|
327
|
+
* @returns {HTMLElement} Menu wrapper.
|
|
328
|
+
*/
|
|
329
|
+
export function renderTableMenu(translate = getText) {
|
|
330
|
+
const createButton = (action, label) => {
|
|
331
|
+
const button = document.createElement('button');
|
|
332
|
+
button.type = 'button';
|
|
333
|
+
button.dataset.tableAction = action;
|
|
334
|
+
button.textContent = translate(label);
|
|
335
|
+
return button;
|
|
336
|
+
};
|
|
337
|
+
const createSizeInput = (name, label) => {
|
|
338
|
+
const element = document.createElement('label');
|
|
339
|
+
const input = document.createElement('input');
|
|
340
|
+
input.type = 'number';
|
|
341
|
+
// belonging to no form keeps it from being submitted, or blocking the post from saving
|
|
342
|
+
input.setAttribute('form', 'wysi-detached');
|
|
343
|
+
input.name = `wysi-table-${name}`;
|
|
344
|
+
input.min = '1';
|
|
345
|
+
input.max = `${MaxSize}`;
|
|
346
|
+
input.setAttribute('value', `${DefaultSize}`);
|
|
347
|
+
input.setAttribute(`data-table-${name}`, '');
|
|
348
|
+
element.append(translate(label), input);
|
|
349
|
+
return element;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const wrapper = document.createElement('div');
|
|
353
|
+
wrapper.className = 'wysi-table';
|
|
354
|
+
wrapper.innerHTML = `
|
|
355
|
+
<button type="button" class="wysi-table-toggle" aria-expanded="false" aria-pressed="false" data-action="table">
|
|
356
|
+
<svg><use href="#wysi-table"></use></svg>
|
|
357
|
+
</button>
|
|
358
|
+
<div class="wysi-table-menu" hidden>
|
|
359
|
+
<div class="wysi-table-size"></div>
|
|
360
|
+
</div>`;
|
|
361
|
+
const toggle = wrapper.querySelector('.wysi-table-toggle');
|
|
362
|
+
toggle.title = translate('editor.table');
|
|
363
|
+
toggle.setAttribute('aria-label', translate('editor.table'));
|
|
364
|
+
|
|
365
|
+
const menu = wrapper.querySelector('.wysi-table-menu');
|
|
366
|
+
menu.querySelector('.wysi-table-size').append(createSizeInput('rows', 'editor.rows'), createSizeInput('columns', 'editor.columns'));
|
|
367
|
+
menu.appendChild(createButton('insert', 'editor.insertTable'));
|
|
368
|
+
for (const action of Actions) {
|
|
369
|
+
const button = createButton(action, `editor.${action}`);
|
|
370
|
+
if (action === 'deleteTable') {
|
|
371
|
+
// deleting a whole table asks first
|
|
372
|
+
button.setAttribute('data-html-delete-table', '');
|
|
373
|
+
const confirm = document.createElement('nilla-confirm');
|
|
374
|
+
confirm.appendChild(button);
|
|
375
|
+
menu.appendChild(confirm);
|
|
376
|
+
} else {
|
|
377
|
+
menu.appendChild(button);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return wrapper;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Show or hide a table menu, enabling the actions that apply to the selection.
|
|
385
|
+
* @param {HTMLElement} wrapper Menu wrapper.
|
|
386
|
+
* @param {boolean} open Show the menu.
|
|
387
|
+
*/
|
|
388
|
+
function setMenuOpen(wrapper, open) {
|
|
389
|
+
wrapper.querySelector('.wysi-table-menu').hidden = !open;
|
|
390
|
+
wrapper.querySelector('.wysi-table-toggle').setAttribute('aria-expanded', `${open}`);
|
|
391
|
+
if (!open) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const editor = wrapper.closest('.wysi-wrapper').querySelector('.wysi-editor');
|
|
396
|
+
const range = getEditorRange(editor);
|
|
397
|
+
if (range) {
|
|
398
|
+
savedRanges.set(wrapper, range.cloneRange());
|
|
399
|
+
} else {
|
|
400
|
+
savedRanges.delete(wrapper);
|
|
401
|
+
}
|
|
402
|
+
const position = getPosition(editor, range);
|
|
403
|
+
const merged = position ? hasMergedCells(position.table) : false;
|
|
404
|
+
wrapper.querySelectorAll('[data-table-action]').forEach((button) => {
|
|
405
|
+
const action = button.dataset.tableAction;
|
|
406
|
+
if (action === 'insert') {
|
|
407
|
+
button.disabled = !range || !!position || !!closestInEditor(range.startContainer, 'pre', editor);
|
|
408
|
+
} else {
|
|
409
|
+
button.disabled = !position || (merged && GridActions.includes(action));
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Run the menu action that was clicked.
|
|
416
|
+
* @param {HTMLElement} wrapper Menu wrapper.
|
|
417
|
+
* @param {string} action Name of the action.
|
|
418
|
+
*/
|
|
419
|
+
function runMenuAction(wrapper, action) {
|
|
420
|
+
const editor = wrapper.closest('.wysi-wrapper').querySelector('.wysi-editor');
|
|
421
|
+
const range = savedRanges.get(wrapper);
|
|
422
|
+
if (range) {
|
|
423
|
+
editor.focus();
|
|
424
|
+
setRange(range);
|
|
425
|
+
}
|
|
426
|
+
if (action === 'insert') {
|
|
427
|
+
insertTable(editor, Number(wrapper.querySelector('[data-table-rows]').value), Number(wrapper.querySelector('[data-table-columns]').value));
|
|
428
|
+
} else {
|
|
429
|
+
runTableAction(editor, action);
|
|
430
|
+
}
|
|
431
|
+
setMenuOpen(wrapper, false);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Handle clicks for every table menu, closing menus when the click is elsewhere.
|
|
436
|
+
* @param {MouseEvent} event Click event.
|
|
437
|
+
*/
|
|
438
|
+
export function handleTableMenuClick(event) {
|
|
439
|
+
const wrapper = event.target.closest?.('.wysi-table');
|
|
440
|
+
document.querySelectorAll('.wysi-table').forEach((x) => {
|
|
441
|
+
if (x !== wrapper && !x.querySelector('.wysi-table-menu').hidden) {
|
|
442
|
+
setMenuOpen(x, false);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
if (!wrapper) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (event.target.closest('.wysi-table-toggle')) {
|
|
450
|
+
setMenuOpen(wrapper, wrapper.querySelector('.wysi-table-menu').hidden);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const button = event.target.closest('[data-table-action]');
|
|
454
|
+
if (button && !button.disabled) {
|
|
455
|
+
runMenuAction(wrapper, button.dataset.tableAction);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Close an editor's table menu when Escape is pressed anywhere in the editor.
|
|
461
|
+
* @param {KeyboardEvent} event Key down event.
|
|
462
|
+
*/
|
|
463
|
+
function closeMenuFrom(event) {
|
|
464
|
+
const wrapper = event.target.closest?.('.wysi-wrapper')?.querySelector('.wysi-table');
|
|
465
|
+
const menu = wrapper?.querySelector('.wysi-table-menu');
|
|
466
|
+
if (menu && !menu.hidden) {
|
|
467
|
+
event.preventDefault();
|
|
468
|
+
// hiding the menu would lose focus that's in it
|
|
469
|
+
if (menu.contains(document.activeElement)) {
|
|
470
|
+
wrapper.querySelector('.wysi-table-toggle').focus();
|
|
471
|
+
}
|
|
472
|
+
setMenuOpen(wrapper, false);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Keep the selection when menu buttons are pressed, and handle the Enter and Escape keys in the menu.
|
|
478
|
+
* @param {Event} event Mouse down or key down event.
|
|
479
|
+
*/
|
|
480
|
+
export function handleTableMenuInput(event) {
|
|
481
|
+
if (event.key === 'Escape') {
|
|
482
|
+
closeMenuFrom(event);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const wrapper = event.target.closest?.('.wysi-table');
|
|
486
|
+
if (!wrapper) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (event.type === 'mousedown') {
|
|
490
|
+
if (event.target.closest('button')) {
|
|
491
|
+
event.preventDefault();
|
|
492
|
+
}
|
|
493
|
+
} else if (event.key === 'Enter' && event.target.matches('input')) {
|
|
494
|
+
event.preventDefault();
|
|
495
|
+
runMenuAction(wrapper, 'insert');
|
|
496
|
+
}
|
|
497
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const entityMap = {
|
|
2
|
+
'&': '&',
|
|
3
|
+
'<': '<',
|
|
4
|
+
'>': '>',
|
|
5
|
+
'"': '"',
|
|
6
|
+
'\'': ''',
|
|
7
|
+
'/': '/',
|
|
8
|
+
'`': '`',
|
|
9
|
+
'=': '=',
|
|
10
|
+
};
|
|
11
|
+
const escapeRegex = /[&<>"'`=/]/g;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Escapes special characters in a string to prevent XSS attacks.
|
|
15
|
+
* @param {string} string The string to escape.
|
|
16
|
+
* @returns {string} The escaped string.
|
|
17
|
+
*/
|
|
18
|
+
export default function escape(string) {
|
|
19
|
+
return `${string}`.replaceAll(escapeRegex, x => entityMap[x]);
|
|
20
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import DefaultTimeout from '../constants/Fetch.js';
|
|
2
|
+
import FetchError from '../errors/FetchError.js';
|
|
3
|
+
import ajaxHeaders from './ajaxHeaders.js';
|
|
4
|
+
import isSameOrigin from './isSameOrigin.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Requests a JSON list from this site, with the ajax headers and a timeout.
|
|
8
|
+
* @param {string|URL} url Absolute or relative URL on this site.
|
|
9
|
+
* @param {{ method?: string, body?: FormData, timeout?: number }} [options] Method and body, for requests other than a plain
|
|
10
|
+
* GET, and a timeout in milliseconds instead of the default.
|
|
11
|
+
* @returns {Promise<object[]>} Items from the response.
|
|
12
|
+
* @throws {FetchError} If the URL is on another site, the response isn't successful, or it isn't a list.
|
|
13
|
+
*/
|
|
14
|
+
export default async function fetchJsonArray(url, { timeout = DefaultTimeout, ...options } = {}) {
|
|
15
|
+
if (!isSameOrigin(url)) {
|
|
16
|
+
throw new FetchError(`Request to '${url}' was not sent, since it isn't on this site.`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const response = await fetch(url, { ...options, headers: ajaxHeaders(), signal: AbortSignal.timeout(timeout) });
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
throw new FetchError(`HTTP ${response.status}: ${response.statusText}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const json = await response.json();
|
|
25
|
+
if (!Array.isArray(json)) {
|
|
26
|
+
throw new FetchError(`Request to '${url}' returned invalid response.`);
|
|
27
|
+
}
|
|
28
|
+
return json;
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Searches for a string in the properties of an object.
|
|
3
|
+
* @param {string} search Search string. Each word must be found in at least one of the properties, ignoring case; words can
|
|
4
|
+
* match different properties, so "ada platform" matches a row with name "Ada" and team "Platform".
|
|
5
|
+
* @param {string[]|null} properties Names of the properties to search, such as the columns a table shows, so values the user can't
|
|
6
|
+
* see don't match. Null searches every property.
|
|
7
|
+
* @param {object} obj Object to search in. Properties whose name starts with an underscore are internal, such as the row index the
|
|
8
|
+
* table adds, so they are not searched.
|
|
9
|
+
* @returns {boolean} True if every word of the search is found, else false.
|
|
10
|
+
*/
|
|
11
|
+
export default function filterArray(search, properties, obj) {
|
|
12
|
+
const words = (search || '').toLowerCase().split(/\s+/).filter(x => x);
|
|
13
|
+
// null and undefined are blank to the user, so they shouldn't match a search for "null"
|
|
14
|
+
const values = Object.entries(obj)
|
|
15
|
+
.filter(([key]) => !key.startsWith('_') && (!properties || properties.includes(key)))
|
|
16
|
+
.map(([, value]) => (value === null || value === undefined ? '' : `${value}`.toLowerCase()));
|
|
17
|
+
return words.every(word => values.some(value => value.includes(word)));
|
|
18
|
+
}
|