forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcolor.js
More file actions
143 lines (129 loc) · 3.04 KB
/
color.js
File metadata and controls
143 lines (129 loc) · 3.04 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import actionStack from "lib/actionStack";
import restoreTheme from "lib/restoreTheme";
import Picker from "vanilla-picker";
let lastPicked = localStorage.__picker_last_picked || "#fff";
/**
* Choose color
* @param {string} defaultColor Default color
* @param {Function} onhide Callback function
* @returns {Promise<string>}
*/
function color(defaultColor, onhide) {
defaultColor = defaultColor || lastPicked;
let type = checkColorType(defaultColor) || "hex";
return new Promise((resolve) => {
const colorModes = ["hsl", "hex", "rgb"];
let mode = colorModes.indexOf(type);
let color = null;
const parent = tag("div", {
className: "message color-picker",
});
const okBtn = tag("button", {
textContent: strings.ok,
onclick: () => {
hide();
lastPicked = color;
localStorage.__picker_last_picked = color;
resolve(color);
},
});
const toggleMode = tag("button", {
textContent: type,
onclick: function (e) {
++mode;
if (mode >= colorModes.length) mode = 0;
type = colorModes[mode];
this.textContent = type;
picker.setOptions({
color,
editorFormat: type,
});
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
},
});
const box = tag("div", {
className: "prompt box",
children: [
tag("strong", {
className: "title",
textContent: strings["choose color"],
}),
parent,
tag("div", {
className: "button-container",
children: [toggleMode, okBtn],
}),
],
});
const mask = tag("span", {
className: "mask",
onclick: hide,
});
const picker = new Picker({
parent,
popup: false,
editor: true,
color: defaultColor,
onChange,
alpha: true,
editorFormat: type,
});
picker.show();
actionStack.push({
id: "box",
action: hideSelect,
});
document.body.append(box, mask);
restoreTheme(true);
function hideSelect() {
box.classList.add("hide");
restoreTheme();
setTimeout(() => {
document.body.removeChild(box);
document.body.removeChild(mask);
if (typeof onhide === "function") onhide();
}, 300);
}
function hide() {
actionStack.remove("box");
const height = box.clientHeight;
box.style.height = height + "px";
picker.destroy();
hideSelect();
}
function onChange(c) {
if (!c) return;
const alpha = c.rgba[3] < 1 ? true : false;
if (type === "hex") {
if (alpha) color = c.hex;
else color = c.hex.slice(0, -2);
} else if (type === "rgb") {
if (alpha) color = c.rgbaString;
else color = c.rgbString;
} else {
if (alpha) color = c.hslaString;
else color = c.hslString;
}
if (color) {
setTimeout(() => {
const $editor = box.get(".picker_editor");
if ($editor) $editor.style.backgroundColor = color;
}, 0);
}
}
});
}
/**
*
* @param {string} color
* @returns {'hex'|'rgb'|'hsl'}
*/
function checkColorType(color) {
if (color.startsWith("#")) return "hex";
if (color.startsWith("rgb")) return "rgb";
if (color.startsWith("hsl")) return "hsl";
return null;
}
export default color;