« get me outta code hell

Move Paste into menu - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-08-03 10:59:25 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-03 10:59:25 -0300
commit20b9ffb594c5cea5b90b1aa07c91764610b70100 (patch)
tree3e18b8a41659f85eeb3b9e688a59d84237ff8436
parent366d162c6b4bf2e9365ba08c0fab6b76641d5e28 (diff)
Move Paste into menu
-rw-r--r--ui.js65
1 files changed, 32 insertions, 33 deletions
diff --git a/ui.js b/ui.js
index af7db75..1500ad7 100644
--- a/ui.js
+++ b/ui.js
@@ -181,34 +181,35 @@ class AppElement extends FocusElement {
       }
     })
 
-    const handlePaste = offset => item => {
-      if (this.editMode) {
-        if (this.markGrouplike.items.length) {
-          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
-              // here is to make the parentSymbols be properly set, as well as to create a set of
-              // totally new objects, so none of the pasted groups already appear somewhere else;
-              // or rather, not as the same objects.) (..Done!..)
-              parent.items.splice(index, 0, ...cloneGrouplike(this.markGrouplike).items.map(
-                item => Object.assign({}, item, {[parentSymbol]: parent})
-              ))
-              updateListingsFor(parent)
-            },
-            undo: () => {
-              parent.items.splice(index, this.markGrouplike.items.length)
-              updateListingsFor(parent)
-            }
-          })
+    grouplikeListing.on('paste', (item, {where = 'below'}) => {
+      if (this.editMode && this.markGrouplike.items.length) {
+        let parent, index
+
+        if (where === 'inside') {
+          parent = item
+          index = 0
+        } else if (where === 'above') {
+          parent = item[parentSymbol]
+          index = parent.items.indexOf(item)
+        } else if (where === 'below') {
+          parent = item[parentSymbol]
+          index = parent.items.indexOf(item) + 1
         }
-      }
-    }
 
-    grouplikeListing.on('paste (below)', handlePaste(+1))
-    grouplikeListing.on('paste (above)', handlePaste(0))
-    grouplikeListing.on('paste (top)', handlePaste('TOP'))
+        this.undoManager.pushAction({
+          activate: () => {
+            parent.items.splice(index, 0, ...cloneGrouplike(this.markGrouplike).items.map(
+              item => Object.assign({}, item, {[parentSymbol]: parent})
+            ))
+            updateListingsFor(parent)
+          },
+          undo: () => {
+            parent.items.splice(index, this.markGrouplike.items.length)
+            updateListingsFor(parent)
+          }
+        })
+      }
+    })
 
     const handleSelectFromPathElement = item => {
       this.form.selectInput(grouplikeListing)
@@ -715,12 +716,6 @@ 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)
     }
@@ -755,7 +750,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', 'mark', 'browse', 'queue', 'menu']) {
+        for (const evtName of ['download', 'remove', 'mark', 'paste', 'browse', 'queue', 'menu']) {
           itemElement.on(evtName, (...data) => this.emit(evtName, item, ...data))
         }
         form.addInput(itemElement)
@@ -935,11 +930,15 @@ class GrouplikeItemElement extends Button {
       this.emit('remove')
     } else if (telc.isCaselessLetter(keyBuf, 'm')) {
       const editMode = this.recordStore.app.editMode
+      const anyMarked = editMode && !!this.recordStore.app.markGrouplike.items.length
       this.emit('menu', {
         x: this.absLeft,
         y: this.absTop + 1,
         items: [
           editMode && {label: this.isMarked ? 'Unmark' : 'Mark', action: () => this.emit('mark')},
+          anyMarked && {label: 'Paste (above)', action: () => this.emit('paste', {where: 'above'})},
+          anyMarked && {label: 'Paste (below)', action: () => this.emit('paste', {where: 'below'})},
+          anyMarked && isGroup(this.item) && {label: 'Paste (inside)', action: () => this.emit('paste', {where: 'inside'})},
           {label: 'Play', action: () => this.emit('queue', {where: 'next', play: true})},
           {label: 'Play next', action: () => this.emit('queue', {where: 'next'})},
           {label: 'Play at end', action: () => this.emit('queue', {where: 'end'})},