2020-11-16 18:14:26 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useRef, useCallback } from 'react';
|
2020-11-13 20:48:42 +02:00
|
|
|
import { ButtonSpec, DialogResult } from '@joplin/lib/services/plugins/api/types';
|
2020-11-07 17:59:37 +02:00
|
|
|
import PluginService from '@joplin/lib/services/plugins/PluginService';
|
|
|
|
import WebviewController from '@joplin/lib/services/plugins/WebviewController';
|
2020-10-09 19:35:46 +02:00
|
|
|
import UserWebview, { Props as UserWebviewProps } from './UserWebview';
|
|
|
|
import UserWebviewDialogButtonBar from './UserWebviewDialogButtonBar';
|
|
|
|
const styled = require('styled-components').default;
|
|
|
|
|
|
|
|
interface Props extends UserWebviewProps {
|
|
|
|
buttons: ButtonSpec[];
|
2021-08-18 13:09:45 +02:00
|
|
|
fitToContent: boolean;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const StyledRoot = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex: 1;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
background-color: rgba(0,0,0,0.5);
|
|
|
|
box-sizing: border-box;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Dialog = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2020-11-12 21:13:28 +02:00
|
|
|
background-color: ${(props: any) => props.theme.backgroundColor};
|
|
|
|
padding: ${(props: any) => `${props.theme.mainPadding}px`};
|
2020-10-09 19:35:46 +02:00
|
|
|
border-radius: 4px;
|
|
|
|
box-shadow: 0 6px 10px #00000077;
|
2021-08-18 13:09:45 +02:00
|
|
|
width: ${(props: any) => props.fitToContent ? 'auto' : '90vw'};
|
|
|
|
height: ${(props: any) => props.fitToContent ? 'auto' : '80vh'};
|
2020-10-09 19:35:46 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const UserWebViewWrapper = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex: 1;
|
|
|
|
`;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function defaultButtons(): ButtonSpec[] {
|
2020-10-09 19:35:46 +02:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: 'ok',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'cancel',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:14:26 +02:00
|
|
|
function findSubmitButton(buttons: ButtonSpec[]): ButtonSpec | null {
|
|
|
|
return buttons.find((b: ButtonSpec) => {
|
|
|
|
return ['ok', 'yes', 'confirm', 'submit'].includes(b.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function findDismissButton(buttons: ButtonSpec[]): ButtonSpec | null {
|
|
|
|
return buttons.find((b: ButtonSpec) => {
|
|
|
|
return ['cancel', 'no', 'reject'].includes(b.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function UserWebviewDialog(props: Props) {
|
2020-11-13 20:48:42 +02:00
|
|
|
const webviewRef = useRef(null);
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function viewController(): WebviewController {
|
2020-10-09 19:35:46 +02:00
|
|
|
return PluginService.instance().pluginById(props.pluginId).viewController(props.viewId) as WebviewController;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const buttons: ButtonSpec[] = (props.buttons ? props.buttons : defaultButtons()).map((b: ButtonSpec) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
return {
|
|
|
|
...b,
|
|
|
|
onClick: () => {
|
2020-11-13 20:48:42 +02:00
|
|
|
const response: DialogResult = { id: b.id };
|
|
|
|
const formData = webviewRef.current.formData();
|
|
|
|
if (formData && Object.keys(formData).length) response.formData = formData;
|
|
|
|
viewController().closeWithResponse(response);
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-11-16 18:14:26 +02:00
|
|
|
const onSubmit = useCallback(() => {
|
|
|
|
const submitButton = findSubmitButton(buttons);
|
|
|
|
if (submitButton) {
|
|
|
|
submitButton.onClick();
|
|
|
|
}
|
|
|
|
}, [buttons]);
|
|
|
|
|
|
|
|
const onDismiss = useCallback(() => {
|
|
|
|
const dismissButton = findDismissButton(buttons);
|
|
|
|
if (dismissButton) {
|
|
|
|
dismissButton.onClick();
|
|
|
|
}
|
|
|
|
}, [buttons]);
|
|
|
|
|
2021-02-07 11:39:56 +02:00
|
|
|
const onReady = useCallback(() => {
|
|
|
|
// We focus the dialog once it's ready to make sure that the ESC/Enter
|
|
|
|
// keyboard shortcuts are working.
|
|
|
|
// https://github.com/laurent22/joplin/issues/4474
|
|
|
|
if (webviewRef.current) webviewRef.current.focus();
|
|
|
|
}, []);
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
return (
|
|
|
|
<StyledRoot>
|
2021-08-18 13:09:45 +02:00
|
|
|
<Dialog fitToContent={props.fitToContent}>
|
2020-10-09 19:35:46 +02:00
|
|
|
<UserWebViewWrapper>
|
|
|
|
<UserWebview
|
2020-11-13 20:48:42 +02:00
|
|
|
ref={webviewRef}
|
2020-10-09 19:35:46 +02:00
|
|
|
html={props.html}
|
|
|
|
scripts={props.scripts}
|
|
|
|
pluginId={props.pluginId}
|
|
|
|
viewId={props.viewId}
|
|
|
|
themeId={props.themeId}
|
|
|
|
borderBottom={false}
|
2021-08-18 13:09:45 +02:00
|
|
|
fitToContent={props.fitToContent}
|
2020-11-16 18:14:26 +02:00
|
|
|
onSubmit={onSubmit}
|
|
|
|
onDismiss={onDismiss}
|
2021-02-07 11:39:56 +02:00
|
|
|
onReady={onReady}
|
2020-10-09 19:35:46 +02:00
|
|
|
/>
|
|
|
|
</UserWebViewWrapper>
|
|
|
|
<UserWebviewDialogButtonBar buttons={buttons}/>
|
|
|
|
</Dialog>
|
|
|
|
</StyledRoot>
|
|
|
|
);
|
|
|
|
}
|