diff --git a/src/api/updatePostApi.ts b/src/api/updatePostApi.ts index 9c650b2..558e4b2 100644 --- a/src/api/updatePostApi.ts +++ b/src/api/updatePostApi.ts @@ -1,6 +1,6 @@ import ENV from "src/env" -interface UpdatePostQueryParams { +interface ModifyPostQueryParams { accessToken: string, blogName: string, postId: string, @@ -10,8 +10,22 @@ interface UpdatePostQueryParams { tag: string } -const updatePostApi = async (updatePostQueryParams: UpdatePostQueryParams) => { - const url = `${ENV.TISTORY_URL}/post/modify?${updatePostQueryParams}` +const modifyPostApi = async (modifyPostQueryParams: ModifyPostQueryParams) => { + const queryParams = `access_token=${modifyPostQueryParams.accessToken}&output=json&blogName=${modifyPostQueryParams.blogName}&title=${modifyPostQueryParams.title}&tag=${modifyPostQueryParams.tag}&visibility=${modifyPostQueryParams.visibility}&content=${ + encodeURIComponent(modifyPostQueryParams.content) + }&postId=${modifyPostQueryParams.postId}` + const url = `${ENV.TISTORY_URL}/post/modify?${queryParams}` + + return await fetch(url, { + method: "POST" + }) + .then((response) => { + return response + }) + .catch((error) => { + console.log(error) + throw new Error(error) + }) } -export default updatePostApi \ No newline at end of file +export default modifyPostApi \ No newline at end of file diff --git a/src/components/modal/ModifyModal.ts b/src/components/modal/ModifyModal.ts new file mode 100644 index 0000000..d2aaea9 --- /dev/null +++ b/src/components/modal/ModifyModal.ts @@ -0,0 +1,52 @@ +import { App, Modal, Setting } from "obsidian"; + +class ModifyModal extends Modal { + tags: string + postId: string + onSubmit: (tags: string, postId: string) => void; + + constructor(app: App, onSubmit: (tags: string, postId: string) => void) { + super(app) + this.onSubmit = onSubmit + } + + onOpen(): void { + const {contentEl} = this + contentEl.createEl("h1", {text: "Modify Post"}) + + new Setting(contentEl) + .setName("Tag") + .setDesc("enter tag separate by `,`") + .addText((text) => + text.onChange((value) => { + this.tags = value + }) + ) + + new Setting(contentEl) + .setName("Post ID") + .setDesc("enter post id to modifiy ") + .addText((text) => + text.onChange((value) => { + this.postId = value + }) + ) + + new Setting(contentEl).addButton((button) => { + button + .setButtonText("Submit") + .setCta() + .onClick(() => { + this.close() + this.onSubmit(this.tags, this.postId) + }) + }) + } + + onClose(): void { + const {contentEl} = this + contentEl.empty() + } +} + +export default ModifyModal \ No newline at end of file diff --git a/src/components/modal/PublishModal.ts b/src/components/modal/PublishModal.ts index 1969aeb..3e8a4b4 100644 --- a/src/components/modal/PublishModal.ts +++ b/src/components/modal/PublishModal.ts @@ -11,12 +11,12 @@ class PublishModal extends Modal { onOpen(): void { const {contentEl} = this - contentEl.createEl("h1", {text: "Insert tag"}) + contentEl.createEl("h1", {text: "Publish Post"}) // create input tags box new Setting(contentEl) .setName("Tag") - .setDesc("input tag separate by `,`") + .setDesc("enter tag separate by `,`") .addText((text) => text.onChange((value) => { this.tags = value diff --git a/src/components/modal/index.ts b/src/components/modal/index.ts index dd539d5..6fba852 100644 --- a/src/components/modal/index.ts +++ b/src/components/modal/index.ts @@ -1,3 +1,7 @@ import PublishModal from "./PublishModal"; +import ModifyModal from "./ModifyModal"; -export default PublishModal \ No newline at end of file +export { + PublishModal, + ModifyModal +}; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 1d94491..5fbc476 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,9 @@ import { Plugin, Editor, MarkdownView, MarkdownRenderer } from "obsidian"; import { DEFFAULT_SETTINGS, TistoryPublisherSettings } from "./components/settings"; -import PublishModal from "./components/modal"; +import {PublishModal, ModifyModal} from "./components/modal"; import createPostApi from "./api/createPostApi"; import TistoryPublisherSettingTab from "./components/settingTab"; +import modifyPostApi from "./api/updatePostApi"; export default class TistoryPublisherPlugin extends Plugin { settings: TistoryPublisherSettings @@ -12,10 +13,10 @@ export default class TistoryPublisherPlugin extends Plugin { await this.loadSettings() this.addSettingTab(new TistoryPublisherSettingTab(this.app, this)) - // create publish-to-tistory custom command + // create publish-to command this.addCommand({ - id: "publish-to-tistory", - name: "publish current note to tistory", + id: "publish-post", + name: "publish current note.", editorCallback: (editor: Editor, view: MarkdownView) => { new PublishModal(this.app, async (tag: string) => { const el = document.createElement("div") @@ -37,6 +38,33 @@ export default class TistoryPublisherPlugin extends Plugin { }).open() } }); + + // create modify-post command + this.addCommand({ + id: "modify-post", + name: "modify current note.", + editorCallback: (editor: Editor, view: MarkdownView) => { + new ModifyModal(this.app, async (tag: string, postId: string) => { + const el = document.createElement("div") + await MarkdownRenderer.render( + this.app, + editor.getValue(), + el, + this.app.workspace.getActiveFile()?.path ?? "/", + view + ) + await modifyPostApi({ + "accessToken": this.settings.accessToken, + "blogName": this.settings.blogName, + "title": view.getDisplayText(), + "tag": tag, + "visibility": this.settings.visibility, + "content": el.innerHTML, + "postId": postId + }) + }).open() + } + }) } async loadSettings() {