Skip to content

Commit

Permalink
feat: auto sync memos when start logseq
Browse files Browse the repository at this point in the history
	modified:   readme.md
  • Loading branch information
EINDEX committed Feb 2, 2023
1 parent 2886f63 commit c2e3d38
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Features

- Sync memos to logseq via memos openAPI
- Auto Sync memos when start Logseq

## How to use

Expand Down
13 changes: 11 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ function main() {
key: "includeArchive",
type: "boolean",
title: "Include archive",
description: "Also sync archive memos",
description: "Sync archive memos",
default: false,
},{
key: "autoSync",
type: "boolean",
title: "Auto Sync",
description: "Also sync when open Logseq",
default: false,
},{
key: "mode",
Expand All @@ -33,8 +39,11 @@ function main() {
default: "Memos",
}])

const memosSync = new MemosSync();

logseq.App.registerCommandPalette({key: "sync-memos", label: "Sync Memos"} , async (e: IHookEvent) => {
(new MemosSync()).syncMemos()
logseq.UI.showMsg("Staring Sync Memos");
memosSync.syncMemos()
})
}

Expand Down
15 changes: 13 additions & 2 deletions src/memos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MemosSync {
private openId: string | undefined;
private host: string | undefined;
private includeArchive: boolean | undefined;
private autoSync: boolean | undefined;

constructor() {
this.parseSetting()
Expand All @@ -82,12 +83,19 @@ class MemosSync {
public async syncMemos() {
try {
await this.sync();
logseq.UI.showMsg("Moes Sync Success", "success");
} catch (e) {
console.error(e);
logseq.UI.showMsg(String(e), "error");
}
}

public async autoSyncWhenStartLogseq() {
if (this.autoSync) {
await this.syncMemos();
}
}

private openAPI() {
const url = new URL(`${this.host}/api/memo`)
url.searchParams.append("openId", String(this.openId))
Expand All @@ -100,7 +108,7 @@ class MemosSync {

private parseSetting() {
try {
const { openAPI, mode, customPage, includeArchive } : any = logseq.settings;
const { openAPI, mode, customPage, includeArchive, autoSync } : any = logseq.settings;
const openAPIURL = new URL(openAPI);
this.host = openAPIURL.origin;
const openId = openAPIURL.searchParams.get("openId");
Expand All @@ -109,16 +117,17 @@ class MemosSync {
}
this.openId = openId;
this.mode = mode;
this.autoSync = autoSync;
this.customPage = customPage;
this.includeArchive = includeArchive;
this.autoSyncWhenStartLogseq();
} catch (e) {
console.error(e)
logseq.UI.showMsg( "Memos OpenAPI is not a URL", "error");
}
}

private async sync() {
logseq.UI.showMsg("Staring Sync Memos");
const memos = await this.fetchMemos();
for (const memo of memos) {
const existMemo = await searchExistsMemo(memo.id);
Expand All @@ -128,6 +137,8 @@ class MemosSync {
}
}



private async generateParentBlock(
memo: Memo,
preferredDateFormat: string
Expand Down

0 comments on commit c2e3d38

Please sign in to comment.