-
Notifications
You must be signed in to change notification settings - Fork 1
/
Color.ts
45 lines (34 loc) · 1.11 KB
/
Color.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { getAnchorElement, Tool } from '../Tool';
export type ColorName = 'color' | 'backgroundColor';
export abstract class ColorTool extends Tool {
abstract colorName: ColorName;
getColor() {
const box = getAnchorElement(),
{ colorName } = this;
if (!box) return colorName === 'color' ? '#000000' : '#ffffff';
const { [colorName]: color } = getComputedStyle(box);
const [red, green, blue] = color.match(/\d+/g);
return (
'#' +
(+red).toString(16).padStart(2, '0') +
(+green).toString(16).padStart(2, '0') +
(+blue).toString(16).padStart(2, '0')
);
}
get active() {
return !!this.getColor();
}
execute(editor: HTMLElement, color: string) {
this.edit(editor, color);
}
}
export abstract class ForeColorTool extends ColorTool {
name = 'Fore Color';
command = 'foreColor';
colorName = 'color' as ColorName;
}
export abstract class BackColorTool extends ColorTool {
name = 'Back Color';
command = 'backColor';
colorName = 'backgroundColor' as ColorName;
}