@blinkk/editor
Version:
Structured content editor with live previews.
302 lines • 9.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_DEVICES = exports.EditorState = void 0;
const api_1 = require("./api");
const events_1 = require("./events");
const amagakiState_1 = require("../projectType/amagaki/amagakiState");
const mixins_1 = require("@blinkk/selective-edit/dist/src/mixins");
const featureManager_1 = require("../utility/featureManager");
const growState_1 = require("../projectType/grow/growState");
const listeners_1 = require("../mixin/listeners");
/**
* Track the references to the editor state.
*
* These is done as a property of a class so that it can be used
* with part configs and always have access to the latest
* value without each part having to request the same information.
*/
class EditorState extends listeners_1.ListenersMixin(mixins_1.Base) {
constructor(api) {
super();
this.api = api;
this.promises = {};
// Features are on by default.
this.features = new featureManager_1.FeatureManager({
defaultStatus: true,
});
// Experiments are off by default.
this.experiments = new featureManager_1.FeatureManager({
defaultStatus: false,
});
this.projectTypes = {
amagaki: new amagakiState_1.AmagakiState(this),
grow: new growState_1.GrowState(this),
};
}
copyFile(originalPath, path, callback, callbackError) {
this.api
.copyFile(originalPath, path)
.then(data => {
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback(data);
}
// Reload the files.
this.getFiles();
})
.catch(error => api_1.catchError(error, callbackError));
}
createFile(path, callback, callbackError) {
this.api
.createFile(path)
.then(data => {
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback(data);
}
// Reload the files.
this.getFiles();
})
.catch(error => api_1.catchError(error, callbackError));
}
createWorkspace(base, workspace, callback, callbackError) {
this.api
.createWorkspace(base, workspace)
.then(data => {
if (callback) {
callback(data);
}
// Reload the workspaces.
this.getWorkspaces();
})
.catch(error => api_1.catchError(error, callbackError));
}
deleteFile(file, callback, callbackError) {
this.api
.deleteFile(file)
.then(() => {
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback();
}
// Reload the files.
this.getFiles();
})
.catch(error => api_1.catchError(error, callbackError));
}
getDevices(callback, callbackError) {
const promiseKey = 'getDevices';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getDevices()
.then(data => {
if (!data.length) {
this.devices = exports.DEFAULT_DEVICES;
}
else {
this.devices = data;
}
delete this.promises[promiseKey];
if (callback) {
callback(this.devices);
}
this.triggerListener(promiseKey);
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.devices;
}
getFile(file, callback, callbackError) {
const promiseKey = 'getFile';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getFile(file)
.then(data => {
this.file = data;
delete this.promises[promiseKey];
if (callback) {
callback(data);
}
this.triggerListener(promiseKey);
document.dispatchEvent(new CustomEvent(events_1.EVENT_FILE_LOAD_COMPLETE));
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.file;
}
getFiles(callback, callbackError) {
const promiseKey = 'getFiles';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getFiles()
.then(data => {
this.files = data;
delete this.promises[promiseKey];
if (callback) {
callback(data);
}
this.triggerListener(promiseKey, data);
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.files;
}
getProject(callback, callbackError) {
const promiseKey = 'getProject';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getProject()
.then(data => {
this.project = data;
delete this.promises[promiseKey];
// Pull in the feature flags and settings.
if (this.project.features) {
for (const key of Object.keys(this.project.features)) {
this.features.set(key, this.project.features[key]);
}
}
// Pull in the experiment flags and settings.
if (this.project.experiments) {
for (const key of Object.keys(this.project.experiments)) {
this.experiments.set(key, this.project.experiments[key]);
}
}
if (callback) {
callback(data);
}
this.triggerListener(promiseKey);
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.project;
}
getWorkspace(callback, callbackError) {
const promiseKey = 'getWorkspace';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getWorkspace()
.then(data => {
this.workspace = data;
delete this.promises[promiseKey];
if (callback) {
callback(data);
}
this.triggerListener(promiseKey);
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.workspace;
}
getWorkspaces(callback, callbackError) {
const promiseKey = 'getWorkspaces';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.getWorkspaces()
.then(data => {
this.workspaces = data;
delete this.promises[promiseKey];
if (callback) {
callback(data);
}
this.triggerListener(promiseKey);
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
return this.workspaces;
}
loadWorkspace(workspace, callback, callbackError) {
this.api
.loadWorkspace(workspace)
.then((data) => {
this.workspace = data;
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback(data);
}
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
}
publish(workspace, data, callback, callbackError) {
this.api
.publish(workspace, data)
.then((result) => {
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback(result);
}
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
}
/**
* Signal for the editor to re-render.
*/
render() {
document.dispatchEvent(new CustomEvent(events_1.EVENT_RENDER));
}
saveFile(file, isRawEdit, callback, callbackError) {
const promiseKey = 'saveFile';
if (this.promises[promiseKey]) {
return;
}
this.promises[promiseKey] = this.api
.saveFile(file, isRawEdit)
.then(data => {
this.file = data;
delete this.promises[promiseKey];
// Reload the workspace from the api.
// Refreshes the publish status.
this.workspace = this.getWorkspace();
if (callback) {
callback(data);
}
this.triggerListener(promiseKey);
document.dispatchEvent(new CustomEvent(events_1.EVENT_FILE_LOAD_COMPLETE));
this.render();
})
.catch(error => api_1.catchError(error, callbackError));
}
}
exports.EditorState = EditorState;
exports.DEFAULT_DEVICES = [
{
label: 'Phone',
width: 411,
height: 731,
canRotate: true,
},
{
label: 'Tablet',
width: 1024,
height: 768,
canRotate: true,
},
{
label: 'Desktop',
width: 1440,
},
];
//# sourceMappingURL=state.js.map