Skip to content

Commit

Permalink
feat: support book info output
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Apr 29, 2021
1 parent fab7f02 commit f81e4eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "AidenLx",
"license": "MIT",
"devDependencies": {
"@alx-plugins/marginnote": "^0.5.4",
"@alx-plugins/marginnote": "^0.5.7",
"@release-it/bumper": "^2.0.0",
"@release-it/conventional-changelog": "^2.0.1",
"@rollup/plugin-commonjs": "^18.0.0",
Expand Down
20 changes: 16 additions & 4 deletions src/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ export function onPopupMenuOnNote(sender: NotifySender) {
if (self.recorder.isDuplicate(Date.now()))
return;

const src = sender.userInfo.note;
const srcNote = sender.userInfo.note;
let currentBook;

if (!srcNote) {
showHUD("no note in sender");
return;
}

if (srcNote.docMd5)
currentBook= Database.sharedInstance().getDocumentById(srcNote.docMd5);

try {
copy(stringify(src, self.recorder));
copy(stringify(srcNote,self.recorder,currentBook));
} catch (error) {
showHUD(error.toString());
}
Expand All @@ -36,10 +46,12 @@ export function onPopupMenuOnSelection(sender: NotifySender) {
self.recorder = new PopupRecorder();
}

const selection = sender.userInfo.documentController.selectionText;
const { selectionText: selection, document: currentBook } = sender.userInfo
.documentController as DocumentController;

try {
if (selection && selection.length) {
copy(stringify({ sel: selection }, self.recorder));
copy(stringify({ sel: selection }, self.recorder, currentBook));
}
} catch (error) {
showHUD(error.toString());
Expand Down
7 changes: 4 additions & 3 deletions src/modules/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import { node, ReturnBody } from "./return";
import { scanObject } from "./tools";


function process(node: node, rec: PopupRecorder): ReturnBody {
function process(node: node, rec: PopupRecorder, currentBook?: MbBook): ReturnBody {
let data = isSel(node) ? node : scanObject(node, 2);
const last = rec.last;
rec.push(data);
const sendTime = rec.last.addTime as number;
return {
type: isSel(node) ? "sel" : "note",
sendTime,
currentBook: currentBook ? scanObject(currentBook) : undefined,
data,
last,
};
}

export function stringify<T extends node>(node: T, rec: PopupRecorder): string {
return "<!--MN-->\n" + JSON.stringify(process(node, rec));
export function stringify<T extends node>(node: T, rec: PopupRecorder, currentBook?: MbBook): string {
return "<!--MN-->\n" + JSON.stringify(process(node, rec, currentBook));
}

function isSel(node: node): node is selection {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/return.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export type node = selection | MbBookNote;
type ReturnBody_Basic = {
type: "sel" | "note";
sendTime: ReturnType<typeof Date.now>;
last: item;
currentBook?: MbBook;
data: node;
last: item;
};

export type ReturnBody = ReturnBody_Note | ReturnBody_Sel;
Expand Down

0 comments on commit f81e4eb

Please sign in to comment.