2020-10-09 19:35:46 +02:00
import Plugin from '../Plugin' ;
import createViewHandle from '../utils/createViewHandle' ;
2020-11-13 19:09:28 +02:00
import WebviewController , { ContainerType } from '../WebviewController' ;
2020-10-09 19:35:46 +02:00
import { ViewHandle } from './types' ;
/ * *
2020-11-13 19:09:28 +02:00
* Allows creating and managing view panels . View panels currently are
* displayed at the right of the sidebar and allows displaying any HTML
* content ( within a webview ) and update it in real - time . For example it
* could be used to display a table of content for the active note , or
* display various metadata or graph .
2020-10-09 19:35:46 +02:00
*
2020-11-05 18:58:23 +02:00
* [ View the demo plugin ] ( https : //github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/toc)
2020-10-09 19:35:46 +02:00
* /
export default class JoplinViewsPanels {
private store : any ;
private plugin : Plugin ;
2021-01-02 15:32:15 +02:00
public constructor ( plugin : Plugin , store : any ) {
2020-10-09 19:35:46 +02:00
this . store = store ;
this . plugin = plugin ;
}
2020-11-12 21:13:28 +02:00
private controller ( handle : ViewHandle ) : WebviewController {
2020-10-09 19:35:46 +02:00
return this . plugin . viewController ( handle ) as WebviewController ;
}
/ * *
* Creates a new panel
* /
2021-01-02 15:32:15 +02:00
public async create ( id : string ) : Promise < ViewHandle > {
2020-11-13 19:09:28 +02:00
if ( ! id ) {
2021-08-05 13:02:03 +02:00
this . plugin . deprecationNotice ( '1.5' , 'Creating a view without an ID is deprecated. To fix it, change your call to `joplin.views.panels.create("my-unique-id")`' , true ) ;
2020-11-13 19:09:28 +02:00
id = ` ${ this . plugin . viewCount } ` ;
}
const handle = createViewHandle ( this . plugin , id ) ;
const controller = new WebviewController ( handle , this . plugin . id , this . store , this . plugin . baseDir , ContainerType . Panel ) ;
2020-10-09 19:35:46 +02:00
this . plugin . addViewController ( controller ) ;
return handle ;
}
/ * *
* Sets the panel webview HTML
* /
2021-01-02 15:32:15 +02:00
public async setHtml ( handle : ViewHandle , html : string ) {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . html = html ;
}
/ * *
* Adds and loads a new JS or CSS files into the panel .
* /
2021-01-02 15:32:15 +02:00
public async addScript ( handle : ViewHandle , scriptPath : string ) {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . addScript ( scriptPath ) ;
}
/ * *
* Called when a message is sent from the webview ( using postMessage ) .
2021-01-12 01:33:10 +02:00
*
* To post a message from the webview to the plugin use :
*
* ` ` ` javascript
* const response = await webviewApi . postMessage ( message ) ;
* ` ` `
*
* - ` message ` can be any JavaScript object , string or number
* - ` response ` is whatever was returned by the ` onMessage ` handler
*
* Using this mechanism , you can have two - way communication between the
* plugin and webview .
*
* See the [ postMessage
* demo ] ( https : //github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/post_messages) for more details.
*
2020-10-09 19:35:46 +02:00
* /
2021-01-02 15:32:15 +02:00
public async onMessage ( handle : ViewHandle , callback : Function ) {
2020-10-09 19:35:46 +02:00
return this . controller ( handle ) . onMessage ( callback ) ;
}
2021-01-02 15:32:15 +02:00
/ * *
* Shows the panel
* /
public async show ( handle : ViewHandle , show : boolean = true ) : Promise < void > {
await this . controller ( handle ) . show ( show ) ;
}
/ * *
* Hides the panel
* /
public async hide ( handle : ViewHandle ) : Promise < void > {
await this . show ( handle , false ) ;
}
/ * *
* Tells whether the panel is visible or not
* /
public async visible ( handle : ViewHandle ) : Promise < boolean > {
return this . controller ( handle ) . visible ;
}
2020-10-09 19:35:46 +02:00
}