Skip to content

Commit

Permalink
修复浏览器端无法使用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zuoez02 committed Oct 23, 2023
1 parent ffa4b70 commit b4c6c98
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@vitejs/plugin-react": "^4.1.0",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-error-overlay": "^6.0.11"
}
}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "siyuan-plugin-whiteboard",
"author": "zuoez02",
"url": "https://github.com/左恩镇/siyuan-plugin-whiteboard",
"version": "0.1.0",
"version": "0.1.1",
"minAppVersion": "2.10.3",
"backends": ["all"],
"frontends": ["all"],
Expand Down
37 changes: 18 additions & 19 deletions src/components/tab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, useState, useCallback, useEffect } from "react";
import { Fragment, useState } from "react";
import { Tldraw, Editor } from "@tldraw/tldraw";
import { getAssetUrls } from "@tldraw/assets/selfHosted";

Expand All @@ -10,15 +10,22 @@ import { uiOverrides } from "@/whiteboard/ui-overrides";
export const Tab = (props) => {
const initData = props.initData;
const realName = props.name;
const [editor, setEditor] = useState<Editor>();
const [editor, setEditor] = useState<Editor>(null);

const assetUrls = getAssetUrls({
baseUrl: "/plugins/siyuan-plugin-whiteboard/assets/",
});

const setAppToState = useCallback((editor: Editor) => {
setEditor(editor);
}, []);
const setAppToState = (e: Editor) => {
if (!editor) {
setEditor(e);
if (Object.keys(initData).length) {
e.store.loadSnapshot(initData);
}
e.store.listen(() => save(e));
console.log('init');
}
};

let timer;

Expand All @@ -29,32 +36,24 @@ export const Tab = (props) => {
timer = setTimeout(() => fn(), timeout);
}

function saveNow() {
const json = editor.store.getSnapshot();
function saveNow(e) {
console.log('save');
const json = e.store.getSnapshot();
saveData(realName, json);
}

function save() {
function save(e) {
debounce(() => {
saveNow();
saveNow(e);
}, 500);
}

useEffect(() => {
if (!editor) {
return;
}
if (Object.keys(initData).length) {
editor.store.loadSnapshot(initData);
}
editor.store.listen(() => save());
}, [editor]);

return (
<Fragment>
<div className="whiteboard-component">
<Tldraw
onMount={setAppToState}
persistenceKey={realName}
assetUrls={assetUrls}
overrides={uiOverrides}
/>
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './utils/env';
import { icon } from './assets/icon';
import { Plugin, openTab } from "siyuan";
import { initWhiteboardTab, initWhiteboardDock } from "./utils";
Expand Down
1 change: 1 addition & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.process = Object.assign({ env: {} }, window.process);

0 comments on commit b4c6c98

Please sign in to comment.