UNPKG

@wordpress/components

Version:
206 lines (205 loc) • 7.53 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let gradient_parser = require("gradient-parser"); gradient_parser = __toESM(gradient_parser); let colord = require("colord"); let colord_plugins_names = require("colord/plugins/names"); colord_plugins_names = __toESM(colord_plugins_names); let __constants_ts = require("./constants.cjs"); let __serializer_ts = require("./serializer.cjs"); // packages/components/src/custom-gradient-picker/utils.ts (0, colord.extend)([colord_plugins_names.default]); const ANGLE_UNIT_TO_DEGREES = { deg: 1, rad: 180 / Math.PI, grad: .9, turn: 360 }; function getLinearGradientRepresentation(gradientAST) { return (0, __serializer_ts.serializeGradient)({ type: "linear-gradient", orientation: __constants_ts.HORIZONTAL_GRADIENT_ORIENTATION, colorStops: gradientAST.colorStops }); } function hasUnsupportedLength(item) { return item.length === void 0 || item.length.type !== "%"; } /** * Reads the first argument of a gradient, the one that holds the orientation * and the color interpolation method, as a list of space separated tokens. * * @param gradient CSS gradient. * @param start Index right after the opening parenthesis. */ function readFirstArgument(gradient, start) { const tokens = []; let token = ""; let depth = 0; for (let index = start; index < gradient.length; index++) { const char = gradient[index]; if (depth === 0 && (char === "," || char === ")")) { if (token) tokens.push(token); return { tokens, end: index }; } if (char === "(") depth++; else if (char === ")") depth--; if (depth === 0 && /\s/.test(char)) { if (token) tokens.push(token); token = ""; } else token += char; } } /** * `gradient-parser` can't read a color interpolation method such as * `in oklch`, so it is taken out of the gradient before it is parsed. * * Only the first argument is searched, at its top level, so a color stop like * `color-mix( in srgb, … )` is never mistaken for one. * * This is a workaround until `gradient-parser` supports color interpolation * methods. It can be removed once an upstream release does: * https://github.com/rafaelcaricio/gradient-parser/issues/57 * * @param gradient CSS gradient. */ function extractColorInterpolation(gradient) { const start = gradient.indexOf("(") + 1; const firstArgument = start && readFirstArgument(gradient, start); if (!firstArgument) return { gradient }; const { tokens, end } = firstArgument; const words = tokens.map((token) => token.toLowerCase()); const index = words.indexOf("in"); if (index === -1) return { gradient }; const colorSpace = words[index + 1]; let length; if (__constants_ts.POLAR_INTERPOLATION_COLOR_SPACES.includes(colorSpace) && __constants_ts.HUE_INTERPOLATION_METHODS.includes(words[index + 2]) && words[index + 3] === "hue") length = 4; else if (__constants_ts.RECTANGULAR_INTERPOLATION_COLOR_SPACES.includes(colorSpace) || __constants_ts.POLAR_INTERPOLATION_COLOR_SPACES.includes(colorSpace)) length = 2; else return { gradient }; const remainder = [...tokens.slice(0, index), ...tokens.slice(index + length)].join(" "); const hasEmptyFirstArgument = !remainder && gradient[end] === ","; return { gradient: gradient.slice(0, start) + remainder + gradient.slice(hasEmptyFirstArgument ? end + 1 : end), colorInterpolation: words.slice(index, index + length).join(" ") }; } function getGradientAstWithDefault(value) { let gradientAST; let hasGradient = !!value; const { gradient: valueToParse, colorInterpolation } = extractColorInterpolation(value ?? __constants_ts.DEFAULT_GRADIENT); try { gradientAST = gradient_parser.default.parse(valueToParse)[0]; if (colorInterpolation) gradientAST.colorInterpolation = colorInterpolation; } catch (error) { console.warn("wp.components.CustomGradientPicker failed to parse the gradient with error", error); } if (!gradientAST || gradientAST.type === "conic-gradient" || gradientAST.type === "repeating-conic-gradient" || gradientAST.colorStops.some((stop) => stop.length2 !== void 0)) { gradientAST = gradient_parser.default.parse(__constants_ts.DEFAULT_GRADIENT)[0]; hasGradient = false; } if (!Array.isArray(gradientAST.orientation) && gradientAST.orientation?.type === "directional") { const direction = gradientAST.orientation.value.toLowerCase(); gradientAST.orientation = { type: "angular", value: __constants_ts.DIRECTIONAL_ORIENTATION_ANGLE_MAP[direction].toString(), unit: "deg" }; } if (!Array.isArray(gradientAST.orientation) && gradientAST.orientation?.type === "angular" && gradientAST.orientation.unit !== "deg") { const { value: angle, unit } = gradientAST.orientation; gradientAST.orientation = { type: "angular", value: `${Number(angle) * ANGLE_UNIT_TO_DEGREES[unit]}`, unit: "deg" }; } if (gradientAST.colorStops.some(hasUnsupportedLength)) { const { colorStops } = gradientAST; const step = 100 / (colorStops.length - 1); colorStops.forEach((stop, index) => { stop.length = { value: `${step * index}`, type: "%" }; }); } return { gradientAST, hasGradient }; } function getGradientAstWithControlPoints(gradientAST, newControlPoints) { return { ...gradientAST, colorStops: newControlPoints.map(({ position, color }) => { const { r, g, b, a } = (0, colord.colord)(color).toRgb(); return { length: { type: "%", value: position?.toString() }, type: a < 1 ? "rgba" : "rgb", value: a < 1 ? [ `${r}`, `${g}`, `${b}`, `${a}` ] : [ `${r}`, `${g}`, `${b}` ] }; }) }; } function getStopCssColor(colorStop) { switch (colorStop.type) { case "hex": return `#${colorStop.value}`; case "literal": return colorStop.value; case "var": return `${colorStop.type}(${colorStop.value})`; case "rgb": case "rgba": return `${colorStop.type}(${colorStop.value.join(",")})`; case "hsl": { const [hue, saturation, lightness] = colorStop.value; return `hsl(${hue},${saturation}%,${lightness}%)`; } case "hsla": { const [hue, saturation, lightness, alpha = "1"] = colorStop.value; return `hsla(${hue},${saturation}%,${lightness}%,${alpha})`; } default: return "transparent"; } } exports.getGradientAstWithControlPoints = getGradientAstWithControlPoints; exports.getGradientAstWithDefault = getGradientAstWithDefault; exports.getLinearGradientRepresentation = getLinearGradientRepresentation; exports.getStopCssColor = getStopCssColor; //# sourceMappingURL=utils.cjs.map