Skip to content

Commit

Permalink
feat: send to obsidian via url in favor of clipboard
Browse files Browse the repository at this point in the history
fix sel.book return empty
  • Loading branch information
aidenlx committed Aug 23, 2021
1 parent e3e7582 commit 25ccae1
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 53 deletions.
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -35,14 +37,16 @@
"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",
"rollup-plugin-zip": "^1.0.2",
"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"
Expand Down
7 changes: 3 additions & 4 deletions src/event-handlers.ts
@@ -1,6 +1,5 @@
import {
ChangeExcerptRange_Sender,
DocumentController,
EventHandler,
NotifySender,
PopupMenuOnNote_Sender,
Expand All @@ -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 (
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/modules/const.ts
@@ -1,4 +1,4 @@
import { MNMark } from "./return";
export const PREFIX: MNMark = "<!--MN-->\n";
const version = "2.1.0";
export const VERSION: string = "v" + version;
export const VERSION: string = version;
51 changes: 17 additions & 34 deletions 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,
Expand All @@ -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 =
Expand All @@ -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"] } => {
Expand All @@ -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 = <V>(
Expand All @@ -56,7 +49,7 @@ const arrToObj = <V>(
return obj;
}, {} as any) as Record<string, V>;

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));
Expand All @@ -75,37 +68,27 @@ export const handleNote = (note: MbBookNote): void => {
})
: {};

const returns: ReturnBody_Note = {
version: VERSION,
return {
type: "note",
sendTime,
bookMap,
mediaMap,
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";
3 changes: 1 addition & 2 deletions src/modules/return.d.ts
Expand Up @@ -14,14 +14,13 @@ export type DataType = "sel" | "note" | "toc";
export type MNMark = "<!--MN-->\n";

type ReturnBody_Basic = {
version: string;
type: DataType;
sendTime: ReturnType<typeof Date.now>;
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";
Expand Down
7 changes: 3 additions & 4 deletions src/modules/scan.ts
Expand Up @@ -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<string> = new Set();
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions 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()));
};
4 changes: 4 additions & 0 deletions src/modules/translate.ts
Expand Up @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion src/togglePlugin.ts
Expand Up @@ -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"),
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Expand Up @@ -11,11 +11,7 @@
"resolveJsonModule": true,
"importHelpers": true,
"strict": true,
"lib": [
"ES2020"
]
"lib": ["ES2020"]
},
"include": [
"src/**/*.ts",
]
"include": ["src/**/*.ts"]
}

0 comments on commit 25ccae1

Please sign in to comment.