Don't save when moving the cursor

This commit is contained in:
Peter Stuifzand 2020-10-15 22:38:04 +02:00
parent 7867d1caa2
commit 0e01fe7a16
2 changed files with 42 additions and 6 deletions

View File

@ -220,7 +220,10 @@ function editor(root, inputData, options) {
})
})
trigger('change')
if (store.hasChanged()) {
trigger('change')
store.clearChanged()
}
let $span = $('<div class="content">');
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()
}
});
}

View File

@ -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,
};
}