striven-editor

Maintained and contributed to an open source WYSIWYG editor as an internship project that made it to production and was adopted by the company I was working at.
git clone https://unitehenry.com/git/striven-editor.git
Log | Files | Refs | README | LICENSE

commit ee07cc437be32a780fad12aaac76a48149c6afd5
parent b723be8b8facf3c6a68f2492fc550c00c65c1acb
Author: unitehenry <unitehenry@gmail.com>
Date:   Thu, 16 Jul 2020 10:38:43 -0400

knockout will only initialize editor binding handler once

Diffstat:
Msrc/ko-striveneditor.js | 121+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 60 insertions(+), 61 deletions(-)

diff --git a/src/ko-striveneditor.js b/src/ko-striveneditor.js @@ -1,77 +1,76 @@ import StrivenEditor from './striveneditor'; -console.log('welcome'); - export default class KoStrivenEditor { constructor(ko, bindingName = 'striveneditor') { - - ko.bindingHandlers.striveneditor = { - init: function( - element, - valueAccessor, - allBindings, - viewModel, - bindingContext, - ) { - // Get options - let options = valueAccessor(); - // Options must be passed as an observable - if (!ko.isObservable(options)) { - throw 'Options must be an observable.'; - } + if(!ko.bindingHandlers.striveneditor) { + ko.bindingHandlers.striveneditor = { + init: function( + element, + valueAccessor, + allBindings, + viewModel, + bindingContext, + ) { + // Get options + let options = valueAccessor(); + // Options must be passed as an observable + if (!ko.isObservable(options)) { + throw 'Options must be an observable.'; + } - // Get the value binding in case we are binding the editor's contents to an observable - let value = allBindings().value; - if (value && !ko.isObservable(value)) { - throw 'Value must be an observable.'; - } + // Get the value binding in case we are binding the editor's contents to an observable + let value = allBindings().value; + if (value && !ko.isObservable(value)) { + throw 'Value must be an observable.'; + } - // Pass the value to the editor in options - options().value = value(); + // Pass the value to the editor in options + options().value = value(); - let onChange = null; - let changeOpt = options().change; - if (changeOpt) { - if (ko.isObservable(changeOpt)) { - onChange = val => changeOpt(val); - } else { - onChange = changeOpt; + let onChange = null; + let changeOpt = options().change; + if (changeOpt) { + if (ko.isObservable(changeOpt)) { + onChange = val => changeOpt(val); + } else { + onChange = changeOpt; + } } - } - let editor = new StrivenEditor(element, { - ...options(), - change: onChange, - toolbarTemplate: - options().toolbarTemplate && options().toolbarTemplate(), - }); - let pauseUpdate = false; + let editor = new StrivenEditor(element, { + ...options(), + change: onChange, + toolbarTemplate: + options().toolbarTemplate && options().toolbarTemplate(), + }); + let pauseUpdate = false; - // Handle updates and changes to value observable - if (ko.isObservable(value)) { - // Listen to input event - editor.body.oninput = e => { - pauseUpdate = true; - value(editor.getContent()); - pauseUpdate = false; - }; + // Handle updates and changes to value observable + if (ko.isObservable(value)) { + // Listen to input event + editor.body.oninput = e => { + pauseUpdate = true; + value(editor.getContent()); + pauseUpdate = false; + }; - // Subscribe to the value observable to detect when it is changed and update the editor - value.subscribe(newValue => { - // Update the content if it's not being updated from within the editor - if (!pauseUpdate) { - editor.setContent(newValue); - } - }); - } + // Subscribe to the value observable to detect when it is changed and update the editor + value.subscribe(newValue => { + // Update the content if it's not being updated from within the editor + if (!pauseUpdate) { + editor.setContent(newValue); + } + }); + } - // Set the editor in options - options().api = editor; + // Set the editor in options + options().api = editor; - ko.applyBindings(bindingContext, editor.toolbar); - return { controlsDescendantBindings: true }; + ko.applyBindings(bindingContext, editor.toolbar); + return { controlsDescendantBindings: true }; - }, - }; + }, + }; + } } }