2024-09-18 23:21:16 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useRef, useCallback } from 'react';
|
2024-10-05 00:28:06 +08:00
|
|
|
import { isEqual } from 'radash';
|
2024-03-25 01:22:49 +08:00
|
|
|
import {
|
|
|
|
Tldraw,
|
|
|
|
DefaultColorStyle,
|
|
|
|
DefaultDashStyle,
|
|
|
|
DefaultFillStyle,
|
|
|
|
DefaultFontStyle,
|
|
|
|
DefaultSizeStyle,
|
2024-04-04 10:00:56 +08:00
|
|
|
InstancePresenceRecordType,
|
2024-05-01 00:40:43 +08:00
|
|
|
setDefaultUiAssetUrls,
|
|
|
|
setDefaultEditorAssetUrls,
|
2024-09-18 23:21:16 +08:00
|
|
|
} from '@bigbluebutton/tldraw';
|
|
|
|
import '@bigbluebutton/tldraw/tldraw.css';
|
2023-03-10 19:30:46 +08:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
2024-09-18 23:21:16 +08:00
|
|
|
import { compressToBase64, decompressFromBase64 } from 'lz-string';
|
|
|
|
import SlideCalcUtil, { HUNDRED_PERCENT } from '/imports/utils/slideCalcUtils';
|
2024-06-08 04:13:25 +08:00
|
|
|
import meetingClientSettingsInitialValues from '/imports/ui/core/initial-values/meetingClientSettings';
|
|
|
|
import getFromUserSettings from '/imports/ui/services/users-settings';
|
2024-09-18 23:21:16 +08:00
|
|
|
import KEY_CODES from '/imports/utils/keyCodes';
|
|
|
|
import Styled from './styles';
|
2023-03-10 19:30:46 +08:00
|
|
|
import {
|
2024-04-18 03:04:31 +08:00
|
|
|
mapLanguage,
|
|
|
|
isValidShapeType,
|
2024-09-18 23:21:16 +08:00
|
|
|
usePrevious,
|
2024-09-19 23:38:57 +08:00
|
|
|
getDifferences,
|
2024-09-18 23:21:16 +08:00
|
|
|
} from './utils';
|
|
|
|
import { useMouseEvents, useCursor } from './hooks';
|
|
|
|
import { notifyShapeNumberExceeded, getCustomEditorAssetUrls, getCustomAssetUrls } from './service';
|
2022-11-01 00:42:38 +08:00
|
|
|
|
2024-05-02 08:40:58 +08:00
|
|
|
import NoopTool from './custom-tools/noop-tool/component';
|
2024-04-11 08:53:42 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const CAMERA_TYPE = 'camera';
|
2024-07-10 02:36:06 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
// Helper functions
|
|
|
|
const deleteLocalStorageItemsWithPrefix = (prefix) => {
|
2024-09-18 23:21:16 +08:00
|
|
|
const keysToRemove = Object.keys(localStorage).filter((key) => key.startsWith(prefix));
|
2023-12-06 02:15:25 +08:00
|
|
|
keysToRemove.forEach((key) => localStorage.removeItem(key));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Example of typical LocalStorage entry tldraw creates:
|
|
|
|
// `{ TLDRAW_USER_DATA_v3: '{"version":2,"user":{"id":"epDk1 ...`
|
|
|
|
const clearTldrawCache = () => {
|
2024-09-18 23:21:16 +08:00
|
|
|
deleteLocalStorageItemsWithPrefix('TLDRAW');
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
2024-07-10 02:36:06 +08:00
|
|
|
const createCamera = (pageId, zoomLevel) => ({
|
|
|
|
id: `camera:page:${pageId}`,
|
|
|
|
meta: {},
|
|
|
|
typeName: CAMERA_TYPE,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
z: zoomLevel,
|
|
|
|
});
|
|
|
|
|
2024-06-11 21:05:08 +08:00
|
|
|
const defaultUser = {
|
|
|
|
userId: '',
|
|
|
|
};
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const Whiteboard = React.memo((props) => {
|
2022-04-05 22:49:13 +08:00
|
|
|
const {
|
2024-06-11 21:05:08 +08:00
|
|
|
isPresenter = false,
|
2022-05-04 20:40:56 +08:00
|
|
|
removeShapes,
|
2024-01-24 19:37:51 +08:00
|
|
|
persistShapeWrapper,
|
2022-04-05 22:49:13 +08:00
|
|
|
shapes,
|
2022-10-06 04:43:48 +08:00
|
|
|
assets,
|
2024-06-11 21:05:08 +08:00
|
|
|
currentUser = defaultUser,
|
|
|
|
whiteboardId = undefined,
|
2022-05-12 05:58:16 +08:00
|
|
|
zoomSlide,
|
2024-09-13 01:34:36 +08:00
|
|
|
curPageNum: curPageId,
|
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,
|
2024-01-20 22:01:20 +08:00
|
|
|
colorStyle,
|
|
|
|
dashStyle,
|
|
|
|
fillStyle,
|
|
|
|
fontStyle,
|
|
|
|
sizeStyle,
|
2023-01-26 02:49:09 +08:00
|
|
|
presentationAreaHeight,
|
|
|
|
presentationAreaWidth,
|
2023-03-10 19:30:46 +08:00
|
|
|
setTldrawIsMounting,
|
2023-03-13 20:17:14 +08:00
|
|
|
setTldrawAPI,
|
2023-03-27 06:08:09 +08:00
|
|
|
whiteboardToolbarAutoHide,
|
|
|
|
toggleToolsAnimations,
|
2023-04-14 23:27:37 +08:00
|
|
|
animations,
|
2023-04-19 02:03:22 +08:00
|
|
|
isToolbarVisible,
|
2023-06-06 04:02:06 +08:00
|
|
|
isModerator,
|
2023-09-28 04:42:47 +08:00
|
|
|
currentPresentationPage,
|
2024-06-11 21:05:08 +08:00
|
|
|
presentationId = undefined,
|
2023-10-12 04:17:10 +08:00
|
|
|
hasWBAccess,
|
2023-12-06 02:15:25 +08:00
|
|
|
bgShape,
|
|
|
|
publishCursorUpdate,
|
|
|
|
otherCursors,
|
|
|
|
hideViewersCursor,
|
2024-10-05 00:28:06 +08:00
|
|
|
presentationWidth,
|
2024-02-01 02:53:05 +08:00
|
|
|
presentationHeight,
|
2024-02-27 01:06:18 +08:00
|
|
|
skipToSlide,
|
2024-04-16 20:45:22 +08:00
|
|
|
intl,
|
|
|
|
maxNumberOfAnnotations,
|
2024-04-18 03:04:31 +08:00
|
|
|
notifyNotAllowedChange,
|
2024-05-10 11:48:22 +08:00
|
|
|
locale,
|
|
|
|
darkTheme,
|
|
|
|
selectedLayout,
|
2024-07-06 04:09:19 +08:00
|
|
|
isInfiniteWhiteboard,
|
2024-07-19 01:00:28 +08:00
|
|
|
whiteboardWriters,
|
2022-04-05 22:49:13 +08:00
|
|
|
} = props;
|
2023-10-13 02:29:29 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
clearTldrawCache();
|
|
|
|
|
|
|
|
const [tlEditor, setTlEditor] = React.useState(null);
|
2022-08-16 20:12:43 +08:00
|
|
|
const [isMounting, setIsMounting] = React.useState(true);
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
if (isMounting) {
|
|
|
|
setDefaultEditorAssetUrls(getCustomEditorAssetUrls());
|
|
|
|
setDefaultUiAssetUrls(getCustomAssetUrls());
|
|
|
|
}
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const whiteboardRef = React.useRef(null);
|
|
|
|
const zoomValueRef = React.useRef(null);
|
2023-12-12 03:31:21 +08:00
|
|
|
const prevShapesRef = React.useRef(shapes);
|
2023-12-06 02:15:25 +08:00
|
|
|
const tlEditorRef = React.useRef(tlEditor);
|
|
|
|
const slideChanged = React.useRef(false);
|
|
|
|
const slideNext = React.useRef(null);
|
|
|
|
const prevZoomValueRef = React.useRef(null);
|
|
|
|
const initialZoomRef = useRef(null);
|
|
|
|
const isMouseDownRef = useRef(false);
|
2024-09-18 03:57:25 +08:00
|
|
|
const shapeBatchRef = useRef({});
|
2023-12-06 02:15:25 +08:00
|
|
|
const isMountedRef = useRef(false);
|
2024-01-09 22:31:08 +08:00
|
|
|
const isWheelZoomRef = useRef(false);
|
2024-02-19 06:10:04 +08:00
|
|
|
const isPresenterRef = useRef(isPresenter);
|
2024-10-05 00:28:06 +08:00
|
|
|
const fitToWidthRef = useRef(fitToWidth);
|
2024-02-08 01:07:40 +08:00
|
|
|
const whiteboardIdRef = React.useRef(whiteboardId);
|
|
|
|
const curPageIdRef = React.useRef(curPageId);
|
2024-02-08 04:20:06 +08:00
|
|
|
const hasWBAccessRef = React.useRef(hasWBAccess);
|
2024-02-29 00:50:14 +08:00
|
|
|
const isModeratorRef = React.useRef(isModerator);
|
2024-04-13 04:39:53 +08:00
|
|
|
const currentPresentationPageRef = React.useRef(currentPresentationPage);
|
|
|
|
const initialViewBoxWidthRef = React.useRef(null);
|
2024-07-01 22:19:42 +08:00
|
|
|
const initialViewBoxHeightRef = React.useRef(null);
|
2024-06-17 21:50:15 +08:00
|
|
|
const previousTool = React.useRef(null);
|
2023-08-28 02:45:37 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const THRESHOLD = 0.1;
|
2024-08-07 22:00:41 +08:00
|
|
|
const CAMERA_UPDATE_DELAY = 650;
|
2023-12-06 02:15:25 +08:00
|
|
|
const lastKnownHeight = React.useRef(presentationAreaHeight);
|
|
|
|
const lastKnownWidth = React.useRef(presentationAreaWidth);
|
2023-02-25 07:00:15 +08:00
|
|
|
|
2024-09-24 01:24:22 +08:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const [shapesVersion, setShapesVersion] = React.useState(0);
|
2024-06-11 05:12:40 +08:00
|
|
|
const customTools = [NoopTool];
|
|
|
|
|
2024-07-04 01:05:06 +08:00
|
|
|
const presenterChanged = usePrevious(isPresenter) !== isPresenter;
|
|
|
|
|
2024-06-25 21:57:10 +08:00
|
|
|
let clipboardContent = null;
|
|
|
|
let isPasting = false;
|
|
|
|
let pasteTimeout = null;
|
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
const setIsMouseDown = (val) => {
|
|
|
|
isMouseDownRef.current = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setIsWheelZoom = (val) => {
|
|
|
|
isWheelZoomRef.current = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setWheelZoomTimeout = () => {
|
|
|
|
isWheelZoomRef.currentTimeout = setTimeout(() => {
|
|
|
|
setIsWheelZoom(false);
|
|
|
|
}, 300);
|
|
|
|
};
|
|
|
|
|
2024-04-13 04:39:53 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
currentPresentationPageRef.current = currentPresentationPage;
|
|
|
|
}, [currentPresentationPage]);
|
|
|
|
|
2024-02-08 01:07:40 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
curPageIdRef.current = curPageId;
|
|
|
|
}, [curPageId]);
|
|
|
|
|
2024-02-29 00:50:14 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
isModeratorRef.current = isModerator;
|
|
|
|
}, [isModerator]);
|
|
|
|
|
2024-02-08 01:07:40 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
whiteboardIdRef.current = whiteboardId;
|
|
|
|
}, [whiteboardId]);
|
2024-02-08 04:20:06 +08:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
hasWBAccessRef.current = hasWBAccess;
|
|
|
|
|
|
|
|
if (!hasWBAccess && !isPresenter) {
|
2024-05-23 04:19:44 +08:00
|
|
|
tlEditorRef?.current?.setCurrentTool('noop');
|
|
|
|
} else if (hasWBAccess && !isPresenter) {
|
|
|
|
tlEditorRef?.current?.setCurrentTool('draw');
|
2024-02-08 04:20:06 +08:00
|
|
|
}
|
|
|
|
}, [hasWBAccess]);
|
2024-02-08 01:07:40 +08:00
|
|
|
|
2024-02-19 06:10:04 +08:00
|
|
|
React.useEffect(() => {
|
2024-09-18 23:21:16 +08:00
|
|
|
isPresenterRef.current = isPresenter;
|
2024-02-19 06:10:04 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (!hasWBAccessRef.current && !isPresenter) {
|
|
|
|
tlEditorRef?.current?.setCurrentTool('noop');
|
|
|
|
}
|
2024-04-11 08:53:42 +08:00
|
|
|
}, [isPresenter]);
|
2024-02-19 06:10:04 +08:00
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
fitToWidthRef.current = fitToWidth;
|
|
|
|
}, [fitToWidth]);
|
|
|
|
|
2023-12-12 03:31:21 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!isEqual(prevShapesRef.current, shapes)) {
|
|
|
|
prevShapesRef.current = shapes;
|
2024-09-24 01:24:22 +08:00
|
|
|
setShapesVersion((v) => v + 1);
|
2023-02-24 07:47:34 +08:00
|
|
|
}
|
2023-12-12 03:31:21 +08:00
|
|
|
}, [shapes]);
|
2023-02-24 07:47:34 +08:00
|
|
|
|
2024-06-25 21:57:10 +08:00
|
|
|
const handleCopy = useCallback(() => {
|
|
|
|
const selectedShapes = tlEditorRef.current?.getSelectedShapes();
|
|
|
|
if (!selectedShapes || selectedShapes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-18 23:21:16 +08:00
|
|
|
const content = tlEditorRef.current?.getContentFromCurrentPage(
|
|
|
|
selectedShapes.map((shape) => shape.id),
|
|
|
|
);
|
2024-06-25 21:57:10 +08:00
|
|
|
if (content) {
|
|
|
|
clipboardContent = content;
|
|
|
|
const stringifiedClipboard = compressToBase64(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'application/tldraw',
|
|
|
|
kind: 'content',
|
|
|
|
data: content,
|
2024-09-18 23:21:16 +08:00
|
|
|
}),
|
2024-06-25 21:57:10 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (navigator.clipboard?.write) {
|
|
|
|
const htmlBlob = new Blob([`<tldraw>${stringifiedClipboard}</tldraw>`], {
|
|
|
|
type: 'text/html',
|
|
|
|
});
|
|
|
|
|
|
|
|
navigator.clipboard.write([
|
|
|
|
new ClipboardItem({
|
|
|
|
'text/html': htmlBlob,
|
|
|
|
'text/plain': new Blob([''], { type: 'text/plain' }),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
} else if (navigator.clipboard.writeText) {
|
|
|
|
navigator.clipboard.writeText(`<tldraw>${stringifiedClipboard}</tldraw>`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [tlEditorRef]);
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const handleCut = useCallback((shouldCopy) => {
|
|
|
|
const selectedShapes = tlEditorRef.current?.getSelectedShapes();
|
|
|
|
if (!selectedShapes || selectedShapes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (shouldCopy) {
|
|
|
|
handleCopy();
|
|
|
|
}
|
|
|
|
tlEditorRef.current?.deleteShapes(selectedShapes.map((shape) => shape.id));
|
|
|
|
}, [tlEditorRef]);
|
|
|
|
|
|
|
|
const pasteTldrawContent = (editor, clipboard, point) => {
|
|
|
|
const p = point ?? (editor.inputs.shiftKey ? editor.inputs.currentPagePoint : undefined);
|
|
|
|
editor.mark('paste');
|
|
|
|
editor.putContentOntoCurrentPage(clipboard, {
|
|
|
|
point: p,
|
|
|
|
select: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-06-25 21:57:10 +08:00
|
|
|
const handlePaste = useCallback(() => {
|
|
|
|
if (isPasting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isPasting = true;
|
|
|
|
|
|
|
|
clearTimeout(pasteTimeout);
|
|
|
|
pasteTimeout = setTimeout(() => {
|
|
|
|
if (clipboardContent) {
|
|
|
|
pasteTldrawContent(tlEditorRef.current, clipboardContent);
|
|
|
|
isPasting = false;
|
|
|
|
} else {
|
|
|
|
navigator.clipboard.readText().then((text) => {
|
|
|
|
const match = text.match(/<tldraw>(.*)<\/tldraw>/);
|
|
|
|
if (match && match[1]) {
|
|
|
|
const content = JSON.parse(decompressFromBase64(match[1]));
|
|
|
|
pasteTldrawContent(tlEditorRef.current, content);
|
|
|
|
}
|
|
|
|
isPasting = false;
|
2024-09-18 23:21:16 +08:00
|
|
|
}).catch(() => {
|
2024-06-25 21:57:10 +08:00
|
|
|
isPasting = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}, [tlEditorRef]);
|
|
|
|
|
|
|
|
const handleKeyDown = useCallback((event) => {
|
2024-10-03 21:20:15 +08:00
|
|
|
if (event.repeat) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
2024-10-05 00:28:06 +08:00
|
|
|
// ignore if the edit link dialog is open
|
|
|
|
if (document.querySelector('h2.tlui-dialog__header__title')?.textContent === 'Edit link') {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-26 03:16:23 +08:00
|
|
|
|
2024-10-03 21:20:15 +08:00
|
|
|
const key = event.key.toLowerCase();
|
|
|
|
|
|
|
|
if (key === 'escape' || event.keyCode === 27) {
|
2024-06-06 06:15:43 +08:00
|
|
|
tlEditorRef.current?.deselect(...tlEditorRef.current?.getSelectedShapes());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-11 03:42:08 +08:00
|
|
|
const editingShape = tlEditorRef.current?.getEditingShape();
|
|
|
|
if (editingShape && (isPresenterRef.current || hasWBAccessRef.current)) {
|
2024-06-26 02:52:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:20:15 +08:00
|
|
|
if (key === 'delete') {
|
2024-07-11 03:42:08 +08:00
|
|
|
handleCut(false);
|
2024-06-06 06:15:43 +08:00
|
|
|
return;
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
2024-06-05 01:25:09 +08:00
|
|
|
|
2024-10-03 21:20:15 +08:00
|
|
|
if (key === ' ' && tlEditorRef.current?.getCurrentToolId() !== 'hand' && isPresenterRef.current) {
|
2024-06-17 21:50:15 +08:00
|
|
|
previousTool.current = tlEditorRef.current?.getCurrentToolId();
|
2024-09-18 23:21:16 +08:00
|
|
|
tlEditorRef.current?.setCurrentTool('hand');
|
2024-06-17 21:50:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-05 01:25:09 +08:00
|
|
|
// Mapping of simple key shortcuts to tldraw functions
|
|
|
|
const simpleKeyMap = {
|
2024-09-18 23:21:16 +08:00
|
|
|
v: () => tlEditorRef.current?.setCurrentTool('select'),
|
|
|
|
d: () => tlEditorRef.current?.setCurrentTool('draw'),
|
|
|
|
e: () => tlEditorRef.current?.setCurrentTool('eraser'),
|
|
|
|
h: () => tlEditorRef.current?.setCurrentTool('hand'),
|
|
|
|
r: () => tlEditorRef.current?.setCurrentTool('rectangle'),
|
|
|
|
o: () => tlEditorRef.current?.setCurrentTool('ellipse'),
|
|
|
|
a: () => tlEditorRef.current?.setCurrentTool('arrow'),
|
|
|
|
l: () => tlEditorRef.current?.setCurrentTool('line'),
|
|
|
|
t: () => tlEditorRef.current?.setCurrentTool('text'),
|
|
|
|
f: () => tlEditorRef.current?.setCurrentTool('frame'),
|
|
|
|
n: () => tlEditorRef.current?.setCurrentTool('note'),
|
2024-06-05 01:25:09 +08:00
|
|
|
};
|
|
|
|
|
2024-07-09 02:06:23 +08:00
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2024-10-03 21:20:15 +08:00
|
|
|
if (key === 'z') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
if (event.shiftKey) {
|
|
|
|
// Redo (Ctrl + Shift + z)
|
|
|
|
tlEditorRef.current?.redo();
|
|
|
|
} else {
|
|
|
|
// Undo (Ctrl + z)
|
|
|
|
tlEditorRef.current?.undo();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-05 01:25:09 +08:00
|
|
|
const ctrlKeyMap = {
|
2024-09-18 23:21:16 +08:00
|
|
|
a: () => {
|
2024-06-05 01:25:09 +08:00
|
|
|
tlEditorRef.current?.selectAll();
|
2024-09-18 23:21:16 +08:00
|
|
|
tlEditorRef.current?.setCurrentTool('select');
|
2024-06-05 01:25:09 +08:00
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
d: () => {
|
|
|
|
tlEditorRef.current
|
|
|
|
?.duplicateShapes(tlEditorRef.current?.getSelectedShapes(), { x: 35, y: 35 });
|
2024-06-05 01:25:09 +08:00
|
|
|
tlEditorRef.current?.selectNone();
|
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
x: () => {
|
2024-06-26 02:52:50 +08:00
|
|
|
handleCut(true);
|
2024-06-25 21:57:10 +08:00
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
c: () => {
|
2024-06-25 21:57:10 +08:00
|
|
|
handleCopy();
|
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
v: () => {
|
2024-06-25 21:57:10 +08:00
|
|
|
if (!isPasting) {
|
|
|
|
handlePaste();
|
|
|
|
}
|
|
|
|
},
|
2024-06-05 01:25:09 +08:00
|
|
|
};
|
2024-10-03 21:20:15 +08:00
|
|
|
|
|
|
|
if (ctrlKeyMap[key]) {
|
2024-06-05 01:25:09 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2024-10-03 21:20:15 +08:00
|
|
|
ctrlKeyMap[key]();
|
2024-06-05 01:25:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:20:15 +08:00
|
|
|
if (simpleKeyMap[key]) {
|
2024-06-05 01:25:09 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2024-10-03 21:20:15 +08:00
|
|
|
simpleKeyMap[key]();
|
2024-06-05 01:25:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-06 06:15:43 +08:00
|
|
|
const moveDistance = 10;
|
2024-09-18 23:21:16 +08:00
|
|
|
const selectedShapes = tlEditorRef.current?.getSelectedShapes().map((shape) => shape.id);
|
2024-06-06 06:15:43 +08:00
|
|
|
|
|
|
|
const arrowKeyMap = {
|
2024-09-18 23:21:16 +08:00
|
|
|
ArrowUp: { x: 0, y: -moveDistance },
|
|
|
|
ArrowDown: { x: 0, y: moveDistance },
|
|
|
|
ArrowLeft: { x: -moveDistance, y: 0 },
|
|
|
|
ArrowRight: { x: moveDistance, y: 0 },
|
2024-06-06 06:15:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (arrowKeyMap[event.key]) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
tlEditorRef.current?.nudgeShapes(selectedShapes, arrowKeyMap[event.key], { squashing: true });
|
2024-06-05 01:25:09 +08:00
|
|
|
}
|
2024-09-18 23:21:16 +08:00
|
|
|
}, [
|
|
|
|
tlEditorRef, isPresenterRef, hasWBAccessRef, previousTool, handleCut, handleCopy, handlePaste,
|
|
|
|
]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (whiteboardRef.current) {
|
|
|
|
whiteboardRef.current.addEventListener('keydown', handleKeyDown, {
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
whiteboardRef.current?.removeEventListener('keydown', handleKeyDown);
|
|
|
|
};
|
|
|
|
}, [whiteboardRef.current]);
|
|
|
|
|
|
|
|
const language = React.useMemo(() => mapLanguage(locale?.toLowerCase() || 'en'), [locale]);
|
|
|
|
|
|
|
|
const [cursorPosition, updateCursorPosition] = useCursor(
|
|
|
|
publishCursorUpdate,
|
|
|
|
whiteboardIdRef.current,
|
|
|
|
);
|
2024-04-05 23:14:08 +08:00
|
|
|
|
2024-10-08 21:59:18 +08:00
|
|
|
const setCamera = (zoom, x = 0, y = 0) => {
|
|
|
|
if (tlEditorRef.current) {
|
|
|
|
tlEditorRef.current.setCamera({ x, y, z: zoom }, { duration: 175 });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const calculateZoomValue = (localWidth, localHeight) => {
|
|
|
|
const calcedZoom = fitToWidth
|
|
|
|
? presentationAreaWidth / localWidth
|
|
|
|
: Math.min(
|
|
|
|
presentationAreaWidth / localWidth,
|
|
|
|
presentationAreaHeight / localHeight,
|
|
|
|
);
|
|
|
|
|
|
|
|
return calcedZoom === 0 || calcedZoom === Infinity
|
|
|
|
? HUNDRED_PERCENT
|
|
|
|
: calcedZoom;
|
|
|
|
};
|
|
|
|
|
|
|
|
const adjustCameraOnMount = (includeViewerLogic = true) => {
|
|
|
|
if (presenterChanged) {
|
|
|
|
localStorage.removeItem('initialViewBoxWidth');
|
|
|
|
localStorage.removeItem('initialViewBoxHeight');
|
|
|
|
}
|
|
|
|
|
|
|
|
const storedWidth = localStorage.getItem('initialViewBoxWidth');
|
|
|
|
const storedHeight = localStorage.getItem('initialViewBoxHeight');
|
|
|
|
if (storedWidth && storedHeight) {
|
|
|
|
initialViewBoxWidthRef.current = parseFloat(storedWidth);
|
|
|
|
initialViewBoxHeightRef.current = parseFloat(storedHeight);
|
|
|
|
} else {
|
|
|
|
const currentZoomLevel = currentPresentationPageRef.current.scaledWidth
|
|
|
|
/ currentPresentationPageRef.current.scaledViewBoxWidth;
|
|
|
|
const calculatedWidth = currentZoomLevel !== 1
|
|
|
|
? currentPresentationPageRef.current.scaledWidth / currentZoomLevel
|
|
|
|
: currentPresentationPageRef.current.scaledWidth;
|
|
|
|
const calculatedHeight = currentZoomLevel !== 1
|
|
|
|
? currentPresentationPageRef.current.scaledHeight / currentZoomLevel
|
|
|
|
: currentPresentationPageRef.current.scaledHeight;
|
|
|
|
|
|
|
|
initialViewBoxWidthRef.current = calculatedWidth;
|
|
|
|
initialViewBoxHeightRef.current = calculatedHeight;
|
|
|
|
localStorage.setItem('initialViewBoxWidth', calculatedWidth.toString());
|
|
|
|
localStorage.setItem('initialViewBoxHeight', calculatedHeight.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
if (
|
|
|
|
presentationAreaHeight > 0
|
|
|
|
&& presentationAreaWidth > 0
|
|
|
|
&& currentPresentationPageRef.current
|
|
|
|
&& currentPresentationPageRef.current.scaledWidth > 0
|
|
|
|
&& currentPresentationPageRef.current.scaledHeight > 0
|
|
|
|
) {
|
|
|
|
const adjustedPresentationAreaHeight = isPresenter
|
|
|
|
? presentationAreaHeight - 40
|
|
|
|
: presentationAreaHeight;
|
|
|
|
|
|
|
|
let baseZoom;
|
|
|
|
if (isPresenter) {
|
|
|
|
baseZoom = fitToWidth
|
|
|
|
? presentationAreaWidth / currentPresentationPageRef.current.scaledWidth
|
|
|
|
: Math.min(
|
|
|
|
presentationAreaWidth / currentPresentationPageRef.current.scaledWidth,
|
|
|
|
adjustedPresentationAreaHeight / currentPresentationPageRef.current.scaledHeight,
|
|
|
|
);
|
|
|
|
|
|
|
|
const zoomAdjustmentFactor = currentPresentationPageRef.current.scaledWidth
|
|
|
|
/ currentPresentationPageRef.current.scaledViewBoxWidth;
|
|
|
|
baseZoom *= zoomAdjustmentFactor;
|
|
|
|
|
|
|
|
const adjustedXPos = currentPresentationPageRef.current.xOffset;
|
|
|
|
const adjustedYPos = currentPresentationPageRef.current.yOffset;
|
|
|
|
|
|
|
|
setCamera(baseZoom, adjustedXPos, adjustedYPos);
|
|
|
|
} else if (includeViewerLogic) {
|
|
|
|
baseZoom = calculateZoomValue(
|
|
|
|
currentPresentationPage.scaledViewBoxWidth,
|
|
|
|
currentPresentationPage.scaledViewBoxHeight,
|
|
|
|
);
|
|
|
|
|
|
|
|
const presenterXOffset = currentPresentationPageRef.current.xOffset;
|
|
|
|
const presenterYOffset = currentPresentationPageRef.current.yOffset;
|
|
|
|
|
|
|
|
const adjustedXPos = isInfiniteWhiteboard
|
|
|
|
? presenterXOffset
|
|
|
|
: currentPresentationPage.xOffset;
|
|
|
|
const adjustedYPos = isInfiniteWhiteboard
|
|
|
|
? presenterYOffset
|
|
|
|
: currentPresentationPage.yOffset;
|
|
|
|
|
|
|
|
setCamera(baseZoom, adjustedXPos, adjustedYPos);
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 19:52:52 +08:00
|
|
|
}, CAMERA_UPDATE_DELAY);
|
2024-10-08 21:59:18 +08:00
|
|
|
};
|
|
|
|
|
2024-04-05 23:14:08 +08:00
|
|
|
const handleTldrawMount = (editor) => {
|
|
|
|
setTlEditor(editor);
|
|
|
|
setTldrawAPI(editor);
|
|
|
|
|
|
|
|
editor?.user?.updateUserPreferences({ locale: language });
|
|
|
|
|
2024-04-04 10:00:56 +08:00
|
|
|
const colorStyles = [
|
2024-09-18 23:21:16 +08:00
|
|
|
'black',
|
|
|
|
'blue',
|
|
|
|
'green',
|
|
|
|
'grey',
|
|
|
|
'light-blue',
|
|
|
|
'light-green',
|
|
|
|
'light-red',
|
|
|
|
'light-violet',
|
|
|
|
'orange',
|
|
|
|
'red',
|
|
|
|
'violet',
|
|
|
|
'yellow',
|
2024-04-04 10:00:56 +08:00
|
|
|
];
|
2024-09-18 23:21:16 +08:00
|
|
|
const dashStyles = ['dashed', 'dotted', 'draw', 'solid'];
|
|
|
|
const fillStyles = ['none', 'pattern', 'semi', 'solid'];
|
|
|
|
const fontStyles = ['draw', 'mono', 'sans', 'serif'];
|
|
|
|
const sizeStyles = ['l', 'm', 's', 'xl'];
|
2024-04-04 10:00:56 +08:00
|
|
|
|
|
|
|
if (colorStyles.includes(colorStyle)) {
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.setStyleForNextShapes(DefaultColorStyle, colorStyle);
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
if (dashStyles.includes(dashStyle)) {
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.setStyleForNextShapes(DefaultDashStyle, dashStyle);
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
if (fillStyles.includes(fillStyle)) {
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.setStyleForNextShapes(DefaultFillStyle, fillStyle);
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
if (fontStyles.includes(fontStyle)) {
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.setStyleForNextShapes(DefaultFontStyle, fontStyle);
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
if (sizeStyles.includes(sizeStyle)) {
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.setStyleForNextShapes(DefaultSizeStyle, sizeStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.store.listen(
|
|
|
|
(entry) => {
|
|
|
|
const { changes } = entry;
|
|
|
|
const { added, updated, removed } = changes;
|
|
|
|
|
2024-04-24 02:59:26 +08:00
|
|
|
const addedCount = Object.keys(added).length;
|
2024-09-20 03:17:11 +08:00
|
|
|
const localShapes = editor.getCurrentPageShapes();
|
|
|
|
const filteredShapes = localShapes?.filter((item) => item?.index !== 'a0') || [];
|
|
|
|
const shapeNumberExceeded = filteredShapes
|
|
|
|
.length + addedCount - 1 > maxNumberOfAnnotations;
|
2024-04-24 02:59:26 +08:00
|
|
|
const invalidShapeType = Object.keys(added).find((id) => !isValidShapeType(added[id]));
|
|
|
|
|
2024-09-20 03:17:11 +08:00
|
|
|
if (addedCount > 0 && (shapeNumberExceeded || invalidShapeType)) {
|
2024-09-18 23:21:16 +08:00
|
|
|
// notify and undo last command without persisting
|
|
|
|
// to not generate the onUndo/onRedo callback
|
2024-04-24 02:59:26 +08:00
|
|
|
if (shapeNumberExceeded) {
|
|
|
|
notifyShapeNumberExceeded(intl, maxNumberOfAnnotations);
|
|
|
|
} else {
|
|
|
|
notifyNotAllowedChange(intl);
|
|
|
|
}
|
2024-09-20 03:17:11 +08:00
|
|
|
// use remote to not trigger unwanted updates
|
|
|
|
editor.store.mergeRemoteChanges(() => {
|
|
|
|
editor.history.undo({ persist: false });
|
|
|
|
const tool = editor.getCurrentToolId();
|
|
|
|
editor.setCurrentTool('noop');
|
|
|
|
editor.setCurrentTool(tool);
|
|
|
|
});
|
2024-04-24 02:59:26 +08:00
|
|
|
} else {
|
2024-09-18 03:57:25 +08:00
|
|
|
// Add new shapes to the batch
|
2024-04-24 02:59:26 +08:00
|
|
|
Object.values(added).forEach((record) => {
|
|
|
|
const updatedRecord = {
|
|
|
|
...record,
|
|
|
|
meta: {
|
|
|
|
...record.meta,
|
|
|
|
createdBy: currentUser?.userId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-09-18 03:57:25 +08:00
|
|
|
shapeBatchRef.current[updatedRecord.id] = updatedRecord;
|
2024-04-24 02:59:26 +08:00
|
|
|
});
|
|
|
|
}
|
2024-04-05 23:14:08 +08:00
|
|
|
|
2024-09-18 03:57:25 +08:00
|
|
|
// Update existing shapes and add them to the batch
|
2024-09-18 23:21:16 +08:00
|
|
|
Object.values(updated).forEach(([, record]) => {
|
|
|
|
const createdBy = prevShapesRef.current[record?.id]?.meta?.createdBy
|
|
|
|
|| currentUser?.userId;
|
2024-04-05 23:14:08 +08:00
|
|
|
const updatedRecord = {
|
|
|
|
...record,
|
|
|
|
meta: {
|
|
|
|
createdBy,
|
|
|
|
updatedBy: currentUser?.userId,
|
|
|
|
},
|
|
|
|
};
|
2024-04-02 23:33:55 +08:00
|
|
|
|
2024-09-19 23:38:57 +08:00
|
|
|
const diff = getDifferences(prevShapesRef.current[record?.id], updatedRecord);
|
|
|
|
|
|
|
|
if (diff) {
|
|
|
|
diff.id = record.id;
|
|
|
|
|
|
|
|
shapeBatchRef.current[updatedRecord.id] = diff;
|
|
|
|
} else {
|
|
|
|
shapeBatchRef.current[updatedRecord.id] = updatedRecord;
|
|
|
|
}
|
2024-04-05 23:14:08 +08:00
|
|
|
});
|
|
|
|
|
2024-09-18 03:57:25 +08:00
|
|
|
// Handle removed shapes immediately (not batched)
|
2024-09-20 03:17:11 +08:00
|
|
|
const idsToRemove = Object.keys(removed);
|
|
|
|
if (idsToRemove.length > 0) {
|
|
|
|
removeShapes(idsToRemove);
|
|
|
|
}
|
2024-04-05 23:14:08 +08:00
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
{ source: 'user', scope: 'document' },
|
2024-04-05 23:14:08 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
editor.store.listen(
|
|
|
|
(entry) => {
|
2024-09-18 23:21:16 +08:00
|
|
|
const { changes } = entry;
|
2024-04-05 23:14:08 +08:00
|
|
|
const { updated } = changes;
|
2024-09-18 23:21:16 +08:00
|
|
|
const { 'pointer:pointer': pointers } = updated;
|
2024-04-13 04:39:53 +08:00
|
|
|
|
2024-09-18 03:57:25 +08:00
|
|
|
const path = editor.getPath();
|
|
|
|
|
2024-07-04 01:05:06 +08:00
|
|
|
if ((isPresenterRef.current || hasWBAccessRef.current) && pointers) {
|
2024-09-18 23:21:16 +08:00
|
|
|
const [, nextPointer] = pointers;
|
2024-04-05 23:14:08 +08:00
|
|
|
updateCursorPosition(nextPointer?.x, nextPointer?.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
const camKey = `camera:page:${curPageIdRef.current}`;
|
|
|
|
const { [camKey]: cameras } = updated;
|
|
|
|
|
|
|
|
if (cameras) {
|
|
|
|
const [prevCam, nextCam] = cameras;
|
|
|
|
const panned = prevCam.x !== nextCam.x || prevCam.y !== nextCam.y;
|
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
const zoomed = prevCam.z !== nextCam.z;
|
|
|
|
|
|
|
|
if ((panned || (zoomed && fitToWidthRef.current)) && isPresenterRef.current) {
|
2024-09-18 23:21:16 +08:00
|
|
|
const viewedRegionW = SlideCalcUtil.calcViewedRegionWidth(
|
2024-04-05 23:14:08 +08:00
|
|
|
editor?.getViewportPageBounds()?.w,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPageRef.current?.scaledWidth,
|
2024-04-05 23:14:08 +08:00
|
|
|
);
|
2024-09-18 23:21:16 +08:00
|
|
|
const viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(
|
2024-04-05 23:14:08 +08:00
|
|
|
editor?.getViewportPageBounds()?.h,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPageRef.current?.scaledHeight,
|
2024-04-05 23:14:08 +08:00
|
|
|
);
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
zoomSlide(
|
|
|
|
viewedRegionW, viewedRegionH, nextCam.x, nextCam.y,
|
|
|
|
currentPresentationPageRef.current,
|
|
|
|
);
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 03:57:25 +08:00
|
|
|
|
|
|
|
// Check for idle states and persist the batch if there are shapes
|
|
|
|
if (path === 'select.idle' || path === 'draw.idle' || path === 'select.editing_shape' || path === 'highlight.idle') {
|
|
|
|
if (Object.keys(shapeBatchRef.current).length > 0) {
|
|
|
|
const shapesToPersist = Object.values(shapeBatchRef.current);
|
|
|
|
shapesToPersist.forEach((shape) => {
|
|
|
|
persistShapeWrapper(
|
|
|
|
shape,
|
|
|
|
whiteboardIdRef.current,
|
2024-09-18 23:21:16 +08:00
|
|
|
isModeratorRef.current,
|
2024-09-18 03:57:25 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
shapeBatchRef.current = {};
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 23:14:08 +08:00
|
|
|
},
|
2024-09-18 23:21:16 +08:00
|
|
|
{ source: 'user' },
|
2024-04-05 23:14:08 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (editor && curPageIdRef.current) {
|
|
|
|
const pages = [
|
|
|
|
{
|
|
|
|
meta: {},
|
|
|
|
id: `page:${curPageIdRef.current}`,
|
|
|
|
name: `Slide ${curPageIdRef.current}`,
|
2024-09-18 23:21:16 +08:00
|
|
|
index: 'a1',
|
|
|
|
typeName: 'page',
|
2024-04-05 23:14:08 +08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
editor.store.mergeRemoteChanges(() => {
|
|
|
|
editor.batch(() => {
|
|
|
|
editor.store.put(pages);
|
|
|
|
editor.store.put(assets);
|
2024-07-10 02:36:06 +08:00
|
|
|
editor.setCurrentPage(`page:${curPageIdRef.current}`);
|
|
|
|
editor.store.put(bgShape);
|
2024-04-05 23:14:08 +08:00
|
|
|
editor.history.clear();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const remoteShapes = shapes;
|
|
|
|
const localShapes = editor.store.allRecords();
|
2024-09-18 23:21:16 +08:00
|
|
|
const filteredShapes = localShapes.filter((item) => item?.typeName === 'shape') || [];
|
2024-04-05 23:14:08 +08:00
|
|
|
|
|
|
|
const localShapesObj = {};
|
|
|
|
filteredShapes.forEach((shape) => {
|
|
|
|
localShapesObj[shape.id] = shape;
|
|
|
|
});
|
|
|
|
|
|
|
|
const shapesToAdd = [];
|
2024-09-18 23:21:16 +08:00
|
|
|
Object.keys(remoteShapes).forEach((id) => {
|
2024-04-05 23:14:08 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
!localShapesObj[id]
|
|
|
|
|| JSON.stringify(remoteShapes[id])
|
|
|
|
!== JSON.stringify(localShapesObj[id])
|
2024-04-05 23:14:08 +08:00
|
|
|
) {
|
|
|
|
shapesToAdd.push(remoteShapes[id]);
|
|
|
|
}
|
2024-09-18 23:21:16 +08:00
|
|
|
});
|
2024-04-05 23:14:08 +08:00
|
|
|
|
|
|
|
editor.store.mergeRemoteChanges(() => {
|
|
|
|
if (shapesToAdd && shapesToAdd.length) {
|
|
|
|
shapesToAdd.forEach((shape) => {
|
2024-09-18 23:21:16 +08:00
|
|
|
const newShape = shape;
|
|
|
|
delete newShape.isModerator;
|
|
|
|
delete newShape.questionType;
|
2024-04-05 23:14:08 +08:00
|
|
|
});
|
|
|
|
editor.store.put(shapesToAdd);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
editor.store.onBeforeChange = (prev, next) => {
|
|
|
|
const newNext = next;
|
|
|
|
if (next?.typeName === 'instance_page_state') {
|
2024-07-04 01:05:06 +08:00
|
|
|
if (isPresenterRef.current || isModeratorRef.current) return next;
|
2024-04-05 23:14:08 +08:00
|
|
|
|
|
|
|
// Filter selectedShapeIds based on shape owner
|
2024-09-18 23:21:16 +08:00
|
|
|
if (next.selectedShapeIds.length > 0
|
|
|
|
&& !isEqual(prev.selectedShapeIds, next.selectedShapeIds)
|
|
|
|
) {
|
|
|
|
newNext.selectedShapeIds = next.selectedShapeIds.filter((shapeId) => {
|
2024-05-22 00:03:01 +08:00
|
|
|
const shapeOwner = prevShapesRef.current[shapeId]?.meta?.createdBy;
|
2024-04-05 23:14:08 +08:00
|
|
|
return !shapeOwner || shapeOwner === currentUser?.userId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEqual(prev.hoveredShapeId, next.hoveredShapeId)) {
|
2024-05-22 00:03:01 +08:00
|
|
|
const hoveredShapeOwner = prevShapesRef.current[next.hoveredShapeId]?.meta?.createdBy;
|
2024-04-05 23:14:08 +08:00
|
|
|
if (hoveredShapeOwner !== currentUser?.userId) {
|
2024-09-18 23:21:16 +08:00
|
|
|
newNext.hoveredShapeId = null;
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
return newNext;
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
|
|
|
|
2024-05-22 00:03:01 +08:00
|
|
|
// Get viewport dimensions and bounds
|
2024-10-09 23:49:22 +08:00
|
|
|
let viewportWidth;
|
|
|
|
let viewportHeight;
|
|
|
|
|
|
|
|
if (isPresenterRef.current) {
|
|
|
|
const viewportPageBounds = editor?.getViewportPageBounds();
|
|
|
|
viewportWidth = viewportPageBounds?.w
|
|
|
|
viewportHeight = viewportPageBounds?.h
|
|
|
|
} else {
|
|
|
|
viewportWidth = currentPresentationPageRef.current?.scaledViewBoxWidth
|
|
|
|
viewportHeight = currentPresentationPageRef.current?.scaledViewBoxHeight
|
|
|
|
}
|
2024-05-22 00:03:01 +08:00
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
const presentationWidthLocal = currentPresentationPage?.scaledWidth || 0;
|
2024-09-18 23:21:16 +08:00
|
|
|
const presentationHeightLocal = currentPresentationPage?.scaledHeight || 0;
|
2024-05-22 00:03:01 +08:00
|
|
|
|
|
|
|
// Adjust camera position to ensure it stays within bounds
|
2024-09-18 23:21:16 +08:00
|
|
|
const panned = next?.id?.includes('camera') && (prev.x !== next.x || prev.y !== next.y);
|
2024-07-06 04:09:19 +08:00
|
|
|
if (panned && !currentPresentationPageRef.current?.infiniteWhiteboard) {
|
2024-05-22 00:03:01 +08:00
|
|
|
// Horizontal bounds check
|
|
|
|
if (next.x > 0) {
|
2024-09-18 23:21:16 +08:00
|
|
|
newNext.x = 0;
|
2024-10-05 00:28:06 +08:00
|
|
|
} else if (next.x < -(presentationWidthLocal - viewportWidth)) {
|
|
|
|
newNext.x = -(presentationWidthLocal - viewportWidth);
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
2024-05-22 00:03:01 +08:00
|
|
|
|
|
|
|
// Vertical bounds check
|
|
|
|
if (next.y > 0) {
|
2024-09-18 23:21:16 +08:00
|
|
|
newNext.y = 0;
|
|
|
|
} else if (next.y < -(presentationHeightLocal - viewportHeight)) {
|
|
|
|
newNext.y = -(presentationHeightLocal - viewportHeight);
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
|
|
|
}
|
2024-05-22 00:03:01 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
return newNext;
|
2024-04-05 23:14:08 +08:00
|
|
|
};
|
2024-05-23 04:19:44 +08:00
|
|
|
|
|
|
|
if (!isPresenterRef.current && !hasWBAccessRef.current) {
|
|
|
|
editor.setCurrentTool('noop');
|
|
|
|
}
|
2024-04-05 23:14:08 +08:00
|
|
|
}
|
|
|
|
|
2024-10-08 21:59:18 +08:00
|
|
|
adjustCameraOnMount(true);
|
2024-04-05 23:14:08 +08:00
|
|
|
isMountedRef.current = true;
|
|
|
|
};
|
|
|
|
|
2024-09-18 03:57:25 +08:00
|
|
|
const shapesToRemove = React.useMemo(() => {
|
|
|
|
if (isMouseDownRef.current) return [];
|
|
|
|
const remoteShapeIds = Object.keys(prevShapesRef.current);
|
2024-04-05 23:14:08 +08:00
|
|
|
const localShapes = tlEditorRef.current?.getCurrentPageShapes();
|
2024-09-18 23:21:16 +08:00
|
|
|
const filteredShapes = localShapes?.filter((item) => item?.index !== 'a0') || [];
|
2024-09-18 03:57:25 +08:00
|
|
|
return filteredShapes
|
|
|
|
.filter((localShape) => !remoteShapeIds.includes(localShape.id))
|
|
|
|
.map((localShape) => localShape.id);
|
|
|
|
}, [prevShapesRef.current, curPageId]);
|
|
|
|
|
|
|
|
const { shapesToAdd, shapesToUpdate } = React.useMemo(() => {
|
2023-12-06 02:15:25 +08:00
|
|
|
const toAdd = [];
|
|
|
|
const toUpdate = [];
|
|
|
|
|
2023-12-12 03:31:21 +08:00
|
|
|
Object.values(prevShapesRef.current).forEach((remoteShape) => {
|
2024-01-23 01:15:41 +08:00
|
|
|
if (!remoteShape.id) return;
|
2024-09-18 03:57:25 +08:00
|
|
|
const localShapes = tlEditorRef.current?.getCurrentPageShapes();
|
2024-09-18 23:21:16 +08:00
|
|
|
const filteredShapes = localShapes?.filter((item) => item?.index !== 'a0') || [];
|
2024-09-18 03:57:25 +08:00
|
|
|
const localLookup = new Map(
|
2024-09-18 23:21:16 +08:00
|
|
|
filteredShapes.map((shape) => [shape.id, shape]),
|
2024-09-18 03:57:25 +08:00
|
|
|
);
|
2023-12-06 02:15:25 +08:00
|
|
|
const localShape = localLookup.get(remoteShape.id);
|
2022-11-26 05:24:06 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
if (!localShape) {
|
2024-09-18 23:21:16 +08:00
|
|
|
const newRemoteShape = remoteShape;
|
|
|
|
delete newRemoteShape.isModerator;
|
|
|
|
delete newRemoteShape.questionType;
|
|
|
|
toAdd.push(newRemoteShape);
|
2024-07-09 02:35:34 +08:00
|
|
|
} else {
|
2024-06-04 20:37:58 +08:00
|
|
|
const remoteShapeMeta = remoteShape?.meta;
|
|
|
|
const isCreatedByCurrentUser = remoteShapeMeta?.createdBy === currentUser?.userId;
|
|
|
|
const isUpdatedByCurrentUser = remoteShapeMeta?.updatedBy === currentUser?.userId;
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
// System-level shapes (background image) lack createdBy
|
|
|
|
// and updatedBy metadata, which can cause false positives.
|
2024-06-04 22:53:21 +08:00
|
|
|
// These cases expect an early return and shouldn't be updated.
|
2024-06-04 08:00:37 +08:00
|
|
|
if (
|
2024-06-04 20:37:58 +08:00
|
|
|
remoteShapeMeta && (
|
|
|
|
(isCreatedByCurrentUser && isUpdatedByCurrentUser)
|
2024-06-04 22:53:21 +08:00
|
|
|
|| (!isCreatedByCurrentUser && isUpdatedByCurrentUser)
|
2024-06-04 20:37:58 +08:00
|
|
|
)
|
2024-06-04 08:00:37 +08:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-09 02:35:34 +08:00
|
|
|
const diff = remoteShape;
|
2024-04-04 10:00:56 +08:00
|
|
|
delete diff.isModerator;
|
|
|
|
delete diff.questionType;
|
2024-04-05 23:14:08 +08:00
|
|
|
toUpdate.push(diff);
|
2023-03-27 06:08:09 +08:00
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
});
|
2023-03-14 00:16:37 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
return {
|
|
|
|
shapesToAdd: toAdd,
|
|
|
|
shapesToUpdate: toUpdate,
|
2023-01-18 21:12:16 +08:00
|
|
|
};
|
2024-07-01 22:19:42 +08:00
|
|
|
}, [prevShapesRef.current, curPageId]);
|
2023-01-18 21:12:16 +08:00
|
|
|
|
2024-04-04 10:00:56 +08:00
|
|
|
const calculateZoomWithGapValue = (
|
|
|
|
localWidth,
|
|
|
|
localHeight,
|
2024-09-18 23:21:16 +08:00
|
|
|
widthAdjustment = 0,
|
2024-04-04 10:00:56 +08:00
|
|
|
) => {
|
2024-10-05 00:28:06 +08:00
|
|
|
const presentationWidthLocal = presentationAreaWidth - widthAdjustment;
|
2024-09-18 23:21:16 +08:00
|
|
|
const calcedZoom = (fitToWidth
|
2024-10-05 00:28:06 +08:00
|
|
|
? presentationWidthLocal / localWidth
|
2024-04-04 10:00:56 +08:00
|
|
|
: Math.min(
|
2024-10-05 00:28:06 +08:00
|
|
|
presentationWidthLocal / localWidth,
|
2024-09-18 23:21:16 +08:00
|
|
|
presentationAreaHeight / localHeight,
|
|
|
|
));
|
2024-04-04 10:00:56 +08:00
|
|
|
return calcedZoom === 0 || calcedZoom === Infinity
|
|
|
|
? HUNDRED_PERCENT
|
|
|
|
: calcedZoom;
|
|
|
|
};
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
useMouseEvents(
|
2024-09-18 23:21:16 +08:00
|
|
|
{
|
2024-10-05 00:28:06 +08:00
|
|
|
whiteboardRef, tlEditorRef, isWheelZoomRef, initialZoomRef, isPresenterRef,
|
2024-09-18 23:21:16 +08:00
|
|
|
},
|
2023-12-06 02:15:25 +08:00
|
|
|
{
|
2024-02-08 04:20:06 +08:00
|
|
|
hasWBAccess: hasWBAccessRef.current,
|
2023-12-06 02:15:25 +08:00
|
|
|
whiteboardToolbarAutoHide,
|
|
|
|
animations,
|
|
|
|
publishCursorUpdate,
|
2024-02-08 01:07:40 +08:00
|
|
|
whiteboardId: whiteboardIdRef.current,
|
2023-12-06 02:15:25 +08:00
|
|
|
cursorPosition,
|
|
|
|
updateCursorPosition,
|
|
|
|
toggleToolsAnimations,
|
2024-01-09 22:31:08 +08:00
|
|
|
currentPresentationPage,
|
2024-02-01 02:53:05 +08:00
|
|
|
zoomChanger,
|
2024-03-25 01:22:49 +08:00
|
|
|
setIsMouseDown,
|
|
|
|
setIsWheelZoom,
|
|
|
|
setWheelZoomTimeout,
|
2024-09-18 23:21:16 +08:00
|
|
|
},
|
2023-12-06 02:15:25 +08:00
|
|
|
);
|
2023-03-08 21:01:18 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
tlEditorRef.current = tlEditor;
|
|
|
|
}, [tlEditor]);
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2024-02-08 01:07:40 +08:00
|
|
|
React.useEffect(() => {
|
2024-02-27 01:06:18 +08:00
|
|
|
const handleArrowPress = (event) => {
|
2024-09-18 23:21:16 +08:00
|
|
|
const currPageNum = parseInt(curPageIdRef.current, 10);
|
2024-05-17 01:20:20 +08:00
|
|
|
const shapeSelected = tlEditorRef.current.getSelectedShapes()?.length > 0;
|
2024-02-27 01:06:18 +08:00
|
|
|
const changeSlide = (direction) => {
|
2024-03-16 04:11:56 +08:00
|
|
|
if (!currentPresentationPage) return;
|
2024-09-18 23:21:16 +08:00
|
|
|
const newSlideNum = currPageNum + direction;
|
|
|
|
const outOfBounds = direction > 0
|
|
|
|
? newSlideNum > currentPresentationPage?.totalPages
|
|
|
|
: newSlideNum < 1;
|
2024-02-27 01:06:18 +08:00
|
|
|
|
|
|
|
if (outOfBounds) return;
|
|
|
|
|
|
|
|
skipToSlide(newSlideNum);
|
|
|
|
zoomChanger(HUNDRED_PERCENT);
|
|
|
|
zoomSlide(HUNDRED_PERCENT, HUNDRED_PERCENT, 0, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!shapeSelected) {
|
2024-02-27 01:10:40 +08:00
|
|
|
if (event.keyCode === KEY_CODES.ARROW_RIGHT) {
|
2024-02-27 01:06:18 +08:00
|
|
|
changeSlide(1); // Move to the next slide
|
2024-02-27 01:10:40 +08:00
|
|
|
} else if (event.keyCode === KEY_CODES.ARROW_LEFT) {
|
2024-02-27 01:06:18 +08:00
|
|
|
changeSlide(-1); // Move to the previous slide
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
};
|
2024-02-27 01:06:18 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const handleKeyDown2 = (event) => {
|
2024-04-04 10:00:56 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
(event.keyCode === KEY_CODES.ARROW_RIGHT
|
|
|
|
|| event.keyCode === KEY_CODES.ARROW_LEFT)
|
|
|
|
&& isPresenterRef.current
|
2024-04-04 10:00:56 +08:00
|
|
|
) {
|
|
|
|
handleArrowPress(event);
|
2024-02-27 01:06:18 +08:00
|
|
|
}
|
2024-02-08 01:07:40 +08:00
|
|
|
};
|
|
|
|
|
2024-02-09 00:13:38 +08:00
|
|
|
const handleKeyUp = (event) => {
|
2024-06-17 21:50:15 +08:00
|
|
|
if (event.key === ' ') {
|
|
|
|
if (previousTool.current) {
|
|
|
|
tlEditorRef.current?.setCurrentTool(previousTool.current);
|
|
|
|
previousTool.current = null;
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 00:13:38 +08:00
|
|
|
};
|
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
whiteboardRef.current?.addEventListener('keydown', handleKeyDown2, {
|
2024-04-04 10:00:56 +08:00
|
|
|
capture: true,
|
|
|
|
});
|
2024-09-18 23:21:16 +08:00
|
|
|
whiteboardRef.current?.addEventListener('keyup', handleKeyUp, {
|
2024-04-04 10:00:56 +08:00
|
|
|
capture: true,
|
|
|
|
});
|
2024-02-08 01:07:40 +08:00
|
|
|
|
|
|
|
return () => {
|
2024-09-18 23:21:16 +08:00
|
|
|
whiteboardRef.current?.removeEventListener('keydown', handleKeyDown2);
|
|
|
|
whiteboardRef.current?.removeEventListener('keyup', handleKeyUp);
|
2024-02-08 01:07:40 +08:00
|
|
|
};
|
2024-02-09 00:13:38 +08:00
|
|
|
}, [whiteboardRef.current]);
|
2024-02-08 01:07:40 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
zoomValueRef.current = zoomValue;
|
2024-04-02 23:33:55 +08:00
|
|
|
let timeoutId = null;
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2024-04-04 10:00:56 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
tlEditor
|
|
|
|
&& curPageIdRef.current
|
|
|
|
&& currentPresentationPage
|
|
|
|
&& isPresenter
|
|
|
|
&& isWheelZoomRef.current === false
|
2024-04-04 10:00:56 +08:00
|
|
|
) {
|
2024-10-05 00:28:06 +08:00
|
|
|
const zoomLevelForReset = (fitToWidthRef.current || !initialZoomRef.current)
|
|
|
|
? calculateZoomValue(
|
2024-04-04 10:00:56 +08:00
|
|
|
currentPresentationPage.scaledWidth,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPage.scaledHeight,
|
2024-10-05 00:28:06 +08:00
|
|
|
) : initialZoomRef.current;
|
2022-11-16 23:01:12 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const zoomCamera = zoomValue === HUNDRED_PERCENT
|
|
|
|
? zoomLevelForReset
|
|
|
|
: (zoomLevelForReset * zoomValue) / HUNDRED_PERCENT;
|
2024-04-04 02:39:24 +08:00
|
|
|
const camera = tlEditorRef.current.getCamera();
|
2024-02-02 08:36:40 +08:00
|
|
|
|
|
|
|
const nextCamera = {
|
2024-04-04 10:00:56 +08:00
|
|
|
x:
|
|
|
|
zoomValue === HUNDRED_PERCENT
|
|
|
|
? 0
|
2024-09-18 23:21:16 +08:00
|
|
|
: (camera.x
|
|
|
|
+ (tlEditorRef.current.getViewportPageBounds().w / 2 / zoomCamera
|
|
|
|
- tlEditorRef.current.getViewportPageBounds().w / 2 / camera.z)),
|
2024-04-04 10:00:56 +08:00
|
|
|
y:
|
|
|
|
zoomValue === HUNDRED_PERCENT
|
|
|
|
? 0
|
2024-09-18 23:21:16 +08:00
|
|
|
: (camera.y
|
|
|
|
+ (tlEditorRef.current.getViewportPageBounds().h / 2 / zoomCamera
|
|
|
|
- tlEditorRef.current.getViewportPageBounds().h / 2 / camera.z)),
|
2024-02-02 08:36:40 +08:00
|
|
|
z: zoomCamera,
|
|
|
|
};
|
|
|
|
|
2024-04-04 10:00:56 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
zoomValue !== prevZoomValueRef.current
|
2024-04-04 10:00:56 +08:00
|
|
|
) {
|
2024-09-11 01:38:55 +08:00
|
|
|
tlEditor.setCamera(nextCamera, { duration: 175 });
|
2022-04-26 21:55:05 +08:00
|
|
|
|
2024-04-02 23:33:55 +08:00
|
|
|
timeoutId = setTimeout(() => {
|
2024-10-05 00:28:06 +08:00
|
|
|
if (zoomValue === HUNDRED_PERCENT && !fitToWidthRef.current) {
|
2024-04-04 02:39:24 +08:00
|
|
|
zoomChanger(HUNDRED_PERCENT);
|
2024-04-04 10:00:56 +08:00
|
|
|
zoomSlide(HUNDRED_PERCENT, HUNDRED_PERCENT, 0, 0);
|
2024-04-04 02:39:24 +08:00
|
|
|
} else {
|
|
|
|
// Recalculate viewed region width and height for zoomSlide call
|
2024-09-18 23:21:16 +08:00
|
|
|
const viewedRegionW = SlideCalcUtil.calcViewedRegionWidth(
|
2024-04-04 02:39:24 +08:00
|
|
|
tlEditorRef.current.getViewportPageBounds().w,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPage.scaledWidth,
|
2024-04-04 02:39:24 +08:00
|
|
|
);
|
2024-09-18 23:21:16 +08:00
|
|
|
const viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(
|
2024-04-04 02:39:24 +08:00
|
|
|
tlEditorRef.current.getViewportPageBounds().h,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPage.scaledHeight,
|
2024-04-04 02:39:24 +08:00
|
|
|
);
|
2024-01-09 22:31:08 +08:00
|
|
|
|
2024-04-04 10:00:56 +08:00
|
|
|
zoomSlide(viewedRegionW, viewedRegionH, nextCamera.x, nextCamera.y);
|
2024-04-04 02:39:24 +08:00
|
|
|
}
|
2024-05-10 11:48:22 +08:00
|
|
|
}, 500);
|
2022-10-25 23:53:19 +08:00
|
|
|
}
|
2022-04-26 21:55:05 +08:00
|
|
|
}
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
// Update the previous zoom value ref with the current zoom value
|
|
|
|
prevZoomValueRef.current = zoomValue;
|
2024-04-02 23:33:55 +08:00
|
|
|
return () => clearTimeout(timeoutId);
|
2024-10-05 00:28:06 +08:00
|
|
|
}, [zoomValue, tlEditor, curPageId, isWheelZoomRef.current, fitToWidthRef.current]);
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
React.useEffect(() => {
|
2024-04-02 23:33:55 +08:00
|
|
|
// A slight delay to ensure the canvas has rendered
|
|
|
|
const timeoutId = setTimeout(() => {
|
2024-04-04 10:00:56 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPage.scaledWidth > 0
|
|
|
|
&& currentPresentationPage.scaledHeight > 0
|
2024-04-04 10:00:56 +08:00
|
|
|
) {
|
2024-04-02 23:33:55 +08:00
|
|
|
// Subtract the toolbar height from the presentation area height for the presenter
|
2024-04-04 10:00:56 +08:00
|
|
|
const adjustedPresentationAreaHeight = isPresenter
|
|
|
|
? presentationAreaHeight - 40
|
|
|
|
: presentationAreaHeight;
|
2024-09-18 23:21:16 +08:00
|
|
|
const slideAspectRatio = currentPresentationPage.scaledWidth
|
|
|
|
/ currentPresentationPage.scaledHeight;
|
|
|
|
const presentationAreaAspectRatio = presentationAreaWidth / adjustedPresentationAreaHeight;
|
2024-04-02 23:33:55 +08:00
|
|
|
|
|
|
|
let initialZoom;
|
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
if (slideAspectRatio > presentationAreaAspectRatio
|
|
|
|
|| (fitToWidthRef.current && isPresenter)
|
|
|
|
) {
|
2024-09-18 23:21:16 +08:00
|
|
|
initialZoom = presentationAreaWidth / currentPresentationPage.scaledWidth;
|
2024-04-02 23:33:55 +08:00
|
|
|
} else {
|
2024-09-18 23:21:16 +08:00
|
|
|
initialZoom = adjustedPresentationAreaHeight
|
|
|
|
/ currentPresentationPage.scaledHeight;
|
2024-04-02 23:33:55 +08:00
|
|
|
}
|
2024-02-01 02:53:05 +08:00
|
|
|
|
2024-04-02 23:33:55 +08:00
|
|
|
initialZoomRef.current = initialZoom;
|
|
|
|
prevZoomValueRef.current = zoomValue;
|
|
|
|
}
|
2024-05-10 11:48:22 +08:00
|
|
|
}, CAMERA_UPDATE_DELAY);
|
2024-04-02 23:33:55 +08:00
|
|
|
|
|
|
|
return () => clearTimeout(timeoutId);
|
|
|
|
}, [
|
2024-07-10 02:36:06 +08:00
|
|
|
currentPresentationPage.scaledHeight,
|
|
|
|
currentPresentationPage.scaledWidth,
|
2024-04-02 23:33:55 +08:00
|
|
|
presentationAreaWidth,
|
|
|
|
presentationAreaHeight,
|
2024-10-05 00:28:06 +08:00
|
|
|
presentationWidth,
|
|
|
|
presentationHeight,
|
2024-04-02 23:33:55 +08:00
|
|
|
isPresenter,
|
2024-07-01 22:19:42 +08:00
|
|
|
presentationId,
|
2024-10-05 00:28:06 +08:00
|
|
|
fitToWidthRef.current,
|
|
|
|
]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (fitToWidthRef.current && isPresenterRef.current) {
|
|
|
|
// when presentationHeight changes and we are using fitToWidht
|
|
|
|
// the zoom level and camera position stays same
|
|
|
|
// so we have to send the updated viewed area to server manually
|
|
|
|
const handleHeightChanged = () => {
|
|
|
|
const viewedRegionW = SlideCalcUtil.calcViewedRegionWidth(
|
|
|
|
tlEditorRef.current?.getViewportPageBounds()?.w,
|
|
|
|
currentPresentationPageRef.current?.scaledWidth,
|
|
|
|
);
|
|
|
|
const viewedRegionH = SlideCalcUtil.calcViewedRegionHeight(
|
|
|
|
tlEditorRef.current?.getViewportPageBounds()?.h,
|
|
|
|
currentPresentationPageRef.current?.scaledHeight,
|
|
|
|
);
|
|
|
|
|
|
|
|
const camera = tlEditorRef.current.getCamera();
|
|
|
|
|
|
|
|
zoomSlide(
|
|
|
|
viewedRegionW, viewedRegionH, camera.x, camera.y,
|
|
|
|
currentPresentationPageRef.current,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const timeoutId = setTimeout(handleHeightChanged, CAMERA_UPDATE_DELAY);
|
|
|
|
return () => clearTimeout(timeoutId);
|
|
|
|
}
|
|
|
|
return () => null;
|
|
|
|
}, [
|
|
|
|
presentationHeight,
|
|
|
|
fitToWidthRef.current,
|
|
|
|
isPresenterRef.current,
|
2024-04-02 23:33:55 +08:00
|
|
|
]);
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
React.useEffect(() => {
|
2024-04-03 02:00:25 +08:00
|
|
|
const handleResize = () => {
|
2024-04-13 04:39:53 +08:00
|
|
|
if (!initialViewBoxWidthRef.current) {
|
|
|
|
initialViewBoxWidthRef.current = currentPresentationPageRef.current?.scaledViewBoxWidth;
|
|
|
|
}
|
2024-07-01 22:19:42 +08:00
|
|
|
if (!initialViewBoxHeightRef.current) {
|
|
|
|
initialViewBoxHeightRef.current = currentPresentationPageRef.current?.scaledViewBoxHeight;
|
|
|
|
}
|
2024-04-03 02:00:25 +08:00
|
|
|
// Calculate the absolute difference
|
2024-04-04 10:00:56 +08:00
|
|
|
const heightDifference = Math.abs(
|
2024-09-18 23:21:16 +08:00
|
|
|
presentationAreaHeight - lastKnownHeight.current,
|
2024-04-04 10:00:56 +08:00
|
|
|
);
|
|
|
|
const widthDifference = Math.abs(
|
2024-09-18 23:21:16 +08:00
|
|
|
presentationAreaWidth - lastKnownWidth.current,
|
2024-04-04 10:00:56 +08:00
|
|
|
);
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-04-03 02:00:25 +08:00
|
|
|
// Check if the difference is greater than the threshold
|
|
|
|
if (heightDifference > THRESHOLD || widthDifference > THRESHOLD) {
|
|
|
|
// Update the last known dimensions
|
|
|
|
lastKnownHeight.current = presentationAreaHeight;
|
|
|
|
lastKnownWidth.current = presentationAreaWidth;
|
2022-04-11 00:31:12 +08:00
|
|
|
|
2024-04-03 02:00:25 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
presentationAreaHeight > 0
|
|
|
|
&& presentationAreaWidth > 0
|
|
|
|
&& tlEditor
|
|
|
|
&& currentPresentationPage
|
|
|
|
&& currentPresentationPage.scaledWidth > 0
|
|
|
|
&& currentPresentationPage.scaledHeight > 0
|
2024-04-03 02:00:25 +08:00
|
|
|
) {
|
|
|
|
const currentZoom = zoomValueRef.current || HUNDRED_PERCENT;
|
|
|
|
const baseZoom = calculateZoomValue(
|
2024-09-26 02:54:21 +08:00
|
|
|
currentPresentationPageRef.current.scaledWidth,
|
2024-10-05 00:28:06 +08:00
|
|
|
currentPresentationPageRef.current.scaledHeight,
|
2024-04-03 02:00:25 +08:00
|
|
|
);
|
|
|
|
let adjustedZoom = baseZoom * (currentZoom / HUNDRED_PERCENT);
|
|
|
|
if (isPresenter) {
|
2024-05-08 23:24:25 +08:00
|
|
|
const container = document.querySelector(
|
2024-09-18 23:21:16 +08:00
|
|
|
'[data-test="presentationContainer"]',
|
2024-05-08 23:24:25 +08:00
|
|
|
);
|
|
|
|
const innerWrapper = document.getElementById(
|
2024-09-18 23:21:16 +08:00
|
|
|
'presentationInnerWrapper',
|
2024-05-08 23:24:25 +08:00
|
|
|
);
|
|
|
|
const containerWidth = container ? container.offsetWidth : 0;
|
|
|
|
const innerWrapperWidth = innerWrapper
|
|
|
|
? innerWrapper.offsetWidth
|
|
|
|
: 0;
|
|
|
|
const widthGap = Math.max(containerWidth - innerWrapperWidth, 0);
|
|
|
|
const camera = tlEditorRef.current.getCamera();
|
|
|
|
|
|
|
|
if (widthGap > 0) {
|
|
|
|
adjustedZoom = calculateZoomWithGapValue(
|
2024-09-26 02:54:21 +08:00
|
|
|
currentPresentationPageRef.current.scaledWidth,
|
|
|
|
currentPresentationPageRef.current.scaledHeight,
|
2024-09-18 23:21:16 +08:00
|
|
|
widthGap,
|
2024-04-03 02:00:25 +08:00
|
|
|
);
|
2024-04-04 10:00:56 +08:00
|
|
|
|
2024-05-08 23:24:25 +08:00
|
|
|
adjustedZoom *= currentZoom / HUNDRED_PERCENT;
|
|
|
|
} else {
|
|
|
|
adjustedZoom = baseZoom * (currentZoom / HUNDRED_PERCENT);
|
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const zoomToApply = widthGap > 0
|
|
|
|
? adjustedZoom
|
|
|
|
: baseZoom * (currentZoom / HUNDRED_PERCENT);
|
2024-04-04 10:00:56 +08:00
|
|
|
|
2024-07-10 02:36:06 +08:00
|
|
|
const formattedPageId = Number(curPageIdRef?.current);
|
2024-09-26 02:54:21 +08:00
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
const updatedCurrentCam = {
|
2024-09-26 02:54:21 +08:00
|
|
|
...camera,
|
2024-10-05 00:28:06 +08:00
|
|
|
z: adjustedZoom,
|
2024-09-26 02:54:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let cameras = [
|
2024-07-10 02:36:06 +08:00
|
|
|
createCamera(formattedPageId - 1, zoomToApply),
|
2024-09-26 02:54:21 +08:00
|
|
|
updatedCurrentCam,
|
2024-07-10 02:36:06 +08:00
|
|
|
createCamera(formattedPageId + 1, zoomToApply),
|
|
|
|
];
|
2024-10-05 00:28:06 +08:00
|
|
|
cameras = cameras.filter((cam) => cam.id !== 'camera:page:0');
|
2024-07-10 02:36:06 +08:00
|
|
|
tlEditorRef.current.store.put(cameras);
|
2023-12-06 02:15:25 +08:00
|
|
|
} else {
|
2024-04-03 02:00:25 +08:00
|
|
|
// Viewer logic
|
2024-10-05 00:28:06 +08:00
|
|
|
const newZoom = calculateZoomValue(
|
|
|
|
currentPresentationPage.scaledViewBoxWidth,
|
|
|
|
currentPresentationPage.scaledViewBoxHeight,
|
2024-04-03 02:00:25 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const camera = tlEditorRef.current.getCamera();
|
2024-07-10 02:36:06 +08:00
|
|
|
const formattedPageId = Number(curPageIdRef?.current);
|
2024-10-05 00:28:06 +08:00
|
|
|
const updatedCurrentCam = {
|
2024-09-26 02:54:21 +08:00
|
|
|
...camera,
|
2024-10-05 00:28:06 +08:00
|
|
|
z: newZoom,
|
2024-09-26 02:54:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let cameras = [
|
2024-10-05 00:28:06 +08:00
|
|
|
createCamera(formattedPageId - 1, newZoom),
|
2024-09-26 02:54:21 +08:00
|
|
|
updatedCurrentCam,
|
2024-10-05 00:28:06 +08:00
|
|
|
createCamera(formattedPageId + 1, newZoom),
|
2024-07-10 02:36:06 +08:00
|
|
|
];
|
2024-10-05 00:28:06 +08:00
|
|
|
cameras = cameras.filter((cam) => cam.id !== 'camera:page:0');
|
2024-07-10 02:36:06 +08:00
|
|
|
tlEditorRef.current.store.put(cameras);
|
2023-12-06 02:15:25 +08:00
|
|
|
}
|
2022-08-24 02:19:17 +08:00
|
|
|
}
|
2022-09-30 22:35:29 +08:00
|
|
|
}
|
2024-04-04 10:00:56 +08:00
|
|
|
};
|
2024-04-03 02:00:25 +08:00
|
|
|
|
2024-05-10 11:48:22 +08:00
|
|
|
const timeoutId = setTimeout(handleResize, CAMERA_UPDATE_DELAY);
|
2024-04-03 02:00:25 +08:00
|
|
|
|
|
|
|
return () => clearTimeout(timeoutId);
|
2024-07-01 22:19:42 +08:00
|
|
|
}, [presentationAreaHeight, presentationAreaWidth, curPageId, presentationId]);
|
2022-09-30 22:35:29 +08:00
|
|
|
|
2022-04-26 21:55:05 +08:00
|
|
|
React.useEffect(() => {
|
2023-12-06 02:15:25 +08:00
|
|
|
if (!fitToWidth && isPresenter) {
|
|
|
|
zoomChanger(HUNDRED_PERCENT);
|
2024-04-04 10:00:56 +08:00
|
|
|
zoomSlide(HUNDRED_PERCENT, HUNDRED_PERCENT, 0, 0);
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
}, [fitToWidth, isPresenter]);
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2022-05-20 01:24:47 +08:00
|
|
|
React.useEffect(() => {
|
2024-09-18 23:21:16 +08:00
|
|
|
if (!isPresenter
|
|
|
|
&& tlEditorRef.current
|
|
|
|
&& initialViewBoxWidthRef.current
|
|
|
|
&& initialViewBoxHeightRef.current
|
|
|
|
&& currentPresentationPage
|
|
|
|
) {
|
2024-10-05 00:28:06 +08:00
|
|
|
const newZoom = calculateZoomValue(
|
2024-07-01 22:19:42 +08:00
|
|
|
currentPresentationPage.scaledViewBoxWidth,
|
2024-09-18 23:21:16 +08:00
|
|
|
currentPresentationPage.scaledViewBoxHeight,
|
2024-07-01 22:19:42 +08:00
|
|
|
);
|
2024-09-18 23:21:16 +08:00
|
|
|
|
2024-04-13 04:39:53 +08:00
|
|
|
const adjustedXPos = currentPresentationPage.xOffset;
|
|
|
|
const adjustedYPos = currentPresentationPage.yOffset;
|
2024-04-02 23:33:55 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
setCamera(
|
2024-10-05 00:28:06 +08:00
|
|
|
newZoom,
|
2024-04-13 04:39:53 +08:00
|
|
|
adjustedXPos,
|
2024-09-18 23:21:16 +08:00
|
|
|
adjustedYPos,
|
2023-12-06 02:15:25 +08:00
|
|
|
);
|
2023-08-28 03:19:04 +08:00
|
|
|
}
|
2024-10-08 21:59:18 +08:00
|
|
|
}, [currentPresentationPage, isPresenter]);
|
2022-05-20 01:24:47 +08:00
|
|
|
|
2022-08-16 20:12:43 +08:00
|
|
|
React.useEffect(() => {
|
2023-12-06 02:15:25 +08:00
|
|
|
if (shapesToAdd.length || shapesToUpdate.length || shapesToRemove.length) {
|
2024-02-14 08:44:47 +08:00
|
|
|
const tlStoreUpdateTimeoutId = setTimeout(() => {
|
|
|
|
tlEditor?.store?.mergeRemoteChanges(() => {
|
|
|
|
if (shapesToRemove.length > 0) {
|
|
|
|
tlEditor?.store?.remove(shapesToRemove);
|
|
|
|
}
|
|
|
|
if (shapesToAdd.length) {
|
|
|
|
tlEditor?.store?.put(shapesToAdd);
|
|
|
|
}
|
|
|
|
if (shapesToUpdate.length) {
|
2024-09-18 23:21:16 +08:00
|
|
|
const updatedShapes = shapesToUpdate.map((shape) => {
|
2024-07-05 22:14:36 +08:00
|
|
|
const currentShape = tlEditor?.getShape(shape.id);
|
|
|
|
if (currentShape) {
|
|
|
|
return { ...currentShape, ...shape };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
|
|
|
if (updatedShapes.length) {
|
|
|
|
tlEditor?.store?.put(updatedShapes);
|
|
|
|
}
|
2024-02-14 08:44:47 +08:00
|
|
|
}
|
|
|
|
});
|
2024-07-09 02:35:34 +08:00
|
|
|
}, 300);
|
2024-02-14 08:44:47 +08:00
|
|
|
|
|
|
|
return () => clearTimeout(tlStoreUpdateTimeoutId);
|
2022-08-16 20:12:43 +08:00
|
|
|
}
|
2024-09-18 23:21:16 +08:00
|
|
|
return undefined;
|
2023-12-06 02:15:25 +08:00
|
|
|
}, [shapesToAdd, shapesToUpdate, shapesToRemove]);
|
2022-08-16 20:12:43 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
// Updating presences in tldraw store based on changes in cursors
|
2022-08-19 02:18:02 +08:00
|
|
|
React.useEffect(() => {
|
2023-12-06 02:15:25 +08:00
|
|
|
if (tlEditorRef.current) {
|
2024-09-18 23:21:16 +08:00
|
|
|
const useElement = document.querySelector('.tl-cursor use');
|
2024-01-18 01:44:31 +08:00
|
|
|
if (useElement && !isMultiUserActive && !isPresenter) {
|
2024-09-18 23:21:16 +08:00
|
|
|
useElement.setAttribute('href', '#redPointer');
|
2023-12-06 02:15:25 +08:00
|
|
|
} else if (useElement) {
|
2024-09-18 23:21:16 +08:00
|
|
|
useElement.setAttribute('href', '#cursor');
|
2023-12-06 02:15:25 +08:00
|
|
|
}
|
|
|
|
|
2024-07-19 01:00:28 +08:00
|
|
|
const idsToRemove = [];
|
|
|
|
|
|
|
|
// Get all presence records from the store
|
|
|
|
const allRecords = tlEditorRef.current.store.allRecords();
|
2024-09-18 23:21:16 +08:00
|
|
|
const presenceRecords = allRecords.filter((record) => record.id.startsWith('instance_presence:'));
|
2024-07-19 01:00:28 +08:00
|
|
|
|
|
|
|
// Check if any presence records correspond to users not in whiteboardWriters
|
2024-09-18 23:21:16 +08:00
|
|
|
presenceRecords.forEach((record) => {
|
2024-07-19 01:00:28 +08:00
|
|
|
const userId = record.userId.split('instance_presence:')[1];
|
2024-09-18 23:21:16 +08:00
|
|
|
const hasAccessToWhiteboard = whiteboardWriters.some((writer) => writer.userId === userId);
|
2024-07-19 01:00:28 +08:00
|
|
|
|
|
|
|
if (!hasAccessToWhiteboard) {
|
|
|
|
idsToRemove.push(record.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const updatedPresences = otherCursors
|
2024-09-18 23:21:16 +08:00
|
|
|
.map(({
|
|
|
|
userId, user, xPercent, yPercent,
|
|
|
|
}) => {
|
2023-12-06 02:15:25 +08:00
|
|
|
const { presenter, name } = user;
|
|
|
|
const id = InstancePresenceRecordType.createId(userId);
|
2024-07-04 19:35:01 +08:00
|
|
|
const active = xPercent !== -1 && yPercent !== -1;
|
2023-12-06 02:15:25 +08:00
|
|
|
// if cursor is not active remove it from tldraw store
|
2024-04-04 10:00:56 +08:00
|
|
|
if (
|
2024-09-18 23:21:16 +08:00
|
|
|
!active
|
|
|
|
|| (hideViewersCursor
|
|
|
|
&& user.role === 'VIEWER'
|
|
|
|
&& !currentUser?.presenter)
|
|
|
|
|| (!presenter && !isMultiUserActive)
|
2024-04-04 10:00:56 +08:00
|
|
|
) {
|
2024-07-19 01:00:28 +08:00
|
|
|
idsToRemove.push(id);
|
2023-12-06 02:15:25 +08:00
|
|
|
return null;
|
2022-10-19 23:13:08 +08:00
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
|
|
|
|
const cursor = {
|
|
|
|
x: xPercent,
|
|
|
|
y: yPercent,
|
2024-09-18 23:21:16 +08:00
|
|
|
type: 'default',
|
2023-12-06 02:15:25 +08:00
|
|
|
rotation: 0,
|
|
|
|
};
|
2024-09-18 23:21:16 +08:00
|
|
|
const color = presenter ? '#FF0000' : '#70DB70';
|
2023-12-06 02:15:25 +08:00
|
|
|
const c = {
|
|
|
|
...InstancePresenceRecordType.create({
|
|
|
|
id,
|
2024-04-05 23:14:08 +08:00
|
|
|
currentPageId: `page:${curPageIdRef.current}`,
|
2023-12-06 02:15:25 +08:00
|
|
|
userId,
|
|
|
|
userName: name,
|
|
|
|
cursor,
|
|
|
|
color,
|
|
|
|
}),
|
|
|
|
lastActivityTimestamp: Date.now(),
|
|
|
|
};
|
2024-07-19 01:00:28 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
return c;
|
|
|
|
})
|
|
|
|
.filter((cursor) => cursor && cursor.userId !== currentUser?.userId);
|
|
|
|
|
2024-07-19 01:00:28 +08:00
|
|
|
if (idsToRemove.length) {
|
|
|
|
tlEditorRef.current?.store.remove(idsToRemove);
|
|
|
|
}
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
// If there are any updated presences, put them all in the store
|
|
|
|
if (updatedPresences.length) {
|
|
|
|
tlEditorRef.current?.store.put(updatedPresences);
|
2022-06-01 03:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 01:00:28 +08:00
|
|
|
}, [otherCursors, whiteboardWriters]);
|
2022-05-20 01:24:47 +08:00
|
|
|
|
2024-10-05 00:28:06 +08:00
|
|
|
const createPage = (currentPageId) => [
|
|
|
|
{
|
|
|
|
meta: {},
|
|
|
|
id: currentPageId,
|
|
|
|
name: `Slide ${currentPageId?.split(':')[1]}`,
|
|
|
|
index: 'a1',
|
|
|
|
typeName: 'page',
|
|
|
|
},
|
|
|
|
];
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const createCameras = (pageId, tlZ) => {
|
2024-09-25 05:24:40 +08:00
|
|
|
const cameras = [];
|
|
|
|
const MIN_PAGE_ID = 1;
|
|
|
|
const totalPages = currentPresentationPageRef.current?.totalPages || 1;
|
|
|
|
|
|
|
|
if (pageId > MIN_PAGE_ID) {
|
|
|
|
cameras.push(createCamera(pageId - 1, tlZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
cameras.push(createCamera(pageId, tlZ));
|
|
|
|
|
|
|
|
if (pageId < totalPages) {
|
|
|
|
cameras.push(createCamera(pageId + 1, tlZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cameras;
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const cleanupStore = (currentPageId) => {
|
|
|
|
const allRecords = tlEditorRef.current.store.allRecords();
|
|
|
|
const shapeIdsToRemove = allRecords
|
2024-10-05 00:28:06 +08:00
|
|
|
.filter((record) => record.typeName === 'shape' && record.parentId && record.parentId !== currentPageId)
|
|
|
|
.map((shape) => shape.id);
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
if (shapeIdsToRemove.length > 0) {
|
|
|
|
tlEditorRef.current.deleteShapes(shapeIdsToRemove);
|
|
|
|
}
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const updateStore = (pages, cameras) => {
|
|
|
|
tlEditorRef.current.store.put(pages);
|
|
|
|
tlEditorRef.current.store.put(cameras);
|
|
|
|
tlEditorRef.current.store.put(assets);
|
|
|
|
tlEditorRef.current.store.put(bgShape);
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const finalizeStore = () => {
|
|
|
|
tlEditorRef.current.history.clear();
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const toggleToolbarIfNeeded = () => {
|
|
|
|
if (whiteboardToolbarAutoHide && toggleToolsAnimations) {
|
2024-10-05 00:28:06 +08:00
|
|
|
toggleToolsAnimations('fade-in', 'fade-out', '0s', hasWBAccessRef.current || isPresenterRef.current);
|
2024-08-30 21:46:06 +08:00
|
|
|
}
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
|
|
|
const resetSlideState = () => {
|
|
|
|
slideChanged.current = false;
|
|
|
|
slideNext.current = null;
|
2024-10-05 00:28:06 +08:00
|
|
|
};
|
2024-08-30 21:46:06 +08:00
|
|
|
|
2022-08-15 06:49:39 +08:00
|
|
|
React.useEffect(() => {
|
2024-09-25 05:24:40 +08:00
|
|
|
const formattedPageId = parseInt(curPageIdRef.current, 10);
|
|
|
|
if (tlEditorRef.current && formattedPageId !== 0) {
|
|
|
|
const currentPageId = `page:${formattedPageId}`;
|
2024-08-30 21:46:06 +08:00
|
|
|
const tlZ = tlEditorRef.current.getCamera()?.z;
|
|
|
|
|
|
|
|
const pages = createPage(currentPageId);
|
|
|
|
const cameras = createCameras(formattedPageId, tlZ);
|
2022-08-15 06:49:39 +08:00
|
|
|
|
2024-02-08 01:07:40 +08:00
|
|
|
tlEditorRef.current.store.mergeRemoteChanges(() => {
|
|
|
|
tlEditorRef.current.batch(() => {
|
2024-08-30 21:46:06 +08:00
|
|
|
cleanupStore(currentPageId);
|
|
|
|
updateStore(pages, cameras);
|
|
|
|
tlEditorRef.current.setCurrentPage(currentPageId);
|
|
|
|
finalizeStore();
|
2024-02-08 01:07:40 +08:00
|
|
|
});
|
|
|
|
});
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-08-30 21:46:06 +08:00
|
|
|
toggleToolbarIfNeeded();
|
|
|
|
resetSlideState();
|
2023-12-06 02:15:25 +08:00
|
|
|
}
|
2024-07-01 22:19:42 +08:00
|
|
|
}, [curPageId]);
|
2023-05-27 02:39:09 +08:00
|
|
|
|
2024-05-10 11:48:22 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (isMountedRef.current) {
|
|
|
|
adjustCameraOnMount(false);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
isMountedRef.current,
|
|
|
|
selectedLayout,
|
2024-07-06 04:09:19 +08:00
|
|
|
isInfiniteWhiteboard,
|
2024-10-05 00:28:06 +08:00
|
|
|
fitToWidthRef.current,
|
|
|
|
presentationWidth,
|
2024-10-08 21:59:18 +08:00
|
|
|
curPageId,
|
|
|
|
isPresenter,
|
|
|
|
animations,
|
|
|
|
locale,
|
|
|
|
darkTheme,
|
2023-12-06 02:15:25 +08:00
|
|
|
]);
|
2022-12-17 03:00:02 +08:00
|
|
|
|
2024-04-13 04:39:53 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
setTldrawIsMounting(true);
|
|
|
|
return () => {
|
|
|
|
isMountedRef.current = false;
|
2024-07-05 08:55:47 +08:00
|
|
|
localStorage.removeItem('initialViewBoxWidth');
|
|
|
|
localStorage.removeItem('initialViewBoxHeight');
|
2024-04-13 04:39:53 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-01-26 02:49:09 +08:00
|
|
|
React.useEffect(() => {
|
2023-12-06 02:15:25 +08:00
|
|
|
if (isMounting) {
|
|
|
|
setIsMounting(false);
|
|
|
|
/// brings presentation toolbar back
|
|
|
|
setTldrawIsMounting(false);
|
|
|
|
}
|
2023-12-12 03:31:21 +08:00
|
|
|
}, [
|
|
|
|
tlEditorRef?.current?.camera,
|
|
|
|
presentationAreaWidth,
|
|
|
|
presentationAreaHeight,
|
2024-07-01 22:19:42 +08:00
|
|
|
presentationId,
|
2023-12-12 03:31:21 +08:00
|
|
|
]);
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
const bbbMultiUserTools = getFromUserSettings(
|
2024-06-08 04:13:25 +08:00
|
|
|
'bbb_multi_user_tools',
|
|
|
|
meetingClientSettingsInitialValues.public.whiteboard.toolbar.multiUserTools,
|
|
|
|
);
|
2024-09-18 23:21:16 +08:00
|
|
|
const allElements = document.querySelectorAll('[data-testid^="tools."]');
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (bbbMultiUserTools.length >= 1 && !isModerator) {
|
|
|
|
allElements.forEach((element) => {
|
|
|
|
const toolName = element.getAttribute('data-testid').split('.')[1];
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (!bbbMultiUserTools.includes(toolName)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
element.style.display = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-09-24 01:24:22 +08:00
|
|
|
// TODO: we should add the dependency list in [] parameter here
|
|
|
|
// so this is not run on every render
|
|
|
|
});
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
const bbbPresenterTools = getFromUserSettings(
|
|
|
|
'bbb_presenter_tools',
|
|
|
|
meetingClientSettingsInitialValues.public.whiteboard.toolbar.presenterTools,
|
|
|
|
);
|
|
|
|
const allElements = document.querySelectorAll('[data-testid^="tools."]');
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (bbbPresenterTools.length >= 1 && isPresenter) {
|
|
|
|
allElements.forEach((element) => {
|
|
|
|
const toolName = element.getAttribute('data-testid').split('.')[1];
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (!bbbPresenterTools.includes(toolName)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
element.style.display = 'none';
|
2024-06-08 04:13:25 +08:00
|
|
|
}
|
2024-09-18 23:21:16 +08:00
|
|
|
});
|
|
|
|
}
|
2024-09-24 01:24:22 +08:00
|
|
|
// TODO: we should add the dependency list in [] parameter here
|
|
|
|
// so this is not run on every render
|
|
|
|
});
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
const bbbMultiUserPenOnly = getFromUserSettings(
|
|
|
|
'bbb_multi_user_pen_only',
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
const allElements = document.querySelectorAll('[data-testid^="tools."]');
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
if (bbbMultiUserPenOnly && !isModerator && !isPresenter) {
|
|
|
|
allElements.forEach((element) => {
|
|
|
|
const toolName = element.getAttribute('data-testid').split('.')[1];
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2024-09-18 23:21:16 +08:00
|
|
|
const displayStyle = toolName !== 'draw' ? 'none' : 'flex';
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
element.style.display = displayStyle;
|
|
|
|
});
|
|
|
|
}
|
2024-09-24 01:24:22 +08:00
|
|
|
// TODO: we should add the dependency list in [] parameter here
|
|
|
|
// so this is not run on every render
|
|
|
|
});
|
2024-06-08 04:13:25 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={whiteboardRef}
|
2024-09-18 23:21:16 +08:00
|
|
|
id="whiteboard-element"
|
2024-07-01 22:19:42 +08:00
|
|
|
key={`animations=-${animations}-${whiteboardToolbarAutoHide}-${language}-${presentationId}`}
|
2023-12-06 02:15:25 +08:00
|
|
|
>
|
2022-12-14 00:11:56 +08:00
|
|
|
<Tldraw
|
2024-05-11 00:22:56 +08:00
|
|
|
autoFocus={false}
|
2024-07-06 04:09:19 +08:00
|
|
|
key={`tldrawv2-${presentationId}-${animations}-${isInfiniteWhiteboard}`}
|
2024-09-18 23:21:16 +08:00
|
|
|
forceMobile
|
|
|
|
hideUi={!(hasWBAccessRef.current || isPresenter)}
|
2023-12-06 02:15:25 +08:00
|
|
|
onMount={handleTldrawMount}
|
2024-04-11 08:53:42 +08:00
|
|
|
tools={customTools}
|
2023-12-06 02:15:25 +08:00
|
|
|
/>
|
|
|
|
<Styled.TldrawV2GlobalStyle
|
2024-04-04 10:00:56 +08:00
|
|
|
{...{
|
|
|
|
hasWBAccess: hasWBAccessRef.current,
|
|
|
|
isPresenter,
|
|
|
|
isRTL,
|
|
|
|
isMultiUserActive,
|
|
|
|
isToolbarVisible,
|
2024-05-08 08:50:31 +08:00
|
|
|
presentationHeight,
|
2024-04-04 10:00:56 +08:00
|
|
|
}}
|
2022-12-14 00:11:56 +08:00
|
|
|
/>
|
2023-04-14 23:27:37 +08:00
|
|
|
</div>
|
2022-04-05 22:49:13 +08:00
|
|
|
);
|
2023-12-06 02:15:25 +08:00
|
|
|
});
|
2023-03-10 19:30:46 +08:00
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
export default Whiteboard;
|
|
|
|
|
2023-03-10 19:30:46 +08:00
|
|
|
Whiteboard.propTypes = {
|
2024-02-17 04:25:43 +08:00
|
|
|
isPresenter: PropTypes.bool,
|
2023-03-10 19:30:46 +08:00
|
|
|
removeShapes: PropTypes.func.isRequired,
|
2024-01-24 19:37:51 +08:00
|
|
|
persistShapeWrapper: PropTypes.func.isRequired,
|
2023-03-10 19:30:46 +08:00
|
|
|
notifyNotAllowedChange: PropTypes.func.isRequired,
|
|
|
|
shapes: PropTypes.objectOf(PropTypes.shape).isRequired,
|
2024-02-17 04:25:43 +08:00
|
|
|
assets: PropTypes.arrayOf(PropTypes.shape).isRequired,
|
2023-03-10 19:30:46 +08:00
|
|
|
currentUser: PropTypes.shape({
|
|
|
|
userId: PropTypes.string.isRequired,
|
2024-02-17 04:25:43 +08:00
|
|
|
}),
|
2023-03-10 19:30:46 +08:00
|
|
|
whiteboardId: PropTypes.string,
|
|
|
|
zoomSlide: PropTypes.func.isRequired,
|
2024-09-20 03:17:11 +08:00
|
|
|
curPageNum: PropTypes.number.isRequired,
|
2024-10-05 00:28:06 +08:00
|
|
|
presentationWidth: PropTypes.number.isRequired,
|
2023-03-10 19:30:46 +08:00
|
|
|
presentationHeight: PropTypes.number.isRequired,
|
|
|
|
zoomChanger: PropTypes.func.isRequired,
|
|
|
|
isRTL: PropTypes.bool.isRequired,
|
|
|
|
fitToWidth: PropTypes.bool.isRequired,
|
|
|
|
zoomValue: PropTypes.number.isRequired,
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
2024-01-20 22:01:20 +08:00
|
|
|
colorStyle: PropTypes.string.isRequired,
|
|
|
|
dashStyle: PropTypes.string.isRequired,
|
|
|
|
fillStyle: PropTypes.string.isRequired,
|
|
|
|
fontStyle: PropTypes.string.isRequired,
|
|
|
|
sizeStyle: PropTypes.string.isRequired,
|
2023-03-10 19:30:46 +08:00
|
|
|
presentationAreaHeight: PropTypes.number.isRequired,
|
|
|
|
presentationAreaWidth: PropTypes.number.isRequired,
|
|
|
|
maxNumberOfAnnotations: PropTypes.number.isRequired,
|
|
|
|
darkTheme: PropTypes.bool.isRequired,
|
|
|
|
setTldrawIsMounting: PropTypes.func.isRequired,
|
2023-09-28 04:42:47 +08:00
|
|
|
presentationId: PropTypes.string,
|
2024-09-18 23:21:16 +08:00
|
|
|
setTldrawAPI: PropTypes.func.isRequired,
|
|
|
|
isMultiUserActive: PropTypes.bool,
|
|
|
|
whiteboardToolbarAutoHide: PropTypes.bool,
|
|
|
|
toggleToolsAnimations: PropTypes.func.isRequired,
|
|
|
|
animations: PropTypes.bool,
|
|
|
|
isToolbarVisible: PropTypes.bool,
|
|
|
|
isModerator: PropTypes.bool,
|
|
|
|
currentPresentationPage: PropTypes.shape(),
|
|
|
|
hasWBAccess: PropTypes.bool,
|
2024-09-20 03:17:11 +08:00
|
|
|
bgShape: PropTypes.arrayOf(PropTypes.shape).isRequired,
|
2024-09-18 23:21:16 +08:00
|
|
|
publishCursorUpdate: PropTypes.func.isRequired,
|
|
|
|
otherCursors: PropTypes.arrayOf(PropTypes.shape).isRequired,
|
|
|
|
hideViewersCursor: PropTypes.bool,
|
|
|
|
skipToSlide: PropTypes.func.isRequired,
|
|
|
|
locale: PropTypes.string.isRequired,
|
|
|
|
selectedLayout: PropTypes.string.isRequired,
|
|
|
|
isInfiniteWhiteboard: PropTypes.bool,
|
|
|
|
whiteboardWriters: PropTypes.arrayOf(PropTypes.shape).isRequired,
|
2023-03-10 19:30:46 +08:00
|
|
|
};
|