UNPKG

@blinkk/editor

Version:

Structured content editor with live previews.

81 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RawPart = void 0; const section_1 = require("./section"); const selective_edit_1 = require("@blinkk/selective-edit"); const events_1 = require("../../events"); const EXTENSIONS_DATA_ONLY = ['yaml', 'yml']; const RAW_FIELD_CONTENT = { type: 'textarea', key: 'content', label: 'Content', wrap: 'hard', }; const RAW_FIELD_DATA = { type: 'textarea', key: 'dataRaw', label: 'Data', wrap: 'hard', }; class RawPart 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; } this.isProcessing = true; this.render(); this.config.state.saveFile(this.selective.value, true, () => { this.isProcessing = false; this.render(); }); } get label() { return 'Raw'; } loadEditorConfig() { this.data = new selective_edit_1.DeepObject(this.config.state.file || {}); this.selective.data = this.data; this.selective.resetFields(); const extension = this.config.state.file?.file.path.split('.').pop() || ''; if (EXTENSIONS_DATA_ONLY.includes(extension)) { this.selective.fields.addField(RAW_FIELD_DATA); } else { this.selective.fields.addField(RAW_FIELD_DATA); this.selective.fields.addField(RAW_FIELD_CONTENT); } this.render(); } get section() { return 'raw'; } // eslint-disable-next-line @typescript-eslint/no-unused-vars 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.RawPart = RawPart; //# sourceMappingURL=sectionRaw.js.map