Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
feat: add debug parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed May 24, 2024
1 parent fcb1908 commit c10b238
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ document.body.appendChild(tldrawEditor);

| Nom | Type | Requis | Default | Description |
| --------------------- | :---------------: | :------------------------: | :-----: | ----------------------------------------------------------------------------------------------------------- |
| `debug` | `boolean` | `false` | `false` | Active les logs de debug |
| `mode` | `single \| multi` | `true` | | Mode de fonctionnement |
| `persistance-api-url` | `string` | `false` | | URL du fichier (GET & PUT) \| (En mode multi, permet la sauvegarde sur le fichier) |
| `assets-api-url` | `string` | `false` | | URL des assets (GET, POST & DELETE) |
Expand All @@ -60,6 +61,7 @@ document.body.appendChild(tldrawEditor);

```html
<tldraw-editor
debug
mode=""
persistance-api-url=""
assets-api-url=""
Expand Down
1 change: 1 addition & 0 deletions src/AppDev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function App() {
owner={owner}
clearOnLeave={clearOnLeave}
leave={leave}
debug={true}
/>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions src/ce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import r2wc from '@r2wc/react-to-web-component';

const TldrawEditor = r2wc(TldrawEditorSFC, {
props: {
debug: 'boolean',
mode: 'string',
persistanceApiUrl: 'string',
assetsApiUrl: 'string',
Expand Down
3 changes: 2 additions & 1 deletion src/components/MultiplayerEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export default function MultiplayerEditor({
setProvider,
...params
}: Readonly<MultiplayerEditorProps>) {
const { autoSave, autoSaveDelay, open, isReady, setIsSaving, setIsLoading, setIsError, setIsReady } = params;
const { debug, autoSave, autoSaveDelay, open, isReady, setIsSaving, setIsLoading, setIsError, setIsReady } = params;

const props = {
...params,
...useMultiplayer(
debug,
persistanceApiUrl,
assetsApiUrl,
token,
Expand Down
2 changes: 2 additions & 0 deletions src/components/TldrawEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useEffect, useState } from 'react';
import { WebsocketProvider } from 'y-websocket';

export default function TldrawEditor({
debug,
mode,
persistanceApiUrl,
assetsApiUrl,
Expand Down Expand Up @@ -55,6 +56,7 @@ export default function TldrawEditor({
const isMulti: boolean = cMode == 'multi' && websocketApiUrl != undefined && roomId != undefined;

const common: CommonProps = {
debug: debug ?? false,
darkMode: darkMode ?? false,
autoSave: autoSave ?? true,
autoSaveDelay: autoSaveDelay ?? 3000,
Expand Down
39 changes: 28 additions & 11 deletions src/hooks/useMultiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { WebsocketProvider } from 'y-websocket';

export function useMultiplayer(
debug: boolean,
persistanceApiUrl: string | undefined,
assetsApiUrl: string | undefined,
token: string | undefined,
Expand Down Expand Up @@ -41,6 +42,10 @@ export function useMultiplayer(
);
const { loadDocument } = usePersistance(initUrl);

const log = (message: string, variable: object): void => {
if (debug) console.debug(`Multiplayer - ${message}`, { ...variable });
};

/**
* Force rerendering
*/
Expand All @@ -55,14 +60,20 @@ export function useMultiplayer(
const oldValue: string = yPersistanceApiUrl.toString();
if (oldValue !== persistanceApiUrl) {
yPersistanceApiUrl.delete(0, oldValue.length);
setTimeout(() => {
if (persistanceApiUrl) yPersistanceApiUrl.insert(0, persistanceApiUrl);
}, 200);
if (persistanceApiUrl) yPersistanceApiUrl.insert(0, persistanceApiUrl);
log('persistance API URL has been set', { newValue: yPersistanceApiUrl.toString(), oldValue });
}
}
}, 200);
}, 500);

const handleChanges = throttle(() => setKey((value) => value + 1), 200, { trailing: true });
const handleChanges = throttle(
() => {
log('persistance API URL has changed', { newValue: yPersistanceApiUrl.toString() });
setKey((value) => value + 1);
},
200,
{ trailing: true },
);

yPersistanceApiUrl.observeDeep(handleChanges);

Expand All @@ -78,14 +89,20 @@ export function useMultiplayer(
const oldValue: string = yAssetsApiUrl.toString();
if (oldValue !== assetsApiUrl) {
yAssetsApiUrl.delete(0, oldValue.length);
setTimeout(() => {
if (assetsApiUrl) yAssetsApiUrl.insert(0, assetsApiUrl);
}, 200);
if (assetsApiUrl) yAssetsApiUrl.insert(0, assetsApiUrl);
log('assets API URL has been set', { newValue: yAssetsApiUrl.toString(), oldValue });
}
}
}, 200);

const handleChanges = throttle(() => setKey((value) => value + 1), 200, { trailing: true });
}, 500);

const handleChanges = throttle(
() => {
log('assets API URL has changed', { newValue: yAssetsApiUrl.toString() });
setKey((value) => value + 1);
},
200,
{ trailing: true },
);

yAssetsApiUrl.observeDeep(handleChanges);

Expand Down
2 changes: 2 additions & 0 deletions src/types/CommonProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type CommonProps = {
debug: boolean;

// Tldraw props
darkMode: boolean;
readOnly: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/types/TldrawEditorProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type TldrawEditorProps = {
debug?: boolean;
mode?: 'single' | 'multi';

// Common properties
Expand Down

0 comments on commit c10b238

Please sign in to comment.