import {
$,
DATA_ELEMENT,
NodeInterface,
UI,
TRIGGER_CARD_ID,
isMobile,
EditorInterface,
Position,
} from '@aomao/engine';
import {
AnyColor,
Colord,
colord,
HslaColor,
HsvaColor,
RgbaColor,
} from 'colord';
import Palette from './palette';
export type Options = {
colors: Array<
Array<{
color: string;
border?: string;
}>
>;
defaultColor?: string;
onChange?: (color: string) => void;
onDestroy?: () => void;
};
class Color {
#editor: EditorInterface;
#options: Options;
#color: string;
#button: NodeInterface;
#cardId: string;
#container?: NodeInterface;
#position?: Position;
constructor(editor: EditorInterface, cardId: string, options: Options) {
this.#editor = editor;
this.#cardId = cardId;
this.#options = options;
this.#position = new Position(this.#editor);
this.#color = options.defaultColor || 'transparent';
this.#button = $(`
`);
this.#button
.find('.table-color-dropdown-arrow')
.on('mousedown', (event: MouseEvent) => {
event.preventDefault();
if (
this.#container !== undefined &&
this.#container.length > 0
) {
this.remove();
} else this.render();
});
this.#button
.find('.table-color-dropdown-button-text')
.on('mousedown', (event: MouseEvent) => {
event.preventDefault();
const { onChange } = this.#options;
if (onChange) onChange(this.#color!);
});
}
getButton() {
return this.#button;
}
select(color: string) {
const stroke = Palette.getStroke(color);
const rectElement = this.#button.find('rect');
rectElement.attributes('stroke', stroke);
rectElement.attributes('fill', color);
this.#button
.find('svg')
.css('margin-bottom', color === 'transparent' ? -5 : -3);
}
change(color: string) {
this.#color = color;
this.select(color);
const { onChange } = this.#options;
if (onChange) onChange(color);
}
toState(
color: (AnyColor | Colord) & {
hex?: string;
h?: string;
source?: string;
},
oldHue?: number,
) {
let c = color.hex ?? color;
if (c === 'transparent') {
c = 'rgba(0,0,0,0)';
}
const tinyColor = colord(c);
const hsl = tinyColor.toHsl();
const hsv = tinyColor.toHsv();
const rgb = tinyColor.toRgb();
const hex = tinyColor.toHex();
if (hsl.s === 0) {
hsl.h = oldHue || 0;
hsv.h = oldHue || 0;
}
const transparent = hex === '000000' && rgb.a === 0;
return {
hsl: hsl,
hex: transparent ? 'transparent' : '#'.concat(hex),
rgb: rgb,
hsv: hsv,
oldHue: color['h'] || oldHue || hsl.h,
source: color['source'],
};
}
render() {
this.#container = $(
``,
);
const colorPanle = $(``);
const { colors } = this.#options;
const getItem = (
color: { color: string; border?: string },
display?: boolean,
) => {
//接近白色的颜色,需要添加一个边框。不然看不见
const state = this.toState(color.color || '#FFFFFF');
const needBorder =
['#ffffff', '#fafafa', 'transparent'].indexOf(state.hex) >= 0;
const item = $(``);
item.on('mousedown', (event: MouseEvent) => {
event.preventDefault();
event.stopPropagation();
colorPanle.find('svg').each((svg) => {
(svg as SVGAElement).style.display = 'none';
});
if (display !== false) item.find('svg').css('display', 'block');
this.change(color.color);
});
return item;
};
const defaultItem = $(
``,
);
const item = getItem({ color: 'transparent' }, false);
defaultItem.append(item);
defaultItem.on('mousedown', (event: MouseEvent) => {
event.preventDefault();
colorPanle.find('svg').each((svg) => {
(svg as SVGAElement).style.display = 'none';
});
this.change('transparent');
});
const nofillText = this.#editor.language.get(
'table',
'color',
'nonFillText',
);
defaultItem.append(
$(
`${nofillText}`,
),
);
this.#container.append(defaultItem);
colors.forEach((group) => {
const groupElement = $(
``,
);
group.forEach((color) => {
const item = getItem(color);
groupElement.append(item);
});
colorPanle.append(groupElement);
});
this.#container.append(colorPanle);
this.#position?.bind(this.#container, this.#button);
document.addEventListener('mousedown', this.windowClick, true);
}
windowClick = (event: MouseEvent) => {
const { target } = event;
if (!target) return;
if (
$(target).closest(
'.data-table-color-tool,.table-color-dropdown-arrow',
).length === 0
)
this.remove();
};
remove() {
this.#container?.remove();
this.#position?.destroy();
document.removeEventListener('mousedown', this.windowClick, true);
this.#container = undefined;
}
destroy() {
this.remove();
const { onDestroy } = this.#options;
if (onDestroy) onDestroy();
}
}
export default Color;