Skip to content

Commit

Permalink
Focus title when ciopying #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kzhovn committed May 20, 2022
1 parent 2115ba2 commit 6cb0cdd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/node": "^14.14.37",
"obsidian": "^0.12.0",
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
"@rollup/plugin-typescript": "^8.3.2",
"@types/node": "^14.18.18",
"obsidian": "^0.12.17",
"rollup": "^2.74.1",
"tslib": "^2.4.0",
"typescript": "^4.6.4"
}
}
19 changes: 11 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export default class CopyPlugin extends Plugin {
menu.addItem((item) => { item
.setTitle("Make a copy")
.setIcon(FILE_COPY_ICON)
.onClick(() => {
.onClick(() => {
this.copyFile(abstractFile, abstractFile.parent);
});
});
} else if (abstractFile instanceof TFolder && source === "file-explorer-context-menu") {
menu.addItem((item) => { item
.setTitle("Copy folder")
.setIcon(FOLDER_COPY_ICON)
.onClick(() => {
.onClick(() => {
this.copyFolder(abstractFile, abstractFile.parent);
});
});
Expand All @@ -50,25 +50,28 @@ export default class CopyPlugin extends Plugin {
}

//copy a file to newFolder with name "Old Name 1"
async copyFile(originalFile: TFile, newFileLocation: TFolder, nameSuffix = " 1", openFile = true) {
let newFileLocationPath = newFileLocation.path;
async copyFile(originalFile: TFile, newFileLocation: TFolder, nameSuffix = " 1", openFile = true) {
let newFileLocationPath = newFileLocation.path;

const newFilePath = newFileLocationPath + "/" + originalFile.basename + nameSuffix + "." + originalFile.extension;
const newFile = await this.app.vault.copy(originalFile, newFilePath);

if (openFile === true) {
if (openFile === true) {
this.app.workspace.getLeaf().openFile(newFile);
const name = 'view-header-title';
//@ts-ignore
document.getElementsByClassName(name)[0].focus();
}
}

//recursively copy a folder with name "Old Name 1" to newFolder, without renaming the contents
async copyFolder(originalFolder: TFolder, newFolderLocation: TFolder, nameSuffix = " 1") {
let newFolderPath;

if (newFolderLocation.path === "/") {
newFolderPath = originalFolder.name + nameSuffix;
} else {
newFolderPath = newFolderLocation.path + "/" + originalFolder.name + nameSuffix;
newFolderPath = newFolderLocation.path + "/" + originalFolder.name + nameSuffix;
}

await this.app.vault.createFolder(newFolderPath);
Expand Down

0 comments on commit 6cb0cdd

Please sign in to comment.