From 376d83615437be4288e82d697ff0576c7a0aedff Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Sat, 4 Sep 2021 16:40:23 +0100 Subject: [PATCH] Adds onChange and onMount to constructor --- packages/dev/src/components/editor.tsx | 20 +++++++++++++++++-- packages/tldraw/package.json | 4 ++-- .../tldraw/src/components/tldraw/tldraw.tsx | 10 +++------- packages/tldraw/src/state/tlstate.ts | 17 +++++++++++++--- yarn.lock | 8 ++++---- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/packages/dev/src/components/editor.tsx b/packages/dev/src/components/editor.tsx index 06eb17a12..ba0101975 100644 --- a/packages/dev/src/components/editor.tsx +++ b/packages/dev/src/components/editor.tsx @@ -2,10 +2,26 @@ import * as React from 'react' import { TLDraw, TLDrawState } from '@tldraw/tldraw' export default function Editor(): JSX.Element { - const handleMount = React.useCallback((tlstate: TLDrawState) => { + const rTLDrawState = React.useRef() + + const handleMount = React.useCallback((state: TLDrawState) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - window.tlstate = tlstate + window.tlstate = state + + rTLDrawState.current = state + state.selectAll() + state.createShapes({ + id: 'rect1', + type: 'rectangle', + point: [100, 100], + size: [200, 200], + }) + state.updateShapes({ + id: 'rect1', + point: [150, 150], + }) }, []) + return } diff --git a/packages/tldraw/package.json b/packages/tldraw/package.json index 4126f7768..77b8e5fbd 100644 --- a/packages/tldraw/package.json +++ b/packages/tldraw/package.json @@ -63,7 +63,7 @@ "@tldraw/core": "^0.0.58", "perfect-freehand": "^0.5.3", "react-hotkeys-hook": "^3.4.0", - "rko": "^0.5.19" + "rko": "^0.5.20" }, "gitHead": "838fabdbff1a66d4d7ee8aa5c5d117bc55acbff2" -} +} \ No newline at end of file diff --git a/packages/tldraw/src/components/tldraw/tldraw.tsx b/packages/tldraw/src/components/tldraw/tldraw.tsx index 8a6a3ef01..c8ba14a29 100644 --- a/packages/tldraw/src/components/tldraw/tldraw.tsx +++ b/packages/tldraw/src/components/tldraw/tldraw.tsx @@ -50,11 +50,11 @@ export interface TLDrawProps { onChange?: TLDrawState['_onChange'] } -export function TLDraw({ id, document, currentPageId, onMount, onChange: _onChange }: TLDrawProps) { +export function TLDraw({ id, document, currentPageId, onMount, onChange }: TLDrawProps) { const [tlstate, setTlstate] = React.useState(() => new TLDrawState(id)) React.useEffect(() => { - setTlstate(new TLDrawState(id)) + setTlstate(new TLDrawState(id, onChange, onMount)) }, [id]) const [context] = React.useState(() => { @@ -63,7 +63,7 @@ export function TLDraw({ id, document, currentPageId, onMount, onChange: _onChan React.useEffect(() => { if (!document) return - tlstate.loadDocument(document, _onChange) + tlstate.loadDocument(document) }, [document, tlstate]) React.useEffect(() => { @@ -71,10 +71,6 @@ export function TLDraw({ id, document, currentPageId, onMount, onChange: _onChan tlstate.changePage(currentPageId) }, [currentPageId, tlstate]) - React.useEffect(() => { - onMount?.(tlstate) - }, []) - return ( diff --git a/packages/tldraw/src/state/tlstate.ts b/packages/tldraw/src/state/tlstate.ts index 4c7b7afb0..71fa16c5a 100644 --- a/packages/tldraw/src/state/tlstate.ts +++ b/packages/tldraw/src/state/tlstate.ts @@ -96,6 +96,7 @@ const initialData: Data = { export class TLDrawState extends StateManager { private _onChange?: (tlstate: TLDrawState, data: Data, reason: string) => void + private _onMount?: (tlstate: TLDrawState) => void selectHistory: SelectHistory = { stack: [[]], @@ -116,7 +117,11 @@ export class TLDrawState extends StateManager { selectedGroupId?: string - constructor(id = Utils.uniqueId()) { + constructor( + id = Utils.uniqueId(), + onChange?: (tlstate: TLDrawState, data: Data, reason: string) => void, + onMount?: (tlstate: TLDrawState) => void + ) { super(initialData, id, 2, (prev, next, prevVersion) => { const state = { ...prev } if (prevVersion === 1) @@ -127,11 +132,18 @@ export class TLDrawState extends StateManager { return state }) + this._onChange = onChange + this._onMount = onMount + this.session = undefined this.pointedId = undefined } /* -------------------- Internal -------------------- */ + onReady = () => { + this._onMount?.(this) + } + /** * Cleanup the state after each state change. * @param state The new state @@ -474,8 +486,7 @@ export class TLDrawState extends StateManager { * @param onChange (optional) A callback to call when the document changes * @returns this */ - loadDocument = (document: TLDrawDocument, onChange?: TLDrawState['_onChange']): this => { - this._onChange = onChange + loadDocument = (document: TLDrawDocument): this => { this.deselectAll() this.resetHistory() this.clearSelectHistory() diff --git a/yarn.lock b/yarn.lock index 79c7eefb4..e67192f93 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11530,10 +11530,10 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" -rko@^0.5.19: - version "0.5.19" - resolved "https://registry.yarnpkg.com/rko/-/rko-0.5.19.tgz#33577596167178abc30063b6dd0a8bbde0362c27" - integrity sha512-0KSdDnbhD11GCDvZFARvCJedJuwWIh5F1cCqTGljFF/wQi9PUVu019qH4ME4LdPF3HotMLcdQsxEXmIDeeD0zQ== +rko@^0.5.19, rko@^0.5.20: + version "0.5.20" + resolved "https://registry.yarnpkg.com/rko/-/rko-0.5.20.tgz#a0a374e9b8d6880f05a1d13f2efc277299644e19" + integrity sha512-u9V6n0rx9nHI+hO0e20LYdvKEHCDDwVRsD0vMxMe2A2sHWVb6qtmwMw3hifJTo0Tn3UJQDOiBy+uE7zxEyrS/g== dependencies: idb-keyval "^5.1.3" zustand "^3.5.9"