1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/packages/lib/services/plugins/api/JoplinViewsDialogs.ts

111 lines
3.6 KiB
TypeScript

import Plugin from '../Plugin';
import createViewHandle from '../utils/createViewHandle';
import WebviewController, { ContainerType } from '../WebviewController';
import { ButtonSpec, ViewHandle, DialogResult } from './types';
import { _ } from '@joplin/lib/locale';
/**
* Allows creating and managing dialogs. A dialog is modal window that
* contains a webview and a row of buttons. You can update the
* webview using the `setHtml` method. Dialogs are hidden by default and
* you need to call `open()` to open them. Once the user clicks on a
* button, the `open` call will return an object indicating what button was
* clicked on.
*
* ## Retrieving form values
*
* If your HTML content included one or more forms, a `formData` object
* will also be included with the key/value for each form.
*
* ## Special button IDs
*
* The following buttons IDs have a special meaning:
*
* - `ok`, `yes`, `submit`, `confirm`: They are considered "submit" buttons
* - `cancel`, `no`, `reject`: They are considered "dismiss" buttons
*
* This information is used by the application to determine what action
* should be done when the user presses "Enter" or "Escape" within the
* dialog. If they press "Enter", the first "submit" button will be
* automatically clicked. If they press "Escape" the first "dismiss" button
* will be automatically clicked.
*
* [View the demo
* plugin](https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/dialog)
*/
export default class JoplinViewsDialogs {
private store: any;
private plugin: Plugin;
private implementation_: any;
constructor(implementation: any, plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
this.implementation_ = implementation;
}
private controller(handle: ViewHandle): WebviewController {
return this.plugin.viewController(handle) as WebviewController;
}
/**
* Creates a new dialog
*/
async create(id: string): Promise<ViewHandle> {
if (!id) {
this.plugin.deprecationNotice('1.5', 'Creating a view without an ID is deprecated. To fix it, change your call to `joplin.views.dialogs.create("my-unique-id")`', true);
id = `${this.plugin.viewCount}`;
}
const handle = createViewHandle(this.plugin, id);
const controller = new WebviewController(handle, this.plugin.id, this.store, this.plugin.baseDir, ContainerType.Dialog);
this.plugin.addViewController(controller);
return handle;
}
/**
* Displays a message box with OK/Cancel buttons. Returns the button index that was clicked - "0" for OK and "1" for "Cancel"
*/
async showMessageBox(message: string): Promise<number> {
return this.implementation_.showMessageBox(`${_('(In plugin: %s)', this.plugin.manifest.name)}\n\n${message}`);
}
/**
* Sets the dialog HTML content
*/
async setHtml(handle: ViewHandle, html: string) {
return this.controller(handle).html = html;
}
/**
* Adds and loads a new JS or CSS files into the dialog.
*/
public async addScript(handle: ViewHandle, scriptPath: string) {
return this.controller(handle).addScript(scriptPath);
}
/**
* Sets the dialog buttons.
*/
async setButtons(handle: ViewHandle, buttons: ButtonSpec[]) {
return this.controller(handle).buttons = buttons;
}
/**
* Opens the dialog
*/
async open(handle: ViewHandle): Promise<DialogResult> {
return this.controller(handle).open();
}
/**
* Toggle on whether to fit the dialog size to the content or not.
* When set to false, the dialog is set to 90vw and 80vh
* @default true
*/
async setFitToContent(handle: ViewHandle, status: boolean) {
return this.controller(handle).fitToContent = status;
}
}