From ab22f304df6cfcd4fc5103e7bfde740c963c537f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 14 Aug 2019 19:06:43 +0200 Subject: [PATCH] Automatically update preview on change --- Models/Editor.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Models/Editor.js b/Models/Editor.js index 89988ee..21d1efa 100644 --- a/Models/Editor.js +++ b/Models/Editor.js @@ -6,25 +6,28 @@ export class Editor { this.id = id; this.editor = document.getElementById(id); this.markdown = new Markdown.Converter({extensions: [], sanitize: true}); + this.editorContent = null; }; bind () { const editorButtons = document.querySelectorAll('#' + this.id + '-tools .editor-button'), - editorContent = this.editor.getElementsByTagName('textarea')[0], editorPreview = this.editor.getElementsByTagName('article')[0], length = editorButtons.length, self = this; - for(let i = 0; i < length; ++i) { + this.editorContent = this.editor.getElementsByTagName('textarea')[0]; + + for (let i = 0; i < length; ++i) { editorButtons[i].addEventListener('click', function(event) { // todo: identify button by class and then call function for this class. self.toolsButton(this, event); + jsOMS.triggerEvent(self.editorContent, 'input'); }); } - editorContent.addEventListener('change', function() { - editorPreview.innerHTML = self.markdown.makeHtml(editorContent.value); + this.editorContent.addEventListener('input', function() { + editorPreview.innerHTML = self.markdown.makeHtml(self.editorContent.value); }) };