[Perf] Get rid of slow body.no-selection
This commit is contained in:
parent
cd055732ba
commit
ebb6f710d9
@ -2,7 +2,6 @@ import React, {
|
|||||||
FC, memo, useCallback, useEffect, useRef,
|
FC, memo, useCallback, useEffect, useRef,
|
||||||
} from '../../../lib/teact/teact';
|
} from '../../../lib/teact/teact';
|
||||||
|
|
||||||
import { IS_TOUCH_ENV } from '../../../util/environment';
|
|
||||||
import buildClassName from '../../../util/buildClassName';
|
import buildClassName from '../../../util/buildClassName';
|
||||||
import findInViewport from '../../../util/findInViewport';
|
import findInViewport from '../../../util/findInViewport';
|
||||||
import isFullyVisible from '../../../util/isFullyVisible';
|
import isFullyVisible from '../../../util/isFullyVisible';
|
||||||
@ -90,14 +89,6 @@ const EmojiTooltip: FC<OwnProps> = ({
|
|||||||
setItemVisible(selectedIndex, containerRef);
|
setItemVisible(selectedIndex, containerRef);
|
||||||
}, [selectedIndex]);
|
}, [selectedIndex]);
|
||||||
|
|
||||||
const handleMouseEnter = () => {
|
|
||||||
document.body.classList.add('no-select');
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleMouseLeave = () => {
|
|
||||||
document.body.classList.remove('no-select');
|
|
||||||
};
|
|
||||||
|
|
||||||
const className = buildClassName(
|
const className = buildClassName(
|
||||||
'EmojiTooltip composer-tooltip custom-scroll-x',
|
'EmojiTooltip composer-tooltip custom-scroll-x',
|
||||||
transitionClassNames,
|
transitionClassNames,
|
||||||
@ -107,8 +98,6 @@ const EmojiTooltip: FC<OwnProps> = ({
|
|||||||
<div
|
<div
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
className={className}
|
className={className}
|
||||||
onMouseEnter={!IS_TOUCH_ENV ? handleMouseEnter : undefined}
|
|
||||||
onMouseLeave={!IS_TOUCH_ENV ? handleMouseLeave : undefined}
|
|
||||||
>
|
>
|
||||||
{shouldRender && listEmojis ? (
|
{shouldRender && listEmojis ? (
|
||||||
listEmojis.map((emoji, index) => (
|
listEmojis.map((emoji, index) => (
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import { getDispatch, withGlobal } from '../../../lib/teact/teactn';
|
|||||||
import { ApiSticker } from '../../../api/types';
|
import { ApiSticker } from '../../../api/types';
|
||||||
|
|
||||||
import { STICKER_SIZE_PICKER } from '../../../config';
|
import { STICKER_SIZE_PICKER } from '../../../config';
|
||||||
import { IS_TOUCH_ENV } from '../../../util/environment';
|
|
||||||
import buildClassName from '../../../util/buildClassName';
|
import buildClassName from '../../../util/buildClassName';
|
||||||
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
||||||
import { useIntersectionObserver } from '../../../hooks/useIntersectionObserver';
|
import { useIntersectionObserver } from '../../../hooks/useIntersectionObserver';
|
||||||
@ -54,18 +53,10 @@ const StickerTooltip: FC<OwnProps & StateProps> = ({
|
|||||||
|
|
||||||
useEffect(() => (isOpen ? captureEscKeyListener(clearStickersForEmoji) : undefined), [isOpen, clearStickersForEmoji]);
|
useEffect(() => (isOpen ? captureEscKeyListener(clearStickersForEmoji) : undefined), [isOpen, clearStickersForEmoji]);
|
||||||
|
|
||||||
const handleMouseEnter = () => {
|
|
||||||
document.body.classList.add('no-select');
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleMouseMove = () => {
|
const handleMouseMove = () => {
|
||||||
sendMessageAction({ type: 'chooseSticker' });
|
sendMessageAction({ type: 'chooseSticker' });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseLeave = () => {
|
|
||||||
document.body.classList.remove('no-select');
|
|
||||||
};
|
|
||||||
|
|
||||||
const className = buildClassName(
|
const className = buildClassName(
|
||||||
'StickerTooltip composer-tooltip custom-scroll',
|
'StickerTooltip composer-tooltip custom-scroll',
|
||||||
transitionClassNames,
|
transitionClassNames,
|
||||||
@ -76,8 +67,6 @@ const StickerTooltip: FC<OwnProps & StateProps> = ({
|
|||||||
<div
|
<div
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
className={className}
|
className={className}
|
||||||
onMouseEnter={!IS_TOUCH_ENV ? handleMouseEnter : undefined}
|
|
||||||
onMouseLeave={!IS_TOUCH_ENV ? handleMouseLeave : undefined}
|
|
||||||
onMouseMove={handleMouseMove}
|
onMouseMove={handleMouseMove}
|
||||||
>
|
>
|
||||||
{shouldRender && displayedStickers ? (
|
{shouldRender && displayedStickers ? (
|
||||||
|
|||||||
@ -56,10 +56,9 @@ const useContextMenuHandlers = (
|
|||||||
|
|
||||||
const handleContextMenuHide = useCallback(() => {
|
const handleContextMenuHide = useCallback(() => {
|
||||||
setContextMenuPosition(undefined);
|
setContextMenuPosition(undefined);
|
||||||
document.body.classList.remove('no-selection');
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Support context menu on touch-devices
|
// Support context menu on touch devices
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isMenuDisabled || !IS_TOUCH_ENV || shouldDisableOnLongTap) {
|
if (isMenuDisabled || !IS_TOUCH_ENV || shouldDisableOnLongTap) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -102,7 +101,6 @@ const useContextMenuHandlers = (
|
|||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body.classList.add('no-selection');
|
|
||||||
setIsContextMenuOpen(true);
|
setIsContextMenuOpen(true);
|
||||||
setContextMenuPosition({ x: clientX, y: clientY });
|
setContextMenuPosition({ x: clientX, y: clientY });
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,12 +2,12 @@ import { RefObject } from 'react';
|
|||||||
import { useState, useEffect } from '../lib/teact/teact';
|
import { useState, useEffect } from '../lib/teact/teact';
|
||||||
import useFlag from './useFlag';
|
import useFlag from './useFlag';
|
||||||
|
|
||||||
export const useResize = (
|
export function useResize(
|
||||||
elementRef: RefObject<HTMLElement>,
|
elementRef: RefObject<HTMLElement>,
|
||||||
onResize: (width: number) => void,
|
onResize: (width: number) => void,
|
||||||
onReset: NoneToVoidFunction,
|
onReset: NoneToVoidFunction,
|
||||||
initialWidth?: number,
|
initialWidth?: number,
|
||||||
) => {
|
) {
|
||||||
const [isActive, markIsActive, unmarkIsActive] = useFlag();
|
const [isActive, markIsActive, unmarkIsActive] = useFlag();
|
||||||
const [initialMouseX, setInitialMouseX] = useState<number>();
|
const [initialMouseX, setInitialMouseX] = useState<number>();
|
||||||
const [initialElementWidth, setInitialElementWidth] = useState<number>();
|
const [initialElementWidth, setInitialElementWidth] = useState<number>();
|
||||||
@ -21,19 +21,21 @@ export const useResize = (
|
|||||||
}, [elementRef, initialWidth]);
|
}, [elementRef, initialWidth]);
|
||||||
|
|
||||||
function handleMouseUp() {
|
function handleMouseUp() {
|
||||||
document.body.classList.remove('no-selection', 'cursor-ew-resize');
|
document.body.classList.remove('cursor-ew-resize');
|
||||||
}
|
}
|
||||||
|
|
||||||
function initResize(event: React.MouseEvent<HTMLElement, MouseEvent>) {
|
function initResize(e: React.MouseEvent<HTMLElement, MouseEvent>) {
|
||||||
document.body.classList.add('no-selection', 'cursor-ew-resize');
|
e.preventDefault();
|
||||||
|
|
||||||
setInitialMouseX(event.clientX);
|
document.body.classList.add('cursor-ew-resize');
|
||||||
|
|
||||||
|
setInitialMouseX(e.clientX);
|
||||||
setInitialElementWidth(elementRef.current!.offsetWidth);
|
setInitialElementWidth(elementRef.current!.offsetWidth);
|
||||||
markIsActive();
|
markIsActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetResize(event: React.MouseEvent<HTMLElement, MouseEvent>) {
|
function resetResize(e: React.MouseEvent<HTMLElement, MouseEvent>) {
|
||||||
event.preventDefault();
|
e.preventDefault();
|
||||||
elementRef.current!.style.width = '';
|
elementRef.current!.style.width = '';
|
||||||
onReset();
|
onReset();
|
||||||
}
|
}
|
||||||
@ -41,8 +43,8 @@ export const useResize = (
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isActive) return undefined;
|
if (!isActive) return undefined;
|
||||||
|
|
||||||
const handleMouseMove = (event: MouseEvent) => {
|
const handleMouseMove = (e: MouseEvent) => {
|
||||||
const newWidth = Math.ceil(initialElementWidth + event.clientX - initialMouseX);
|
const newWidth = Math.ceil(initialElementWidth + e.clientX - initialMouseX);
|
||||||
elementRef.current!.style.width = `${newWidth}px`;
|
elementRef.current!.style.width = `${newWidth}px`;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -67,4 +69,4 @@ export const useResize = (
|
|||||||
}, [initialElementWidth, initialMouseX, elementRef, onResize, isActive, unmarkIsActive]);
|
}, [initialElementWidth, initialMouseX, elementRef, onResize, isActive, unmarkIsActive]);
|
||||||
|
|
||||||
return { initResize, resetResize, handleMouseUp };
|
return { initResize, resetResize, handleMouseUp };
|
||||||
};
|
}
|
||||||
|
|||||||
@ -94,6 +94,10 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
|
|||||||
captureEvent = e;
|
captureEvent = e;
|
||||||
|
|
||||||
if (e.type === 'mousedown') {
|
if (e.type === 'mousedown') {
|
||||||
|
if (options.onDrag) {
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('mousemove', onMove);
|
document.addEventListener('mousemove', onMove);
|
||||||
document.addEventListener('mouseup', onRelease);
|
document.addEventListener('mouseup', onRelease);
|
||||||
} else if (e.type === 'touchstart') {
|
} else if (e.type === 'touchstart') {
|
||||||
@ -120,7 +124,6 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body.classList.add('no-selection');
|
|
||||||
if (options.withCursor) {
|
if (options.withCursor) {
|
||||||
document.body.classList.add('cursor-grabbing');
|
document.body.classList.add('cursor-grabbing');
|
||||||
}
|
}
|
||||||
@ -135,7 +138,6 @@ export function captureEvents(element: HTMLElement, options: CaptureOptions) {
|
|||||||
if (options.withCursor) {
|
if (options.withCursor) {
|
||||||
document.body.classList.remove('cursor-grabbing');
|
document.body.classList.remove('cursor-grabbing');
|
||||||
}
|
}
|
||||||
document.body.classList.remove('no-selection');
|
|
||||||
|
|
||||||
document.removeEventListener('mouseup', onRelease);
|
document.removeEventListener('mouseup', onRelease);
|
||||||
document.removeEventListener('mousemove', onMove);
|
document.removeEventListener('mousemove', onMove);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user