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 02a5359085c4a24ad7a3d07c498aeb64ac470c7f
parent 183873e1ff15a7bb895211e80a0da9cb36d718bd
Author: ebutler <e.c.butler@outlook.com>
Date:   Fri,  2 Jul 2021 12:26:00 -0400

Updated the knockout binding to prevent the value from triggering the subscription

Diffstat:
Mpackage-lock.json | 2+-
Msrc/ko-striveneditor.js | 127++++++++++++++++++++++++++++++++++++++++---------------------------------------
2 files changed, 66 insertions(+), 63 deletions(-)

diff --git a/package-lock.json b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@striven-erp/striven-editor", - "version": "3.9.6", + "version": "3.9.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/ko-striveneditor.js b/src/ko-striveneditor.js @@ -1,76 +1,79 @@ import StrivenEditor from './striveneditor'; export default class KoStrivenEditor { - constructor(ko, bindingName = 'striveneditor') { - 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.'; - } + constructor(ko, bindingName = 'striveneditor') { + 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()); + setTimeout(() => { + pauseUpdate = false; + }, 0); + + }; - // 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 }; - }, - }; + }, + }; + } } - } }