Skip to content

Commit

Permalink
Fix IFrame Provider issues (#47)
Browse files Browse the repository at this point in the history
* fix small issues on IFrame Provider and CrossWindow Provider

* add missing changes from last commit

* remove setAddress override

* 0.3.0-alpha.1

* fix iframe provider position and logout behavior

* 0.3.0-alpha.2

* changelog update
  • Loading branch information
CiprianDraghici authored Aug 2, 2024
1 parent d690029 commit 18b0e37
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 148 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Fix IFrame Provider issues](https://github.com/multiversx/mx-wallet-dapp/pull/47)
- [Implement IFrameProvider](https://github.com/multiversx/mx-wallet-dapp/pull/46)

## [[0.1.3](https://github.com/multiversx/mx-wallet-dapp/pull/45)] - 2024-05-29
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-web-wallet-cross-window-provider",
"version": "0.3.0-alpha.0",
"version": "0.3.0-alpha.2",
"description": "Signing provider for dApps: Cross Window",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
7 changes: 1 addition & 6 deletions src/CrossWindowProvider/CrossWindowProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ErrCouldNotLogin,
ErrCouldNotSignMessage,
ErrCouldNotSignTransactions,
ErrInstantiationFailed,
ErrProviderNotInitialized,
ErrTransactionCancelled
} from '../errors';
Expand Down Expand Up @@ -67,12 +66,8 @@ export class CrossWindowProvider {
}

public setAddress(address: string): CrossWindowProvider {
if (!CrossWindowProvider._instance) {
throw new ErrInstantiationFailed();
}

this.account.address = address;
return CrossWindowProvider._instance;
return this;
}

public setWalletUrl(url: string): CrossWindowProvider {
Expand Down
109 changes: 17 additions & 92 deletions src/IFrameManager/IFrameProviderContentWindow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { safeDocument, safeWindow } from '../constants';
import { safeDocument } from '../constants';
import {
bodyStyle,
closeWalletButtonStyle,
collapsibleButtonStyle,
containerStyle,
headerStyle,
Expand All @@ -17,17 +16,8 @@ export class IFrameProviderContentWindow {
private readonly header: HTMLDivElement;
private readonly body: HTMLDivElement;

private readonly onClose: (() => void) | undefined = undefined;

public constructor(props: {
id: string;
url: string;
anchor?: HTMLElement;
onClose?: () => void;
}) {
const { id, url, anchor, onClose } = props;

this.onClose = onClose;
public constructor(props: { id: string; url: string; anchor?: HTMLElement }) {
const { id, url, anchor } = props;

this.container = safeDocument.createElement?.('div');
this.header = safeDocument.createElement?.('div');
Expand All @@ -36,7 +26,6 @@ export class IFrameProviderContentWindow {

this.buildWindow(id, url);
this.contentWindow = this.iframe.contentWindow;

this.setupWindow();

if (anchor) {
Expand All @@ -47,35 +36,26 @@ export class IFrameProviderContentWindow {
}

private buildWindow(id: string, url: string) {
this.container.setAttribute('data-draggable', 'true');
this.container.setAttribute('data-resizable', 'true');
this.header.setAttribute('data-drag-handle', 'true');
this.container.id = `window-container-${id}`;
this.iframe.id = id;
this.iframe.src = url;

this.container.style.cssText = containerStyle;
this.header.style.cssText = headerStyle;
this.body.style.cssText = bodyStyle;

const { collapsibleButton, closeWalletButton } = this.getHeaderButtons();

this.header.appendChild(closeWalletButton);
this.header.appendChild(collapsibleButton);
this.container.appendChild(this.header);
this.container.appendChild(this.body);

this.iframe.id = id;
this.iframe.src = url;
this.iframe.style.cssText = iframeStyle;

this.body.appendChild(this.iframe);
this.buildContainer();
}

private getHeaderButtons() {
private buildHeader() {
const title = safeDocument.createElement?.('span');
title.innerText = 'Wallet';
this.header.appendChild(title);

const collapsibleButton = safeDocument.createElement?.('span');
collapsibleButton.id = 'iframe-toggle-button';
collapsibleButton.innerText = '+';
collapsibleButton.innerText = '-';
collapsibleButton.style.cssText = collapsibleButtonStyle;
collapsibleButton.onclick = () => {
this.body.style.visibility =
Expand All @@ -89,16 +69,14 @@ export class IFrameProviderContentWindow {
this.body.style.visibility === 'hidden' ? 'none' : 'both'
);
};
this.header.appendChild(collapsibleButton);
}

const closeWalletButton = safeDocument.createElement?.('span');
closeWalletButton.id = 'iframe-close-button';
closeWalletButton.innerText = '✖';
closeWalletButton.style.cssText = closeWalletButtonStyle;
closeWalletButton.onclick = () => {
this.container.remove();
this.onClose?.();
};
return { collapsibleButton, closeWalletButton };
private buildContainer() {
this.container.appendChild(this.header);
this.container.appendChild(this.body);
this.body.appendChild(this.iframe);
this.buildHeader();
}

private setupWindow() {
Expand All @@ -111,9 +89,6 @@ export class IFrameProviderContentWindow {

this.iframe.dispatchEvent(event);
};

this.setupResizable();
this.setupDraggable();
}

public getContainer(): HTMLDivElement {
Expand Down Expand Up @@ -146,54 +121,4 @@ export class IFrameProviderContentWindow {
): void {
this.iframe.addEventListener(type, listener);
}

private setupResizable() {
this.container.style.setProperty('resize', 'both');
this.container.style.setProperty('overflow', 'hidden');
}

private setupDraggable() {
this.container.ondragstart = () => {
return false;
};

this.header.onmousedown = (event) => {
const shiftX =
event.clientX - this.container.getBoundingClientRect().left;
const shiftY = event.clientY - this.container.getBoundingClientRect().top;

// moves the window at (pageX, pageY) coordinates
// taking initial shifts into account
const moveAt = (pageX: number, pageY: number) => {
this.container.style.left = pageX - shiftX + 'px';
this.container.style.top = pageY - shiftY + 'px';
};

this.container.style.position = 'absolute';
this.container.style.zIndex = '1000';

moveAt(event.pageX, event.pageY);

const onMouseMove = (ev: MouseEvent) => {
moveAt(ev.pageX, ev.pageY);
safeWindow.getSelection()?.removeAllRanges();
};

// move the container on mousemove
safeDocument.addEventListener?.('mousemove', onMouseMove);

// drop the container, remove unneeded handlers
this.container.onmouseup = () => {
safeDocument.removeEventListener?.('mousemove', onMouseMove);
this.container.onmouseup = null;
};

// drop the header, remove unneeded handlers
this.header.onmouseup = () => {
safeDocument.removeEventListener?.('mousemove', onMouseMove);
this.header.removeEventListener('mouseup', onMouseMove);
this.header.onmouseup = null;
};
};
}
}
16 changes: 4 additions & 12 deletions src/IFrameManager/IframeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import { IFrameProviderContentWindow } from './IFrameProviderContentWindow';

export class IframeManager extends WindowManager {
private floatingWalletComponent: IFrameProviderContentWindow | null = null;
private readonly onClose: (() => void) | undefined = undefined;

constructor(props?: { onClose?: () => void }) {
super();
this.onClose = props?.onClose;
}
private readonly iframeId = 'floating-wallet';

public get floatingWallet() {
return this.floatingWalletComponent;
Expand Down Expand Up @@ -66,13 +61,9 @@ export class IframeManager extends WindowManager {

const anchor = safeDocument.getElementById?.('root');
this.floatingWalletComponent = new IFrameProviderContentWindow({
id: 'floating-wallet',
id: this.iframeId,
anchor,
url: this.walletUrl,
onClose: () => {
this.closeConnection();
this.onClose?.();
}
url: this.walletUrl
});
this.floatingWalletComponent.walletAddress = this.walletUrl;

Expand All @@ -92,6 +83,7 @@ export class IframeManager extends WindowManager {
}

this.walletWindow = iframe.contentWindow;
this.setWalletVisible(true);
}

public setWalletVisible(visible: boolean): void {
Expand Down
32 changes: 4 additions & 28 deletions src/IFrameManager/iframeManager.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export const containerStyle = `
border: 1px solid #0d0e10;
border-radius: 5px;
z-index: 9999;
position: absolute;
position: fixed;
bottom: 0;
right: 0;
left: 2rem;
visibility: hidden;
padding-bottom: 10px;
`;

export const headerStyle = `
Expand Down Expand Up @@ -51,29 +52,4 @@ export const collapsibleButtonStyle = `
z-index: 50;
user-select: none;
float: right;
`;

export const closeWalletButtonStyle = `
width: 30px;
height: 30px;
cursor: pointer;
margin-top: 0px;
display: inline-block;
cursor: pointer;
margin-top: 5px;
padding: 0px;
box-sizing: content-box;
font-family: sans-serif;
text-align: center;
font-size: 30px;
line-height: 14px;
border-width: 0px;
border-radius: 2px;
border-color: rgb(170, 170, 170);
border-style: solid;
background-color: transparent;
color: gray;
z-index: 50;
user-select: none;
float: right;
`;
`;
16 changes: 9 additions & 7 deletions src/IFrameProvider/IframeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { IframeManager } from '../IFrameManager/IframeManager';
export class IframeProvider extends CrossWindowProvider {
public constructor() {
super();
this.windowManager = new IframeManager({
// TODO check why the logout is not sending logout request through postMessage
onClose: this.logout.bind(this)
});
this.windowManager = new IframeManager();
}

public static getInstance(): IframeProvider {
Expand Down Expand Up @@ -46,12 +43,17 @@ export class IframeProvider extends CrossWindowProvider {
throw new ErrProviderNotInitialized();
}

this.ensureConnected();
const connectionClosed = await this.windowManager.closeConnection();
try {
this.ensureConnected();
await this.windowManager.closeConnection();
} catch (e) {
console.error(e);
}

this.initialized = false;
this.disconnect();

return connectionClosed;
return true;
}

public override async openPopupConsent(): Promise<boolean> {
Expand Down

0 comments on commit 18b0e37

Please sign in to comment.