@spfx-fast/sharepoint-ui 0.0.0-stage → 0.4.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/AGENTS.md +211 -0
- package/LICENSE +21 -0
- package/README.md +897 -2
- package/components.json +5810 -0
- package/dist/appearance.d.ts +6 -0
- package/dist/appearance.js +217 -0
- package/dist/elements/accordion.d.ts +9 -0
- package/dist/elements/accordion.js +338 -0
- package/dist/elements/alert.d.ts +2 -0
- package/dist/elements/alert.js +62 -0
- package/dist/elements/avatar-group-primitives.d.ts +3 -0
- package/dist/elements/avatar-group-primitives.js +189 -0
- package/dist/elements/avatar-group.d.ts +2 -0
- package/dist/elements/avatar-group.js +110 -0
- package/dist/elements/avatar.d.ts +4 -0
- package/dist/elements/avatar.js +251 -0
- package/dist/elements/badge.d.ts +8 -0
- package/dist/elements/badge.js +182 -0
- package/dist/elements/breadcrumb.d.ts +4 -0
- package/dist/elements/breadcrumb.js +233 -0
- package/dist/elements/button-group.d.ts +1 -0
- package/dist/elements/button-group.js +20 -0
- package/dist/elements/button-variants.d.ts +6 -0
- package/dist/elements/button-variants.js +339 -0
- package/dist/elements/button.d.ts +5 -0
- package/dist/elements/button.js +233 -0
- package/dist/elements/card-primitives.d.ts +3 -0
- package/dist/elements/card-primitives.js +83 -0
- package/dist/elements/card.d.ts +5 -0
- package/dist/elements/card.js +213 -0
- package/dist/elements/carousel.d.ts +92 -0
- package/dist/elements/carousel.js +1112 -0
- package/dist/elements/color-picker.d.ts +24 -0
- package/dist/elements/color-picker.js +1004 -0
- package/dist/elements/combobox.d.ts +1 -0
- package/dist/elements/combobox.js +1012 -0
- package/dist/elements/data-grid-model.d.ts +117 -0
- package/dist/elements/data-grid-model.js +294 -0
- package/dist/elements/data-grid.d.ts +29 -0
- package/dist/elements/data-grid.js +1567 -0
- package/dist/elements/date-picker.d.ts +1 -0
- package/dist/elements/date-picker.js +486 -0
- package/dist/elements/dialog.d.ts +23 -0
- package/dist/elements/dialog.js +409 -0
- package/dist/elements/divider.d.ts +2 -0
- package/dist/elements/divider.js +66 -0
- package/dist/elements/drawer.d.ts +3 -0
- package/dist/elements/drawer.js +308 -0
- package/dist/elements/field-composition.d.ts +26 -0
- package/dist/elements/field-composition.js +133 -0
- package/dist/elements/field.d.ts +1 -0
- package/dist/elements/field.js +67 -0
- package/dist/elements/file-upload.d.ts +6 -0
- package/dist/elements/file-upload.js +211 -0
- package/dist/elements/floating-position.d.ts +26 -0
- package/dist/elements/floating-position.js +39 -0
- package/dist/elements/image.d.ts +28 -0
- package/dist/elements/image.js +38 -0
- package/dist/elements/info.d.ts +9 -0
- package/dist/elements/info.js +102 -0
- package/dist/elements/input.d.ts +5 -0
- package/dist/elements/input.js +255 -0
- package/dist/elements/label.d.ts +15 -0
- package/dist/elements/label.js +56 -0
- package/dist/elements/link.d.ts +1 -0
- package/dist/elements/link.js +92 -0
- package/dist/elements/list.d.ts +19 -0
- package/dist/elements/list.js +297 -0
- package/dist/elements/menu.d.ts +33 -0
- package/dist/elements/menu.js +1103 -0
- package/dist/elements/message-bar.d.ts +10 -0
- package/dist/elements/message-bar.js +299 -0
- package/dist/elements/nav.d.ts +32 -0
- package/dist/elements/nav.js +791 -0
- package/dist/elements/overflow.d.ts +9 -0
- package/dist/elements/overflow.js +218 -0
- package/dist/elements/persona.d.ts +3 -0
- package/dist/elements/persona.js +124 -0
- package/dist/elements/popover.d.ts +77 -0
- package/dist/elements/popover.js +840 -0
- package/dist/elements/progress-bar.d.ts +4 -0
- package/dist/elements/progress-bar.js +82 -0
- package/dist/elements/radio-group.d.ts +1 -0
- package/dist/elements/radio-group.js +303 -0
- package/dist/elements/rating-display.d.ts +2 -0
- package/dist/elements/rating-display.js +125 -0
- package/dist/elements/rating.d.ts +2 -0
- package/dist/elements/rating.js +206 -0
- package/dist/elements/rich-text-editor.d.ts +18 -0
- package/dist/elements/rich-text-editor.js +508 -0
- package/dist/elements/search.d.ts +3 -0
- package/dist/elements/search.js +207 -0
- package/dist/elements/select.d.ts +6 -0
- package/dist/elements/select.js +291 -0
- package/dist/elements/shared.d.ts +19 -0
- package/dist/elements/shared.js +396 -0
- package/dist/elements/skeleton.d.ts +6 -0
- package/dist/elements/skeleton.js +91 -0
- package/dist/elements/slider.d.ts +1 -0
- package/dist/elements/slider.js +248 -0
- package/dist/elements/spin-button.d.ts +7 -0
- package/dist/elements/spin-button.js +499 -0
- package/dist/elements/spinner.d.ts +4 -0
- package/dist/elements/spinner.js +124 -0
- package/dist/elements/swatch-picker.d.ts +5 -0
- package/dist/elements/swatch-picker.js +363 -0
- package/dist/elements/table.d.ts +21 -0
- package/dist/elements/table.js +261 -0
- package/dist/elements/tabs.d.ts +1 -0
- package/dist/elements/tabs.js +276 -0
- package/dist/elements/tag-picker.d.ts +29 -0
- package/dist/elements/tag-picker.js +697 -0
- package/dist/elements/tags.d.ts +21 -0
- package/dist/elements/tags.js +421 -0
- package/dist/elements/teaching-popover.d.ts +32 -0
- package/dist/elements/teaching-popover.js +412 -0
- package/dist/elements/text.d.ts +5 -0
- package/dist/elements/text.js +69 -0
- package/dist/elements/textarea.d.ts +1 -0
- package/dist/elements/textarea.js +182 -0
- package/dist/elements/toast.d.ts +25 -0
- package/dist/elements/toast.js +337 -0
- package/dist/elements/toaster.d.ts +6 -0
- package/dist/elements/toaster.js +356 -0
- package/dist/elements/toggle.d.ts +2 -0
- package/dist/elements/toggle.js +246 -0
- package/dist/elements/toolbar.d.ts +12 -0
- package/dist/elements/toolbar.js +381 -0
- package/dist/elements/tooltip.d.ts +76 -0
- package/dist/elements/tooltip.js +510 -0
- package/dist/elements/tree-model.d.ts +19 -0
- package/dist/elements/tree-model.js +34 -0
- package/dist/elements/tree.d.ts +5 -0
- package/dist/elements/tree.js +273 -0
- package/dist/elements/view-card.d.ts +2 -0
- package/dist/elements/view-card.js +78 -0
- package/dist/elements/virtual-list.d.ts +41 -0
- package/dist/elements/virtual-list.js +369 -0
- package/dist/elements.d.ts +72 -0
- package/dist/elements.js +207 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +66 -0
- package/dist/react-theme-styles.d.ts +2 -0
- package/dist/react-theme-styles.js +75 -0
- package/dist/react.d.ts +4540 -0
- package/dist/react.js +6799 -0
- package/dist/theme.d.ts +169 -0
- package/dist/theme.js +167 -0
- package/dist/virtualizer-hooks.d.ts +80 -0
- package/dist/virtualizer-hooks.js +253 -0
- package/package.json +87 -4
- package/scripts/generate-token-index.mjs +43 -0
- package/scripts/sync-component-catalog-signatures.mjs +55 -0
- package/scripts/validate-component-catalog.mjs +44 -0
- package/skills/sharepoint-ui/SKILL.md +12 -0
- package/skills/sharepoint-ui/references/choosing-components.md +20 -0
- package/skills/sharepoint-ui/references/sharepoint-patterns.md +17 -0
- package/tokens.json +3777 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { base, syncHostAccessibleName } from "./shared.js";
|
|
2
|
+
const ALLOWED = new Set(["P", "DIV", "SPAN", "BR", "HR", "STRONG", "B", "EM", "I", "U", "S", "DEL", "INS", "UL", "OL", "LI", "A", "H1", "H2", "H3", "H4", "H5", "H6", "BLOCKQUOTE", "PRE", "TABLE", "THEAD", "TBODY", "TFOOT", "TR", "TH", "TD", "CAPTION", "SUP", "SUB", "CODE", "IMG"]);
|
|
3
|
+
const DROP_CONTENT = new Set(["SCRIPT", "STYLE", "IFRAME", "OBJECT", "EMBED", "TEMPLATE"]);
|
|
4
|
+
function safeHref(value) {
|
|
5
|
+
if (/[\u0000-\u001f\u007f]/.test(value))
|
|
6
|
+
return false;
|
|
7
|
+
try {
|
|
8
|
+
return ["http:", "https:", "mailto:"].includes(new URL(value.trim(), document.baseURI).protocol);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function safeImageSrc(value) {
|
|
15
|
+
try {
|
|
16
|
+
return ["http:", "https:"].includes(new URL(value.trim(), document.baseURI).protocol);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function cleanHtml(html) {
|
|
23
|
+
const doc = new DOMParser().parseFromString(html, "text/html");
|
|
24
|
+
const clean = (node) => {
|
|
25
|
+
for (const child of [...node.childNodes]) {
|
|
26
|
+
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
27
|
+
const element = child;
|
|
28
|
+
if (DROP_CONTENT.has(element.tagName)) {
|
|
29
|
+
child.remove();
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (!ALLOWED.has(element.tagName)) {
|
|
33
|
+
clean(element);
|
|
34
|
+
child.replaceWith(...[...child.childNodes]);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (const attr of [...element.attributes]) {
|
|
38
|
+
const link = element.tagName === "A" && attr.name === "href" && safeHref(attr.value);
|
|
39
|
+
const span = ["TD", "TH"].includes(element.tagName) && ["colspan", "rowspan"].includes(attr.name) && /^\d{1,2}$/.test(attr.value) && Number(attr.value) > 0;
|
|
40
|
+
const scope = element.tagName === "TH" && attr.name === "scope" && ["row", "col", "rowgroup", "colgroup"].includes(attr.value);
|
|
41
|
+
const image = element.tagName === "IMG" && ((attr.name === "src" && safeImageSrc(attr.value)) || attr.name === "alt" || ((attr.name === "width" || attr.name === "height") && /^\d{1,4}$/.test(attr.value)));
|
|
42
|
+
if (!link && !span && !scope && !image)
|
|
43
|
+
element.removeAttribute(attr.name);
|
|
44
|
+
}
|
|
45
|
+
clean(element);
|
|
46
|
+
}
|
|
47
|
+
else if (child.nodeType !== Node.TEXT_NODE)
|
|
48
|
+
child.remove();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
clean(doc.body);
|
|
52
|
+
return doc.body.innerHTML;
|
|
53
|
+
}
|
|
54
|
+
export function defineRichTextEditor() {
|
|
55
|
+
if (customElements.get("spui-rich-text-editor"))
|
|
56
|
+
return;
|
|
57
|
+
class SpuiRichTextEditor extends HTMLElement {
|
|
58
|
+
static formAssociated = true;
|
|
59
|
+
static observedAttributes = ["label", "editor-label", "placeholder", "disabled", "read-only", "required", "required-message", "value", "name", "aria-label", "aria-labelledby", "aria-description", "aria-describedby", "aria-invalid", "bold-label", "italic-label", "underline-label", "bullet-list-label", "number-list-label", "link-label", "undo-label", "redo-label", "insert-table-label", "insert-image-label", "image-input-label", "insert-row-above-label", "insert-row-below-label", "insert-column-before-label", "insert-column-after-label", "delete-row-label", "delete-column-label", "delete-table-label", "toolbar-label", "link-prompt"];
|
|
60
|
+
editor;
|
|
61
|
+
internals;
|
|
62
|
+
formDisabled = false;
|
|
63
|
+
pendingValue;
|
|
64
|
+
customError = "";
|
|
65
|
+
savedRange;
|
|
66
|
+
/** Uploads to a durable asset store and returns the image URL to persist in editor HTML. */
|
|
67
|
+
imageUploader;
|
|
68
|
+
get uploadImage() { return this.imageUploader; }
|
|
69
|
+
set uploadImage(value) { this.imageUploader = value; this.syncImageButton(); }
|
|
70
|
+
imageInput;
|
|
71
|
+
uploadingImage = false;
|
|
72
|
+
savedCell;
|
|
73
|
+
selectedTablePath;
|
|
74
|
+
events;
|
|
75
|
+
constructor() { super(); try {
|
|
76
|
+
this.internals = this.attachInternals();
|
|
77
|
+
}
|
|
78
|
+
catch { /* Editing remains available in engines without form association. */ } }
|
|
79
|
+
connectedCallback() {
|
|
80
|
+
this.events?.abort();
|
|
81
|
+
this.events = new AbortController();
|
|
82
|
+
this.render();
|
|
83
|
+
document.addEventListener("selectionchange", this.onSelectionChange, { signal: this.events.signal });
|
|
84
|
+
if (this.pendingValue !== undefined)
|
|
85
|
+
this.value = this.pendingValue;
|
|
86
|
+
else if (this.hasAttribute("value"))
|
|
87
|
+
this.value = this.getAttribute("value") ?? "";
|
|
88
|
+
this.syncForm();
|
|
89
|
+
}
|
|
90
|
+
disconnectedCallback() { this.events?.abort(); }
|
|
91
|
+
attributeChangedCallback(name) {
|
|
92
|
+
if (!this.isConnected)
|
|
93
|
+
return;
|
|
94
|
+
if (name === "value")
|
|
95
|
+
this.value = this.getAttribute("value") ?? "";
|
|
96
|
+
else if ((name === "disabled" || name === "read-only") && this.editor) {
|
|
97
|
+
this.syncDisabled();
|
|
98
|
+
this.syncForm();
|
|
99
|
+
}
|
|
100
|
+
else if (name === "required" && this.editor) {
|
|
101
|
+
this.editor.setAttribute("aria-required", String(this.hasAttribute("required")));
|
|
102
|
+
this.syncForm();
|
|
103
|
+
}
|
|
104
|
+
else if (name === "required-message" && this.editor)
|
|
105
|
+
this.syncForm();
|
|
106
|
+
else if (name === "placeholder" && this.editor)
|
|
107
|
+
this.editor.dataset.placeholder = this.getAttribute("placeholder") ?? "Write something…";
|
|
108
|
+
else if (name === "label" && this.editor) {
|
|
109
|
+
const label = this.shadowRoot?.querySelector("label");
|
|
110
|
+
if (label) {
|
|
111
|
+
label.textContent = this.getAttribute("label") ?? "";
|
|
112
|
+
label.hidden = !this.hasAttribute("label");
|
|
113
|
+
}
|
|
114
|
+
this.syncEditorLabel();
|
|
115
|
+
this.refreshStrings();
|
|
116
|
+
}
|
|
117
|
+
else if (name === "editor-label" && this.editor)
|
|
118
|
+
this.syncEditorLabel();
|
|
119
|
+
else if (["aria-label", "aria-labelledby", "aria-description", "aria-describedby"].includes(name) && this.editor)
|
|
120
|
+
syncHostAccessibleName(this, this.editor);
|
|
121
|
+
else if (name === "aria-invalid" && this.editor) {
|
|
122
|
+
const value = this.getAttribute(name);
|
|
123
|
+
if (value === null)
|
|
124
|
+
this.editor.removeAttribute(name);
|
|
125
|
+
else
|
|
126
|
+
this.editor.setAttribute(name, value);
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
this.refreshStrings();
|
|
130
|
+
}
|
|
131
|
+
syncEditorLabel() { this.editor.setAttribute("aria-label", this.getAttribute("aria-label") ?? this.getAttribute("label") ?? this.getAttribute("editor-label") ?? "Rich text"); }
|
|
132
|
+
get value() { return this.editor ? cleanHtml(this.editor.innerHTML) : this.pendingValue ?? this.getAttribute("value") ?? ""; }
|
|
133
|
+
set value(value) { this.pendingValue = value; if (this.editor) {
|
|
134
|
+
this.editor.innerHTML = cleanHtml(value);
|
|
135
|
+
this.syncForm();
|
|
136
|
+
} }
|
|
137
|
+
get form() { return this.internals?.form ?? null; }
|
|
138
|
+
get validity() { return this.internals?.validity; }
|
|
139
|
+
get validationMessage() { return this.internals?.validationMessage ?? ""; }
|
|
140
|
+
get willValidate() { return Boolean(this.editor && !this.formDisabled && !this.hasAttribute("disabled") && !this.hasAttribute("read-only") && (this.internals?.willValidate ?? true)); }
|
|
141
|
+
checkValidity() { return this.willValidate ? this.internals?.checkValidity() ?? true : true; }
|
|
142
|
+
reportValidity() { return this.willValidate ? this.internals?.reportValidity() ?? true : true; }
|
|
143
|
+
setCustomValidity(message) { this.customError = String(message ?? ""); this.syncForm(); }
|
|
144
|
+
formResetCallback() { this.value = this.getAttribute("value") ?? ""; }
|
|
145
|
+
formStateRestoreCallback(state) { if (typeof state === "string")
|
|
146
|
+
this.value = state; }
|
|
147
|
+
formDisabledCallback(disabled) { this.formDisabled = disabled; if (this.editor) {
|
|
148
|
+
this.syncDisabled();
|
|
149
|
+
this.syncForm();
|
|
150
|
+
} }
|
|
151
|
+
syncDisabled() {
|
|
152
|
+
const disabled = this.formDisabled || this.hasAttribute("disabled");
|
|
153
|
+
const readOnly = disabled || this.hasAttribute("read-only");
|
|
154
|
+
this.editor.contentEditable = String(!readOnly);
|
|
155
|
+
this.editor.setAttribute("aria-readonly", String(this.hasAttribute("read-only")));
|
|
156
|
+
this.editor.setAttribute("aria-disabled", String(disabled));
|
|
157
|
+
this.shadowRoot?.querySelectorAll("[role=toolbar] button").forEach(button => { button.disabled = readOnly; });
|
|
158
|
+
this.syncImageButton();
|
|
159
|
+
}
|
|
160
|
+
syncImageButton() {
|
|
161
|
+
const button = this.shadowRoot?.querySelector('[data-command="insertImage"]');
|
|
162
|
+
if (button)
|
|
163
|
+
button.disabled = this.formDisabled || this.hasAttribute("disabled") || this.hasAttribute("read-only") || !this.imageUploader || this.uploadingImage;
|
|
164
|
+
}
|
|
165
|
+
syncForm() { const value = this.value; this.pendingValue = value; this.internals?.setFormValue(value || null); const active = this.willValidate; const missing = active && this.hasAttribute("required") && !this.editor?.textContent?.trim(); const customError = active && Boolean(this.customError); const flags = { ...(missing ? { valueMissing: true } : {}), ...(customError ? { customError: true } : {}) }; const message = customError ? this.customError : missing ? this.getAttribute("required-message") ?? "Enter some text." : ""; try {
|
|
166
|
+
this.internals?.setValidity(flags, message);
|
|
167
|
+
}
|
|
168
|
+
catch { /* Engines without form association remain editable. */ } }
|
|
169
|
+
sanitizeEditor() {
|
|
170
|
+
const html = this.editor.innerHTML;
|
|
171
|
+
const value = cleanHtml(html);
|
|
172
|
+
if (value === html)
|
|
173
|
+
return;
|
|
174
|
+
const cell = this.selectedCell();
|
|
175
|
+
const tableIndex = cell ? [...this.editor.querySelectorAll("table")].indexOf(cell.closest("table")) : -1;
|
|
176
|
+
const rowIndex = cell?.closest("tr")?.rowIndex ?? -1;
|
|
177
|
+
const cellIndex = cell?.cellIndex ?? -1;
|
|
178
|
+
this.editor.innerHTML = value;
|
|
179
|
+
const restoredCell = tableIndex >= 0 && rowIndex >= 0 && cellIndex >= 0
|
|
180
|
+
? this.editor.querySelectorAll("table")[tableIndex]?.rows[rowIndex]?.cells[cellIndex]
|
|
181
|
+
: undefined;
|
|
182
|
+
this.savedCell = restoredCell;
|
|
183
|
+
if (restoredCell) {
|
|
184
|
+
const range = document.createRange();
|
|
185
|
+
range.selectNodeContents(restoredCell);
|
|
186
|
+
range.collapse(true);
|
|
187
|
+
this.savedRange = range;
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
this.savedRange = undefined;
|
|
191
|
+
this.syncTableActions();
|
|
192
|
+
}
|
|
193
|
+
selectedCell() {
|
|
194
|
+
const node = this.savedRange?.startContainer;
|
|
195
|
+
const element = node instanceof Element ? node : node?.parentElement;
|
|
196
|
+
const cell = element?.closest("td,th");
|
|
197
|
+
if (cell && this.editor.contains(cell)) {
|
|
198
|
+
this.rememberCell(cell);
|
|
199
|
+
return cell;
|
|
200
|
+
}
|
|
201
|
+
if (this.savedCell && this.editor.contains(this.savedCell))
|
|
202
|
+
return this.savedCell;
|
|
203
|
+
const path = this.selectedTablePath;
|
|
204
|
+
return path ? this.editor.querySelectorAll("table")[path.table]?.rows[path.row]?.cells[path.cell] : undefined;
|
|
205
|
+
}
|
|
206
|
+
rememberCell(cell) {
|
|
207
|
+
const table = cell.closest("table");
|
|
208
|
+
this.selectedTablePath = table ? { table: [...this.editor.querySelectorAll("table")].indexOf(table), row: cell.parentElement.rowIndex, cell: cell.cellIndex } : undefined;
|
|
209
|
+
}
|
|
210
|
+
selection() {
|
|
211
|
+
const root = this.editor.getRootNode();
|
|
212
|
+
return root instanceof ShadowRoot ? root.getSelection?.() ?? document.getSelection() : document.getSelection();
|
|
213
|
+
}
|
|
214
|
+
onSelectionChange = () => {
|
|
215
|
+
const selection = this.selection();
|
|
216
|
+
if (selection?.rangeCount) {
|
|
217
|
+
const range = selection.getRangeAt(0);
|
|
218
|
+
if (this.editor.contains(range.commonAncestorContainer)) {
|
|
219
|
+
this.savedRange = range.cloneRange();
|
|
220
|
+
const node = range.startContainer;
|
|
221
|
+
const element = node instanceof Element ? node : node.parentElement;
|
|
222
|
+
const cell = element?.closest("td,th");
|
|
223
|
+
if (cell && this.editor.contains(cell)) {
|
|
224
|
+
this.savedCell = cell;
|
|
225
|
+
this.rememberCell(cell);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
this.syncTableActions();
|
|
230
|
+
};
|
|
231
|
+
onEditorClick = (event) => {
|
|
232
|
+
const cell = event.composedPath().find((node) => node instanceof HTMLTableCellElement && this.editor.contains(node));
|
|
233
|
+
if (cell) {
|
|
234
|
+
this.savedCell = cell;
|
|
235
|
+
this.rememberCell(cell);
|
|
236
|
+
}
|
|
237
|
+
this.onSelectionChange();
|
|
238
|
+
};
|
|
239
|
+
syncTableActions() {
|
|
240
|
+
const cell = this.selectedCell();
|
|
241
|
+
const table = cell?.closest("table");
|
|
242
|
+
const hasSpans = Boolean(table && [...table.querySelectorAll("td,th")].some(cell => cell.rowSpan !== 1 || cell.colSpan !== 1));
|
|
243
|
+
const rowCount = table?.rows.length ?? 0;
|
|
244
|
+
const columnCount = table ? Math.min(...[...table.rows].map(row => row.cells.length)) : 0;
|
|
245
|
+
this.shadowRoot?.querySelectorAll("button[data-table-command]").forEach(button => {
|
|
246
|
+
const command = button.dataset.tableCommand;
|
|
247
|
+
button.disabled = !cell || !table || hasSpans || (command === "deleteRow" && rowCount <= 1) || (command === "deleteColumn" && columnCount <= 1);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
restoreSelection() {
|
|
251
|
+
this.editor.focus();
|
|
252
|
+
const selection = this.selection();
|
|
253
|
+
if (!selection || !this.savedRange || !this.editor.contains(this.savedRange.commonAncestorContainer))
|
|
254
|
+
return;
|
|
255
|
+
selection.removeAllRanges();
|
|
256
|
+
selection.addRange(this.savedRange);
|
|
257
|
+
}
|
|
258
|
+
editTable(command) {
|
|
259
|
+
const cell = this.selectedCell();
|
|
260
|
+
const table = cell?.closest("table");
|
|
261
|
+
const merged = Boolean(table && [...table.querySelectorAll("td,th")].some(item => item.rowSpan !== 1 || item.colSpan !== 1));
|
|
262
|
+
if (!cell || !table || merged)
|
|
263
|
+
return;
|
|
264
|
+
let focusCell;
|
|
265
|
+
if (command === "deleteTable") {
|
|
266
|
+
table.remove();
|
|
267
|
+
this.savedRange = undefined;
|
|
268
|
+
this.savedCell = undefined;
|
|
269
|
+
this.selectedTablePath = undefined;
|
|
270
|
+
}
|
|
271
|
+
else if (command === "insertRowAbove" || command === "insertRowBelow") {
|
|
272
|
+
const source = cell.parentElement;
|
|
273
|
+
const row = source.cloneNode(true);
|
|
274
|
+
for (const clone of [...row.cells]) {
|
|
275
|
+
clone.replaceChildren(document.createElement("br"));
|
|
276
|
+
}
|
|
277
|
+
source.parentElement?.insertBefore(row, command === "insertRowAbove" ? source : source.nextSibling);
|
|
278
|
+
focusCell = row.cells[cell.cellIndex];
|
|
279
|
+
}
|
|
280
|
+
else if (command === "insertColumnBefore" || command === "insertColumnAfter") {
|
|
281
|
+
const index = cell.cellIndex + (command === "insertColumnAfter" ? 1 : 0);
|
|
282
|
+
for (const row of [...table.rows]) {
|
|
283
|
+
const isHeader = row.parentElement?.tagName === "THEAD";
|
|
284
|
+
const next = document.createElement(isHeader ? "th" : "td");
|
|
285
|
+
if (isHeader)
|
|
286
|
+
next.setAttribute("scope", "col");
|
|
287
|
+
next.append(document.createElement("br"));
|
|
288
|
+
row.insertBefore(next, row.cells[index] ?? null);
|
|
289
|
+
if (row === cell.parentElement)
|
|
290
|
+
focusCell = next;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else if (command === "deleteRow") {
|
|
294
|
+
const row = cell.parentElement;
|
|
295
|
+
const index = row.rowIndex;
|
|
296
|
+
const cellIndex = cell.cellIndex;
|
|
297
|
+
row.remove();
|
|
298
|
+
const next = table.rows[Math.min(index, table.rows.length - 1)];
|
|
299
|
+
focusCell = next?.cells[Math.min(cellIndex, next.cells.length - 1)];
|
|
300
|
+
}
|
|
301
|
+
else if (command === "deleteColumn") {
|
|
302
|
+
const index = cell.cellIndex;
|
|
303
|
+
const row = cell.parentElement;
|
|
304
|
+
for (const row of [...table.rows])
|
|
305
|
+
row.cells[index]?.remove();
|
|
306
|
+
focusCell = row.cells[Math.min(index, row.cells.length - 1)];
|
|
307
|
+
}
|
|
308
|
+
if (focusCell) {
|
|
309
|
+
this.savedCell = focusCell;
|
|
310
|
+
this.rememberCell(focusCell);
|
|
311
|
+
const range = document.createRange();
|
|
312
|
+
range.selectNodeContents(focusCell);
|
|
313
|
+
range.collapse(true);
|
|
314
|
+
this.savedRange = range;
|
|
315
|
+
this.restoreSelection();
|
|
316
|
+
}
|
|
317
|
+
else
|
|
318
|
+
this.editor.focus();
|
|
319
|
+
this.syncForm();
|
|
320
|
+
this.syncTableActions();
|
|
321
|
+
this.dispatchEvent(new CustomEvent("spui-input", { detail: { value: this.value }, bubbles: true, composed: true }));
|
|
322
|
+
}
|
|
323
|
+
command(command, value) {
|
|
324
|
+
if (command === "insertImage") {
|
|
325
|
+
this.imageInput?.click();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (["insertRowAbove", "insertRowBelow", "insertColumnBefore", "insertColumnAfter", "deleteRow", "deleteColumn", "deleteTable"].includes(command)) {
|
|
329
|
+
this.editTable(command);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
this.restoreSelection();
|
|
333
|
+
if (command === "createLink") {
|
|
334
|
+
const href = window.prompt(this.getAttribute("link-prompt") ?? "Link URL");
|
|
335
|
+
if (!href || !safeHref(href))
|
|
336
|
+
return;
|
|
337
|
+
value = href.trim();
|
|
338
|
+
}
|
|
339
|
+
if (command === "insertTable")
|
|
340
|
+
value = '<table><caption><br></caption><thead><tr><th scope="col"><br></th><th scope="col"><br></th></tr></thead><tbody><tr><td><br></td><td><br></td></tr><tr><td><br></td><td><br></td></tr></tbody></table><p><br></p>';
|
|
341
|
+
this.editor.ownerDocument.execCommand(command === "insertTable" ? "insertHTML" : command, false, value);
|
|
342
|
+
this.sanitizeEditor();
|
|
343
|
+
this.syncForm();
|
|
344
|
+
this.onSelectionChange();
|
|
345
|
+
this.dispatchEvent(new CustomEvent("spui-input", { detail: { value: this.value }, bubbles: true, composed: true }));
|
|
346
|
+
}
|
|
347
|
+
async insertUploadedImage(file) {
|
|
348
|
+
if (!this.imageUploader || !file.type.startsWith("image/") || file.size > 10 * 1024 * 1024 || this.uploadingImage)
|
|
349
|
+
return;
|
|
350
|
+
this.uploadingImage = true;
|
|
351
|
+
this.syncImageButton();
|
|
352
|
+
try {
|
|
353
|
+
const src = await this.imageUploader(file);
|
|
354
|
+
if (!safeImageSrc(src))
|
|
355
|
+
throw new Error("Image upload must return an HTTP or HTTPS URL.");
|
|
356
|
+
this.restoreSelection();
|
|
357
|
+
const image = document.createElement("img");
|
|
358
|
+
image.src = new URL(src, document.baseURI).href;
|
|
359
|
+
image.alt = file.name.replace(/\\.[^.]*$/, "").slice(0, 200);
|
|
360
|
+
const selection = this.selection();
|
|
361
|
+
const range = selection?.rangeCount ? selection.getRangeAt(0) : undefined;
|
|
362
|
+
if (range && this.editor.contains(range.commonAncestorContainer)) {
|
|
363
|
+
range.deleteContents();
|
|
364
|
+
range.insertNode(image);
|
|
365
|
+
range.setStartAfter(image);
|
|
366
|
+
range.collapse(true);
|
|
367
|
+
selection.removeAllRanges();
|
|
368
|
+
selection.addRange(range);
|
|
369
|
+
}
|
|
370
|
+
else
|
|
371
|
+
this.editor.append(image);
|
|
372
|
+
this.savedRange = range?.cloneRange();
|
|
373
|
+
this.syncForm();
|
|
374
|
+
this.dispatchEvent(new CustomEvent("spui-input", { detail: { value: this.value }, bubbles: true, composed: true }));
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
this.dispatchEvent(new CustomEvent("spui-image-upload-error", { detail: { fileName: file.name, error }, bubbles: true, composed: true }));
|
|
378
|
+
}
|
|
379
|
+
finally {
|
|
380
|
+
this.uploadingImage = false;
|
|
381
|
+
this.syncImageButton();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
render() {
|
|
385
|
+
const shadow = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
386
|
+
const label = document.createElement("label");
|
|
387
|
+
label.textContent = this.getAttribute("label") ?? "";
|
|
388
|
+
label.hidden = !this.hasAttribute("label");
|
|
389
|
+
label.part.add("label");
|
|
390
|
+
const toolbar = document.createElement("div");
|
|
391
|
+
toolbar.setAttribute("role", "toolbar");
|
|
392
|
+
toolbar.setAttribute("aria-orientation", "horizontal");
|
|
393
|
+
toolbar.part.add("toolbar");
|
|
394
|
+
const actions = [["Bold", "bold"], ["Italic", "italic"], ["Underline", "underline"], ["Bulleted list", "insertUnorderedList", undefined, "• List"], ["Numbered list", "insertOrderedList", undefined, "1. List"], ["Undo", "undo"], ["Redo", "redo"], ["Insert table", "insertTable"], ["Insert image", "insertImage", undefined, undefined, "insert-image-label"], ["Insert row above", "insertRowAbove", undefined, undefined, "insert-row-above-label"], ["Insert row below", "insertRowBelow", undefined, undefined, "insert-row-below-label"], ["Insert column before", "insertColumnBefore", undefined, undefined, "insert-column-before-label"], ["Insert column after", "insertColumnAfter", undefined, undefined, "insert-column-after-label"], ["Delete row", "deleteRow", undefined, undefined, "delete-row-label"], ["Delete column", "deleteColumn", undefined, undefined, "delete-column-label"], ["Delete table", "deleteTable", undefined, undefined, "delete-table-label"], ["Link", "createLink"]];
|
|
395
|
+
for (const [title, command, value, short, labelAttribute] of actions) {
|
|
396
|
+
const button = document.createElement("button");
|
|
397
|
+
button.type = "button";
|
|
398
|
+
button.dataset.command = command;
|
|
399
|
+
button.dataset.fallback = short ?? title;
|
|
400
|
+
if (labelAttribute) {
|
|
401
|
+
button.dataset.labelAttribute = labelAttribute;
|
|
402
|
+
if (command.startsWith("insertRow") || command.startsWith("insertColumn") || command.startsWith("delete"))
|
|
403
|
+
button.dataset.tableCommand = command;
|
|
404
|
+
}
|
|
405
|
+
button.tabIndex = toolbar.children.length === 0 ? 0 : -1;
|
|
406
|
+
button.textContent = title;
|
|
407
|
+
button.setAttribute("aria-label", title);
|
|
408
|
+
button.addEventListener("mousedown", event => event.preventDefault());
|
|
409
|
+
button.addEventListener("click", () => this.command(command, value));
|
|
410
|
+
toolbar.append(button);
|
|
411
|
+
}
|
|
412
|
+
this.imageInput = document.createElement("input");
|
|
413
|
+
this.imageInput.type = "file";
|
|
414
|
+
this.imageInput.accept = "image/*";
|
|
415
|
+
this.imageInput.hidden = true;
|
|
416
|
+
this.imageInput.setAttribute("aria-label", this.getAttribute("image-input-label") ?? "Choose image");
|
|
417
|
+
this.imageInput.addEventListener("change", () => { const file = this.imageInput?.files?.[0]; if (file)
|
|
418
|
+
void this.insertUploadedImage(file); this.imageInput.value = ""; });
|
|
419
|
+
toolbar.append(this.imageInput);
|
|
420
|
+
toolbar.addEventListener("focusin", event => { const active = event.target; toolbar.querySelectorAll("button").forEach(button => { button.tabIndex = button === active ? 0 : -1; }); });
|
|
421
|
+
toolbar.addEventListener("keydown", event => {
|
|
422
|
+
const key = event.key;
|
|
423
|
+
const buttons = [...toolbar.querySelectorAll("button:not(:disabled)")];
|
|
424
|
+
const current = buttons.indexOf(event.target);
|
|
425
|
+
const rtl = getComputedStyle(this).direction === "rtl";
|
|
426
|
+
const step = key === "ArrowRight" ? (rtl ? -1 : 1) : key === "ArrowLeft" ? (rtl ? 1 : -1) : 0;
|
|
427
|
+
const index = key === "Home" ? 0 : key === "End" ? buttons.length - 1 : step ? (current + step + buttons.length) % buttons.length : -1;
|
|
428
|
+
if (index < 0 || !buttons[index])
|
|
429
|
+
return;
|
|
430
|
+
event.preventDefault();
|
|
431
|
+
buttons[index].tabIndex = 0;
|
|
432
|
+
buttons.forEach((button, buttonIndex) => { if (buttonIndex !== index)
|
|
433
|
+
button.tabIndex = -1; });
|
|
434
|
+
buttons[index].focus();
|
|
435
|
+
});
|
|
436
|
+
this.editor = document.createElement("div");
|
|
437
|
+
this.editor.setAttribute("role", "textbox");
|
|
438
|
+
this.editor.setAttribute("aria-multiline", "true");
|
|
439
|
+
this.syncEditorLabel();
|
|
440
|
+
const invalid = this.getAttribute("aria-invalid");
|
|
441
|
+
if (invalid !== null)
|
|
442
|
+
this.editor.setAttribute("aria-invalid", invalid);
|
|
443
|
+
this.editor.contentEditable = String(!this.hasAttribute("disabled") && !this.hasAttribute("read-only"));
|
|
444
|
+
this.editor.setAttribute("aria-readonly", String(this.hasAttribute("read-only")));
|
|
445
|
+
this.editor.setAttribute("aria-disabled", String(this.hasAttribute("disabled")));
|
|
446
|
+
this.editor.setAttribute("aria-required", String(this.hasAttribute("required")));
|
|
447
|
+
this.editor.dataset.placeholder = this.getAttribute("placeholder") ?? "Write something…";
|
|
448
|
+
this.editor.part.add("editor");
|
|
449
|
+
this.editor.addEventListener("keydown", event => { const key = event.key.toLowerCase(); const shortcut = event.metaKey || event.ctrlKey; if (!shortcut)
|
|
450
|
+
return; if (key === "z") {
|
|
451
|
+
event.preventDefault();
|
|
452
|
+
this.command(event.shiftKey ? "redo" : "undo");
|
|
453
|
+
}
|
|
454
|
+
else if (key === "y") {
|
|
455
|
+
event.preventDefault();
|
|
456
|
+
this.command("redo");
|
|
457
|
+
} });
|
|
458
|
+
this.editor.addEventListener("input", () => { this.sanitizeEditor(); this.syncForm(); this.dispatchEvent(new CustomEvent("spui-input", { detail: { value: this.value }, bubbles: true, composed: true })); });
|
|
459
|
+
this.editor.addEventListener("mouseup", this.onSelectionChange);
|
|
460
|
+
this.editor.addEventListener("keyup", this.onSelectionChange);
|
|
461
|
+
this.editor.addEventListener("click", this.onEditorClick);
|
|
462
|
+
shadow.addEventListener("selectionchange", this.onSelectionChange, { signal: this.events?.signal });
|
|
463
|
+
this.editor.addEventListener("paste", event => {
|
|
464
|
+
event.preventDefault();
|
|
465
|
+
const clipboard = event.clipboardData;
|
|
466
|
+
const imageFile = [...(clipboard?.files ?? [])].find(file => file.type.startsWith("image/"));
|
|
467
|
+
if (imageFile) {
|
|
468
|
+
void this.insertUploadedImage(imageFile);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const html = cleanHtml(clipboard?.getData("text/html") ?? "");
|
|
472
|
+
const text = clipboard?.getData("text/plain") ?? "";
|
|
473
|
+
if (html)
|
|
474
|
+
this.editor.ownerDocument.execCommand("insertHTML", false, html);
|
|
475
|
+
else
|
|
476
|
+
this.editor.ownerDocument.execCommand("insertText", false, text);
|
|
477
|
+
this.sanitizeEditor();
|
|
478
|
+
this.syncForm();
|
|
479
|
+
this.dispatchEvent(new CustomEvent("spui-input", { detail: { value: this.value }, bubbles: true, composed: true }));
|
|
480
|
+
});
|
|
481
|
+
this.editor.addEventListener("blur", () => { this.sanitizeEditor(); this.syncForm(); this.dispatchEvent(new CustomEvent("spui-change", { detail: { value: this.value }, bubbles: true, composed: true })); });
|
|
482
|
+
const style = document.createElement("style");
|
|
483
|
+
style.textContent = `${base}:host{display:grid;gap:6px}label{font-weight:600;color:var(--spui-ui-muted,var(--spui-semantic-body-text,inherit))}[role=toolbar]{display:flex;flex-wrap:wrap;gap:4px;padding:4px;border:1px solid var(--spui-control-border,var(--spui-ui-stroke,#c8c8c8));border-radius:var(--spui-control-radius,6px);background:var(--spui-ui-surface,#fff)}button{border:0;border-radius:4px;background:transparent;color:var(--spui-ui-text,inherit);padding:6px 8px;font:inherit;cursor:pointer}button:hover:not(:disabled){background:var(--spui-ui-surface-hover,#eee)}button:disabled{opacity:.55;cursor:not-allowed}button:focus-visible,[role=textbox]:focus-visible{outline:2px solid var(--spui-focus-ring,#005a9e);outline-offset:2px}[role=textbox]{min-height:120px;box-sizing:border-box;padding:10px 12px;border:1px solid var(--spui-control-border,var(--spui-ui-stroke,#c8c8c8));border-radius:var(--spui-control-radius,6px);background:var(--spui-control-background,var(--spui-ui-surface,#fff));color:var(--spui-ui-text,inherit);font:inherit;line-height:1.5;overflow:auto;caret-color:var(--spui-focus-ring,#005a9e)}[role=textbox]:empty:before{content:attr(data-placeholder);color:var(--spui-ui-muted,#666);pointer-events:none}table{border-collapse:collapse;max-width:100%;margin-block:8px}caption{text-align:start;font-weight:600}td,th{min-width:56px;padding:6px 9px;border:1px solid var(--spui-ui-stroke,#c8c8c8);text-align:start}th{background:var(--spui-ui-surface-hover,#f5f5f5);font-weight:600}a{color:var(--spui-semantic-link,#0f6cbd)}@media(forced-colors:active){button{border:1px solid ButtonText}td,th{border-color:CanvasText}}`;
|
|
484
|
+
shadow.replaceChildren(style, label, toolbar, this.editor);
|
|
485
|
+
syncHostAccessibleName(this, this.editor);
|
|
486
|
+
this.syncDisabled();
|
|
487
|
+
this.refreshStrings();
|
|
488
|
+
this.onSelectionChange();
|
|
489
|
+
}
|
|
490
|
+
refreshStrings() {
|
|
491
|
+
const toolbar = this.shadowRoot?.querySelector('[role="toolbar"]');
|
|
492
|
+
if (!toolbar)
|
|
493
|
+
return;
|
|
494
|
+
this.imageInput?.setAttribute("aria-label", this.getAttribute("image-input-label") ?? "Choose image");
|
|
495
|
+
const label = this.getAttribute("label") ?? "Rich text";
|
|
496
|
+
toolbar.setAttribute("aria-label", this.getAttribute("toolbar-label") ?? `${label} formatting`);
|
|
497
|
+
const attrs = ["bold-label", "italic-label", "underline-label", "bullet-list-label", "number-list-label", "undo-label", "redo-label", "insert-table-label", "insert-image-label", "insert-row-above-label", "insert-row-below-label", "insert-column-before-label", "insert-column-after-label", "delete-row-label", "delete-column-label", "delete-table-label", "link-label"];
|
|
498
|
+
toolbar.querySelectorAll("button[data-command]").forEach((button, index) => {
|
|
499
|
+
const attribute = button.dataset.labelAttribute ?? attrs[index];
|
|
500
|
+
const text = this.getAttribute(attribute) ?? button.dataset.fallback ?? "";
|
|
501
|
+
button.textContent = button.dataset.command === "insertUnorderedList" && !this.hasAttribute(attribute) ? "• List" : button.dataset.command === "insertOrderedList" && !this.hasAttribute(attribute) ? "1. List" : text;
|
|
502
|
+
button.setAttribute("aria-label", text);
|
|
503
|
+
});
|
|
504
|
+
this.syncTableActions();
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
customElements.define("spui-rich-text-editor", SpuiRichTextEditor);
|
|
508
|
+
}
|