From 25ccae15c69ff46cb22256ab147384984b218fe6 Mon Sep 17 00:00:00 2001 From: AidenLx Date: Mon, 23 Aug 2021 10:31:21 +0800 Subject: [PATCH] feat: send to obsidian via url in favor of clipboard fix sel.book return empty --- package.json | 6 ++++- src/event-handlers.ts | 7 +++--- src/modules/const.ts | 2 +- src/modules/parser.ts | 51 ++++++++++++++-------------------------- src/modules/return.d.ts | 3 +-- src/modules/scan.ts | 7 +++--- src/modules/sender.ts | 35 +++++++++++++++++++++++++++ src/modules/translate.ts | 4 ++++ src/togglePlugin.ts | 2 +- tsconfig.json | 8 ++----- 10 files changed, 72 insertions(+), 53 deletions(-) create mode 100644 src/modules/sender.ts diff --git a/package.json b/package.json index 68e503a..490a868 100644 --- a/package.json +++ b/package.json @@ -18,11 +18,13 @@ "license": "MIT", "devDependencies": { "@alx-plugins/marginnote": "^1.8.3", + "@jsonurl/jsonurl": "^1.1.4", "@release-it/bumper": "^3.0.1", "@release-it/conventional-changelog": "^3.3.0", "@rollup/plugin-commonjs": "^20.0.0", "@rollup/plugin-node-resolve": "^13.0.4", "@rollup/plugin-typescript": "^8.2.5", + "@types/url-parse": "^1.4.4", "@typescript-eslint/eslint-plugin": "^4.29.0", "@typescript-eslint/parser": "^4.29.0", "cz-conventional-changelog": "^3.3.0", @@ -35,6 +37,7 @@ "eslint-plugin-prettier": "^3.4.0", "eslint-plugin-simple-import-sort": "^7.0.0", "prettier": "^2.3.2", + "query-string": "^6.14.1", "release-it": "^14.11.5", "rollup": "^2.56.2", "rollup-plugin-copy2": "^0.3.1", @@ -42,7 +45,8 @@ "ts-jest": "^27.0.5", "ts-node": "^10.2.1", "tslib": "^2.3.1", - "typescript": "^4.3.5" + "typescript": "^4.3.5", + "url-parse": "^1.5.3" }, "peerDependencies": { "@alx-plugins/marginnote": "^1.8.3" diff --git a/src/event-handlers.ts b/src/event-handlers.ts index 0cb3d6a..e233f69 100644 --- a/src/event-handlers.ts +++ b/src/event-handlers.ts @@ -1,6 +1,5 @@ import { ChangeExcerptRange_Sender, - DocumentController, EventHandler, NotifySender, PopupMenuOnNote_Sender, @@ -11,7 +10,7 @@ import PopupRecorder from "modules/PopupRecorder"; import { showHUD } from "modules/tools"; import { addonOnName } from "togglePlugin"; -import { handleNote, handleSel, handleToc } from "./modules/parser"; +import { SendNote, SendSel, SendToc } from "./modules/sender"; export const onPopupMenuOnNote = (sender: PopupMenuOnNote_Sender) => { if ( @@ -35,7 +34,7 @@ export const onPopupMenuOnNote = (sender: PopupMenuOnNote_Sender) => { const note = sender.userInfo.note; - self.tocMode ? handleToc(note) : handleNote(note); + self.tocMode ? SendToc(note) : SendNote(note); } catch (error) { showHUD(error.toString()); } @@ -57,7 +56,7 @@ export const onPopupMenuOnSelection = (sender: PopupMenuOnSelection_Sender) => { const { selectionText: sel, document: book } = sender.userInfo.documentController; if (sel && sel.length) { - handleSel({ sel, book }); + SendSel({ sel, book }); } } catch (error) { showHUD(error.toString()); diff --git a/src/modules/const.ts b/src/modules/const.ts index 84b6304..4176804 100644 --- a/src/modules/const.ts +++ b/src/modules/const.ts @@ -1,4 +1,4 @@ import { MNMark } from "./return"; export const PREFIX: MNMark = "\n"; const version = "2.1.0"; -export const VERSION: string = "v" + version; +export const VERSION: string = version; diff --git a/src/modules/parser.ts b/src/modules/parser.ts index f568fb1..a0185ae 100644 --- a/src/modules/parser.ts +++ b/src/modules/parser.ts @@ -1,6 +1,5 @@ import { excerptPic_video, MbBook, MbBookNote } from "@alx-plugins/marginnote"; -import { PREFIX, VERSION } from "./const"; import PopupRecorder from "./PopupRecorder"; import { Data, @@ -11,8 +10,7 @@ import { Selection, } from "./return"; import { scanNote, scanObject, scanToc } from "./scan"; -import { copy, showHUD } from "./tools"; -import getText from "./translate"; +import { showHUD } from "./tools"; const getBook = (docMd5: string | undefined): MbBook | null => { const bookObj = @@ -22,10 +20,6 @@ const getBook = (docMd5: string | undefined): MbBook | null => { return bookObj ? scanObject(bookObj) : null; }; -const stringify = (obj: any): string => { - return PREFIX + JSON.stringify(obj); -}; - const getLastAndSendTime = ( data: Data, ): { sendTime: ReturnBody["sendTime"]; last: ReturnBody["last"] } => { @@ -34,16 +28,15 @@ const getLastAndSendTime = ( return { last, sendTime: rec.push(data) }; }; -export const handleSel = (sel: Selection): void => { +export const getBody_Sel = (sel: Selection): ReturnBody_Sel => { const { last, sendTime } = getLastAndSendTime(sel); - const returns: ReturnBody_Sel = { - version: VERSION, + if (sel.book) sel.book = scanObject(sel.book); + return { type: "sel", sendTime, data: sel, last, }; - copy(stringify(returns)); }; const arrToObj = ( @@ -56,7 +49,7 @@ const arrToObj = ( return obj; }, {} as any) as Record; -export const handleNote = (note: MbBookNote): void => { +export const getBody_Note = (note: MbBookNote): ReturnBody_Note => { const [data, bookMd5s] = scanNote(note, 2), { last, sendTime } = getLastAndSendTime(data), bookMap = arrToObj(bookMd5s, (id) => getBook(id)); @@ -75,8 +68,7 @@ export const handleNote = (note: MbBookNote): void => { }) : {}; - const returns: ReturnBody_Note = { - version: VERSION, + return { type: "note", sendTime, bookMap, @@ -84,28 +76,19 @@ export const handleNote = (note: MbBookNote): void => { data, last, }; - copy(stringify(returns)); }; -export const handleToc = (note: MbBookNote): void => { +export const getBody_Toc = (note: MbBookNote): ReturnBody_Toc => { // if (note.parentNote) return; const result = scanToc(note); - if (typeof result !== "string") { - const [data, bookMd5s] = result, - { last, sendTime } = getLastAndSendTime(data), - bookMap = arrToObj(bookMd5s, (id) => getBook(id)); - const returns: ReturnBody_Toc = { - version: VERSION, - type: "toc", - sendTime, - bookMap, - data, - last, - }; - copy(stringify(returns)); - showHUD(getText("hint_toc_success") + note.noteTitle); - } else showHUD(result); + const [data, bookMd5s] = result, + { last, sendTime } = getLastAndSendTime(data), + bookMap = arrToObj(bookMd5s, (id) => getBook(id)); + return { + type: "toc", + sendTime, + bookMap, + data, + last, + }; }; - -const isSel = (node: Data): node is Selection => - typeof (node as Selection).sel === "string"; diff --git a/src/modules/return.d.ts b/src/modules/return.d.ts index dd264a4..33dc3e6 100644 --- a/src/modules/return.d.ts +++ b/src/modules/return.d.ts @@ -14,14 +14,13 @@ export type DataType = "sel" | "note" | "toc"; export type MNMark = "\n"; type ReturnBody_Basic = { - version: string; type: DataType; sendTime: ReturnType; data: Data; last: item | null; }; -export type ReturnBody = ReturnBody_Note | ReturnBody_Sel; +export type ReturnBody = ReturnBody_Note | ReturnBody_Sel | ReturnBody_Toc; export interface ReturnBody_Sel extends ReturnBody_Basic { type: "sel"; diff --git a/src/modules/scan.ts b/src/modules/scan.ts index 2e3e90f..a25c396 100644 --- a/src/modules/scan.ts +++ b/src/modules/scan.ts @@ -6,9 +6,7 @@ import { Toc } from "./return"; /** * @returns when note missing params, return error message */ -export const scanToc = ( - note: MbBookNote, -): [note: Toc, bookMd5s: string[]] | string => { +export const scanToc = (note: MbBookNote): [note: Toc, bookMd5s: string[]] => { const depth = 99; let invaild: string[] = [], bookMd5s: Set = new Set(); @@ -62,8 +60,9 @@ export const scanToc = ( const result = scan(note); if (!result) { JSB.log("🌈🌈🌈 MNLOG toc: invaild param\n" + invaild.join("\n")); + throw new EvalError("invaild param: " + invaild.join(",")); } - return result ? [result, [...bookMd5s]] : invaild.join("\n"); + return [result, [...bookMd5s]]; }; export const scanNote = ( note: MbBookNote, diff --git a/src/modules/sender.ts b/src/modules/sender.ts new file mode 100644 index 0000000..d2f88d3 --- /dev/null +++ b/src/modules/sender.ts @@ -0,0 +1,35 @@ +import { MbBookNote } from "@alx-plugins/marginnote"; +import JsonURL from "@jsonurl/jsonurl"; +import { stringify as toQs } from "query-string"; +import Url from "url-parse"; + +import { VERSION } from "./const"; +import { getBody_Note, getBody_Sel, getBody_Toc } from "./parser"; +import { ReturnBody, Selection } from "./return"; +import { showHUD } from "./tools"; +import getText from "./translate"; + +export const SendToc = (src: MbBookNote): void => { + try { + toURL(getBody_Toc(src)); + showHUD(getText("hint_toc_success") + src.noteTitle); + } catch (error) { + showHUD(error); + } +}; +export const SendSel = (src: Selection): void => toURL(getBody_Sel(src)); +export const SendNote = (src: MbBookNote): void => toURL(getBody_Note(src)); + +const toURL = (obj: ReturnBody) => { + const url = new Url("obsidian://mncomp"), + { type, sendTime, last, data } = obj, + qs = toQs({ + version: VERSION, + type, + sendTime, + last: JsonURL.stringify(last), + data: JsonURL.stringify(data), + }); + url.set("query", qs); + Application.sharedInstance().openURL(NSURL.URLWithString(url.toString())); +}; diff --git a/src/modules/translate.ts b/src/modules/translate.ts index 283b3fe..eb39286 100644 --- a/src/modules/translate.ts +++ b/src/modules/translate.ts @@ -12,6 +12,10 @@ const text = { "⚠️仍处于获取目录模式", "⚠️Fetch TOC Mode is still enabled", ], + hint_addon_enabled: [ + "注意:该插件启用时,选择文本或笔记均会唤醒Obsidian", + "Note: With addon enabled, selecting text or notes will open Obsidian", + ], off: ["⛔️关闭", "⛔️Disable "], on: ["✅开启", "✅Enable "], disabled: ["已关闭", " Disabled"], diff --git a/src/togglePlugin.ts b/src/togglePlugin.ts index c17e34a..c1e49ed 100644 --- a/src/togglePlugin.ts +++ b/src/togglePlugin.ts @@ -29,7 +29,7 @@ export const togglePlugin = (sender: NotifySender) => { if (!self[addonOnName]) { toggleAddon(); - showHUD(gt("addon") + gt("enabled")); + showHUD(gt("hint_addon_enabled")); } else UIAlertView.showWithTitleMessageStyleCancelButtonTitleOtherButtonTitlesTapBlock( gt("toggle_title"), diff --git a/tsconfig.json b/tsconfig.json index cfdcccf..87e4b61 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,11 +11,7 @@ "resolveJsonModule": true, "importHelpers": true, "strict": true, - "lib": [ - "ES2020" - ] + "lib": ["ES2020"] }, - "include": [ - "src/**/*.ts", - ] + "include": ["src/**/*.ts"] }