52 lines
2.3 KiB
JavaScript
52 lines
2.3 KiB
JavaScript
"use strict";
|
|
|
|
/** Coordinate CMap import and export workflows without owning their formats. */
|
|
export class CmapTransferController {
|
|
constructor({ repository, jsonImporter, jsonExporter, markdownExporter,
|
|
getCurrentMap, getPages, getConceptMaps, loadPages, loadConceptMaps,
|
|
navigateToMap, saveBeforeTransfer, confirm, status, translate }) {
|
|
Object.assign(this, {
|
|
repository, jsonImporter, jsonExporter, markdownExporter, getCurrentMap,
|
|
getPages, getConceptMaps, loadPages, loadConceptMaps, navigateToMap,
|
|
saveBeforeTransfer, confirm, status, translate
|
|
});
|
|
}
|
|
|
|
/** Save pending changes, then create the requested readable Markdown export. */
|
|
async exportMarkdown(options) {
|
|
const map = this.getCurrentMap();
|
|
if (!map) throw new Error(this.translate("cmap-export-unavailable", "CMap export is unavailable."));
|
|
await this.saveBeforeTransfer();
|
|
return this.markdownExporter.export(map, options);
|
|
}
|
|
|
|
/** Save pending changes, then create the requested portable JSON bundle. */
|
|
async exportJson(depth) {
|
|
const map = this.getCurrentMap();
|
|
if (!map) throw new Error(this.translate("cmap-json-unavailable", "CMap JSON export is unavailable."));
|
|
await this.saveBeforeTransfer();
|
|
return this.jsonExporter.export(map, depth);
|
|
}
|
|
|
|
/**
|
|
* goal : Import a validated CMap bundle after presenting replacement conflicts.
|
|
* post : Page and CMap catalogues are refreshed and navigation opens the imported root CMap.
|
|
* result : The importer summary.
|
|
*/
|
|
async importFile(file) {
|
|
const bundle = await this.jsonImporter.read(file);
|
|
await this.saveBeforeTransfer();
|
|
const conflicts = this.jsonImporter.conflicts(bundle, this.getPages(), this.getConceptMaps());
|
|
const replaceExisting = (conflicts.pages.length || conflicts.conceptMaps.length) && this.confirm(
|
|
this.translate("cmap-import-conflicts", "The import contains existing CMaps or pages. Replace them?"));
|
|
const result = await this.jsonImporter.import(bundle, {
|
|
pages: this.getPages(), conceptMaps: this.getConceptMaps(), replaceExisting,
|
|
summary: this.translate("imported-from-cmap-json", "Imported from CMap JSON")
|
|
});
|
|
await this.loadPages();
|
|
await this.loadConceptMaps();
|
|
await this.navigateToMap(bundle.rootCmapSlug);
|
|
return result;
|
|
}
|
|
}
|