-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from bridgedxyz/feature/exporter
Feature/exporter
- Loading branch information
Showing
25 changed files
with
551 additions
and
452 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 |
---|---|---|
@@ -1,3 +1,16 @@ | ||
# UI Exporter | ||
|
||
- reflect json exporter - expors ui as a whole json tree | ||
|
||
## Export mode | ||
|
||
- reflect vanilla | ||
- reflect standard | ||
- figma raw (only for figma platform) | ||
|
||
## Export range | ||
|
||
- current page | ||
- whole docs | ||
- selecetd single node | ||
- selected nodes (this will download multiple files per selection) |
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,10 @@ | ||
export function downloadFile(content) { | ||
const blob = new Blob([content], { type: "text/plain" }); | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.href = url; | ||
a.download = `export-node.json`; | ||
a.click(); | ||
a.remove(); | ||
window.URL.revokeObjectURL(url); | ||
} |
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,12 @@ | ||
import React from "react"; | ||
import { FigmaExporter } from "./figma-exporter"; | ||
import { VanillaExporter } from "./vanilla-exporter"; | ||
|
||
export function ExporterScreen() { | ||
return ( | ||
<> | ||
<FigmaExporter /> | ||
</> | ||
); | ||
// <VanillaExporter />; | ||
} |
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,22 @@ | ||
/** | ||
* file id of bridged design - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/bridged | ||
*/ | ||
export const FIGMA_DEMO_DEFAULT_FILE_ID = "Y0Gh77AqBoHH7dG1GtK3xF"; | ||
|
||
/** | ||
* extracts file id from share link | ||
* | ||
* e.g. in - "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/bridged?node-id=775%3A112" | ||
* | ||
* out - "Y0Gh77AqBoHH7dG1GtK3xF" | ||
* @param url | ||
* @returns | ||
*/ | ||
export function parseFileIdFromUrl_Figma(url: string) { | ||
// this logic is dangerous, but clean and simple. works for now. (think the url format won't change) | ||
if (url.includes("https://www.figma.com/file/")) { | ||
return url.split("/")[4]; | ||
} else { | ||
throw `figma file url must contain "https://www.figma.com/file/". the givven was ${url}, which we cannot extract file id from it.`; | ||
} | ||
} |
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,144 @@ | ||
import React, { useState } from "react"; | ||
import * as FigmaApi from "figma-js"; | ||
import { Button, LinearProgress, TextField, Tooltip } from "@material-ui/core"; | ||
import { useSingleSelection } from "../../../utils/plugin-hooks"; | ||
import { parseFileIdFromUrl_Figma } from "./figma-api-utils"; | ||
import { PluginSdk } from "../../../utils/plugin-provider/plugin-app-sdk"; | ||
import { downloadFile } from "./export-utils"; | ||
|
||
const _maybetoken = process.env.FIGMA_PERSONAL_ACCESS_TOKEN; | ||
function makeClient(token?: string): FigmaApi.ClientInterface { | ||
if (!token) { | ||
const maybePersonalAccessToken = process.env.FIGMA_PERSONAL_ACCESS_TOKEN; | ||
if (maybePersonalAccessToken) { | ||
} else { | ||
throw "no personal access token provided"; | ||
} | ||
token = maybePersonalAccessToken; | ||
} | ||
|
||
return FigmaApi.Client({ | ||
personalAccessToken: token, | ||
}); | ||
} | ||
|
||
export async function fetchFile( | ||
id: string, | ||
opt?: { | ||
token?: string; | ||
} | ||
) { | ||
const client = makeClient(opt?.token); | ||
const _fileRes = await client.file(id); | ||
const file = _fileRes.data; | ||
return file; | ||
} | ||
|
||
async function fetchNode( | ||
fileId: string, | ||
nodeIds: string[], | ||
opt?: { | ||
token?: string; | ||
} | ||
) { | ||
const client = makeClient(opt?.token); | ||
const _nodesRes = await client.fileNodes(fileId, { | ||
ids: nodeIds, | ||
}); | ||
return _nodesRes.data.nodes; | ||
} | ||
|
||
export function FigmaExporter() { | ||
const selection = useSingleSelection(); | ||
const [personalToken, setPersonalToken] = useState(_maybetoken); | ||
const [computing, setComputing] = useState<boolean>(false); | ||
const [fileId, setFileId] = useState(undefined); | ||
const handleExportCurrentSelection = () => { | ||
setComputing(true); | ||
fetchNode(fileId, [selection.id], { | ||
token: personalToken, | ||
}).then((d) => { | ||
downloadFile(JSON.stringify(d, null, 4)); | ||
setComputing(false); | ||
}); | ||
}; | ||
|
||
const handleExportCurrentFile = () => { | ||
setComputing(true); | ||
fetchFile(fileId, { | ||
token: personalToken, | ||
}).then((d) => { | ||
downloadFile(JSON.stringify(d, null, 4)); | ||
setComputing(false); | ||
}); | ||
}; | ||
|
||
const handleFileUrlUpdate = (e) => { | ||
const input = e.target.value; | ||
try { | ||
const id = parseFileIdFromUrl_Figma(input); | ||
setFileId(id); | ||
} catch (_) { | ||
PluginSdk.notify("url is invalid"); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<h4>Figma exporter</h4> | ||
<p> | ||
requirements: this feature is for bridged contributors, enabling them to | ||
export figma data for design to code development. You'll need to provide | ||
your own "FIGMA_PERSONAL_ACCESS_TOKEN" in figma/.env currently, figma | ||
plugin sdk does not allow us to retrieve file id, so you'll have to | ||
explicitly provide us the current figma file's id which is from file | ||
share link. copy-pase the whole url, we will automatically extract file | ||
id from it. | ||
</p> | ||
{computing ? ( | ||
<> | ||
<LinearProgress /> | ||
</> | ||
) : ( | ||
<> | ||
<TextField | ||
label="personal access token" | ||
fullWidth | ||
type="password" | ||
defaultValue={personalToken} | ||
onChange={(e) => { | ||
setPersonalToken(e.target.value); | ||
}} | ||
/> | ||
<TextField | ||
required | ||
disabled={fileId !== undefined} | ||
label="figma file share url" | ||
fullWidth | ||
onKeyPress={(ev) => { | ||
if (ev.key === "Enter") { | ||
handleFileUrlUpdate(ev); | ||
ev.preventDefault(); | ||
} | ||
}} | ||
/> | ||
|
||
{fileId && ( | ||
<> | ||
<Button | ||
disabled={selection == undefined} | ||
onClick={handleExportCurrentSelection} | ||
> | ||
export current selection | ||
</Button> | ||
<Button onClick={handleExportCurrentFile}> | ||
export current file | ||
</Button> | ||
<Button disabled>export current page</Button> | ||
</> | ||
)} | ||
</> | ||
)} | ||
</> | ||
); | ||
} |
Oops, something went wrong.