Skip to content

Commit

Permalink
feat: now mediaList is included in the api
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed May 5, 2021
1 parent 5bab58c commit 65abaa1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/modules/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,40 @@ import { item, MNMark, node, ReturnBody, selection } from "./return";
import { scanObject } from "./tools";


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 as Exclude<item,null>).addTime;
return {
type: isSel(node) ? "sel" : "note",
sendTime,
currentBook: currentBook ? scanObject(currentBook) : undefined,
data,
last,
function process(node: node, rec: PopupRecorder, book?: MbBook): ReturnBody {

const currentBook = book ? scanObject(book) : undefined;

const getLastAndSendTime = (
data: node
): { sendTime: ReturnBody["sendTime"]; last: ReturnBody["last"] } => {
const last = rec.last;
rec.push(data);
const sendTime = (rec.last as Exclude<item, null>).addTime;
return { last, sendTime };
};

if (isSel(node)){
const data = node;
const { last, sendTime } = getLastAndSendTime(data);
const mediaList = null;
return { type: "sel", sendTime, currentBook, mediaList, data, last };
} else {
const data = scanObject(node, 2);
const { last, sendTime } = getLastAndSendTime(data);
const mediaList = [];
const mediaIds = node.mediaList?.split('-');
if (mediaIds && mediaIds.length > 1) {
for (const id of mediaIds) {
if (!id) continue; // escape empty string
const mediaData = Database.sharedInstance()
.getMediaByHash(id)
?.base64Encoding();
if (mediaData) mediaList.push({ id, data: mediaData });
}
}
return { type: "note", sendTime, currentBook, mediaList, data, last };
}
}

const MNMark: MNMark = "<!--MN-->\n";
Expand Down
9 changes: 9 additions & 0 deletions src/modules/return.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ export type node = selection | MbBookNote;

export type MNMark = "<!--MN-->\n";

type Media = {
id: string;
/** encoded in base64 */
data: string;
};

type ReturnBody_Basic = {
type: "sel" | "note";
sendTime: ReturnType<typeof Date.now>;
currentBook?: MbBook;
mediaList: Media[] | null;
data: node;
last: item | null;
};
Expand All @@ -25,9 +32,11 @@ export type ReturnBody = ReturnBody_Note | ReturnBody_Sel;
export interface ReturnBody_Sel extends ReturnBody_Basic {
type: "sel";
data: selection;
mediaList: null;
}

export interface ReturnBody_Note extends ReturnBody_Basic {
type: "note"
data: MbBookNote;
mediaList: Array<{ id: string; data: string }>;
}

0 comments on commit 65abaa1

Please sign in to comment.