Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New chat from template doesn't work if chats folder is missing #35

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { FileManager, MarkdownView, Notice, Vault } from "obsidian";
import {
FileManager,
MarkdownView,
Notice,
Vault,
Modal,
App,
Setting,
} from "obsidian";

// check for unclosed code block in MD (three backticks), string should contain three backticks in a row
export const unfinishedCodeBlock = (txt: string) => {
Expand Down Expand Up @@ -38,7 +46,102 @@ export const writeInferredTitleToEditor = async (
fileManager.renameFile(file, newFileName);
} catch (err) {
new Notice("[ChatGPT MD] Error writing inferred title to editor");
console.log("[ChatGPT MD] Error writing inferred title to editor", err)
console.log("[ChatGPT MD] Error writing inferred title to editor", err);
throw err;
}
};

export const createFolderModal = async (
app: App,
vault: Vault,
folderName: string,
folderPath: string
) => {
const folderCreationModal = new FolderCreationModal(
app,
folderName,
folderPath
);

folderCreationModal.open();
const result = await folderCreationModal.waitForModalValue();

if (result) {
console.log("[ChatGPT MD] Creating folder");
await vault.createFolder(folderPath);
} else {
console.log("[ChatGPT MD] Not creating folder");
}

return result;
};

class FolderCreationModal extends Modal {
result: boolean;
folderName: string;
folderPath: string;
modalPromise: Promise<boolean>;
resolveModalPromise: (value: boolean) => void;

constructor(
app: App,
folderName: string,
folderPath: string
) {
super(app);
this.folderName = folderName;
this.folderPath = folderPath;

this.result = false;
this.modalPromise = new Promise((resolve) => {
this.resolveModalPromise = resolve;
});
}

onOpen() {
const { contentEl } = this;

contentEl.createEl("h2", {
text: `[ChatGPT MD] No ${this.folderName} folder found.`,
});

contentEl.createEl("p", {
text: `If you choose "Yes, Create", the plugin will automatically create a folder at: ${this.folderPath}. You can change this path in the plugin settings.`,
});


new Setting(contentEl).addButton((btn) =>
btn
.setButtonText("Yes, Create Folder")
.setTooltip("Create folder")
.setCta()
.onClick(() => {
this.result = true; // This can be any value the user provides.
this.resolveModalPromise(this.result);
this.close();
})
);

new Setting(contentEl).addButton((btn) =>
btn
.setButtonText("No, I'll create it myself")
.setTooltip("Cancel")
.setCta()
.onClick(() => {
this.result = false; // This can be any value the user provides.
this.resolveModalPromise(this.result);
this.close();
})
);

}

waitForModalValue() {
return this.modalPromise;
}

onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
64 changes: 39 additions & 25 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "obsidian";

import { StreamManager } from "./stream";
import { unfinishedCodeBlock, writeInferredTitleToEditor } from "helpers";
import { unfinishedCodeBlock, writeInferredTitleToEditor, createFolderModal } from "helpers";

interface ChatGPT_MDSettings {
apiKey: string;
Expand Down Expand Up @@ -652,18 +652,23 @@ export default class ChatGPT_MD extends Plugin {
try {
const selectedText = editor.getSelection();

if (
!this.settings.chatFolder ||
!this.app.vault.getAbstractFileByPath(
this.settings.chatFolder
)
) {
if (!this.settings.chatFolder || this.settings.chatFolder.trim() === "") {
new Notice(
`[ChatGPT MD] No chat folder found. Please set one in settings and make sure it exists.`
`[ChatGPT MD] No chat folder value found. Please set one in settings.`
);
return;
}

if (!await this.app.vault.adapter.exists(this.settings.chatFolder)) {
const result = await createFolderModal(this.app, this.app.vault, "chatFolder", this.settings.chatFolder);
if (!result) {
new Notice(
`[ChatGPT MD] No chat folder found. One must be created to use plugin. Set one in settings and make sure it exists.`
);
return;
}
}

const newFile = await this.app.vault.create(
`${this.settings.chatFolder}/${this.getDate(
new Date(),
Expand All @@ -690,33 +695,42 @@ export default class ChatGPT_MD extends Plugin {
id: "choose-chat-template",
name: "Create new chat from template",
icon: "layout-template",
editorCallback: (editor: Editor, view: MarkdownView) => {
// check if chats folder exists
if (
!this.settings.chatFolder ||
!this.app.vault.getAbstractFileByPath(
this.settings.chatFolder
)
) {
editorCallback: async (editor: Editor, view: MarkdownView) => {

if (!this.settings.chatFolder || this.settings.chatFolder.trim() === "") {
new Notice(
`[ChatGPT MD] No chat folder found. Please set one in settings and make sure it exists.`
`[ChatGPT MD] No chat folder value found. Please set one in settings.`
);
return;
}

// check if templates folder exists
if (
!this.settings.chatTemplateFolder ||
!this.app.vault.getAbstractFileByPath(
this.settings.chatTemplateFolder
)
) {
if (!await this.app.vault.adapter.exists(this.settings.chatFolder)) {
const result = await createFolderModal(this.app, this.app.vault, "chatFolder", this.settings.chatFolder);
if (!result) {
new Notice(
`[ChatGPT MD] No chat folder found. One must be created to use plugin. Set one in settings and make sure it exists.`
);
return;
}
}

if (!this.settings.chatTemplateFolder || this.settings.chatTemplateFolder.trim() === "") {
new Notice(
`[ChatGPT MD] No templates folder found. Please set one in settings and make sure it exists.`
`[ChatGPT MD] No chat template folder value found. Please set one in settings.`
);
return;
}

if (!await this.app.vault.adapter.exists(this.settings.chatTemplateFolder)) {
const result = await createFolderModal(this.app, this.app.vault, "chatTemplateFolder", this.settings.chatTemplateFolder);
if (!result) {
new Notice(
`[ChatGPT MD] No chat template folder found. One must be created to use plugin. Set one in settings and make sure it exists.`
);
return;
}
}

new ChatTemplates(
this.app,
this.settings,
Expand Down