Skip to content

Commit b10661d

Browse files
committed
Implement drag-and-drop file upload feature in app settings
* Added a new checkbox option to enable automatic upload and opening of files when dragged and dropped onto the app window. Updated relevant functions to handle this new setting, ensuring it is saved, reset, and checked for changes correctly. * Enhanced the UIWindow and launch_app functions to support the new upload_and_open_on_drag functionality, allowing for a smoother user experience when interacting with files.
1 parent 4dae0d6 commit b10661d

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/dev-center/js/apps.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ function generate_edit_app_section(app) {
436436
<p>This will set your app's window title to the opened file's name when a user opens a file in your app.</p>
437437
</div>
438438
439+
<div style="margin-top:30px;">
440+
<input type="checkbox" id="edit-app-upload-and-open-on-drag" name="edit-app-upload-and-open-on-drag" value="true" ${app.metadata?.upload_and_open_on_drag ? 'checked' : ''} ${app.background ? 'disabled' : ''}>
441+
<label for="edit-app-upload-and-open-on-drag" style="display: inline;">Upload and open files on drag-and-drop</label>
442+
<p>If checked, when the user drags and drops a file on the app it will be automatically uploaded to their desktop and opened in a new instance of the app.</p>
443+
</div>
444+
439445
<h3 style="font-size: 23px; border-bottom: 1px solid #EEE; margin-top: 50px; margin-bottom: 0px;">Misc</h3>
440446
<div style="margin-top:30px;">
441447
<input type="checkbox" id="edit-app-locked" name="edit-app-locked" value="true" ${app.metadata?.locked ? 'checked' : ''}>
@@ -511,7 +517,8 @@ function trackOriginalValues(){
511517
hideTitleBar: $('#edit-app-hide-titlebar').is(':checked'),
512518
locked: $('#edit-app-locked').is(':checked'),
513519
fullPageOnLanding: $('#edit-app-fullpage-on-landing').is(':checked'),
514-
setTitleToFile: $('#edit-app-set-title-to-file').is(':checked')
520+
setTitleToFile: $('#edit-app-set-title-to-file').is(':checked'),
521+
uploadOnDrag: $('#edit-app-upload-and-open-on-drag').is(':checked')
515522
}
516523
};
517524
}
@@ -548,7 +555,8 @@ function hasChanges() {
548555
$('#edit-app-hide-titlebar').is(':checked') !== originalValues.checkboxes.hideTitleBar ||
549556
$('#edit-app-locked').is(':checked') !== originalValues.checkboxes.locked ||
550557
$('#edit-app-fullpage-on-landing').is(':checked') !== originalValues.checkboxes.fullPageOnLanding ||
551-
$('#edit-app-set-title-to-file').is(':checked') !== originalValues.checkboxes.setTitleToFile
558+
$('#edit-app-set-title-to-file').is(':checked') !== originalValues.checkboxes.setTitleToFile ||
559+
$('#edit-app-upload-and-open-on-drag').is(':checked') !== originalValues.checkboxes.uploadOnDrag
552560
);
553561
}
554562

@@ -596,6 +604,7 @@ function resetToOriginalValues() {
596604
$('#edit-app-locked').prop('checked', originalValues.checkboxes.locked);
597605
$('#edit-app-fullpage-on-landing').prop('checked', originalValues.checkboxes.fullPageOnLanding);
598606
$('#edit-app-set-title-to-file').prop('checked', originalValues.checkboxes.setTitleToFile);
607+
$('#edit-app-upload-and-open-on-drag').prop('checked', originalValues.checkboxes.uploadOnDrag);
599608

600609
if (originalValues.icon) {
601610
$('#edit-app-icon').css('background-image', `url(${originalValues.icon})`);
@@ -1151,6 +1160,7 @@ $(document).on('click', '.edit-app-save-btn', async function (e) {
11511160
hide_titlebar: $('#edit-app-hide-titlebar').is(":checked"),
11521161
locked: $(`#edit-app-locked`).is(":checked") ?? false,
11531162
set_title_to_opened_file: $('#edit-app-set-title-to-file').is(":checked"),
1163+
upload_and_open_on_drag: $('#edit-app-upload-and-open-on-drag').is(":checked"),
11541164
},
11551165
filetypeAssociations: filetype_associations,
11561166
}).then(async (app) => {

src/gui/src/UI/UIWindow.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ async function UIWindow(options) {
117117
options.is_maximized = options.is_maximized ?? false;
118118
options.is_openFileDialog = options.is_openFileDialog ?? false;
119119
options.is_resizable = options.is_resizable ?? true;
120+
options.upload_and_open_on_drag = options.upload_and_open_on_drag ?? false;
120121

121122
// if this is a fullpage window, it won't be resizable
122123
if(options.is_fullpage){
@@ -257,6 +258,7 @@ async function UIWindow(options) {
257258
data-update_window_url = "${options.update_window_url && options.is_visible}"
258259
data-user_set_url_params = "${html_encode(user_set_url_params)}"
259260
data-initial_zindex = "${zindex}"
261+
data-upload_and_open_on_drag = "${options.upload_and_open_on_drag}"
260262
style=" z-index: ${zindex};
261263
${options.width !== undefined ? 'width: ' + html_encode(options.width) +'; ':''}
262264
${options.height !== undefined ? 'height: ' + html_encode(options.height) +'; ':''}
@@ -1689,7 +1691,7 @@ async function UIWindow(options) {
16891691

16901692
// --------------------------------------------------------
16911693
// Dragster
1692-
// Allow dragging of local files onto this window, if it's is_dir
1694+
// Allow dragging of local files onto this window, if it's is_dir or if upload_and_open_on_drag is true
16931695
// --------------------------------------------------------
16941696
$(el_window_body).dragster({
16951697
enter: function (dragsterEvent, event) {
@@ -1716,6 +1718,19 @@ async function UIWindow(options) {
17161718
// de-highlight all windows
17171719
$('.item-container').removeClass('item-container-active');
17181720
}
1721+
// if upload_and_open_on_drag is true, open the file in a new instance of the app
1722+
else if(options.upload_and_open_on_drag){
1723+
// upload items
1724+
window.upload_items(e.dataTransfer.items, window.desktop_path, {
1725+
success: (items) => {
1726+
// open app
1727+
launch_app({
1728+
name: options.app,
1729+
file_path: $(el_window).attr('data-path'),
1730+
})
1731+
}
1732+
})
1733+
}
17191734
e.stopPropagation();
17201735
e.preventDefault();
17211736
return false;

src/gui/src/helpers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ window.init_upload_using_dialog = function(el_target_container, target_path = nu
17701770
})
17711771
}
17721772

1773-
window.upload_items = async function(items, dest_path){
1773+
window.upload_items = async function(items, dest_path, options = {}){
17741774
let upload_progress_window;
17751775
let opid;
17761776

@@ -1845,6 +1845,11 @@ window.upload_items = async function(items, dest_path){
18451845
})
18461846
// remove from active_uploads
18471847
delete window.active_uploads[opid];
1848+
// if success callback is provided, call it
1849+
if(options.success && typeof options.success === 'function'){
1850+
console.log('success callback', items);
1851+
options.success(items);
1852+
}
18481853
},
18491854
// error
18501855
error: async function(err){

src/gui/src/helpers/launch_app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ const launch_app = async (options)=>{
356356
if(app_info.metadata?.set_title_to_opened_file !== undefined && typeof app_info.metadata.set_title_to_opened_file === 'boolean' && app_info.metadata.set_title_to_opened_file === true)
357357
title = options.file_path ? path.basename(options.file_path) : title;
358358

359+
// upload_on_drag
360+
let upload_and_open_on_drag = false;
361+
if(app_info.metadata?.upload_and_open_on_drag !== undefined && typeof app_info.metadata.upload_and_open_on_drag === 'boolean')
362+
upload_and_open_on_drag = app_info.metadata.upload_and_open_on_drag;
363+
359364
// open window
360365
el_win = UIWindow({
361366
element_uuid: uuid,
@@ -380,6 +385,7 @@ const launch_app = async (options)=>{
380385
is_resizable: window_resizable,
381386
has_head: ! hide_titlebar,
382387
show_in_taskbar: app_info.background ? false : window_options?.show_in_taskbar,
388+
upload_and_open_on_drag: upload_and_open_on_drag,
383389
});
384390

385391
// If the app is not in the background, show the window

0 commit comments

Comments
 (0)