Browse Source

Better copy and paste

With thi change we now use a json representation to copy and paste list items.
master
Peter Stuifzand 9 months ago
parent
commit
b8910f9aa6
  1. 5
      editor/src/menu.js
  2. 35
      list-editor/index.js
  3. 2
      list-editor/store.js

5
editor/src/menu.js

@ -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'

35
list-editor/index.js

@ -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 item = $(this).parents('.list-item')
let id = item.attr('data-id')
let currentID = store.currentID(index);
let baseIndent = store.value(currentID).indented
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')

2
list-editor/store.js

@ -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)

Loading…
Cancel
Save