Skip to content

Commit 2b05259

Browse files
authored
Tiptap: Validates toolbar configuration value (#2393)
Adds `isValidTiptapToolbarValue()` method to ensure that the Tiptap toolbar value is in the correct structure.
1 parent 7ff4a68 commit 2b05259

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/packages/tiptap/property-editors/tiptap/components/property-editor-ui-tiptap-toolbar-configuration.element.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ export class UmbPropertyEditorUiTiptapToolbarConfigurationElement
3232

3333
@property({ attribute: false })
3434
set value(value: UmbTiptapToolbarValue | undefined) {
35-
if (!value) {
35+
if (!this.#isValidTiptapToolbarValue(value)) {
3636
this.#value = [[[]]];
37-
} else {
38-
// TODO: This can be optimized with cashing;
39-
this.#value = value ? value.map((rows) => rows.map((groups) => [...groups])) : [[[]]];
37+
return;
38+
}
39+
40+
if (value.length > 0) {
41+
this.#value = value.map((rows) => rows.map((groups) => [...groups]));
4042
value.forEach((row) => row.forEach((group) => group.forEach((alias) => this.#inUse.add(alias))));
4143
}
4244
}
4345
get value(): UmbTiptapToolbarValue {
44-
// TODO: This can be optimized with cashing;
45-
return this.#value.map((rows) => rows.map((groups) => [...groups]));
46+
return this.#value;
4647
}
4748
#value: UmbTiptapToolbarValue = [[[]]];
4849

@@ -55,6 +56,20 @@ export class UmbPropertyEditorUiTiptapToolbarConfigurationElement
5556
});
5657
}
5758

59+
#isValidTiptapToolbarValue(value: unknown): value is UmbTiptapToolbarValue {
60+
if (!Array.isArray(value)) return false;
61+
for (const row of value) {
62+
if (!Array.isArray(row)) return false;
63+
for (const group of row) {
64+
if (!Array.isArray(group)) return false;
65+
for (const alias of group) {
66+
if (typeof alias !== 'string') return false;
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
5873
#onDragStart(event: DragEvent, alias: string, fromPos?: [number, number, number]) {
5974
event.dataTransfer!.effectAllowed = 'move';
6075
this.#currentDragItem = { alias, fromPos };

0 commit comments

Comments
 (0)