1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-27 23:28:38 +02:00

Plugins: Allow posting messages from plugin to webview (#5569)

This commit is contained in:
agerardin
2021-11-09 10:50:50 -05:00
committed by GitHub
parent 200ba858dd
commit 6b31609338
8 changed files with 126 additions and 19 deletions

View File

@ -2,6 +2,7 @@ import ViewController, { EmitMessageEvent } from './ViewController';
import shim from '../../shim';
import { ButtonSpec, DialogResult, ViewHandle } from './api/types';
const { toSystemSlashes } = require('../../path-utils');
import PostMessageService, { MessageParticipant } from '../PostMessageService';
export enum ContainerType {
Panel = 'panel',
@ -103,7 +104,24 @@ export default class WebviewController extends ViewController {
});
}
public postMessage(message: any) {
const messageId = `plugin_${Date.now()}${Math.random()}`;
void PostMessageService.instance().postMessage({
pluginId: this.pluginId,
viewId: this.handle,
contentScriptId: null,
from: MessageParticipant.Plugin,
to: MessageParticipant.UserWebview,
id: messageId,
content: message,
});
}
public async emitMessage(event: EmitMessageEvent): Promise<any> {
if (!this.messageListener_) return;
return this.messageListener_(event.message);
}