From fb77323ef2c81f16d72ea85f3f6c39cacee83701 Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Fri, 5 Nov 2021 14:13:14 +0000 Subject: [PATCH] [feature] filesystem + readonly (#218) * Fix escape key for menu * Adds filesystem support, readonly mode * Move file system events to external hook * Adds onSignIn callback, prevent event by default --- example/esbuild.config.mjs | 27 +- example/package.json | 8 +- example/src/app.tsx | 7 + example/src/components/editor.tsx | 22 +- example/src/index.html | 4 +- example/src/readonly.tsx | 6 + packages/tldraw/scripts/build.js | 2 + packages/tldraw/scripts/dev.js | 1 + packages/tldraw/src/TLDraw.test.tsx | 22 +- packages/tldraw/src/TLDraw.tsx | 98 ++++- .../components/ContextMenu/ContextMenu.tsx | 4 +- .../src/components/DropdownMenu/DMContent.tsx | 4 +- .../components/ToolsPanel/PrimaryTools.tsx | 14 +- .../src/components/TopPanel/ColorMenu.tsx | 1 - .../tldraw/src/components/TopPanel/Menu.tsx | 147 ++++---- .../src/components/TopPanel/SizeMenu.tsx | 2 +- .../src/components/TopPanel/TopPanel.tsx | 7 +- packages/tldraw/src/hooks/index.ts | 2 + packages/tldraw/src/hooks/useFileSystem.ts | 49 +++ .../tldraw/src/hooks/useFileSystemHandlers.ts | 45 +++ .../tldraw/src/hooks/useKeyboardShortcuts.tsx | 45 ++- .../tldraw/src/hooks/useTLDrawContext.tsx | 12 +- packages/tldraw/src/index.ts | 1 + .../state/data/browser-fs-access/index.d.ts | 99 ++++++ .../src/state/data/browser-fs-access/index.js | 334 ++++++++++++++++++ .../tldraw/src/state/data/filesystem.spec.ts | 10 + packages/tldraw/src/state/data/filesystem.ts | 101 ++++++ .../tldraw/src/state/data/migrate.spec.ts | 15 + .../tldraw/src/state/{ => data}/migrate.ts | 9 +- packages/tldraw/src/state/tlstate.ts | 134 +++++-- packages/tldraw/src/test/migration.spec.ts | 35 -- packages/tldraw/src/test/mock-document.tsx | 1 + .../tldraw/src/test/renderWithContext.tsx | 2 +- packages/tldraw/src/types.ts | 11 + www/components/editor.tsx | 12 +- www/hooks/useFileSystem.ts | 43 +++ yarn.lock | 4 +- 37 files changed, 1132 insertions(+), 208 deletions(-) create mode 100644 example/src/readonly.tsx create mode 100644 packages/tldraw/src/hooks/useFileSystem.ts create mode 100644 packages/tldraw/src/hooks/useFileSystemHandlers.ts create mode 100644 packages/tldraw/src/state/data/browser-fs-access/index.d.ts create mode 100644 packages/tldraw/src/state/data/browser-fs-access/index.js create mode 100644 packages/tldraw/src/state/data/filesystem.spec.ts create mode 100644 packages/tldraw/src/state/data/filesystem.ts create mode 100644 packages/tldraw/src/state/data/migrate.spec.ts rename packages/tldraw/src/state/{ => data}/migrate.ts (90%) delete mode 100644 packages/tldraw/src/test/migration.spec.ts create mode 100644 www/hooks/useFileSystem.ts diff --git a/example/esbuild.config.mjs b/example/esbuild.config.mjs index 398a8f1ae..fe792d001 100644 --- a/example/esbuild.config.mjs +++ b/example/esbuild.config.mjs @@ -3,8 +3,6 @@ import fs from 'fs' import esbuild from 'esbuild' import serve, { error, log } from 'create-serve' -const isDevServer = true - if (!fs.existsSync('./dist')) { fs.mkdirSync('./dist') } @@ -16,17 +14,18 @@ fs.copyFile('./src/index.html', './dist/index.html', (err) => { esbuild .build({ entryPoints: ['src/index.tsx'], - bundle: true, - outfile: 'dist/bundle.js', + outfile: 'dist/index.js', minify: false, + bundle: true, sourcemap: true, - incremental: isDevServer, - target: ['chrome58', 'firefox57', 'safari11', 'edge18'], + incremental: true, + format: 'esm', + target: 'esnext', define: { 'process.env.LIVEBLOCKS_PUBLIC_API_KEY': process.env.LIVEBLOCKS_PUBLIC_API_KEY, - 'process.env.NODE_ENV': isDevServer ? '"development"' : '"production"', + 'process.env.NODE_ENV': '"development"', }, - watch: isDevServer && { + watch: { onRebuild(err) { serve.update() err ? error('❌ Failed') : log('✅ Updated') @@ -35,10 +34,8 @@ esbuild }) .catch(() => process.exit(1)) -if (isDevServer) { - serve.start({ - port: 5000, - root: './dist', - live: true, - }) -} +serve.start({ + port: 5000, + root: './dist', + live: true, +}) diff --git a/example/package.json b/example/package.json index 1aacc3729..ddb48fbe3 100644 --- a/example/package.json +++ b/example/package.json @@ -18,11 +18,9 @@ "src" ], "sideEffects": false, - "peerDependencies": { - "react": ">=16.8", - "react-dom": "^16.8 || ^17.0" - }, "devDependencies": { + "react": ">=16.8", + "react-dom": "^16.8 || ^17.0", "@liveblocks/client": "^0.12.1", "@liveblocks/react": "^0.12.1", "@tldraw/tldraw": "^0.1.2", @@ -39,4 +37,4 @@ "typescript": "4.2.3" }, "gitHead": "a7dac0f83ad998e205c2aab58182cb4ba4e099a6" -} +} \ No newline at end of file diff --git a/example/src/app.tsx b/example/src/app.tsx index 3efc230d9..a69944d7d 100644 --- a/example/src/app.tsx +++ b/example/src/app.tsx @@ -1,6 +1,7 @@ import * as React from 'react' import { Switch, Route, Link } from 'react-router-dom' import Basic from './basic' +import ReadOnly from './readonly' import Controlled from './controlled' import Imperative from './imperative' import Embedded from './embedded' @@ -16,6 +17,9 @@ export default function App(): JSX.Element { + + + @@ -39,6 +43,9 @@ export default function App(): JSX.Element {
  • basic
  • +
  • + readonly +
  • controlled
  • diff --git a/example/src/components/editor.tsx b/example/src/components/editor.tsx index 5d519bec1..0286fa6c4 100644 --- a/example/src/components/editor.tsx +++ b/example/src/components/editor.tsx @@ -1,9 +1,11 @@ import * as React from 'react' -import { TLDraw, TLDrawProps, TLDrawState } from '@tldraw/tldraw' +import { TLDraw, TLDrawProps, TLDrawState, useFileSystem } from '@tldraw/tldraw' export default function Editor(props: TLDrawProps): JSX.Element { const rTLDrawState = React.useRef() + const fileSystemEvents = useFileSystem() + const handleMount = React.useCallback((state: TLDrawState) => { rTLDrawState.current = state props.onMount?.(state) @@ -12,9 +14,25 @@ export default function Editor(props: TLDrawProps): JSX.Element { window.tlstate = state }, []) + const onSignIn = React.useCallback((state: TLDrawState) => { + // Sign in? + }, []) + + const onSignOut = React.useCallback((state: TLDrawState) => { + // Sign out? + }, []) + return (
    - +
    ) } diff --git a/example/src/index.html b/example/src/index.html index c7ceedfe8..1da2d34e1 100644 --- a/example/src/index.html +++ b/example/src/index.html @@ -2,14 +2,14 @@ - + tldraw
    - +