-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Fill Styles to generate fills more efficiently (#180)
* first commit * change esbuild * changeset
- Loading branch information
Showing
23 changed files
with
137 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"penpot-exporter": minor | ||
--- | ||
|
||
Use Fill Styles to optimize fills transformations |
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,27 @@ | ||
import { Fill } from '@ui/lib/types/utils/fill'; | ||
|
||
class StyleLibrary { | ||
private styles: Map<string, Fill[]> = new Map(); | ||
|
||
public register(id: string, styles: Fill[]) { | ||
this.styles.set(id, styles); | ||
} | ||
|
||
public get(id: string): Fill[] | undefined { | ||
return this.styles.get(id); | ||
} | ||
|
||
public has(id: string): boolean { | ||
return this.styles.has(id); | ||
} | ||
|
||
public all(): Record<string, Fill[]> { | ||
return Object.fromEntries(this.styles.entries()); | ||
} | ||
|
||
public init(styles: Record<string, Fill[]>): void { | ||
this.styles = new Map(Object.entries(styles)); | ||
} | ||
} | ||
|
||
export const styleLibrary = new StyleLibrary(); |
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 |
---|---|---|
@@ -1,11 +1,53 @@ | ||
import { translateFills } from '@plugin/translators/fills'; | ||
import { translateFillStyle, translateFills } from '@plugin/translators/fills'; | ||
import { StyleTextSegment } from '@plugin/translators/text/paragraph'; | ||
|
||
import { ShapeAttributes } from '@ui/lib/types/shapes/shape'; | ||
import { TextStyle } from '@ui/lib/types/shapes/textShape'; | ||
|
||
export const transformFills = ( | ||
node: MinimalFillsMixin & DimensionAndPositionMixin | ||
): Pick<ShapeAttributes, 'fills'> => { | ||
node: | ||
| (MinimalFillsMixin & DimensionAndPositionMixin) | ||
| VectorRegion | ||
| VectorNode | ||
| StyleTextSegment | ||
): Pick<ShapeAttributes, 'fills' | 'fillStyleId'> | Pick<TextStyle, 'fills' | 'fillStyleId'> => { | ||
if (hasFillStyle(node)) { | ||
return { | ||
fills: [], | ||
fillStyleId: translateFillStyle(node.fillStyleId, node.fills) | ||
}; | ||
} | ||
|
||
return { | ||
fills: translateFills(node.fills) | ||
}; | ||
}; | ||
|
||
export const transformVectorFills = ( | ||
node: VectorNode, | ||
vectorPath: VectorPath, | ||
vectorRegion: VectorRegion | undefined | ||
): Pick<ShapeAttributes, 'fills' | 'fillStyleId'> => { | ||
if (vectorPath.windingRule === 'NONE') { | ||
return { | ||
fills: [] | ||
}; | ||
} | ||
|
||
const fillsNode = vectorRegion?.fills ? vectorRegion : node; | ||
return transformFills(fillsNode); | ||
}; | ||
|
||
const hasFillStyle = ( | ||
node: | ||
| (MinimalFillsMixin & DimensionAndPositionMixin) | ||
| VectorRegion | ||
| VectorNode | ||
| StyleTextSegment | ||
): boolean => { | ||
return ( | ||
node.fillStyleId !== figma.mixed && | ||
node.fillStyleId !== undefined && | ||
node.fillStyleId.length > 0 | ||
); | ||
}; |
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { PenpotPage } from '@ui/lib/types/penpotPage'; | ||
import { ComponentShape } from '@ui/lib/types/shapes/componentShape'; | ||
import { Fill } from '@ui/lib/types/utils/fill'; | ||
|
||
export type PenpotDocument = { | ||
name: string; | ||
children?: PenpotPage[]; | ||
components: Record<string, ComponentShape>; | ||
images: Record<string, Uint8Array>; | ||
styles: Record<string, Fill[]>; | ||
}; |