« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ui.js24
1 files changed, 15 insertions, 9 deletions
diff --git a/ui.js b/ui.js
index 89801d7..762914e 100644
--- a/ui.js
+++ b/ui.js
@@ -175,8 +175,8 @@ class AppElement extends FocusElement {
     const handlePaste = offset => item => {
       if (this.editMode) {
         if (this.markGrouplike.items.length) {
-          const parent = item[parentSymbol]
-          const index = parent.items.indexOf(item) + offset
+          const parent = offset === 'TOP' ? item : item[parentSymbol]
+          const index = offset === 'TOP' ? 0 : (parent.items.indexOf(item) + offset)
           this.undoManager.pushAction({
             activate: () => {
               // TODO: More "proper" way of cloning a grouplike. (The purpose of updateGroupFormat
@@ -199,6 +199,7 @@ class AppElement extends FocusElement {
 
     grouplikeListing.on('paste (below)', handlePaste(+1))
     grouplikeListing.on('paste (above)', handlePaste(0))
+    grouplikeListing.on('paste (top)', handlePaste('TOP'))
 
     const handleSelectFromPathElement = item => {
       this.form.selectInput(grouplikeListing)
@@ -707,6 +708,12 @@ class GrouplikeListingElement extends FocusElement {
   keyPressed(keyBuf) {
     if (telc.isBackspace(keyBuf)) {
       this.loadParentGrouplike()
+    } else if (this.currentItem && telc.isCharacter(keyBuf, 'p')) {
+      this.emit('paste (below)', this.currentItem)
+    } else if (this.currentItem && telc.isCharacter(keyBuf, 'P')) {
+      this.emit('paste (above)', this.currentItem)
+    } else if (!this.currentItem && telc.isCaselessLetter(keyBuf, 'p')) {
+      this.emit('paste (top)', this.grouplike)
     } else {
       return super.keyPressed(keyBuf)
     }
@@ -741,7 +748,7 @@ class GrouplikeListingElement extends FocusElement {
     if (this.grouplike.items.length) {
       for (const item of this.grouplike.items) {
         const itemElement = new GrouplikeItemElement(item, this.recordStore)
-        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'paste (above)', 'paste (below)', 'select (space)', 'select (enter)', 'queue', 'queue (shuffled)', 'queue (play next)']) {
+        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'select (space)', 'select (enter)', 'queue', 'queue (shuffled)', 'queue (play next)']) {
           itemElement.on(evtName, () => this.emit(evtName, item))
         }
         form.addInput(itemElement)
@@ -800,6 +807,11 @@ class GrouplikeListingElement extends FocusElement {
       return 'No group open'
     }
   }
+
+  get currentItem() {
+    const element = this.form.inputs[this.form.curIndex] || null
+    return element && element.item
+  }
 }
 
 class GrouplikeListingForm extends ListScrollForm {
@@ -916,14 +928,8 @@ class GrouplikeItemElement extends Button {
       this.emit('remove (backspace)')
     } else if (telc.isCaselessLetter(keyBuf, 'x')) {
       this.emit('remove (x)')
-
-      // Edit-mode things
     } else if (telc.isCaselessLetter(keyBuf, 'm')) {
       this.emit('mark')
-    } else if (telc.isCharacter(keyBuf, 'p')) {
-      this.emit('paste (below)')
-    } else if (telc.isCharacter(keyBuf, 'P')) {
-      this.emit('paste (above)')
     }
   }
 }