2023-03-10 19:30:46 +08:00
|
|
|
import * as React from 'react';
|
2023-02-24 07:47:34 +08:00
|
|
|
import ReactDOM from 'react-dom';
|
2023-03-10 19:30:46 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2023-02-24 07:47:34 +08:00
|
|
|
import { HUNDRED_PERCENT } from '/imports/utils/slideCalcUtils';
|
|
|
|
import Styled from '../styles';
|
|
|
|
|
|
|
|
const DEFAULT_TOOL_COUNT = 9;
|
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
class PanToolInjector extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
this.addPanTool();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const {
|
|
|
|
zoomValue,
|
|
|
|
fitToWidth,
|
|
|
|
isPanning,
|
|
|
|
setIsPanning,
|
|
|
|
tldrawAPI,
|
|
|
|
panSelected,
|
2023-03-10 19:30:46 +08:00
|
|
|
setPanSelected,
|
2023-02-25 01:17:54 +08:00
|
|
|
} = this.props;
|
|
|
|
if (prevProps.zoomValue !== zoomValue
|
|
|
|
|| prevProps.fitToWidth !== fitToWidth
|
|
|
|
|| prevProps.isPanning !== isPanning
|
|
|
|
|| prevProps.tldrawAPI !== tldrawAPI
|
|
|
|
|| prevProps.panSelected !== panSelected
|
|
|
|
) {
|
|
|
|
this.addPanTool();
|
2023-02-25 06:51:03 +08:00
|
|
|
if (panSelected) {
|
|
|
|
setIsPanning(true);
|
|
|
|
setPanSelected(true);
|
|
|
|
} else {
|
|
|
|
setIsPanning(false);
|
|
|
|
setPanSelected(false);
|
|
|
|
}
|
2023-02-25 01:17:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addPanTool() {
|
|
|
|
const {
|
|
|
|
zoomValue,
|
|
|
|
fitToWidth,
|
|
|
|
setIsPanning,
|
|
|
|
formatMessage,
|
|
|
|
tldrawAPI,
|
|
|
|
panSelected,
|
2023-03-10 19:30:46 +08:00
|
|
|
setPanSelected,
|
2023-02-25 01:17:54 +08:00
|
|
|
} = this.props;
|
2023-02-24 07:47:34 +08:00
|
|
|
|
2023-03-01 10:03:33 +08:00
|
|
|
if (panSelected) {
|
2023-03-01 11:32:24 +08:00
|
|
|
tldrawAPI?.selectTool('select');
|
2023-03-01 10:03:33 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 07:47:34 +08:00
|
|
|
const tools = document.querySelectorAll('[id*="TD-PrimaryTools-"]');
|
2023-03-10 19:30:46 +08:00
|
|
|
tools.forEach((tool) => {
|
|
|
|
const { classList } = tool.firstElementChild;
|
2023-02-24 07:47:34 +08:00
|
|
|
if (panSelected) {
|
|
|
|
classList.add('overrideSelect');
|
|
|
|
} else {
|
|
|
|
classList.remove('overrideSelect');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
const parentElement = document.getElementById('TD-PrimaryTools');
|
|
|
|
if (!parentElement) return;
|
2023-02-24 07:47:34 +08:00
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
if (parentElement.childElementCount === DEFAULT_TOOL_COUNT) {
|
|
|
|
parentElement.removeChild(parentElement.children[1]);
|
|
|
|
}
|
2023-02-24 07:47:34 +08:00
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
if (parentElement.childElementCount < DEFAULT_TOOL_COUNT) {
|
|
|
|
const label = formatMessage({
|
|
|
|
id: 'app.whiteboard.toolbar.tools.hand',
|
|
|
|
description: 'presentation toolbar pan label',
|
|
|
|
});
|
|
|
|
const container = document.createElement('span');
|
|
|
|
parentElement.appendChild(container);
|
|
|
|
ReactDOM.render(
|
|
|
|
<Styled.PanTool
|
2023-03-10 19:30:46 +08:00
|
|
|
key="bbb-panBtn"
|
2023-02-25 01:17:54 +08:00
|
|
|
role="button"
|
|
|
|
data-test="panButton"
|
2023-02-25 06:51:03 +08:00
|
|
|
data-zoom={zoomValue}
|
2023-03-10 19:30:46 +08:00
|
|
|
className={panSelected ? 'select' : 'overrideSelect'}
|
2023-02-25 01:17:54 +08:00
|
|
|
color="light"
|
|
|
|
icon="hand"
|
|
|
|
size="md"
|
2023-03-12 01:11:48 +08:00
|
|
|
label={label}
|
|
|
|
aria-label={label}
|
2023-02-25 01:17:54 +08:00
|
|
|
onClick={() => {
|
2023-02-25 06:51:03 +08:00
|
|
|
setPanSelected(true);
|
|
|
|
setIsPanning(true);
|
|
|
|
if (!(zoomValue <= HUNDRED_PERCENT && !fitToWidth)) {
|
|
|
|
const panButton = document.querySelector('[data-test="panButton"]');
|
|
|
|
if (panButton) {
|
|
|
|
panButton.classList.remove('selectOverride');
|
|
|
|
panButton.classList.add('select');
|
|
|
|
}
|
2023-02-25 01:17:54 +08:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
hideLabel
|
2023-04-02 22:42:12 +08:00
|
|
|
{...{
|
|
|
|
panSelected,
|
|
|
|
}}
|
2023-02-25 01:17:54 +08:00
|
|
|
/>,
|
2023-03-10 19:30:46 +08:00
|
|
|
container,
|
2023-02-25 01:17:54 +08:00
|
|
|
);
|
2023-03-10 19:30:46 +08:00
|
|
|
const { lastChild } = parentElement;
|
2023-02-25 01:17:54 +08:00
|
|
|
const secondChild = parentElement.children[1];
|
|
|
|
parentElement.insertBefore(lastChild, secondChild);
|
|
|
|
}
|
2023-02-24 07:47:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 07:47:34 +08:00
|
|
|
|
2023-03-10 19:30:46 +08:00
|
|
|
PanToolInjector.propTypes = {
|
|
|
|
fitToWidth: PropTypes.bool.isRequired,
|
|
|
|
zoomValue: PropTypes.number.isRequired,
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
isPanning: PropTypes.bool.isRequired,
|
|
|
|
setIsPanning: PropTypes.func.isRequired,
|
|
|
|
tldrawAPI: PropTypes.shape({
|
|
|
|
selectTool: PropTypes.func.isRequired,
|
2023-03-10 20:36:54 +08:00
|
|
|
}),
|
2023-03-10 19:30:46 +08:00
|
|
|
panSelected: PropTypes.bool.isRequired,
|
|
|
|
setPanSelected: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2023-03-10 20:36:54 +08:00
|
|
|
PanToolInjector.defaultProps = {
|
|
|
|
tldrawAPI: null,
|
|
|
|
};
|
|
|
|
|
2023-02-25 01:17:54 +08:00
|
|
|
export default PanToolInjector;
|