2023-06-12 14:04:14 +00:00
|
|
|
import {
|
|
|
|
Canvas,
|
|
|
|
Editor,
|
|
|
|
ErrorBoundary,
|
|
|
|
TldrawEditor,
|
|
|
|
defaultShapes,
|
|
|
|
defaultTools,
|
|
|
|
setRuntimeOverrides,
|
|
|
|
} from '@tldraw/editor'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { linksUiOverrides } from './utils/links'
|
|
|
|
// eslint-disable-next-line import/no-internal-modules
|
|
|
|
import '@tldraw/editor/editor.css'
|
2023-06-02 21:16:09 +00:00
|
|
|
import { ContextMenu, TLUiMenuSchema, TldrawUi } from '@tldraw/ui'
|
2023-04-25 11:01:25 +00:00
|
|
|
// eslint-disable-next-line import/no-internal-modules
|
|
|
|
import '@tldraw/ui/ui.css'
|
[refactor] User-facing APIs (#1478)
This PR updates our user-facing APIs for the Tldraw and TldrawEditor
components, as well as the Editor (App). It mainly incorporates surface
changes from #1450 without any changes to validators or migrators,
incorporating feedback / discussion with @SomeHats and @ds300.
Here we:
- remove the TldrawEditorConfig
- bring back a loose version of shape definitions
- make a separation between "core" shapes and "default" shapes
- do not allow custom shapes, migrators or validators to overwrite core
shapes
- but _do_ allow new shapes
## `<Tldraw>` component
In this PR, the `Tldraw` component wraps both the `TldrawEditor`
component and our `TldrawUi` component. It accepts a union of props for
both components. Previously, this component also added local syncing via
a `useLocalSyncClient` hook call, however that has been pushed down to
the `TldrawEditor` component.
## `<TldrawEditor>` component
The `TldrawEditor` component now more neatly wraps up the different ways
that the editor can be configured.
## The store prop (`TldrawEditorProps.store`)
There are three main ways for the `TldrawEditor` component to be run:
1. with an externally defined store
2. with an externally defined syncing store (local or remote)
3. with an internally defined store
4. with an internally defined locally syncing store
The `store` prop allows for these configurations.
If the `store` prop is defined, it may be defined either as a `TLStore`
or as a `SyncedStore`. If the store is a `TLStore`, then the Editor will
assume that the store is ready to go; if it is defined as a SyncedStore,
then the component will display the loading / error screens as needed,
or the final editor once the store's status is "synced".
When the store is left undefined, then the `TldrawEditor` will create
its own internal store using the optional `instanceId`, `initialData`,
or `shapes` props to define the store / store schema.
If the `persistenceKey` prop is left undefined, then the store will not
be synced. If the `persistenceKey` is defined, then the store will be
synced locally. In the future, we may also here accept the API key /
roomId / etc for creating a remotely synced store.
The `SyncedStore` type has been expanded to also include types used for
remote syncing, e.g. with `ConnectionStatus`.
## Tools
By default, the App has two "baked-in" tools: the select tool and the
zoom tool. These cannot (for now) be replaced or removed. The default
tools are used by default, but may be replaced by other tools if
provided.
## Shapes
By default, the App has a set of "core" shapes:
- group
- embed
- bookmark
- image
- video
- text
That cannot by overwritten because they're created by the app at
different moments, such as when double clicking on the canvas or via a
copy and paste event. In follow up PRs, we'll split these out so that
users can replace parts of the code where these shapes are created.
### Change Type
- [x] `major` — Breaking Change
### Test Plan
- [x] Unit Tests
2023-06-01 15:47:34 +00:00
|
|
|
// eslint-disable-next-line import/no-internal-modules
|
|
|
|
import { getAssetUrlsByImport } from '@tldraw/assets/imports'
|
`ExternalContentManager` for handling external content (files, images, etc) (#1550)
This PR improves the editor's APIs around creating assets and files.
This allows end user developers to replace behavior that might occur,
for example, when pasting images or dragging files onto the canvas.
Here, we:
- remove `onCreateAssetFromFile` prop
- remove `onCreateBookmarkFromUrl` prop
- introduce `onEditorReady` prop
- introduce `onEditorWillDispose` prop
- introduce `ExternalContentManager`
The `ExternalContentManager` (ECM) is used in circumstances where we're
turning external content (text, images, urls, etc) into assets or
shapes. It is designed to allow certain methods to be overwritten by
other developers as a kind of weakly supported hack.
For example, when a user drags an image onto the canvas, the event
handler passes a `TLExternalContent` object to the editor's
`putExternalContent` method. This method runs the ECM's handler for this
content type. That handler may in turn run other methods, such as
`createAssetFromFile` or `createShapesForAssets`, which will lead to the
image being created on the canvas.
If a developer wanted to change the way that assets are created from
files, then they could overwrite that method at runtime.
```ts
const handleEditorReady = (editor: Editor) => {
editor.externalContentManager.createAssetFromFile = myHandler
}
function Example() {
return <Tldraw onEditorReady={handleEditorReady}/>
}
```
If you wanted to go even deeper, you could override the editor's
`putExternalContent` method.
```ts
const handleEditorReady = (editor: Editor) => {
const handleExternalContent = (info: TLExternalContent): Promise<void> => {
if (info.type === 'files') {
// do something here
} else {
// do the normal thing
editor.externalContentManager.handleContent(info)
}
}
```
### Change Type
- [x] `major`
### Test Plan
1. Drag images, urls, etc. onto the canvas
2. Use copy and paste for single and multiple files
3. Use bookmark / embed shapes and convert between eachother
### Release Notes
- [editor] add `ExternalContentManager` for plopping content onto the
canvas
- [editor] remove `onCreateAssetFromFile` prop
- [editor] remove `onCreateBookmarkFromUrl` prop
- [editor] introduce `onEditorReady` prop
- [editor] introduce `onEditorWillDispose` prop
- [editor] introduce `ExternalContentManager`
2023-06-08 14:53:11 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { VscodeMessage } from '../../messages'
|
|
|
|
import '../public/index.css'
|
|
|
|
import { ChangeResponder } from './ChangeResponder'
|
|
|
|
import { FileOpen } from './FileOpen'
|
|
|
|
import { FullPageMessage } from './FullPageMessage'
|
`ExternalContentManager` for handling external content (files, images, etc) (#1550)
This PR improves the editor's APIs around creating assets and files.
This allows end user developers to replace behavior that might occur,
for example, when pasting images or dragging files onto the canvas.
Here, we:
- remove `onCreateAssetFromFile` prop
- remove `onCreateBookmarkFromUrl` prop
- introduce `onEditorReady` prop
- introduce `onEditorWillDispose` prop
- introduce `ExternalContentManager`
The `ExternalContentManager` (ECM) is used in circumstances where we're
turning external content (text, images, urls, etc) into assets or
shapes. It is designed to allow certain methods to be overwritten by
other developers as a kind of weakly supported hack.
For example, when a user drags an image onto the canvas, the event
handler passes a `TLExternalContent` object to the editor's
`putExternalContent` method. This method runs the ECM's handler for this
content type. That handler may in turn run other methods, such as
`createAssetFromFile` or `createShapesForAssets`, which will lead to the
image being created on the canvas.
If a developer wanted to change the way that assets are created from
files, then they could overwrite that method at runtime.
```ts
const handleEditorReady = (editor: Editor) => {
editor.externalContentManager.createAssetFromFile = myHandler
}
function Example() {
return <Tldraw onEditorReady={handleEditorReady}/>
}
```
If you wanted to go even deeper, you could override the editor's
`putExternalContent` method.
```ts
const handleEditorReady = (editor: Editor) => {
const handleExternalContent = (info: TLExternalContent): Promise<void> => {
if (info.type === 'files') {
// do something here
} else {
// do the normal thing
editor.externalContentManager.handleContent(info)
}
}
```
### Change Type
- [x] `major`
### Test Plan
1. Drag images, urls, etc. onto the canvas
2. Use copy and paste for single and multiple files
3. Use bookmark / embed shapes and convert between eachother
### Release Notes
- [editor] add `ExternalContentManager` for plopping content onto the
canvas
- [editor] remove `onCreateAssetFromFile` prop
- [editor] remove `onCreateBookmarkFromUrl` prop
- [editor] introduce `onEditorReady` prop
- [editor] introduce `onEditorWillDispose` prop
- [editor] introduce `ExternalContentManager`
2023-06-08 14:53:11 +00:00
|
|
|
import { onCreateAssetFromUrl } from './utils/bookmarks'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { vscode } from './utils/vscode'
|
|
|
|
|
|
|
|
setRuntimeOverrides({
|
|
|
|
openWindow: (url, target) => {
|
|
|
|
vscode.postMessage({
|
|
|
|
type: 'vscode:open-window',
|
|
|
|
data: {
|
|
|
|
url,
|
|
|
|
target,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
refreshPage: () => {
|
|
|
|
vscode.postMessage({
|
|
|
|
type: 'vscode:refresh-page',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
hardReset: async () => {
|
|
|
|
await (window as any).__tldraw__hardReset?.()
|
|
|
|
vscode.postMessage({
|
|
|
|
type: 'vscode:hard-reset',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const handleError = (error: any) => {
|
|
|
|
console.error(error.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function WrappedTldrawEditor() {
|
|
|
|
return (
|
|
|
|
<div className="tldraw--editor">
|
|
|
|
<ErrorBoundary
|
|
|
|
fallback={() => <FullPageMessage>Fallback</FullPageMessage>}
|
|
|
|
onError={handleError}
|
|
|
|
>
|
|
|
|
<TldrawWrapper />
|
|
|
|
</ErrorBoundary>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const menuOverrides = {
|
2023-06-02 21:16:09 +00:00
|
|
|
menu: (_editor: Editor, schema: TLUiMenuSchema, _helpers: any) => {
|
2023-04-25 11:01:25 +00:00
|
|
|
schema.forEach((item) => {
|
|
|
|
if (item.id === 'menu' && item.type === 'group') {
|
|
|
|
item.children = item.children.filter((menuItem) => {
|
|
|
|
if (menuItem.id === 'file' && menuItem.type === 'submenu') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return schema
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TldrawWrapper = () => {
|
|
|
|
const [tldrawInnerProps, setTldrawInnerProps] = useState<TLDrawInnerProps | null>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function handleMessage({ data: message }: MessageEvent<VscodeMessage>) {
|
|
|
|
switch (message.type) {
|
|
|
|
case 'vscode:opened-file': {
|
|
|
|
setTldrawInnerProps({
|
|
|
|
assetSrc: message.data.assetSrc,
|
|
|
|
fileContents: message.data.fileContents,
|
|
|
|
uri: message.data.uri,
|
|
|
|
isDarkMode: message.data.isDarkMode,
|
|
|
|
})
|
|
|
|
// We only want to listen for this message once
|
|
|
|
window.removeEventListener('message', handleMessage)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', handleMessage)
|
|
|
|
|
|
|
|
vscode.postMessage({ type: 'vscode:ready-to-receive-file' })
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('message', handleMessage)
|
|
|
|
}
|
|
|
|
}, [setTldrawInnerProps])
|
|
|
|
|
|
|
|
return tldrawInnerProps === null ? (
|
|
|
|
<FullPageMessage>Loading</FullPageMessage>
|
|
|
|
) : (
|
|
|
|
<TldrawInner {...tldrawInnerProps} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TLDrawInnerProps = {
|
|
|
|
assetSrc: string
|
|
|
|
fileContents: string
|
|
|
|
uri: string
|
|
|
|
isDarkMode: boolean
|
|
|
|
}
|
|
|
|
|
[refactor] User-facing APIs (#1478)
This PR updates our user-facing APIs for the Tldraw and TldrawEditor
components, as well as the Editor (App). It mainly incorporates surface
changes from #1450 without any changes to validators or migrators,
incorporating feedback / discussion with @SomeHats and @ds300.
Here we:
- remove the TldrawEditorConfig
- bring back a loose version of shape definitions
- make a separation between "core" shapes and "default" shapes
- do not allow custom shapes, migrators or validators to overwrite core
shapes
- but _do_ allow new shapes
## `<Tldraw>` component
In this PR, the `Tldraw` component wraps both the `TldrawEditor`
component and our `TldrawUi` component. It accepts a union of props for
both components. Previously, this component also added local syncing via
a `useLocalSyncClient` hook call, however that has been pushed down to
the `TldrawEditor` component.
## `<TldrawEditor>` component
The `TldrawEditor` component now more neatly wraps up the different ways
that the editor can be configured.
## The store prop (`TldrawEditorProps.store`)
There are three main ways for the `TldrawEditor` component to be run:
1. with an externally defined store
2. with an externally defined syncing store (local or remote)
3. with an internally defined store
4. with an internally defined locally syncing store
The `store` prop allows for these configurations.
If the `store` prop is defined, it may be defined either as a `TLStore`
or as a `SyncedStore`. If the store is a `TLStore`, then the Editor will
assume that the store is ready to go; if it is defined as a SyncedStore,
then the component will display the loading / error screens as needed,
or the final editor once the store's status is "synced".
When the store is left undefined, then the `TldrawEditor` will create
its own internal store using the optional `instanceId`, `initialData`,
or `shapes` props to define the store / store schema.
If the `persistenceKey` prop is left undefined, then the store will not
be synced. If the `persistenceKey` is defined, then the store will be
synced locally. In the future, we may also here accept the API key /
roomId / etc for creating a remotely synced store.
The `SyncedStore` type has been expanded to also include types used for
remote syncing, e.g. with `ConnectionStatus`.
## Tools
By default, the App has two "baked-in" tools: the select tool and the
zoom tool. These cannot (for now) be replaced or removed. The default
tools are used by default, but may be replaced by other tools if
provided.
## Shapes
By default, the App has a set of "core" shapes:
- group
- embed
- bookmark
- image
- video
- text
That cannot by overwritten because they're created by the app at
different moments, such as when double clicking on the canvas or via a
copy and paste event. In follow up PRs, we'll split these out so that
users can replace parts of the code where these shapes are created.
### Change Type
- [x] `major` — Breaking Change
### Test Plan
- [x] Unit Tests
2023-06-01 15:47:34 +00:00
|
|
|
function TldrawInner({ uri, assetSrc, isDarkMode, fileContents }: TLDrawInnerProps) {
|
2023-05-09 16:08:38 +00:00
|
|
|
const assetUrls = useMemo(() => getAssetUrlsByImport({ baseUrl: assetSrc }), [assetSrc])
|
2023-04-25 11:01:25 +00:00
|
|
|
|
`ExternalContentManager` for handling external content (files, images, etc) (#1550)
This PR improves the editor's APIs around creating assets and files.
This allows end user developers to replace behavior that might occur,
for example, when pasting images or dragging files onto the canvas.
Here, we:
- remove `onCreateAssetFromFile` prop
- remove `onCreateBookmarkFromUrl` prop
- introduce `onEditorReady` prop
- introduce `onEditorWillDispose` prop
- introduce `ExternalContentManager`
The `ExternalContentManager` (ECM) is used in circumstances where we're
turning external content (text, images, urls, etc) into assets or
shapes. It is designed to allow certain methods to be overwritten by
other developers as a kind of weakly supported hack.
For example, when a user drags an image onto the canvas, the event
handler passes a `TLExternalContent` object to the editor's
`putExternalContent` method. This method runs the ECM's handler for this
content type. That handler may in turn run other methods, such as
`createAssetFromFile` or `createShapesForAssets`, which will lead to the
image being created on the canvas.
If a developer wanted to change the way that assets are created from
files, then they could overwrite that method at runtime.
```ts
const handleEditorReady = (editor: Editor) => {
editor.externalContentManager.createAssetFromFile = myHandler
}
function Example() {
return <Tldraw onEditorReady={handleEditorReady}/>
}
```
If you wanted to go even deeper, you could override the editor's
`putExternalContent` method.
```ts
const handleEditorReady = (editor: Editor) => {
const handleExternalContent = (info: TLExternalContent): Promise<void> => {
if (info.type === 'files') {
// do something here
} else {
// do the normal thing
editor.externalContentManager.handleContent(info)
}
}
```
### Change Type
- [x] `major`
### Test Plan
1. Drag images, urls, etc. onto the canvas
2. Use copy and paste for single and multiple files
3. Use bookmark / embed shapes and convert between eachother
### Release Notes
- [editor] add `ExternalContentManager` for plopping content onto the
canvas
- [editor] remove `onCreateAssetFromFile` prop
- [editor] remove `onCreateBookmarkFromUrl` prop
- [editor] introduce `onEditorReady` prop
- [editor] introduce `onEditorWillDispose` prop
- [editor] introduce `ExternalContentManager`
2023-06-08 14:53:11 +00:00
|
|
|
const handleMount = useCallback((editor: Editor) => {
|
|
|
|
editor.externalContentManager.createAssetFromUrl = onCreateAssetFromUrl
|
|
|
|
}, [])
|
|
|
|
|
2023-04-25 11:01:25 +00:00
|
|
|
return (
|
2023-06-12 14:04:14 +00:00
|
|
|
<TldrawEditor
|
|
|
|
shapes={defaultShapes}
|
|
|
|
tools={defaultTools}
|
|
|
|
assetUrls={assetUrls}
|
|
|
|
persistenceKey={uri}
|
|
|
|
onMount={handleMount}
|
|
|
|
autoFocus
|
|
|
|
>
|
2023-04-25 11:01:25 +00:00
|
|
|
{/* <DarkModeHandler themeKind={themeKind} /> */}
|
|
|
|
<TldrawUi assetUrls={assetUrls} overrides={[menuOverrides, linksUiOverrides]}>
|
[refactor] User-facing APIs (#1478)
This PR updates our user-facing APIs for the Tldraw and TldrawEditor
components, as well as the Editor (App). It mainly incorporates surface
changes from #1450 without any changes to validators or migrators,
incorporating feedback / discussion with @SomeHats and @ds300.
Here we:
- remove the TldrawEditorConfig
- bring back a loose version of shape definitions
- make a separation between "core" shapes and "default" shapes
- do not allow custom shapes, migrators or validators to overwrite core
shapes
- but _do_ allow new shapes
## `<Tldraw>` component
In this PR, the `Tldraw` component wraps both the `TldrawEditor`
component and our `TldrawUi` component. It accepts a union of props for
both components. Previously, this component also added local syncing via
a `useLocalSyncClient` hook call, however that has been pushed down to
the `TldrawEditor` component.
## `<TldrawEditor>` component
The `TldrawEditor` component now more neatly wraps up the different ways
that the editor can be configured.
## The store prop (`TldrawEditorProps.store`)
There are three main ways for the `TldrawEditor` component to be run:
1. with an externally defined store
2. with an externally defined syncing store (local or remote)
3. with an internally defined store
4. with an internally defined locally syncing store
The `store` prop allows for these configurations.
If the `store` prop is defined, it may be defined either as a `TLStore`
or as a `SyncedStore`. If the store is a `TLStore`, then the Editor will
assume that the store is ready to go; if it is defined as a SyncedStore,
then the component will display the loading / error screens as needed,
or the final editor once the store's status is "synced".
When the store is left undefined, then the `TldrawEditor` will create
its own internal store using the optional `instanceId`, `initialData`,
or `shapes` props to define the store / store schema.
If the `persistenceKey` prop is left undefined, then the store will not
be synced. If the `persistenceKey` is defined, then the store will be
synced locally. In the future, we may also here accept the API key /
roomId / etc for creating a remotely synced store.
The `SyncedStore` type has been expanded to also include types used for
remote syncing, e.g. with `ConnectionStatus`.
## Tools
By default, the App has two "baked-in" tools: the select tool and the
zoom tool. These cannot (for now) be replaced or removed. The default
tools are used by default, but may be replaced by other tools if
provided.
## Shapes
By default, the App has a set of "core" shapes:
- group
- embed
- bookmark
- image
- video
- text
That cannot by overwritten because they're created by the app at
different moments, such as when double clicking on the canvas or via a
copy and paste event. In follow up PRs, we'll split these out so that
users can replace parts of the code where these shapes are created.
### Change Type
- [x] `major` — Breaking Change
### Test Plan
- [x] Unit Tests
2023-06-01 15:47:34 +00:00
|
|
|
<FileOpen fileContents={fileContents} forceDarkMode={isDarkMode} />
|
|
|
|
<ChangeResponder />
|
2023-04-25 11:01:25 +00:00
|
|
|
<ContextMenu>
|
|
|
|
<Canvas />
|
|
|
|
</ContextMenu>
|
|
|
|
</TldrawUi>
|
|
|
|
</TldrawEditor>
|
|
|
|
)
|
|
|
|
}
|