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 c2e955b6a1f11218a578b1e2bf7dc52a4054d936
parent 75ba1a3e499aaa1d76e366870209bb56e39e62b1
Author: ebutler <e.c.butler@outlook.com>
Date:   Wed, 23 Oct 2019 16:47:21 -0400

Update to KO and Vue wrappers

Diffstat:
Mdemo/src/App.vue | 13++++++++++---
Msrc/ko-striveneditor.js | 38+++++++++++++++++++++++++++++++++-----
Msrc/vue-striveneditor.vue | 13+++++++++----
3 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/demo/src/App.vue b/demo/src/App.vue @@ -4,11 +4,14 @@ style="font-family: Segoe UI; color: #2c3e50; margin-top: 20vh; display: flex; justify-content: center; align-items: center; flex-direction: column; padding: 0 1rem;" > <div style="display: flex; padding: 1rem; width: 100%; justify-content: center;"> - <div + <!-- <div ref="editor" class="editor" style="margin-top: 20px; background-color: #fff; min-height: 300px; width: 600px;" - ></div> + ></div> --> + <striven-editor :placeholder="'Hey there'" v-model="notes"></striven-editor> + {{notes}} + <button @click="notes = null">Clear</button> </div> <div ref="customButton" class="custom-button" @click="sendContent"> Send @@ -18,10 +21,14 @@ <script> /* eslint-disable */ -import { StrivenEditor } from "@striven-erp/striven-editor"; +import { VueStrivenEditor as StrivenEditor } from "@striven-erp/striven-editor"; export default { name: "app", + components: {StrivenEditor}, + data(){ + return {notes: ''}; + }, mounted() { const editorOptions = { metaUrl: "https://mighty-anchorage-82390.herokuapp.com/meta", // metaserver.js diff --git a/src/ko-striveneditor.js b/src/ko-striveneditor.js @@ -4,11 +4,39 @@ export default class KoStrivenEditor { constructor(ko) { ko.bindingHandlers.striveneditor = { init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { - var valueOption = valueAccessor(); - var value = ko.utils.unwrapObservable(valueOption); - - var editor = new StrivenEditor(value); + + // Unwrap options + var options = ko.utils.unwrapObservable(valueAccessor()); + // Get the value binding in case we are binding the editor's contents to an observable + var value = allBindings().value; + if (ko.isObservable(value)) { + // Pass the value to the editor in options + options.value = value(); + } + + let editor = new StrivenEditor(element, options); + 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; + }; + + // 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); + } + }); + } + + } - } + }; } } \ No newline at end of file diff --git a/src/vue-striveneditor.vue b/src/vue-striveneditor.vue @@ -30,20 +30,25 @@ export default { vm.editor = new StrivenEditor(vm.$refs.editor, config); // Listen to input event for v-model support vm.editor.body.oninput = e => { + vm.pauseUpdate = true; vm.$emit("input", vm.editor.getContent()); + }; }, data() { return { - editor: {} + editor: {}, + pauseUpdate: false }; }, watch:{ value(val){ - // Clear the editor if the value is cleared - if (!val){ - this.editor.setContent(null); + // Set the editor content when not modified from within the editor + if (!this.pauseUpdate){ + this.editor.setContent(val); } + + this.pauseUpdate = false; } } };