From e4539b520cc565b8206be56d8b5c42427ebc9911 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Wed, 12 Jan 2022 22:45:46 +0100 Subject: [PATCH] Problem: some standard copy paste formats do not work Solution: add solution for copying text from Roam. This formats looks like: - parent - child 1 - child 2 The first line starts with a dash and space. The other lines also start with spaces (multiple of 4 or 0), dash, space. Every 4 spaces is 1 indent in our format. --- list-editor/index.js | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/list-editor/index.js b/list-editor/index.js index c644428..1dc9c8a 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -523,22 +523,42 @@ function editor(root, inputData, options) { trigger('change') return false } catch (e) { - let items = pastedData.toString().split(/\n+/); - let item = $(this).parents('.list-item') - let id = item.attr('data-id') - if (items.length === 1) { - return true - } else { + let inputString = pastedData.toString() + if (inputString.match(/^- /)) { + let item = $(this).parents('.list-item') + let id = item.attr('data-id') + let lines = inputString.split(/( *)- /) + lines.shift() const firstItem = store.value(id) - items = _.map(items, text => { - const item = newListItem(firstItem.indented) - item.text = text + const firstIndent = firstItem.indented + + let items = _.map(_.chunk(lines, 2), line => { + const indent = Math.trunc(line[0].length / 4); + const item = newListItem(firstItem.indented+indent) + item.text = _.trimEnd(line[1]).replace(/\n/g, " \n") return item }) store.insertAfter(id, ...items) trigger('change') + return false + } else { + let items = inputString.split(/\n+/); + let item = $(this).parents('.list-item') + let id = item.attr('data-id') + if (items.length === 1) { + return true + } else { + const firstItem = store.value(id) + items = _.map(items, text => { + const item = newListItem(firstItem.indented) + item.text = text + return item + }) + store.insertAfter(id, ...items) + trigger('change') + } + return false } - return false } });