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

102 lines
3.2 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-30 11:37:14 +01:00
import { $notify, buildDesigner, LocalStorageManager, PersistenceManager, RESTPersistenceManager, DesignerOptionsBuilder } from '@wisemapping/mindplot';
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-01-25 19:10:40 +01:00
export type EditorPropsType = {
2022-01-30 11:37:14 +01:00
initCallback?: () => void;
2022-01-25 19:10:40 +01:00
mapId: number;
memoryPersistence: boolean;
readOnlyMode: boolean;
locale?: string;
onAction: (action: ToolbarActionType) => void;
};
2022-01-30 11:37:14 +01:00
const initMindplot = () => {
// Change page title ...
document.title = `${global.mapTitle} | WiseMapping `
// Configure persistence manager ...
let persistence;
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-01-30 11:37:14 +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-30 17:35:51 +01:00
zoom: zoomParam || (global.userOptions?.zoom != undefined ? Number.parseFloat(global.userOptions.zoom as string) : 0.8),
2022-01-25 19:10:40 +01:00
locale: global.locale,
});
// Build designer ...
const designer = buildDesigner(options);
// Load map from XML file persisted on disk...
const instance = PersistenceManager.getInstance();
const mindmap = instance.load(global.mapId);
designer.loadMap(mindmap);
if (global.mindmapLocked) {
$notify(global.mindmapLockedMsg);
}
};
export default function Editor({
initCallback = initMindplot,
mapId,
memoryPersistence,
readOnlyMode,
locale = 'en',
onAction,
}: EditorPropsType): React.ReactElement {
2022-01-30 11:37:14 +01:00
React.useEffect(initCallback, []);
2022-01-25 19:10:40 +01:00
return (
<IntlProvider locale={locale} defaultLocale="en" messages={{}}>
<div id="header">
<Toolbar
memoryPersistence={memoryPersistence}
readOnlyMode={readOnlyMode}
onAction={onAction}
/>
</div>
<div id="mindplot"></div>
<Footer showTryPanel={memoryPersistence} />
</IntlProvider>
);
2022-01-30 11:37:14 +01:00
}