Skip to content

Commit

Permalink
feat: add publish memos from logseq
Browse files Browse the repository at this point in the history
  • Loading branch information
samwei12 authored and EINDEX committed Mar 5, 2023
1 parent 9c03229 commit 473dad3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@logseq/libs";

import { IHookEvent } from "@logseq/libs/dist/LSPlugin";
import {BlockEntity, IHookEvent} from "@logseq/libs/dist/LSPlugin";
import MemosSync from "./memos";

function main() {
Expand Down Expand Up @@ -78,6 +78,13 @@ function main() {
memosSync.parseSetting();
});

logseq.Editor.registerSlashCommand("Post memo", async () => {
const entity: BlockEntity | null = await logseq.Editor.getCurrentBlock()
if (entity) {
await memosSync.publish(entity);
}
},);

memosSync.autoSyncWhenStartLogseq();
}

Expand Down
28 changes: 26 additions & 2 deletions src/memos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ type ListMemo = {
data: Memo[];
};

type SingleMemo = {
data: Memo;
};

const searchExistsMemo = async (
memoId: number
): Promise<BlockEntity | null> => {
const memo_blocks: BlockEntity[] | null = await logseq.DB.q(
`(property memoid ${memoId})`
`(property memoId ${memoId})`
);
if (memo_blocks && memo_blocks.length > 0) {
return memo_blocks[0];
Expand Down Expand Up @@ -169,6 +173,13 @@ class MemosSync {
}
}

public async publish(block: BlockEntity) {
const memo = await this.postMemo(block.content)
console.log(`${memo.id}`)
await logseq.Editor.upsertBlockProperty(block.uuid, "memoId", memo.id);
await logseq.UI.showMsg("Post memo success");
}

private async sync() {
const memos = await this.fetchMemos();
for (const memo of memos) {
Expand All @@ -185,7 +196,7 @@ class MemosSync {
): Promise<BlockEntity | null> {
const opts = {
properties: {
memoid: memo.id,
memoId: memo.id,
},
};
if (this.mode === "Custom Page") {
Expand Down Expand Up @@ -234,6 +245,19 @@ class MemosSync {
}
return resp.data.data;
}

private async postMemo(content: string, visibility = "PUBLIC"): Promise<Memo> {
const payload = {
"content": `${content}`,
"visibility": `${visibility}`
}
const resp: AxiosResponse<SingleMemo> = await axios.post(this.openAPI(), payload);
if (resp.status !== 200) {
logseq.Request;
throw "Connect issue";
}
return resp.data.data;
}
}

export default MemosSync;

0 comments on commit 473dad3

Please sign in to comment.