add ability to move files from the root dir
This commit is contained in:
parent
7676973bf0
commit
54696309f6
35
main.ts
35
main.ts
|
@ -20,8 +20,13 @@ import { renderPreviewFiles } from './src/components/RenderPreviewFiles';
|
|||
import { createBackslash } from './src/components/RegExpBackslash';
|
||||
import { RegExpFlag } from './src/constants/RegExpFlags';
|
||||
import { RegExpFlagsSuggest } from './src/suggestions/RegExpFlagsSuggest';
|
||||
import {
|
||||
isViewTypeRegExp,
|
||||
isViewTypeFolder,
|
||||
isViewTypeTags,
|
||||
} from './src/services/settings.service';
|
||||
|
||||
interface BulkRenamePluginSettings {
|
||||
export interface BulkRenamePluginSettings {
|
||||
folderName: string;
|
||||
fileNames: TFile[];
|
||||
existingSymbol: string;
|
||||
|
@ -47,18 +52,6 @@ const DEFAULT_SETTINGS: BulkRenamePluginSettings = {
|
|||
viewType: 'folder',
|
||||
};
|
||||
|
||||
const isViewTypeFolder = ({ settings }: BulkRenamePlugin) => {
|
||||
return settings.viewType === 'folder';
|
||||
};
|
||||
|
||||
const isViewTypeTags = ({ settings }: BulkRenamePlugin) => {
|
||||
return settings.viewType === 'tags';
|
||||
};
|
||||
|
||||
const isViewTypeRegExp = ({ settings }: BulkRenamePlugin) => {
|
||||
return settings.viewType === 'regexp';
|
||||
};
|
||||
|
||||
class BulkRenamePlugin extends Plugin {
|
||||
settings: BulkRenamePluginSettings;
|
||||
|
||||
|
@ -126,7 +119,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
.setName('UI will be changed when you click those buttons')
|
||||
.addButton((button) => {
|
||||
button.setButtonText('Search by folder');
|
||||
if (isViewTypeFolder(this.plugin)) {
|
||||
if (isViewTypeFolder(this.plugin.settings)) {
|
||||
button.setCta();
|
||||
}
|
||||
button.onClick(async () => {
|
||||
|
@ -137,7 +130,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
})
|
||||
.addButton((button) => {
|
||||
button.setButtonText('Search By Tags');
|
||||
if (isViewTypeTags(this.plugin)) {
|
||||
if (isViewTypeTags(this.plugin.settings)) {
|
||||
button.setCta();
|
||||
}
|
||||
button.onClick(async () => {
|
||||
|
@ -148,7 +141,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
})
|
||||
.addButton((button) => {
|
||||
button.setButtonText('Search by RegExp');
|
||||
if (isViewTypeRegExp(this.plugin)) {
|
||||
if (isViewTypeRegExp(this.plugin.settings)) {
|
||||
button.setCta();
|
||||
}
|
||||
button.onClick(async () => {
|
||||
|
@ -160,7 +153,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
}
|
||||
|
||||
renderFileLocation() {
|
||||
if (!isViewTypeFolder(this.plugin)) {
|
||||
if (!isViewTypeFolder(this.plugin.settings)) {
|
||||
return;
|
||||
}
|
||||
new Setting(this.containerEl)
|
||||
|
@ -183,7 +176,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
}
|
||||
|
||||
renderTagNames() {
|
||||
if (!isViewTypeTags(this.plugin)) {
|
||||
if (!isViewTypeTags(this.plugin.settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -215,7 +208,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
}
|
||||
|
||||
renderRegExpInput() {
|
||||
if (!isViewTypeRegExp(this.plugin)) {
|
||||
if (!isViewTypeRegExp(this.plugin.settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -384,12 +377,12 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
|||
};
|
||||
|
||||
calculateFileNames() {
|
||||
if (isViewTypeTags(this.plugin)) {
|
||||
if (isViewTypeTags(this.plugin.settings)) {
|
||||
this.getFilesByTags();
|
||||
return;
|
||||
}
|
||||
|
||||
if (isViewTypeRegExp(this.plugin)) {
|
||||
if (isViewTypeRegExp(this.plugin.settings)) {
|
||||
this.getFilesByRegExp();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
export const ROOT_FOLDER_NAME = '/';
|
|
@ -1,20 +1,27 @@
|
|||
import { App, Notice, TFile } from 'obsidian';
|
||||
import BulkRenamePlugin from '../../main';
|
||||
import BulkRenamePlugin, { BulkRenamePluginSettings } from '../../main';
|
||||
import { isViewTypeFolder } from './settings.service';
|
||||
import { ROOT_FOLDER_NAME } from '../constants/folders';
|
||||
|
||||
export const getFilesNamesInDirectory = (plugin: BulkRenamePlugin) => {
|
||||
const { fileNames } = plugin.settings;
|
||||
|
||||
return getFilesAsString(fileNames);
|
||||
return getFilesAsString(plugin.settings);
|
||||
};
|
||||
|
||||
const getFilesAsString = (fileNames: TFile[]) => {
|
||||
const getFilesAsString = (settings: BulkRenamePluginSettings) => {
|
||||
let value = '';
|
||||
const { fileNames, folderName } = settings;
|
||||
const shouldPrependSlash =
|
||||
isViewTypeFolder(settings) && folderName === ROOT_FOLDER_NAME;
|
||||
|
||||
fileNames.forEach((fileName, index) => {
|
||||
const isLast = index + 1 === fileNames.length;
|
||||
const filePath = shouldPrependSlash ? '/' + fileName.path : fileName.path;
|
||||
|
||||
if (isLast) {
|
||||
return (value += fileName.path);
|
||||
return (value += filePath);
|
||||
}
|
||||
value += fileName.path + '\r\n';
|
||||
|
||||
value += filePath + '\r\n';
|
||||
});
|
||||
|
||||
return value;
|
||||
|
@ -23,7 +30,10 @@ const getFilesAsString = (fileNames: TFile[]) => {
|
|||
export const getRenderedFileNamesReplaced = (plugin: BulkRenamePlugin) => {
|
||||
const newFiles = selectFilenamesWithReplacedPath(plugin);
|
||||
|
||||
return getFilesAsString(newFiles);
|
||||
return getFilesAsString({
|
||||
...plugin.settings,
|
||||
fileNames: newFiles,
|
||||
});
|
||||
};
|
||||
|
||||
export const selectFilenamesWithReplacedPath = (plugin: BulkRenamePlugin) => {
|
||||
|
@ -38,14 +48,16 @@ export const selectFilenamesWithReplacedPath = (plugin: BulkRenamePlugin) => {
|
|||
};
|
||||
|
||||
export const replaceFilePath = (plugin: BulkRenamePlugin, file: TFile) => {
|
||||
const pathWithoutExtension = file.path.split('.').slice(0, -1).join('.');
|
||||
const { replacePattern, existingSymbol } = plugin.settings;
|
||||
|
||||
const pathWithoutExtension = file.path.split('.').slice(0, -1).join('.');
|
||||
if (isRootFilesSelected(plugin)) {
|
||||
const newPath = replacePattern + pathWithoutExtension;
|
||||
return `${newPath}.${file.extension}`;
|
||||
}
|
||||
|
||||
const convertedToRegExpString = escapeRegExp(existingSymbol);
|
||||
|
||||
const regExpSymbol = new RegExp(convertedToRegExpString, 'g');
|
||||
|
||||
const newPath = pathWithoutExtension?.replace(regExpSymbol, replacePattern);
|
||||
|
||||
return `${newPath}.${file.extension}`;
|
||||
|
@ -68,13 +80,26 @@ export const renameFilesInObsidian = async (
|
|||
}
|
||||
|
||||
new Notice('renaming has been started');
|
||||
let success = true;
|
||||
for (const fileName of fileNames) {
|
||||
try {
|
||||
await app.fileManager.renameFile(
|
||||
fileName,
|
||||
replaceFilePath(plugin, fileName),
|
||||
);
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
new Notice('FILES NOT RENAMED!');
|
||||
new Notice(
|
||||
'WARNING: YOU MUST CREATE FOLDER BEFORE MOVING INTO IT',
|
||||
7000,
|
||||
);
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
new Notice('successfully renamed all files');
|
||||
}
|
||||
}
|
||||
success && new Notice('successfully renamed all files');
|
||||
};
|
||||
|
||||
let reRegExpChar = /[\\^$.*+?()[\]{}]/g,
|
||||
|
@ -83,3 +108,13 @@ let reRegExpChar = /[\\^$.*+?()[\]{}]/g,
|
|||
export function escapeRegExp(s: string) {
|
||||
return s && reHasRegExpChar.test(s) ? s.replace(reRegExpChar, '\\$&') : s;
|
||||
}
|
||||
|
||||
const isRootFilesSelected = (plugin: BulkRenamePlugin) => {
|
||||
const { existingSymbol, folderName } = plugin.settings;
|
||||
|
||||
return (
|
||||
existingSymbol === '/' &&
|
||||
folderName === '/' &&
|
||||
isViewTypeFolder(plugin.settings)
|
||||
);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import BulkRenamePlugin from '../../main';
|
||||
|
||||
export const isViewTypeFolder = (settings: BulkRenamePlugin['settings']) => {
|
||||
return settings.viewType === 'folder';
|
||||
};
|
||||
|
||||
export const isViewTypeTags = (settings: BulkRenamePlugin['settings']) => {
|
||||
return settings.viewType === 'tags';
|
||||
};
|
||||
|
||||
export const isViewTypeRegExp = (settings: BulkRenamePlugin['settings']) => {
|
||||
return settings.viewType === 'regexp';
|
||||
};
|
Loading…
Reference in New Issue