117 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-02-21 21:37:00 -08:00
import React, { useEffect } from 'react';
2022-01-25 18:10:40 +00:00
import Toolbar, { ToolbarActionType } from './components/toolbar';
import Footer from './components/footer';
import { IntlProvider } from 'react-intl';
2022-01-31 21:07:59 +00:00
import {
$notify,
buildDesigner,
PersistenceManager,
DesignerOptionsBuilder,
2022-02-13 03:15:51 +00:00
Designer,
DesignerKeyboard,
2022-01-31 21:07:59 +00:00
} from '@wisemapping/mindplot';
2022-02-05 14:10:37 -08:00
import FR from './compiled-lang/fr.json';
import ES from './compiled-lang/es.json';
import EN from './compiled-lang/en.json';
import DE from './compiled-lang/de.json';
2022-02-20 21:25:18 -08:00
import './global-styled.css';
2022-02-21 21:37:00 -08:00
import { EditorModeType } from '@wisemapping/mindplot/src/components/DesignerOptionsBuilder';
2022-01-30 02:37:14 -08:00
2022-02-04 19:26:54 +00:00
2022-02-21 21:37:00 -08:00
declare global {
2022-02-04 19:26:54 +00:00
// used in mindplot
var designer: Designer;
var accountEmail: string;
2022-01-25 18:10:40 +00:00
}
2022-02-21 21:37:00 -08: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-01-25 18:10:40 +00:00
onAction: (action: ToolbarActionType) => void;
2022-02-21 02:50:03 +00:00
persistenceManager: PersistenceManager;
2022-02-21 21:37:00 -08:00
initCallback?: (mapId: string, options: EditorOptions, persistenceManager: PersistenceManager) => void;
2022-01-25 18:10:40 +00:00
};
2022-02-06 11:12:20 -08:00
const loadLocaleData = (locale: string) => {
2022-02-04 19:26:54 +00:00
switch (locale) {
2022-02-05 14:10:37 -08:00
case 'fr':
return FR;
case 'en':
return EN;
case 'es':
return ES;
case 'de':
return DE;
default:
return EN;
2022-02-04 19:26:54 +00:00
}
2022-02-05 14:10:37 -08:00
}
2022-02-04 19:26:54 +00:00
2022-02-21 21:37:00 -08:00
const defaultCallback = (mapId: string, options: EditorOptions, persistenceManager: PersistenceManager) => {
2022-01-30 02:37:14 -08:00
// Change page title ...
2022-02-21 21:37:00 -08:00
document.title = `${options.mapTitle} | WiseMapping `;
2022-01-30 02:37:14 -08:00
2022-02-21 21:37:00 -08:00
const buildOptions = DesignerOptionsBuilder.buildOptions({
2022-02-21 02:50:03 +00:00
persistenceManager,
2022-02-21 21:37:00 -08:00
mode: options.mode,
mapId: mapId,
2022-01-25 18:10:40 +00:00
container: 'mindplot',
2022-02-21 21:37:00 -08:00
zoom: options.zoom,
locale: options.locale,
2022-01-25 18:10:40 +00:00
});
// Build designer ...
2022-02-21 21:37:00 -08:00
const designer = buildDesigner(buildOptions);
2022-01-25 18:10:40 +00:00
// Load map from XML file persisted on disk...
const instance = PersistenceManager.getInstance();
2022-02-21 21:37:00 -08:00
const mindmap = instance.load(mapId);
2022-01-25 18:10:40 +00:00
designer.loadMap(mindmap);
2022-02-21 21:37:00 -08:00
if (options.locked) {
$notify(options.lockedMsg);
2022-01-25 18:10:40 +00:00
}
};
2022-02-06 11:12:20 -08:00
const Editor = ({
2022-01-25 18:10:40 +00:00
mapId,
2022-02-21 21:37:00 -08:00
options,
2022-02-21 02:50:03 +00:00
persistenceManager,
2022-02-21 21:37:00 -08:00
initCallback = defaultCallback,
onAction,
}: EditorProps) => {
useEffect(() => {
initCallback(mapId, options, persistenceManager);
2022-02-06 11:12:20 -08:00
}, []);
2022-02-21 21:37:00 -08:00
useEffect(() => {
if (options.enableKeyboardEvents) {
2022-02-13 03:15:51 +00:00
DesignerKeyboard.resume();
} else {
DesignerKeyboard.pause();
}
2022-02-21 21:37:00 -08:00
}, [options.enableKeyboardEvents]);
2022-02-13 03:15:51 +00:00
2022-01-25 18:10:40 +00:00
return (
2022-02-21 21:37:00 -08:00
<IntlProvider locale={options.locale} messages={loadLocaleData(options.locale)}>
2022-01-31 21:07:59 +00:00
<Toolbar
2022-02-21 21:37:00 -08:00
isTryMode={options.mode === 'showcase'}
2022-01-31 21:07:59 +00:00
onAction={onAction}
/>
2022-01-25 18:10:40 +00:00
<div id="mindplot"></div>
2022-02-21 21:37:00 -08:00
<Footer showTryPanel={options.mode === 'showcase'} />
2022-01-25 18:10:40 +00:00
</IntlProvider>
);
2022-01-31 21:07:59 +00:00
}
2022-02-06 11:12:20 -08:00
export default Editor;