From c89037b4a28be1a8f33ad963e8e3e0f73df37602 Mon Sep 17 00:00:00 2001 From: Ahmad Mamdouh Date: Thu, 22 Jul 2021 11:21:57 +0200 Subject: [PATCH] Plugins: Add ability to make dialogs fit the application window (#5219) --- .../tests/support/plugins/dialog/src/index.ts | 15 +++++++++++++++ .../app-desktop/gui/MainScreen/MainScreen.tsx | 1 + .../app-desktop/services/plugins/UserWebview.tsx | 4 ++-- .../services/plugins/UserWebviewDialog.tsx | 3 ++- .../lib/services/plugins/WebviewController.ts | 8 ++++++++ .../services/plugins/api/JoplinViewsDialogs.ts | 9 +++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/packages/app-cli/tests/support/plugins/dialog/src/index.ts b/packages/app-cli/tests/support/plugins/dialog/src/index.ts index 747732a4a..3a9f32722 100644 --- a/packages/app-cli/tests/support/plugins/dialog/src/index.ts +++ b/packages/app-cli/tests/support/plugins/dialog/src/index.ts @@ -42,6 +42,21 @@ joplin.plugins.register({ const result3 = await dialogs.open(handle3); console.info('Got result: ' + JSON.stringify(result3)); + + const handle4 = await dialogs.create('myDialog4'); + await dialogs.setHtml(handle4, ` +

This dialog tests dynamic sizing

+

Resize the window and the dialog should resize accordingly

+

+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. + Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum +

+ `); + await (dialogs as any).fitToContent(handle4, false); + await dialogs.open(handle4); + }, }); diff --git a/packages/app-desktop/gui/MainScreen/MainScreen.tsx b/packages/app-desktop/gui/MainScreen/MainScreen.tsx index 6d912be97..77d9ce01d 100644 --- a/packages/app-desktop/gui/MainScreen/MainScreen.tsx +++ b/packages/app-desktop/gui/MainScreen/MainScreen.tsx @@ -779,6 +779,7 @@ class MainScreenComponent extends React.Component { scripts={view.scripts} pluginId={plugin.id} buttons={view.buttons} + fitToContent={view.fitToContent} />); } diff --git a/packages/app-desktop/services/plugins/UserWebview.tsx b/packages/app-desktop/services/plugins/UserWebview.tsx index 9f6791b0b..dacfa222a 100644 --- a/packages/app-desktop/services/plugins/UserWebview.tsx +++ b/packages/app-desktop/services/plugins/UserWebview.tsx @@ -31,8 +31,8 @@ export interface Props { const StyledFrame = styled.iframe` padding: 0; margin: 0; - width: ${(props: any) => props.fitToContent ? `${props.width}px` : '100%'}; - height: ${(props: any) => props.fitToContent ? `${props.height}px` : '100%'}; + width: ${(props: any) => props.fitToContent ? `${props.width}px` : '90vw'}; + height: ${(props: any) => props.fitToContent ? `${props.height}px` : '80vh'}; border: none; border-bottom: ${(props: Props) => props.borderBottom ? `1px solid ${props.theme.dividerColor}` : 'none'}; `; diff --git a/packages/app-desktop/services/plugins/UserWebviewDialog.tsx b/packages/app-desktop/services/plugins/UserWebviewDialog.tsx index ff13a45e4..7d906a967 100644 --- a/packages/app-desktop/services/plugins/UserWebviewDialog.tsx +++ b/packages/app-desktop/services/plugins/UserWebviewDialog.tsx @@ -9,6 +9,7 @@ const styled = require('styled-components').default; interface Props extends UserWebviewProps { buttons: ButtonSpec[]; + fitToContent: boolean; } const StyledRoot = styled.div` @@ -113,7 +114,7 @@ export default function UserWebviewDialog(props: Props) { viewId={props.viewId} themeId={props.themeId} borderBottom={false} - fitToContent={true} + fitToContent={props.fitToContent} onSubmit={onSubmit} onDismiss={onDismiss} onReady={onReady} diff --git a/packages/lib/services/plugins/WebviewController.ts b/packages/lib/services/plugins/WebviewController.ts index dfc243f88..aca19ee10 100644 --- a/packages/lib/services/plugins/WebviewController.ts +++ b/packages/lib/services/plugins/WebviewController.ts @@ -58,6 +58,7 @@ export default class WebviewController extends ViewController { scripts: [], opened: false, buttons: null, + fitToContent: true, }, }); } @@ -173,4 +174,11 @@ export default class WebviewController extends ViewController { this.setStoreProp('buttons', buttons); } + public get fitToContent(): boolean { + return this.storeView.fitToContent; + } + + public set fitToContent(fitToContent: boolean) { + this.setStoreProp('fitToContent', fitToContent); + } } diff --git a/packages/lib/services/plugins/api/JoplinViewsDialogs.ts b/packages/lib/services/plugins/api/JoplinViewsDialogs.ts index ae7aa32fb..e0e01351c 100644 --- a/packages/lib/services/plugins/api/JoplinViewsDialogs.ts +++ b/packages/lib/services/plugins/api/JoplinViewsDialogs.ts @@ -98,4 +98,13 @@ export default class JoplinViewsDialogs { return this.controller(handle).open(); } + /** + * Toggle on whether to fit the dialog size to the content or not. + * When set to false, the dialog stretches to fill the application + * window. + * @default true + */ + public async setFitToContent(handle: ViewHandle, status: boolean) { + return this.controller(handle).fitToContent = status; + } }