Skip to content

Commit 0185551

Browse files
authored
lib: handle onpaste and create new nodes for each line (#296)
1 parent 3bf48f0 commit 0185551

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/ui/node/editor.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const NodeEditor: m.Component = {
4545
if (node.parent && node.parent.hasComponent(Document) && window.Editor) {
4646
editor = CodeMirrorEditor;
4747
}
48-
return m(editor, {id, getter, setter, display, onkeydown, onfocus, oninput, placeholder});
48+
return m(editor, {id, getter, setter, display, onkeydown, onfocus, oninput, placeholder, workbench, path});
4949
}
5050
}
5151

@@ -145,7 +145,7 @@ export const TextAreaEditor: m.Component<Attrs, State> = {
145145
onupdate() {
146146
this.updateHeight();
147147
},
148-
view ({attrs: {id, onkeydown, onfocus, onblur, oninput, getter, setter, display, placeholder}, state}) {
148+
view ({attrs: {id, onkeydown, onfocus, onblur, oninput, getter, setter, display, placeholder, path, workbench}, state}) {
149149
const value = (state.editing)
150150
? state.buffer
151151
: (display) ? display() : getter();
@@ -180,6 +180,29 @@ export const TextAreaEditor: m.Component<Attrs, State> = {
180180
oninput(e);
181181
}
182182
}
183+
const handlePaste = (e) => {
184+
const textData = e.clipboardData.getData('Text');
185+
if (textData.length > 0) {
186+
e.preventDefault();
187+
e.stopPropagation();
188+
189+
const lines = textData.split('\n').map(line => line.trim()).filter(line => line.length > 0);
190+
state.buffer = lines.shift();
191+
setter(state.buffer, true);
192+
193+
let node = path.node;
194+
for (const line of lines) {
195+
const newNode = workbench.workspace.new(line);
196+
newNode.parent = node.parent;
197+
newNode.siblingIndex = node.siblingIndex + 1;
198+
m.redraw.sync();
199+
const p = path.clone();
200+
p.pop();
201+
workbench.focus(p.append(newNode));
202+
node = newNode;
203+
}
204+
}
205+
}
183206

184207
return (
185208
<div class="text-editor">
@@ -189,6 +212,7 @@ export const TextAreaEditor: m.Component<Attrs, State> = {
189212
onfocus={startEdit}
190213
onblur={finishEdit}
191214
oninput={edit}
215+
onpaste={handlePaste}
192216
placeholder={placeholder}
193217
onkeydown={onkeydown||defaultKeydown}
194218
value={value}>{value}</textarea>

0 commit comments

Comments
 (0)