Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Add support for applying gradient to text #13590

Merged
merged 34 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9d03253
Add textColor attribute to TextElement
Swanand01 Feb 16, 2024
734bd44
Add `generateTextColorCSS` for generating CSS styles from textColor
Swanand01 Feb 16, 2024
ecf99c0
Get color using `useCommonColorValue` and use `pushUpdate` instead of…
Swanand01 Feb 16, 2024
81328b3
Generate and apply textColor styles to `EditTextBox`
Swanand01 Feb 16, 2024
9246668
Remove `useColorTransformHandler` for `color` property.
Swanand01 Feb 16, 2024
57f6e90
Merge branch 'main' into feat/12143-apply-gradient-to-text
AnuragVasanwala Feb 17, 2024
9562520
⏪ Revert the added `textColor` attribute
Swanand01 Feb 22, 2024
861e8ad
⏪ Revert the added `generateTextColorCSS` function
Swanand01 Feb 22, 2024
d9df571
⏪ Revert the used `textColor` attribute
Swanand01 Feb 22, 2024
b03db75
Add rich-text formatter (getter and setter) for gradient color
Swanand01 Feb 22, 2024
cbb9ce0
Add `getColorFromGradientStyle` and `getGradientStyleFromColor` utili…
Swanand01 Feb 22, 2024
987ad71
⏪ Revert the used `textColor` attribute in `edit.js`
Swanand01 Feb 22, 2024
eec3e96
♻️ Refactor to use the gradientColor formatters
Swanand01 Feb 22, 2024
5a6aa48
Merge branch 'feat/12143-apply-gradient-to-text' of https://github.co…
Swanand01 Feb 22, 2024
09ba44d
Update regex for rotation in linear gradient
Swanand01 Mar 4, 2024
e587b08
♻️ Remove use of shorthand css styles
Swanand01 Mar 4, 2024
6ad55fa
Unset WebkitTextFillColor on selecting text
Swanand01 Mar 4, 2024
9e0b841
Refine rotation regex for linear gradient
Swanand01 Mar 4, 2024
3e5fa35
Export and use `DEFAULT_GRADIENT`
Swanand01 Mar 6, 2024
ebe42f8
Merge branch 'main' into feat/12143-apply-gradient-to-text
AnuragVasanwala Mar 6, 2024
387d45d
Fix: Show `Mixed` in color input on selecting two text elements with …
Swanand01 Mar 18, 2024
0edb225
Fix redundant imports
Swanand01 Mar 18, 2024
b918943
Use `DEFAULT_GRADIENT` in `textColor.js`
Swanand01 Mar 18, 2024
ac339b3
Add unit tests
Swanand01 Mar 18, 2024
5ad437d
Add gradient button getters to `ColorPicker` container
Swanand01 Mar 18, 2024
55e14a8
Add karma tests
Swanand01 Mar 18, 2024
af4583f
Fix linting issues
Swanand01 Mar 18, 2024
9b69f1d
Merge branch 'main' into feat/12143-apply-gradient-to-text
Swanand01 Mar 18, 2024
06e0660
Move `DEFAULT_GRADIENT`, `GRADIENT` and `GRADIENT_REGEX` to `constant…
Swanand01 Mar 19, 2024
a949b4a
Fix order of styles in `stylesToCSS`
Swanand01 Mar 19, 2024
8cb55a8
Throw error if invalid style string is passed to `getColorFromGradien…
Swanand01 Mar 19, 2024
4610988
Add unit tests for `getColorFromGradientStyle`
Swanand01 Mar 19, 2024
64710d2
Update the order of styles in unit tests and karma tests
Swanand01 Mar 19, 2024
413b47d
Merge branch 'feat/12143-apply-gradient-to-text' of https://github.co…
Swanand01 Mar 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/element-library/src/text/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ function TextDisplay({
targetRef: bgRef,
expectedStyle: 'background',
});
useColorTransformHandler({ id, targetRef: fgRef, expectedStyle: 'color' });
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
useColorTransformHandler({
id,
targetRef: refWithBorder,
Expand Down
54 changes: 54 additions & 0 deletions packages/patterns/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Internal dependencies
*/
import { PatternType, type Linear } from './types';

export const DEFAULT_GRADIENT: Linear = {
type: PatternType.Linear,
stops: [
{
color: {
r: 0,
g: 0,
b: 0,
},
position: 0,
},
{
color: {
r: 1,
g: 1,
b: 1,
},
position: 1,
},
],
};

export const GRADIENT = {
LINEAR: 'linear-gradient',
RADIAL: 'radial-gradient',
};

export const GRADIENT_REGEX = {
[GRADIENT.LINEAR]:
/linear-gradient\((?:-?\d*\.?\d+turn,\s*)?(?:rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),?\s*(\d*\.?\d+)?\)|#([0-9a-fA-F]{6}))\s*0%,\s*(?:rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),?\s*(\d*\.?\d+)?\)|#([0-9a-fA-F]{6}))\s*100%\)/,
[GRADIENT.RADIAL]:
/radial-gradient\((?:rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),?\s*(\d*\.?\d+)?\)|#([0-9a-fA-F]{6}))\s*0%,\s*(?:rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),?\s*(\d*\.?\d+)?\)|#([0-9a-fA-F]{6}))\s*100%\)/,
};
96 changes: 96 additions & 0 deletions packages/patterns/src/getColorFromGradientStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Internal dependencies
*/
import { DEFAULT_GRADIENT, GRADIENT, GRADIENT_REGEX } from './constants';
import createSolidFromString from './createSolidFromString';
import { PatternType, type Gradient } from './types';

export default function getColorFromGradientStyle(style: string): Gradient {
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
if (style.includes(GRADIENT.LINEAR)) {
return parseGradient(style, GRADIENT.LINEAR);
} else if (style.includes(GRADIENT.RADIAL)) {
return parseGradient(style, GRADIENT.RADIAL);
}
throw new Error('Invalid style string passed.');
}

function parseGradient(style: string, gradient: string): Gradient {
const regex = GRADIENT_REGEX[gradient];
const matches = style.match(regex);
if (!matches) {
return DEFAULT_GRADIENT;
}

const { startColor, endColor } = getColorRange(matches);

if (gradient === GRADIENT.LINEAR) {
const rotationMatch = style.match(/0(\.((25|5|75)))?turn/);
return {
type: PatternType.Linear,
stops: [
{ color: startColor, position: 0 },
{ color: endColor, position: 1 },
],
rotation: rotationMatch ? parseFloat(rotationMatch[0]) : 0,
};
}

return {
type: PatternType.Radial,
stops: [
{ color: startColor, position: 0 },
{ color: endColor, position: 1 },
],
};
}

function getColorRange(matches: RegExpMatchArray) {
const [
,
startColorR,
startColorG,
startColorB,
startAlpha,
startHex,
endColorR,
endColorG,
endColorB,
endAlpha,
endHex,
] = matches;

const startColor = startHex
? createSolidFromString(`#${startHex}`).color
: parseColor(startColorR, startColorG, startColorB, startAlpha);

const endColor = endHex
? createSolidFromString(`#${endHex}`).color
: parseColor(endColorR, endColorG, endColorB, endAlpha);

return { startColor, endColor };
}

function parseColor(r: string, g: string, b: string, a: string) {
return {
r: parseInt(r),
g: parseInt(g),
b: parseInt(b),
a: a ? parseFloat(a) : undefined,
};
}
26 changes: 26 additions & 0 deletions packages/patterns/src/getGradientStyleFromColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Internal dependencies
*/
import generatePatternStyles from './generatePatternStyles';
import type { Gradient } from './types';

export default function getGradientStyleFromColor(color: Gradient) {
const gradient = generatePatternStyles(color);

return gradient.backgroundImage;
}
3 changes: 3 additions & 0 deletions packages/patterns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

export * from './types';
export * from './constants';

export { default as convertToCSS } from './convertToCSS';
export { default as createSolid } from './createSolid';
Expand All @@ -30,3 +31,5 @@ export { default as hasGradient } from './hasGradient';
export { default as hasOpacity } from './hasOpacity';
export { default as isPatternEqual } from './isPatternEqual';
export { default as isHexColorString } from './isHexColorString';
export { default as getGradientStyleFromColor } from './getGradientStyleFromColor';
export { default as getColorFromGradientStyle } from './getColorFromGradientStyle';
Loading
Loading