wisemapping-frontend/packages/editor/src/components/index.tsx

147 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-10-08 06:52:39 +02:00
/*
* Copyright [2021] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-10-08 05:22:27 +02:00
import React, { useCallback, useEffect, useRef, useState } from 'react';
import Popover from '@mui/material/Popover';
2022-10-08 22:46:52 +02:00
import Model from '../classes/model/editor';
2022-10-08 23:52:40 +02:00
import {
2022-10-10 02:44:55 +02:00
buildAppBarConfig,
2022-10-08 23:52:40 +02:00
buildToolbarConfig,
2022-10-10 02:44:55 +02:00
buildZoomToolbarConfig,
} from './toolbar/toolbarConfigBuilder';
2022-10-08 05:22:27 +02:00
import { IntlProvider } from 'react-intl';
2022-10-08 23:18:41 +02:00
import { DesignerKeyboard, MindplotWebComponent } from '@wisemapping/mindplot';
2022-10-08 05:22:27 +02:00
import I18nMsg from '../classes/i18n-msg';
2022-10-08 23:52:40 +02:00
import Toolbar from './toolbar';
2022-10-08 05:22:27 +02:00
import { theme as defaultEditorTheme } from '../theme';
import ThemeProvider from '@mui/material/styles/ThemeProvider';
import { Theme } from '@mui/material/styles';
2022-10-08 19:12:07 +02:00
import { Notifier } from './warning-dialog/styled';
import WarningDialog from './warning-dialog';
2022-10-08 05:22:27 +02:00
import DefaultWidgetManager from '../classes/default-widget-manager';
import AppBar from './app-bar';
2022-10-08 23:18:41 +02:00
import { EditorProps } from '..';
2022-10-08 19:12:07 +02:00
import Capability from '../classes/action/capability';
2022-10-08 05:22:27 +02:00
const Editor = ({
mapId,
options,
persistenceManager,
onAction,
theme,
accountConfiguration,
}: EditorProps) => {
2022-10-10 02:44:55 +02:00
const [model, setModel]: [Model | undefined, Function] = useState();
2022-10-08 05:22:27 +02:00
const editorTheme: Theme = theme ? theme : defaultEditorTheme;
const [toolbarsRerenderSwitch, setToolbarsRerenderSwitch] = useState(0);
const toolbarConfiguration = useRef([]);
2022-10-08 19:12:07 +02:00
2022-10-08 23:18:41 +02:00
const [popoverOpen, popoverTarget, widgetManager] = DefaultWidgetManager.create();
2022-10-08 19:12:07 +02:00
const capability = new Capability(options.mode, options.locked);
2022-10-08 23:18:41 +02:00
const mindplotRef = useCallback((component: MindplotWebComponent) => {
2022-10-10 02:44:55 +02:00
// Initialized model ...
const model = new Model(component);
model.loadMindmap(mapId, persistenceManager, widgetManager);
model.registerEvents(setToolbarsRerenderSwitch, capability);
setModel(model);
2022-10-08 05:22:27 +02:00
}, []);
useEffect(() => {
2022-10-10 02:44:55 +02:00
if (model) {
toolbarConfiguration.current = buildToolbarConfig(model.getDesigner());
2022-10-08 23:18:41 +02:00
}
2022-10-10 02:44:55 +02:00
}, [model]);
2022-10-08 05:22:27 +02:00
useEffect(() => {
if (options.enableKeyboardEvents) {
DesignerKeyboard.resume();
} else {
DesignerKeyboard.pause();
}
}, [options.enableKeyboardEvents]);
2022-10-08 23:18:41 +02:00
// Initialize locate ...
2022-10-08 05:22:27 +02:00
const locale = options.locale;
const msg = I18nMsg.loadLocaleData(locale);
2022-10-08 19:12:07 +02:00
2022-10-08 05:22:27 +02:00
return (
<ThemeProvider theme={editorTheme}>
<IntlProvider locale={locale} messages={msg}>
2022-10-10 06:00:53 +02:00
<AppBar
configurations={buildAppBarConfig(
model,
options.mapTitle,
capability,
onAction,
accountConfiguration,
() => {
model.save(true);
},
)}
/>
2022-10-08 05:22:27 +02:00
<Popover
id="popover"
open={popoverOpen}
anchorEl={popoverTarget}
onClose={widgetManager.handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
>
{widgetManager.getEditorContent()}
</Popover>
2022-10-08 19:12:07 +02:00
{!capability.isHidden('edition-toolbar') && (
2022-10-08 05:22:27 +02:00
<Toolbar
configurations={toolbarConfiguration.current}
rerender={toolbarsRerenderSwitch}
></Toolbar>
)}
<Toolbar
2022-10-10 06:00:53 +02:00
configurations={buildZoomToolbarConfig(model, capability)}
2022-10-10 02:44:55 +02:00
position={{
position: {
right: '7px',
top: '93%',
},
vertical: false,
}}
2022-10-08 05:22:27 +02:00
rerender={toolbarsRerenderSwitch}
></Toolbar>
2022-10-08 22:46:52 +02:00
2022-10-08 05:22:27 +02:00
<mindplot-component
ref={mindplotRef}
id="mindmap-comp"
mode={options.mode}
locale={options.locale}
></mindplot-component>
2022-10-08 22:46:52 +02:00
2022-10-08 05:22:27 +02:00
<Notifier id="headerNotifier"></Notifier>
2022-10-08 22:46:52 +02:00
<WarningDialog
capability={capability}
message={options.locked ? options.lockedMsg : ''}
></WarningDialog>
2022-10-08 05:22:27 +02:00
</IntlProvider>
</ThemeProvider>
);
};
export default Editor;