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

108 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-02-22 06:37:00 +01:00
import React, { useEffect } from 'react';
2022-01-25 19:10:40 +01:00
import Toolbar, { ToolbarActionType } from './components/toolbar';
import Footer from './components/footer';
import { IntlProvider } from 'react-intl';
2022-01-31 22:07:59 +01:00
import {
$notify,
buildDesigner,
PersistenceManager,
DesignerOptionsBuilder,
2022-02-13 04:15:51 +01:00
Designer,
DesignerKeyboard,
2022-01-31 22:07:59 +01:00
} from '@wisemapping/mindplot';
2022-02-21 06:25:18 +01:00
import './global-styled.css';
2022-02-22 06:37:00 +01:00
import { EditorModeType } from '@wisemapping/mindplot/src/components/DesignerOptionsBuilder';
2022-02-25 16:33:37 +01:00
import I18nMsg from './classes/i18n-msg';
2022-02-04 20:26:54 +01:00
2022-02-22 06:37:00 +01:00
declare global {
2022-02-04 20:26:54 +01:00
// used in mindplot
var designer: Designer;
var accountEmail: string;
2022-01-25 19:10:40 +01:00
}
2022-02-22 06:37:00 +01:00
export type EditorOptions = {
mode: EditorModeType,
locale: string,
zoom?: number,
locked?: boolean,
lockedMsg?: string;
mapTitle: string;
enableKeyboardEvents: boolean;
}
export type EditorProps = {
mapId: string;
options: EditorOptions;
2022-02-21 03:50:03 +01:00
persistenceManager: PersistenceManager;
2022-02-25 16:33:37 +01:00
onAction: (action: ToolbarActionType) => void;
onLoad?: (designer: Designer) => void;
2022-01-25 19:10:40 +01:00
};
2022-02-06 20:12:20 +01:00
const Editor = ({
2022-01-25 19:10:40 +01:00
mapId,
2022-02-22 06:37:00 +01:00
options,
2022-02-21 03:50:03 +01:00
persistenceManager,
2022-02-22 06:37:00 +01:00
onAction,
2022-02-25 16:33:37 +01:00
onLoad,
2022-02-22 06:37:00 +01:00
}: EditorProps) => {
2022-02-25 16:33:37 +01:00
2022-02-22 06:37:00 +01:00
useEffect(() => {
2022-02-25 16:33:37 +01:00
// Change page title ...
document.title = `${options.mapTitle} | WiseMapping `;
// Load mindmap ...
const designer = onLoadDesigner(mapId, options, persistenceManager);
// Has extended actions been customized ...
if (onLoad) {
onLoad(designer);
}
// Load mindmap ...
const instance = PersistenceManager.getInstance();
const mindmap = instance.load(mapId);
designer.loadMap(mindmap);
if (options.locked) {
$notify(options.lockedMsg);
}
2022-02-06 20:12:20 +01:00
}, []);
2022-02-22 06:37:00 +01:00
useEffect(() => {
if (options.enableKeyboardEvents) {
2022-02-13 04:15:51 +01:00
DesignerKeyboard.resume();
} else {
DesignerKeyboard.pause();
}
2022-02-22 06:37:00 +01:00
}, [options.enableKeyboardEvents]);
2022-02-13 04:15:51 +01:00
2022-02-25 16:33:37 +01:00
const onLoadDesigner = (mapId: string, options: EditorOptions, persistenceManager: PersistenceManager): Designer => {
const buildOptions = DesignerOptionsBuilder.buildOptions({
persistenceManager,
mode: options.mode,
mapId: mapId,
container: 'mindplot',
zoom: options.zoom,
locale: options.locale,
});
// Build designer ...
return buildDesigner(buildOptions);
};
const locale = options.locale;
const msg = I18nMsg.loadLocaleData(locale);
2022-01-25 19:10:40 +01:00
return (
2022-02-25 16:33:37 +01:00
<IntlProvider locale={locale} messages={msg}>
2022-01-31 22:07:59 +01:00
<Toolbar
2022-02-22 06:37:00 +01:00
isTryMode={options.mode === 'showcase'}
2022-01-31 22:07:59 +01:00
onAction={onAction}
/>
<div id="mindplot"></div>
2022-02-22 06:37:00 +01:00
<Footer showTryPanel={options.mode === 'showcase'} />
2022-01-25 19:10:40 +01:00
</IntlProvider>
);
2022-01-31 22:07:59 +01:00
}
2022-02-06 20:12:20 +01:00
export default Editor;