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 { createBackslash } from './src/components/RegExpBackslash';
|
||||||
import { RegExpFlag } from './src/constants/RegExpFlags';
|
import { RegExpFlag } from './src/constants/RegExpFlags';
|
||||||
import { RegExpFlagsSuggest } from './src/suggestions/RegExpFlagsSuggest';
|
import { RegExpFlagsSuggest } from './src/suggestions/RegExpFlagsSuggest';
|
||||||
|
import {
|
||||||
|
isViewTypeRegExp,
|
||||||
|
isViewTypeFolder,
|
||||||
|
isViewTypeTags,
|
||||||
|
} from './src/services/settings.service';
|
||||||
|
|
||||||
interface BulkRenamePluginSettings {
|
export interface BulkRenamePluginSettings {
|
||||||
folderName: string;
|
folderName: string;
|
||||||
fileNames: TFile[];
|
fileNames: TFile[];
|
||||||
existingSymbol: string;
|
existingSymbol: string;
|
||||||
|
@ -47,18 +52,6 @@ const DEFAULT_SETTINGS: BulkRenamePluginSettings = {
|
||||||
viewType: 'folder',
|
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 {
|
class BulkRenamePlugin extends Plugin {
|
||||||
settings: BulkRenamePluginSettings;
|
settings: BulkRenamePluginSettings;
|
||||||
|
|
||||||
|
@ -126,7 +119,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
.setName('UI will be changed when you click those buttons')
|
.setName('UI will be changed when you click those buttons')
|
||||||
.addButton((button) => {
|
.addButton((button) => {
|
||||||
button.setButtonText('Search by folder');
|
button.setButtonText('Search by folder');
|
||||||
if (isViewTypeFolder(this.plugin)) {
|
if (isViewTypeFolder(this.plugin.settings)) {
|
||||||
button.setCta();
|
button.setCta();
|
||||||
}
|
}
|
||||||
button.onClick(async () => {
|
button.onClick(async () => {
|
||||||
|
@ -137,7 +130,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
})
|
})
|
||||||
.addButton((button) => {
|
.addButton((button) => {
|
||||||
button.setButtonText('Search By Tags');
|
button.setButtonText('Search By Tags');
|
||||||
if (isViewTypeTags(this.plugin)) {
|
if (isViewTypeTags(this.plugin.settings)) {
|
||||||
button.setCta();
|
button.setCta();
|
||||||
}
|
}
|
||||||
button.onClick(async () => {
|
button.onClick(async () => {
|
||||||
|
@ -148,7 +141,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
})
|
})
|
||||||
.addButton((button) => {
|
.addButton((button) => {
|
||||||
button.setButtonText('Search by RegExp');
|
button.setButtonText('Search by RegExp');
|
||||||
if (isViewTypeRegExp(this.plugin)) {
|
if (isViewTypeRegExp(this.plugin.settings)) {
|
||||||
button.setCta();
|
button.setCta();
|
||||||
}
|
}
|
||||||
button.onClick(async () => {
|
button.onClick(async () => {
|
||||||
|
@ -160,7 +153,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderFileLocation() {
|
renderFileLocation() {
|
||||||
if (!isViewTypeFolder(this.plugin)) {
|
if (!isViewTypeFolder(this.plugin.settings)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
new Setting(this.containerEl)
|
new Setting(this.containerEl)
|
||||||
|
@ -183,7 +176,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTagNames() {
|
renderTagNames() {
|
||||||
if (!isViewTypeTags(this.plugin)) {
|
if (!isViewTypeTags(this.plugin.settings)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +208,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderRegExpInput() {
|
renderRegExpInput() {
|
||||||
if (!isViewTypeRegExp(this.plugin)) {
|
if (!isViewTypeRegExp(this.plugin.settings)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,12 +377,12 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
|
||||||
};
|
};
|
||||||
|
|
||||||
calculateFileNames() {
|
calculateFileNames() {
|
||||||
if (isViewTypeTags(this.plugin)) {
|
if (isViewTypeTags(this.plugin.settings)) {
|
||||||
this.getFilesByTags();
|
this.getFilesByTags();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isViewTypeRegExp(this.plugin)) {
|
if (isViewTypeRegExp(this.plugin.settings)) {
|
||||||
this.getFilesByRegExp();
|
this.getFilesByRegExp();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
export const ROOT_FOLDER_NAME = '/';
|
|
@ -1,20 +1,27 @@
|
||||||
import { App, Notice, TFile } from 'obsidian';
|
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) => {
|
export const getFilesNamesInDirectory = (plugin: BulkRenamePlugin) => {
|
||||||
const { fileNames } = plugin.settings;
|
return getFilesAsString(plugin.settings);
|
||||||
|
|
||||||
return getFilesAsString(fileNames);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFilesAsString = (fileNames: TFile[]) => {
|
const getFilesAsString = (settings: BulkRenamePluginSettings) => {
|
||||||
let value = '';
|
let value = '';
|
||||||
|
const { fileNames, folderName } = settings;
|
||||||
|
const shouldPrependSlash =
|
||||||
|
isViewTypeFolder(settings) && folderName === ROOT_FOLDER_NAME;
|
||||||
|
|
||||||
fileNames.forEach((fileName, index) => {
|
fileNames.forEach((fileName, index) => {
|
||||||
const isLast = index + 1 === fileNames.length;
|
const isLast = index + 1 === fileNames.length;
|
||||||
|
const filePath = shouldPrependSlash ? '/' + fileName.path : fileName.path;
|
||||||
|
|
||||||
if (isLast) {
|
if (isLast) {
|
||||||
return (value += fileName.path);
|
return (value += filePath);
|
||||||
}
|
}
|
||||||
value += fileName.path + '\r\n';
|
|
||||||
|
value += filePath + '\r\n';
|
||||||
});
|
});
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
@ -23,7 +30,10 @@ const getFilesAsString = (fileNames: TFile[]) => {
|
||||||
export const getRenderedFileNamesReplaced = (plugin: BulkRenamePlugin) => {
|
export const getRenderedFileNamesReplaced = (plugin: BulkRenamePlugin) => {
|
||||||
const newFiles = selectFilenamesWithReplacedPath(plugin);
|
const newFiles = selectFilenamesWithReplacedPath(plugin);
|
||||||
|
|
||||||
return getFilesAsString(newFiles);
|
return getFilesAsString({
|
||||||
|
...plugin.settings,
|
||||||
|
fileNames: newFiles,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const selectFilenamesWithReplacedPath = (plugin: BulkRenamePlugin) => {
|
export const selectFilenamesWithReplacedPath = (plugin: BulkRenamePlugin) => {
|
||||||
|
@ -38,14 +48,16 @@ export const selectFilenamesWithReplacedPath = (plugin: BulkRenamePlugin) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const replaceFilePath = (plugin: BulkRenamePlugin, file: TFile) => {
|
export const replaceFilePath = (plugin: BulkRenamePlugin, file: TFile) => {
|
||||||
|
const pathWithoutExtension = file.path.split('.').slice(0, -1).join('.');
|
||||||
const { replacePattern, existingSymbol } = plugin.settings;
|
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 convertedToRegExpString = escapeRegExp(existingSymbol);
|
||||||
|
|
||||||
const regExpSymbol = new RegExp(convertedToRegExpString, 'g');
|
const regExpSymbol = new RegExp(convertedToRegExpString, 'g');
|
||||||
|
|
||||||
const newPath = pathWithoutExtension?.replace(regExpSymbol, replacePattern);
|
const newPath = pathWithoutExtension?.replace(regExpSymbol, replacePattern);
|
||||||
|
|
||||||
return `${newPath}.${file.extension}`;
|
return `${newPath}.${file.extension}`;
|
||||||
|
@ -68,13 +80,26 @@ export const renameFilesInObsidian = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
new Notice('renaming has been started');
|
new Notice('renaming has been started');
|
||||||
|
let success = true;
|
||||||
for (const fileName of fileNames) {
|
for (const fileName of fileNames) {
|
||||||
|
try {
|
||||||
await app.fileManager.renameFile(
|
await app.fileManager.renameFile(
|
||||||
fileName,
|
fileName,
|
||||||
replaceFilePath(plugin, 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,
|
let reRegExpChar = /[\\^$.*+?()[\]{}]/g,
|
||||||
|
@ -83,3 +108,13 @@ let reRegExpChar = /[\\^$.*+?()[\]{}]/g,
|
||||||
export function escapeRegExp(s: string) {
|
export function escapeRegExp(s: string) {
|
||||||
return s && reHasRegExpChar.test(s) ? s.replace(reRegExpChar, '\\$&') : s;
|
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