bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/whiteboard/component.jsx

300 lines
9.3 KiB
React
Raw Normal View History

2022-04-05 22:49:13 +08:00
import * as React from "react";
import _ from "lodash";
2022-04-05 22:49:13 +08:00
import Cursors from "./cursors/container";
import { TldrawApp, Tldraw } from "@tldraw/tldraw";
import {
ColorStyle,
DashStyle,
SizeStyle,
TDDocument,
TDShapeType,
} from "@tldraw/tldraw";
import { Renderer } from "@tldraw/core";
function usePrevious(value) {
const ref = React.useRef();
React.useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
2022-04-05 22:49:13 +08:00
const findRemoved = (A, B) => {
return A.filter((a) => {
return !B.includes(a);
});
};
export default function Whiteboard(props) {
const {
isPresenter,
removeShapes,
2022-04-05 22:49:13 +08:00
initDefaultPages,
meetingId,
persistShape,
persistAsset,
shapes,
assets,
currentUser,
curPres,
curSlide,
changeCurrentSlide,
2022-05-07 00:37:43 +08:00
whiteboardId,
2022-05-12 05:58:16 +08:00
podId,
zoomSlide,
slidePosition,
2022-04-05 22:49:13 +08:00
} = props;
2022-05-11 08:22:30 +08:00
// console.log('curPres : ', curPres)
2022-05-07 00:37:43 +08:00
//console.log('whiteboardId : ', whiteboardId)
const { pages, pageStates } = initDefaultPages(curPres?.pages.length || 1);
2022-04-05 22:49:13 +08:00
const rDocument = React.useRef({
name: "test",
version: TldrawApp.version,
id: `WB-${meetingId}`,
pages,
pageStates,
bindings: {},
assets,
});
const [doc, setDoc] = React.useState(rDocument.current);
const [curPage, setCurPage] = React.useState({ id: "1" });
2022-05-11 08:22:30 +08:00
const [_assets, setAssets] = React.useState(assets);
const [command, setCommand] = React.useState("");
const [selectedIds, setSelectedIds] = React.useState([]);
const [tldrawAPI, setTLDrawAPI] = React.useState(null);
const prevShapes = usePrevious(shapes);
const prevPage = usePrevious(curPage);
2022-05-12 05:58:16 +08:00
const prevSlidePosition = usePrevious(slidePosition);
2022-05-12 05:58:16 +08:00
const handleChange = React.useCallback((state, reason) => {
2022-04-05 22:49:13 +08:00
rDocument.current = state.document;
}, []);
React.useMemo(() => {
const currentDoc = rDocument.current;
const propShapes = Object.entries(shapes || {})?.map(([k, v]) => v.id);
if (!curPage && tldrawAPI) {
tldrawAPI.getPage();
2022-04-05 22:49:13 +08:00
}
2022-04-05 22:49:13 +08:00
const next = { ...currentDoc };
next.assets = { ...assets };
const pShapes = Object.entries(shapes || {})?.map(([k, v]) => v.id);
shapes?.forEach((s) => {
try {
2022-05-13 22:51:41 +08:00
Object.keys(next.pages[s.parentId].shapes).forEach((k) => {
if (!pShapes.includes(k) && s.parentId === tldrawAPI?.getPage()?.id) {
delete next.pages[s.parentId].shapes[k];
}
});
next.pages[s.parentId] = {
...next.pages[s.parentId],
shapes: {
...next.pages[s.parentId].shapes,
[s.id]: { ...s },
},
};
} catch (err) {
}
});
if (curPres?.pages.length) {
curPres.pages.map((p, i) => {
next.assets[`slide-background-asset-${i}`] = {
id: `slide-background-asset-${i}`,
size: [2560 / 3.5, 1440 / 3.5],
src: curPres?.pages[i]?.svgUri,
type: "image",
};
2022-05-13 22:51:41 +08:00
try {
next.pages[i + 1]["shapes"]["slide-background-shape"] = {
assetId: `slide-background-asset-${i}`,
childIndex: 1,
id: "slide-background-shape",
name: "Image",
type: TDShapeType.Image,
parentId: `${i + 1}`,
point: [50, 60],
isLocked: true,
size: [2560 / 3.5, 1440 / 3.5],
style: {
dash: DashStyle.Draw,
size: SizeStyle.Medium,
color: ColorStyle.Blue,
},
};
} catch (err) {
2022-05-13 22:51:41 +08:00
}
return p;
// setDoc(next);
});
}
2022-04-05 22:49:13 +08:00
rDocument.current = next;
const pageID = tldrawAPI?.getPage()?.id;
if (next.pageStates[pageID]?.selectedIds.length > 0) {
2022-04-05 22:49:13 +08:00
// if a selected id is not in the list of shapes remove it from list
next.pageStates[pageID]?.selectedIds.map((k) => {
if (!next.pages[pageID].shapes[k]) {
next.pageStates[pageID].selectedIds =
next.pageStates[pageID].selectedIds.filter(
(id) => id !== k
);
2022-04-05 22:49:13 +08:00
}
});
}
2022-05-12 05:58:16 +08:00
if (next.pageStates[pageID] && !isPresenter && !_.isEqual(slidePosition, prevSlidePosition)) {
next.pageStates[pageID].camera.point = [slidePosition.xCamera, slidePosition.yCamera]
next.pageStates[pageID].camera.zoom = slidePosition.zoom
}
2022-04-05 22:49:13 +08:00
setDoc(next);
//
if (
tldrawAPI &&
!_.isEqual(shapes, prevShapes) &&
2022-05-11 08:22:30 +08:00
!_.isEqual(assets, _assets)
) {
2022-05-11 08:22:30 +08:00
setAssets(assets);
tldrawAPI?.replacePageContent(next?.pages[pageID]?.shapes, {}, assets);
}
2022-05-11 08:22:30 +08:00
if (tldrawAPI && !_.isEqual(shapes, prevShapes) && !_.isEqual(assets, _assets)) {
tldrawAPI?.replacePageContent(next?.pages[pageID]?.shapes, {}, assets);
}
// setDoc(next);
}, [assets, shapes, curPres, tldrawAPI]);
React.useEffect(() => {
isPresenter && curPage && changeCurrentSlide(curPage?.id);
}, [curPage]);
React.useEffect(() => {
tldrawAPI &&
!isPresenter &&
curSlide?.activeSlide &&
tldrawAPI.changePage(curSlide?.activeSlide);
}, [curSlide]);
2022-04-05 22:49:13 +08:00
return (
<>
<Cursors
tldrawAPI={tldrawAPI}
currentUser={currentUser}
2022-05-07 00:37:43 +08:00
whiteboardId={whiteboardId}
>
<Tldraw
document={doc}
disableAssets={false}
onChangePage={(app, s, b, a) => {
setCurPage(app.getPage());
}}
onMount={(app) => {
setTLDrawAPI(app);
}}
onChange={handleChange}
onPersist={(e) => {
///////////// handle assets /////////////////////////
e?.assets?.forEach((a) => {
persistAsset(a);
});
}}
2022-05-11 08:22:30 +08:00
showPages={isPresenter}
showUI={isPresenter}
showMenu={false}
showMultiplayerMenu={false}
2022-05-11 08:22:30 +08:00
readOnly={!isPresenter}
onUndo={s => {
s?.selectedIds?.map(id => {
2022-05-07 00:37:43 +08:00
persistShape(s.getShape(id), whiteboardId);
})
}}
onRedo={s => {
s?.selectedIds?.map(id => {
2022-05-07 00:37:43 +08:00
persistShape(s.getShape(id), whiteboardId);
});
}}
onChangePage={(app, s, b, a) => {
if (curPage?.id !== app.getPage()?.id) setCurPage(app.getPage());
}}
onCommand={(e, s, g) => {
if (s.includes("session:complete:DrawSession")) {
Object.entries(rDocument?.current?.pages[e.getPage()?.id]?.shapes)
.filter(([k, s]) => s?.type === 'draw')
.forEach(([k, s]) => {
if (!e.prevShapes[k] || !k.includes('slide-background')) {
2022-05-07 00:37:43 +08:00
persistShape(s, whiteboardId);
}
});
}
if (s.includes("style")
|| s?.includes("session:complete:ArrowSession")) {
e.selectedIds.forEach(id => {
2022-05-07 00:37:43 +08:00
persistShape(e.getShape(id), whiteboardId);
});
}
if (s?.includes("session:complete:TransformSingleSession")
|| s?.includes("session:complete:TranslateSession")
|| s?.includes("updated_shapes")
|| s?.includes("session:complete:RotateSession")) {
e.selectedIds.forEach(id => {
2022-05-07 00:37:43 +08:00
persistShape(e.getShape(id), whiteboardId);
//checks to find any bindings assosiated with the selected shapes.
//If any, they need to be updated as well.
const pageBindings = rDocument?.current?.pages[e.getPage()?.id]?.bindings;
2022-05-11 08:22:30 +08:00
console.log('pageBindings', pageBindings)
const boundShapes = [];
if (pageBindings) {
Object.entries(pageBindings).map(([k,b]) => {
2022-05-07 00:37:43 +08:00
if (b.toId.includes(id), whiteboardId) {
boundShapes.push(rDocument?.current?.pages[e.getPage()?.id]?.shapes[b.fromId])
}
})
}
//persist shape(s) that was updated by the client and any shapes bound to it.
2022-05-11 08:22:30 +08:00
boundShapes.forEach(bs => persistShape(bs, whiteboardId))
});
}
if (s?.includes("session:complete:EraseSession") || s?.includes("delete")) {
let shapesIdsToRemove = []
shapes.forEach(s => {
const ids = e.shapes.map(ss => ss.id);
if (!ids.includes(s.id)) shapesIdsToRemove.push(s.id);
});
2022-05-07 00:37:43 +08:00
removeShapes(shapesIdsToRemove, whiteboardId)
}
}}
2022-05-12 05:58:16 +08:00
onPatch={(s, reason) => {
if (reason && isPresenter && (reason.includes("zoomed") || reason.includes("panned"))) {
const pageID = tldrawAPI?.getPage()?.id;
const camera = s.document.pageStates[pageID]?.camera
//console.log("Camera!!: ", camera);
zoomSlide(parseInt(pageID), podId, camera.zoom, camera.point[0], camera.point[1]);
}
}}
/>
</Cursors>
</>
2022-04-05 22:49:13 +08:00
);
}