Better copy and paste

With thi change we now use a json representation to copy and paste list items.
This commit is contained in:
Peter Stuifzand 2020-11-16 22:38:38 +01:00
parent 10d6d7521a
commit b8910f9aa6
3 changed files with 19 additions and 21 deletions

View File

@ -29,8 +29,9 @@ function connectContextMenu(editor) {
copy: {
name: 'Copy',
callback: function (key, opt) {
editor.copy(this, {recursive: true}).then(result => {
copy(renderTree(result).join("\n"))
editor.flat(this).then(result => {
let data = JSON.stringify(result);
copy(data)
})
},
className: 'action-copy'

View File

@ -108,6 +108,15 @@ function editor(root, inputData, options) {
});
}
function flat(element) {
let item = $(element).parents('.list-item')
let id = item.attr('data-id')
return new Promise(function (resolve, reject) {
resolve(store.flat(id));
});
}
function zoomin(element, opt) {
let item = $(element).parents('.list-item')
let id = item.attr('data-id')
@ -159,6 +168,7 @@ function editor(root, inputData, options) {
save,
saveTree,
copy,
flat,
update,
start,
zoomin,
@ -477,26 +487,13 @@ function editor(root, inputData, options) {
return true
}
let parentItem = $(this).parents('.list-item')
let index = $(root).children('div.list-item').index(parentItem)
let pastedData = event.originalEvent.clipboardData.getData('text')
let lines = pastedData.split(/\n/)
if (lines.length === 1) {
return true;
}
let pastedData = event.originalEvent.clipboardData.getData('text/plain')
let items = JSON.parse(pastedData.toString());
let currentID = store.currentID(index);
let baseIndent = store.value(currentID).indented
let item = $(this).parents('.list-item')
let id = item.attr('data-id')
let newItems = _.filter(_.map(lines, function (line) {
if (line.length === 0) return;
let matches = line.match(/(\s{4})/g)
let relIndent = matches ? matches.length : 0;
let newItem = newListItem(baseIndent + relIndent)
newItem.text = line.replace(/^\s+/, '')
return newItem
}))
store.insertAfter(currentID, ...newItems)
store.insertAfter(id, ...items)
trigger('change')

View File

@ -156,8 +156,8 @@ function Store(inputData) {
if (!id) {
let newId = ID()
item.id = newId
values[newId] = item
}
values[item.id] = item
return item.id
})
idList.splice(index + 1, 0, ...newItems)