Skip to content

Commit 2258cab

Browse files
samuelreichertgjulivan
authored andcommitted
feat(rich-text-web): enhance color picker functionality in table properties form
1 parent cd92cb1 commit 2258cab

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

packages/pluggableWidgets/rich-text-web/src/utils/formats/quill-table-better/ui/table-properties-form.ts

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class TablePropertiesForm {
8585
this.borderForm = [];
8686
this.saveButton = null;
8787
this.form = this.createPropertiesForm(options);
88+
this.createColorPicker();
8889
}
8990

9091
checkBtnsAction(status: string) {
@@ -108,7 +109,7 @@ class TablePropertiesForm {
108109
const iconClasses = icon.split(" ");
109110
iconContainer.classList.add(...iconClasses);
110111
button.appendChild(iconContainer);
111-
setElementAttribute(button, { label });
112+
button.setAttribute("label", label);
112113
if (showLabel) {
113114
const labelContainer = ownerDocument.createElement("span");
114115
labelContainer.innerText = useLanguage(label);
@@ -156,71 +157,71 @@ class TablePropertiesForm {
156157
const container = this.tableMenus.quill.root.ownerDocument.createElement("div");
157158
container.classList.add("ql-table-color-container");
158159
const input = this.createColorInput(child);
159-
this.createColorPicker(child, input.querySelector("input"));
160+
const inputEl = input.querySelector("input");
161+
if (inputEl) {
162+
this.setColorPicker(inputEl);
163+
}
160164
container.appendChild(input);
161165
return container;
162166
}
163167

164168
createColorInput(child: Child) {
165-
const { attribute, value } = child;
169+
const { attribute = {}, propertyName, value } = child;
166170
const ownerDocument = this.tableMenus.quill.root.ownerDocument;
167171
const placeholder = attribute?.placeholder ?? "";
168172
const container = ownerDocument.createElement("div");
169173
container.classList.add("label-field-view", "label-field-view-color");
170174
const label = ownerDocument.createElement("label");
171175
label.innerText = placeholder;
172176
const input = ownerDocument.createElement("input");
173-
setElementAttribute(input, attribute!);
174-
input.classList.add("property-input");
175-
input.value = value ?? "";
177+
const attributes = {
178+
...attribute,
179+
class: "property-input",
180+
value: value ?? "",
181+
"data-property": propertyName
182+
};
183+
setElementAttribute(input, attributes);
176184

177185
container.appendChild(input);
178186
container.appendChild(label);
179187
return container;
180188
}
181189

182-
createColorPicker(child: Child, input: HTMLInputElement | null): void {
183-
if (input) {
184-
const { propertyName } = child;
185-
Coloris.init();
186-
Coloris({
187-
// a11y: {},
188-
el: input,
189-
clearButton: true,
190-
closeButton: true,
191-
onChange: (color: string): void => {
192-
this.setAttribute(propertyName, color);
193-
},
194-
swatches: COLOR_LIST.map(({ value }) => value),
195-
theme: "polaroid"
196-
});
197-
}
190+
createColorPicker(): void {
191+
Coloris.init();
192+
Coloris({
193+
clearButton: true,
194+
closeButton: true,
195+
// @ts-ignore
196+
onChange: (color: string, input: HTMLElement): void => {
197+
const propertyName = input.getAttribute("data-property") ?? "";
198+
this.setAttribute(propertyName, color, input);
199+
},
200+
swatches: COLOR_LIST.map(({ value }) => value),
201+
theme: "polaroid"
202+
});
203+
}
204+
205+
setColorPicker(input: HTMLInputElement) {
206+
Coloris.coloris({ el: input });
198207
}
199208

200-
createDropdown(value: string, category?: string) {
209+
createDropdown(value: string) {
201210
const ownerDocument = this.tableMenus.quill.root.ownerDocument;
202-
const container = ownerDocument.createElement("div");
211+
const dropdown = ownerDocument.createElement("div");
203212
const dropIcon = ownerDocument.createElement("span");
204213
const dropText = ownerDocument.createElement("span");
205-
switch (category) {
206-
case "dropdown":
207-
dropIcon.classList.add("icons", "icon-Arrow-down", "ql-table-dropdown-icon");
208-
break;
209-
case "color":
210-
break;
211-
default:
212-
break;
213-
}
214+
dropIcon.classList.add("icons", "icon-Arrow-down", "ql-table-dropdown-icon");
214215
value && (dropText.innerText = value);
215-
container.classList.add("ql-table-dropdown-properties");
216216
dropText.classList.add("ql-table-dropdown-text");
217-
container.appendChild(dropText);
218-
container.appendChild(dropIcon);
219-
return { dropdown: container, dropText };
217+
dropdown.classList.add("ql-table-dropdown-properties");
218+
dropdown.appendChild(dropText);
219+
dropdown.appendChild(dropIcon);
220+
return { dropdown, dropText };
220221
}
221222

222223
createInput(child: Child) {
223-
const { attribute, message, propertyName, valid, value } = child;
224+
const { attribute = {}, message, propertyName, valid, value } = child;
224225
const ownerDocument = this.tableMenus.quill.root.ownerDocument;
225226
const placeholder = attribute?.placeholder ?? "";
226227
const container = ownerDocument.createElement("div");
@@ -231,9 +232,12 @@ class TablePropertiesForm {
231232
container.classList.add("label-field-view");
232233
wrapper.classList.add("label-field-view-input-wrapper");
233234
label.innerText = placeholder;
234-
setElementAttribute(input, attribute!);
235-
input.classList.add("property-input");
236-
input.value = value ?? "";
235+
const attributes = {
236+
...attribute,
237+
class: "property-input",
238+
value: value ?? ""
239+
};
240+
setElementAttribute(input, attributes);
237241
input.addEventListener("input", e => {
238242
const value = (e.target as HTMLInputElement).value;
239243
valid && this.switchHidden(status, valid(value));
@@ -298,7 +302,7 @@ class TablePropertiesForm {
298302
const { category, value } = child;
299303
switch (category) {
300304
case "dropdown":
301-
const { dropdown, dropText } = this.createDropdown(value!, category);
305+
const { dropdown, dropText } = this.createDropdown(value!);
302306
const list = this.createList(child, dropText);
303307
dropdown.appendChild(list!);
304308
dropdown.addEventListener("click", () => {
@@ -307,14 +311,11 @@ class TablePropertiesForm {
307311
});
308312
return dropdown;
309313
case "color":
310-
const colorContainer = this.createColorContainer(child);
311-
return colorContainer;
314+
return this.createColorContainer(child);
312315
case "menus":
313-
const checkBtns = this.createCheckBtns(child);
314-
return checkBtns;
316+
return this.createCheckBtns(child);
315317
case "input":
316-
const input = this.createInput(child);
317-
return input;
318+
return this.createInput(child);
318319
default:
319320
break;
320321
}

0 commit comments

Comments
 (0)