Skip to content

Commit

Permalink
feat: add fast-pr button position (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyantong2000 authored Nov 11, 2024
1 parent 3dd0bcd commit 1c48b96
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
20 changes: 15 additions & 5 deletions public_configs/fast-pr-url-rules.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const urlRules = [
const repoName = 'X-lab2017/open-digger-website';
const branch = 'master';
const platform = 'Github';
const horizontalRatio=0.95;
const verticalRatio=0.5;
let filePath = '';
if (!url.startsWith(baseUrl)) return null;
let i18n = null;
Expand All @@ -23,7 +25,7 @@ const urlRules = [
} else if (docPath.startsWith('blog/')) {
filePath = `${i18n != null ? `i18n/${i18n}docusaurus-plugin-content-` : ''}${docPath}/index.mdx`;
}
return { filePath, repoName, branch, platform };
return { filePath, repoName, branch, platform,horizontalRatio,verticalRatio };
},
tests: [
[
Expand Down Expand Up @@ -60,10 +62,12 @@ const urlRules = [
const repoName = 'X-lab2017/oss101-bok';
const branch = 'master';
const platform = 'Github';
const horizontalRatio=0.95;
const verticalRatio=0.5;
if (!url.startsWith(baseUrl)) return null;
const docPath = url.replace(baseUrl, '').split('#')[0];
const filePath = `docs/textbook/${docPath.slice(0, -1)}.md`;
return { filePath, repoName, branch, platform };
return { filePath, repoName, branch, platform,horizontalRatio,verticalRatio };
},
},
{
Expand All @@ -73,10 +77,12 @@ const urlRules = [
const repoName = 'wangyantong2000/docwebsite';
const branch = 'main';
const platform = 'Gitee';
const horizontalRatio=0.95;
const verticalRatio=0.5;
if (!url.startsWith(baseUrl)) return null;
const docPath = url.replace(baseUrl, '').split('#')[0];
const filePath = `docs/textbooks/${docPath}index.md`;
return { filePath, repoName, branch, platform };
return { filePath, repoName, branch, platform,horizontalRatio,verticalRatio };
},
},
{
Expand All @@ -86,12 +92,14 @@ const urlRules = [
const repoName = 'kaiyuanshe/oss-book';
const branch = 'main';
const platform = 'Github';
const horizontalRatio=0.95;
const verticalRatio=0.95;
if (!url.startsWith(baseUrl)) return null;
let docPath = url.replace(baseUrl, '').split('#')[0];
if (docPath.startsWith('slide')) return null;
docPath = docPath.replace('.html', '');
const filePath = `src/${docPath}.md`;
return { filePath, repoName, branch, platform };
return { filePath, repoName, branch, platform,horizontalRatio,verticalRatio };
},
tests: [
['https://kaiyuanshe.github.io/oss-book/Enterprise-and-Open-Source.html', 'src/Enterprise-and-Open-Source.md'],
Expand All @@ -110,6 +118,8 @@ const urlRules = [
const repoName = 'kwdb/docs';
let branch = 'master';
const platform = 'Gitee';
const horizontalRatio=0.95;
const verticalRatio=0.95;
if (!url.startsWith(baseUrl)) return null;
let docPath = url.replace(baseUrl, '').split('#')[0].replace('.html', '');
function extractVersion(str) {
Expand All @@ -132,7 +142,7 @@ const urlRules = [
}
}
const filePath = `${docPath}.md`;
return { filePath, repoName, branch, platform };
return { filePath, repoName, branch, platform,horizontalRatio,verticalRatio };
},
tests: [
[
Expand Down
31 changes: 28 additions & 3 deletions src/pages/ContentScripts/features/fast-pr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ interface MatchedUrl {
repoName: string;
branch: string;
platform: string;
horizontalRatio: number;
verticalRatio: number;
}

const renderTo = (container: HTMLElement, filePath: string, repoName: string, branch: string, platform: string) => {
const renderTo = (
container: HTMLElement,
filePath: string,
repoName: string,
branch: string,
platform: string,
horizontalRatio: number,
verticalRatio: number
) => {
createRoot(container).render(
<View filePath={filePath} originalRepo={repoName} branch={branch} platform={platform} />
<View
filePath={filePath}
originalRepo={repoName}
branch={branch}
platform={platform}
horizontalRatio={horizontalRatio}
verticalRatio={verticalRatio}
/>
);
};

Expand All @@ -27,7 +44,15 @@ const init = async (matchedUrl: MatchedUrl | null) => {
if (matchedUrl) {
const container = document.createElement('div');
container.id = featureId;
renderTo(container, matchedUrl.filePath, matchedUrl.repoName, matchedUrl.branch, matchedUrl.platform);
renderTo(
container,
matchedUrl.filePath,
matchedUrl.repoName,
matchedUrl.branch,
matchedUrl.platform,
matchedUrl.horizontalRatio,
matchedUrl.verticalRatio
);
document.body.appendChild(container);
}
};
Expand Down
8 changes: 5 additions & 3 deletions src/pages/ContentScripts/features/fast-pr/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ interface Props {
originalRepo: string;
branch: string;
platform: string;
horizontalRatio: number;
verticalRatio: number;
}
const View = ({ filePath, originalRepo, branch, platform }: Props) => {
const View = ({ filePath, originalRepo, branch, platform, horizontalRatio, verticalRatio }: Props) => {
const [giteeToken, setGiteeToken] = useState('');
const [githubToken, setGithubToken] = useState('');
const [options, setOptions] = useState<HypercrxOptions>(defaults);
Expand Down Expand Up @@ -128,8 +130,8 @@ const View = ({ filePath, originalRepo, branch, platform }: Props) => {

// Set the initial position to the middle-right of the screen when the component loads
useEffect(() => {
const initialX = window.innerWidth - buttonSize - padding; // 24px from the right of the screen
const initialY = window.innerHeight / 2 - buttonSize / 2; // Center vertically
const initialX = (window.innerWidth - buttonSize) * horizontalRatio;
const initialY = (window.innerHeight - buttonSize) * verticalRatio;
setPosition({ x: initialX, y: initialY });
}, []);
// Record the starting position when the mouse is pressed
Expand Down

0 comments on commit 1c48b96

Please sign in to comment.