Skip to content

Commit

Permalink
feat(api): api export Date.getTime() for Date
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Aug 23, 2021
1 parent 249ce49 commit 13e4129
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 46 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand Down
17 changes: 7 additions & 10 deletions 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
Expand All @@ -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",
}),
],
};
6 changes: 3 additions & 3 deletions src/modules/parser.ts
Expand Up @@ -2,6 +2,7 @@ import { excerptPic_video, MbBook, MbBookNote } from "@alx-plugins/marginnote";

import PopupRecorder from "./PopupRecorder";
import {
Book,
Data,
ReturnBody,
ReturnBody_Note,
Expand All @@ -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 = (
Expand Down
24 changes: 19 additions & 5 deletions 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<MbBook>;
type note_valOnly = NonTypeProps<MbBookNote, Function>;
type note_dateCvt = DateCvt<note_valOnly>;
export type Note = NonTypeProps<
note_dateCvt,
MbBookNote | MbBookNote[] | undefined
> &
{
[P in TypePropNames<note_dateCvt, MbBookNote | undefined>]?: Note;
} &
{ [P in TypePropNames<note_dateCvt, MbBookNote[]>]: Note[] };

export type Selection = { sel: string; book?: MbBook };
export type inHistory = Data | null;
export type time = number | null;
Expand All @@ -8,7 +22,7 @@ export type item = {
addTime: Exclude<time, null>;
} | null;

export type Data = Selection | MbBookNote | Toc;
export type Data = Selection | Note | Toc;
export type DataType = "sel" | "note" | "toc";

export type MNMark = "<!--MN-->\n";
Expand All @@ -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<string, string>;
bookMap: Record<string, MbBook>;
bookMap: Record<string, Book>;
}

export interface ReturnBody_Toc extends ReturnBody_Basic {
type: "toc";
data: Toc;
bookMap: Record<string, MbBook>;
bookMap: Record<string, Book>;
}
export interface Toc {
noteTitle: string;
Expand Down
16 changes: 10 additions & 6 deletions 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
Expand Down Expand Up @@ -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<string> = 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 (
Expand Down Expand Up @@ -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])
Expand Down
26 changes: 6 additions & 20 deletions 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)));
13 changes: 13 additions & 0 deletions src/modules/optional.d.ts → src/modules/type-tools.d.ts
Expand Up @@ -13,3 +13,16 @@ export type OptionalKeys<T> = Exclude<
NonNullable<keyof SubType<Undefined<T>, never>>
>;
export type RequiredKeys<T> = NonNullable<keyof SubType<Undefined<T>, never>>;

type NonTypePropNames<T, Target> = {
[K in NonNullable<keyof T>]: T[K] extends Target ? never : K;
}[NonNullable<keyof T>];
export type NonTypeProps<T, Target> = Pick<T, NonTypePropNames<T, Target>>;
export type TypePropNames<T, Target> = {
[K in NonNullable<keyof T>]: T[K] extends Target ? K : never;
}[NonNullable<keyof T>];
type TypeProps<T, Target> = Pick<T, TypePropNames<T, Target>>;
export type DateCvt<T> = NonTypeProps<T, Date | undefined> &
{
[P in TypePropNames<T, Date | undefined>]?: number;
};
26 changes: 26 additions & 0 deletions 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();
};

0 comments on commit 13e4129

Please sign in to comment.