From 13e4129dcaba0d34bfe14d7222e9cf9fe7e6892f Mon Sep 17 00:00:00 2001 From: AidenLx Date: Mon, 23 Aug 2021 17:12:57 +0800 Subject: [PATCH] feat(api): api export Date.getTime() for Date --- package.json | 6 +++-- rollup.config.js | 17 +++++------- src/modules/parser.ts | 6 ++--- src/modules/return.d.ts | 24 +++++++++++++---- src/modules/scan.ts | 16 +++++++----- src/modules/sender.ts | 26 +++++-------------- .../{optional.d.ts => type-tools.d.ts} | 13 ++++++++++ src/modules/url-obj.ts | 26 +++++++++++++++++++ 8 files changed, 88 insertions(+), 46 deletions(-) rename src/modules/{optional.d.ts => type-tools.d.ts} (50%) create mode 100644 src/modules/url-obj.ts diff --git a/package.json b/package.json index 490a868..29cc29e 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "author": "AidenLx", "license": "MIT", "devDependencies": { - "@alx-plugins/marginnote": "^1.8.3", + "@alx-plugins/marginnote": "^1.8.4", "@jsonurl/jsonurl": "^1.1.4", "@release-it/bumper": "^3.0.1", "@release-it/conventional-changelog": "^3.3.0", @@ -27,6 +27,7 @@ "@types/url-parse": "^1.4.4", "@typescript-eslint/eslint-plugin": "^4.29.0", "@typescript-eslint/parser": "^4.29.0", + "assert-never": "^1.2.1", "cz-conventional-changelog": "^3.3.0", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", @@ -36,6 +37,7 @@ "eslint-plugin-prefer-arrow": "^1.2.3", "eslint-plugin-prettier": "^3.4.0", "eslint-plugin-simple-import-sort": "^7.0.0", + "obsidian": "^0.12.11", "prettier": "^2.3.2", "query-string": "^6.14.1", "release-it": "^14.11.5", @@ -49,7 +51,7 @@ "url-parse": "^1.5.3" }, "peerDependencies": { - "@alx-plugins/marginnote": "^1.8.3" + "@alx-plugins/marginnote": "^1.8.4" }, "release-it": { "hooks": { diff --git a/rollup.config.js b/rollup.config.js index ac426e6..97e7ef7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,8 +1,8 @@ +import commonjs from "@rollup/plugin-commonjs"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; import typescript from "@rollup/plugin-typescript"; import copy from "rollup-plugin-copy2"; -import zip from 'rollup-plugin-zip'; -import {nodeResolve} from '@rollup/plugin-node-resolve'; -import commonjs from '@rollup/plugin-commonjs'; +import zip from "rollup-plugin-zip"; const banner = `/* THIS IS A GENERATED/BUNDLED FILE BY ROLLUP @@ -21,16 +21,13 @@ export default { }, plugins: [ typescript(), - nodeResolve({browser: true}), + nodeResolve({ browser: true }), commonjs(), copy({ - assets: [ - "mnaddon.json", - ["assets/title.png","title.png"] - ] + assets: ["mnaddon.json", ["assets/title.png", "title.png"]], }), zip({ - file: "ob-bridge.mnaddon" - }) + file: "ob-bridge.mnaddon", + }), ], }; diff --git a/src/modules/parser.ts b/src/modules/parser.ts index a0185ae..1529bb7 100644 --- a/src/modules/parser.ts +++ b/src/modules/parser.ts @@ -2,6 +2,7 @@ import { excerptPic_video, MbBook, MbBookNote } from "@alx-plugins/marginnote"; import PopupRecorder from "./PopupRecorder"; import { + Book, Data, ReturnBody, ReturnBody_Note, @@ -10,14 +11,13 @@ import { Selection, } from "./return"; import { scanNote, scanObject, scanToc } from "./scan"; -import { showHUD } from "./tools"; -const getBook = (docMd5: string | undefined): MbBook | null => { +const getBook = (docMd5: string | undefined): Book | null => { const bookObj = docMd5 && typeof docMd5 === "string" ? Database.sharedInstance().getDocumentById(docMd5) : null; - return bookObj ? scanObject(bookObj) : null; + return bookObj ? (scanObject(bookObj) as Book) : null; }; const getLastAndSendTime = ( diff --git a/src/modules/return.d.ts b/src/modules/return.d.ts index 33dc3e6..80000de 100644 --- a/src/modules/return.d.ts +++ b/src/modules/return.d.ts @@ -1,5 +1,19 @@ import { MbBook, MbBookNote } from "@alx-plugins/marginnote"; +import { DateCvt, NonTypeProps, TypePropNames } from "./type-tools"; + +export type Book = DateCvt; +type note_valOnly = NonTypeProps; +type note_dateCvt = DateCvt; +export type Note = NonTypeProps< + note_dateCvt, + MbBookNote | MbBookNote[] | undefined +> & + { + [P in TypePropNames]?: Note; + } & + { [P in TypePropNames]: Note[] }; + export type Selection = { sel: string; book?: MbBook }; export type inHistory = Data | null; export type time = number | null; @@ -8,7 +22,7 @@ export type item = { addTime: Exclude; } | null; -export type Data = Selection | MbBookNote | Toc; +export type Data = Selection | Note | Toc; export type DataType = "sel" | "note" | "toc"; export type MNMark = "\n"; @@ -25,21 +39,21 @@ export type ReturnBody = ReturnBody_Note | ReturnBody_Sel | ReturnBody_Toc; export interface ReturnBody_Sel extends ReturnBody_Basic { type: "sel"; data: Selection; - book?: MbBook; + book?: Book; } export interface ReturnBody_Note extends ReturnBody_Basic { type: "note"; - data: MbBookNote; + data: Note; /** id - base64(png) pair */ mediaMap: Record; - bookMap: Record; + bookMap: Record; } export interface ReturnBody_Toc extends ReturnBody_Basic { type: "toc"; data: Toc; - bookMap: Record; + bookMap: Record; } export interface Toc { noteTitle: string; diff --git a/src/modules/scan.ts b/src/modules/scan.ts index ac6aa47..8760bba 100644 --- a/src/modules/scan.ts +++ b/src/modules/scan.ts @@ -1,7 +1,7 @@ import { MbBookNote } from "@alx-plugins/marginnote"; -import { RequiredKeys } from "./optional"; -import { Toc } from "./return"; +import { Note, Toc } from "./return"; +import { RequiredKeys } from "./type-tools"; /** * @returns when note missing params, return error message @@ -67,17 +67,19 @@ export const scanToc = (note: MbBookNote): [note: Toc, bookMd5s: string[]] => { export const scanNote = ( note: MbBookNote, depth = 1, -): [note: MbBookNote, bookMd5s: string[]] => { +): [note: Note, bookMd5s: string[]] => { let bookMd5s: Set = new Set(); const generalScan = getScanFunc(depth), - scan = (obj: MbBookNote, dive?: boolean, accu: number = 0): MbBookNote => { + scan = (obj: MbBookNote, dive?: boolean, accu: number = 0): Note => { let out: any = {}; for (const k of getAllProperties(obj)) { const key = k as keyof MbBookNote; let value; if (accu < depth) { - if (key === "docMd5" && obj.docMd5) { + if (obj[key] instanceof Date) { + value = (obj[key] as Date).getTime(); + } else if (key === "docMd5" && obj.docMd5) { bookMd5s.add(obj.docMd5); value = obj.docMd5; } else if ( @@ -128,7 +130,9 @@ const getScanFunc = (depth: number) => { for (const key of getAllProperties(obj)) { let value; if (accu < depth) { - if ( + if (obj[key] instanceof Date) { + value = (obj[key] as Date).getTime(); + } else if ( key === "parentNote" && (dive === undefined || !dive) && isMbBookNote(obj[key]) diff --git a/src/modules/sender.ts b/src/modules/sender.ts index d2f88d3..1dc3ad4 100644 --- a/src/modules/sender.ts +++ b/src/modules/sender.ts @@ -1,35 +1,21 @@ 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"; +import { ObjToUrl } from "./url-obj"; export const SendToc = (src: MbBookNote): void => { try { - toURL(getBody_Toc(src)); + ObjToUrl(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)); +export const SendSel = (src: Selection): void => send(getBody_Sel(src)); +export const SendNote = (src: MbBookNote): void => send(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())); -}; +const send = (body: ReturnBody) => + Application.sharedInstance().openURL(NSURL.URLWithString(ObjToUrl(body))); diff --git a/src/modules/optional.d.ts b/src/modules/type-tools.d.ts similarity index 50% rename from src/modules/optional.d.ts rename to src/modules/type-tools.d.ts index 92c0366..c3e8ef1 100644 --- a/src/modules/optional.d.ts +++ b/src/modules/type-tools.d.ts @@ -13,3 +13,16 @@ export type OptionalKeys = Exclude< NonNullable, never>> >; export type RequiredKeys = NonNullable, never>>; + +type NonTypePropNames = { + [K in NonNullable]: T[K] extends Target ? never : K; +}[NonNullable]; +export type NonTypeProps = Pick>; +export type TypePropNames = { + [K in NonNullable]: T[K] extends Target ? K : never; +}[NonNullable]; +type TypeProps = Pick>; +export type DateCvt = NonTypeProps & + { + [P in TypePropNames]?: number; + }; diff --git a/src/modules/url-obj.ts b/src/modules/url-obj.ts new file mode 100644 index 0000000..9c3cd41 --- /dev/null +++ b/src/modules/url-obj.ts @@ -0,0 +1,26 @@ +import JsonURL from "@jsonurl/jsonurl"; +import assertNever from "assert-never"; +import { ObsidianProtocolData } from "obsidian"; +import { stringify as toQs } from "query-string"; +import URLParse from "url-parse"; + +import { VERSION } from "./const"; +import { DataType, ReturnBody } from "./return"; +import { copy } from "./tools"; + +export const ObjToUrl = (obj: ReturnBody) => { + // copy(JSON.stringify(obj)); + const url = URLParse("obsidian://mncomp"), + { type, sendTime, last, data } = obj, + qsObj: QsObj = { + version: VERSION, + type, + sendTime, + last: JsonURL.stringify(last) as string, + data: JsonURL.stringify(data) as string, + }, + qs = toQs(qsObj); + url.set("query", qs); + return url.toString(); +}; +