UNPKG

@blinkk/editor

Version:

Structured content editor with live previews.

70 lines 2.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FieldsPart = void 0; const section_1 = require("./section"); const selective_edit_1 = require("@blinkk/selective-edit"); const events_1 = require("../../events"); const lodash_merge_1 = __importDefault(require("lodash.merge")); class FieldsPart extends section_1.ContentSectionPart { constructor(config) { super(config); this.data = new selective_edit_1.DeepObject(); this.loadEditorConfig(); document.addEventListener(events_1.EVENT_FILE_LOAD_COMPLETE, () => { this.loadEditorConfig(); }); document.addEventListener(events_1.EVENT_SAVE, (evt) => { // If the section is not visible, then disregard the event. if (this.isVisible) { this.handleAction(evt); } }); } // eslint-disable-next-line @typescript-eslint/no-unused-vars handleAction(evt) { if (this.selective.isClean || !this.selective.isValid) { return; } const value = lodash_merge_1.default({}, this.config.state.file || {}); // Do not 'merge' the data as it will get merged with the original data // instead of overwritten. value.data = this.selective.value; this.isProcessing = true; this.render(); this.config.state.saveFile(value, false, () => { this.isProcessing = false; this.render(); }); } get label() { return 'Fields'; } loadEditorConfig() { this.data = new selective_edit_1.DeepObject(this.config.state.file?.data || {}); this.selective.resetFields(); // TODO: Auto guess fields if not field configuration is defined. for (const fieldConfig of this.config.state.file?.editor?.fields || []) { this.selective.fields.addField(fieldConfig); } this.render(); } get section() { return 'fields'; } templateContent(editor) { const isValid = this.selective.isValid; try { return this.selective.template(this.selective, this.data); } finally { if (isValid !== this.selective.isValid) { this.render(); } } } } exports.FieldsPart = FieldsPart; //# sourceMappingURL=sectionFields.js.map