Skip to content

Commit 251102f

Browse files
committed
improve logic when opening multiple files
closes #2582
1 parent 2511021 commit 251102f

File tree

2 files changed

+65
-71
lines changed

2 files changed

+65
-71
lines changed

src/renderer/src/App.tsx

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,29 +1707,27 @@ function App() {
17071707
});
17081708
}, []);
17091709

1710-
const userOpenFiles = useCallback(async (filePathsIn?: string[]) => {
1710+
const userOpenFiles = useCallback(async (newFilePathsIn?: string[]) => {
17111711
await withErrorHandling(async () => {
1712-
let filePaths = filePathsIn;
1713-
if (!filePaths || filePaths.length === 0) return;
1712+
let newFilePaths = newFilePathsIn;
1713+
if (!newFilePaths || newFilePaths.length === 0) return;
17141714

17151715
console.log('userOpenFiles');
1716-
console.log(filePaths.join('\n'));
1716+
console.log(newFilePaths.join('\n'));
17171717

1718-
lastOpenedPathRef.current = filePaths[0]!;
1718+
lastOpenedPathRef.current = newFilePaths[0]!;
17191719

1720-
let firstFilePath = filePaths[0]!;
1720+
let firstNewFilePath = newFilePaths[0]!;
17211721

17221722
// first check if it is a single directory, and if so, read it recursively
1723-
if (filePaths.length === 1 && (await lstat(firstFilePath)).isDirectory()) {
1723+
if (newFilePaths.length === 1 && (await lstat(firstNewFilePath)).isDirectory()) {
17241724
console.log('Reading directory...');
1725-
invariant(firstFilePath != null);
1726-
filePaths = await readDirRecursively(firstFilePath);
1725+
invariant(firstNewFilePath != null);
1726+
newFilePaths = await readDirRecursively(firstNewFilePath);
17271727
}
17281728

17291729
// Only allow opening regular files
1730-
// eslint-disable-next-line no-restricted-syntax
1731-
for (const path of filePaths) {
1732-
// eslint-disable-next-line no-await-in-loop
1730+
for (const path of newFilePaths) {
17331731
const fileStat = await lstat(path);
17341732

17351733
if (!fileStat.isFile()) {
@@ -1739,98 +1737,93 @@ function App() {
17391737
}
17401738
}
17411739

1742-
if (filePaths.length > 1) {
1743-
if (alwaysConcatMultipleFiles) {
1744-
batchLoadPaths(filePaths);
1745-
setConcatSheetOpen(true);
1746-
} else {
1747-
batchLoadPaths(filePaths, true);
1748-
}
1740+
if (newFilePaths.length > 1 && alwaysConcatMultipleFiles) {
1741+
batchLoadPaths(newFilePaths);
1742+
setConcatSheetOpen(true);
17491743
return;
17501744
}
17511745

1752-
// filePaths.length is now 1
1753-
firstFilePath = filePaths[0]!;
1754-
invariant(firstFilePath != null);
1746+
firstNewFilePath = newFilePaths[0]!;
1747+
invariant(firstNewFilePath != null);
17551748

17561749
// https://en.wikibooks.org/wiki/Inside_DVD-Video/Directory_Structure
1757-
if (/^video_ts$/i.test(basename(firstFilePath))) {
1750+
if (newFilePaths.length === 1 && /^video_ts$/i.test(basename(firstNewFilePath))) {
17581751
if (mustDisallowVob()) return;
1759-
filePaths = await readVideoTs(firstFilePath);
1752+
newFilePaths = await readVideoTs(firstNewFilePath);
17601753
}
17611754

17621755
if (workingRef.current) return;
17631756
try {
17641757
setWorking({ text: i18n.t('Loading file') });
17651758

1766-
// Import segments for for already opened file
1767-
const matchingImportProjectType = getImportProjectType(firstFilePath);
1759+
// If it's a project file (not llc) and we have an already opened file, import segments from the project
1760+
const matchingImportProjectType = getImportProjectType(firstNewFilePath);
17681761
if (matchingImportProjectType) {
17691762
if (!checkFileOpened()) return;
1770-
await loadEdlFile({ path: firstFilePath, type: matchingImportProjectType, append: true });
1763+
await loadEdlFile({ path: firstNewFilePath, type: matchingImportProjectType, append: true });
17711764
return;
17721765
}
17731766

1774-
const filePathLowerCase = firstFilePath.toLowerCase();
1767+
const filePathLowerCase = firstNewFilePath.toLowerCase();
17751768
const isLlcProject = filePathLowerCase.endsWith('.llc');
17761769

17771770
// Need to ask the user what to do if more than one option
1778-
const inputOptions: { open: string, project?: string, tracks?: string, subtitles?: string, addToBatch?: string, mergeWithCurrentFile?: string } = {
1779-
open: isFileOpened ? i18n.t('Open the file instead of the current one') : i18n.t('Open the file'),
1780-
};
1771+
const inputOptions: { open?: string, project?: string, tracks?: string, subtitles?: string, addToBatch?: string, mergeWithCurrentFile?: string } = {};
17811772

1782-
if (isFileOpened) {
1773+
if (newFilePaths.length === 1) {
1774+
inputOptions.open = isFileOpened ? i18n.t('Open the file instead of the current one') : i18n.t('Open the file');
1775+
}
1776+
1777+
if (isFileOpened && newFilePaths.length === 1) {
17831778
if (isLlcProject) inputOptions.project = i18n.t('Load segments from the new file, but keep the current media');
1784-
if (filePathLowerCase.endsWith('.srt')) inputOptions.subtitles = i18n.t('Convert subtitiles into segments');
1779+
else if (filePathLowerCase.endsWith('.srt')) inputOptions.subtitles = i18n.t('Convert subtitiles into segments');
17851780
inputOptions.tracks = i18n.t('Include all tracks from the new file');
17861781
}
17871782

1788-
if (batchFiles.length > 0) inputOptions.addToBatch = i18n.t('Add the file to the batch list');
1789-
else if (isFileOpened) inputOptions.mergeWithCurrentFile = i18n.t('Merge/concatenate with current file');
1783+
if (isFileOpened) inputOptions.mergeWithCurrentFile = i18n.t('Merge/concatenate with current file');
1784+
if (batchFiles.length > 0 || newFilePaths.length > 1) inputOptions.addToBatch = i18n.t('Add the file to the batch list');
17901785

1791-
if (Object.keys(inputOptions).length > 1) {
1792-
const openFileResponse = enableAskForFileOpenAction ? await askForFileOpenAction(inputOptions) : 'open';
1786+
const inputOptionsKeys = Object.keys(inputOptions);
17931787

1794-
if (openFileResponse === 'open') {
1795-
await userOpenSingleFile({ path: firstFilePath, isLlcProject });
1796-
return;
1797-
}
1798-
if (openFileResponse === 'project') {
1799-
await loadEdlFile({ path: firstFilePath, type: 'llc' });
1800-
return;
1801-
}
1802-
if (openFileResponse === 'subtitles') {
1803-
await loadEdlFile({ path: firstFilePath, type: 'srt' });
1804-
return;
1805-
}
1806-
if (openFileResponse === 'tracks') {
1807-
await addStreamSourceFile(firstFilePath);
1808-
setStreamsSelectorShown(true);
1809-
return;
1810-
}
1811-
if (openFileResponse === 'addToBatch') {
1812-
batchLoadPaths([firstFilePath], true);
1813-
return;
1814-
}
1815-
if (openFileResponse === 'mergeWithCurrentFile') {
1816-
const batchPaths = new Set<string>();
1817-
if (filePath) batchPaths.add(filePath);
1818-
filePaths.forEach((path) => batchPaths.add(path));
1819-
batchLoadPaths([...batchPaths]);
1820-
if (batchPaths.size > 1) setConcatSheetOpen(true);
1821-
return;
1822-
}
1788+
let openFileResponse: string | undefined;
1789+
if (inputOptionsKeys.length === 1) [openFileResponse] = inputOptionsKeys;
1790+
if (enableAskForFileOpenAction && inputOptionsKeys.length > 1) openFileResponse = await askForFileOpenAction(inputOptions);
1791+
else if (newFilePaths.length === 1) openFileResponse = 'open';
18231792

1824-
// Dialog canceled:
1793+
if (openFileResponse === 'open') {
1794+
await userOpenSingleFile({ path: firstNewFilePath, isLlcProject });
18251795
return;
18261796
}
1827-
1828-
await userOpenSingleFile({ path: firstFilePath, isLlcProject });
1797+
if (openFileResponse === 'project') {
1798+
await loadEdlFile({ path: firstNewFilePath, type: 'llc' });
1799+
return;
1800+
}
1801+
if (openFileResponse === 'subtitles') {
1802+
await loadEdlFile({ path: firstNewFilePath, type: 'srt' });
1803+
return;
1804+
}
1805+
if (openFileResponse === 'tracks') {
1806+
await addStreamSourceFile(firstNewFilePath);
1807+
setStreamsSelectorShown(true);
1808+
return;
1809+
}
1810+
if (openFileResponse === 'addToBatch') {
1811+
batchLoadPaths(newFilePaths, true);
1812+
return;
1813+
}
1814+
if (openFileResponse === 'mergeWithCurrentFile') {
1815+
const batchPaths = new Set<string>();
1816+
if (filePath) batchPaths.add(filePath);
1817+
newFilePaths.forEach((path) => batchPaths.add(path));
1818+
batchLoadPaths([...batchPaths]);
1819+
if (batchPaths.size > 1) setConcatSheetOpen(true);
1820+
}
1821+
// else: no match means dialog canceled or nothing useful to do:
18291822
} finally {
18301823
setWorking(undefined);
18311824
}
18321825
}, i18n.t('Failed to open file'));
1833-
}, [withErrorHandling, workingRef, alwaysConcatMultipleFiles, batchLoadPaths, setWorking, isFileOpened, batchFiles.length, userOpenSingleFile, checkFileOpened, loadEdlFile, enableAskForFileOpenAction, addStreamSourceFile, filePath]);
1826+
}, [withErrorHandling, alwaysConcatMultipleFiles, workingRef, batchLoadPaths, setWorking, isFileOpened, batchFiles.length, enableAskForFileOpenAction, checkFileOpened, loadEdlFile, userOpenSingleFile, addStreamSourceFile, filePath]);
18341827

18351828
const openFilesDialog = useCallback(async () => {
18361829
// On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set `properties` to `['openFile', 'openDirectory']` on these platforms, a directory selector will be shown. #1995

src/renderer/src/dialogs/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export async function askForFfPath(defaultPath?: string | undefined) {
8282
}
8383

8484
export async function askForFileOpenAction(inputOptions: Record<string, string>) {
85-
let value;
85+
let value: string | undefined;
86+
8687
function onClick(key?: string) {
8788
value = key;
8889
getSwal().Swal.close();

0 commit comments

Comments
 (0)