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

148 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-01-25 19:10:40 +01:00
import React from 'react';
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,
LocalStorageManager,
PersistenceManager,
RESTPersistenceManager,
DesignerOptionsBuilder,
2022-02-13 04:15:51 +01:00
Designer,
DesignerKeyboard,
2022-01-31 22:07:59 +01:00
} from '@wisemapping/mindplot';
2022-02-05 23:10:37 +01: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-01-30 11:37:14 +01:00
2022-01-25 19:10:40 +01:00
declare global {
var memoryPersistence: boolean;
var readOnly: boolean;
var lockTimestamp: string;
var lockSession: string;
var historyId: string;
var isAuth: boolean;
var mapId: number;
var userOptions: { zoom: string | number } | null;
var locale: string;
var mindmapLocked: boolean;
var mindmapLockedMsg: string;
2022-02-04 20:26:54 +01:00
var mapTitle: string;
// used in mindplot
var designer: Designer;
var accountEmail: string;
2022-01-25 19:10:40 +01:00
}
2022-01-25 19:10:40 +01:00
export type EditorPropsType = {
2022-02-10 01:48:33 +01:00
initCallback?: (locale: string) => void;
2022-02-06 20:12:20 +01:00
mapId?: number;
isTryMode: boolean;
2022-01-25 19:10:40 +01:00
readOnlyMode: boolean;
locale?: string;
onAction: (action: ToolbarActionType) => void;
2022-02-13 04:15:51 +01:00
hotkeys?: boolean;
2022-01-25 19:10:40 +01:00
};
2022-02-06 20:12:20 +01:00
const loadLocaleData = (locale: string) => {
2022-02-04 20:26:54 +01:00
switch (locale) {
2022-02-05 23:10:37 +01:00
case 'fr':
return FR;
case 'en':
return EN;
case 'es':
return ES;
case 'de':
return DE;
default:
return EN;
2022-02-04 20:26:54 +01:00
}
2022-02-05 23:10:37 +01:00
}
2022-02-04 20:26:54 +01:00
2022-02-10 01:48:33 +01:00
const initMindplot = (locale: string) => {
2022-01-30 11:37:14 +01:00
// Change page title ...
2022-01-31 22:07:59 +01:00
document.title = `${global.mapTitle} | WiseMapping `;
2022-01-30 11:37:14 +01:00
// Configure persistence manager ...
2022-02-06 20:12:20 +01:00
let persistence: PersistenceManager;
2022-01-25 19:10:40 +01:00
if (!global.memoryPersistence && !global.readOnly) {
persistence = new RESTPersistenceManager({
documentUrl: '/c/restful/maps/{id}/document',
revertUrl: '/c/restful/maps/{id}/history/latest',
lockUrl: '/c/restful/maps/{id}/lock',
timestamp: global.lockTimestamp,
session: global.lockSession,
});
} else {
persistence = new LocalStorageManager(
2022-02-05 23:10:37 +01:00
`/c/restful/maps/{id}/${global.historyId ? `${global.historyId}/` : ''}document/xml${!global.isAuth ? '-pub' : ''
2022-01-25 19:10:40 +01:00
}`,
true
);
}
const params = new URLSearchParams(window.location.search.substring(1));
const zoomParam = Number.parseFloat(params.get('zoom'));
const options = DesignerOptionsBuilder.buildOptions({
persistenceManager: persistence,
readOnly: Boolean(global.readOnly || false),
2022-01-30 11:37:14 +01:00
mapId: String(global.mapId),
2022-01-25 19:10:40 +01:00
container: 'mindplot',
2022-01-31 22:07:59 +01:00
zoom:
zoomParam ||
(global.userOptions?.zoom != undefined
? Number.parseFloat(global.userOptions.zoom as string)
: 0.8),
2022-02-10 01:48:33 +01:00
locale: locale,
2022-01-25 19:10:40 +01:00
});
// Build designer ...
const designer = buildDesigner(options);
// Load map from XML file persisted on disk...
const instance = PersistenceManager.getInstance();
2022-01-31 01:05:22 +01:00
const mindmap = instance.load(String(global.mapId));
2022-01-25 19:10:40 +01:00
designer.loadMap(mindmap);
if (global.mindmapLocked) {
$notify(global.mindmapLockedMsg);
}
};
2022-02-06 20:12:20 +01:00
const Editor = ({
2022-01-25 19:10:40 +01:00
initCallback = initMindplot,
mapId,
2022-02-06 20:12:20 +01:00
isTryMode: isTryMode,
2022-01-25 19:10:40 +01:00
locale = 'en',
onAction,
2022-02-13 04:15:51 +01:00
hotkeys = true,
2022-02-06 20:12:20 +01:00
}: EditorPropsType): React.ReactElement => {
2022-02-04 20:26:54 +01:00
React.useEffect(() => {
2022-02-10 01:48:33 +01:00
initCallback(locale);
2022-02-06 20:12:20 +01:00
}, []);
2022-02-13 04:15:51 +01:00
React.useEffect(() => {
if (hotkeys) {
DesignerKeyboard.resume();
} else {
DesignerKeyboard.pause();
}
}, [hotkeys]);
2022-01-25 19:10:40 +01:00
return (
2022-02-10 01:48:33 +01:00
<IntlProvider locale={locale} messages={loadLocaleData(locale)}>
2022-01-31 22:07:59 +01:00
<Toolbar
2022-02-06 20:12:20 +01:00
isTryMode={isTryMode}
2022-01-31 22:07:59 +01:00
onAction={onAction}
/>
2022-01-25 19:10:40 +01:00
<div id="mindplot"></div>
2022-02-06 20:12:20 +01:00
<Footer showTryPanel={isTryMode} />
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;