3601if (shapeSettings && !from) return null;3602const angle = from ? t_animationSBS__parseGradientAngle(from[1]) : 0;3603if (angle === null || Number.isNaN(angle)) return null;3604return { type, kind: type, angle, center: position };3605}36063607const tokens = shapeSettings.split(/\s+/).filter(Boolean);3608const shape = tokens.includes('circle') ? 'circle' : 'ellipse';3609const sizeTokens = tokens.filter(token => token !== 'circle' && token !== 'ellipse');3610const sizeKeywords = ['closest-side', 'closest-corner', 'farthest-side', 'farthest-corner'];3611if (!sizeTokens.length || sizeKeywords.includes(sizeTokens[0])) {
if (sizeTokens.length > 1) return null; const sizeKeyword = sizeTokens[0] || 'farthest-corner'; return { type, kind: `${type}-${shape}-${sizeKeyword}`, center: position }; } // у круга размер - одна длина в px, у эллипса - две длины в px или % const lengths = sizeTokens.map(t_animationSBS__parseGradientLength); const isValidCircle = shape === 'circle' && lengths.length === 1 && lengths[0] && !lengths[0].p; const isValidEllipse = shape === 'ellipse' && lengths.length === 2 && lengths.every(Boolean); if (!isValidCircle && !isValidEllipse) return null; const size = { w: lengths[0], h: lengths[1] || lengths[0] }; return { type, kind: `${type}-${shape}-length`, center: position, size };}/** * Парсит угол градиента: в deg, turn, rad или grad, у linear еще 'to top/right/bottom/left' * * @param {string | undefined} token * @returns {number | null} - угол в градусах; null - аргумент не угол; NaN - направление в угол */function t_animationSBS__parseGradientAngle(token) { if (!token) return null; const directions = { 'to top': 0, 'to right': 90, 'to bottom': 180, 'to left': 270 }; if (token in directions) return directions[token]; if (token.startsWith('to ')) return NaN; const angle = token.match(/^(-?[\d.]+)(deg|turn|rad|grad)$/); if (!angle) return null; const units = { deg: 1, turn: 360, rad: 180 / Math.PI, grad: 0.9 }; return parseFloat(angle[1]) * units[angle[2]];}/** * Парсит длину градиента в px или % в вид «проценты + пиксели», который можно интерполировать * * @param {string} token * @returns {sbsLength | null} */function t_animationSBS__parseGradientLength(token) { const length = token.match(/^(-?[\d.]+)(px|%)?$/); if (!length || (!length[2] && parseFloat(length[1]) !== 0)) return null; const number = parseFloat(length[1]); return length[2] === '%' ? { p: number, x: 0 } : { p: 0, x: number };}/** * Парсит позицию центра radial и conic: одно или два значения (px, %, left/center/right/top/bottom) * или 4-значную форму от краев: 'right 10px bottom 20%' - это x = calc(100% - 10px), y = 80% * * @param {string} settings * @returns {{x: sbsLength, y: sbsLength} | null} - null для 3-значной формы и нераспознанных значений */function t_animationSBS__parseGradientPosition(settings) { const keywords = { x: { left: 0, center: 50, right: 100 }, y: { top: 0, center: 50, bottom: 100 }, }; const parseCoordinate = (token, axis) => token in keywords[axis] ? { p: keywords[axis][token], x: 0 } : t_animationSBS__parseGradientLength(token); const tokens = settings.trim().split(/\s+/); if (tokens.length === 4) { const position = {}; for (let i = 0; i < 4; i += 2) { const edge = tokens[i]; const offset = t_animationSBS__parseGradientLength(tokens[i + 1]); const axis = edge === 'left' || edge === 'right' ? 'x' : 'y'; if (!offset || !['left', 'right', 'top', 'bottom'].includes(edge) || position[axis]) return null; // от правого и нижнего края позиция считается как 100% минус отступ const isFarEdge = edge === 'right' || edge === 'bottom'; position[axis] = isFarEdge ? { p: 100 - offset.p, x: -offset.x } : offset; } return position; } if (tokens.length > 2) return null; // ключевые слова по вертикали могут стоять первыми: 'top left' const isSwapped = tokens[0] in keywords.y && !(tokens[0] in keywords.x); const [xToken, yToken = 'center'] = isSwapped ? [tokens[1] || 'center', tokens[0]] : tokens; const x = parseCoordinate(xToken, 'x'); const y = parseCoordinate(yToken, 'y'); return x && y ? { x, y } : null;}/** * Парсит стоп градиента. Цвет может содержать пробелы (rgba(0, 0, 0, 1)), поэтому сначала стоп целиком * пробуется как цвет без позиции, а потом позицией считается последнее число с % через пробел * * @param {string} token - стоп градиента: цвет и позиция в процентах, позиция может отсутствовать * @returns {{color: sbsRGBA, pos: number | null} | null} - null, если цвет не распарсился