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

145 lines
4.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-31 22:07:59 +01:00
import {
$notify,
buildDesigner,
LocalStorageManager,
PersistenceManager,
RESTPersistenceManager,
DesignerOptionsBuilder,
2022-02-04 20:26:54 +01:00
Designer
2022-01-31 22:07:59 +01:00
} from '@wisemapping/mindplot';
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-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-02-04 20:26:54 +01:00
function loadLocaleData(locale: string) {
switch (locale) {
case 'fr':
return import('./compiled-lang/fr.json');
case 'en':
return import('./compiled-lang/en.json');
case 'es':
return import('./compiled-lang/es.json');
case 'de':
return import('./compiled-lang/de.json');
default:
return import('./compiled-lang/en.json')
}
}
2022-01-30 11:37:14 +01:00
const initMindplot = () => {
// 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 ...
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-31 22:07:59 +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-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();
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);
}
};
export default function Editor({
initCallback = initMindplot,
mapId,
memoryPersistence,
readOnlyMode,
locale = 'en',
onAction,
}: EditorPropsType): React.ReactElement {
2022-02-04 20:26:54 +01:00
const [localeTranslation, setLocaleTranslation] = React.useState(null);
React.useEffect(() => {
if(localeTranslation && !global.designer) {
initCallback();
}
}, [localeTranslation]);
React.useEffect(() => {
const loadAndSetLocale = async () => {
setLocaleTranslation(await loadLocaleData(locale));
};
loadAndSetLocale();
}, [locale])
if (!localeTranslation) {
return null;
}
2022-01-25 19:10:40 +01:00
return (
2022-02-04 20:26:54 +01:00
<IntlProvider locale={locale} defaultLocale="en" messages={localeTranslation}>
2022-01-31 22:07:59 +01:00
<Toolbar
memoryPersistence={memoryPersistence}
readOnlyMode={readOnlyMode}
onAction={onAction}
/>
2022-01-25 19:10:40 +01:00
<div id="mindplot"></div>
<Footer showTryPanel={memoryPersistence} />
</IntlProvider>
);
2022-01-31 22:07:59 +01:00
}