This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 822
Modal Widgets - MSC2790 #5252
Merged
Merged
Modal Widgets - MSC2790 #5252
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
342f1d5
Extremely bad support for "temporary widgets"
turt2live 44bc8fc
Initial Modal Widget work tweaks MSC2790
t3chguy 6919a82
i18n
t3chguy fbbba75
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy e3b0bf1
Fix url encoding issue for modal widgets widgetId
t3chguy 30d2e61
Continuation
t3chguy 13d09df
tidy
t3chguy 290ef5e
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy 00b1a03
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy 1636244
Iterate Modal Widgets
t3chguy b6fd2a0
Iterate the Modal Widget Dialog paddings and phishing warning
t3chguy 24c4a38
Increase Border Radius of Accessible Buttons to 8px
t3chguy 12bc3ea
remove stale comments
t3chguy 0004dd4
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy cf93f75
fix sandbox flags
t3chguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright 2020 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
.mx_ModalWidgetDialog { | ||
.mx_ModalWidgetDialog_warning { | ||
margin-bottom: 24px; | ||
|
||
> img { | ||
vertical-align: middle; | ||
margin-right: 8px; | ||
} | ||
} | ||
|
||
.mx_ModalWidgetDialog_buttons { | ||
float: right; | ||
margin-top: 24px; | ||
|
||
.mx_AccessibleButton + .mx_AccessibleButton { | ||
margin-left: 8px; | ||
} | ||
} | ||
|
||
iframe { | ||
width: 100%; | ||
height: 450px; | ||
border: 0; | ||
border-radius: 8px; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
Copyright 2020 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import BaseDialog from './BaseDialog'; | ||
import { _t } from '../../../languageHandler'; | ||
import AccessibleButton from "../elements/AccessibleButton"; | ||
import { | ||
ClientWidgetApi, | ||
IModalWidgetCloseRequest, | ||
IModalWidgetOpenRequestData, | ||
IModalWidgetReturnData, | ||
ModalButtonKind, | ||
Widget, | ||
WidgetApiFromWidgetAction, | ||
} from "matrix-widget-api"; | ||
import {StopGapWidgetDriver} from "../../../stores/widgets/StopGapWidgetDriver"; | ||
import {MatrixClientPeg} from "../../../MatrixClientPeg"; | ||
import RoomViewStore from "../../../stores/RoomViewStore"; | ||
import {OwnProfileStore} from "../../../stores/OwnProfileStore"; | ||
|
||
interface IProps { | ||
widgetDefinition: IModalWidgetOpenRequestData; | ||
sourceWidgetId: string; | ||
onFinished(success: boolean, data?: IModalWidgetReturnData): void; | ||
} | ||
|
||
interface IState { | ||
messaging?: ClientWidgetApi; | ||
} | ||
|
||
const MAX_BUTTONS = 3; | ||
|
||
export default class ModalWidgetDialog extends React.PureComponent<IProps, IState> { | ||
private readonly widget: Widget; | ||
private appFrame: React.RefObject<HTMLIFrameElement> = React.createRef(); | ||
|
||
state: IState = {}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.widget = new Widget({ | ||
...this.props.widgetDefinition, | ||
creatorUserId: MatrixClientPeg.get().getUserId(), | ||
id: `modal_${this.props.sourceWidgetId}`, | ||
}); | ||
} | ||
|
||
public componentDidMount() { | ||
const driver = new StopGapWidgetDriver( []); | ||
const messaging = new ClientWidgetApi(this.widget, this.appFrame.current, driver); | ||
this.setState({messaging}); | ||
} | ||
|
||
public componentWillUnmount() { | ||
this.state.messaging.off("ready", this.onReady); | ||
this.state.messaging.off(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose); | ||
this.state.messaging.stop(); | ||
} | ||
|
||
private onReady = () => { | ||
this.state.messaging.sendWidgetConfig(this.props.widgetDefinition); | ||
}; | ||
|
||
private onLoad = () => { | ||
this.state.messaging.once("ready", this.onReady); | ||
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose); | ||
}; | ||
|
||
private onWidgetClose = (ev: CustomEvent<IModalWidgetCloseRequest>) => { | ||
this.props.onFinished(true, ev.detail.data); | ||
} | ||
|
||
public render() { | ||
const templated = this.widget.getCompleteUrl({ | ||
currentRoomId: RoomViewStore.getRoomId(), | ||
currentUserId: MatrixClientPeg.get().getUserId(), | ||
userDisplayName: OwnProfileStore.instance.displayName, | ||
userHttpAvatarUrl: OwnProfileStore.instance.getHttpAvatarUrl(), | ||
}); | ||
|
||
const parsed = new URL(templated); | ||
|
||
// Add in some legacy support sprinkles (for non-popout widgets) | ||
// TODO: Replace these with proper widget params | ||
// See https://github.com/matrix-org/matrix-doc/pull/1958/files#r405714833 | ||
parsed.searchParams.set('widgetId', this.widget.id); | ||
parsed.searchParams.set('parentUrl', window.location.href.split('#', 2)[0]); | ||
|
||
// Replace the encoded dollar signs back to dollar signs. They have no special meaning | ||
// in HTTP, but URL parsers encode them anyways. | ||
const widgetUrl = parsed.toString().replace(/%24/g, '$'); | ||
|
||
let buttons; | ||
if (this.props.widgetDefinition.buttons) { | ||
// show first button rightmost for a more natural specification | ||
buttons = this.props.widgetDefinition.buttons.slice(0, MAX_BUTTONS).reverse().map(def => { | ||
let kind = "secondary"; | ||
switch (def.kind) { | ||
case ModalButtonKind.Primary: | ||
kind = "primary"; | ||
break; | ||
case ModalButtonKind.Secondary: | ||
kind = "primary_outline"; | ||
break | ||
case ModalButtonKind.Danger: | ||
kind = "danger"; | ||
break; | ||
} | ||
|
||
const onClick = () => { | ||
this.state.messaging.notifyModalWidgetButtonClicked(def.id); | ||
}; | ||
|
||
return <AccessibleButton key={def.id} kind={kind} onClick={onClick}> | ||
{ def.label } | ||
</AccessibleButton>; | ||
}); | ||
} | ||
|
||
return <BaseDialog | ||
title={this.props.widgetDefinition.name || _t("Modal Widget")} | ||
className="mx_ModalWidgetDialog" | ||
contentId="mx_Dialog_content" | ||
onFinished={this.props.onFinished} | ||
> | ||
<div className="mx_ModalWidgetDialog_warning"> | ||
<img | ||
src={require("../../../../res/img/element-icons/warning-badge.svg")} | ||
height="16" | ||
width="16" | ||
alt="" | ||
/> | ||
{_t("Data on this screen is shared with %(widgetDomain)s", { | ||
widgetDomain: parsed.hostname, | ||
})} | ||
</div> | ||
<div> | ||
<iframe | ||
ref={this.appFrame} | ||
sandbox="allow-forms allow-scripts allow-same-origin" | ||
src={widgetUrl} | ||
onLoad={this.onLoad} | ||
/> | ||
</div> | ||
<div className="mx_ModalWidgetDialog_buttons"> | ||
{ buttons } | ||
</div> | ||
</BaseDialog>; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2020 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { AsyncStoreWithClient } from "./AsyncStoreWithClient"; | ||
import defaultDispatcher from "../dispatcher/dispatcher"; | ||
import { ActionPayload } from "../dispatcher/payloads"; | ||
import Modal, {IHandle, IModal} from "../Modal"; | ||
import ModalWidgetDialog from "../components/views/dialogs/ModalWidgetDialog"; | ||
import {WidgetMessagingStore} from "./widgets/WidgetMessagingStore"; | ||
import {IModalWidgetOpenRequestData, IModalWidgetReturnData, Widget} from "matrix-widget-api"; | ||
|
||
interface IState { | ||
modal?: IModal<any>; | ||
openedFromId?: string; | ||
} | ||
|
||
export class ModalWidgetStore extends AsyncStoreWithClient<IState> { | ||
private static internalInstance = new ModalWidgetStore(); | ||
private modalInstance: IHandle<void[]> = null; | ||
private openSourceWidgetId: string = null; | ||
|
||
private constructor() { | ||
super(defaultDispatcher, {}); | ||
} | ||
|
||
public static get instance(): ModalWidgetStore { | ||
return ModalWidgetStore.internalInstance; | ||
} | ||
|
||
protected async onAction(payload: ActionPayload): Promise<any> { | ||
// nothing | ||
} | ||
|
||
public canOpenModalWidget = () => { | ||
return !this.modalInstance; | ||
}; | ||
|
||
public openModalWidget = (requestData: IModalWidgetOpenRequestData, sourceWidget: Widget) => { | ||
if (this.modalInstance) return; | ||
this.openSourceWidgetId = sourceWidget.id; | ||
this.modalInstance = Modal.createTrackedDialog('Modal Widget', '', ModalWidgetDialog, { | ||
widgetDefinition: {...requestData}, | ||
sourceWidgetId: sourceWidget.id, | ||
onFinished: (success: boolean, data?: IModalWidgetReturnData) => { | ||
if (!success) { | ||
this.closeModalWidget(sourceWidget, { "m.exited": true }); | ||
} else { | ||
this.closeModalWidget(sourceWidget, data); | ||
} | ||
|
||
this.openSourceWidgetId = null; | ||
this.modalInstance = null; | ||
}, | ||
}); | ||
}; | ||
|
||
public closeModalWidget = (sourceWidget: Widget, data?: IModalWidgetReturnData) => { | ||
if (!this.modalInstance) return; | ||
if (this.openSourceWidgetId === sourceWidget.id) { | ||
this.openSourceWidgetId = null; | ||
this.modalInstance.close(); | ||
this.modalInstance = null; | ||
|
||
const sourceMessaging = WidgetMessagingStore.instance.getMessaging(sourceWidget); | ||
if (!sourceMessaging) { | ||
console.error("No source widget messaging for modal widget"); | ||
return; | ||
} | ||
sourceMessaging.notifyModalWidgetClose(data); | ||
} | ||
}; | ||
} | ||
|
||
window.mxModalWidgetStore = ModalWidgetStore.instance; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly for my notes: a lot of this is copied from SGWidget, but that's going away with the stuff I'm doing. Will need to refactor this a bit.