-
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.
- Loading branch information
1 parent
7e4a618
commit ec88fb9
Showing
8 changed files
with
200 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
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,76 @@ | ||
import { emit, on, showUI } from "@create-figma-plugin/utilities"; | ||
import { SourceCopies } from "./types"; | ||
import { getTextNodes } from "./utils"; | ||
|
||
export default function () { | ||
showUI({ | ||
height: 400, | ||
width: 500, | ||
}); | ||
|
||
// Get the nodes from the selection or the whole document | ||
const selection = figma.currentPage.selection.slice(); | ||
const textNodes = selection.length | ||
? getTextNodes(selection) | ||
: figma.currentPage.findAll((node) => node.type === "TEXT") as TextNode[]; | ||
|
||
if (!textNodes.length) { | ||
emit("COMPLETED", { | ||
title: selection.length | ||
? "No text nodes found in the selection" | ||
: "No text nodes found in the document", | ||
}); | ||
} else { | ||
// Send document defaults | ||
emit("DEFAULTS", { | ||
url: figma.root.getPluginData("url"), | ||
url_2: figma.root.getPluginData("url_2"), | ||
url_3: figma.root.getPluginData("url_3"), | ||
}); | ||
} | ||
|
||
// The copies have been loaded | ||
on("FETCHED_COPIES", (data: SourceCopies) => { | ||
// Save document urls defaults | ||
figma.root.setPluginData("url", data.url); | ||
figma.root.setPluginData("url_2", data.url_2); | ||
figma.root.setPluginData("url_3", data.url_3); | ||
|
||
// Search and replace the text nodes with the copies | ||
const nodes = getUntranslatedNodes(textNodes, data.copies); | ||
|
||
if (nodes.length) { | ||
// Update the copies | ||
emit("UNTRANSLATED", { nodes }); | ||
} else { | ||
const title = selection.length | ||
? "No missing copies found in the selection" | ||
: "No missing copies found in the document"; | ||
emit("COMPLETED", { title }); | ||
} | ||
}); | ||
} | ||
|
||
// Select a text node | ||
on("SELECT_NODE", (data: { node: { id: string } }) => { | ||
const node = figma.currentPage.findOne((node) => | ||
node.id === data.node.id | ||
) as TextNode; | ||
if (node) { | ||
figma.currentPage.selection = [node]; | ||
figma.viewport.scrollAndZoomIntoView([node]); | ||
} | ||
}); | ||
|
||
// Close the plugin when the UI is closed | ||
on("CLOSE", () => figma.closePlugin()); | ||
|
||
/** Filter all TextNodes without translations */ | ||
function getUntranslatedNodes( | ||
nodes: TextNode[], | ||
copies: Record<string, string>, | ||
): [string, string, string][] { | ||
return nodes.filter((node) => !(node.name in copies)).map(( | ||
node, | ||
) => [node.name, node.characters, node.id]); | ||
} |
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,62 @@ | ||
import { | ||
Button, | ||
IconButton, | ||
IconPlus32, | ||
Inline, | ||
Text, | ||
VerticalSpace, | ||
} from "@create-figma-plugin/ui"; | ||
import { h } from "preact"; | ||
|
||
interface Props { | ||
onClose: () => void; | ||
onSelectNode: (data: any) => void; | ||
nodes: [string, string, string][]; | ||
} | ||
|
||
export default function VariablesForm( | ||
{ nodes, onSelectNode, onClose }: Props, | ||
) { | ||
nodes.sort((a, b) => a[0].localeCompare(b[0])); | ||
|
||
return ( | ||
<div> | ||
<VerticalSpace space="large" /> | ||
<Text> | ||
The following nodes are missing in the Google Spreadsheet document: | ||
</Text> | ||
<VerticalSpace space="extraLarge" /> | ||
|
||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Id</th> | ||
<th>Text</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{nodes.map((node, index) => ( | ||
<tr> | ||
<td>{node[0]}</td> | ||
<td>{node[1]}</td> | ||
<IconButton | ||
type="button" | ||
onClick={() => onSelectNode({ node: { id: node[2] } })} | ||
> | ||
<IconPlus32 /> | ||
</IconButton> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
|
||
<VerticalSpace space="large" /> | ||
|
||
<Inline space="extraSmall"> | ||
<Button onClick={onClose}>Close</Button> | ||
</Inline> | ||
<VerticalSpace space="small" /> | ||
</div> | ||
); | ||
} |
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