1 import {EditorUiElement} from "../core";
2 import {$getSelection} from "lexical";
3 import {$patchStyleText} from "@lexical/selection";
4 import {el} from "../../../utils/dom";
6 import removeIcon from "@icons/editor/color-clear.svg";
37 export class EditorColorPicker extends EditorUiElement {
39 protected styleProperty: string;
41 constructor(styleProperty: string) {
43 this.styleProperty = styleProperty;
46 buildDOM(): HTMLElement {
48 const colorOptions = colorChoices.map(choice => {
50 class: 'editor-color-select-option',
51 style: `background-color: ${choice}`,
57 const removeButton = el('div', {
58 class: 'editor-color-select-option',
62 removeButton.innerHTML = removeIcon;
63 colorOptions.push(removeButton);
66 for (let i = 0; i < colorOptions.length; i+=5) {
67 const options = colorOptions.slice(i, i + 5);
68 colorRows.push(el('div', {
69 class: 'editor-color-select-row',
73 const wrapper = el('div', {
74 class: 'editor-color-select',
77 wrapper.addEventListener('click', this.onClick.bind(this));
82 onClick(event: MouseEvent) {
83 const colorEl = (event.target as HTMLElement).closest('[data-color]') as HTMLElement;
86 const color = colorEl.dataset.color as string;
87 this.getContext().editor.update(() => {
88 const selection = $getSelection();
90 $patchStyleText(selection, {[this.styleProperty]: color || null});