2022-04-05 22:49:13 +08:00
|
|
|
import * as React from "react";
|
2022-04-26 21:55:05 +08:00
|
|
|
import _ from "lodash";
|
2023-01-12 03:32:03 +08:00
|
|
|
import styled, { createGlobalStyle } from "styled-components";
|
2022-04-05 22:49:13 +08:00
|
|
|
import Cursors from "./cursors/container";
|
|
|
|
import { TldrawApp, Tldraw } from "@tldraw/tldraw";
|
2022-08-16 20:12:43 +08:00
|
|
|
import SlideCalcUtil, {HUNDRED_PERCENT} from '/imports/utils/slideCalcUtils';
|
2022-10-21 22:05:31 +08:00
|
|
|
import { Utils } from "@tldraw/core";
|
2022-11-01 00:42:38 +08:00
|
|
|
import Settings from '/imports/ui/services/settings';
|
2023-01-24 04:00:05 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2023-02-06 21:37:34 +08:00
|
|
|
import KEY_CODES from '/imports/utils/keyCodes';
|
2022-04-11 00:31:12 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-01 00:42:38 +08:00
|
|
|
// map different localeCodes from bbb to tldraw
|
|
|
|
const mapLanguage = (language) => {
|
2023-01-19 04:08:32 +08:00
|
|
|
// bbb has xx-xx but in tldraw it's only xx
|
|
|
|
if (['es', 'fa', 'it', 'pl', 'sv', 'uk'].some((lang) => language.startsWith(lang))) {
|
|
|
|
return language.substring(0, 2);
|
|
|
|
}
|
|
|
|
// exceptions
|
|
|
|
switch (language) {
|
2022-11-01 00:42:38 +08:00
|
|
|
case 'nb-no':
|
|
|
|
return 'no';
|
|
|
|
case 'zh-cn':
|
|
|
|
return 'zh-ch';
|
|
|
|
default:
|
|
|
|
return language;
|
|
|
|
}
|
2023-01-19 04:08:32 +08:00
|
|
|
};
|
2022-11-01 00:42:38 +08:00
|
|
|
|
2022-08-13 03:08:26 +08:00
|
|
|
const SMALL_HEIGHT = 435;
|
|
|
|
const SMALLEST_HEIGHT = 363;
|
2022-11-18 07:37:17 +08:00
|
|
|
const SMALL_WIDTH = 800;
|
|
|
|
const SMALLEST_WIDTH = 645;
|
2022-08-13 03:08:26 +08:00
|
|
|
const TOOLBAR_SMALL = 28;
|
2022-08-22 05:38:54 +08:00
|
|
|
const TOOLBAR_LARGE = 38;
|
2022-09-07 22:34:54 +08:00
|
|
|
const TOOLBAR_OFFSET = 0;
|
2022-08-13 03:08:26 +08:00
|
|
|
|
2022-08-12 00:38:17 +08:00
|
|
|
const TldrawGlobalStyle = createGlobalStyle`
|
|
|
|
${({ hideContextMenu }) => hideContextMenu && `
|
|
|
|
#TD-ContextMenu {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`}
|
2022-11-01 01:07:03 +08:00
|
|
|
#TD-PrimaryTools-Image {
|
|
|
|
display: none;
|
|
|
|
}
|
2023-01-12 19:50:05 +08:00
|
|
|
#slide-background-shape div {
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
2023-01-18 08:22:50 +08:00
|
|
|
[aria-expanded*="false"][aria-controls*="radix-"] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
${({ hasWBAccess, isPresenter, size }) => (hasWBAccess || isPresenter) && `
|
|
|
|
#TD-Tools-Dots {
|
|
|
|
height: ${size}px;
|
|
|
|
width: ${size}px;
|
|
|
|
}
|
|
|
|
#TD-Delete {
|
|
|
|
& button {
|
|
|
|
height: ${size}px;
|
|
|
|
width: ${size}px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#TD-PrimaryTools button {
|
|
|
|
height: ${size}px;
|
|
|
|
width: ${size}px;
|
|
|
|
}
|
|
|
|
`}
|
2022-08-12 00:38:17 +08:00
|
|
|
`;
|
|
|
|
|
2023-01-12 03:32:03 +08:00
|
|
|
const EditableWBWrapper = styled.div`
|
|
|
|
&, & > :first-child {
|
|
|
|
cursor: inherit !important;
|
|
|
|
}
|
2022-08-12 00:38:17 +08:00
|
|
|
`;
|
|
|
|
|
2022-04-05 22:49:13 +08:00
|
|
|
export default function Whiteboard(props) {
|
|
|
|
const {
|
|
|
|
isPresenter,
|
2022-10-21 22:05:31 +08:00
|
|
|
isModerator,
|
2022-05-04 20:40:56 +08:00
|
|
|
removeShapes,
|
2022-04-05 22:49:13 +08:00
|
|
|
initDefaultPages,
|
|
|
|
persistShape,
|
2022-10-21 22:05:31 +08:00
|
|
|
notifyNotAllowedChange,
|
2022-04-05 22:49:13 +08:00
|
|
|
shapes,
|
2022-10-06 04:43:48 +08:00
|
|
|
assets,
|
2022-04-05 22:49:13 +08:00
|
|
|
currentUser,
|
2022-04-26 21:55:05 +08:00
|
|
|
curPres,
|
2022-05-07 00:37:43 +08:00
|
|
|
whiteboardId,
|
2022-05-12 05:58:16 +08:00
|
|
|
podId,
|
|
|
|
zoomSlide,
|
2022-05-19 03:35:42 +08:00
|
|
|
skipToSlide,
|
2022-05-12 05:58:16 +08:00
|
|
|
slidePosition,
|
2022-05-16 10:35:17 +08:00
|
|
|
curPageId,
|
2022-07-08 23:06:00 +08:00
|
|
|
presentationWidth,
|
|
|
|
presentationHeight,
|
2022-06-02 23:00:28 +08:00
|
|
|
isViewersCursorLocked,
|
2022-06-17 08:35:27 +08:00
|
|
|
zoomChanger,
|
2022-06-30 04:25:32 +08:00
|
|
|
isMultiUserActive,
|
2022-08-03 22:19:12 +08:00
|
|
|
isRTL,
|
2022-08-16 20:12:43 +08:00
|
|
|
fitToWidth,
|
|
|
|
zoomValue,
|
2022-08-15 06:49:39 +08:00
|
|
|
isPanning,
|
2022-10-21 22:05:31 +08:00
|
|
|
intl,
|
2022-11-09 02:39:16 +08:00
|
|
|
svgUri,
|
2022-11-24 01:03:20 +08:00
|
|
|
maxStickyNoteLength,
|
2023-01-16 19:40:42 +08:00
|
|
|
fontFamily,
|
2023-01-25 02:32:49 +08:00
|
|
|
hasShapeAccess,
|
2023-01-26 02:49:09 +08:00
|
|
|
presentationAreaHeight,
|
|
|
|
presentationAreaWidth,
|
2023-02-03 03:47:16 +08:00
|
|
|
maxNumberOfAnnotations,
|
|
|
|
notifyShapeNumberExceeded,
|
2022-04-05 22:49:13 +08:00
|
|
|
} = props;
|
2022-05-16 10:35:17 +08:00
|
|
|
|
2022-04-26 21:55:05 +08:00
|
|
|
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,
|
2022-05-16 10:35:17 +08:00
|
|
|
id: whiteboardId,
|
2022-04-05 22:49:13 +08:00
|
|
|
pages,
|
|
|
|
pageStates,
|
|
|
|
bindings: {},
|
2022-07-08 23:06:00 +08:00
|
|
|
assets: {},
|
2022-04-05 22:49:13 +08:00
|
|
|
});
|
2022-04-11 00:31:12 +08:00
|
|
|
const [tldrawAPI, setTLDrawAPI] = React.useState(null);
|
2022-11-01 18:46:17 +08:00
|
|
|
const [history, setHistory] = React.useState(null);
|
2022-08-15 06:49:39 +08:00
|
|
|
const [forcePanning, setForcePanning] = React.useState(false);
|
2022-08-16 20:12:43 +08:00
|
|
|
const [zoom, setZoom] = React.useState(HUNDRED_PERCENT);
|
2023-01-26 02:49:09 +08:00
|
|
|
const [tldrawZoom, setTldrawZoom] = React.useState(1);
|
|
|
|
const [enable, setEnable] = React.useState(true);
|
2022-08-16 20:12:43 +08:00
|
|
|
const [isMounting, setIsMounting] = React.useState(true);
|
2022-04-11 00:31:12 +08:00
|
|
|
const prevShapes = usePrevious(shapes);
|
2022-08-16 20:12:43 +08:00
|
|
|
const prevSlidePosition = usePrevious(slidePosition);
|
|
|
|
const prevFitToWidth = usePrevious(fitToWidth);
|
2022-11-09 02:39:16 +08:00
|
|
|
const prevSvgUri = usePrevious(svgUri);
|
2022-11-01 00:42:38 +08:00
|
|
|
const language = mapLanguage(Settings?.application?.locale?.toLowerCase() || 'en');
|
2023-01-12 03:32:03 +08:00
|
|
|
const [currentTool, setCurrentTool] = React.useState(null);
|
2022-04-11 00:31:12 +08:00
|
|
|
|
2023-01-26 02:49:09 +08:00
|
|
|
const throttledResetCurrentPoint = React.useRef(_.throttle(() => {
|
|
|
|
setEnable(false);
|
|
|
|
setEnable(true);
|
|
|
|
}, 1000, { trailing: true }));
|
|
|
|
|
2022-08-16 20:12:43 +08:00
|
|
|
const calculateZoom = (width, height) => {
|
2023-01-27 23:49:32 +08:00
|
|
|
const calcedZoom = fitToWidth ? (presentationWidth / width) : Math.min(
|
|
|
|
(presentationWidth) / width,
|
|
|
|
(presentationHeight) / height
|
|
|
|
);
|
|
|
|
|
|
|
|
return (calcedZoom === 0 || calcedZoom === Infinity) ? HUNDRED_PERCENT : calcedZoom;
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
|
|
|
|
2023-01-16 22:35:39 +08:00
|
|
|
const isValidShapeType = (shape) => {
|
2023-01-21 00:16:38 +08:00
|
|
|
const invalidTypes = ['image', 'video'];
|
2023-01-16 22:35:39 +08:00
|
|
|
return !invalidTypes.includes(shape?.type);
|
|
|
|
}
|
|
|
|
|
2023-01-24 04:00:05 +08:00
|
|
|
const filterInvalidShapes = (shapes) => {
|
|
|
|
const keys = Object.keys(shapes);
|
|
|
|
const removedChildren = [];
|
2023-01-27 21:28:20 +08:00
|
|
|
const removedParents = [];
|
2023-01-24 04:00:05 +08:00
|
|
|
|
|
|
|
keys.forEach((shape) => {
|
|
|
|
if (shapes[shape].parentId !== curPageId) {
|
|
|
|
if(!keys.includes(shapes[shape].parentId)) {
|
|
|
|
delete shapes[shape];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if (shapes[shape].type === "group") {
|
|
|
|
const groupChildren = shapes[shape].children;
|
|
|
|
|
|
|
|
groupChildren.forEach((child) => {
|
|
|
|
if (!keys.includes(child)) {
|
|
|
|
removedChildren.push(child);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
shapes[shape].children = groupChildren.filter((child) => !removedChildren.includes(child));
|
|
|
|
|
|
|
|
if (shapes[shape].children.length < 2) {
|
2023-01-27 21:28:20 +08:00
|
|
|
removedParents.push(shape);
|
2023-01-24 04:00:05 +08:00
|
|
|
delete shapes[shape];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-01-27 21:28:20 +08:00
|
|
|
// remove orphaned children
|
|
|
|
Object.keys(shapes).forEach((shape) => {
|
|
|
|
if (shapes[shape] && shapes[shape].parentId !== curPageId) {
|
|
|
|
if (removedParents.includes(shapes[shape].parentId)) {
|
|
|
|
delete shapes[shape];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-01-24 04:00:05 +08:00
|
|
|
return shapes;
|
|
|
|
}
|
|
|
|
|
2022-10-21 22:05:31 +08:00
|
|
|
const sendShapeChanges= (app, changedShapes, redo = false) => {
|
|
|
|
const invalidChange = Object.keys(changedShapes)
|
2023-01-16 22:35:39 +08:00
|
|
|
.find(id => !hasShapeAccess(id));
|
|
|
|
|
|
|
|
const invalidShapeType = Object.keys(changedShapes)
|
|
|
|
.find(id => !isValidShapeType(changedShapes[id]));
|
|
|
|
|
2023-02-03 20:11:07 +08:00
|
|
|
const currentShapes = app?.document?.pages[app?.currentPageId]?.shapes;
|
|
|
|
// -1 for background shape
|
|
|
|
const shapeNumberExceeded = Object.keys(currentShapes).length - 1 > maxNumberOfAnnotations;
|
|
|
|
|
2023-02-03 03:47:16 +08:00
|
|
|
const isInserting = Object.keys(changedShapes)
|
|
|
|
.filter(
|
|
|
|
shape => typeof changedShapes[shape] === 'object'
|
|
|
|
&& changedShapes[shape].type
|
2023-02-03 20:11:07 +08:00
|
|
|
&& !prevShapes[shape]
|
2023-02-03 03:47:16 +08:00
|
|
|
).length !== 0;
|
|
|
|
|
|
|
|
if (invalidChange || invalidShapeType || (shapeNumberExceeded && isInserting)) {
|
|
|
|
if (shapeNumberExceeded) {
|
|
|
|
notifyShapeNumberExceeded(intl, maxNumberOfAnnotations);
|
|
|
|
} else {
|
|
|
|
notifyNotAllowedChange(intl);
|
|
|
|
}
|
2022-10-21 22:05:31 +08:00
|
|
|
// undo last command without persisting to not generate the onUndo/onRedo callback
|
|
|
|
if (!redo) {
|
|
|
|
const command = app.stack[app.pointer];
|
|
|
|
app.pointer--;
|
|
|
|
return app.applyPatch(command.before, `undo`);
|
|
|
|
} else {
|
|
|
|
app.pointer++
|
|
|
|
const command = app.stack[app.pointer]
|
|
|
|
return app.applyPatch(command.after, 'redo');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let deletedShapes = [];
|
|
|
|
Object.entries(changedShapes)
|
|
|
|
.forEach(([id, shape]) => {
|
|
|
|
if (!shape) deletedShapes.push(id);
|
|
|
|
else {
|
|
|
|
//checks to find any bindings assosiated with the changed shapes.
|
|
|
|
//If any, they may need to be updated as well.
|
|
|
|
const pageBindings = app.page.bindings;
|
|
|
|
if (pageBindings) {
|
|
|
|
Object.entries(pageBindings).map(([k,b]) => {
|
|
|
|
if (b.toId.includes(id)) {
|
|
|
|
const boundShape = app.getShape(b.fromId);
|
|
|
|
if (shapes[b.fromId] && !_.isEqual(boundShape, shapes[b.fromId])) {
|
|
|
|
const shapeBounds = app.getShapeBounds(b.fromId);
|
|
|
|
boundShape.size = [shapeBounds.width, shapeBounds.height];
|
|
|
|
persistShape(boundShape, whiteboardId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!shape.id) {
|
|
|
|
// check it already exists (otherwise we need the full shape)
|
|
|
|
if (!shapes[id]) {
|
|
|
|
shape = app.getShape(id);
|
|
|
|
}
|
|
|
|
shape.id = id;
|
|
|
|
}
|
|
|
|
const shapeBounds = app.getShapeBounds(id);
|
|
|
|
const size = [shapeBounds.width, shapeBounds.height];
|
|
|
|
if (!shapes[id] || (shapes[id] && !_.isEqual(shapes[id].size, size))) {
|
|
|
|
shape.size = size;
|
|
|
|
}
|
|
|
|
if (!shapes[id] || (shapes[id] && !shapes[id].userId)) shape.userId = currentUser?.userId;
|
|
|
|
persistShape(shape, whiteboardId);
|
|
|
|
}
|
|
|
|
});
|
2023-01-10 03:40:47 +08:00
|
|
|
|
|
|
|
//order the ids of shapes being deleted to prevent crash when removing a group shape before its children
|
|
|
|
const orderedDeletedShapes = [];
|
|
|
|
deletedShapes.forEach(eid => {
|
|
|
|
if (shapes[eid]?.type !== 'group') {
|
|
|
|
orderedDeletedShapes.unshift(eid);
|
|
|
|
} else {
|
|
|
|
orderedDeletedShapes.push(eid)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-24 04:00:05 +08:00
|
|
|
if (orderedDeletedShapes.length > 0) {
|
|
|
|
removeShapes(orderedDeletedShapes, whiteboardId);
|
|
|
|
}
|
2022-10-21 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 05:24:06 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
props.setTldrawIsMounting(true);
|
|
|
|
}, []);
|
|
|
|
|
2023-01-18 21:12:16 +08:00
|
|
|
const checkClientBounds = (e) => {
|
|
|
|
if (
|
|
|
|
e.clientX > document.documentElement.clientWidth ||
|
|
|
|
e.clientX < 0 ||
|
|
|
|
e.clientY > document.documentElement.clientHeight ||
|
|
|
|
e.clientY < 0
|
|
|
|
) {
|
|
|
|
if (tldrawAPI?.session) {
|
|
|
|
tldrawAPI?.completeSession?.();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-20 04:27:30 +08:00
|
|
|
const checkVisibility = () => {
|
|
|
|
if (document.visibilityState === 'hidden' && tldrawAPI?.session) {
|
|
|
|
tldrawAPI?.completeSession?.();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-18 21:12:16 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
document.addEventListener('mouseup', checkClientBounds);
|
2023-01-20 04:27:30 +08:00
|
|
|
document.addEventListener('visibilitychange', checkVisibility);
|
2023-01-18 21:12:16 +08:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('mouseup', checkClientBounds);
|
2023-01-20 04:27:30 +08:00
|
|
|
document.removeEventListener('visibilitychange', checkVisibility);
|
2023-01-18 21:12:16 +08:00
|
|
|
};
|
|
|
|
}, [tldrawAPI]);
|
|
|
|
|
2022-05-19 03:35:42 +08:00
|
|
|
const doc = React.useMemo(() => {
|
2022-04-05 22:49:13 +08:00
|
|
|
const currentDoc = rDocument.current;
|
|
|
|
|
2022-05-19 03:35:42 +08:00
|
|
|
let next = { ...currentDoc };
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2022-05-19 03:35:42 +08:00
|
|
|
let changed = false;
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2022-05-19 03:35:42 +08:00
|
|
|
if (next.pageStates[curPageId] && !_.isEqual(prevShapes, shapes)) {
|
2022-11-16 12:12:28 +08:00
|
|
|
const editingShape = tldrawAPI?.getShape(tldrawAPI?.getPageState()?.editingId);
|
2022-11-16 23:01:12 +08:00
|
|
|
|
2022-11-16 12:12:28 +08:00
|
|
|
if (editingShape) {
|
|
|
|
shapes[editingShape?.id] = editingShape;
|
|
|
|
}
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2023-01-10 03:40:47 +08:00
|
|
|
const removed = prevShapes && findRemoved(Object.keys(prevShapes),Object.keys((shapes)));
|
2022-10-25 23:53:19 +08:00
|
|
|
if (removed && removed.length > 0) {
|
2023-01-24 04:00:05 +08:00
|
|
|
const patchedShapes = Object.fromEntries(removed.map((id) => [id, undefined]));
|
|
|
|
|
|
|
|
try {
|
|
|
|
tldrawAPI?.patchState(
|
|
|
|
{
|
|
|
|
document: {
|
|
|
|
pageStates: {
|
|
|
|
[curPageId]: {
|
|
|
|
selectedIds: tldrawAPI?.selectedIds?.filter(id => !removed.includes(id)) || [],
|
|
|
|
},
|
2022-10-25 23:53:19 +08:00
|
|
|
},
|
2023-01-24 04:00:05 +08:00
|
|
|
pages: {
|
|
|
|
[curPageId]: {
|
|
|
|
shapes: patchedShapes,
|
|
|
|
},
|
2022-10-25 23:53:19 +08:00
|
|
|
},
|
2022-10-21 22:05:31 +08:00
|
|
|
},
|
|
|
|
},
|
2023-01-24 04:00:05 +08:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'whiteboard_shapes_remove_error',
|
|
|
|
extraInfo: { error },
|
|
|
|
}, 'Whiteboard catch error on removing shapes');
|
|
|
|
}
|
|
|
|
|
2022-10-25 23:53:19 +08:00
|
|
|
}
|
2023-01-10 03:40:47 +08:00
|
|
|
|
2023-01-24 04:00:05 +08:00
|
|
|
next.pages[curPageId].shapes = filterInvalidShapes(shapes);
|
2022-05-19 03:35:42 +08:00
|
|
|
changed = true;
|
2022-04-26 21:55:05 +08:00
|
|
|
}
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2022-11-09 02:39:16 +08:00
|
|
|
if (curPageId && (!next.assets[`slide-background-asset-${curPageId}`]) || (svgUri && !_.isEqual(prevSvgUri, svgUri))) {
|
2022-10-06 04:43:48 +08:00
|
|
|
next.assets[`slide-background-asset-${curPageId}`] = assets[`slide-background-asset-${curPageId}`]
|
2022-10-21 22:05:31 +08:00
|
|
|
tldrawAPI?.patchState(
|
|
|
|
{
|
|
|
|
document: {
|
|
|
|
assets: assets
|
|
|
|
},
|
2022-05-19 03:35:42 +08:00
|
|
|
},
|
2022-10-21 22:05:31 +08:00
|
|
|
);
|
2023-01-18 01:45:15 +08:00
|
|
|
changed = true;
|
2022-04-05 22:49:13 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 22:05:31 +08:00
|
|
|
if (changed && tldrawAPI) {
|
|
|
|
// merge patch manually (this improves performance and reduce side effects on fast updates)
|
|
|
|
const patch = {
|
|
|
|
document: {
|
|
|
|
pages: {
|
2023-01-24 04:00:05 +08:00
|
|
|
[curPageId]: { shapes: filterInvalidShapes(shapes) }
|
2022-10-21 22:05:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const prevState = tldrawAPI._state;
|
|
|
|
const nextState = Utils.deepMerge(tldrawAPI._state, patch);
|
2023-01-24 04:00:05 +08:00
|
|
|
if(nextState.document.pages[curPageId].shapes) {
|
|
|
|
filterInvalidShapes(nextState.document.pages[curPageId].shapes);
|
|
|
|
}
|
2022-10-25 23:53:19 +08:00
|
|
|
const final = tldrawAPI.cleanup(nextState, prevState, patch, '');
|
|
|
|
tldrawAPI._state = final;
|
2023-01-24 04:00:05 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
tldrawAPI?.forceUpdate();
|
|
|
|
} catch (e) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'whiteboard_shapes_update_error',
|
|
|
|
extraInfo: { error },
|
|
|
|
}, 'Whiteboard catch error on updating shapes');
|
|
|
|
}
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// move poll result text to bottom right
|
2023-01-20 04:56:20 +08:00
|
|
|
if (next.pages[curPageId] && slidePosition) {
|
2022-06-01 03:40:31 +08:00
|
|
|
const pollResults = Object.entries(next.pages[curPageId].shapes)
|
2022-10-21 22:05:31 +08:00
|
|
|
.filter(([id, shape]) => shape.name?.includes("poll-result"))
|
2022-06-01 03:40:31 +08:00
|
|
|
for (const [id, shape] of pollResults) {
|
|
|
|
if (_.isEqual(shape.point, [0, 0])) {
|
|
|
|
const shapeBounds = tldrawAPI?.getShapeBounds(id);
|
|
|
|
if (shapeBounds) {
|
|
|
|
shape.point = [
|
|
|
|
slidePosition.width - shapeBounds.width,
|
|
|
|
slidePosition.height - shapeBounds.height
|
|
|
|
]
|
|
|
|
shape.size = [shapeBounds.width, shapeBounds.height]
|
|
|
|
isPresenter && persistShape(shape, whiteboardId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-04-26 21:55:05 +08:00
|
|
|
}
|
2022-05-19 10:32:32 +08:00
|
|
|
|
2022-05-19 03:35:42 +08:00
|
|
|
return currentDoc;
|
2022-07-08 23:06:00 +08:00
|
|
|
}, [shapes, tldrawAPI, curPageId, slidePosition]);
|
2022-04-11 00:31:12 +08:00
|
|
|
|
2022-07-08 23:06:00 +08:00
|
|
|
// when presentationSizes change, update tldraw camera
|
2022-06-01 03:40:31 +08:00
|
|
|
React.useEffect(() => {
|
2022-09-30 22:35:29 +08:00
|
|
|
if (curPageId && slidePosition && tldrawAPI && presentationWidth > 0 && presentationHeight > 0) {
|
2022-08-16 20:12:43 +08:00
|
|
|
if (prevFitToWidth !== null && fitToWidth !== prevFitToWidth) {
|
|
|
|
const zoom = calculateZoom(slidePosition.width, slidePosition.height)
|
|
|
|
tldrawAPI?.setCamera([0, 0], zoom);
|
2023-01-30 23:05:30 +08:00
|
|
|
const viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(tldrawAPI?.viewport.height, slidePosition.height);
|
2022-09-30 22:35:29 +08:00
|
|
|
setZoom(HUNDRED_PERCENT);
|
|
|
|
zoomChanger(HUNDRED_PERCENT);
|
2022-08-16 20:12:43 +08:00
|
|
|
zoomSlide(parseInt(curPageId), podId, HUNDRED_PERCENT, viewedRegionH, 0, 0);
|
|
|
|
} else {
|
2022-08-24 02:19:17 +08:00
|
|
|
const currentAspectRatio = Math.round((presentationWidth / presentationHeight) * 100) / 100;
|
|
|
|
const previousAspectRatio = Math.round((slidePosition.viewBoxWidth / slidePosition.viewBoxHeight) * 100) / 100;
|
2022-09-30 22:35:29 +08:00
|
|
|
if (fitToWidth && currentAspectRatio !== previousAspectRatio) {
|
|
|
|
// wee need this to ensure tldraw updates the viewport size after re-mounting
|
2022-08-24 02:19:17 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
const zoom = calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight);
|
|
|
|
tldrawAPI.setCamera([slidePosition.x, slidePosition.y], zoom, 'zoomed');
|
|
|
|
}, 50);
|
|
|
|
} else {
|
|
|
|
const zoom = calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight);
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], zoom);
|
|
|
|
}
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-08 23:06:00 +08:00
|
|
|
}, [presentationWidth, presentationHeight, curPageId, document?.documentElement?.dir]);
|
2022-06-01 03:40:31 +08:00
|
|
|
|
2022-09-30 22:35:29 +08:00
|
|
|
React.useEffect(() => {
|
2023-01-20 04:56:20 +08:00
|
|
|
if (presentationWidth > 0 && presentationHeight > 0 && slidePosition) {
|
2022-09-30 22:35:29 +08:00
|
|
|
const cameraZoom = tldrawAPI?.getPageState()?.camera?.zoom;
|
|
|
|
const newzoom = calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight);
|
|
|
|
if (cameraZoom && cameraZoom === 1) {
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], newzoom);
|
|
|
|
} else if (isMounting) {
|
2022-10-19 23:13:08 +08:00
|
|
|
setIsMounting(false);
|
2022-11-23 23:05:57 +08:00
|
|
|
props.setTldrawIsMounting(false);
|
2022-10-19 23:13:08 +08:00
|
|
|
const currentAspectRatio = Math.round((presentationWidth / presentationHeight) * 100) / 100;
|
|
|
|
const previousAspectRatio = Math.round((slidePosition.viewBoxWidth / slidePosition.viewBoxHeight) * 100) / 100;
|
|
|
|
// case where the presenter had fit-to-width enabled and he reloads the page
|
|
|
|
if (!fitToWidth && currentAspectRatio !== previousAspectRatio) {
|
2022-09-30 22:35:29 +08:00
|
|
|
// wee need this to ensure tldraw updates the viewport size after re-mounting
|
|
|
|
setTimeout(() => {
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], newzoom, 'zoomed');
|
|
|
|
}, 50);
|
|
|
|
} else {
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], newzoom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [tldrawAPI?.getPageState()?.camera, presentationWidth, presentationHeight]);
|
|
|
|
|
2022-05-20 01:24:47 +08:00
|
|
|
// change tldraw page when presentation page changes
|
2022-04-26 21:55:05 +08:00
|
|
|
React.useEffect(() => {
|
2023-01-20 04:56:20 +08:00
|
|
|
if (tldrawAPI && curPageId && slidePosition) {
|
2022-06-01 03:40:31 +08:00
|
|
|
tldrawAPI.changePage(curPageId);
|
2022-08-16 20:12:43 +08:00
|
|
|
let zoom = prevSlidePosition
|
|
|
|
? calculateZoom(prevSlidePosition.viewBoxWidth, prevSlidePosition.viewBoxHeight)
|
|
|
|
: calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight)
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], zoom, 'zoomed_previous_page');
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
2022-05-19 03:35:42 +08:00
|
|
|
}, [curPageId]);
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2022-05-20 01:24:47 +08:00
|
|
|
// change tldraw camera when slidePosition changes
|
|
|
|
React.useEffect(() => {
|
2022-06-01 03:40:31 +08:00
|
|
|
if (tldrawAPI && !isPresenter && curPageId && slidePosition) {
|
2022-08-16 20:12:43 +08:00
|
|
|
const zoom = calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight)
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], zoom, 'zoomed');
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
2022-05-20 01:24:47 +08:00
|
|
|
}, [curPageId, slidePosition]);
|
|
|
|
|
2022-08-16 20:12:43 +08:00
|
|
|
// update zoom according to toolbar
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (tldrawAPI && isPresenter && curPageId && slidePosition && zoom !== zoomValue) {
|
|
|
|
const zoomFitSlide = calculateZoom(slidePosition.width, slidePosition.height);
|
|
|
|
const zoomCamera = (zoomFitSlide * zoomValue) / HUNDRED_PERCENT;
|
2022-09-30 22:35:29 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
tldrawAPI?.zoomTo(zoomCamera);
|
|
|
|
}, 50);
|
2022-08-16 20:12:43 +08:00
|
|
|
}
|
|
|
|
}, [zoomValue]);
|
|
|
|
|
2022-10-19 23:13:08 +08:00
|
|
|
// update zoom when presenter changes if the aspectRatio has changed
|
2022-08-19 02:18:02 +08:00
|
|
|
React.useEffect(() => {
|
2022-09-30 22:35:29 +08:00
|
|
|
if (tldrawAPI && isPresenter && curPageId && slidePosition && !isMounting) {
|
2022-08-24 02:19:17 +08:00
|
|
|
const currentAspectRatio = Math.round((presentationWidth / presentationHeight) * 100) / 100;
|
|
|
|
const previousAspectRatio = Math.round((slidePosition.viewBoxWidth / slidePosition.viewBoxHeight) * 100) / 100;
|
2022-10-19 23:13:08 +08:00
|
|
|
if (previousAspectRatio !== currentAspectRatio) {
|
|
|
|
if (fitToWidth) {
|
|
|
|
const zoom = calculateZoom(slidePosition.width, slidePosition.height)
|
|
|
|
tldrawAPI?.setCamera([0, 0], zoom);
|
2023-01-30 23:05:30 +08:00
|
|
|
const viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(tldrawAPI?.viewport.height, slidePosition.height);
|
2022-10-19 23:13:08 +08:00
|
|
|
zoomSlide(parseInt(curPageId), podId, HUNDRED_PERCENT, viewedRegionH, 0, 0);
|
|
|
|
setZoom(HUNDRED_PERCENT);
|
|
|
|
zoomChanger(HUNDRED_PERCENT);
|
|
|
|
} else if (!isMounting) {
|
2023-01-30 23:05:30 +08:00
|
|
|
let viewedRegionW = SlideCalcUtil.calcViewedRegionWidth(tldrawAPI?.viewport.width, slidePosition.width);
|
|
|
|
let viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(tldrawAPI?.viewport.height, slidePosition.height);
|
2022-10-19 23:13:08 +08:00
|
|
|
const camera = tldrawAPI?.getPageState()?.camera;
|
|
|
|
const zoomFitSlide = calculateZoom(slidePosition.width, slidePosition.height);
|
|
|
|
if (!fitToWidth && camera.zoom === zoomFitSlide) {
|
|
|
|
viewedRegionW = HUNDRED_PERCENT;
|
|
|
|
viewedRegionH = HUNDRED_PERCENT;
|
|
|
|
}
|
|
|
|
zoomSlide(parseInt(curPageId), podId, viewedRegionW, viewedRegionH, camera.point[0], camera.point[1]);
|
|
|
|
const zoomToolbar = Math.round((HUNDRED_PERCENT * camera.zoom) / zoomFitSlide * 100) / 100;
|
|
|
|
if (zoom !== zoomToolbar) {
|
|
|
|
setZoom(zoomToolbar);
|
|
|
|
zoomChanger(zoomToolbar);
|
|
|
|
}
|
2022-09-30 22:35:29 +08:00
|
|
|
}
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-19 02:18:02 +08:00
|
|
|
}, [isPresenter]);
|
2022-05-20 01:24:47 +08:00
|
|
|
|
2022-08-03 22:30:22 +08:00
|
|
|
const hasWBAccess = props?.hasMultiUserAccess(props.whiteboardId, props.currentUser.userId);
|
|
|
|
|
2022-08-15 06:49:39 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (tldrawAPI) {
|
|
|
|
tldrawAPI.isForcePanning = isPanning;
|
|
|
|
}
|
|
|
|
}, [isPanning]);
|
|
|
|
|
2022-11-01 00:42:38 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
tldrawAPI?.setSetting('language', language);
|
|
|
|
}, [language]);
|
|
|
|
|
2022-12-17 03:00:02 +08:00
|
|
|
// Reset zoom to default when current presentation changes.
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (isPresenter && slidePosition && tldrawAPI) {
|
2023-01-27 21:05:08 +08:00
|
|
|
tldrawAPI.zoomTo(0);
|
2022-12-17 03:00:02 +08:00
|
|
|
}
|
|
|
|
}, [curPres?.id]);
|
|
|
|
|
2023-01-26 02:49:09 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
const currentZoom = tldrawAPI?.getPageState()?.camera?.zoom;
|
|
|
|
|
|
|
|
if(currentZoom !== tldrawZoom) {
|
|
|
|
setTldrawZoom(currentZoom);
|
|
|
|
}else{
|
|
|
|
throttledResetCurrentPoint.current();
|
|
|
|
}
|
|
|
|
}, [presentationAreaHeight, presentationAreaWidth]);
|
|
|
|
|
2023-02-06 21:37:34 +08:00
|
|
|
const fullscreenToggleHandler = () => {
|
|
|
|
const {
|
|
|
|
fullscreenElementId,
|
|
|
|
isFullscreen,
|
|
|
|
layoutContextDispatch,
|
|
|
|
fullscreenAction,
|
|
|
|
fullscreenRef,
|
|
|
|
handleToggleFullScreen,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
handleToggleFullScreen(fullscreenRef);
|
|
|
|
const newElement = isFullscreen ? '' : fullscreenElementId;
|
|
|
|
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: fullscreenAction,
|
|
|
|
value: {
|
|
|
|
element: newElement,
|
|
|
|
group: '',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextSlideHandler = (event) => {
|
|
|
|
const {
|
|
|
|
nextSlide, curPageId, numberOfSlides, podId,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
if (event) event.currentTarget.blur();
|
|
|
|
nextSlide(+curPageId, numberOfSlides, podId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const previousSlideHandler = (event) => {
|
|
|
|
const { previousSlide, curPageId, podId } = props;
|
|
|
|
|
|
|
|
if (event) event.currentTarget.blur();
|
|
|
|
previousSlide(+curPageId, podId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const switchSlide = (event) => {
|
|
|
|
const { which } = event;
|
|
|
|
|
|
|
|
switch (which) {
|
|
|
|
case KEY_CODES.ARROW_LEFT:
|
|
|
|
case KEY_CODES.PAGE_UP:
|
|
|
|
previousSlideHandler();
|
|
|
|
break;
|
|
|
|
case KEY_CODES.ARROW_RIGHT:
|
|
|
|
case KEY_CODES.PAGE_DOWN:
|
|
|
|
nextSlideHandler();
|
|
|
|
break;
|
|
|
|
case KEY_CODES.ENTER:
|
|
|
|
fullscreenToggleHandler();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 02:50:13 +08:00
|
|
|
const onMount = (app) => {
|
2022-10-27 21:15:09 +08:00
|
|
|
const menu = document.getElementById("TD-Styles")?.parentElement;
|
|
|
|
if (menu) {
|
2022-10-28 01:16:44 +08:00
|
|
|
const MENU_OFFSET = `48px`;
|
2022-10-27 21:15:09 +08:00
|
|
|
menu.style.position = `relative`;
|
2022-10-28 01:16:44 +08:00
|
|
|
if (isRTL) {
|
|
|
|
menu.style.left = MENU_OFFSET;
|
|
|
|
} else {
|
|
|
|
menu.style.right = MENU_OFFSET;
|
|
|
|
}
|
|
|
|
|
2022-10-27 21:15:09 +08:00
|
|
|
[...menu.children]
|
|
|
|
.sort((a,b)=> a?.id>b?.id?-1:1)
|
|
|
|
.forEach(n=> menu.appendChild(n));
|
|
|
|
}
|
2022-11-01 00:42:38 +08:00
|
|
|
app.setSetting('language', language);
|
2022-11-14 02:15:17 +08:00
|
|
|
app?.setSetting('isDarkMode', false);
|
|
|
|
app?.patchState(
|
|
|
|
{
|
|
|
|
appState: {
|
|
|
|
currentStyle: {
|
|
|
|
textAlign: isRTL ? "end" : "start",
|
2023-01-16 19:40:42 +08:00
|
|
|
font: fontFamily,
|
2022-11-14 02:15:17 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-07-21 02:50:13 +08:00
|
|
|
setTLDrawAPI(app);
|
|
|
|
props.setTldrawAPI(app);
|
|
|
|
// disable for non presenter that doesn't have multi user access
|
|
|
|
if (!hasWBAccess && !isPresenter) {
|
|
|
|
app.onPan = () => {};
|
|
|
|
app.setSelectedIds = () => {};
|
|
|
|
app.setHoveredId = () => {};
|
|
|
|
}
|
2022-06-26 02:47:15 +08:00
|
|
|
|
2022-07-21 02:50:13 +08:00
|
|
|
if (curPageId) {
|
|
|
|
app.changePage(curPageId);
|
2022-10-19 23:13:08 +08:00
|
|
|
setIsMounting(true);
|
2022-07-21 02:50:13 +08:00
|
|
|
}
|
2022-11-01 18:46:17 +08:00
|
|
|
|
|
|
|
if (history) {
|
|
|
|
app.replaceHistory(history);
|
|
|
|
}
|
2022-07-21 02:50:13 +08:00
|
|
|
};
|
2022-05-03 22:47:45 +08:00
|
|
|
|
2022-08-03 22:19:12 +08:00
|
|
|
const onPatch = (e, t, reason) => {
|
2023-01-26 21:11:31 +08:00
|
|
|
if (!e?.pageState) return;
|
|
|
|
|
2022-10-21 22:05:31 +08:00
|
|
|
// don't allow select others shapes for editing if don't have permission
|
|
|
|
if (reason && reason.includes("set_editing_id")) {
|
|
|
|
if (!hasShapeAccess(e.pageState.editingId)) {
|
|
|
|
e.pageState.editingId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't allow hover others shapes for editing if don't have permission
|
|
|
|
if (reason && reason.includes("set_hovered_id")) {
|
|
|
|
if (!hasShapeAccess(e.pageState.hoveredId)) {
|
|
|
|
e.pageState.hoveredId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't allow select others shapes if don't have permission
|
|
|
|
if (reason && reason.includes("selected")) {
|
|
|
|
const validIds = [];
|
|
|
|
e.pageState.selectedIds.forEach(id => hasShapeAccess(id) && validIds.push(id));
|
|
|
|
e.pageState.selectedIds = validIds;
|
|
|
|
e.patchState(
|
|
|
|
{
|
|
|
|
document: {
|
|
|
|
pageStates: {
|
|
|
|
[e.getPage()?.id]: {
|
|
|
|
selectedIds: validIds,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// don't allow selecting others shapes with ctrl (brush)
|
|
|
|
if (e?.session?.type === "brush" && e?.session?.status === "brushing") {
|
|
|
|
const validIds = [];
|
|
|
|
e.pageState.selectedIds.forEach(id => hasShapeAccess(id) && validIds.push(id));
|
|
|
|
e.pageState.selectedIds = validIds;
|
|
|
|
if (!validIds.find(id => id === e.pageState.hoveredId)) {
|
|
|
|
e.pageState.hoveredId = undefined;
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 21:15:09 +08:00
|
|
|
|
2023-01-20 04:56:20 +08:00
|
|
|
if (reason && isPresenter && slidePosition && (reason.includes("zoomed") || reason.includes("panned"))) {
|
2022-07-21 03:20:46 +08:00
|
|
|
const camera = tldrawAPI.getPageState()?.camera;
|
2022-08-16 20:12:43 +08:00
|
|
|
|
|
|
|
// limit bounds
|
|
|
|
if (tldrawAPI?.viewport.maxX > slidePosition.width) {
|
|
|
|
camera.point[0] = camera.point[0] + (tldrawAPI?.viewport.maxX - slidePosition.width);
|
|
|
|
}
|
|
|
|
if (tldrawAPI?.viewport.maxY > slidePosition.height) {
|
|
|
|
camera.point[1] = camera.point[1] + (tldrawAPI?.viewport.maxY - slidePosition.height);
|
|
|
|
}
|
|
|
|
if (camera.point[0] > 0 || tldrawAPI?.viewport.minX < 0) {
|
|
|
|
camera.point[0] = 0;
|
|
|
|
}
|
|
|
|
if (camera.point[1] > 0 || tldrawAPI?.viewport.minY < 0) {
|
|
|
|
camera.point[1] = 0;
|
2022-07-21 02:50:13 +08:00
|
|
|
}
|
2022-08-16 20:12:43 +08:00
|
|
|
const zoomFitSlide = calculateZoom(slidePosition.width, slidePosition.height);
|
|
|
|
if (camera.zoom < zoomFitSlide) {
|
|
|
|
camera.zoom = zoomFitSlide;
|
|
|
|
}
|
2023-01-27 23:49:32 +08:00
|
|
|
|
2022-08-16 20:12:43 +08:00
|
|
|
tldrawAPI?.setCamera([camera.point[0], camera.point[1]], camera.zoom);
|
|
|
|
|
2022-08-24 02:19:17 +08:00
|
|
|
const zoomToolbar = Math.round((HUNDRED_PERCENT * camera.zoom) / zoomFitSlide * 100) / 100;
|
2022-08-16 20:12:43 +08:00
|
|
|
if (zoom !== zoomToolbar) {
|
|
|
|
setZoom(zoomToolbar);
|
|
|
|
isPresenter && zoomChanger(zoomToolbar);
|
2022-07-21 02:50:13 +08:00
|
|
|
}
|
2022-08-16 20:12:43 +08:00
|
|
|
|
2023-01-30 23:05:30 +08:00
|
|
|
let viewedRegionW = SlideCalcUtil.calcViewedRegionWidth(tldrawAPI?.viewport.width, slidePosition.width);
|
|
|
|
let viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(tldrawAPI?.viewport.height, slidePosition.height);
|
2022-08-24 02:19:17 +08:00
|
|
|
|
|
|
|
if (!fitToWidth && camera.zoom === zoomFitSlide) {
|
|
|
|
viewedRegionW = HUNDRED_PERCENT;
|
|
|
|
viewedRegionH = HUNDRED_PERCENT;
|
|
|
|
}
|
2022-08-16 20:12:43 +08:00
|
|
|
|
|
|
|
zoomSlide(parseInt(curPageId), podId, viewedRegionW, viewedRegionH, camera.point[0], camera.point[1]);
|
2022-07-21 02:50:13 +08:00
|
|
|
}
|
|
|
|
//don't allow non-presenters to pan&zoom
|
|
|
|
if (slidePosition && reason && !isPresenter && (reason.includes("zoomed") || reason.includes("panned"))) {
|
2022-08-16 20:12:43 +08:00
|
|
|
const zoom = calculateZoom(slidePosition.viewBoxWidth, slidePosition.viewBoxHeight)
|
|
|
|
tldrawAPI?.setCamera([slidePosition.x, slidePosition.y], zoom);
|
2022-07-21 02:50:13 +08:00
|
|
|
}
|
2022-09-03 04:59:03 +08:00
|
|
|
// disable select for non presenter that doesn't have multi user access
|
|
|
|
if (!hasWBAccess && !isPresenter) {
|
|
|
|
if (e?.getPageState()?.brush || e?.selectedIds?.length !== 0) {
|
|
|
|
e.patchState(
|
|
|
|
{
|
|
|
|
document: {
|
|
|
|
pageStates: {
|
|
|
|
[e?.currentPageId]: {
|
|
|
|
selectedIds: [],
|
|
|
|
brush: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 03:01:53 +08:00
|
|
|
|
2023-01-15 04:37:43 +08:00
|
|
|
if (reason && reason === 'patched_shapes' && e?.session?.type === 'edit') {
|
2022-09-01 03:01:53 +08:00
|
|
|
const patchedShape = e?.getShape(e?.getPageState()?.editingId);
|
2022-11-24 01:03:20 +08:00
|
|
|
|
2023-01-15 04:37:43 +08:00
|
|
|
if (e?.session?.initialShape?.type === 'sticky' && patchedShape?.text?.length > maxStickyNoteLength) {
|
2022-11-24 01:03:20 +08:00
|
|
|
patchedShape.text = patchedShape.text.substring(0, maxStickyNoteLength);
|
|
|
|
}
|
|
|
|
|
2023-01-15 04:37:43 +08:00
|
|
|
if (e?.session?.initialShape?.type === 'text' && !shapes[patchedShape.id]) {
|
2023-02-03 20:11:07 +08:00
|
|
|
// check for maxShapes
|
|
|
|
const currentShapes = e?.document?.pages[e?.currentPageId]?.shapes;
|
|
|
|
const shapeNumberExceeded = Object.keys(currentShapes).length - 1 > maxNumberOfAnnotations;
|
|
|
|
if (shapeNumberExceeded) {
|
|
|
|
notifyShapeNumberExceeded(intl, maxNumberOfAnnotations);
|
|
|
|
e?.cancelSession?.();
|
|
|
|
} else {
|
|
|
|
patchedShape.userId = currentUser?.userId;
|
|
|
|
persistShape(patchedShape, whiteboardId);
|
|
|
|
}
|
2022-10-21 22:05:31 +08:00
|
|
|
} else {
|
|
|
|
const diff = {
|
|
|
|
id: patchedShape.id,
|
|
|
|
point: patchedShape.point,
|
2023-01-15 04:37:43 +08:00
|
|
|
text: patchedShape.text,
|
|
|
|
};
|
2022-10-21 22:05:31 +08:00
|
|
|
persistShape(diff, whiteboardId);
|
2022-09-01 03:01:53 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-15 06:33:11 +08:00
|
|
|
|
2023-01-12 03:32:03 +08:00
|
|
|
if (reason && reason.includes('selected_tool')) {
|
|
|
|
const tool = reason.split(':')[1];
|
2022-11-24 01:03:20 +08:00
|
|
|
|
2023-01-12 03:32:03 +08:00
|
|
|
setCurrentTool(tool);
|
2022-11-15 06:33:11 +08:00
|
|
|
}
|
2022-07-21 02:50:13 +08:00
|
|
|
};
|
|
|
|
|
2022-10-21 22:05:31 +08:00
|
|
|
const onUndo = (app) => {
|
|
|
|
if (app.currentPageId !== curPageId) {
|
|
|
|
if (isPresenter) {
|
|
|
|
// change slide for others
|
|
|
|
skipToSlide(Number.parseInt(app.currentPageId), podId)
|
|
|
|
} else {
|
|
|
|
// ignore, stay on same page
|
|
|
|
app.changePage(curPageId);
|
2022-09-03 03:26:26 +08:00
|
|
|
}
|
2022-10-21 22:05:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const lastCommand = app.stack[app.pointer+1];
|
|
|
|
const changedShapes = lastCommand?.before?.document?.pages[app.currentPageId]?.shapes;
|
|
|
|
if (changedShapes) {
|
|
|
|
sendShapeChanges(app, changedShapes, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRedo = (app) => {
|
|
|
|
if (app.currentPageId !== curPageId) {
|
|
|
|
if (isPresenter) {
|
|
|
|
// change slide for others
|
|
|
|
skipToSlide(Number.parseInt(app.currentPageId), podId)
|
|
|
|
} else {
|
|
|
|
// ignore, stay on same page
|
|
|
|
app.changePage(curPageId);
|
2022-09-01 03:01:53 +08:00
|
|
|
}
|
2022-10-21 22:05:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const lastCommand = app.stack[app.pointer];
|
|
|
|
const changedShapes = lastCommand?.after?.document?.pages[app.currentPageId]?.shapes;
|
|
|
|
if (changedShapes) {
|
|
|
|
sendShapeChanges(app, changedShapes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-01 18:46:17 +08:00
|
|
|
const onCommand = (app, command, reason) => {
|
|
|
|
setHistory(app.history);
|
2022-10-21 22:05:31 +08:00
|
|
|
const changedShapes = command.after?.document?.pages[app.currentPageId]?.shapes;
|
|
|
|
if (!isMounting && app.currentPageId !== curPageId) {
|
|
|
|
// can happen then the "move to page action" is called, or using undo after changing a page
|
|
|
|
const newWhiteboardId = curPres.pages.find(page => page.num === Number.parseInt(app.currentPageId)).id;
|
|
|
|
//remove from previous page and persist on new
|
|
|
|
changedShapes && removeShapes(Object.keys(changedShapes), whiteboardId);
|
|
|
|
changedShapes && Object.entries(changedShapes)
|
|
|
|
.forEach(([id, shape]) => {
|
|
|
|
const shapeBounds = app.getShapeBounds(id);
|
|
|
|
shape.size = [shapeBounds.width, shapeBounds.height];
|
|
|
|
persistShape(shape, newWhiteboardId);
|
|
|
|
});
|
|
|
|
if (isPresenter) {
|
|
|
|
// change slide for others
|
|
|
|
skipToSlide(Number.parseInt(app.currentPageId), podId)
|
|
|
|
} else {
|
|
|
|
// ignore, stay on same page
|
|
|
|
app.changePage(curPageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (changedShapes) {
|
|
|
|
sendShapeChanges(app, changedShapes);
|
2022-09-01 03:01:53 +08:00
|
|
|
}
|
2022-07-21 02:50:13 +08:00
|
|
|
};
|
|
|
|
|
2022-08-22 22:37:34 +08:00
|
|
|
const webcams = document.getElementById('cameraDock');
|
|
|
|
const dockPos = webcams?.getAttribute("data-position");
|
2023-01-26 02:49:09 +08:00
|
|
|
|
2023-01-31 00:05:35 +08:00
|
|
|
if (currentTool && !isPanning) tldrawAPI?.selectTool(currentTool);
|
2023-01-26 02:49:09 +08:00
|
|
|
|
2022-07-21 02:50:13 +08:00
|
|
|
const editableWB = (
|
2023-02-06 21:37:34 +08:00
|
|
|
<EditableWBWrapper onKeyDown={switchSlide}>
|
2022-12-14 00:11:56 +08:00
|
|
|
<Tldraw
|
|
|
|
key={`wb-${isRTL}-${dockPos}-${forcePanning}`}
|
|
|
|
document={doc}
|
|
|
|
// disable the ability to drag and drop files onto the whiteboard
|
|
|
|
// until we handle saving of assets in akka.
|
|
|
|
disableAssets={true}
|
|
|
|
// Disable automatic focus. Users were losing focus on shared notes
|
|
|
|
// and chat on presentation mount.
|
|
|
|
autofocus={false}
|
|
|
|
onMount={onMount}
|
|
|
|
showPages={false}
|
|
|
|
showZoom={false}
|
|
|
|
showUI={curPres ? (isPresenter || hasWBAccess) : true}
|
|
|
|
showMenu={curPres ? false : true}
|
|
|
|
showMultiplayerMenu={false}
|
|
|
|
readOnly={false}
|
|
|
|
onPatch={onPatch}
|
|
|
|
onUndo={onUndo}
|
|
|
|
onRedo={onRedo}
|
|
|
|
onCommand={onCommand}
|
|
|
|
/>
|
2023-01-12 03:32:03 +08:00
|
|
|
</EditableWBWrapper>
|
2022-07-21 02:50:13 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const readOnlyWB = (
|
|
|
|
<Tldraw
|
2022-07-21 03:54:23 +08:00
|
|
|
key={`wb-readOnly`}
|
2022-07-21 02:50:13 +08:00
|
|
|
document={doc}
|
|
|
|
onMount={onMount}
|
|
|
|
// disable the ability to drag and drop files onto the whiteboard
|
|
|
|
// until we handle saving of assets in akka.
|
|
|
|
disableAssets={true}
|
|
|
|
// Disable automatic focus. Users were losing focus on shared notes
|
|
|
|
// and chat on presentation mount.
|
|
|
|
autofocus={false}
|
|
|
|
showPages={false}
|
|
|
|
showZoom={false}
|
|
|
|
showUI={false}
|
|
|
|
showMenu={false}
|
|
|
|
showMultiplayerMenu={false}
|
|
|
|
readOnly={true}
|
|
|
|
onPatch={onPatch}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2023-01-18 08:22:50 +08:00
|
|
|
const size = ((props.height < SMALL_HEIGHT) || (props.width < SMALL_WIDTH))
|
|
|
|
? TOOLBAR_SMALL : TOOLBAR_LARGE;
|
|
|
|
|
|
|
|
if (isPanning && tldrawAPI) {
|
|
|
|
tldrawAPI.isForcePanning = isPanning;
|
|
|
|
}
|
|
|
|
|
2023-01-23 23:30:59 +08:00
|
|
|
if (hasWBAccess || isPresenter) {
|
|
|
|
if (((props.height < SMALLEST_HEIGHT) || (props.width < SMALLEST_WIDTH))) {
|
|
|
|
tldrawAPI?.setSetting('dockPosition', 'bottom');
|
|
|
|
} else {
|
|
|
|
tldrawAPI?.setSetting('dockPosition', isRTL ? 'left' : 'right');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 02:50:13 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Cursors
|
|
|
|
tldrawAPI={tldrawAPI}
|
|
|
|
currentUser={currentUser}
|
|
|
|
hasMultiUserAccess={props?.hasMultiUserAccess}
|
|
|
|
whiteboardId={whiteboardId}
|
|
|
|
isViewersCursorLocked={isViewersCursorLocked}
|
|
|
|
isMultiUserActive={isMultiUserActive}
|
2022-08-15 21:02:56 +08:00
|
|
|
isPanning={isPanning}
|
2023-01-12 03:32:03 +08:00
|
|
|
currentTool={currentTool}
|
2022-07-21 02:50:13 +08:00
|
|
|
>
|
2023-01-26 02:49:09 +08:00
|
|
|
{enable && (hasWBAccess || isPresenter) ? editableWB : readOnlyWB}
|
2023-01-18 08:22:50 +08:00
|
|
|
<TldrawGlobalStyle
|
|
|
|
hasWBAccess={hasWBAccess}
|
|
|
|
isPresenter={isPresenter}
|
|
|
|
hideContextMenu={!hasWBAccess && !isPresenter}
|
|
|
|
size={size}
|
2022-11-01 01:07:03 +08:00
|
|
|
/>
|
2022-04-26 21:55:05 +08:00
|
|
|
</Cursors>
|
|
|
|
</>
|
2022-04-05 22:49:13 +08:00
|
|
|
);
|
|
|
|
}
|