Convert promptToSaveItem

This commit is contained in:
Wliu
2017-11-18 00:21:40 +01:00
parent 3591f17738
commit c3bc4973f9

View File

@@ -790,57 +790,53 @@ class Pane {
}
promptToSaveItem (item, options = {}) {
if (typeof item.shouldPromptToSave !== 'function' || !item.shouldPromptToSave(options)) {
return Promise.resolve(true)
}
let uri
if (typeof item.getURI === 'function') {
uri = item.getURI()
} else if (typeof item.getUri === 'function') {
uri = item.getUri()
} else {
return Promise.resolve(true)
}
const title = (typeof item.getTitle === 'function' && item.getTitle()) || uri
const saveDialog = (saveButtonText, saveFn, message) => {
const chosen = this.applicationDelegate.confirm({
message,
detailedMessage: 'Your changes will be lost if you close this item without saving.',
buttons: [saveButtonText, 'Cancel', "&Don't Save"]}
)
switch (chosen) {
case 0:
return new Promise(resolve => {
return saveFn(item, error => {
if (error instanceof SaveCancelledError) {
resolve(false)
} else if (error) {
saveDialog(
'Save as',
this.saveItemAs,
`'${title}' could not be saved.\nError: ${this.getMessageForErrorCode(error.code)}`
).then(resolve)
} else {
resolve(true)
}
})
})
case 1:
return Promise.resolve(false)
case 2:
return Promise.resolve(true)
return new Promise((resolve, reject) => {
if (typeof item.shouldPromptToSave !== 'function' || !item.shouldPromptToSave(options)) {
return resolve(true)
}
}
return saveDialog(
'Save',
this.saveItem,
`'${title}' has changes, do you want to save them?`
)
let uri
if (typeof item.getURI === 'function') {
uri = item.getURI()
} else if (typeof item.getUri === 'function') {
uri = item.getUri()
} else {
return resolve(true)
}
const title = (typeof item.getTitle === 'function' && item.getTitle()) || uri
const saveDialog = (saveButtonText, saveFn, message) => {
this.applicationDelegate.confirm({
message,
detailedMessage: 'Your changes will be lost if you close this item without saving.',
buttons: [saveButtonText, 'Cancel', "&Don't Save"]
}, response => {
switch (response) {
case 0:
return saveFn(item, error => {
if (error instanceof SaveCancelledError) {
resolve(false)
} else if (error) {
saveDialog(
'Save as',
this.saveItemAs,
`'${title}' could not be saved.\nError: ${this.getMessageForErrorCode(error.code)}`
)
} else {
resolve(true)
}
})
case 1:
return resolve(false)
case 2:
return resolve(true)
}
})
}
saveDialog('Save', this.saveItem, `'${title}' has changes, do you want to save them?`)
})
}
// Public: Save the active item.