-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): use figma variables to store/export raddii slices
introduced logic to init the morfeo variable collection and save on the state the radii variables found. Add also some defaults if there are no valid radii variables found wip #27
- Loading branch information
1 parent
243d05f
commit 40175c8
Showing
11 changed files
with
318 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { MORFEO_COLLECTION_NAME } from "../constants"; | ||
import { useInitCollection } from "./useInitCollection"; | ||
|
||
describe("useInitCollection", () => { | ||
it("should do nothing if the collection already exist", () => { | ||
jest | ||
.spyOn(figma.variables, "getVariableCollectionById") | ||
.mockReturnValue({} as any); | ||
const mockSetCollection = jest.fn(); | ||
|
||
useInitCollection( | ||
{ id: "", defaultModeId: "", name: "" }, | ||
mockSetCollection | ||
); | ||
|
||
expect(mockSetCollection).not.toBeCalled(); | ||
expect(figma.variables.createVariableCollection).not.toBeCalled(); | ||
}); | ||
|
||
it("should set the state if getVariableCollectionById doesn't find anything and there's a collection with matching name", () => { | ||
jest | ||
.spyOn(figma.variables, "getVariableCollectionById") | ||
.mockReturnValue(null); | ||
|
||
jest.spyOn(figma.variables, "getLocalVariableCollections").mockReturnValue([ | ||
{ | ||
id: "morfeo:collection:id", | ||
defaultModeId: "default:mode:id", | ||
name: MORFEO_COLLECTION_NAME, | ||
} as any, | ||
]); | ||
const mockSetCollection = jest.fn(); | ||
|
||
useInitCollection( | ||
{ id: "", defaultModeId: "", name: "" }, | ||
mockSetCollection | ||
); | ||
|
||
expect(mockSetCollection).toBeCalledWith({ | ||
id: "morfeo:collection:id", | ||
defaultModeId: "default:mode:id", | ||
name: MORFEO_COLLECTION_NAME, | ||
}); | ||
expect(figma.variables.createVariableCollection).not.toBeCalled(); | ||
}); | ||
|
||
it("should set the collection if getVariableCollectionById doesn't find anything and there's NOT any collection with matching name", () => { | ||
jest | ||
.spyOn(figma.variables, "getVariableCollectionById") | ||
.mockReturnValue(null); | ||
|
||
jest | ||
.spyOn(figma.variables, "getLocalVariableCollections") | ||
.mockReturnValue([]); | ||
const mockSetCollection = jest.fn(); | ||
|
||
useInitCollection( | ||
{ id: "", defaultModeId: "", name: "" }, | ||
mockSetCollection | ||
); | ||
|
||
expect(mockSetCollection).toBeCalledWith({ | ||
id: expect.any(String), | ||
defaultModeId: expect.any(String), | ||
name: MORFEO_COLLECTION_NAME, | ||
}); | ||
expect(figma.variables.createVariableCollection).toBeCalledWith( | ||
MORFEO_COLLECTION_NAME | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { MORFEO_COLLECTION_NAME } from "../constants"; | ||
import { MorfeoCollection } from "../types"; | ||
|
||
const { widget } = figma; | ||
const { useEffect } = widget; | ||
|
||
export const useInitCollection = ( | ||
collection: MorfeoCollection, | ||
setCollection: (newCollection: MorfeoCollection) => void | ||
) => { | ||
useEffect(() => { | ||
if (figma.variables.getVariableCollectionById(collection.id)) { | ||
return; | ||
} | ||
|
||
const documentCollections = figma.variables.getLocalVariableCollections(); | ||
|
||
const morfeoCollection = documentCollections.find( | ||
(collection) => collection.name === MORFEO_COLLECTION_NAME | ||
); | ||
|
||
if (morfeoCollection) { | ||
const { id, name, defaultModeId } = morfeoCollection; | ||
setCollection({ id, name, defaultModeId }); | ||
} else { | ||
const { id, name, defaultModeId } = | ||
figma.variables.createVariableCollection(MORFEO_COLLECTION_NAME); | ||
setCollection({ id, name, defaultModeId }); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.