| 1 |
import { Component, onWillUpdateProps, useState, useRef } from "@odoo/owl"; |
| 2 |
import { CustomColorPicker as ColorPicker } from "@web/core/color_picker/custom_color_picker/custom_color_picker"; |
| 3 |
import { |
| 4 |
isColorGradient, |
| 5 |
standardizeGradient, |
| 6 |
rgbaToHex, |
| 7 |
convertCSSColorToRgba, |
| 8 |
} from "@web/core/utils/colors"; |
| 9 |
|
| 10 |
export class GradientPicker extends Component { |
| 11 |
static components = { ColorPicker }; |
| 12 |
static template = "html_editor.GradientPicker"; |
| 13 |
static props = { |
| 14 |
onGradientChange: { type: Function, optional: true }, |
| 15 |
onGradientPreview: { type: Function, optional: true }, |
| 16 |
setOnCloseCallback: { type: Function, optional: true }, |
| 17 |
setOperationCallbacks: { type: Function, optional: true }, |
| 18 |
selectedGradient: { type: String, optional: true }, |
| 19 |
noTransparency: { type: Boolean, optional: true }, |
| 20 |
}; |
| 21 |
|
| 22 |
setup() { |
| 23 |
this.state = useState({ |
| 24 |
type: "linear", |
| 25 |
angle: 135, |
| 26 |
currentColorIndex: 0, |
| 27 |
size: "closest-side", |
| 28 |
}); |
| 29 |
this.positions = useState({ x: 25, y: 25 }); |
| 30 |
this.colors = useState([ |
| 31 |
{ hex: "#DF7CC4", percentage: 0 }, |
| 32 |
{ hex: "#6C3582", percentage: 100 }, |
| 33 |
]); |
| 34 |
this.cssGradients = useState({ preview: "", linear: "", radial: "", sliderThumbStyle: "" }); |
| 35 |
this.knobRef = useRef("gradientAngleKnob"); |
| 36 |
|
| 37 |
if (this.props.selectedGradient && isColorGradient(this.props.selectedGradient)) { |
| 38 |
// initialization of the gradient with the selected value |
| 39 |
this.setGradientFromString(this.props.selectedGradient); |
| 40 |
} else { |
| 41 |
// initialization of the gradient with default value |
| 42 |
this.onColorGradientChange(); |
| 43 |
} |
| 44 |
|
| 45 |
onWillUpdateProps((newProps) => { |
| 46 |
if (newProps.selectedGradient) { |
| 47 |
this.setGradientFromString(newProps.selectedGradient); |
| 48 |
} |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
setGradientFromString(gradient) { |
| 53 |
if (!gradient || !isColorGradient(gradient)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
gradient = standardizeGradient(gradient); |
| 57 |
const colors = [ |
| 58 |
...gradient.matchAll( |
| 59 |
/(#[0-9a-f]{6}|rgba?\(\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[0-9]+\s*[,\s*[0-9.]*]?\s*\)|[a-z]+)\s*([[0-9]+%]?)/g |
| 60 |
), |
| 61 |
].filter((color) => rgbaToHex(color[1]) !== "#"); |
| 62 |
|
| 63 |
this.colors.splice(0, this.colors.length); |
| 64 |
for (const color of colors) { |
| 65 |
this.colors.push({ hex: rgbaToHex(color[1]), percentage: color[2].replace("%", "") }); |
| 66 |
} |
| 67 |
|
| 68 |
const isLinear = gradient.startsWith("linear-gradient("); |
| 69 |
if (isLinear) { |
| 70 |
const angle = gradient.match(/(-?[0-9]+)deg/); |
| 71 |
if (angle) { |
| 72 |
this.state.angle = parseInt(angle[1]); |
| 73 |
} |
| 74 |
} else { |
| 75 |
this.state.type = "radial"; |
| 76 |
const sizeMatch = gradient.match(/(closest|farthest)-(side|corner)/); |
| 77 |
const size = sizeMatch ? sizeMatch[0] : "farthest-corner"; |
| 78 |
this.state.size = size; |
| 79 |
|
| 80 |
const position = gradient.match(/ at ([0-9]+)% ([0-9]+)%/) || ["", "50", "50"]; |
| 81 |
this.positions.x = position[1]; |
| 82 |
this.positions.y = position[2]; |
| 83 |
} |
| 84 |
|
| 85 |
this.updateCssGradients(); |
| 86 |
} |
| 87 |
|
| 88 |
selectType(type) { |
| 89 |
this.state.type = type; |
| 90 |
this.onColorGradientChange(); |
| 91 |
} |
| 92 |
|
| 93 |
onAngleChange(ev) { |
| 94 |
const angle = parseInt(ev.target.value); |
| 95 |
if (!isNaN(angle)) { |
| 96 |
const clampedAngle = Math.min(Math.max(angle, 0), 360); |
| 97 |
ev.target.value = clampedAngle; |
| 98 |
this.state.angle = clampedAngle; |
| 99 |
this.onColorGradientChange(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
onPositionChange(position, ev) { |
| 104 |
const inputValue = parseFloat(ev.target.value); |
| 105 |
if (!isNaN(inputValue)) { |
| 106 |
const clampedValue = Math.min(Math.max(inputValue, 0), 100); |
| 107 |
ev.target.value = clampedValue; |
| 108 |
this.positions[position] = clampedValue; |
| 109 |
this.onColorGradientChange(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
onColorChange(color) { |
| 114 |
const hex = rgbaToHex(color.cssColor); |
| 115 |
this.colors[this.state.currentColorIndex].hex = hex; |
| 116 |
this.onColorGradientChange(); |
| 117 |
} |
| 118 |
|
| 119 |
onColorPreview(color) { |
| 120 |
const hex = rgbaToHex(color.cssColor); |
| 121 |
this.colors[this.state.currentColorIndex].hex = hex; |
| 122 |
this.onColorGradientPreview(); |
| 123 |
} |
| 124 |
|
| 125 |
onSizeChange(size) { |
| 126 |
this.state.size = size; |
| 127 |
this.onColorGradientChange(); |
| 128 |
} |
| 129 |
|
| 130 |
onColorPercentageChange(colorIndex, ev) { |
| 131 |
this.state.currentColorIndex = colorIndex; |
| 132 |
this.colors[colorIndex].percentage = ev.target.value; |
| 133 |
this.sortColors(); |
| 134 |
this.onColorGradientChange(); |
| 135 |
} |
| 136 |
|
| 137 |
onGradientPreviewClick(ev) { |
| 138 |
const width = parseInt(window.getComputedStyle(ev.target).width, 10); |
| 139 |
const percentage = Math.round((100 * ev.offsetX) / width); |
| 140 |
this.addColorStop(percentage); |
| 141 |
} |
| 142 |
|
| 143 |
addColorStop(percentage) { |
| 144 |
let color; |
| 145 |
|
| 146 |
let previousColor = this.colors.findLast((color) => color.percentage <= percentage); |
| 147 |
let nextColor = this.colors.find((color) => color.percentage > percentage); |
| 148 |
if (!previousColor && nextColor) { |
| 149 |
// Click position is before the first color |
| 150 |
color = nextColor.hex; |
| 151 |
} else if (!nextColor && previousColor) { |
| 152 |
// Click position is after the last color |
| 153 |
color = previousColor.hex; |
| 154 |
} else if (nextColor && previousColor) { |
| 155 |
const previousRatio = |
| 156 |
(nextColor.percentage - percentage) / |
| 157 |
(nextColor.percentage - previousColor.percentage); |
| 158 |
const nextRatio = 1 - previousRatio; |
| 159 |
|
| 160 |
previousColor = convertCSSColorToRgba(previousColor.hex); |
| 161 |
nextColor = convertCSSColorToRgba(nextColor.hex); |
| 162 |
|
| 163 |
const red = Math.round(previousRatio * previousColor.red + nextRatio * nextColor.red); |
| 164 |
const green = Math.round( |
| 165 |
previousRatio * previousColor.green + nextRatio * nextColor.green |
| 166 |
); |
| 167 |
const blue = Math.round( |
| 168 |
previousRatio * previousColor.blue + nextRatio * nextColor.blue |
| 169 |
); |
| 170 |
const opacity = Math.round( |
| 171 |
previousRatio * previousColor.opacity + nextRatio * nextColor.opacity |
| 172 |
); |
| 173 |
color = `rgba(${red}, ${green}, ${blue}, ${opacity / 100})`; |
| 174 |
} |
| 175 |
|
| 176 |
this.colors.push({ hex: color, percentage }); |
| 177 |
this.sortColors(); |
| 178 |
this.state.currentColorIndex = this.colors.findIndex( |
| 179 |
(color) => color.percentage === percentage |
| 180 |
); |
| 181 |
this.onColorGradientChange(); |
| 182 |
} |
| 183 |
|
| 184 |
removeColor(colorIndex) { |
| 185 |
if (this.colors.length <= 2) { |
| 186 |
return; |
| 187 |
} |
| 188 |
this.colors.splice(colorIndex, 1); |
| 189 |
this.state.currentColorIndex = 0; |
| 190 |
this.onColorGradientChange(); |
| 191 |
} |
| 192 |
|
| 193 |
sortColors() { |
| 194 |
this.colors = this.colors.sort((a, b) => a.percentage - b.percentage); |
| 195 |
} |
| 196 |
|
| 197 |
updateCssGradients() { |
| 198 |
const gradientColors = this.colors |
| 199 |
.map((color) => `${color.hex} ${color.percentage}%`) |
| 200 |
.join(", "); |
| 201 |
let sliderThumbStyle = ""; |
| 202 |
// color the slider thumb with the color of the gradient |
| 203 |
for (let i = 0; i < this.colors.length; i++) { |
| 204 |
const selector = `.gradient-colors div:nth-child(${i + 1}) input[type="range"]`; |
| 205 |
const style = `background-color: ${this.colors[i].hex};`; |
| 206 |
sliderThumbStyle += `${selector}::-webkit-slider-thumb { ${style} }\n`; |
| 207 |
sliderThumbStyle += `${selector}::-moz-range-thumb { ${style} }\n`; |
| 208 |
} |
| 209 |
|
| 210 |
this.cssGradients.preview = `linear-gradient(90deg, ${gradientColors})`; |
| 211 |
this.cssGradients.linear = `linear-gradient(${this.state.angle}deg, ${gradientColors})`; |
| 212 |
this.cssGradients.radial = `radial-gradient(circle ${this.state.size} at ${this.positions.x}% ${this.positions.y}%, ${gradientColors})`; |
| 213 |
this.cssGradients.sliderThumbStyle = sliderThumbStyle; |
| 214 |
} |
| 215 |
|
| 216 |
onColorGradientChange() { |
| 217 |
this.updateCssGradients(); |
| 218 |
this.props?.onGradientChange(this.cssGradients[this.state.type]); |
| 219 |
} |
| 220 |
|
| 221 |
onColorGradientPreview() { |
| 222 |
this.updateCssGradients(); |
| 223 |
this.props.onGradientPreview?.({ gradient: this.cssGradients[this.state.type] }); |
| 224 |
} |
| 225 |
|
| 226 |
get currentColorHex() { |
| 227 |
return this.colors?.[this.state.currentColorIndex]?.hex || "#000000"; |
| 228 |
} |
| 229 |
|
| 230 |
onKnobMouseDown(ev) { |
| 231 |
const knobEl = this.knobRef.el; |
| 232 |
if (!knobEl) { |
| 233 |
return; |
| 234 |
} |
| 235 |
const knobRadius = knobEl.offsetWidth / 2; |
| 236 |
const knobRect = knobEl.getBoundingClientRect(); |
| 237 |
const centerX = knobRect.left + knobRadius; |
| 238 |
const centerY = knobRect.top + knobRadius; |
| 239 |
|
| 240 |
const updateAngle = (ev) => { |
| 241 |
// calculate the differences between the mouse position and the |
| 242 |
// center of the knob |
| 243 |
const distanceX = ev.clientX - centerX; |
| 244 |
const distanceY = ev.clientY - centerY; |
| 245 |
|
| 246 |
// calculate the angle between the center and the mouse position |
| 247 |
const angle = Math.atan2(distanceY, distanceX) * (180 / Math.PI); |
| 248 |
this.state.angle = Math.round((angle + 360) % 360); |
| 249 |
}; |
| 250 |
|
| 251 |
updateAngle(ev); |
| 252 |
this.onColorGradientChange(); |
| 253 |
|
| 254 |
const onKnobMouseMove = (ev) => { |
| 255 |
updateAngle(ev); |
| 256 |
this.onColorGradientChange(); |
| 257 |
}; |
| 258 |
const onKnobMouseUp = () => document.removeEventListener("mousemove", onKnobMouseMove); |
| 259 |
|
| 260 |
document.addEventListener("mousemove", onKnobMouseMove); |
| 261 |
document.addEventListener("mouseup", onKnobMouseUp, { once: true }); |
| 262 |
} |
| 263 |
} |