@blinkk/editor
Version:
Structured content editor with live previews.
1,375 lines • 53.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkspaceWorkflow = exports.ErrorController = exports.ExampleGrowApi = exports.ExampleAmagakiApi = exports.ExampleApi = void 0;
const api_1 = require("../editor/api");
const MAX_RESPONSE_MS = 1200;
const MIN_RESPONSE_MS = 250;
/**
* Simulate having the request be slowed down by a network.
*
* @param callback Callback after 'network' lag complete.
* @param response Response for the callback.
*/
function simulateNetwork(callback, response) {
setTimeout(() => {
callback(response);
}, Math.random() * (MAX_RESPONSE_MS - MIN_RESPONSE_MS) + MIN_RESPONSE_MS);
}
const DEFAULT_EDITOR_FILE = {
content: 'Example content.',
data: {
title: 'Testing',
},
dataRaw: 'title: Testing',
file: {
path: '/content/pages/index.yaml',
},
editor: {
fields: [
{
type: 'text',
key: 'title',
label: 'Title',
validation: [
{
type: 'require',
message: 'Title is required.',
},
],
},
],
},
history: [
{
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: 'db29a258dacdd416bb24bb63c689d669df08d409',
summary: 'Example commit summary.',
timestamp: new Date(new Date().getTime() - 1 * 60 * 60 * 1000).toISOString(),
},
{
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: 'f36d7c0d556e30421a7a8f22038234a9174f0e04',
summary: 'Example commit summary.',
timestamp: new Date(new Date().getTime() - 2 * 60 * 60 * 1000).toISOString(),
},
{
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: '6dda2682901bf4f2f03f936267169454120f1806',
summary: 'Example commit summary. With a long summary. Like really too long for a summary. Probably should use a shorter summary.',
timestamp: new Date(new Date().getTime() - 4 * 60 * 60 * 1000).toISOString(),
},
{
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: '465e3720c050f045d9500bd9bc7c7920f192db78',
summary: 'Example commit summary.',
timestamp: new Date(new Date().getTime() - 14 * 60 * 60 * 1000).toISOString(),
},
],
url: 'preview.html',
urls: [
{
url: '#private',
label: 'Live editor preview',
level: api_1.UrlLevel.Private,
},
{
url: '#protected',
label: 'Staging',
level: api_1.UrlLevel.Protected,
},
{
url: '#public',
label: 'Live',
level: api_1.UrlLevel.Public,
},
{
url: 'https://github.com/blinkkcode/live-edit/',
label: 'View in Github',
level: api_1.UrlLevel.Source,
},
],
};
const fullFiles = {
'/example/list.yaml': {
data: {
listSimple: ['values', 'in', 'a', 'list'],
listSimpleMedia: [
{
url: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
label: 'Google logo',
},
],
},
editor: {
fields: [
// Simple list example.
{
type: 'exampleField',
key: 'listSimple',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_list.listfieldconfig.html',
},
],
field: {
type: 'list',
key: 'listSimple',
label: 'List (Simple)',
fields: [
{
type: 'text',
key: '',
},
],
validation: [
{
type: 'length',
min: {
value: 1,
},
max: { value: 5 },
},
],
},
},
// Simple with complex list example.
// Only one field, but with a non-simple field.
// Should show as a normal list instead of a 'simple' list.
{
type: 'exampleField',
key: 'listSimpleMedia',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_list.listfieldconfig.html',
},
],
field: {
type: 'list',
key: 'listSimpleMedia',
label: 'List (Simple w/complex field)',
addLabel: 'Add media',
fields: [
{
type: 'media',
key: '',
},
],
},
},
// Multi-field list example.
{
type: 'exampleField',
key: 'listMulti',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_list.listfieldconfig.html',
},
],
field: {
type: 'list',
key: 'listMulti',
label: 'List (Multiple fields)',
fields: [
{
type: 'text',
key: 'title',
label: 'Title',
},
{
type: 'text',
key: 'subtitle',
label: 'Sub title',
},
{
type: 'media',
key: 'media',
label: 'Image',
},
],
},
},
],
},
file: {
path: '/example/list.yaml',
},
url: 'preview.html',
},
'/example/media.yaml': {
editor: {
fields: [
// Media example.
{
type: 'exampleField',
key: 'media',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/editor_field_media.mediafieldconfig.html',
},
{
label: 'Generic config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field.fieldconfig.html',
},
],
field: {
type: 'media',
key: 'media',
label: 'Media',
validation: {
path: [
{
type: 'require',
message: 'Path is required.',
},
],
label: [
{
type: 'require',
message: 'Label is required.',
},
],
},
},
},
// Media w/extra example.
{
type: 'exampleField',
key: 'mediaExtra',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/editor_field_media.mediafieldconfig.html',
},
{
label: 'Generic config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field.fieldconfig.html',
},
],
field: {
type: 'media',
key: 'mediaExtra',
label: 'Media w/extra fields',
fields: [
{
type: 'text',
key: 'title',
label: 'Title',
},
{
type: 'textarea',
key: 'description',
label: 'Description',
},
],
validation: {
path: [
{
type: 'require',
message: 'Path is required.',
},
],
},
},
},
],
},
file: {
path: '/example/media.yaml',
},
url: 'preview.html',
},
'/example/mediaList.yaml': {
data: {
mediaList: [
{
url: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
label: 'Google logo',
},
],
},
editor: {
fields: [
// Media list example.
{
type: 'exampleField',
key: 'mediaList',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/editor_field_media.mediafieldconfig.html',
},
{
label: 'Generic config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field.fieldconfig.html',
},
],
field: {
type: 'mediaList',
key: 'mediaList',
label: 'Media list',
},
},
],
},
file: {
path: '/example/mediaList.yaml',
},
url: 'preview.html',
},
'/example/standard.yaml': {
editor: {
fields: [
// Text example.
{
type: 'exampleField',
key: 'text',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_text.textfieldconfig.html',
},
],
field: {
type: 'text',
key: 'text',
label: 'Title',
validation: [
{
type: 'require',
message: 'Title is required.',
},
],
},
},
// Textarea example.
{
type: 'exampleField',
key: 'textarea',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_textarea.textareafieldconfig.html',
},
],
field: {
type: 'textarea',
key: 'description',
label: 'Description',
},
},
// Checkbox example.
{
type: 'exampleField',
key: 'isVisible',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_checkbox.checkboxfieldconfig.html',
},
],
field: {
type: 'checkbox',
key: 'isVisible',
label: 'Is visible?',
},
},
// Checkbox multi example.
{
type: 'exampleField',
key: 'options',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_checkboxmulti.checkboxmultifieldconfig.html',
},
],
field: {
type: 'checkboxMulti',
key: 'options',
label: 'View options',
options: [
{
label: 'Option 1',
value: 'option-1',
},
{
label: 'Option 2',
value: 'option-2',
},
{
label: 'Option 3',
value: 'option-3',
},
],
},
},
// Radio example.
{
type: 'exampleField',
key: 'skyColor',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_radio.radiofieldconfig.html',
},
],
field: {
type: 'radio',
key: 'skyColor',
label: 'Current sky color',
options: [
{
label: 'Cloudy sky',
value: 'cloudy',
color: '#dde5f2',
},
{
label: 'Blue sky',
value: 'blue',
color: '#006beb',
},
{
label: 'Night sky',
value: 'night',
color: '#131862',
},
{
label: 'Stormy sky',
value: 'stormy',
gradient: {
colors: ['#57728f', '#2e5073', '#24405c'],
isSmooth: true,
},
},
{
label: 'Sunset sky',
value: 'sunset',
gradient: {
colors: ['#e85566', '#f47b5a', '#f9ac5e'],
orientation: 'slope',
},
},
],
},
},
// Number example.
{
type: 'exampleField',
key: 'napsInADay',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_number.numberfieldconfig.html',
},
],
field: {
type: 'number',
key: 'napsInADay',
label: 'Naps in a day',
help: 'How many naps should there be in a day?',
max: 100,
min: 0,
},
},
// Color example.
{
type: 'exampleField',
key: 'favoriteColor',
docUrls: [
{
label: 'Module',
url: 'https://blinkkcode.github.io/selective-edit/modules/selective_field_color.html',
},
],
field: {
type: 'color',
key: 'favoriteColor',
label: 'What is your favorite color?',
},
},
// Date example.
{
type: 'exampleField',
key: 'birthdate',
docUrls: [
{
label: 'Module',
url: 'https://blinkkcode.github.io/selective-edit/modules/selective_field_date.html',
},
],
field: {
type: 'date',
key: 'birthdate',
label: 'Birth date',
},
},
// Datetime example.
{
type: 'exampleField',
key: 'naptime',
docUrls: [
{
label: 'Module',
url: 'https://blinkkcode.github.io/selective-edit/modules/selective_field_datetime.html',
},
],
field: {
type: 'datetime',
key: 'naptime',
label: 'Next nap time',
},
},
// Time example.
{
type: 'exampleField',
key: 'wakeuptime',
docUrls: [
{
label: 'Module',
url: 'https://blinkkcode.github.io/selective-edit/modules/selective_field_time.html',
},
],
field: {
type: 'time',
key: 'wakeuptime',
label: 'Time to wake up',
},
},
],
},
file: {
path: '/example/standard.yaml',
},
url: 'preview.html',
},
'/example/structure.yaml': {
data: {
group: {
title: 'testing',
},
},
editor: {
fields: [
// Group example.
{
type: 'exampleField',
key: 'group',
cleanerKeys: ['isExpanded'],
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_group.groupfieldconfig.html',
},
],
field: {
type: 'group',
key: 'group',
label: 'Group',
previewFields: ['title'],
fields: [
{
type: 'text',
key: 'title',
label: 'Title',
},
],
},
},
// Variant example.
{
type: 'exampleField',
key: 'variant',
cleanerKeys: ['isExpanded'],
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_variant.variantfieldconfig.html',
},
{
label: 'Option interface',
url: 'https://blinkkcode.github.io/selective-edit/interfaces/selective_field_variant.variantoptionconfig.html',
},
],
field: {
type: 'variant',
key: 'variant',
label: 'Variant',
variants: {
hero: {
label: 'Hero',
fields: [
{
type: 'text',
key: 'title',
label: 'Hero Title',
},
],
},
heroWithImage: {
label: 'Hero with Image',
fields: [
{
type: 'text',
key: 'title',
label: 'Hero Title',
},
{
type: 'media',
key: 'media',
label: 'Hero image',
},
],
},
},
},
},
],
},
file: {
path: '/example/structure.yaml',
},
url: 'preview.html',
},
'/example/utility.yaml': {
editor: {
fields: [
// Aside example.
{
type: 'exampleField',
key: 'aside',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/editor_field_aside.asidefieldconfig.html',
},
],
field: {
type: 'aside',
key: '',
source: 'Use **markdown** to provide more information to the user.\n\nIt does not save as part of the data, purely informational.',
},
},
],
},
file: {
path: '/example/utility.yaml',
},
url: 'preview.html',
},
'/example/amagaki/document.yaml': {
editor: {
fields: [
// Amagaki document examples.
{
type: 'exampleField',
key: 'document',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_amagaki_field_document.amagakidocumentconfig.html',
},
],
field: {
type: 'amagakiDocument',
key: 'doc',
label: 'Amagaki document',
help: 'In yaml: !pod.document: <document path>',
validation: [
{
type: 'require',
message: 'Document is required.',
},
],
},
},
],
},
file: {
path: '/example/amagaki/document.yaml',
},
url: 'preview.html',
},
'/example/amagaki/static.yaml': {
editor: {
fields: [
// Amagaki static file examples.
{
type: 'exampleField',
key: 'static',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_amagaki_field_static.amagakistaticconfig.html',
},
],
field: {
type: 'amagakiStatic',
key: 'doc',
label: 'Amagaki static file',
help: 'In yaml: !pod.staticFile: <static file path>',
},
},
],
},
file: {
path: '/example/amagaki/static.yaml',
},
url: 'preview.html',
},
'/example/amagaki/string.yaml': {
editor: {
fields: [
// Amagaki string examples.
{
type: 'exampleField',
key: 'string',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_amagaki_field_string.amagakistringconfig.html',
},
],
field: {
type: 'amagakiString',
key: 'string',
label: 'Amagaki string',
help: 'In yaml: !pod.string: { value: "<string value>" }',
},
},
],
},
file: {
path: '/example/amagaki/string.yaml',
},
url: 'preview.html',
},
'/example/amagaki/yaml.yaml': {
editor: {
fields: [
// Amagaki yaml examples.
{
type: 'exampleField',
key: 'yaml',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_amagaki_field_yaml.amagakiyamlconfig.html',
},
],
field: {
type: 'amagakiYaml',
key: 'yaml',
label: 'Amagaki yaml',
help: 'In yaml: !pod.yaml: { value: "<yaml file and query reference>" }',
},
},
],
},
file: {
path: '/example/amagaki/yaml.yaml',
},
url: 'preview.html',
},
'/example/grow/document.yaml': {
editor: {
fields: [
// Grow document examples.
{
type: 'exampleField',
key: 'document',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_grow_field_document.growdocumentconfig.html',
},
],
field: {
type: 'growDocument',
key: 'doc',
label: 'Grow document',
help: 'In yaml: !g.doc: <document path>',
validation: [
{
type: 'require',
message: 'Document is required.',
},
],
},
},
],
},
file: {
path: '/example/grow/document.yaml',
},
url: 'preview.html',
},
'/example/grow/static.yaml': {
editor: {
fields: [
// Grow static file examples.
{
type: 'exampleField',
key: 'static',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_grow_field_static.growstaticconfig.html',
},
],
field: {
type: 'growStatic',
key: 'static',
label: 'Grow static',
help: 'In yaml: !g.static: <static file path>',
validation: [
{
type: 'require',
message: 'Static file is required.',
},
],
},
},
],
},
file: {
path: '/example/grow/static.yaml',
},
url: 'preview.html',
},
'/example/grow/string.yaml': {
editor: {
fields: [
// Grow string examples.
{
type: 'exampleField',
key: 'string',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_grow_field_string.growstringconfig.html',
},
],
field: {
type: 'growString',
key: 'string',
label: 'Grow string',
help: 'In yaml: !g.string: <string reference>',
},
},
],
},
file: {
path: '/example/grow/string.yaml',
},
url: 'preview.html',
},
'/example/grow/yaml.yaml': {
editor: {
fields: [
// Grow yaml examples.
{
type: 'exampleField',
key: 'yaml',
docUrls: [
{
label: 'Config interface',
url: 'https://blinkkcode.github.io/live-edit/interfaces/projecttype_grow_field_yaml.growyamlconfig.html',
},
],
field: {
type: 'growYaml',
key: 'yaml',
label: 'Grow yaml',
help: 'In yaml: !g.yaml: <yaml file and query reference>',
},
},
],
},
file: {
path: '/example/grow/yaml.yaml',
},
url: 'preview.html',
},
};
const currentFileset = [
{
path: '/content/pages/index.yaml',
},
{
path: '/content/pages/index.png',
},
{
path: '/content/pages/about.md',
},
{
path: '/content/pages/contact.yaml',
},
{
path: '/static/img/landscape.png',
url: 'image-landscape.png',
},
{
path: '/static/img/portrait.png',
url: 'image-portrait.png',
},
{
path: '/static/img/square.png',
url: 'image-square.png',
},
];
// Pull in the fullFiles automatically.
for (const key of Object.keys(fullFiles)) {
currentFileset.push(fullFiles[key].file);
}
let currentWorkspace = {
branch: {
name: 'main',
commit: {
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: '951c206e5f10ba99d13259293b349e321e4a6a9e',
summary: 'Example commit summary.',
timestamp: new Date().toISOString(),
},
},
name: 'main',
};
const currentWorkspaces = [
currentWorkspace,
{
branch: {
name: 'staging',
commit: {
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: '26506fd82b7d5d6aab6b3a92c7ef641c7073b249',
summary: 'Example commit summary.',
timestamp: new Date(new Date().getTime() - 2 * 60 * 60 * 1000).toISOString(),
},
},
name: 'staging',
},
{
branch: {
name: 'workspace/redesign',
commit: {
author: {
name: 'Example User',
email: 'example@example.com',
},
hash: 'db29a258dacdd416bb24bb63c689d669df08d409',
summary: 'Example commit summary.',
timestamp: new Date(new Date().getTime() - 6 * 60 * 60 * 1000).toISOString(),
},
},
name: 'redesign',
},
];
/**
* Example api that returns data through a 'simulated' network.
*/
class ExampleApi {
constructor() {
this.errorController = new ErrorController();
this.workflow = WorkspaceWorkflow.Success;
this.projectTypes = {
amagaki: new ExampleAmagakiApi(this.errorController),
grow: new ExampleGrowApi(this.errorController),
};
}
checkAuth() {
return true;
}
async copyFile(originalPath, path) {
return new Promise((resolve, reject) => {
const methodName = 'copyFile';
console.log(`API: ${methodName}`, originalPath, path);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to copy the file.',
description: 'Api is set to always return an error.',
});
return;
}
const newFile = {
path: path,
};
currentFileset.push(newFile);
simulateNetwork(resolve, newFile);
});
}
async createFile(path) {
return new Promise((resolve, reject) => {
const methodName = 'createFile';
console.log(`API: ${methodName}`, path);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to create the file.',
description: 'Api is set to always return an error.',
});
return;
}
const newFile = {
path: path,
};
currentFileset.push(newFile);
simulateNetwork(resolve, newFile);
});
}
async createWorkspace(base, workspace) {
return new Promise((resolve, reject) => {
const methodName = 'createWorkspace';
console.log(`API: ${methodName}`, base, workspace);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to create the workspace.',
description: 'Api is set to always return an error.',
});
return;
}
const newWorkspace = {
branch: {
name: `workspace/${workspace}`,
commit: {
author: base.branch.commit.author,
hash: base.branch.commit.hash,
message: base.branch.commit.message,
summary: base.branch.commit.summary,
timestamp: new Date().toISOString(),
},
},
name: workspace,
};
currentWorkspaces.push(newWorkspace);
simulateNetwork(resolve, newWorkspace);
});
}
async deleteFile(file) {
return new Promise((resolve, reject) => {
const methodName = 'deleteFile';
console.log(`API: ${methodName}`, file.path);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to delete the file.',
description: 'Api is set to always return an error.',
});
return;
}
for (let i = 0; i < currentFileset.length; i++) {
if (currentFileset[i].path === file.path) {
currentFileset.splice(i, 1);
break;
}
}
simulateNetwork(resolve, {});
});
}
async getDevices() {
return new Promise((resolve, reject) => {
const methodName = 'getDevices';
console.log(`API: ${methodName}`);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the devices.',
description: 'Api is set to always return an error.',
});
return;
}
simulateNetwork(resolve, [
{
label: 'Mobile',
width: 411,
height: 731,
canRotate: true,
},
{
label: 'Tablet',
width: 1024,
height: 768,
canRotate: true,
},
{
label: 'Desktop',
width: 1440,
},
{
label: 'Desktop (Large)',
width: 2200,
},
]);
});
}
async getFile(file) {
return new Promise((resolve, reject) => {
const methodName = 'loadFile';
console.log(`API: ${methodName}`, file);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to load the file.',
description: 'Api is set to always return an error.',
});
return;
}
const url = new URL(window.location.toString());
url.searchParams.set('path', file.path);
window.history.pushState({}, '', url.toString());
simulateNetwork(resolve, fullFiles[file.path] || DEFAULT_EDITOR_FILE);
});
}
async getFiles() {
return new Promise((resolve, reject) => {
const methodName = 'getFiles';
console.log(`API: ${methodName}`);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the files.',
description: 'Api is set to always return an error.',
});
return;
}
simulateNetwork(resolve, [...currentFileset]);
});
}
async getFileUrl(file) {
return new Promise((resolve, reject) => {
const methodName = 'getFileUrl';
console.log(`API: ${methodName}`, file.path);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the file url.',
description: 'Api is set to always return an error.',
});
return;
}
// TODO: Use some logic to determine what url to return.
simulateNetwork(resolve, {
path: file.path,
url: 'image-landscape.png',
});
});
}
async getProject() {
return new Promise((resolve, reject) => {
const methodName = 'getProject';
console.log(`API: ${methodName}`);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the project.',
description: 'Api is set to always return an error.',
});
return;
}
let publish = undefined;
if ([
WorkspaceWorkflow.Success,
WorkspaceWorkflow.Pending,
WorkspaceWorkflow.Failure,
].includes(this.workflow)) {
publish = {
fields: [
{
type: 'text',
key: 'message',
label: 'Publish message',
validation: [
{
type: 'require',
message: 'Message for publishing is required.',
},
],
},
],
};
}
else if (this.workflow !== WorkspaceWorkflow.NoPublish) {
publish = {};
}
simulateNetwork(resolve, {
title: 'Example project',
publish: publish,
users: [
{
name: 'Example User',
email: 'example@example.com',
},
{
name: 'Domain users',
email: '@domain.com',
isGroup: true,
},
],
});
});
}
async getWorkspace() {
return new Promise((resolve, reject) => {
const methodName = 'getWorkspace';
console.log(`API: ${methodName}`);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the workspace.',
description: 'Api is set to always return an error.',
});
return;
}
if ([WorkspaceWorkflow.Pending].includes(this.workflow)) {
if (!currentWorkspace.publish) {
currentWorkspace.publish = {
status: api_1.PublishStatus.Pending,
};
}
else {
currentWorkspace.publish.status = api_1.PublishStatus.Pending;
}
}
if ([WorkspaceWorkflow.NoChanges].includes(this.workflow)) {
if (!currentWorkspace.publish) {
currentWorkspace.publish = {
status: api_1.PublishStatus.NoChanges,
};
}
else {
currentWorkspace.publish.status = api_1.PublishStatus.NoChanges;
}
}
if ([WorkspaceWorkflow.Failure].includes(this.workflow)) {
if (!currentWorkspace.publish) {
currentWorkspace.publish = {
status: api_1.PublishStatus.Failure,
};
}
else {
currentWorkspace.publish.status = api_1.PublishStatus.Failure;
}
}
simulateNetwork(resolve, currentWorkspace);
});
}
async getWorkspaces() {
return new Promise((resolve, reject) => {
const methodName = 'getWorkspaces';
console.log(`API: ${methodName}`);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to get the workspaces.',
description: 'Api is set to always return an error.',
});
return;
}
simulateNetwork(resolve, [...currentWorkspaces]);
});
}
async loadWorkspace(workspace) {
return new Promise((resolve, reject) => {
const methodName = 'loadWorkspace';
console.log(`API: ${methodName}`, workspace.name);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to load the workspaces.',
description: 'Api is set to always return an error.',
});
return;
}
currentWorkspace = workspace;
simulateNetwork(resolve, currentWorkspace);
});
}
async publish(workspace, data) {
return new Promise((resolve, reject) => {
const methodName = 'publish';
console.log(`API: ${methodName}`, workspace.name, data);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to publish.',
description: 'Api is set to always return an error.',
});
return;
}
let status = api_1.PublishStatus.Complete;
if ([WorkspaceWorkflow.Failure].includes(this.workflow)) {
status = api_1.PublishStatus.Failure;
}
else if ([WorkspaceWorkflow.Pending].includes(this.workflow)) {
status = api_1.PublishStatus.Pending;
}
let responseWorkspace = currentWorkspace;
// If the workflow changes the workspace, use a different workspace than
// the current workspace in the response.
if (this.workflow === WorkspaceWorkflow.SuccessChangeWorkspace) {
for (const workspace of currentWorkspaces) {
if (currentWorkspace !== workspace) {
responseWorkspace = workspace;
break;
}
}
}
simulateNetwork(resolve, {
status: status,
workspace: responseWorkspace,
});
});
}
async saveFile(file,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isRawEdit) {
return new Promise((resolve, reject) => {
const methodName = 'saveFile';
console.log(`API: ${methodName}`, file);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to save the file.',
description: 'Api is set to always return an error.',
});
return;
}
fullFiles[file.file.path] = file;
simulateNetwork(resolve, fullFiles[file.file.path] || DEFAULT_EDITOR_FILE);
});
}
async uploadFile(file, meta) {
return new Promise((resolve, reject) => {
const methodName = 'uploadFile';
console.log(`API: ${methodName}`, file, meta);
if (this.errorController.shouldError(methodName)) {
reject({
message: 'Failed to upload file.',
description: 'Api is set to always return an error.',
});
return;
}
simulateNetwork(resolve, {
path: '/static/img/portrait.png',
url: 'image-portrait.png',
});
});
}
}
exports.ExampleApi = ExampleApi;
class ExampleAmagakiApi {
constructor(errorController) {
this.errorController = errorController;
}
async getPartials() {
return new Promise((resolve, reject) => {
const methodName = 'getPartials';
console.log