Skip to content

Commit

Permalink
Merge pull request #1145 from DDMAL/trash-bin
Browse files Browse the repository at this point in the history
Fix automatic rename when filename exists
  • Loading branch information
yinanazhou authored Nov 28, 2023
2 parents 779d0c6 + 7eb11da commit 5304896
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
20 changes: 17 additions & 3 deletions src/Dashboard/Dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IEntry, IFile, IFolder, FileSystemTools } from './FileSystem';
import { deleteDocument, updateDocName } from './Storage';
import { formatFilename } from './UploadTools';
import { FileSystemManager } from './FileSystem';
import { ShiftSelectionManager, dashboardState } from './DashboardTools';
import { InitUploadArea } from './UploadArea';
Expand Down Expand Up @@ -170,7 +169,7 @@ function createTile(entry: IEntry) {
const icon = document.createElement('img');
icon.classList.add('document-icon');
const name = document.createElement('div');
name.innerText = formatFilename(entry.name, 25);
name.innerText = entry.name;

switch (entry.type) {
case 'folder':
Expand Down Expand Up @@ -347,6 +346,12 @@ function putBackDocsHandler() {
const targetFolder = state.getFolderPathByNames(folderPathNames);
if (targetFolder) {
entry = FileSystemTools.removeMetadata(entry, ['removed_on', 'recover_folder']);
const dateTimePattern = / - \d{1,2}\/\d{1,2}\/\d{4}, \d{1,2}:\d{2}:\d{2} [APMapm]{2}$/;

// Check if the filename ends with the date time pattern
if (dateTimePattern.test(entry.name)) {
entry.name = entry.name.replace(dateTimePattern, '');
}
moveToFolder([entry], parentFolder, targetFolder);
}
}
Expand Down Expand Up @@ -743,7 +748,7 @@ export async function updateDashboard(newPath?: IFolder[]): Promise<void> {
const tile = document.getElementById(entry.id);
addDragStartListener(tile);

if (entry.type === 'folder') {
if (entry.type === 'folder' || entry.type === 'trash') {
addDropTargetListeners(tile, currentFolder, entry as IFolder);
}
});
Expand Down Expand Up @@ -804,6 +809,10 @@ function createHandleDrop(currentFolder: IFolder, destinationFolder: IFolder) {
function moveToFolder(entries: IEntry[], parentFolder: IFolder, newFolder: IFolder) {
const errorMessages = [];
entries.forEach((entry) => {
// Handle name conflicts for trash folder
if (newFolder.type === 'trash' && newFolder.children.some((e) => e.name === entry.name)) {
entry.name = trashFNConflictHandler(entry.name);
}
const response = FileSystemTools.canMoveEntry(entry, parentFolder, newFolder);
if (!response.succeeded) errorMessages.push(response.error);
else FileSystemTools.moveEntry(entry, parentFolder, newFolder);
Expand All @@ -815,6 +824,11 @@ function moveToFolder(entries: IEntry[], parentFolder: IFolder, newFolder: IFold
updateDashboard();
}

function trashFNConflictHandler(filename: string): string {
const datetime = new Date().toLocaleString();
return filename + ' - ' + datetime;
}

/**
* Opens Move-To menu modal window with UI for moving selected entries to a new folder.
*/
Expand Down
49 changes: 12 additions & 37 deletions src/Dashboard/UploadTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function createUnpairedItem(filename: string, group: string): HTMLDivElement {
label.setAttribute('for', id);

const text = document.createElement('span');
text.innerText = formatFilename(filename, 28);
text.innerText = formatFilename(filename, 50);
text.setAttribute('style', 'margin-top: auto');

const delBtn = document.createElement('img');
Expand Down Expand Up @@ -110,7 +110,7 @@ function createPairedFolio(filename: string, mei_filename: string, image_filenam

const folio_filename = document.createElement('div');
folio_filename.classList.add('folio-filename');
folio_filename.innerHTML = formatFilename(filename, 25);
folio_filename.innerHTML = formatFilename(filename, 50);
folio.appendChild(folio_filename);

function handleUnpair() {
Expand Down Expand Up @@ -236,6 +236,7 @@ export async function sortFileByName(sortByNameBtn: Element): Promise<void> {
// });
// }

// Limit filename length
export function formatFilename(filename: string, maxLen: number): string {
const chunkLen = Math.floor(maxLen/2);
const len = filename.length;
Expand All @@ -244,39 +245,13 @@ export function formatFilename(filename: string, maxLen: number): string {
}

// Renames file if there are naming conflicts, in the form of 'foobar (1)'
export function fnConflictHandler(filename: string, comparisons: string[]): string {
const reg = new RegExp(filename);
const results = comparisons.filter((comparison: string) => reg.test(comparison));

if (results.length !== 0) {
// Find lowest digit to name
const digitsReg = /\(\d+\),/g;
const soup = results.join().concat(',');
const digitsResults = soup.match(digitsReg);

// If no digit matches then go to else statement
if (digitsResults !== null) {
const digits: number[] = digitsResults.map(str => {
const stripped = str.substring(1, str.length-2);
return Number(stripped);
});
digits.sort();

const idx = digits.indexOf(1);
if (idx === -1) return `${filename}(1)`;
else {
let prev = 1;
for (let i = idx+1 ; i < digits.length ; i++) {
const cur = digits[i];
if (cur !== prev + 1) {
break;
}
prev += 1;
}
return `${filename}(${prev+1})`;
}
}
else return `${filename}(1)`;
}
else return filename;
export function fnConflictHandler(filename: string, existingNames: string[]): string {
let newFilename = filename;
let counter = 1;

while (existingNames.includes(newFilename)) {
newFilename = `${filename}(${counter})`;
counter++;
}
return newFilename;
}

0 comments on commit 5304896

Please sign in to comment.