1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00
joplin/packages/pdf-viewer/messageService.ts

36 lines
845 B
TypeScript

export default class MessageService {
private viewerType: string;
public constructor(type: string) {
this.viewerType = type;
}
private sendMessage = (name: string, data?: any) => {
if (this.viewerType === 'full') {
const message = {
name,
...data,
};
window.postMessage(message, '*');
} else if (this.viewerType === 'mini') {
const message = {
name,
data,
target: 'webview',
};
window.parent.postMessage(message, '*');
}
};
public textSelected = (text: string) => {
this.sendMessage('textSelected', { text });
};
public close = () => {
this.sendMessage('close');
};
public openExternalViewer = () => {
this.sendMessage('externalViewer');
};
public openFullScreenViewer = (resourceId: string, pageNo: number) => {
this.sendMessage('openPdfViewer', { resourceId, pageNo });
};
}