11'use strict' ;
22
3+ const { tryToCatch} = require ( 'try-to-catch' ) ;
4+
35/* global CloudCmd, DOM */
46CloudCmd . EditNames = exports ;
57
6- const currify = require ( 'currify' ) ;
78const exec = require ( 'execon' ) ;
89const supermenu = require ( 'supermenu' ) ;
910const multiRename = require ( 'multi-rename' ) ;
1011
11- const reject = Promise . reject . bind ( Promise ) ;
12-
1312const Info = DOM . CurrentInfo ;
1413const { Dialog} = DOM ;
1514
16- const refresh = currify ( _refresh ) ;
17- const rename = currify ( _rename ) ;
18-
1915let Menu ;
2016
2117const ConfigView = {
@@ -93,7 +89,7 @@ function setListeners() {
9389 DOM . Events . addOnce ( 'contextmenu' , element , setMenu ) ;
9490}
9591
96- function applyNames ( ) {
92+ async function applyNames ( ) {
9793 const dir = Info . dirPath ;
9894 const from = getActiveNames ( ) ;
9995 const nameIndex = from . indexOf ( Info . name ) ;
@@ -105,18 +101,18 @@ function applyNames() {
105101
106102 const root = CloudCmd . config ( 'root' ) ;
107103
108- Promise
109- . resolve ( root )
110- . then ( rename ( dir , from , to ) )
111- . then ( refresh ( to , nameIndex ) )
112- . catch ( alert ) ;
104+ const response = rename ( dir , from , to , root ) ;
105+ const [ error ] = await tryToCatch ( refresh , to , nameIndex , response ) ;
106+
107+ if ( error )
108+ alert ( error ) ;
113109}
114110
115- function _refresh ( to , nameIndex , res ) {
116- if ( res . status === 404 )
117- return res
118- . text ( )
119- . then ( reject ) ;
111+ function refresh ( to , nameIndex , res ) {
112+ if ( res . status === 404 ) {
113+ const error = res . text ( ) ;
114+ throw error ;
115+ }
120116
121117 const currentName = to [ nameIndex ] ;
122118
@@ -132,7 +128,7 @@ function getDir(root, dir) {
132128 return root + dir ;
133129}
134130
135- function _rename ( path , from , to , root ) {
131+ function rename ( path , from , to , root ) {
136132 const dir = getDir ( root , path ) ;
137133 const { prefix} = CloudCmd ;
138134
@@ -172,8 +168,8 @@ function setMenu(event) {
172168 } ;
173169
174170 const menuData = {
175- 'Save Ctrl+S' : ( ) => {
176- applyNames ( ) ;
171+ 'Save Ctrl+S' : async ( ) => {
172+ await applyNames ( ) ;
177173 hide ( ) ;
178174 } ,
179175 'Go To Line Ctrl+G' : ( ) => {
@@ -214,6 +210,7 @@ async function isChanged() {
214210 if ( ! editor . isChanged ( ) )
215211 return ;
216212
217- const [ , names ] = await Dialog . confirm ( msg ) ;
218- names && applyNames ( ) ;
213+ const [ cancel ] = await Dialog . confirm ( msg ) ;
214+
215+ ! cancel && await applyNames ( ) ;
219216}
0 commit comments