Files
racket-wiki/static/js/wiki/cmap/cmap-editor-host.js
T
2026-09-03 17:24:56 +02:00

39 lines
1.3 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;
}
/**
* goal : Replace any active editor with a newly configured editor instance.
* post : The previous editor is destroyed; an optional model is loaded before onCreated runs.
* result : The active editor, or null when no factory is configured.
*/
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 the active editor once and release the host's reference to it. */
destroy() {
if (!this.editor) return;
this.editor.destroy();
if (this.onDestroyed) this.onDestroyed(this.editor);
this.editor = null;
}
}