UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const lodash_1 = require("lodash");
4const SerializeError_1 = require("./SerializeError");
5class EditorBuilder {
6 constructor(spec) {
7 this.spec = spec ? spec : {};
8 }
9 id(id) {
10 return this.clone({ id });
11 }
12 getId() {
13 return this.spec.id;
14 }
15 title(title) {
16 return this.clone({ title, id: this.spec.id || lodash_1.camelCase(title) });
17 }
18 getTitle() {
19 return this.spec.title;
20 }
21 documentId(documentId) {
22 return this.clone({
23 options: Object.assign({}, (this.spec.options || {}), { id: documentId })
24 });
25 }
26 getDocumentId() {
27 return this.spec.options && this.spec.options.id;
28 }
29 schemaType(documentType) {
30 return this.clone({
31 options: Object.assign({}, (this.spec.options || {}), { type: typeof documentType === 'string' ? documentType : documentType.name })
32 });
33 }
34 getSchemaType() {
35 return this.spec.options && this.spec.options.type;
36 }
37 serialize({ path, index, hint } = { path: [] }) {
38 const { id, options } = this.spec;
39 if (typeof id !== 'string' || !id) {
40 throw new SerializeError_1.SerializeError('`id` is required for editor nodes', path, index, hint).withHelpUrl(SerializeError_1.HELP_URL.ID_REQUIRED);
41 }
42 if (!options || !options.id) {
43 throw new SerializeError_1.SerializeError('document id (`id`) is required for editor nodes', path, id, hint).withHelpUrl(SerializeError_1.HELP_URL.DOCUMENT_ID_REQUIRED);
44 }
45 return Object.assign({}, this.spec, { id, type: 'document', options: { id: options.id, type: options.type } });
46 }
47 clone(withSpec = {}) {
48 const builder = new EditorBuilder();
49 const options = Object.assign({}, (this.spec.options || {}), (withSpec.options || {}));
50 builder.spec = Object.assign({}, this.spec, withSpec, { options });
51 return builder;
52 }
53}
54exports.EditorBuilder = EditorBuilder;
55
56//# sourceMappingURL=Editor.js.map