Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the not hex color doesn't change in toolbar #4139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/quill/src/formats/background.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ClassAttributor, Scope } from 'parchment';
import { ColorAttributor } from './color.js';
import { ClassAttributor, Scope, StyleAttributor } from 'parchment';

const BackgroundClass = new ClassAttributor('background', 'ql-bg', {
scope: Scope.INLINE,
});
const BackgroundStyle = new ColorAttributor('background', 'background-color', {
const BackgroundStyle = new StyleAttributor('background', 'background-color', {
scope: Scope.INLINE,
});

Expand Down
17 changes: 2 additions & 15 deletions packages/quill/src/formats/color.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { ClassAttributor, Scope, StyleAttributor } from 'parchment';

class ColorAttributor extends StyleAttributor {
value(domNode: HTMLElement) {
let value = super.value(domNode) as string;
if (!value.startsWith('rgb(')) return value;
value = value.replace(/^[^\d]+/, '').replace(/[^\d]+$/, '');
const hex = value
.split(',')
.map((component) => `00${parseInt(component, 10).toString(16)}`.slice(-2))
.join('');
return `#${hex}`;
}
}

const ColorClass = new ClassAttributor('color', 'ql-color', {
scope: Scope.INLINE,
});
const ColorStyle = new ColorAttributor('color', 'color', {
const ColorStyle = new StyleAttributor('color', 'color', {
scope: Scope.INLINE,
});

export { ColorAttributor, ColorClass, ColorStyle };
export { ColorClass, ColorStyle };
8 changes: 7 additions & 1 deletion packages/quill/src/modules/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,18 @@ function addSelect(
format: string,
values: Array<string | boolean>,
) {
const colorGetter = document.createElement('span') as any;
const input = document.createElement('select');
input.classList.add(`ql-${format}`);
values.forEach((value) => {
const option = document.createElement('option');
if (value !== false) {
option.setAttribute('value', String(value));
let colorValue = String(value);
if (format === 'color' || format === 'background') {
colorGetter.style[format] = String(value);
colorValue = colorGetter.style[format];
}
option.setAttribute('value', colorValue);
} else {
option.setAttribute('selected', 'selected');
}
Expand Down
21 changes: 20 additions & 1 deletion packages/quill/src/themes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ class BaseTheme extends Theme {
? 'background'
: 'color';
if (select.querySelector('option') == null) {
fillSelect(
fillColorSelect(
select,
COLORS,
format,
format === 'background' ? '#ffffff' : '#000000',
);
}
Expand Down Expand Up @@ -359,5 +360,23 @@ function fillSelect(
select.appendChild(option);
});
}
function fillColorSelect(
select: HTMLSelectElement,
values: Array<string | boolean>,
format: string,
defaultValue: unknown = false,
) {
const colorGetter = document.createElement('span') as any;
values.forEach((value) => {
const option = document.createElement('option');
if (value === defaultValue) {
option.setAttribute('selected', 'selected');
} else {
colorGetter.style[format] = String(value);
option.setAttribute('value', colorGetter.style[format]);
}
select.appendChild(option);
});
}

export { BaseTooltip, BaseTheme as default };
10 changes: 9 additions & 1 deletion packages/quill/test/fuzz/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ const attributeDefs: {
blockEmbed: AttributeDef[];
} = {
text: [
{ name: 'color', values: ['#ffffff', '#000000', '#ff0000', '#ffff00'] },
{
name: 'color',
values: [
'rgb(255, 255, 255)',
'rgb(0, 0, 0)',
'rgb(255, 0, 0)',
'rgb(255, 255, 0)',
],
},
{ name: 'bold', values: [true] },
{ name: 'code', values: [true] },
// @ts-expect-error
Expand Down