Add component for viewing an image of a snapshot (#2804)
This PR adds the `TldrawImage` component that displays a tldraw snapshot
as an SVG image.
![2024-02-15 at 12 29 52 - Coral
Cod](https://github.com/tldraw/tldraw/assets/15892272/14140e9e-7d6d-4dd3-88a3-86a6786325c5)
## Why
We've seen requests for this kind of thing from users. eg: GitBook, and
on discord:
<img width="710" alt="image"
src="https://github.com/tldraw/tldraw/assets/15892272/3d3a3e9d-66b9-42e7-81de-a70aa7165bdc">
The component provides a way to do that.
This PR also untangles various bits of editor state from image
exporting, which makes it easier for library users to export images more
agnostically. (ie: they can now export any shapes on any page in any
theme. previously, they had to change the user's state to do that).
## What else
- This PR also adds an **Image snapshot** example to demonstrate the new
component.
- We now pass an `isDarkMode` property to the `toSvg` method (inside the
`ctx` argument). This means that `toSvg` doesn't have to rely on editor
state anymore. I updated all our `toSvg` methods to use it.
- See code comments for more info.
## Any issues?
When you toggle to editing mode in the new example, text measurements
are initially wrong (until you edit the size of a text shape). Click on
the text shape to see how its indicator is wrong. Not sure why this is,
or if it's even related. Does it ring a bell with anyone? If not, I'll
take a closer look. (fixed, see comments --steve)
## Future work
Now that we've untangled image exporting from editor state, we could
expose some more helpful helpers for making this easier.
Fixes tld-2122
### Change Type
- [x] `minor` — New feature
[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version
### Test Plan
1. Open the **Image snapshot** example.
2. Try editing the image, saving the image, and making sure the image
updates.
- [ ] Unit Tests
- [ ] End to end tests
### Release Notes
- Dev: Added the `TldrawImage` component.
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-02-16 13:54:48 +00:00
|
|
|
import { useState } from 'react'
|
2024-04-15 12:53:42 +00:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Editor,
|
|
|
|
StoreSnapshot,
|
|
|
|
TLPageId,
|
|
|
|
TLRecord,
|
|
|
|
TLStoreSnapshot,
|
|
|
|
Tldraw,
|
|
|
|
TldrawImage,
|
|
|
|
} from 'tldraw'
|
2024-02-29 16:06:19 +00:00
|
|
|
import 'tldraw/tldraw.css'
|
Add component for viewing an image of a snapshot (#2804)
This PR adds the `TldrawImage` component that displays a tldraw snapshot
as an SVG image.
![2024-02-15 at 12 29 52 - Coral
Cod](https://github.com/tldraw/tldraw/assets/15892272/14140e9e-7d6d-4dd3-88a3-86a6786325c5)
## Why
We've seen requests for this kind of thing from users. eg: GitBook, and
on discord:
<img width="710" alt="image"
src="https://github.com/tldraw/tldraw/assets/15892272/3d3a3e9d-66b9-42e7-81de-a70aa7165bdc">
The component provides a way to do that.
This PR also untangles various bits of editor state from image
exporting, which makes it easier for library users to export images more
agnostically. (ie: they can now export any shapes on any page in any
theme. previously, they had to change the user's state to do that).
## What else
- This PR also adds an **Image snapshot** example to demonstrate the new
component.
- We now pass an `isDarkMode` property to the `toSvg` method (inside the
`ctx` argument). This means that `toSvg` doesn't have to rely on editor
state anymore. I updated all our `toSvg` methods to use it.
- See code comments for more info.
## Any issues?
When you toggle to editing mode in the new example, text measurements
are initially wrong (until you edit the size of a text shape). Click on
the text shape to see how its indicator is wrong. Not sure why this is,
or if it's even related. Does it ring a bell with anyone? If not, I'll
take a closer look. (fixed, see comments --steve)
## Future work
Now that we've untangled image exporting from editor state, we could
expose some more helpful helpers for making this easier.
Fixes tld-2122
### Change Type
- [x] `minor` — New feature
[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version
### Test Plan
1. Open the **Image snapshot** example.
2. Try editing the image, saving the image, and making sure the image
updates.
- [ ] Unit Tests
- [ ] End to end tests
### Release Notes
- Dev: Added the `TldrawImage` component.
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-02-16 13:54:48 +00:00
|
|
|
import initialSnapshot from './snapshot.json'
|
|
|
|
|
|
|
|
// There's a guide at the bottom of this file!
|
|
|
|
|
|
|
|
export default function TldrawImageExample() {
|
|
|
|
const [editor, setEditor] = useState<Editor>()
|
2024-04-15 12:53:42 +00:00
|
|
|
const [snapshot, setSnapshot] = useState<StoreSnapshot<TLRecord>>(
|
|
|
|
initialSnapshot as TLStoreSnapshot
|
|
|
|
)
|
Add component for viewing an image of a snapshot (#2804)
This PR adds the `TldrawImage` component that displays a tldraw snapshot
as an SVG image.
![2024-02-15 at 12 29 52 - Coral
Cod](https://github.com/tldraw/tldraw/assets/15892272/14140e9e-7d6d-4dd3-88a3-86a6786325c5)
## Why
We've seen requests for this kind of thing from users. eg: GitBook, and
on discord:
<img width="710" alt="image"
src="https://github.com/tldraw/tldraw/assets/15892272/3d3a3e9d-66b9-42e7-81de-a70aa7165bdc">
The component provides a way to do that.
This PR also untangles various bits of editor state from image
exporting, which makes it easier for library users to export images more
agnostically. (ie: they can now export any shapes on any page in any
theme. previously, they had to change the user's state to do that).
## What else
- This PR also adds an **Image snapshot** example to demonstrate the new
component.
- We now pass an `isDarkMode` property to the `toSvg` method (inside the
`ctx` argument). This means that `toSvg` doesn't have to rely on editor
state anymore. I updated all our `toSvg` methods to use it.
- See code comments for more info.
## Any issues?
When you toggle to editing mode in the new example, text measurements
are initially wrong (until you edit the size of a text shape). Click on
the text shape to see how its indicator is wrong. Not sure why this is,
or if it's even related. Does it ring a bell with anyone? If not, I'll
take a closer look. (fixed, see comments --steve)
## Future work
Now that we've untangled image exporting from editor state, we could
expose some more helpful helpers for making this easier.
Fixes tld-2122
### Change Type
- [x] `minor` — New feature
[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version
### Test Plan
1. Open the **Image snapshot** example.
2. Try editing the image, saving the image, and making sure the image
updates.
- [ ] Unit Tests
- [ ] End to end tests
### Release Notes
- Dev: Added the `TldrawImage` component.
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-02-16 13:54:48 +00:00
|
|
|
const [currentPageId, setCurrentPageId] = useState<TLPageId | undefined>()
|
|
|
|
const [showBackground, setShowBackground] = useState(true)
|
|
|
|
const [isDarkMode, setIsDarkMode] = useState(false)
|
|
|
|
const [viewportPageBounds, setViewportPageBounds] = useState(new Box(0, 0, 600, 400))
|
|
|
|
const [isEditing, setIsEditing] = useState(false)
|
|
|
|
const [format, setFormat] = useState<'svg' | 'png'>('svg')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={{ padding: 30 }}>
|
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
style={{ cursor: 'pointer', marginRight: 8 }}
|
|
|
|
onClick={() => {
|
|
|
|
if (isEditing) {
|
|
|
|
if (!editor) return
|
|
|
|
setIsDarkMode(editor.user.getIsDarkMode())
|
|
|
|
setShowBackground(editor.getInstanceState().exportBackground)
|
|
|
|
setViewportPageBounds(editor.getViewportPageBounds())
|
|
|
|
setCurrentPageId(editor.getCurrentPageId())
|
|
|
|
setSnapshot(editor.store.getSnapshot())
|
|
|
|
setIsEditing(false)
|
|
|
|
} else {
|
|
|
|
setIsEditing(true)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isEditing ? '✓ Save drawing' : '✎ Edit drawing'}
|
|
|
|
</button>
|
|
|
|
{!isEditing && (
|
|
|
|
<>
|
|
|
|
<label htmlFor="format" style={{ marginRight: 8 }}>
|
|
|
|
Format
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
name="format"
|
|
|
|
value={format}
|
|
|
|
onChange={(e) => {
|
|
|
|
setFormat(e.currentTarget.value as 'svg' | 'png')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<option value="svg">SVG</option>
|
|
|
|
<option value="png">PNG</option>
|
|
|
|
</select>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div style={{ width: 600, height: 400, marginTop: 15 }}>
|
|
|
|
{isEditing ? (
|
|
|
|
<Tldraw
|
|
|
|
snapshot={snapshot}
|
|
|
|
onMount={(editor: Editor) => {
|
|
|
|
setEditor(editor)
|
|
|
|
editor.updateInstanceState({ isDebugMode: false })
|
|
|
|
editor.user.updateUserPreferences({ isDarkMode })
|
|
|
|
if (currentPageId) {
|
|
|
|
editor.setCurrentPage(currentPageId)
|
|
|
|
}
|
|
|
|
if (viewportPageBounds) {
|
|
|
|
editor.zoomToBounds(viewportPageBounds, { inset: 0 })
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<TldrawImage
|
|
|
|
//[1]
|
|
|
|
snapshot={snapshot}
|
|
|
|
// [2]
|
|
|
|
pageId={currentPageId}
|
|
|
|
// [3]
|
|
|
|
background={showBackground}
|
|
|
|
darkMode={isDarkMode}
|
|
|
|
bounds={viewportPageBounds}
|
|
|
|
padding={0}
|
|
|
|
scale={1}
|
|
|
|
format={format}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This example shows how to use the `TldrawImage` component to display a snapshot
|
|
|
|
as an image. The example also allows you to toggle between editing the snapshot
|
|
|
|
and viewing it.
|
|
|
|
|
|
|
|
[1] Pass your snapshot to the `snapshot` prop of the `TldrawImage` component.
|
|
|
|
|
|
|
|
[2] You can specify which page to display by using the `pageId` prop. By
|
|
|
|
default, the first page is shown.
|
2024-02-23 16:07:08 +00:00
|
|
|
|
Add component for viewing an image of a snapshot (#2804)
This PR adds the `TldrawImage` component that displays a tldraw snapshot
as an SVG image.
![2024-02-15 at 12 29 52 - Coral
Cod](https://github.com/tldraw/tldraw/assets/15892272/14140e9e-7d6d-4dd3-88a3-86a6786325c5)
## Why
We've seen requests for this kind of thing from users. eg: GitBook, and
on discord:
<img width="710" alt="image"
src="https://github.com/tldraw/tldraw/assets/15892272/3d3a3e9d-66b9-42e7-81de-a70aa7165bdc">
The component provides a way to do that.
This PR also untangles various bits of editor state from image
exporting, which makes it easier for library users to export images more
agnostically. (ie: they can now export any shapes on any page in any
theme. previously, they had to change the user's state to do that).
## What else
- This PR also adds an **Image snapshot** example to demonstrate the new
component.
- We now pass an `isDarkMode` property to the `toSvg` method (inside the
`ctx` argument). This means that `toSvg` doesn't have to rely on editor
state anymore. I updated all our `toSvg` methods to use it.
- See code comments for more info.
## Any issues?
When you toggle to editing mode in the new example, text measurements
are initially wrong (until you edit the size of a text shape). Click on
the text shape to see how its indicator is wrong. Not sure why this is,
or if it's even related. Does it ring a bell with anyone? If not, I'll
take a closer look. (fixed, see comments --steve)
## Future work
Now that we've untangled image exporting from editor state, we could
expose some more helpful helpers for making this easier.
Fixes tld-2122
### Change Type
- [x] `minor` — New feature
[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version
### Test Plan
1. Open the **Image snapshot** example.
2. Try editing the image, saving the image, and making sure the image
updates.
- [ ] Unit Tests
- [ ] End to end tests
### Release Notes
- Dev: Added the `TldrawImage` component.
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-02-16 13:54:48 +00:00
|
|
|
[3] You can customize the appearance of the image by passing other props to the
|
2024-02-23 16:07:08 +00:00
|
|
|
`TldrawImage` component. For example, you can toggle the background, set
|
|
|
|
the dark mode, and specify the viewport bounds.
|
Add component for viewing an image of a snapshot (#2804)
This PR adds the `TldrawImage` component that displays a tldraw snapshot
as an SVG image.
![2024-02-15 at 12 29 52 - Coral
Cod](https://github.com/tldraw/tldraw/assets/15892272/14140e9e-7d6d-4dd3-88a3-86a6786325c5)
## Why
We've seen requests for this kind of thing from users. eg: GitBook, and
on discord:
<img width="710" alt="image"
src="https://github.com/tldraw/tldraw/assets/15892272/3d3a3e9d-66b9-42e7-81de-a70aa7165bdc">
The component provides a way to do that.
This PR also untangles various bits of editor state from image
exporting, which makes it easier for library users to export images more
agnostically. (ie: they can now export any shapes on any page in any
theme. previously, they had to change the user's state to do that).
## What else
- This PR also adds an **Image snapshot** example to demonstrate the new
component.
- We now pass an `isDarkMode` property to the `toSvg` method (inside the
`ctx` argument). This means that `toSvg` doesn't have to rely on editor
state anymore. I updated all our `toSvg` methods to use it.
- See code comments for more info.
## Any issues?
When you toggle to editing mode in the new example, text measurements
are initially wrong (until you edit the size of a text shape). Click on
the text shape to see how its indicator is wrong. Not sure why this is,
or if it's even related. Does it ring a bell with anyone? If not, I'll
take a closer look. (fixed, see comments --steve)
## Future work
Now that we've untangled image exporting from editor state, we could
expose some more helpful helpers for making this easier.
Fixes tld-2122
### Change Type
- [x] `minor` — New feature
[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version
### Test Plan
1. Open the **Image snapshot** example.
2. Try editing the image, saving the image, and making sure the image
updates.
- [ ] Unit Tests
- [ ] End to end tests
### Release Notes
- Dev: Added the `TldrawImage` component.
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-02-16 13:54:48 +00:00
|
|
|
*/
|