Skip to content

Commit

Permalink
Merge pull request #2 from PlayGuitar-CoderQ/feature/keyboard-shortcuts
Browse files Browse the repository at this point in the history
Feature/keyboard shortcuts
  • Loading branch information
wangrongding authored Dec 2, 2022
2 parents 5e36f33 + ff4dbaa commit 3885129
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

### github 添加在线编辑按钮

github 添加在线编辑按钮,方便快速使用 `1s` 查看代码,(为什么?因为 1s 比 通过 github 页面中快捷键"句号"调出的 github.dev 要快)。
github 添加在线编辑按钮并且可以使用快捷键 ","直接进入。 方便快速使用 `1s` 查看代码,(为什么?因为 1s 比 通过 github 页面中快捷键"句号"调出的 github.dev 要快)。

![](https://assets.fedtop.com/picbed/202210280935904.png)

Expand Down
11 changes: 11 additions & 0 deletions src/contents/event/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function toGithub1s () {
if (!window) {
return;
}

window.open(`${`https://github1s.com${window.location.pathname}`}`);
}

export {
toGithub1s
}
67 changes: 0 additions & 67 deletions src/contents/github.tsx

This file was deleted.

26 changes: 26 additions & 0 deletions src/contents/hooks/useGitHub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useMemo } from 'react';
import registerKeyDownListen from '../register/keydownListen';

const useGitHub = () => {

useEffect(() => {
registerKeyDownListen();
}, [])

const isCodePage = useMemo<boolean>(() => {
const url = window.location.href;
const fileNavigation = document.querySelectorAll('.file-navigation');
const isHasBlob = url.includes('/blob/');
const isHasTree = url.includes('/tree/');

return fileNavigation.length > 0 || isHasBlob || isHasTree;
}, [])

return {
isCodePage,
}
}

export {
useGitHub
}
14 changes: 14 additions & 0 deletions src/contents/register/keydownListen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { keyCodeEnums } from '../../enum/commandEnum';
import { toGithub1s } from '../event';

const keyCodeEventMap: Record<string, Function> = {
[keyCodeEnums.comma]: toGithub1s
}

const keyDownListen = () => {
window.addEventListener("keydown", function (e) {
keyCodeEventMap[e.code]();
})
}

export default keyDownListen;
35 changes: 35 additions & 0 deletions src/contents/views/Github/components/OnlineEditBtn/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { FC } from 'react';

interface OnlineEditBtnProps {
onClick?: () => void;
title: string;
}

const OnlineEditBtn: FC<OnlineEditBtnProps> = props => {
const { onClick, title } = props;

return (
<button
onClick={onClick}
style={{
color: 'white',
colorScheme: 'dark',
fontWeight: 'bold',
padding: '5px 10px',
lineHeight: '20px',
cursor: 'pointer',
fontSize: '14px',
background: 'rgb(52, 125, 57)',
position: 'fixed',
right: '100px',
bottom: '100px',
border: '1px solid rgba(205, 217, 229, 0.1)',
borderRadius: '6px',
}}
>
{title}
</button>
)
}

export default OnlineEditBtn;
21 changes: 21 additions & 0 deletions src/contents/views/Github/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { PlasmoContentScript } from 'plasmo'

import { useGitHub } from '../../hooks/useGitHub';

import OnlineEditBtn from './components/OnlineEditBtn';

export const config: PlasmoContentScript = {
matches: ['https://github.com/*/*'],
}


export default function FunctionPage() {
const { isCodePage } = useGitHub();


return (
<>
{isCodePage && <OnlineEditBtn title='在线编辑' />}
</>
)
}
5 changes: 5 additions & 0 deletions src/enum/commandEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
export enum keyCodeEnums {
/** 逗号的 code */
comma = "Comma"
}

0 comments on commit 3885129

Please sign in to comment.