Skip to content

Commit

Permalink
Lexical: Added color format custom color select
Browse files Browse the repository at this point in the history
Includes tracking of selected colors via localstorage for display.
  • Loading branch information
ssddanbrown committed Jan 17, 2025
1 parent 7f5fd16 commit c091f67
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions resources/icons/editor/color-select.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 48 additions & 1 deletion resources/js/wysiwyg/ui/framework/blocks/color-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {$patchStyleText} from "@lexical/selection";
import {el} from "../../../utils/dom";

import removeIcon from "@icons/editor/color-clear.svg";
import selectIcon from "@icons/editor/color-select.svg";
import {uniqueIdSmall} from "../../../../services/util";

const colorChoices = [
'#000000',
Expand Down Expand Up @@ -34,6 +36,8 @@ const colorChoices = [
'#34495E',
];

const storageKey = 'bs-lexical-custom-colors';

export class EditorColorPicker extends EditorUiElement {

protected styleProperty: string;
Expand All @@ -44,8 +48,10 @@ export class EditorColorPicker extends EditorUiElement {
}

buildDOM(): HTMLElement {
const id = uniqueIdSmall();

const colorOptions = colorChoices.map(choice => {
const allChoices = [...colorChoices, ...this.getCustomColorChoices()];
const colorOptions = allChoices.map(choice => {
return el('div', {
class: 'editor-color-select-option',
style: `background-color: ${choice}`,
Expand All @@ -62,6 +68,25 @@ export class EditorColorPicker extends EditorUiElement {
removeButton.innerHTML = removeIcon;
colorOptions.push(removeButton);

const selectButton = el('label', {
class: 'editor-color-select-option',
for: `color-select-${id}`,
'data-color': '',
title: 'Custom color',
}, []);
selectButton.innerHTML = selectIcon;
colorOptions.push(selectButton);

const input = el('input', {type: 'color', hidden: 'true', id: `color-select-${id}`}) as HTMLInputElement;
colorOptions.push(input);
input.addEventListener('change', e => {
if (input.value) {
this.storeCustomColorChoice(input.value);
this.setColor(input.value);
this.rebuildDOM();
}
});

const colorRows = [];
for (let i = 0; i < colorOptions.length; i+=5) {
const options = colorOptions.slice(i, i + 5);
Expand All @@ -79,11 +104,33 @@ export class EditorColorPicker extends EditorUiElement {
return wrapper;
}

storeCustomColorChoice(color: string) {
if (colorChoices.includes(color)) {
return;
}

const customColors: string[] = this.getCustomColorChoices();
if (customColors.includes(color)) {
return;
}

customColors.push(color);
window.localStorage.setItem(storageKey, JSON.stringify(customColors));
}

getCustomColorChoices(): string[] {
return JSON.parse(window.localStorage.getItem(storageKey) || '[]');
}

onClick(event: MouseEvent) {
const colorEl = (event.target as HTMLElement).closest('[data-color]') as HTMLElement;
if (!colorEl) return;

const color = colorEl.dataset.color as string;
this.setColor(color);
}

setColor(color: string) {
this.getContext().editor.update(() => {
const selection = $getSelection();
if (selection) {
Expand Down
7 changes: 7 additions & 0 deletions resources/js/wysiwyg/ui/framework/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export abstract class EditorUiElement {
return this.dom;
}

rebuildDOM(): HTMLElement {
const newDOM = this.buildDOM();
this.dom?.replaceWith(newDOM);
this.dom = newDOM;
return this.dom;
}

trans(text: string) {
return this.getContext().translate(text);
}
Expand Down

0 comments on commit c091f67

Please sign in to comment.