forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcolorView.js
More file actions
149 lines (140 loc) · 4.16 KB
/
colorView.js
File metadata and controls
149 lines (140 loc) · 4.16 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
144
145
146
147
148
149
import {
Decoration,
EditorView,
ViewPlugin,
WidgetType,
} from "@codemirror/view";
import pickColor from "dialogs/color";
import color from "utils/color";
import { colorRegex, HEX } from "utils/color/regex";
// WeakMap to carry state from widget DOM back into handler
const colorState = new WeakMap();
const HEX_RE = new RegExp(HEX, "gi");
const RGBG = new RegExp(colorRegex.anyGlobal);
const enumColorType = { hex: "hex", rgb: "rgb", hsl: "hsl", named: "named" };
class ColorWidget extends WidgetType {
constructor({ color, colorRaw, ...state }) {
super();
this.state = state; // from, to, colorType, alpha
this.color = color; // hex for input value
this.colorRaw = colorRaw; // original css color string
}
eq(other) {
return (
other.state.colorType === this.state.colorType &&
other.color === this.color &&
other.state.from === this.state.from &&
other.state.to === this.state.to &&
(other.state.alpha || "") === (this.state.alpha || "")
);
}
toDOM() {
const wrapper = document.createElement("span");
wrapper.className = "cm-color-chip";
wrapper.style.display = "inline-block";
wrapper.style.width = "0.9em";
wrapper.style.height = "0.9em";
wrapper.style.borderRadius = "2px";
wrapper.style.verticalAlign = "middle";
wrapper.style.margin = "0 2px";
wrapper.style.boxSizing = "border-box";
wrapper.style.border = "1px solid rgba(0,0,0,0.2)";
wrapper.style.backgroundColor = this.colorRaw;
wrapper.dataset["color"] = this.color;
wrapper.dataset["colorraw"] = this.colorRaw;
wrapper.style.cursor = "pointer";
colorState.set(wrapper, this.state);
return wrapper;
}
ignoreEvent() {
return false;
}
}
function colorDecorations(view) {
const deco = [];
const ranges = view.visibleRanges;
for (const { from, to } of ranges) {
const text = view.state.doc.sliceString(from, to);
// Any color using global matcher from utils (captures named/rgb/rgba/hsl/hsla/hex)
RGBG.lastIndex = 0;
for (let m; (m = RGBG.exec(text)); ) {
const raw = m[2];
const start = from + m.index + m[1].length;
const end = start + raw.length;
const c = color(raw);
const colorHex = c.hex.toString(false);
deco.push(
Decoration.widget({
widget: new ColorWidget({
from: start,
to: end,
color: colorHex,
colorRaw: raw,
colorType: enumColorType.named,
}),
side: -1,
}).range(start),
);
}
}
return Decoration.set(deco, { sort: true });
}
export const colorView = (showPicker = true) =>
ViewPlugin.fromClass(
class ColorViewPlugin {
constructor(view) {
this.decorations = colorDecorations(view);
}
update(update) {
if (update.docChanged || update.viewportChanged) {
this.decorations = colorDecorations(update.view);
}
const readOnly = update.view.contentDOM.ariaReadOnly === "true";
const editable = update.view.contentDOM.contentEditable === "true";
const canBeEdited = readOnly === false && editable;
this.changePicker(update.view, canBeEdited);
}
changePicker(view, canBeEdited) {
const doms = view.contentDOM.querySelectorAll("input[type=color]");
doms.forEach((inp) => {
if (!showPicker) {
inp.setAttribute("disabled", "");
} else {
canBeEdited
? inp.removeAttribute("disabled")
: inp.setAttribute("disabled", "");
}
});
}
},
{
decorations: (v) => v.decorations,
eventHandlers: {
click: async (e, view) => {
const target = e.target;
const chip = target?.closest?.(".cm-color-chip");
if (!chip) return false;
// Respect read-only and setting toggle
const readOnly = view.contentDOM.ariaReadOnly === "true";
const editable = view.contentDOM.contentEditable === "true";
const canBeEdited = !readOnly && editable;
if (!canBeEdited) return true;
const data = colorState.get(chip);
if (!data) return false;
try {
const picked = await pickColor(
chip.dataset.colorraw || chip.dataset.color,
);
if (!picked) return true;
view.dispatch({
changes: { from: data.from, to: data.to, insert: picked },
});
} catch {
/* ignore */
}
return true;
},
},
},
);
export default colorView;