From 9041bae89f54efce14239768e642f99d1f0b35d1 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 4 Apr 2021 23:06:00 +0900 Subject: Make resize to a custom hook --- src/console/components/CommandPrompt.tsx | 17 +++-------------- src/console/components/FindPrompt.tsx | 13 +++---------- src/console/hooks/useAutoResize.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 src/console/hooks/useAutoResize.ts (limited to 'src/console') diff --git a/src/console/components/CommandPrompt.tsx b/src/console/components/CommandPrompt.tsx index 4e02668..f6f4d8f 100644 --- a/src/console/components/CommandPrompt.tsx +++ b/src/console/components/CommandPrompt.tsx @@ -6,12 +6,12 @@ import CommandLineParser, { InputPhase, } from "../commandline/CommandLineParser"; import Completion from "./console/Completion"; -import ConsoleFrameClient from "../clients/ConsoleFrameClient"; import Input from "./console//Input"; import { Command } from "../../shared/Command"; import styled from "styled-components"; import reducer, { defaultState, completedText } from "../reducers/completion"; import CompletionType from "../../shared/CompletionType"; +import useAutoResize from "../hooks/useAutoResize"; const COMPLETION_MAX_ITEMS = 33; @@ -26,7 +26,8 @@ const CommandPrompt: React.FC = () => { defaultState ); const commandLineParser = new CommandLineParser(); - const consoleFrameClient = new ConsoleFrameClient(); + + useAutoResize(); const onBlur = () => { dispatch(consoleActions.hideCommand()); @@ -106,12 +107,6 @@ const CommandPrompt: React.FC = () => { Promise.resolve(action).then((a) => { if (a) { completionDispatch(a); - - const { - scrollWidth: width, - scrollHeight: height, - } = document.getElementById("vimvixen-console")!; - consoleFrameClient.resize(width, height); } }); }; @@ -127,12 +122,6 @@ const CommandPrompt: React.FC = () => { Promise.resolve(completionAction).then((a) => { if (a) { completionDispatch(a); - - const { - scrollWidth: width, - scrollHeight: height, - } = document.getElementById("vimvixen-console")!; - consoleFrameClient.resize(width, height); } }); }); diff --git a/src/console/components/FindPrompt.tsx b/src/console/components/FindPrompt.tsx index c79e4d3..10fa6c3 100644 --- a/src/console/components/FindPrompt.tsx +++ b/src/console/components/FindPrompt.tsx @@ -1,9 +1,9 @@ import React from "react"; import * as consoleActions from "../../console/actions/console"; -import ConsoleFrameClient from "../clients/ConsoleFrameClient"; import AppContext from "./AppContext"; import Input from "./console/Input"; import styled from "styled-components"; +import useAutoResize from "../hooks/useAutoResize"; const ConsoleWrapper = styled.div` border-top: 1px solid gray; @@ -13,11 +13,12 @@ const FindPrompt: React.FC = () => { const { dispatch } = React.useContext(AppContext); const [inputValue, setInputValue] = React.useState(""); - const consoleFrameClient = new ConsoleFrameClient(); const onBlur = () => { dispatch(consoleActions.hideCommand()); }; + useAutoResize(); + const doEnter = (e: React.KeyboardEvent) => { e.stopPropagation(); e.preventDefault(); @@ -41,14 +42,6 @@ const FindPrompt: React.FC = () => { setInputValue(e.target.value); }; - React.useEffect(() => { - const { - scrollWidth: width, - scrollHeight: height, - } = document.getElementById("vimvixen-console")!; - consoleFrameClient.resize(width, height); - }, []); - return ( { + const [prevWidth, setPrevWidth] = React.useState(-1); + const [prevHeight, setPrevHeight] = React.useState(-1); + + const consoleFrameClient = React.useMemo(() => { + return new ConsoleFrameClient(); + }, []); + + React.useLayoutEffect(() => { + const { + scrollWidth: width, + scrollHeight: height, + } = document.getElementById("vimvixen-console")!; + consoleFrameClient.resize(width, height); + + if (width === prevWidth && height === prevHeight) { + return; + } + + setPrevWidth(width); + setPrevHeight(height); + }); +}; + +export default useAutoResize; -- cgit v1.2.3