Lazy loading for map

This commit is contained in:
Paulo Gustavo Veiga 2022-11-15 22:45:59 -08:00
parent 2d133f33fb
commit a96c9902d6
7 changed files with 51 additions and 62 deletions

View File

@ -32,7 +32,7 @@ const ColorPicker = (props: {
<Box component="div" sx={{ m: 2 }}> <Box component="div" sx={{ m: 2 }}>
<ReactColorPicker <ReactColorPicker
color={props.colorModel.getValue() || '#fff'} color={props.colorModel.getValue() || '#fff'}
onChangeComplete={(color) => { onChangeComplete={(color: { hex: string }) => {
props.colorModel.setValue(color.hex); props.colorModel.setValue(color.hex);
props.closeModal(); props.closeModal();
}} }}
@ -40,7 +40,7 @@ const ColorPicker = (props: {
width={216} width={216}
circleSpacing={9} circleSpacing={9}
circleSize={18} circleSize={18}
></ReactColorPicker> />
</Box> </Box>
); );
}; };

View File

@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import $ from 'jquery';
import PersistenceManager from './PersistenceManager'; import PersistenceManager from './PersistenceManager';
class LocalStorageManager extends PersistenceManager { class LocalStorageManager extends PersistenceManager {
@ -47,36 +46,36 @@ class LocalStorageManager extends PersistenceManager {
} }
} }
loadMapDom(mapId: string) { loadMapDom(mapId: string): Promise<Document> {
let xml; let result: Promise<Document>;
let localStorate;
if (!this.readOnly) { if (!this.readOnly) {
xml = localStorage.getItem(`${mapId}-xml`); localStorate = localStorage.getItem(`${mapId}-xml`);
} }
if (xml == null || this.forceLoad) {
$.ajax({ if (localStorate == null || this.forceLoad) {
url: this.documentUrl.replace('{id}', mapId), const url = this.documentUrl.replace('{id}', mapId);
result = fetch(url, {
method: 'get',
headers: { headers: {
'Content-Type': 'text/plain', 'Content-Type': 'text/plain',
Accept: 'application/xml', Accept: 'application/xml',
'X-CSRF-Token': this.getCSRFToken(), 'X-CSRF-Token': this.getCSRFToken(),
}, },
type: 'get', })
dataType: 'text', .then((response: Response) => {
async: false, if (!response.ok) {
success(response) { console.error(`load error: ${response.status}`);
xml = response; throw new Error(`load error: ${response.status}, ${response.statusText}`);
},
error(xhr, ajaxOptions, thrownError) {
console.error(`Request error => status:${xhr.status} ,thrownError: ${thrownError}`);
},
});
// If I could not load it from a file, hard code one.
if (xml == null) {
throw new Error(`Map could not be loaded with id:${mapId}`);
} }
return response.text();
})
.then((xmlStr) => new DOMParser().parseFromString(xmlStr, 'text/xml'));
} else {
const doc = new DOMParser().parseFromString(localStorate, 'text/xml');
result = Promise.resolve(doc);
} }
return result;
return $.parseXML(xml);
} }
unlockMap(): void { unlockMap(): void {

View File

@ -100,9 +100,9 @@ class MindplotWebComponent extends HTMLElement {
* Load map in designer throught persistence manager instance * Load map in designer throught persistence manager instance
* @param id the map id to be loaded. * @param id the map id to be loaded.
*/ */
loadMap(id: string) { async loadMap(id: string): Promise<void> {
const instance = PersistenceManager.getInstance(); const instance = PersistenceManager.getInstance();
this._mindmap = instance.load(id); this._mindmap = await instance.load(id);
this._designer.loadMap(this._mindmap); this._designer.loadMap(this._mindmap);
} }

View File

@ -36,8 +36,8 @@ class MockPersistenceManager extends PersistenceManager {
// Ignore, no implementation required ... // Ignore, no implementation required ...
} }
loadMapDom() { loadMapDom(): Promise<Document> {
return $.parseXML(this.exampleMap); return Promise.resolve($.parseXML(this.exampleMap));
} }
unlockMap(): void { unlockMap(): void {

View File

@ -61,10 +61,12 @@ abstract class PersistenceManager {
return result; return result;
} }
load(mapId: string) { load(mapId: string): Promise<Mindmap> {
$assert(mapId, 'mapId can not be null'); $assert(mapId, 'mapId can not be null');
const domDocument = this.loadMapDom(mapId); return this.loadMapDom(mapId).then((document) => {
return PersistenceManager.loadFromDom(mapId, domDocument); console.log(`Loding map with is ${mapId}}`);
return PersistenceManager.loadFromDom(mapId, document);
});
} }
triggerError(error: PersistenceError) { triggerError(error: PersistenceError) {
@ -75,7 +77,7 @@ abstract class PersistenceManager {
this._errorHandlers.push(callback); this._errorHandlers.push(callback);
} }
removeErrorHandler(callback?: PersistenceErrorCallback) { removeErrorHandler(callback?: PersistenceErrorCallback): void {
if (!callback) { if (!callback) {
this._errorHandlers.length = 0; this._errorHandlers.length = 0;
} }
@ -87,7 +89,7 @@ abstract class PersistenceManager {
abstract discardChanges(mapId: string): void; abstract discardChanges(mapId: string): void;
abstract loadMapDom(mapId: string): Document; abstract loadMapDom(mapId: string): Promise<Document>;
abstract saveMapXml(mapId: string, mapXml: Document, pref?, saveHistory?: boolean, events?); abstract saveMapXml(mapId: string, mapXml: Document, pref?, saveHistory?: boolean, events?);

View File

@ -16,7 +16,6 @@
* limitations under the License. * limitations under the License.
*/ */
import { $assert } from '@wisemapping/core-js'; import { $assert } from '@wisemapping/core-js';
import $ from 'jquery';
import { $msg } from './Messages'; import { $msg } from './Messages';
import PersistenceManager, { PersistenceError } from './PersistenceManager'; import PersistenceManager, { PersistenceError } from './PersistenceManager';
@ -157,31 +156,24 @@ class RESTPersistenceManager extends PersistenceManager {
return { severity, message }; return { severity, message };
} }
loadMapDom(mapId: string): Document { loadMapDom(mapId: string): Promise<Document> {
let xml: Document; const url = `${this.documentUrl.replace('{id}', mapId)}/xml`;
$.ajax({ return fetch(url, {
url: `${this.documentUrl.replace('{id}', mapId)}/xml`,
method: 'get', method: 'get',
async: false,
headers: { headers: {
'Content-Type': 'text/plain', 'Content-Type': 'text/plain',
Accept: 'application/xml', Accept: 'application/xml',
'X-CSRF-Token': this.getCSRFToken(), 'X-CSRF-Token': this.getCSRFToken(),
}, },
success(responseText) { })
xml = responseText; .then((response: Response) => {
}, if (!response.ok) {
error(xhr, ajaxOptions, thrownError) { console.error(`load error: ${response.status}`);
console.error(`Request error => status:${xhr.status} ,thrownError: ${thrownError}`); throw new Error(`load error: ${response.status}, ${response.statusText}`);
},
});
// If I could not load it from a file, hard code one.
if (xml == null) {
throw new Error(`Map with id ${mapId} could not be loaded`);
} }
return response.text();
return xml; })
.then((xmlStr) => new DOMParser().parseFromString(xmlStr, 'text/xml'));
} }
} }

View File

@ -1,13 +1,9 @@
import './wdyr'; // <--- first import // import './wdyr';
import React from 'react'; import React from 'react';
import App from './app'; import App from './app';
import { createRoot } from 'react-dom/client'; import { createRoot } from 'react-dom/client';
async function bootstrapApplication() { const container = document.getElementById('root') as HTMLElement;
const container = document.getElementById('root') as HTMLElement; const root = createRoot(container!);
const root = createRoot(container!); root.render(<App />);
root.render(<App />);
}
bootstrapApplication();