Skip to content

Commit

Permalink
fix(ui): Add Ability To move files from the root dir
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegLustenko authored Sep 24, 2022
2 parents 7676973 + ac103e3 commit af5e4d6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
35 changes: 14 additions & 21 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -160,7 +153,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
}

renderFileLocation() {
if (!isViewTypeFolder(this.plugin)) {
if (!isViewTypeFolder(this.plugin.settings)) {
return;
}
new Setting(this.containerEl)
Expand All @@ -183,7 +176,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
}

renderTagNames() {
if (!isViewTypeTags(this.plugin)) {
if (!isViewTypeTags(this.plugin.settings)) {
return;
}

Expand Down Expand Up @@ -215,7 +208,7 @@ export class BulkRenameSettingsTab extends PluginSettingTab {
}

renderRegExpInput() {
if (!isViewTypeRegExp(this.plugin)) {
if (!isViewTypeRegExp(this.plugin.settings)) {
return;
}

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/folders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ROOT_FOLDER_NAME = '/';
67 changes: 51 additions & 16 deletions src/services/file.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) => {
Expand All @@ -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}`;
Expand All @@ -68,13 +80,26 @@ export const renameFilesInObsidian = async (
}

new Notice('renaming has been started');
let success = true;
for (const fileName of fileNames) {
await app.fileManager.renameFile(
fileName,
replaceFilePath(plugin, fileName),
);
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,
Expand All @@ -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 === ROOT_FOLDER_NAME &&
folderName === ROOT_FOLDER_NAME &&
isViewTypeFolder(plugin.settings)
);
};
13 changes: 13 additions & 0 deletions src/services/settings.service.ts
Original file line number Diff line number Diff line change
@@ -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';
};

0 comments on commit af5e4d6

Please sign in to comment.