Tldraw/packages/ui/src/lib/hooks/useExportAs.ts

99 wiersze
2.1 KiB
TypeScript
Czysty Zwykły widok Historia

2023-04-25 11:01:25 +00:00
import {
downloadDataURLAsFile,
getSvgAsDataUrl,
getSvgAsImage,
TLExportType,
TLFrameShape,
TLShapeId,
useApp,
} from '@tldraw/editor'
import { useCallback } from 'react'
import { useToasts } from './useToastsProvider'
import { useTranslation } from './useTranslation/useTranslation'
/** @public */
export function useExportAs() {
const app = useApp()
const { addToast } = useToasts()
const msg = useTranslation()
return useCallback(
async function exportAs(ids: TLShapeId[] = app.selectedIds, format: TLExportType = 'png') {
if (ids.length === 0) {
ids = [...app.shapeIds]
}
if (ids.length === 0) {
return
}
const svg = await app.getSvg(ids, {
scale: 1,
2023-04-25 11:01:25 +00:00
background: app.instanceState.exportBackground,
})
if (!svg) throw new Error('Could not construct SVG.')
let name = 'shapes'
if (ids.length === 1) {
const first = app.getShapeById(ids[0])!
if (first.type === 'frame') {
name = (first as TLFrameShape).props.name ?? 'frame'
} else {
name = first.id.replace(/:/, '_')
}
}
switch (format) {
case 'svg': {
const dataURL = await getSvgAsDataUrl(svg)
downloadDataURLAsFile(dataURL, `${name || 'shapes'}.svg`)
return
}
case 'webp':
case 'png': {
const image = await getSvgAsImage(svg, {
type: format,
quality: 1,
scale: 2,
2023-04-25 11:01:25 +00:00
})
if (!image) {
addToast({
id: 'export-fail',
// icon: 'error',
title: msg('toast.error.export-fail.title'),
description: msg('toast.error.export-fail.desc'),
})
return
}
const dataURL = URL.createObjectURL(image)
downloadDataURLAsFile(dataURL, `${name || 'shapes'}.${format}`)
[feature] ui events (#1326) This PR updates the editor events: - adds types to the events emitted by the app (by `app.emit`) - removes a few events emitted by the app (e.g. `move-to-page`, `change-camera`) - adds `onEvent` prop to the <TldrawUi> / <Tldraw> components - call the `onEvent` when actions occur or tools are selected - does some superficial cleanup on editor app APIs ### Release Note - Fix layout bug in error dialog - (ui) Add `TLEventMap` for types emitted from editor app - (editor) Update `crash` event emitted from editor app to include error - (editor) Update `change-history` event emitted from editor app - (editor) Remove `change-camera` event from editor app - (editor) Remove `move-to-page` event from editor app - (ui) Add `onEvent` prop and events to <Tldraw> / <TldrawUi> - (editor) Replace `app.openMenus` plain Set with computed value - (editor) Add `addOpenMenu` method - (editor) Add `removeOpenMenu` method - (editor) Add `setFocusMode` method - (editor) Add `setToolLocked` method - (editor) Add `setSnapMode` method - (editor) Add `isSnapMode` method - (editor) Update `setGridMode` method return type to editor app - (editor) Update `setReadOnly` method return type to editor app - (editor) Update `setPenMode` method return type to editor app - (editor) Update `selectNone` method return type to editor app - (editor) Rename `backToContent` to `zoomToContent` - (editor) Remove `TLReorderOperation` type --------- Co-authored-by: Orange Mug <orangemug@users.noreply.github.com>
2023-05-11 22:14:58 +00:00
2023-04-25 11:01:25 +00:00
URL.revokeObjectURL(dataURL)
return
}
case 'json': {
const data = app.getContent(ids)
const dataURL = URL.createObjectURL(
new Blob([JSON.stringify(data, null, 4)], { type: 'application/json' })
)
downloadDataURLAsFile(dataURL, `${name || 'shapes'}.json`)
[feature] ui events (#1326) This PR updates the editor events: - adds types to the events emitted by the app (by `app.emit`) - removes a few events emitted by the app (e.g. `move-to-page`, `change-camera`) - adds `onEvent` prop to the <TldrawUi> / <Tldraw> components - call the `onEvent` when actions occur or tools are selected - does some superficial cleanup on editor app APIs ### Release Note - Fix layout bug in error dialog - (ui) Add `TLEventMap` for types emitted from editor app - (editor) Update `crash` event emitted from editor app to include error - (editor) Update `change-history` event emitted from editor app - (editor) Remove `change-camera` event from editor app - (editor) Remove `move-to-page` event from editor app - (ui) Add `onEvent` prop and events to <Tldraw> / <TldrawUi> - (editor) Replace `app.openMenus` plain Set with computed value - (editor) Add `addOpenMenu` method - (editor) Add `removeOpenMenu` method - (editor) Add `setFocusMode` method - (editor) Add `setToolLocked` method - (editor) Add `setSnapMode` method - (editor) Add `isSnapMode` method - (editor) Update `setGridMode` method return type to editor app - (editor) Update `setReadOnly` method return type to editor app - (editor) Update `setPenMode` method return type to editor app - (editor) Update `selectNone` method return type to editor app - (editor) Rename `backToContent` to `zoomToContent` - (editor) Remove `TLReorderOperation` type --------- Co-authored-by: Orange Mug <orangemug@users.noreply.github.com>
2023-05-11 22:14:58 +00:00
2023-04-25 11:01:25 +00:00
URL.revokeObjectURL(dataURL)
return
}
default:
throw new Error(`Export type ${format} not supported.`)
}
},
[app, addToast, msg]
)
}