/*! * Jodit Editor PRO (https://xdsoft.net/jodit/) * See LICENSE.md in the project root for license information. * Copyright (c) 2013-2022 Valeriy Chupurnov. All rights reserved. https://xdsoft.net/jodit/pro/ */ import './form.less'; import type { IDictionary, IUIInput } from 'jodit/types'; import { UIElement, UIInput, UISelect } from 'jodit/core/ui'; import { toArray } from 'jodit/core/helpers/array/to-array'; import { Dom } from 'jodit/core/dom'; import { UIRange } from '../../../../core/ui/form/range/range'; import { assert } from 'jodit/core/helpers'; export class UIFormButtonGenerator extends UIElement { /** @override */ override className(): string { return 'UIFormButtonGenerator'; } /** @override */ constructor( jodit: UIElement['jodit'], readonly state: IDictionary, readonly updateState: ( name: string, value: boolean | string | number ) => void ) { super(jodit); const getOnUpdate = (uiInput: IUIInput, input: HTMLInputElement | HTMLSelectElement) => (): void => { let newValue = this.state[input.name]; if (/px/.test(newValue.toString())) { newValue = newValue.toString().replace(/px/, ''); } if (newValue.toString() !== uiInput.value) { uiInput.value = newValue; } }; toArray(this.container.querySelectorAll('input,select')).forEach( (input): void => { if (Dom.isTag(input, 'select')) { const uiInput = new UISelect(this.j, { options: toArray(input.options).map(opt => ({ text: opt.innerText, value: opt.value })), onChange: (value): void => { this.updateState(input.name, value); } }); Dom.replace( input, uiInput.container, this.j.create, false, true ); this.onUpdates.push(getOnUpdate(uiInput, input)); return; } if (Dom.isTag(input, 'input')) { switch (input.type) { case 'range': { const uiInput = new UIRange(this.j, { label: input.placeholder, name: input.name, min: parseInt(input.min, 0) || 0, max: parseInt(input.max, 0) || 100, onChange: (value): void => { this.updateState( input.name, parseInt(value, 10) ); } }); Dom.replace( input, uiInput.container, this.j.create ); this.onUpdates.push(getOnUpdate(uiInput, input)); return; } case 'text': { const uiInput = new UIInput(this.j, { placeholder: input.placeholder, name: input.name, onChange: (value): void => { this.updateState(input.name, value); } }); Dom.replace( input, uiInput.container, this.j.create ); this.onUpdates.push(getOnUpdate(uiInput, input)); return; } case 'checkbox': input.onchange = (): void => this.updateState(input.name, input.checked); this.onUpdates.push(() => { input.checked = this.state[input.name]; }); return; } } } ); this.update(); } private onUpdates: Function[] = []; override update(): void { this.onUpdates.forEach(cb => cb()); const boxShadow = this.getElm('box-shadow'); assert(boxShadow != null, 'box-shadow does not exists'); const textShadow = this.getElm('text-shadow'); assert(textShadow != null, 'text-shadow does not exists'); boxShadow.style.display = this.state.boxShadow ? '' : 'none'; textShadow.style.display = this.state.textShadow ? '' : 'none'; } /** @override */ protected override render(): string { return `
Text
~Size~
~Border~
~Box Shadow~
~Text Shadow~
`; } }