33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
"use strict";
|
|
|
|
/** Own creation, loading and destruction of the wiki's active CMap editor. */
|
|
export class CmapEditorHost {
|
|
constructor({ canvas, factory, createOptions = null, getModel = null, onCreated = null, onDestroyed = null }) {
|
|
this.canvas = canvas;
|
|
this.factory = factory;
|
|
this.createOptions = createOptions;
|
|
this.getModel = getModel;
|
|
this.onCreated = onCreated;
|
|
this.onDestroyed = onDestroyed;
|
|
this.editor = null;
|
|
}
|
|
|
|
create(options = null, model = null) {
|
|
this.destroy();
|
|
if (!this.factory) return null;
|
|
const editorOptions = options ?? this.createOptions?.() ?? {};
|
|
this.editor = this.factory(this.canvas, editorOptions);
|
|
const loadedModel = model || this.getModel?.();
|
|
if (loadedModel) this.editor.loadModel(loadedModel);
|
|
if (this.onCreated) this.onCreated(this.editor);
|
|
return this.editor;
|
|
}
|
|
|
|
destroy() {
|
|
if (!this.editor) return;
|
|
this.editor.destroy();
|
|
if (this.onDestroyed) this.onDestroyed(this.editor);
|
|
this.editor = null;
|
|
}
|
|
}
|