@blinkk/editor
Version:
Structured content editor with live previews.
155 lines • 5.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const selective_edit_1 = require("@blinkk/selective-edit");
const editor_1 = require("../editor/editor");
const api_1 = require("../editor/api");
const api_2 = require("./api");
const media_1 = require("../editor/field/media");
const mediaList_1 = require("../editor/field/mediaList");
const aside_1 = require("../editor/field/aside");
const state_1 = require("../editor/state");
const exampleField_1 = require("../example/field/exampleField");
const GCSRemoteMedia_1 = require("../remoteMedia/GCSRemoteMedia");
const githubApi_1 = require("./gh/githubApi");
const local_1 = require("./local");
const stackdriver_errors_js_1 = __importDefault(require("stackdriver-errors-js"));
const rafTimeout_1 = require("../utility/rafTimeout");
const projectId = document.body.dataset.projectId;
// Check for configured stackdriver error reporting.
const stackdriverKey = document.body.dataset.stackdriverKey;
if (stackdriverKey) {
const errorHandler = new stackdriver_errors_js_1.default();
errorHandler.start({
projectId: projectId,
key: stackdriverKey,
service: 'editor.dev',
});
}
const container = document.querySelector('.container');
const localPort = parseInt(container.dataset.port || '');
const isLocal = localPort > 0;
let api = null;
if (isLocal) {
api = new api_2.LocalServerApi(localPort);
}
else {
const service = container.dataset.service;
const organization = container.dataset.organization;
const project = container.dataset.project;
const branch = container.dataset.branch;
const isUnstable = window.location.hostname.includes('beta');
const isDev = container.dataset.mode === 'dev';
if (!service || !organization || !project || !branch) {
throw new Error('Missing service, organization, project, or branch information.');
}
if (service === 'gh') {
api = new githubApi_1.GithubApi(service, organization, project, branch, isUnstable, isDev);
}
}
if (!api) {
throw new Error('Unable to determine api to load.');
}
// Stop the normal editor processing if the check auth fails.
// The api should have set a location redirect, so just stop JS.
if (!api.checkAuth()) {
throw new Error('Unable to verify authentication.');
}
// Add the remote media providers.
api.remoteMediaProviders.push(GCSRemoteMedia_1.GCSRemoteMedia);
const state = new state_1.EditorState(api);
const fieldTypes = {
aside: aside_1.AsideField,
checkbox: selective_edit_1.CheckboxField,
checkboxMulti: selective_edit_1.CheckboxMultiField,
color: selective_edit_1.ColorField,
date: selective_edit_1.DateField,
datetime: selective_edit_1.DatetimeField,
exampleField: exampleField_1.ExampleFieldField,
group: selective_edit_1.GroupField,
list: selective_edit_1.ListField,
media: media_1.MediaField,
mediaList: mediaList_1.MediaListField,
number: selective_edit_1.NumberField,
radio: selective_edit_1.RadioField,
remoteMedia: media_1.RemoteMediaField,
remoteMediaList: mediaList_1.RemoteMediaListField,
text: selective_edit_1.TextField,
textarea: selective_edit_1.TextareaField,
time: selective_edit_1.TimeField,
variant: selective_edit_1.VariantField,
};
const selectiveConfig = {
fieldTypes: fieldTypes,
ruleTypes: {
length: selective_edit_1.LengthRule,
match: selective_edit_1.MatchRule,
pattern: selective_edit_1.PatternRule,
range: selective_edit_1.RangeRule,
require: selective_edit_1.RequireRule,
},
global: {
api: api,
labels: {},
state: state,
},
};
const startEditor = (api, selectiveConfig, state) => {
const editor = new editor_1.LiveEditor({
api: api,
selectiveConfig: selectiveConfig,
state: state,
}, container);
// Check for url path to load.
if (container.dataset.file) {
editor.state.getFile({
path: container.dataset.file || '',
});
}
return editor;
};
let editor = undefined;
if (isLocal) {
const localStatus = new local_1.LocalStatus(container, {
port: localPort,
});
// Test the local api to make sure that it is available before
// we start rendering the editor. Otherwise show instructions for
// starting the local server.
const pingApi = () => {
api
.ping()
.then(pingResponse => {
if (pingResponse.status === api_1.PingStatus.Ok) {
editor = startEditor(api, selectiveConfig, state);
editor.render();
}
else {
console.error('Ping response not expected: ', pingResponse);
}
})
.catch(err => {
console.error('Unable to ping the api.', err);
try {
localStatus.render();
rafTimeout_1.rafTimeout(pingApi, 2500);
}
catch (err) {
// Ignore error.
}
});
};
try {
pingApi();
}
catch (err) {
// Ignore error
}
}
else {
editor = startEditor(api, selectiveConfig, state);
editor.render();
}
//# sourceMappingURL=editor.js.map