mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
Settings done
This commit is contained in:
commit
2857bd3233
11 changed files with 2478 additions and 32 deletions
1889
package-lock.json
generated
1889
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -20,5 +20,8 @@
|
|||
"obsidian": "latest",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@popperjs/core": "^2.11.6"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
67
src/main.ts
67
src/main.ts
|
|
@ -1,6 +1,7 @@
|
|||
import { Plugin, TFile, TFolder, TAbstractFile } from 'obsidian'
|
||||
import { DEFAULT_SETTINGS, FolderNotesSettings, SettingsTab } from './settings'
|
||||
import FolderNameModal from './folderNameModal';
|
||||
import FolderNameModal from './modals/folderName';
|
||||
import { applyTemplate } from './template';
|
||||
export default class FolderNotesPlugin extends Plugin {
|
||||
observer: MutationObserver
|
||||
folders: TFolder[] = []
|
||||
|
|
@ -17,22 +18,35 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
} else {
|
||||
document.body.classList.remove('hide-folder-note');
|
||||
}
|
||||
this.registerEvent(this.app.vault.on('create', (file: TAbstractFile) => {
|
||||
this.registerEvent(this.app.vault.on('create', (folder: TAbstractFile) => {
|
||||
if (!this.app.workspace.layoutReady) return;
|
||||
if (!this.settings.autoCreate) return;
|
||||
if (file instanceof TFolder) {
|
||||
const path = file.path + '/' + file.name + '.md';
|
||||
if (!path) return;
|
||||
const folder = this.app.vault.getAbstractFileByPath(path);
|
||||
if (folder) return;
|
||||
this.createFolderNote(path, true);
|
||||
}
|
||||
if (!(folder instanceof TFolder)) return;
|
||||
|
||||
const excludedFolder = this.settings.excludeFolders.find(
|
||||
(excludedFolder) => (excludedFolder.path === folder.path) ||
|
||||
(excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf("/") >= 0 ? folder.path?.lastIndexOf("/") : folder.path.length)
|
||||
&& excludedFolder.subFolders));
|
||||
if (excludedFolder?.disableAutoCreate) return;
|
||||
|
||||
const path = folder.path + '/' + folder.name + '.md';
|
||||
if (!path) return;
|
||||
const file = this.app.vault.getAbstractFileByPath(path);
|
||||
if (file) return;
|
||||
this.createFolderNote(path, true, true);
|
||||
|
||||
}));
|
||||
|
||||
this.registerEvent(this.app.vault.on('rename', (file: TAbstractFile, oldPath: string) => {
|
||||
if (!this.settings.syncFolderName) return;
|
||||
if (file instanceof TFolder) {
|
||||
const folder = this.app.vault.getAbstractFileByPath(file?.path);
|
||||
if (!folder) return;
|
||||
const excludedFolder = this.settings.excludeFolders.find(
|
||||
(excludedFolder) => (excludedFolder.path === folder.path) ||
|
||||
(excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf("/") >= 0 ? folder.path?.lastIndexOf("/") : folder.path.length)
|
||||
&& excludedFolder.subFolders));
|
||||
if (excludedFolder?.disableSync) return;
|
||||
const oldName = oldPath.substring(oldPath.lastIndexOf('/' || '\\'));
|
||||
const newPath = folder?.path + '/' + folder?.name + '.md';
|
||||
if (folder instanceof TFolder) {
|
||||
|
|
@ -45,6 +59,12 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
} else if (file instanceof TFile) {
|
||||
const folder = this.app.vault.getAbstractFileByPath(oldPath.substring(0, oldPath.lastIndexOf('/' || '\\')));
|
||||
if (!folder) return;
|
||||
const excludedFolder = this.settings.excludeFolders.find(
|
||||
(excludedFolder) => (excludedFolder.path === folder.path) ||
|
||||
(excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf("/") >= 0 ? folder.path?.lastIndexOf("/") : folder.path.length)
|
||||
&& excludedFolder.subFolders));
|
||||
if (excludedFolder?.disableSync) return;
|
||||
if (file.name !== folder?.name + '.md') return;
|
||||
if (folder instanceof TFolder) {
|
||||
this.app.vault.rename(folder, folder.path.substring(0, folder.path.lastIndexOf('/' || '\\')) + '/' + file.name.substring(0, file.name.lastIndexOf('.')));
|
||||
|
|
@ -57,6 +77,7 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
if (rec.type === 'childList') {
|
||||
(<Element>rec.target).querySelectorAll('div.nav-folder-title-content')
|
||||
.forEach((element: HTMLElement) => {
|
||||
if (element.onclick) return;
|
||||
element.onclick = (event: MouseEvent) => this.handleFolderClick(event);
|
||||
});
|
||||
}
|
||||
|
|
@ -78,6 +99,24 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
|
||||
const folder = event.target.parentElement?.getAttribute('data-path');
|
||||
const excludedFolder = this.settings.excludeFolders.find(
|
||||
(excludedFolder) => (excludedFolder.path === folder) ||
|
||||
(excludedFolder.path === folder?.slice(0, folder?.lastIndexOf("/") >= 0 ? folder?.lastIndexOf("/") : folder.length)
|
||||
&& excludedFolder.subFolders));
|
||||
if (excludedFolder?.disableFolderNote) {
|
||||
event.target.onclick = null;
|
||||
event.target.click();
|
||||
event.target.parentElement?.parentElement?.getElementsByClassName('nav-folder-children').item(0)?.querySelectorAll('div.nav-file')
|
||||
.forEach((element: HTMLElement) => {
|
||||
if (element.innerText === (event.target as HTMLElement)?.innerText && element.classList.contains('is-folder-note')) {
|
||||
element.classList.remove('is-folder-note');
|
||||
}
|
||||
});
|
||||
return;
|
||||
} else if (excludedFolder?.enableCollapsing || this.settings.enableCollapsing) {
|
||||
event.target.onclick = null;
|
||||
event.target.click();
|
||||
}
|
||||
const path = folder + '/' + event.target.innerText + '.md';
|
||||
|
||||
if (this.app.vault.getAbstractFileByPath(path)) {
|
||||
|
|
@ -86,13 +125,14 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
event.target.parentElement?.parentElement?.getElementsByClassName('nav-folder-children').item(0)?.querySelectorAll('div.nav-file')
|
||||
.forEach((element: HTMLElement) => {
|
||||
if (element.innerText === (event.target as HTMLElement)?.innerText && !element.classList.contains('is-folder-note')) {
|
||||
console.log(element)
|
||||
element.classList.add('is-folder-note');
|
||||
}
|
||||
});
|
||||
|
||||
} else if (event.altKey || event.ctrlKey) {
|
||||
if ((this.settings.altKey && event.altKey) || (this.settings.ctrlKey && event.ctrlKey)) {
|
||||
this.createFolderNote(path);
|
||||
this.createFolderNote(path, true, true);
|
||||
if (!this.settings.hideFolderNote) return;
|
||||
event.target.parentElement?.parentElement?.getElementsByClassName('nav-folder-children').item(0)?.querySelectorAll('div.nav-file')
|
||||
.forEach((element: HTMLElement) => {
|
||||
|
|
@ -110,10 +150,13 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
}
|
||||
}
|
||||
|
||||
async createFolderNote(path: string, useModal?: boolean) {
|
||||
async createFolderNote(path: string, openFile: boolean, useModal?: boolean) {
|
||||
const leaf = this.app.workspace.getLeaf(false);
|
||||
const file = await this.app.vault.create(path, '');
|
||||
await leaf.openFile(file);
|
||||
if (openFile) {
|
||||
await leaf.openFile(file);
|
||||
}
|
||||
applyTemplate(this, this.settings.templatePath);
|
||||
if (!this.settings.autoCreate) return;
|
||||
if (!useModal) return;
|
||||
const folder = this.app.vault.getAbstractFileByPath(path.substring(0, path.lastIndexOf('/' || '\\')));
|
||||
|
|
|
|||
42
src/modals/confirmCreation.ts
Normal file
42
src/modals/confirmCreation.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { App, Modal, Setting, TFolder } from 'obsidian';
|
||||
import FolderNotesPlugin from '../main';
|
||||
export default class ConfirmationModal extends Modal {
|
||||
plugin: FolderNotesPlugin;
|
||||
app: App;
|
||||
folder: TFolder;
|
||||
constructor(app: App, plugin: FolderNotesPlugin) {
|
||||
super(app);
|
||||
this.plugin = plugin;
|
||||
this.app = app;
|
||||
}
|
||||
onOpen() {
|
||||
const { contentEl } = this;
|
||||
contentEl.createEl('h2', { text: 'Create folder note for every folder' });
|
||||
const setting = new Setting(contentEl)
|
||||
setting.infoEl.createEl('p', { text: 'Make sure to backup your vault before using this feature.' }).style.color = '#fb464c';
|
||||
setting.infoEl.createEl('p', { text: 'This feature will create a folder note for every folder in your vault.' });
|
||||
setting.infoEl.createEl('p', { text: 'Every folder that already has a folder note will be ignored' });
|
||||
setting.infoEl.parentElement?.classList.add('fn-confirmation-modal')
|
||||
const button = setting.infoEl.createEl('button', { text: 'Create' });
|
||||
button.classList.add('mod-warning', 'fn-confirmation-modal-button');
|
||||
button.addEventListener('click', async () => {
|
||||
this.close();
|
||||
this.app.vault.getAllLoadedFiles().forEach(async (file) => {
|
||||
if (file instanceof TFolder) {
|
||||
if (this.app.vault.getAbstractFileByPath(file.path + '/' + file.name + '.md')) return;
|
||||
await this.plugin.createFolderNote(file.path + '/' + file.name + '.md', false);
|
||||
}
|
||||
})
|
||||
});
|
||||
// focus on the button
|
||||
button.focus();
|
||||
const cancelButton = setting.infoEl.createEl('button', { text: 'Cancel' });
|
||||
cancelButton.addEventListener('click', async () => {
|
||||
this.close();
|
||||
});
|
||||
}
|
||||
onClose() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
90
src/modals/exludeFolderSettings.ts
Normal file
90
src/modals/exludeFolderSettings.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import { App, Modal, Setting } from 'obsidian';
|
||||
import FolderNotesPlugin from '../main';
|
||||
import { ExcludedFolder } from '../settings';
|
||||
export default class ExcludedFolderSettings extends Modal {
|
||||
plugin: FolderNotesPlugin;
|
||||
app: App;
|
||||
excludedFolder: ExcludedFolder;
|
||||
constructor(app: App, plugin: FolderNotesPlugin, excludedFolder: ExcludedFolder) {
|
||||
super(app);
|
||||
this.plugin = plugin;
|
||||
this.app = app;
|
||||
this.excludedFolder = excludedFolder;
|
||||
}
|
||||
onOpen() {
|
||||
this.display();
|
||||
}
|
||||
display() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
contentEl.createEl('h2', { text: 'Exluded folder settings' });
|
||||
new Setting(contentEl)
|
||||
.setName('Include subfolders')
|
||||
.setDesc('Choose if the subfolders of the folder should also be excluded')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.excludedFolder.subFolders)
|
||||
.onChange(async (value) => {
|
||||
this.excludedFolder.subFolders = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
new Setting(contentEl)
|
||||
.setName('Disable folder name sync')
|
||||
.setDesc('Choose if the folder note should be renamed when the folder name is changed')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.excludedFolder.disableSync)
|
||||
.onChange(async (value) => {
|
||||
this.excludedFolder.disableSync = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
new Setting(contentEl)
|
||||
.setName('Disable auto creation of folder notes in this folder')
|
||||
.setDesc('Choose if a folder note should be created when a new folder is created')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.excludedFolder.disableAutoCreate)
|
||||
.onChange(async (value) => {
|
||||
this.excludedFolder.disableAutoCreate = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
new Setting(contentEl)
|
||||
.setName('Disable open folder note')
|
||||
.setDesc('Choose if the folder note should be opened when the folder is opened')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.excludedFolder.disableFolderNote)
|
||||
.onChange(async (value) => {
|
||||
this.excludedFolder.disableFolderNote = value;
|
||||
await this.plugin.saveSettings();
|
||||
this.display();
|
||||
})
|
||||
);
|
||||
|
||||
if (!this.excludedFolder.disableFolderNote) {
|
||||
new Setting(contentEl)
|
||||
.setName('Collapse folder when opening folder note')
|
||||
.setDesc('Choose if the folder should be collapsed when the folder note is opened')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.excludedFolder.enableCollapsing)
|
||||
.onChange(async (value) => {
|
||||
this.excludedFolder.enableCollapsing = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
onClose() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import { App, Modal, Setting, TFolder } from 'obsidian';
|
||||
import FolderNotesPlugin from './main';
|
||||
import FolderNotesPlugin from '../main';
|
||||
export default class FolderNameModal extends Modal {
|
||||
plugin: FolderNotesPlugin;
|
||||
app: App;
|
||||
169
src/settings.ts
169
src/settings.ts
|
|
@ -1,5 +1,9 @@
|
|||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
import FolderNotesPlugin from "./main";
|
||||
import { FolderSuggest } from "./suggesters/folderSuggester";
|
||||
import ExcludedFolderSettings from "./modals/exludeFolderSettings";
|
||||
import { TemplateSuggest } from "./suggesters/templateSuggester";
|
||||
import ConfirmationModal from "./modals/confirmCreation";
|
||||
export interface FolderNotesSettings {
|
||||
syncFolderName: boolean;
|
||||
ctrlKey: boolean;
|
||||
|
|
@ -7,16 +11,10 @@ export interface FolderNotesSettings {
|
|||
hideFolderNote: boolean;
|
||||
templatePath: string;
|
||||
autoCreate: boolean;
|
||||
excludeFolders: string[];
|
||||
}
|
||||
export interface ExcludeFolder {
|
||||
path: string;
|
||||
subFolders: boolean;
|
||||
exludeSync: boolean;
|
||||
excludeAutoCreate: boolean;
|
||||
disableFolderNote: boolean;
|
||||
position: number;
|
||||
enableCollapsing: boolean;
|
||||
excludeFolders: ExcludedFolder[];
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
||||
syncFolderName: true,
|
||||
ctrlKey: true,
|
||||
|
|
@ -24,11 +22,13 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = {
|
|||
hideFolderNote: true,
|
||||
templatePath: '',
|
||||
autoCreate: false,
|
||||
enableCollapsing: false,
|
||||
excludeFolders: [],
|
||||
};
|
||||
export class SettingsTab extends PluginSettingTab {
|
||||
plugin: FolderNotesPlugin;
|
||||
app: App;
|
||||
excludeFolders: ExcludedFolder[];
|
||||
constructor(app: App, plugin: FolderNotesPlugin) {
|
||||
super(app, plugin);
|
||||
}
|
||||
|
|
@ -39,6 +39,18 @@ export class SettingsTab extends PluginSettingTab {
|
|||
|
||||
containerEl.createEl('h2', { text: 'Folder notes settings' });
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Disable folder collapsing')
|
||||
.setDesc('Disable the ability to collapse folders by clicking on the folder name')
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(!this.plugin.settings.enableCollapsing)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.enableCollapsing = !value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Hide folder note')
|
||||
.setDesc('Hide the folder note in the file explorer')
|
||||
|
|
@ -96,6 +108,37 @@ export class SettingsTab extends PluginSettingTab {
|
|||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Template path')
|
||||
.setDesc('The path to the template file')
|
||||
.addSearch((cb) => {
|
||||
new TemplateSuggest(cb.inputEl, this.plugin);
|
||||
cb.setPlaceholder('Template path');
|
||||
cb.setValue(this.plugin.app.vault.getAbstractFileByPath(this.plugin.settings.templatePath)?.name.replace('.md', '') || '');
|
||||
cb.onChange(async (value) => {
|
||||
if (value.trim() === '') {
|
||||
this.plugin.settings.templatePath = '';
|
||||
await this.plugin.saveSettings();
|
||||
this.display();
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Create folder note for every folder')
|
||||
.setDesc('Create a folder note for every folder in the vault')
|
||||
.addButton((cb) => {
|
||||
cb.setIcon('plus');
|
||||
|
||||
cb.setTooltip('Create folder notes');
|
||||
cb.onClick(async () => {
|
||||
new ConfirmationModal(this.app, this.plugin).open();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
new Setting(containerEl)
|
||||
.setHeading()
|
||||
.setName('Manage excluded folders')
|
||||
|
|
@ -106,13 +149,113 @@ export class SettingsTab extends PluginSettingTab {
|
|||
cb.setClass('add-exclude-folder');
|
||||
cb.setTooltip('Add excluded folder');
|
||||
cb.onClick(() => {
|
||||
const excludedFolder = new ExcludedFolder('', this.plugin.settings.excludeFolders.length);
|
||||
this.addExcludeFolderListItem(containerEl, excludedFolder);
|
||||
this.addExcludedFolder(excludedFolder);
|
||||
this.display();
|
||||
})
|
||||
})
|
||||
this.plugin.settings.excludeFolders.sort((a, b) => a.position - b.position).forEach((excludedFolder) => {
|
||||
this.addExcludeFolderListItem(containerEl, excludedFolder);
|
||||
})
|
||||
}
|
||||
addExcludeFolderListItem(excludeFolder: ExcludeFolder) {
|
||||
const { containerEl } = this;
|
||||
new Setting(containerEl)
|
||||
addExcludeFolderListItem(containerEl: HTMLElement, excludedFolder: ExcludedFolder) {
|
||||
const setting = new Setting(containerEl)
|
||||
setting.setClass('fn-exclude-folder-list-item')
|
||||
setting.addSearch(cb => {
|
||||
new FolderSuggest(
|
||||
cb.inputEl,
|
||||
this.plugin
|
||||
);
|
||||
// @ts-ignore
|
||||
cb.containerEl.addClass('fn-exclude-folder-path');
|
||||
cb.setPlaceholder('Folder path');
|
||||
cb.setValue(excludedFolder.path);
|
||||
cb.onChange((value) => {
|
||||
if (!this.app.vault.getAbstractFileByPath(value)) return;
|
||||
excludedFolder.path = value;
|
||||
this.updateExcludedFolder(excludedFolder, excludedFolder);
|
||||
})
|
||||
})
|
||||
setting.addButton(cb => {
|
||||
cb.setIcon('edit');
|
||||
cb.setTooltip('Edit folder note');
|
||||
cb.onClick(() => {
|
||||
new ExcludedFolderSettings(this.app, this.plugin, excludedFolder).open();
|
||||
})
|
||||
})
|
||||
|
||||
setting.addButton(cb => {
|
||||
cb.setIcon('up-chevron-glyph');
|
||||
cb.setTooltip('Move up');
|
||||
cb.onClick(() => {
|
||||
if (excludedFolder.position === 0) return;
|
||||
excludedFolder.position = excludedFolder.position - 1;
|
||||
console.log(excludedFolder.position);
|
||||
this.updateExcludedFolder(excludedFolder, excludedFolder);
|
||||
const oldExcludedFolder = this.plugin.settings.excludeFolders.find(folder => folder.position == excludedFolder.position)
|
||||
if (oldExcludedFolder) {
|
||||
|
||||
oldExcludedFolder.position = oldExcludedFolder.position + 1;
|
||||
this.updateExcludedFolder(oldExcludedFolder, oldExcludedFolder);
|
||||
}
|
||||
this.display();
|
||||
})
|
||||
})
|
||||
setting.addButton(cb => {
|
||||
cb.setIcon('down-chevron-glyph');
|
||||
cb.setTooltip('Move down');
|
||||
cb.onClick(() => {
|
||||
if (excludedFolder.position === this.plugin.settings.excludeFolders.length - 1) return;
|
||||
excludedFolder.position = excludedFolder.position + 1;
|
||||
this.updateExcludedFolder(excludedFolder, excludedFolder);
|
||||
const oldExcludedFolder = this.plugin.settings.excludeFolders.find(folder => folder.position == excludedFolder.position)
|
||||
if (oldExcludedFolder) {
|
||||
oldExcludedFolder.position = oldExcludedFolder.position - 1;
|
||||
this.updateExcludedFolder(oldExcludedFolder, oldExcludedFolder);
|
||||
}
|
||||
this.display();
|
||||
})
|
||||
})
|
||||
setting.addButton(cb => {
|
||||
cb.setIcon('trash-2');
|
||||
cb.setTooltip('Delete excluded folder');
|
||||
cb.onClick(() => {
|
||||
this.deleteExcludedFolder(excludedFolder);
|
||||
setting.clear();
|
||||
setting.settingEl.remove();
|
||||
})
|
||||
})
|
||||
}
|
||||
addExcludeFolder(excludeFolder: ExcludeFolder) {
|
||||
addExcludedFolder(excludedFolder: ExcludedFolder) {
|
||||
this.plugin.settings.excludeFolders.push(excludedFolder);
|
||||
this.plugin.saveSettings();
|
||||
}
|
||||
deleteExcludedFolder(excludedFolder: ExcludedFolder) {
|
||||
this.plugin.settings.excludeFolders = this.plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path);
|
||||
this.plugin.saveSettings();
|
||||
}
|
||||
updateExcludedFolder(excludedFolder: ExcludedFolder, newExcludeFolder: ExcludedFolder) {
|
||||
this.plugin.settings.excludeFolders = this.plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path);
|
||||
this.addExcludedFolder(newExcludeFolder);
|
||||
}
|
||||
|
||||
}
|
||||
export class ExcludedFolder {
|
||||
path: string;
|
||||
subFolders: boolean;
|
||||
disableSync: boolean;
|
||||
disableAutoCreate: boolean;
|
||||
disableFolderNote: boolean;
|
||||
enableCollapsing: boolean;
|
||||
position: number;
|
||||
constructor(path: string, position: number) {
|
||||
this.path = path;
|
||||
this.subFolders = true;
|
||||
this.disableSync = true;
|
||||
this.disableAutoCreate = true;
|
||||
this.disableFolderNote = false;
|
||||
this.enableCollapsing = false;
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
54
src/suggesters/FolderSuggester.ts
Normal file
54
src/suggesters/FolderSuggester.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes and https://github.com/SilentVoid13/Templater
|
||||
|
||||
import { TAbstractFile, TFolder } from "obsidian";
|
||||
import { TextInputSuggest } from "./suggest";
|
||||
import FolderNotesPlugin from "../main";
|
||||
export enum FileSuggestMode {
|
||||
TemplateFiles,
|
||||
ScriptFiles,
|
||||
}
|
||||
|
||||
export class FolderSuggest extends TextInputSuggest<TFolder> {
|
||||
constructor(
|
||||
public inputEl: HTMLInputElement,
|
||||
private plugin: FolderNotesPlugin
|
||||
) {
|
||||
super(inputEl);
|
||||
}
|
||||
|
||||
|
||||
get_error_msg(mode: FileSuggestMode): string {
|
||||
switch (mode) {
|
||||
case FileSuggestMode.TemplateFiles:
|
||||
return `Templates folder doesn't exist`;
|
||||
case FileSuggestMode.ScriptFiles:
|
||||
return `User Scripts folder doesn't exist`;
|
||||
}
|
||||
}
|
||||
|
||||
getSuggestions(input_str: string): TFolder[] {
|
||||
const folders: TFolder[] = [];
|
||||
const lower_input_str = input_str.toLowerCase();
|
||||
this.plugin.app.vault.getAllLoadedFiles().forEach((folder: TAbstractFile) => {
|
||||
if (
|
||||
folder instanceof TFolder &&
|
||||
folder.path.toLowerCase().contains(lower_input_str) &&
|
||||
!this.plugin.settings.excludeFolders.find(f => f.path === folder.path)
|
||||
) {
|
||||
folders.push(folder);
|
||||
}
|
||||
});
|
||||
|
||||
return folders;
|
||||
}
|
||||
|
||||
renderSuggestion(folder: TFolder, el: HTMLElement): void {
|
||||
el.setText(folder.path);
|
||||
}
|
||||
|
||||
selectSuggestion(folder: TFolder): void {
|
||||
this.inputEl.value = folder.path;
|
||||
this.inputEl.trigger("input");
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
70
src/suggesters/templateSuggester.ts
Normal file
70
src/suggesters/templateSuggester.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes and https://github.com/SilentVoid13/Templater
|
||||
|
||||
import { TAbstractFile, TFile, TFolder, Vault } from "obsidian";
|
||||
import { TextInputSuggest } from "./suggest";
|
||||
import FolderNotesPlugin from "../main";
|
||||
import { getTemplatePlugins } from "src/template";
|
||||
export enum FileSuggestMode {
|
||||
TemplateFiles,
|
||||
ScriptFiles,
|
||||
}
|
||||
|
||||
export class TemplateSuggest extends TextInputSuggest<TFile> {
|
||||
constructor(
|
||||
public inputEl: HTMLInputElement,
|
||||
private plugin: FolderNotesPlugin
|
||||
) {
|
||||
super(inputEl);
|
||||
}
|
||||
|
||||
|
||||
get_error_msg(mode: FileSuggestMode): string {
|
||||
switch (mode) {
|
||||
case FileSuggestMode.TemplateFiles:
|
||||
return `Templates folder doesn't exist`;
|
||||
case FileSuggestMode.ScriptFiles:
|
||||
return `User Scripts folder doesn't exist`;
|
||||
}
|
||||
}
|
||||
|
||||
getSuggestions(input_str: string): TFile[] {
|
||||
const { templateFolder, templaterPlugin } = getTemplatePlugins(this.plugin.app);
|
||||
if ((!templateFolder || templateFolder?.trim() === "") && !templaterPlugin) {
|
||||
this.plugin.settings.templatePath = "";
|
||||
this.plugin.saveSettings();
|
||||
return [];
|
||||
}
|
||||
let folder: TFolder;
|
||||
if (templaterPlugin) {
|
||||
folder = this.plugin.app.vault.getAbstractFileByPath(templaterPlugin.plugin?.settings?.templates_folder as string) as TFolder;
|
||||
|
||||
} else {
|
||||
folder = this.plugin.app.vault.getAbstractFileByPath(templateFolder) as TFolder;
|
||||
}
|
||||
|
||||
const files: TFile[] = [];
|
||||
const lower_input_str = input_str.toLowerCase();
|
||||
|
||||
Vault.recurseChildren(folder, (file: TAbstractFile) => {
|
||||
if (file instanceof TFile &&
|
||||
file.path.toLowerCase().contains(lower_input_str)
|
||||
) {
|
||||
files.push(file);
|
||||
}
|
||||
});
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
renderSuggestion(file: TFile, el: HTMLElement): void {
|
||||
el.setText(file.name.replace(".md", ""));
|
||||
}
|
||||
|
||||
selectSuggestion(file: TFile): void {
|
||||
this.inputEl.value = file.name.replace(".md", "");
|
||||
this.inputEl.trigger("input");
|
||||
this.plugin.settings.templatePath = file.path;
|
||||
this.plugin.saveSettings();
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
92
src/template.ts
Normal file
92
src/template.ts
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
// Thanks to @mgmeyers for the creating the obsidian kanban plugin https://github.com/mgmeyers/obsidian-kanban
|
||||
// from where I got the template code for this plugin
|
||||
// https://github.com/mgmeyers/obsidian-kanban/blob/48e6c278ce9140b7e034b181432321f697d6e45e/src/components/helpers.ts
|
||||
|
||||
import { TFile, MarkdownView, App } from 'obsidian';
|
||||
import FolderNotesPlugin from './main';
|
||||
export async function applyTemplate(
|
||||
plugin: FolderNotesPlugin,
|
||||
templatePath?: string
|
||||
) {
|
||||
const templateFile = templatePath
|
||||
? plugin.app.vault.getAbstractFileByPath(templatePath)
|
||||
: null;
|
||||
|
||||
if (templateFile && templateFile instanceof TFile) {
|
||||
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
|
||||
|
||||
try {
|
||||
// Force the view to source mode, if needed
|
||||
if (activeView?.getMode() !== 'source') {
|
||||
await activeView?.setState(
|
||||
{
|
||||
...activeView.getState(),
|
||||
mode: 'source',
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
const {
|
||||
templatesEnabled,
|
||||
templaterEnabled,
|
||||
templatesPlugin,
|
||||
templaterPlugin,
|
||||
} = getTemplatePlugins(plugin.app);
|
||||
|
||||
const templateContent = await plugin.app.vault.read(templateFile);
|
||||
|
||||
// If both plugins are enabled, attempt to detect templater first
|
||||
if (templatesEnabled && templaterEnabled) {
|
||||
if (/<%/.test(templateContent)) {
|
||||
return await templaterPlugin.append_template_to_active_file(
|
||||
templateFile
|
||||
);
|
||||
}
|
||||
|
||||
return await templatesPlugin.instance.insertTemplate(templateFile);
|
||||
}
|
||||
|
||||
if (templatesEnabled) {
|
||||
return await templatesPlugin.instance.insertTemplate(templateFile);
|
||||
}
|
||||
|
||||
if (templaterEnabled) {
|
||||
return await templaterPlugin.append_template_to_active_file(
|
||||
templateFile
|
||||
);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getTemplatePlugins(app: App) {
|
||||
const templatesPlugin = (app as any).internalPlugins.plugins.templates;
|
||||
const templatesEnabled = templatesPlugin.enabled;
|
||||
const templaterPlugin = (app as any).plugins.plugins['templater-obsidian'];
|
||||
const templaterEnabled = (app as any).plugins.enabledPlugins.has(
|
||||
'templater-obsidian'
|
||||
);
|
||||
const templaterEmptyFileTemplate =
|
||||
templaterPlugin &&
|
||||
(this.app as any).plugins.plugins['templater-obsidian'].settings
|
||||
?.empty_file_template;
|
||||
|
||||
const templateFolder = templatesEnabled
|
||||
? templatesPlugin.instance.options.folder
|
||||
: templaterPlugin
|
||||
? templaterPlugin.settings.template_folder
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
templatesPlugin,
|
||||
templatesEnabled,
|
||||
templaterPlugin: templaterPlugin?.templater,
|
||||
templaterEnabled,
|
||||
templaterEmptyFileTemplate,
|
||||
templateFolder,
|
||||
};
|
||||
}
|
||||
32
styles.css
32
styles.css
|
|
@ -16,13 +16,33 @@
|
|||
cursor: pointer;
|
||||
}
|
||||
|
||||
.ofwc-command-list,
|
||||
.ofwc-command-list.setting-item-control {
|
||||
flex-shrink: 0;
|
||||
display: block;
|
||||
float: left;
|
||||
.fn-exclude-folder-list-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0.75rem 0;
|
||||
border-top: none;
|
||||
padding-top: 0;
|
||||
}
|
||||
|
||||
.fn-exclude-folder-path {
|
||||
width: calc(100% - 20px);
|
||||
}
|
||||
|
||||
.fn-exclude-folder-list-item .setting-item-control {
|
||||
width: 100%;
|
||||
overflow-x: auto;
|
||||
justify-content: flex-end;
|
||||
flex: 1 1 auto;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.fn-exclude-folder-list-item > *.last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
.fn-confirmation-modal {
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
.fn-confirmation-modal-button {
|
||||
margin-right: 0.7rem;
|
||||
}
|
||||
Loading…
Reference in a new issue