Skip to content

Commit

Permalink
feat: Per-folder settings !
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Feb 14, 2023
1 parent 9448f4f commit 99d2c50
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
tags:
- "*"
env:
PLUGIN_NAME: create-note-in-folder # Change this to match the id of your plugin.
PLUGIN_NAME: create-note-in-path # Change this to match the id of your plugin.

permissions:
contents: write
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
# Create new note in folder
# Create new note in path

This plugin add a new command to create a new note in a specific folder.
This plugin add a new command to create a new note in a specific path.

To add a folder, use the settings tab. It will ask you to select a folder. The plugin will then create a new note in this folder.
To add a path, use the settings tab. It will ask you to select a path. The plugin will then create a new note in this path.

You can choose how, by default :
- The note is named
You can choose how, per each folder :
- The note is named
- The note is created (in the current tab, in a new tab, windows or in a split view)
- If the note must be focused after creation

After adding the folder, you can use the command "Create new note in folder {folder path}" to create a new note in this folder.
After adding the path, you can use the command "Create new note in folder {path}" to create a new note in this path.

### About file name

You can choose three options for the file name:
- A string name (ex: "Untitled")
- The date (based on a format)
- The folder name

For the date, you need to choose the format. You can use the same format as the [date format](https://momentjs.com/docs/#/displaying/format/) of moment.js.

The title will be incremented if a file with the same name already exists.

> **Note**
> If you have set a template for the folder, the new note will be created with the template.
> If you have set a template for the path, the new note will be created with the template.
## Installation

- [ ] From Obsidian's community plugins
- [x] Using [BRAT](https://github.com/TfTHacker/obsidian42-brat#adding-a-beta-plugin) using `https://github.com/Lisandra-dev/create-note-in-folder`
- [x] From release page:
- Download the latest release
- Unzip create-note-in-folder.zip in `.obsidian/plugins/` folder
- Unzip create-note-in-path.zip in `.obsidian/plugins/` path
- In Obsidian settings, reload the plugin
- Enable the plugin

Expand Down
26 changes: 22 additions & 4 deletions plugin/i18n/locales/en.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
export default {
title: "Folders",
error: "This folder is already in the list or not found",
error: "This path is already in the list or not found",
folderNotFound: "Folder not found",
name : {
template: {
title: "Choose the way to name the note. The name will be incremented if the name already exists. Also, no need to add the extension.",
dropDown: {
date: {
title: "Date",
desc: "Valid format can be found ",
here: "here",
url: "https://momentjs.com/docs/#/displaying/format/",
error: "Invalid date format, please use a valid format."
},
string: {
title: "String",
desc: "You can use any string name here."
},
folderName: "Folder Name",
}
},
title: "Default Name",
desc: "Default name for new notes"
},
opening: {
title: "Default Opening",
Expand All @@ -26,7 +42,9 @@ export default {
title: "Focus",
desc: "Focus on the new note after creation"
},
example: "Example: path/to/folder",
example: "Example: path/to/path",
remove: "Remove Folder",
add: "Add Folder"
add: "Add Folder",
modal: "Edit Folder specific settings",
submit: "Submit",
};
25 changes: 23 additions & 2 deletions plugin/i18n/locales/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ export default {
error: "Ce dossier est déjà dans la liste ou n'existe pas.",
folderNotFound: "Dossier introuvable",
name : {
template: {
title: "Choisissez la façon dont les notes seront nommées. Le nom sera incrémenté si le nom existe déjà. De plus, pas besoin d'ajouter l'extension.",
dropDown: {
date: {
title: "Date",
desc: "Les formats valides peuvent être trouvés ",
here: "ici",
url: "https://momentjs.com/docs/#/displaying/format/",
error: "Format de date invalide, veuillez utiliser un format valide."

},
string: {
title: "Chaîne de caractères",
desc: "Vous pouvez utiliser n'importe quelle chaîne de caractères ici."
},
folderName: "Nom du dossier",
}
},
title: "Nom par défaut",
desc: "Nom par défaut pour les nouvelles notes"
},
opening: {

title: "Ouverture par défaut",
desc: "Comment la note sera ouverte après sa création",
dropDown: {
Expand All @@ -28,5 +46,8 @@ export default {
},
example: "Exemple: chemin/vers/dossier",
remove: "Supprimer le dossier",
add: "Ajouter un dossier"
add: "Ajouter un dossier",
modal: "Modifier les paramètres spécifiques au dossier",
submit: "Valider",

};
21 changes: 14 additions & 7 deletions plugin/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ export enum SplitDirection {
vertical = "vertical"
}

export interface NoteInFolderSettings {
folder: string[];
defaultName: string;
export enum TypeName {
string = "string",
date = "date",
folderName = "folderName"
}

export interface FolderSettings {
path: string;
typeName: TypeName;
formatName: string;
opening: DefaultOpening;
focused: boolean;
splitDefault: SplitDirection;
}

export interface NoteInFolderSettings {
folder: FolderSettings[];
}

export const DEFAULT_SETTINGS: NoteInFolderSettings = {
folder: [],
defaultName: "Untitled",
opening: DefaultOpening.newTab,
focused: true,
splitDefault: SplitDirection.horizontal
};


74 changes: 56 additions & 18 deletions plugin/main.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
import {Plugin, WorkspaceLeaf, Notice} from "obsidian";
import {DEFAULT_SETTINGS, DefaultOpening, NoteInFolderSettings} from "./interface";
import {moment, Notice, Plugin, WorkspaceLeaf} from "obsidian";
import {
DEFAULT_SETTINGS,
DefaultOpening,
FolderSettings,
NoteInFolderSettings,
SplitDirection,
TypeName
} from "./interface";
import {NoteInFolderSettingsTab} from "./settings";
import {t} from "./i18n";

export default class NoteInFolder extends Plugin {
settings: NoteInFolderSettings;

addNewCommands(
generateFileName(folder: FolderSettings): string {
let defaultName = folder.formatName;
defaultName.replace(".md", "");
const typeName = folder.typeName;
if (typeName === TypeName.date) {
defaultName = moment().format(defaultName);
}
while (this.app.vault.getAbstractFileByPath(`${folder.path}/${defaultName}.md`)) {
const increment = defaultName.match(/ \d+$/);
const newIncrement = increment ? parseInt(increment[0]) + 1 : 1;
defaultName = defaultName.replace(/ \d+$/, "") + " " + newIncrement;
}
return defaultName + ".md";
}

async addNewCommands(
oldFolder: string | undefined,
newFolder: string | undefined,
newFolder: FolderSettings | undefined,
)
{

if (oldFolder !== undefined) {
//@ts-ignore
app.commands.removeCommand(`${this.manifest.id}:create-note-in-folder-${oldFolder}`);
app.commands.removeCommand(`${this.manifest.id}:create-note-in-folder-${oldFolder.path}`);
}
if (newFolder !== undefined) {
this.addCommand({
id: `create-note-in-folder-${newFolder}`,
name: `${newFolder}`,
id: `create-note-in-folder-${newFolder.path}`,
name: `${newFolder.path}`,
callback: async () => {
const defaultName = this.settings.defaultName;
console.log("Creating note in folder: " + newFolder + " with name: " + defaultName + ".md");
//check if folder exists
if (!this.app.vault.getAbstractFileByPath(newFolder)) {
const defaultName = this.generateFileName(newFolder);
//check if path exists
if (!this.app.vault.getAbstractFileByPath(newFolder.path)) {
new Notice(t("folderNotFound") as string);
this.settings.folder = this.settings.folder.filter((folder) => folder !== newFolder);
this.addNewCommands(newFolder, undefined);
this.saveSettings();
await this.addNewCommands(newFolder.path, undefined);
await this.saveSettings();
return;
}
const file = await this.app.vault.create(`${newFolder}/${defaultName}.md`, "");
console.log("Creating note in path: " + newFolder.path + " with name: " + defaultName);
const file = await this.app.vault.create(`${newFolder.path}/${defaultName}`, "");
let leaf: WorkspaceLeaf;
switch (this.settings.opening) {
switch (newFolder.opening) {
case DefaultOpening.split:
leaf = this.app.workspace.getLeaf("split", this.settings.splitDefault);
leaf = this.app.workspace.getLeaf("split", newFolder.splitDefault);
break;
case DefaultOpening.newWindow:
leaf = this.app.workspace.getLeaf("window");
Expand All @@ -47,7 +69,7 @@ export default class NoteInFolder extends Plugin {
leaf = this.app.workspace.getLeaf(false);
break;
}
await leaf.openFile(file, {active: this.settings.focused});
await leaf.openFile(file, {active: newFolder.focused});
}
});
}
Expand All @@ -57,10 +79,26 @@ export default class NoteInFolder extends Plugin {
console.log("Create Note in Folder plugin loaded");
await this.loadSettings();

//convert old settings (string[] to FolderSettings[])
if (this.settings.folder.length > 0 && typeof this.settings.folder[0] === "string") {
const oldFolders = this.settings.folder as unknown as string[];
this.settings.folder = [];
for (const folder of oldFolders) {
this.settings.folder.push({
path: folder,
formatName: "Untitled",
typeName: TypeName.string,
opening: DefaultOpening.current,
focused: true,
splitDefault: SplitDirection.vertical});
}
await this.saveSettings();
}

this.addSettingTab(new NoteInFolderSettingsTab(this.app, this));
const folders = this.settings.folder;
for (const folder of folders) {
this.addNewCommands(undefined, folder);
await this.addNewCommands(undefined, folder);
}
}

Expand Down
Loading

0 comments on commit 99d2c50

Please sign in to comment.