mirror of
https://github.com/HeyPuter/puter
synced 2024-11-14 22:06:00 +00:00
adding ability to undo rename action
This commit is contained in:
parent
a18ec1efdb
commit
09c706a99d
@ -627,104 +627,7 @@ function UIItem(options){
|
|||||||
$(el_item_name_editor).removeClass('item-name-editor-active');
|
$(el_item_name_editor).removeClass('item-name-editor-active');
|
||||||
|
|
||||||
// Perform rename request
|
// Perform rename request
|
||||||
puter.fs.rename({
|
rename_file(options, new_name, old_name, el_item, el_item_name, el_item_icon, el_item_name_editor);
|
||||||
uid: options.uid === 'null' ? null : options.uid,
|
|
||||||
new_name: new_name,
|
|
||||||
excludeSocketID: window.socket.id,
|
|
||||||
success: async (fsentry)=>{
|
|
||||||
// Has the extension changed? in that case update options.sugggested_apps
|
|
||||||
const old_extension = path.extname(old_name);
|
|
||||||
const new_extension = path.extname(new_name);
|
|
||||||
if(old_extension !== new_extension){
|
|
||||||
suggest_apps_for_fsentry({
|
|
||||||
uid: options.uid,
|
|
||||||
onSuccess: function(suggested_apps){
|
|
||||||
options.suggested_apps = suggested_apps;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set new item name
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).html(html_encode(truncate_filename(new_name, TRUNCATE_LENGTH)).replaceAll(' ', ' '));
|
|
||||||
$(el_item_name).show();
|
|
||||||
|
|
||||||
// Hide item name editor
|
|
||||||
$(el_item_name_editor).hide();
|
|
||||||
|
|
||||||
// Set new icon
|
|
||||||
const new_icon = (options.is_dir ? window.icons['folder.svg'] : (await item_icon(fsentry)).image);
|
|
||||||
$(el_item_icon).find('.item-icon-icon').attr('src', new_icon);
|
|
||||||
|
|
||||||
// Set new data-name
|
|
||||||
options.name = new_name;
|
|
||||||
$(el_item).attr('data-name', html_encode(new_name));
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-name', html_encode(new_name));
|
|
||||||
$(`.window-${options.uid}`).attr('data-name', html_encode(new_name));
|
|
||||||
|
|
||||||
// Set new title attribute
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('title', html_encode(new_name));
|
|
||||||
$(`.window-${options.uid}`).attr('title', html_encode(new_name));
|
|
||||||
|
|
||||||
// Set new value for item-name-editor
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name-editor`).val(html_encode(new_name));
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).attr('title', html_encode(new_name));
|
|
||||||
|
|
||||||
// Set new data-path
|
|
||||||
options.path = path.join( path.dirname(options.path), options.name);
|
|
||||||
const new_path = options.path;
|
|
||||||
$(el_item).attr('data-path', new_path);
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-path', new_path);
|
|
||||||
$(`.window-${options.uid}`).attr('data-path', new_path);
|
|
||||||
|
|
||||||
// Update all elements that have matching paths
|
|
||||||
$(`[data-path="${html_encode(old_path)}" i]`).each(function(){
|
|
||||||
$(this).attr('data-path', new_path)
|
|
||||||
if($(this).hasClass('window-navbar-path-dirname'))
|
|
||||||
$(this).text(new_name);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update the paths of all elements whose paths start with old_path
|
|
||||||
$(`[data-path^="${html_encode(old_path) + '/'}"]`).each(function(){
|
|
||||||
const new_el_path = _.replace($(this).attr('data-path'), old_path + '/', new_path+'/');
|
|
||||||
$(this).attr('data-path', new_el_path);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update the 'Sites Cache'
|
|
||||||
if($(el_item).attr('data-has_website') === '1')
|
|
||||||
await update_sites_cache();
|
|
||||||
|
|
||||||
// Update website_url
|
|
||||||
website_url = determine_website_url(new_path);
|
|
||||||
$(el_item).attr('data-website_url', website_url);
|
|
||||||
|
|
||||||
// Update all exact-matching windows
|
|
||||||
$(`.window-${options.uid}`).each(function(){
|
|
||||||
update_window_path(this, options.path);
|
|
||||||
})
|
|
||||||
|
|
||||||
// Set new name for corresponding open windows
|
|
||||||
$(`.window-${options.uid} .window-head-title`).text(new_name);
|
|
||||||
|
|
||||||
// Re-sort all matching item containers
|
|
||||||
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).parent('.item-container').each(function(){
|
|
||||||
sort_items(this, $(el_item).closest('.item-container').attr('data-sort_by'), $(el_item).closest('.item-container').attr('data-sort_order'));
|
|
||||||
})
|
|
||||||
},
|
|
||||||
error: function (err){
|
|
||||||
// reset to old name
|
|
||||||
$(el_item_name).text(truncate_filename(options.name, TRUNCATE_LENGTH));
|
|
||||||
$(el_item_name).show();
|
|
||||||
|
|
||||||
// hide item name editor
|
|
||||||
$(el_item_name_editor).hide();
|
|
||||||
$(el_item_name_editor).val(html_encode($(el_item).attr('data-name')));
|
|
||||||
|
|
||||||
//show error
|
|
||||||
if(err.message){
|
|
||||||
UIAlert(err.message)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
|
111
src/helpers.js
111
src/helpers.js
@ -3326,6 +3326,114 @@ window.unzipItem = async function(itemPath) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.rename_file = async(options, new_name, old_name, el_item, el_item_name, el_item_icon, el_item_name_editor, is_undo = false)=>{
|
||||||
|
puter.fs.rename({
|
||||||
|
uid: options.uid === 'null' ? null : options.uid,
|
||||||
|
new_name: new_name,
|
||||||
|
excludeSocketID: window.socket.id,
|
||||||
|
success: async (fsentry)=>{
|
||||||
|
// Add action to actions_history for undo ability
|
||||||
|
if (!is_undo)
|
||||||
|
actions_history.push({
|
||||||
|
operation: 'rename',
|
||||||
|
data: {options, new_name, old_name, el_item, el_item_name, el_item_icon, el_item_name_editor}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Has the extension changed? in that case update options.sugggested_apps
|
||||||
|
const old_extension = path.extname(old_name);
|
||||||
|
const new_extension = path.extname(new_name);
|
||||||
|
if(old_extension !== new_extension){
|
||||||
|
suggest_apps_for_fsentry({
|
||||||
|
uid: options.uid,
|
||||||
|
onSuccess: function(suggested_apps){
|
||||||
|
options.suggested_apps = suggested_apps;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set new item name
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).html(html_encode(truncate_filename(new_name, TRUNCATE_LENGTH)).replaceAll(' ', ' '));
|
||||||
|
$(el_item_name).show();
|
||||||
|
|
||||||
|
// Hide item name editor
|
||||||
|
$(el_item_name_editor).hide();
|
||||||
|
|
||||||
|
// Set new icon
|
||||||
|
const new_icon = (options.is_dir ? window.icons['folder.svg'] : (await item_icon(fsentry)).image);
|
||||||
|
$(el_item_icon).find('.item-icon-icon').attr('src', new_icon);
|
||||||
|
|
||||||
|
// Set new data-name
|
||||||
|
options.name = new_name;
|
||||||
|
$(el_item).attr('data-name', html_encode(new_name));
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-name', html_encode(new_name));
|
||||||
|
$(`.window-${options.uid}`).attr('data-name', html_encode(new_name));
|
||||||
|
|
||||||
|
// Set new title attribute
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('title', html_encode(new_name));
|
||||||
|
$(`.window-${options.uid}`).attr('title', html_encode(new_name));
|
||||||
|
|
||||||
|
// Set new value for item-name-editor
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name-editor`).val(html_encode(new_name));
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).attr('title', html_encode(new_name));
|
||||||
|
|
||||||
|
// Set new data-path
|
||||||
|
options.path = path.join( path.dirname(options.path), options.name);
|
||||||
|
const new_path = options.path;
|
||||||
|
$(el_item).attr('data-path', new_path);
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-path', new_path);
|
||||||
|
$(`.window-${options.uid}`).attr('data-path', new_path);
|
||||||
|
|
||||||
|
// Update all elements that have matching paths
|
||||||
|
$(`[data-path="${html_encode(old_path)}" i]`).each(function(){
|
||||||
|
$(this).attr('data-path', new_path)
|
||||||
|
if($(this).hasClass('window-navbar-path-dirname'))
|
||||||
|
$(this).text(new_name);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update the paths of all elements whose paths start with old_path
|
||||||
|
$(`[data-path^="${html_encode(old_path) + '/'}"]`).each(function(){
|
||||||
|
const new_el_path = _.replace($(this).attr('data-path'), old_path + '/', new_path+'/');
|
||||||
|
$(this).attr('data-path', new_el_path);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update the 'Sites Cache'
|
||||||
|
if($(el_item).attr('data-has_website') === '1')
|
||||||
|
await update_sites_cache();
|
||||||
|
|
||||||
|
// Update website_url
|
||||||
|
website_url = determine_website_url(new_path);
|
||||||
|
$(el_item).attr('data-website_url', website_url);
|
||||||
|
|
||||||
|
// Update all exact-matching windows
|
||||||
|
$(`.window-${options.uid}`).each(function(){
|
||||||
|
update_window_path(this, options.path);
|
||||||
|
})
|
||||||
|
|
||||||
|
// Set new name for corresponding open windows
|
||||||
|
$(`.window-${options.uid} .window-head-title`).text(new_name);
|
||||||
|
|
||||||
|
// Re-sort all matching item containers
|
||||||
|
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).parent('.item-container').each(function(){
|
||||||
|
sort_items(this, $(el_item).closest('.item-container').attr('data-sort_by'), $(el_item).closest('.item-container').attr('data-sort_order'));
|
||||||
|
})
|
||||||
|
},
|
||||||
|
error: function (err){
|
||||||
|
// reset to old name
|
||||||
|
$(el_item_name).text(truncate_filename(options.name, TRUNCATE_LENGTH));
|
||||||
|
$(el_item_name).show();
|
||||||
|
|
||||||
|
// hide item name editor
|
||||||
|
$(el_item_name_editor).hide();
|
||||||
|
$(el_item_name_editor).val(html_encode($(el_item).attr('data-name')));
|
||||||
|
|
||||||
|
//show error
|
||||||
|
if(err.message){
|
||||||
|
UIAlert(err.message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
window.undo_last_action = async()=>{
|
window.undo_last_action = async()=>{
|
||||||
if (actions_history.length > 0) {
|
if (actions_history.length > 0) {
|
||||||
const last_action = actions_history.pop();
|
const last_action = actions_history.pop();
|
||||||
@ -3334,6 +3442,9 @@ window.undo_last_action = async()=>{
|
|||||||
if (last_action.operation === 'create_file' || last_action.operation === 'create_folder') {
|
if (last_action.operation === 'create_file' || last_action.operation === 'create_folder') {
|
||||||
const lastCreatedItem = last_action.data;
|
const lastCreatedItem = last_action.data;
|
||||||
undo_create_file_or_folder(lastCreatedItem);
|
undo_create_file_or_folder(lastCreatedItem);
|
||||||
|
} else if(last_action.operation === 'rename') {
|
||||||
|
const {options, new_name, old_name, el_item, el_item_name, el_item_icon, el_item_name_editor} = last_action.data;
|
||||||
|
rename_file(options, old_name, new_name, el_item, el_item_name, el_item_icon, el_item_name_editor, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user