adds export to vscode extension (#608)

This commit is contained in:
Steve Ruiz 2022-03-09 11:26:37 +00:00 committed by GitHub
parent 77534ca124
commit f099630d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import { Tldraw, TldrawApp, TDFile, TDDocument } from '@tldraw/tldraw'
import { vscode } from './utils/vscode' import { vscode } from './utils/vscode'
import { defaultDocument } from './utils/defaultDocument' import { defaultDocument } from './utils/defaultDocument'
import type { MessageFromExtension, MessageFromWebview } from './types' import type { MessageFromExtension, MessageFromWebview } from './types'
import { exportToImage } from 'utils/export'
// Will be placed in global scope by extension // Will be placed in global scope by extension
declare let currentFile: TDFile declare let currentFile: TDFile
@ -60,6 +61,7 @@ export default function App(): JSX.Element {
document={rInitialDocument.current} document={rInitialDocument.current}
onMount={handleMount} onMount={handleMount}
onPersist={handlePersist} onPersist={handlePersist}
onExport={exportToImage}
autofocus autofocus
/> />
</div> </div>

View file

@ -0,0 +1,29 @@
import { TDExport } from '@tldraw/tldraw'
export const EXPORT_ENDPOINT =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000/api/export'
: 'https://www.tldraw.com/api/export'
export async function exportToImage(info: TDExport) {
if (info.serialized) {
const link = document.createElement('a')
link.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(info.serialized)
link.download = info.name + '.' + info.type
link.click()
return
}
const response = await fetch(EXPORT_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(info),
})
const blob = await response.blob()
const blobUrl = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = blobUrl
link.download = info.name + '.' + info.type
link.click()
}