Skip to content

Commit

Permalink
feat: integrate plugin-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ishita1805 committed Jun 2, 2023
1 parent 8b427a4 commit 3a8f6db
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 115 deletions.
84 changes: 84 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@dytesdk/plugin-sdk": "^2.0.4",
"axios": "^1.4.0",
"core-js": "^3.30.2",
"react": "^18.2.0",
Expand Down
11 changes: 7 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { Document, Dashboard } from './pages'
import { MainContext } from './context'

const App = () => {
const { document } = useContext(MainContext);
const { doc, plugin } = useContext(MainContext);

return (
<div className='container'>
{
document
? <Document />
: <Dashboard />
plugin ?
doc
? <Document plugin={plugin} />
: <Dashboard />
: <div>Loading Plugin...</div>
}

</div>
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ToolbarRightProps {
scale: number;
activeColor: string;
activeTool: ToolbarState;
onNewFile: () => void;
onBack: () => void;
setActiveColor: (col: string) => void;
selectActiveTool: (state: ToolbarState) => void;
}
Expand All @@ -27,7 +27,7 @@ const ToolbarRight = (props: ToolbarRightProps) => {
scale,
activeTool,
activeColor,
onNewFile,
onBack,
setActiveColor,
selectActiveTool,
} = props;
Expand Down Expand Up @@ -56,12 +56,12 @@ const ToolbarRight = (props: ToolbarRightProps) => {
return (
<div className="toolbar-right">
<div className="row">
<Icon icon="add_file" className="new-file-tool" onClick={onNewFile} />
<div className="toolbar-page">
<Icon onClick={() => updateTool('zoom-in-tool')} className="toolbar-icon" icon='zoomIn' />
<span>{Math.round(scale * 100)}%</span>
<Icon onClick={() => updateTool('zoom-out-tool')} className="toolbar-icon" icon='zoomOut' />
</div>
<Icon icon="dismiss" className="back-icon" onClick={onBack} />
</div>
<div className="toolbar-tools">
{
Expand Down
4 changes: 2 additions & 2 deletions src/components/toolbar/toolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.toolbar-right {
@apply fixed top-2 right-2 z-50 flex flex-col items-end;
}
.new-file-tool {
@apply bg-blue-600 rounded-md h-9 w-12 text-white mx-2 cursor-pointer border-[1px] border-solid border-blue-900;
.back-icon {
@apply bg-gray-600 rounded-md h-9 w-9 p-1 text-white ml-2 cursor-pointer border-[1px] border-solid border-blue-900;
}
.toolbar-icon {
@apply h-auto !w-7 cursor-pointer;
Expand Down
77 changes: 74 additions & 3 deletions src/context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
import React, { useState } from 'react'
import DytePlugin from '@dytesdk/plugin-sdk';
import React, { useEffect, useState } from 'react'

const MainContext = React.createContext<any>({});

const MainProvider = ({ children }: { children: any }) => {
const [document, setDocument] = useState<string>();
const [base, setBase] = useState<string>('');
const [userId, setUserId] = useState<string>('');
const [plugin, setPlugin] = useState<DytePlugin>();
const [doc, updateDocument] = useState<string>();
const [currentPage, updateCurrentPage] = useState<number>(0);
const [data, updateData] = useState<{[key: number]: string}>({});

const setDocument = async (url: string) => {
if (plugin) {
await plugin.stores.get('doc').set('url', url);
}
}
const setCurrentPage = async (page: number) => {
await setData();
if (plugin) {
await plugin.stores.get('doc').set('page', page);
}
};
const setData = async () => {
const svg = document.getElementById('svg');
if (!svg) return;
if (plugin) {
await plugin.stores.get('doc').set('annotations', { ...data, [currentPage]: svg.innerHTML });
}
}


const loadPlugin = async () => {
// initialize the SDK
const dytePlugin = DytePlugin.init();

// fetch data for a store
await dytePlugin.stores.populate('doc');

// define constants used across the app
const id = await dytePlugin.room.getID();
const userId = await dytePlugin.room.getPeer();
setBase(id.payload.roomName);
setUserId(userId.payload.peer.id);

// subscribe to store
const store = dytePlugin.stores.create('doc');
store.subscribe('url', ({ url }) => {
updateDocument(url);
});
store.subscribe('page', ({ page }) => {
updateCurrentPage(page);
});
store.subscribe('annotations', ({ annotations }) => {
updateData(annotations);
});

// load initial data
const currUrl = store.get('url');
const currPage = store.get('page');
const currAnnotations = store.get('annotations');
if (currUrl) updateDocument(currUrl);
if (currPage) updateCurrentPage(currPage);
if (currAnnotations) updateData(currAnnotations);
setPlugin(dytePlugin);
}

useEffect(() => {
loadPlugin();
return () => {
if (!plugin) return;
plugin.removeListeners('remote-erase-all');
plugin.removeListeners('remote-el');
plugin.removeListeners('remote-erase');
}
}, [])

return (
<MainContext.Provider value={{ document, setDocument }}>
<MainContext.Provider value={{ data, base, userId, plugin, doc, currentPage, setDocument, setCurrentPage }}>
{children}
</MainContext.Provider>
)
Expand Down
Loading

0 comments on commit 3a8f6db

Please sign in to comment.