-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): Add basic canvas plugin with tldraw
- Loading branch information
Mikey Stengel
committed
Dec 23, 2024
1 parent
9c6c86f
commit 5699637
Showing
66 changed files
with
962 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+21.3 KB
.yarn/cache/@radix-ui-react-alert-dialog-npm-1.1.4-70ec3a59a1-f510c784ee.zip
Binary file not shown.
Binary file added
BIN
+5.82 KB
.yarn/cache/@radix-ui-react-arrow-npm-1.1.1-cd53803a53-c75505c285.zip
Binary file not shown.
Binary file added
BIN
+10 KB
.yarn/cache/@radix-ui-react-collection-npm-1.1.1-cc547f8ac0-413fbcb542.zip
Binary file not shown.
Binary file added
BIN
+6.57 KB
.yarn/cache/@radix-ui-react-compose-refs-npm-1.1.1-2480de3ef9-1be82f9f7f.zip
Binary file not shown.
Binary file added
BIN
+21.4 KB
.yarn/cache/@radix-ui-react-context-menu-npm-2.2.4-e7c68c8ff7-3a10009028.zip
Binary file not shown.
Binary file added
BIN
+24.4 KB
.yarn/cache/@radix-ui-react-dialog-npm-1.1.4-c1cc039c0d-695b35c728.zip
Binary file not shown.
Binary file added
BIN
+19.1 KB
.yarn/cache/@radix-ui-react-dismissable-layer-npm-1.1.3-2114a37d20-26d15726bd.zip
Binary file not shown.
Binary file added
BIN
+20.4 KB
.yarn/cache/@radix-ui-react-dropdown-menu-npm-2.1.4-60ee1efa16-51014b38da.zip
Binary file not shown.
Binary file added
BIN
+20.1 KB
.yarn/cache/@radix-ui-react-focus-scope-npm-1.1.1-eaf894ac65-8716fe9b02.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+22 KB
.yarn/cache/@radix-ui-react-popover-npm-1.1.4-81a9688a19-f4525ac9a2.zip
Binary file not shown.
Binary file added
BIN
+23.4 KB
.yarn/cache/@radix-ui-react-popper-npm-1.2.1-298636bfa3-1416acda53.zip
Binary file not shown.
Binary file added
BIN
+6.29 KB
.yarn/cache/@radix-ui-react-portal-npm-1.1.3-4dd747e0ee-470fb50c94.zip
Binary file not shown.
Binary file added
BIN
+13.6 KB
.yarn/cache/@radix-ui-react-presence-npm-1.1.2-9951e170a0-0345bc8d3e.zip
Binary file not shown.
Binary file added
BIN
+10.3 KB
.yarn/cache/@radix-ui-react-primitive-npm-2.0.1-a63c88e534-d758822091.zip
Binary file not shown.
Binary file added
BIN
+19.8 KB
.yarn/cache/@radix-ui-react-roving-focus-npm-1.1.1-0626400ec8-f3a78bd15c.zip
Binary file not shown.
Binary file added
BIN
+32.8 KB
.yarn/cache/@radix-ui-react-slider-npm-1.2.2-21d834588e-43acd22d5b.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+44.3 KB
.yarn/cache/@radix-ui-react-toast-npm-1.2.4-84a2f610bb-6479af12ec.zip
Binary file not shown.
Binary file added
BIN
+6.21 KB
.yarn/cache/@radix-ui-react-visually-hidden-npm-1.1.1-d1e551a0fa-ccbdf29811.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+19 KB
.yarn/cache/fractional-indexing-jittered-npm-0.9.1-ff10af0205-87425a09bc.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+16 KB
.yarn/cache/react-remove-scroll-bar-npm-2.3.8-21a578f734-c4663247f6.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+14.8 KB
.yarn/cache/react-style-singleton-npm-2.2.3-18f32c05f7-a7b0bf493c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useRef } from 'react' | ||
import { Tldraw, TLEditorSnapshot } from 'tldraw' | ||
|
||
// eslint-disable-next-line import/no-unassigned-import | ||
import 'tldraw/tldraw.css' | ||
import { type CanvasProps } from '.' | ||
import { CanvasToolbar } from './toolbar' | ||
|
||
export function CanvasEditor({ state, ...props }: CanvasProps) { | ||
const { focused } = props | ||
const initialSnapshot = useRef<TLEditorSnapshot | undefined>( | ||
state.document.value | ||
? (JSON.parse(state.document.value) as TLEditorSnapshot) | ||
: undefined | ||
) | ||
return ( | ||
<div className="serlo-canvas-editor relative h-[600px] w-full rounded border"> | ||
{focused && <CanvasToolbar id={props.id} />} | ||
<div className="h-full w-full"> | ||
<Tldraw | ||
className="h-full w-full" | ||
snapshot={initialSnapshot.current} | ||
onMount={(editor) => { | ||
if (!state.document.value) { | ||
editor.updateInstanceState({ isReadonly: false }) | ||
} | ||
|
||
editor.store.listen(() => { | ||
const snapshot = editor.getSnapshot() | ||
state.document.set(JSON.stringify(snapshot)) | ||
state.metadata.lastModified.set(Date.now()) | ||
}) | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} |
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,35 @@ | ||
import { CanvasEditor } from './editor' | ||
import { | ||
type EditorPlugin, | ||
type EditorPluginProps, | ||
object, | ||
string, | ||
number, | ||
} from '../../plugin' | ||
|
||
const canvasState = object({ | ||
document: string(''), | ||
metadata: object({ | ||
lastModified: number(0), | ||
}), | ||
}) | ||
|
||
export type CanvasPluginState = typeof canvasState | ||
|
||
export const canvasPlugin: EditorPlugin<CanvasPluginState> = { | ||
Component: CanvasEditor, | ||
config: {}, | ||
state: canvasState, | ||
// We don't need onText for canvas | ||
onText: undefined, | ||
} | ||
|
||
export type CanvasProps = EditorPluginProps<CanvasPluginState> | ||
|
||
export interface SerializedTLStore { | ||
// Serialized TLStoreSnapshot | ||
document: string | ||
metadata: { | ||
lastModified: number | ||
} | ||
} |
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,40 @@ | ||
import type { EditorCanvasDocument } from '@editor/types/editor-plugins' | ||
import { Tldraw, TLEditorSnapshot } from 'tldraw' | ||
|
||
// eslint-disable-next-line import/no-unassigned-import | ||
import 'tldraw/tldraw.css' | ||
|
||
interface CanvasRendererProps { | ||
document: EditorCanvasDocument['state']['document'] | ||
} | ||
|
||
export function CanvasRenderer({ document }: CanvasRendererProps) { | ||
if (!document) return null | ||
|
||
let snapshot: TLEditorSnapshot | undefined | ||
try { | ||
snapshot = JSON.parse(document) as TLEditorSnapshot | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error('Failed to parse canvas document:', e) | ||
return null | ||
} | ||
|
||
return ( | ||
<div className="serlo-canvas-renderer relative h-[600px] w-full rounded border"> | ||
<div className="h-full w-full"> | ||
<Tldraw | ||
className="h-full w-full" | ||
snapshot={snapshot} | ||
components={{ | ||
TopPanel: () => null, | ||
Toolbar: () => null, | ||
}} | ||
onMount={(editor) => { | ||
editor.updateInstanceState({ isReadonly: true }) | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} |
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 @@ | ||
import { CanvasRenderer } from '@editor/plugins/canvas/renderer' | ||
import { EditorCanvasDocument } from '@editor/types/editor-plugins' | ||
|
||
export function CanvasStaticRenderer({ | ||
state: { document }, | ||
}: EditorCanvasDocument) { | ||
if (!document) return null | ||
|
||
return <CanvasRenderer document={document} /> | ||
} |
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,15 @@ | ||
import { PluginToolbar } from '@editor/editor-ui/plugin-toolbar' | ||
import { PluginDefaultTools } from '@editor/editor-ui/plugin-toolbar/plugin-tool-menu/plugin-default-tools' | ||
import { EditorPluginType } from '@editor/types/editor-plugin-type' | ||
|
||
export const CanvasToolbar = ({ id }: { id: string | undefined }) => { | ||
return ( | ||
<PluginToolbar | ||
pluginType={EditorPluginType.Canvas} | ||
pluginSettings={<>{/* Custom toolbar buttons */}</>} | ||
pluginControls={ | ||
<PluginDefaultTools pluginId={id || new Date().toString()} /> | ||
} | ||
/> | ||
) | ||
} |
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
Oops, something went wrong.