From 10796ce537857c29d2da56e621f02cc570af17ff Mon Sep 17 00:00:00 2001 From: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com> Date: Wed, 12 Jul 2023 23:27:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(problems):=20=E8=AE=A1=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E9=80=82=E9=85=8D=E7=81=B5=E5=8A=A8=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/content/pages/problems/App.tsx | 5 +++ src/content/pages/problems/DynamicLayout.tsx | 42 ++++++++++++++++++++ src/content/pages/problems/Timer.tsx | 20 ++++++++-- src/content/pages/problems/utils.ts | 11 ++++- 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/content/pages/problems/DynamicLayout.tsx diff --git a/src/content/pages/problems/App.tsx b/src/content/pages/problems/App.tsx index b7012ee..6e9a154 100644 --- a/src/content/pages/problems/App.tsx +++ b/src/content/pages/problems/App.tsx @@ -5,6 +5,7 @@ import { isBetaUI } from '@/utils' import Beta from './Beta' import Legacy from './Legacy' import { useEffectMount } from '@/hooks' +import DynamicLayout from './DynamicLayout' const App: FC<{ beta?: boolean }> = () => { const [beta, setBeta] = useState() @@ -14,6 +15,10 @@ const App: FC<{ beta?: boolean }> = () => { if (!state.isMount) return setBeta(beta) }, []) + // TODO: 这个判断在第一次进入灵动布局时不会生效,需要刷新页面才能生效 + if (localStorage.getItem('dynamicLayoutGuide') === 'true') { + return + } if (beta === undefined) return null diff --git a/src/content/pages/problems/DynamicLayout.tsx b/src/content/pages/problems/DynamicLayout.tsx new file mode 100644 index 0000000..5574b2a --- /dev/null +++ b/src/content/pages/problems/DynamicLayout.tsx @@ -0,0 +1,42 @@ +import { FC, useState } from 'react' + +import { useAppSelector, useObserverAncestor } from '@/hooks' + +import { selectOptions } from '../global/optionsSlice' +import Timer from './Timer' +import { getRoot } from './utils' + +const DynamicLayout: FC<{ beta?: boolean }> = () => { + const options = useAppSelector(selectOptions) + const [timerRoot, setTimerRoot] = useState() + + const showTimer = !!options?.problemsPage.timer + + useObserverAncestor( + async state => { + if (!showTimer) return + const parent = await getRoot(true) + + // 创建「计时器」按钮根元素 + if (!parent || !state.isMount) return + + const root = document.createElement('div') + parent.style.display = 'flex' + parent.append(root) + setTimerRoot(root) + state.unmount.push(() => root && root.remove()) + return root! + }, + [showTimer] + ) + + return ( + <> + {showTimer && timerRoot && ( + + )} + + ) +} + +export default DynamicLayout diff --git a/src/content/pages/problems/Timer.tsx b/src/content/pages/problems/Timer.tsx index a3c31bc..354c025 100644 --- a/src/content/pages/problems/Timer.tsx +++ b/src/content/pages/problems/Timer.tsx @@ -6,6 +6,7 @@ import { LeetCodeApi, SuccessCheckReturnType, findElement, + findElementByXPath, } from '@/utils' import { useEvent, useHover, useObserverAncestor } from '@/hooks' import { ToolTip } from '@/components/ToolTip' @@ -76,9 +77,10 @@ const Button = styled.button<{ interface TimerProps { beta?: boolean root?: HTMLElement + dynamicLayout?: boolean } -const Timer: FC = ({ beta, root }) => { +const Timer: FC = ({ beta, root, dynamicLayout }) => { const pathnames = location.pathname.split('/').filter(Boolean) const slug = pathnames[1] @@ -267,7 +269,11 @@ const Timer: FC = ({ beta, root }) => { const getSubMitBtn = async () => { let submitBtn: HTMLElement - if (beta) { + if (dynamicLayout) { + const xpath = "//span[text()='提交']" + submitBtn = await findElementByXPath(xpath) + submitBtn = submitBtn.parentElement! + } else if (beta) { const parent = await getRoot() if (parent) { submitBtn = [...parent.children].slice(-1)[0] as HTMLElement @@ -405,7 +411,10 @@ const Timer: FC = ({ beta, root }) => { style={{ display: 'flex', height: beta ? 33 : 35 }} > {!hidden && hover && ( - +
= ({ beta, root }) => {
)} - +