"use strict"; window.addEventListener("load", () => doPageInit()); class ConverterUiUtil { static renderSideMenuDivider ($menu, heavy) { $menu.append(`
`); } static getAceMode (inputMode) { return { "md": "ace/mode/markdown", "html": "ace/mode/html", }[inputMode] || "ace/mode/text"; } } class BaseConverter extends BaseComponent { static _getDisplayMode (mode) { switch (mode) { case "html": return "HTML"; case "md": return "Markdown"; case "txt": return "Text"; default: throw new Error(`Unimplemented!`); } } /** * @param ui Converter UI instance. * @param opts Options object. * @param opts.converterId Converter unique ID. * @param [opts.canSaveLocal] If the output of this converter is suitable for saving to local homebrew. * @param opts.modes Available converter parsing modes (e.g. "txt", "html", "md") * @param [opts.hasPageNumbers] If the entity has page numbers. * @param [opts.titleCaseFields] Array of fields to be (optionally) title-cased. * @param [opts.hasSource] If the output entities can have a source field. * @param opts.prop The data prop for the output entity. */ constructor (ui, opts) { super(); this._ui = ui; this._converterId = opts.converterId; this._canSaveLocal = !!opts.canSaveLocal; this._modes = opts.modes; this._hasPageNumbers = opts.hasPageNumbers; this._titleCaseFields = opts.titleCaseFields; this._hasSource = opts.hasSource; this._prop = opts.prop; // Add default starting state from options this._state.mode = this._modes[0]; if (this._hasPageNumbers) this._state.page = 0; if (this._titleCaseFields) this._state.isTitleCase = false; if (this._hasSource) this._state.source = ""; this._addHookAll("state", this._ui.saveSettingsDebounced); } get converterId () { return this._converterId; } get canSaveLocal () { return this._canSaveLocal; } get prop () { return this._prop; } get source () { return this._hasSource ? this._state.source : null; } set source (val) { if (!this._hasSource) return; this._state.source = val; } get mode () { return this._state.mode; } renderSidebar (parent, $parent) { const $wrpSidebar = $(`
`).appendTo($parent); const hkShowSidebar = () => $wrpSidebar.toggleClass("hidden", parent.get("converter") !== this._converterId); parent.addHook("converter", hkShowSidebar); hkShowSidebar(); this._renderSidebar(parent, $wrpSidebar); this._renderSidebarSamplesPart(parent, $wrpSidebar); this._renderSidebarConverterOptionsPart(parent, $wrpSidebar); this._renderSidebarPagePart(parent, $wrpSidebar); this._renderSidebarSourcePart(parent, $wrpSidebar); } _renderSidebar () { throw new Error("Unimplemented!"); } handleParse () { throw new Error("Unimplemented!"); } _getSample () { throw new Error("Unimplemented!"); } // region sidebar _renderSidebarSamplesPart (parent, $wrpSidebar) { const $btnsSamples = this._modes.map(mode => { return $(``) .click(() => { this._ui.inText = this._getSample(mode); this._state.mode = mode; }); }); $$`
${$btnsSamples}
`.appendTo($wrpSidebar); ConverterUiUtil.renderSideMenuDivider($wrpSidebar); } _renderSidebarConverterOptionsPart (parent, $wrpSidebar) { const hasModes = this._modes.length > 1; if (!hasModes && !this._titleCaseFields) return; const hkMode = () => { this._ui._editorIn.setOptions({ mode: ConverterUiUtil.getAceMode(this._state.mode), }); }; this._addHookBase("mode", hkMode); hkMode(); if (hasModes) { const $selMode = ComponentUiUtil.$getSelEnum(this, "mode", {values: this._modes, html: ``, fnDisplay: it => `Parse as ${BaseConverter._getDisplayMode(it)}`}); $$`
${$selMode}
`.appendTo($wrpSidebar); } if (this._titleCaseFields) { const $cbTitleCase = ComponentUiUtil.$getCbBool(this, "isTitleCase"); $$`
`.appendTo($wrpSidebar); } ConverterUiUtil.renderSideMenuDivider($wrpSidebar); } _renderSidebarPagePart (parent, $wrpSidebar) { if (!this._hasPageNumbers) return; const getBtnIncrementDecrement = (dir) => { const verb = ~dir ? "Increment" : "Decrement"; return $(``) .on("click", evt => this._state.page += dir * (evt.shiftKey ? 5 : 1)); }; const $iptPage = ComponentUiUtil.$getIptInt(this, "page", 0) .addClass("max-w-80p"); $$`
Page
${getBtnIncrementDecrement(-1)} ${$iptPage} ${getBtnIncrementDecrement(1)}
`.appendTo($wrpSidebar); ConverterUiUtil.renderSideMenuDivider($wrpSidebar); } _renderSidebarSourcePart (parent, $wrpSidebar) { if (!this._hasSource) return; const $wrpSourceOverlay = $(`
`); let modalMeta = null; const rebuildStageSource = (options) => { SourceUiUtil.render({ ...options, $parent: $wrpSourceOverlay, cbConfirm: async (source) => { const isNewSource = options.mode !== "edit"; if (isNewSource) await BrewUtil2.pAddSource(source); else await BrewUtil2.pEditSource(source); if (isNewSource) parent.doRefreshAvailableSources(); this._state.source = source.json; if (modalMeta) modalMeta.doClose(); }, cbConfirmExisting: (source) => { this._state.source = source.json; if (modalMeta) modalMeta.doClose(); }, cbCancel: () => { if (modalMeta) modalMeta.doClose(); }, }); }; const $selSource = $$` ` .change(() => this._state.source = $selSource.val()); $(`