Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toolbar popups rendered outside the visible area #1698

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions apps/editor/src/ui/components/popup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExecCommand, HidePopup, PopupInfo, Pos } from '@t/ui';
import { Emitter } from '@t/event';
import { closest, cls } from '@/utils/dom';
import { shallowEqual } from '@/utils/common';
import html from '../vdom/template';
import { Component } from '../vdom/component';

Expand All @@ -16,7 +17,13 @@ interface Props {
execCommand: ExecCommand;
}

export class Popup extends Component<Props> {
interface State {
popupPos: Pos | null;
}

const MARGIN_FROM_RIGHT_SIDE = 20;

export class Popup extends Component<Props, State> {
private handleMousedown = (ev: MouseEvent) => {
if (
!closest(ev.target as HTMLElement, `.${cls('popup')}`) &&
Expand All @@ -34,18 +41,39 @@ export class Popup extends Component<Props> {
document.removeEventListener('mousedown', this.handleMousedown);
}

render() {
const { info, show, hidePopup, eventEmitter, execCommand } = this.props;
const { className = '', style, render, pos, initialValues = {} } = info || {};
const popupStyle: PopupStyle = { display: show ? 'block' : 'none', ...style };
updated(prevProps: Props) {
const { show, info } = this.props;

if (pos) {
popupStyle.left = pos.left;
popupStyle.top = pos.top;
if (show && info.pos && prevProps.show !== show) {
const popupPos = { ...info.pos };
const { offsetWidth } = this.refs.el;
const toolbarEl = closest(this.refs.el, `.${cls('toolbar')}`) as HTMLElement;
const { offsetWidth: toolbarOffsetWidth } = toolbarEl;

if (popupPos.left + offsetWidth >= toolbarOffsetWidth) {
popupPos.left = toolbarOffsetWidth - offsetWidth - MARGIN_FROM_RIGHT_SIDE;
}
if (!shallowEqual(this.state.popupPos, popupPos)) {
this.setState({ popupPos });
}
}
}

render() {
const { info, show, hidePopup, eventEmitter, execCommand } = this.props;
const { className = '', style, render, initialValues = {} } = info || {};
const popupStyle: PopupStyle = {
display: show ? 'block' : 'none',
...style,
...this.state.popupPos,
};

return html`
<div class="${cls('popup')} ${className}" style=${popupStyle}>
<div
class="${cls('popup')} ${className}"
style=${popupStyle}
ref=${(el: HTMLElement) => (this.refs.el = el)}
>
<div class="${cls('popup-body')}">
${render && render({ eventEmitter, show, hidePopup, execCommand, initialValues })}
</div>
Expand Down
2 changes: 1 addition & 1 deletion apps/editor/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function isNil(value: unknown): value is null | undefined {
return isNull(value) || isUndefined(value);
}

export function shallowEqual(o1: Record<string, any>, o2: Record<string, any>) {
export function shallowEqual(o1: Record<string, any> | null, o2: Record<string, any> | null) {
if (o1 === null && o1 === o2) {
return true;
}
Expand Down