Skip to content

Commit 6da47a5

Browse files
committed
feat: move to trash instead of delete when deleting files from project tree
1 parent 6e2ebe7 commit 6da47a5

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/document/DocumentCommandHandlers.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,14 +1846,35 @@ define(function (require, exports, module) {
18461846
}
18471847
}
18481848

1849+
function _getDeleteMessageTemplate(isFile, canMoveToTrash) {
1850+
if(!Phoenix.isNativeApp || !canMoveToTrash){
1851+
return isFile ? Strings.CONFIRM_FILE_DELETE : Strings.CONFIRM_FOLDER_DELETE;
1852+
}
1853+
if(Phoenix.platform === "win") {
1854+
return isFile ? Strings.CONFIRM_FILE_DELETE_RECYCLE_BIN : Strings.CONFIRM_FOLDER_DELETE_RECYCLE_BIN;
1855+
}
1856+
return isFile ? Strings.CONFIRM_FILE_DELETE_TRASH : Strings.CONFIRM_FOLDER_DELETE_TRASH;
1857+
}
1858+
1859+
function _getDeleteButtonString(canMoveToTrash) {
1860+
if(!Phoenix.isNativeApp || !canMoveToTrash){
1861+
return Strings.DELETE;
1862+
}
1863+
if(Phoenix.platform === "win") {
1864+
return Strings.MOVE_TO_RECYCLE_BIN;
1865+
}
1866+
return Strings.MOVE_TO_TRASH;
1867+
}
1868+
18491869
/** Delete file command handler **/
18501870
function handleFileDelete() {
1851-
var entry = ProjectManager.getSelectedItem();
1871+
const entry = ProjectManager.getSelectedItem();
1872+
const canMoveToTrash = Phoenix.app.canMoveToTrash(entry.fullPath);
18521873
Dialogs.showModalDialog(
18531874
DefaultDialogs.DIALOG_ID_EXT_DELETED,
18541875
Strings.CONFIRM_DELETE_TITLE,
18551876
StringUtils.format(
1856-
entry.isFile ? Strings.CONFIRM_FILE_DELETE : Strings.CONFIRM_FOLDER_DELETE,
1877+
_getDeleteMessageTemplate(entry.isFile, canMoveToTrash),
18571878
StringUtils.breakableUrl(ProjectManager.getProjectRelativePath(entry.fullPath))
18581879
),
18591880
[
@@ -1865,12 +1886,15 @@ define(function (require, exports, module) {
18651886
{
18661887
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
18671888
id: Dialogs.DIALOG_BTN_OK,
1868-
text: Strings.DELETE
1889+
text: _getDeleteButtonString(canMoveToTrash)
18691890
}
18701891
]
18711892
)
18721893
.done(function (id) {
18731894
if (id === Dialogs.DIALOG_BTN_OK) {
1895+
if(Phoenix.isNativeApp && canMoveToTrash) {
1896+
Phoenix.app.moveToTrash(entry.fullPath);
1897+
}
18741898
ProjectManager.deleteItem(entry);
18751899
}
18761900
});

src/nls/root/strings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ define({
185185
"CONFIRM_DELETE_TITLE": "Confirm Delete",
186186
"CONFIRM_FILE_DELETE": "Are you sure you want to delete the file <span class='dialog-filename'>{0}</span>?",
187187
"CONFIRM_FOLDER_DELETE": "Are you sure you want to delete the folder <span class='dialog-filename'>{0}</span>?",
188+
"CONFIRM_FILE_DELETE_TRASH": "Are you sure you want to delete the file <span class='dialog-filename'>{0}</span>?<br>You can restore this file from the trash.",
189+
"CONFIRM_FOLDER_DELETE_TRASH": "Are you sure you want to delete the folder <span class='dialog-filename'>{0}</span>?<br>You can restore this folder from the trash.",
190+
"CONFIRM_FILE_DELETE_RECYCLE_BIN": "Are you sure you want to delete the file <span class='dialog-filename'>{0}</span>?<br>You can restore this file from the Recycle Bin.",
191+
"CONFIRM_FOLDER_DELETE_RECYCLE_BIN": "Are you sure you want to delete the folder <span class='dialog-filename'>{0}</span>?<br>You can restore this folder from the Recycle Bin.",
192+
"MOVE_TO_TRASH": "Move to Trash",
193+
"MOVE_TO_RECYCLE_BIN": "Move to Recycle Bin",
188194
"FILE_DELETED_TITLE": "File Deleted",
189195
"EXT_MODIFIED_WARNING": "<span class='dialog-filename'>{0}</span> has been modified on disk outside of {APP_NAME}.<br /><br />Do you want to save the file and overwrite those changes?",
190196
"EXT_MODIFIED_MESSAGE": "<span class='dialog-filename'>{0}</span> has been modified on disk outside of {APP_NAME}, but also has unsaved changes in {APP_NAME}.<br /><br />Which version do you want to keep?",

src/phoenix/shell.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ Phoenix.app = {
347347
}
348348
return fullOrRelativeVFSPath;
349349
},
350+
canMoveToTrash: function(fullVFSPath) {
351+
if(!Phoenix.isNativeApp || !fullVFSPath || !fullVFSPath.startsWith(Phoenix.VFS.getTauriDir())){
352+
// only tauri paths can be moved to trash
353+
return false;
354+
}
355+
return true;
356+
},
357+
moveToTrash: async function(fullVFSPath) {
358+
if(!Phoenix.isNativeApp){
359+
throw new Error("moveToTrash is not supported in browsers");
360+
}
361+
if(!fullVFSPath){
362+
throw new Error("Please specify a path to move to trash");
363+
}
364+
if(!fullVFSPath.startsWith(Phoenix.VFS.getTauriDir())) {
365+
throw new Error("moveToTrash only works with tauri paths, but got: "+ fullVFSPath);
366+
}
367+
const platformPath = Phoenix.fs.getTauriPlatformPath(fullVFSPath);
368+
return window.__TAURI__.invoke('_move_to_trash', { deletePath: platformPath });
369+
},
350370
setWindowTitle: async function (title) {
351371
window.document.title = title;
352372
if(Phoenix.isNativeApp) {

0 commit comments

Comments
 (0)