Skip to content

Commit

Permalink
Merge pull request #395 from chhoumann/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored Feb 21, 2023
2 parents faf7175 + fcbb8ee commit ed3710e
Show file tree
Hide file tree
Showing 20 changed files with 424 additions and 149 deletions.
16 changes: 9 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ export const TITLE_SYNTAX_SUGGEST_REGEX: RegExp = new RegExp(
);

// == File Exists (Template Choice) == //
export const fileExistsAppendToBottom: string =
"Append to the bottom of the file";
export const fileExistsAppendToTop: string = "Append to the top of the file";
export const fileExistsOverwriteFile: string = "Overwrite the file";
export const fileExistsDoNothing: string = "Nothing";
export const fileExistsChoices: string[] = [
export const fileExistsIncrement = "Increment the file name" as const;
export const fileExistsAppendToBottom =
"Append to the bottom of the file" as const;
export const fileExistsAppendToTop = "Append to the top of the file" as const;
export const fileExistsOverwriteFile = "Overwrite the file" as const;
export const fileExistsDoNothing = "Nothing" as const;
export const fileExistsChoices = [
fileExistsAppendToBottom,
fileExistsAppendToTop,
fileExistsOverwriteFile,
fileExistsIncrement,
fileExistsDoNothing,
];
] as const;

// == MISC == //
export const WIKI_LINK_REGEX: RegExp = new RegExp(/\[\[([^\]]*)\]\]/);
16 changes: 9 additions & 7 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
fileExistsChoices,
fileExistsOverwriteFile,
VALUE_SYNTAX,
fileExistsIncrement,
} from "../constants";
import { log } from "../logger/logManager";
import type QuickAdd from "../main";
Expand Down Expand Up @@ -56,7 +57,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
);
}

if (this.choice.incrementFileName)
if (this.choice.fileExistsMode === fileExistsIncrement)
filePath = await this.incrementFileName(filePath);

let createdFile: TFile | null;
Expand All @@ -71,14 +72,15 @@ export class TemplateChoiceEngine extends TemplateEngine {

await this.app.workspace.getLeaf("tab").openFile(file);

let userChoice = this.choice.fileExistsMode;
let userChoice: typeof fileExistsChoices[number] =
this.choice.fileExistsMode;

if(!this.choice.setFileExistsBehavior) {
userChoice = await GenericSuggester.Suggest(
if (!this.choice.setFileExistsBehavior) {
userChoice = (await GenericSuggester.Suggest(
this.app,
fileExistsChoices,
fileExistsChoices
);
[...fileExistsChoices],
[...fileExistsChoices]
)) as typeof fileExistsChoices[number];
}

switch (userChoice) {
Expand Down
7 changes: 5 additions & 2 deletions src/engine/TemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export abstract class TemplateEngine extends QuickAddEngine {
return createdFile;
} catch (e) {
log.logError(
`Could not create file with template. Maybe '${templatePath}' is an invalid template path?`
`Could not create file with template: \n\n${e.message}`
);
return null;
}
Expand Down Expand Up @@ -171,8 +171,11 @@ export abstract class TemplateEngine extends QuickAddEngine {

const templateFile =
this.app.vault.getAbstractFileByPath(correctTemplatePath);

if (!(templateFile instanceof TFile))
throw new Error("Template file not found.");
throw new Error(
`Template file not found at path "${correctTemplatePath}".`
);

return await this.app.vault.cachedRead(templateFile);
}
Expand Down
9 changes: 9 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@ declare module "obsidian" {
enablePlugin: (id: string) => Promise<void>;
disablePlugin: (id: string) => Promise<void>;
};
internalPlugins: {
plugins: {
[pluginId: string]: Plugin & {
[pluginImplementations: string]: any;
};
};
enablePlugin: (id: string) => Promise<void>;
disablePlugin: (id: string) => Promise<void>;
};
}
}
6 changes: 2 additions & 4 deletions src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import {
CREATE_IF_NOT_FOUND_BOTTOM,
CREATE_IF_NOT_FOUND_TOP,
FILE_NAME_FORMAT_SYNTAX,
FORMAT_SYNTAX,
} from "../../constants";
import { FormatDisplayFormatter } from "../../formatters/formatDisplayFormatter";
import type QuickAdd from "../../main";
import { FileNameDisplayFormatter } from "../../formatters/fileNameDisplayFormatter";
import { getTemplatePaths } from "../../utility";
import { NewTabDirection } from "../../types/newTabDirection";
import type { FileViewMode } from "../../types/fileViewMode";
import { GenericTextSuggester } from "../suggesters/genericTextSuggester";
Expand Down Expand Up @@ -350,11 +348,11 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
templateSelector.inputEl.style.width = "100%";
templateSelector.inputEl.style.marginBottom = "8px";

const markdownFiles: string[] = getTemplatePaths(this.app);
const templateFilePaths: string[] = this.plugin.getTemplateFiles().map(f => f.path);
new GenericTextSuggester(
this.app,
templateSelector.inputEl,
markdownFiles
templateFilePaths
);

templateSelector.onChange((value) => {
Expand Down
56 changes: 29 additions & 27 deletions src/gui/ChoiceBuilder/templateChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import { NewTabDirection } from "../../types/newTabDirection";
import FolderList from "./FolderList.svelte";
import { FileNameDisplayFormatter } from "../../formatters/fileNameDisplayFormatter";
import { log } from "../../logger/logManager";
import { getAllFolderPathsInVault, getTemplatePaths } from "../../utility";
import { getAllFolderPathsInVault } from "../../utility";
import type QuickAdd from "../../main";
import type { FileViewMode } from "../../types/fileViewMode";
import { GenericTextSuggester } from "../suggesters/genericTextSuggester";
import { FormatSyntaxSuggester } from "../suggesters/formatSyntaxSuggester";
import { ExclusiveSuggester } from "../suggesters/exclusiveSuggester";
import { fileExistsAppendToBottom, fileExistsAppendToTop, fileExistsDoNothing, fileExistsOverwriteFile } from "src/constants";
import {
fileExistsAppendToBottom,
fileExistsAppendToTop,
fileExistsChoices,
fileExistsDoNothing,
fileExistsIncrement,
fileExistsOverwriteFile,
} from "src/constants";

export class TemplateChoiceBuilder extends ChoiceBuilder {
choice: ITemplateChoice;
Expand All @@ -36,7 +43,6 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
this.addFileNameFormatSetting();
this.addFolderSetting();
this.addAppendLinkSetting();
this.addIncrementFileNameSetting();
this.addFileAlreadyExistsSetting();
this.addOpenFileSetting();
if (this.choice.openFile) this.addOpenFileInNewTabSetting();
Expand All @@ -47,7 +53,9 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
.setName("Template Path")
.setDesc("Path to the Template.")
.addSearch((search) => {
const templates: string[] = getTemplatePaths(this.app);
const templates: string[] = this.plugin
.getTemplateFiles()
.map((f) => f.path);
search.setValue(this.choice.templatePath);
search.setPlaceholder("Template path");

Expand Down Expand Up @@ -150,13 +158,16 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {

const stn = new Setting(chooseFolderFromSubfolderContainer);
stn.setName("Include subfolders")
.setDesc("Get prompted to choose from both the selected folders and their subfolders when creating the note.")
.addToggle((toggle) => toggle
.setValue(this.choice.folder?.chooseFromSubfolders)
.onChange((value) => {
this.choice.folder.chooseFromSubfolders = value;
this.reload();
})
.setDesc(
"Get prompted to choose from both the selected folders and their subfolders when creating the note."
)
.addToggle((toggle) =>
toggle
.setValue(this.choice.folder?.chooseFromSubfolders)
.onChange((value) => {
this.choice.folder.chooseFromSubfolders = value;
this.reload();
})
);
}

Expand Down Expand Up @@ -260,19 +271,6 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
});
}

private addIncrementFileNameSetting(): void {
const incrementFileNameSetting: Setting = new Setting(this.contentEl);
incrementFileNameSetting
.setName("Increment file name")
.setDesc("If the file already exists, increment the file name.")
.addToggle((toggle) => {
toggle.setValue(this.choice.incrementFileName);
toggle.onChange(
(value) => (this.choice.incrementFileName = value)
);
});
}

private addFileAlreadyExistsSetting(): void {
const fileAlreadyExistsSetting: Setting = new Setting(this.contentEl);
fileAlreadyExistsSetting
Expand All @@ -291,14 +289,18 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
this.choice.fileExistsMode = fileExistsDoNothing;

dropdown
.addOption(fileExistsAppendToBottom, fileExistsAppendToBottom)
.addOption(
fileExistsAppendToBottom,
fileExistsAppendToBottom
)
.addOption(fileExistsAppendToTop, fileExistsAppendToTop)
.addOption(fileExistsIncrement, fileExistsIncrement)
.addOption(fileExistsOverwriteFile, fileExistsOverwriteFile)
.addOption(fileExistsDoNothing, fileExistsDoNothing)
.setValue(this.choice.fileExistsMode)
.onChange(
(value) =>
(this.choice.fileExistsMode = value as string)
(value: typeof fileExistsChoices[number]) =>
(this.choice.fileExistsMode = value)
);
});
}
Expand Down
1 change: 0 additions & 1 deletion src/gui/MacroGUIs/MacroBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export class MacroBuilder extends Modal {

private addCommandList() {
const commandList = this.contentEl.createDiv("commandList");
console.log(this.macro.commands);

this.commandListEl = new CommandList({
target: commandList,
Expand Down
2 changes: 2 additions & 0 deletions src/gui/choiceList/ChoiceView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
if (!updatedChoice) return;
choices = choices.map(choice => updateChoiceHelper(choice, updatedChoice));
plugin.removeCommandForChoice(oldChoice);
plugin.addCommandForChoice(updatedChoice);
saveChoices(choices);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/suggesters/formatSyntaxSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
VARIABLE_DATE_SYNTAX_SUGGEST_REGEX,
VARIABLE_SYNTAX_SUGGEST_REGEX,
} from "../../constants";
import { getTemplatePaths } from "../../utility";
import type QuickAdd from "../../main";

enum FormatSyntaxToken {
Expand Down Expand Up @@ -50,7 +49,8 @@ export class FormatSyntaxSuggester extends TextInputSuggest<string> {
this.macroNames = this.plugin.settings.macros.map(
(macro) => macro.name
);
this.templatePaths = getTemplatePaths(this.app);

this.templatePaths = this.plugin.getTemplateFiles().map((file) => file.path);
}

getSuggestions(inputStr: string): string[] {
Expand Down
2 changes: 1 addition & 1 deletion src/logger/guiLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class GuiLogger extends QuickAddLogger {

logError(msg: string): void {
const error = this.getQuickAddError(msg, ErrorLevel.Error);
new Notice(this.formatOutputString(error));
new Notice(this.formatOutputString(error), 15000);
}

logWarning(msg: string): void {
Expand Down
39 changes: 8 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MarkdownView, Plugin } from "obsidian";
import { MarkdownView, Plugin, TFile } from "obsidian";
import { DEFAULT_SETTINGS, QuickAddSettingsTab } from "./quickAddSettingsTab";
import type { QuickAddSettings } from "./quickAddSettingsTab";
import { log } from "./logger/logManager";
Expand All @@ -10,9 +10,9 @@ import { ChoiceExecutor } from "./choiceExecutor";
import type IChoice from "./types/choices/IChoice";
import type IMultiChoice from "./types/choices/IMultiChoice";
import { deleteObsidianCommand } from "./utility";
import type IMacroChoice from "./types/choices/IMacroChoice";
import ChoiceSuggester from "./gui/suggesters/choiceSuggester";
import { QuickAddApi } from "./quickAddApi";
import migrate from "./migrations/migrate";

export default class QuickAdd extends Plugin {
static instance: QuickAdd;
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class QuickAdd extends Plugin {
);
this.addCommandsForChoices(this.settings.choices);

await this.convertMacroChoicesMacroToId();
migrate(this);
}

onunload() {
Expand Down Expand Up @@ -165,34 +165,11 @@ export default class QuickAdd extends Plugin {
deleteObsidianCommand(this.app, `quickadd:choice:${choice.id}`);
}

// Did not make sense to have copies of macros in the choices when they are maintained for themselves.
// Instead we reference by id now. Have to port this over for all users.
private async convertMacroChoicesMacroToId() {
function convertMacroChoiceMacroToIdHelper(choice: IChoice): IChoice {
if (choice.type === ChoiceType.Multi) {
let multiChoice = choice as IMultiChoice;
const multiChoices = multiChoice.choices.map(
convertMacroChoiceMacroToIdHelper
);
multiChoice = { ...multiChoice, choices: multiChoices };
return multiChoice;
}

if (choice.type !== ChoiceType.Macro) return choice;
const macroChoice = choice as IMacroChoice;

if (macroChoice.macro) {
macroChoice.macroId = macroChoice.macro.id;
delete macroChoice.macro;
}

return macroChoice;
}

this.settings.choices = this.settings.choices.map(
convertMacroChoiceMacroToIdHelper
public getTemplateFiles(): TFile[] {
if (!String.isString(this.settings.templateFolderPath)) return [];

return this.app.vault.getFiles().filter((file) =>
file.path.startsWith(this.settings.templateFolderPath)
);

await this.saveSettings();
}
}
11 changes: 11 additions & 0 deletions src/migrations/Migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import QuickAdd from "src/main";
import { QuickAddSettings } from "src/quickAddSettingsTab";

export type Migration = {
description: string;
migrate: (plugin: QuickAdd) => Promise<void>;
};

export type Migrations = {
[key in keyof QuickAddSettings["migrations"]]: Migration;
};
Loading

1 comment on commit ed3710e

@vercel
Copy link

@vercel vercel bot commented on ed3710e Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quickadd – ./

quickadd-chrisbbh.vercel.app
quickadd.obsidian.guide
quickadd-git-master-chrisbbh.vercel.app

Please sign in to comment.