// // @description : // @author : Adarsh Pastakia // @copyright : 2016 // @license : MIT import {autoinject, bindable, containerless, customElement, inlineView, bindingMode, DOM} from "aurelia-framework"; import {UIEvent} from "../../utils/ui-event"; @autoinject() @customElement('ui-markdown') @inlineView(``) export class UIForm { constructor(public element: Element) { this.__counter = element.hasAttribute('charcount'); } bind() { this.disabled = isTrue(this.disabled); this.readonly = isTrue(this.readonly); } __input; __counter; errors = []; /** * @property * @type */ @bindable({ defaultBindingMode: bindingMode.twoWay }) value = ''; @bindable() rows = 12; @bindable() dir = 'ltr'; @bindable() maxlength = 10000; @bindable() placeholder = ''; @bindable() disabled = false; @bindable() readonly = false; __help = false; __preview = false; __disableTools = false; toolClicked(evt) { let btn; if (!(btn = getParentByTag(evt.target, 'button'))) return; if (!(btn = btn['dataset']["id"])) return; let val = this.value || ''; let diff = 0, start = this.__input.selectionStart, end = this.__input.selectionEnd, sub = val.substr(start, end - start) || 'EditThis'; switch (btn) { case 'h1': diff = 3; this.value = val.substr(0, start) + `\n# ${sub}\n\n` + val.substr(end); break; case 'h2': diff = 4; this.value = val.substr(0, start) + `\n## ${sub}\n\n` + val.substr(end); break; case 'h3': diff = 5; this.value = val.substr(0, start) + `\n### ${sub}\n\n` + val.substr(end); break; case 'h4': diff = 6; this.value = val.substr(0, start) + `\n#### ${sub}\n\n` + val.substr(end); break; case 'h5': diff = 7; this.value = val.substr(0, start) + `\n##### ${sub}\n\n` + val.substr(end); break; case 'h6': diff = 8; this.value = val.substr(0, start) + `\n###### ${sub}\n\n` + val.substr(end); break; case 'b': diff = 3; this.value = val.substr(0, start) + ` __${sub}__ ` + val.substr(end); break; case 'i': diff = 2; this.value = val.substr(0, start) + ` _${sub}_ ` + val.substr(end); break; case 's': diff = 3; this.value = val.substr(0, start) + ` ~~${sub}~~ ` + val.substr(end); break; case 'a': diff = 2; this.value = val.substr(0, start) + ` [${sub}](Link_Url_Here) ` + val.substr(end); break; case 'img': diff = 3; this.value = val.substr(0, start) + ` ![${sub}](Image_Url_Here) ` + val.substr(end); break; case 'ul': diff = 2; sub = sub.replace(/^.+$/gm, (t) => `* ${t}`); this.value = val.substr(0, start) + `${sub}\n` + val.substr(end); break; case 'ol': var i = 1; diff = 3; sub = sub.replace(/^.+$/gm, (t) => `${i++ == 1 ? '1.' : '*'} ${t}`); this.value = val.substr(0, start) + `${sub}\n` + val.substr(end); break; case 'help': this.__preview = false; this.__disableTools = this.__help = !this.__help; break; case 'preview': this.__help = false; this.__disableTools = this.__preview = !this.__preview; break; } this.__input.focus(); if (sub == 'EditThis' && btn != 'preview' && btn != 'help') UIEvent.queueTask(() => this.__input.setSelectionRange(start + diff, start + diff + sub.length)); } clear() { this.value = ''; this.__input.focus(); UIEvent.fireEvent('change', this.element, this.value); } busy; disable(disabled?) { this.busy = disabled; } fireChange(evt) { evt.stopPropagation(); UIEvent.fireEvent('change', this.element, this.value); } __focus; fireBlur() { this.__focus = false; UIEvent.fireEvent('blur', this.element); } fireFocus() { this.__focus = true; UIEvent.fireEvent('focus', this.element); } }