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: add width caculate #120

Merged
merged 6 commits into from
Apr 17, 2024
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: 18 additions & 28 deletions src/content/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import IconButton from '@mui/material/IconButton';
import { styled } from '@mui/material/styles';

import * as toaster from '../../components/Toaster';
import { XFANS_CONTENT_WIDTH, XFANS_MIN_WIDTH } from '../../constants';
import { XFANS_CONTENT_WIDTH } from '../../constants';
import { ProfileData } from '../../service/login/me';
import { TwitterOauth2Data } from '../../service/login/twiterOuth2';
import http, { ResultData } from '../../service/request';
Expand All @@ -18,9 +18,9 @@ import ProfileModal from '../../welcome/Wallet/Profile';
import CongratulationPage from '../loginPage/congratulationPage';
import InvitePage from '../loginPage/invitePage';
import SignInWithXPage from '../loginPage/signInWithXPage';
import { getElementRightByXPath } from '../../utils';
import { caculateBackWidth, caculateDrawerWidth } from '../../utils';
import LogoButton from './logoButton';
import { XFANS_CHECK_RETWEET, XFANS_TOKEN, OAUTH2, XFANS_TWITTER_HOMEPAGE } from '../../constants';
import { XFANS_TOKEN } from '../../constants';
import '../../tailwind.css';

const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })<{
Expand Down Expand Up @@ -54,6 +54,8 @@ export default function PersistentDrawerRight() {
const { isShowDrawer, goPage, page, logout } = useGlobalStore((state) => ({ ...state }));

const [loginLoading, setLoginLoading] = React.useState(false);
const [drawerWidth, setDrawerWidth] = React.useState(caculateDrawerWidth());
const [backWidth, setBackWidth] = React.useState(caculateBackWidth());

const handleDrawerOpen = () => {
useGlobalStore.setState({
Expand All @@ -67,35 +69,23 @@ export default function PersistentDrawerRight() {
});
};

const caculateDrawerWidth = () => {
if (window.location.href.includes(OAUTH2)) {
return XFANS_MIN_WIDTH;
}
// 首页信息流dom
const xPath = '/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div';
const right = getElementRightByXPath(xPath);

if (right === null) {
return XFANS_MIN_WIDTH;
}
return Math.max(window.innerWidth - right, XFANS_MIN_WIDTH);
};

const caculateBackWidth = () => {
return caculateDrawerWidth() - XFANS_CONTENT_WIDTH;
const handleResize = () => {
// 当窗口大小变化时,更新 width 的值
setDrawerWidth(caculateDrawerWidth());
setBackWidth(caculateBackWidth());
};

const [drawerWidth, setDrawerWidth] = React.useState(caculateDrawerWidth());

const [backWidth, setBackWidth] = React.useState(caculateBackWidth());
// 动态更新width
// React.useEffect(() => {
// const timer = setInterval(() => {
// handleResize();
// }, 1000);
// return () => {
// clearInterval(timer);
// };
// }, []);

React.useEffect(() => {
function handleResize() {
// 当窗口大小变化时,更新 width 的值
setBackWidth(caculateBackWidth());
setDrawerWidth(caculateDrawerWidth());
}

// 添加窗口大小变化时的事件监听器
window.addEventListener('resize', handleResize);
window.addEventListener('load', handleResize);
Expand Down
1 change: 0 additions & 1 deletion src/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Store } from '@eduardoac-skimlinks/webext-redux';

import { proxyStore as store } from '../app/proxyStore';
import useGlobalStore from '../store/useGlobalStore';

import { addTwitterComponent, addUserPagePriceComponent } from './addToTwitterHome';
import Content from './Content';

Expand Down
1 change: 1 addition & 0 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const manifest: ManifestV3Export = {
{
matches: ['https://twitter.com/*'],
js: ['src/content/index.tsx'],
run_at: 'document_end',
},
],
host_permissions: ['https://twitter.com/*', 'https://x.com/*'],
Expand Down
22 changes: 20 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigNumber from 'bignumber.js';
import dayjs from 'dayjs';
import { OAUTH2, XFANS_MIN_WIDTH, XFANS_CONTENT_WIDTH } from './constants';

export function formatTime(seconds: any) {
if (seconds < 60) {
Expand Down Expand Up @@ -45,7 +46,6 @@ export function getElementWidthByXPath(xpath: string): number | null {
if (element) {
return element.offsetWidth;
} else {
console.error('Element not found with the given XPath');
return null;
}
}
Expand All @@ -65,7 +65,25 @@ export function getElementRightByXPath(xpath: string): number | null {
const rightPosition = rect.right;
return rightPosition;
} else {
console.error('Element not found with the given XPath');
return null;
}
}

export const caculateDrawerWidth = () => {
if (window.location.href.includes(OAUTH2)) {
return XFANS_MIN_WIDTH;
}
// 首页信息流dom
const xPath = '/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div';
const right = getElementRightByXPath(xPath);

if (right === null) {
return XFANS_MIN_WIDTH;
}

return Math.max(document.body.clientWidth - right, XFANS_MIN_WIDTH);
huangbinjie marked this conversation as resolved.
Show resolved Hide resolved
};

export const caculateBackWidth = () => {
return caculateDrawerWidth() - XFANS_CONTENT_WIDTH;
};
Loading