From 0e01fe7a169add293be6107f6644bdba01061e15 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Thu, 15 Oct 2020 22:38:04 +0200 Subject: [PATCH] Don't save when moving the cursor --- list-editor/index.js | 19 ++++++++++++++++--- list-editor/store.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/list-editor/index.js b/list-editor/index.js index 800a2a1..d5577de 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -220,7 +220,10 @@ function editor(root, inputData, options) { }) }) - trigger('change') + if (store.hasChanged()) { + trigger('change') + store.clearChanged() + } let $span = $('
'); options.transform(text, $span) @@ -258,13 +261,23 @@ function editor(root, inputData, options) { function save() { return new Promise(function (resolve, reject) { - resolve(store.debug().result); + if (store.hasChanged()) { + resolve(store.debug().result) + store.clearChanged() + } else { + reject() + } }); } function saveTree(from) { return new Promise(function (resolve, reject) { - resolve(store.tree(from)) + if (store.hasChanged()) { + resolve(store.tree(from)) + store.clearChanged() + } else { + reject() + } }); } diff --git a/list-editor/store.js b/list-editor/store.js index 0f94495..318fef2 100644 --- a/list-editor/store.js +++ b/list-editor/store.js @@ -9,6 +9,7 @@ import _ from 'lodash'; function Store(inputData) { let idList = []; let values = {}; + let changed = false; let ID = function () { return '_' + Math.random().toString(36).substr(2, 12); @@ -123,6 +124,7 @@ function Store(inputData) { id = newId } idList.splice(index, 0, id) + changed = true return id } @@ -151,6 +153,7 @@ function Store(inputData) { return item.id }) idList.splice(index + 1, 0, ...newItems) + changed = true return newItems[0] } @@ -168,6 +171,7 @@ function Store(inputData) { delete item.children values[id] = item; idList.push(id) + changed = true return id; } @@ -185,7 +189,12 @@ function Store(inputData) { */ function update(currentId, callback) { let index = _.findIndex(idList, (id) => id === currentId) - values[currentId] = callback(values[currentId], values[idList[index - 1]], values[idList[index + 1]]) + let oldValue = _.clone(values[currentId]) + let newValue = callback(values[currentId], values[idList[index - 1]], values[idList[index + 1]]); + if (oldValue.text !== newValue.text) { + values[currentId] = newValue + changed = true + } return currentId } @@ -209,6 +218,7 @@ function Store(inputData) { function remove(start, len) { idList.splice(start, len) + changed = true } /** @@ -255,6 +265,7 @@ function Store(inputData) { list.push(item) return list }, []) + changed = true } /** @@ -266,7 +277,9 @@ function Store(inputData) { let fromIndex = _.findIndex(idList, (id) => id === from) let item = values[from] remove(fromIndex, 1) - return insertBefore(to, item) + let result = insertBefore(to, item) + changed = true + return result } /** @@ -362,6 +375,14 @@ function Store(inputData) { return top(stack) } + function clearChanged() { + changed = false + } + + function hasChanged() { + return changed; + } + _.each(inputData, (d) => { append(d) }) @@ -386,7 +407,9 @@ function Store(inputData) { nextCursorPosition, debug, tree, - flat + flat, + hasChanged, + clearChanged, }; }