feat: add modify post command

This commit is contained in:
bekurin 2023-09-24 20:19:54 +09:00
parent b0b1122b0a
commit 140ea85459
5 changed files with 109 additions and 11 deletions

View File

@ -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
export default modifyPostApi

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,7 @@
import PublishModal from "./PublishModal";
import ModifyModal from "./ModifyModal";
export default PublishModal
export {
PublishModal,
ModifyModal
};

View File

@ -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() {