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

807 cannot set a number fontweight in typography #1197

Merged
merged 10 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
24 changes: 24 additions & 0 deletions src/plugin/figmaTransforms/fontWeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function convertFontWeightToFigma(value: string) {
switch (value) {
case '100':
return ['Thin', 'Hairline'];
case '200':
return ['ExtraLight', 'Extra Light', 'UltraLight', 'Ultra Light'];
case '300':
return ['Light'];
case '400':
return ['Regular', 'Normal'];
case '500':
return ['Medium'];
case '600':
return ['SemiBold', 'Semi Bold', 'DemiBold', 'Demi Bold'];
case '700':
return ['Bold'];
case '800':
return ['ExtraBold', 'Extra Bold', 'UltraBold', 'Ultra Bold'];
case '900':
return ['Black', 'Heavy'];
default:
return [];
}
}
10 changes: 10 additions & 0 deletions src/plugin/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe('transformValue', () => {
type: 'opacity',
output: 0.6,
},
{
input: '100',
type: 'fontWeights',
output: ['Thin', 'Hairline'],
},
{
input: 'bold',
type: 'fontWeights',
output: [],
},
];
it('transforms non-conform values into their required formats', () => {
tokens.forEach((token) => {
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { convertLineHeightToFigma } from './figmaTransforms/lineHeight';
import { convertBoxShadowTypeToFigma } from './figmaTransforms/boxShadow';
import { convertTextCaseToFigma } from './figmaTransforms/textCase';
import { convertTextDecorationToFigma } from './figmaTransforms/textDecoration';
import { convertFontWeightToFigma } from './figmaTransforms/fontWeight';
import { UserIdProperty } from '@/figmaStorage';
import { generateId } from '@/utils/generateId';
import { Properties } from '@/constants/Properties';
Expand All @@ -27,6 +28,7 @@ export async function getUserId() {
return userId;
}

export function transformValue(value: string, type: 'fontWeights'): ReturnType<typeof convertFontWeightToFigma>;
export function transformValue(value: string, type: 'letterSpacing'): LetterSpacing | null;
export function transformValue(value: string, type: 'lineHeights'): LineHeight | null;
export function transformValue(value: string, type: 'boxShadowType'): ReturnType<typeof convertBoxShadowTypeToFigma>;
Expand Down Expand Up @@ -60,6 +62,8 @@ export function transformValue(value: string, type: string) {
case 'paragraphSpacing':
case 'fontSizes':
return convertTypographyNumberToFigma(value);
case 'fontWeights':
return convertFontWeightToFigma(value);
case 'letterSpacing':
return convertLetterSpacingToFigma(value);
case 'lineHeights':
Expand Down
20 changes: 20 additions & 0 deletions src/plugin/setTextValuesOnTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import setTextValuesOnTarget from './setTextValuesOnTarget';

describe('setTextValuesOnTarget', () => {
let textNodeMock;
const loadFontAsyncSpy = jest.spyOn(figma, 'loadFontAsync');

beforeEach(() => {
textNodeMock = {
Expand Down Expand Up @@ -35,6 +36,25 @@ describe('setTextValuesOnTarget', () => {
expect(textNodeMock).toEqual({ ...textNodeMock, fontName: { ...textNodeMock.fontName, style: 'Bold' } });
});

it('converts a numerical fontWeight and sets to the node', async () => {
loadFontAsyncSpy.mockImplementationOnce(() => (
Promise.reject()
));
loadFontAsyncSpy.mockImplementation(() => (
Promise.resolve()
));
await setTextValuesOnTarget(textNodeMock, { value: { fontWeight: '500' } });
expect(textNodeMock).toEqual({ ...textNodeMock, fontName: { ...textNodeMock.fontName, style: 'Medium' } });
});

it('can\'t set number fontWeight to the node if there is no matching fontWeight', async () => {
loadFontAsyncSpy.mockImplementation(() => (
Promise.reject()
));
await setTextValuesOnTarget(textNodeMock, { value: { fontWeight: '500' } });
expect(textNodeMock).toEqual({ ...textNodeMock, fontName: { ...textNodeMock.fontName } });
});

it('sets textCase, textDecoration and description if those are given', async () => {
await setTextValuesOnTarget(textNodeMock, {
description: 'Use with care',
Expand Down
34 changes: 27 additions & 7 deletions src/plugin/setTextValuesOnTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,34 @@ export default async function setTextValuesOnTarget(target: TextNode | TextStyle
} = value;
const family = fontFamily?.toString() || (target.fontName !== figma.mixed ? target.fontName.family : '');
const style = fontWeight?.toString() || (target.fontName !== figma.mixed ? target.fontName.style : '');
await figma.loadFontAsync({ family, style });
if (fontFamily || fontWeight) {
target.fontName = {
family,
style,
};
}

try {
if (fontFamily || fontWeight) {
await figma.loadFontAsync({ family, style });
target.fontName = {
family,
style,
};
}
} catch {
const candidateStyles = transformValue(style, 'fontWeights');
await Promise.all(
candidateStyles.map(async (candidateStyle) => (
figma.loadFontAsync({ family, style: candidateStyle })
.then(() => {
if (candidateStyle) {
target.fontName = {
family,
style: candidateStyle,
};
}
})
.catch((e) => {
console.log('Error setting fontWeight on target', e);
})
)),
);
}
if (fontSize) {
target.fontSize = transformValue(fontSize, 'fontSizes');
}
Expand Down
1 change: 0 additions & 1 deletion src/plugin/setValuesOnNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default async function setValuesOnNode(
const stylePathPrefix = prefixStylesWithThemeName && activeThemeObject
? activeThemeObject.name
: null;

try {
// BORDER RADIUS
if (
Expand Down