feat: add modify post command
This commit is contained in:
parent
b0b1122b0a
commit
140ea85459
|
@ -1,6 +1,6 @@
|
||||||
import ENV from "src/env"
|
import ENV from "src/env"
|
||||||
|
|
||||||
interface UpdatePostQueryParams {
|
interface ModifyPostQueryParams {
|
||||||
accessToken: string,
|
accessToken: string,
|
||||||
blogName: string,
|
blogName: string,
|
||||||
postId: string,
|
postId: string,
|
||||||
|
@ -10,8 +10,22 @@ interface UpdatePostQueryParams {
|
||||||
tag: string
|
tag: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatePostApi = async (updatePostQueryParams: UpdatePostQueryParams) => {
|
const modifyPostApi = async (modifyPostQueryParams: ModifyPostQueryParams) => {
|
||||||
const url = `${ENV.TISTORY_URL}/post/modify?${updatePostQueryParams}`
|
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
|
|
@ -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
|
|
@ -11,12 +11,12 @@ class PublishModal extends Modal {
|
||||||
|
|
||||||
onOpen(): void {
|
onOpen(): void {
|
||||||
const {contentEl} = this
|
const {contentEl} = this
|
||||||
contentEl.createEl("h1", {text: "Insert tag"})
|
contentEl.createEl("h1", {text: "Publish Post"})
|
||||||
|
|
||||||
// create input tags box
|
// create input tags box
|
||||||
new Setting(contentEl)
|
new Setting(contentEl)
|
||||||
.setName("Tag")
|
.setName("Tag")
|
||||||
.setDesc("input tag separate by `,`")
|
.setDesc("enter tag separate by `,`")
|
||||||
.addText((text) =>
|
.addText((text) =>
|
||||||
text.onChange((value) => {
|
text.onChange((value) => {
|
||||||
this.tags = value
|
this.tags = value
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
import PublishModal from "./PublishModal";
|
import PublishModal from "./PublishModal";
|
||||||
|
import ModifyModal from "./ModifyModal";
|
||||||
|
|
||||||
export default PublishModal
|
export {
|
||||||
|
PublishModal,
|
||||||
|
ModifyModal
|
||||||
|
};
|
36
src/main.ts
36
src/main.ts
|
@ -1,8 +1,9 @@
|
||||||
import { Plugin, Editor, MarkdownView, MarkdownRenderer } from "obsidian";
|
import { Plugin, Editor, MarkdownView, MarkdownRenderer } from "obsidian";
|
||||||
import { DEFFAULT_SETTINGS, TistoryPublisherSettings } from "./components/settings";
|
import { DEFFAULT_SETTINGS, TistoryPublisherSettings } from "./components/settings";
|
||||||
import PublishModal from "./components/modal";
|
import {PublishModal, ModifyModal} from "./components/modal";
|
||||||
import createPostApi from "./api/createPostApi";
|
import createPostApi from "./api/createPostApi";
|
||||||
import TistoryPublisherSettingTab from "./components/settingTab";
|
import TistoryPublisherSettingTab from "./components/settingTab";
|
||||||
|
import modifyPostApi from "./api/updatePostApi";
|
||||||
|
|
||||||
export default class TistoryPublisherPlugin extends Plugin {
|
export default class TistoryPublisherPlugin extends Plugin {
|
||||||
settings: TistoryPublisherSettings
|
settings: TistoryPublisherSettings
|
||||||
|
@ -12,10 +13,10 @@ export default class TistoryPublisherPlugin extends Plugin {
|
||||||
await this.loadSettings()
|
await this.loadSettings()
|
||||||
this.addSettingTab(new TistoryPublisherSettingTab(this.app, this))
|
this.addSettingTab(new TistoryPublisherSettingTab(this.app, this))
|
||||||
|
|
||||||
// create publish-to-tistory custom command
|
// create publish-to command
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: "publish-to-tistory",
|
id: "publish-post",
|
||||||
name: "publish current note to tistory",
|
name: "publish current note.",
|
||||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||||
new PublishModal(this.app, async (tag: string) => {
|
new PublishModal(this.app, async (tag: string) => {
|
||||||
const el = document.createElement("div")
|
const el = document.createElement("div")
|
||||||
|
@ -37,6 +38,33 @@ export default class TistoryPublisherPlugin extends Plugin {
|
||||||
}).open()
|
}).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() {
|
async loadSettings() {
|
||||||
|
|
Loading…
Reference in New Issue