verdere refactoring
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"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
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user