Skip to content

Commit

Permalink
🐛 fix: 修正 search bar 开发时配置
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Feb 14, 2021
1 parent f4f55fe commit fce42d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
3 changes: 3 additions & 0 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default defineConfig({
theme: resolve(__dirname, '../src/theme'),
},
theme,
define: {
'process.env.SEARCH_BAR': process.env.SEARCH_BAR,
},
// chrome 插件配置项
extensions: {
name: `Power Yuque${isDev ? ' DEV' : ''}`,
Expand Down
19 changes: 9 additions & 10 deletions src/contentScripts/searchBar/app/SearchResult/useKeyboardResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ export const useKeyboardResult = () => {

const [resultIndex, setResultIndex] = useState(0);
const resultRef = useRef<HTMLDivElement>(null);
console.log('real', resultIndex);

/**
* 处理结果区高度
*/
const scrollResultContainer = (index: number, back?: boolean) => {
const scrollResultContainer = (back?: boolean) => {
const { current: ctn } = resultRef;
if (!ctn) return;

const step = 77;

if (index === result.length) {
ctn.scrollTop = 0;
}

if (!back) {
if (index > 3) {
if (resultIndex > 3) {
ctn.scrollTop += step;
}

// 最后一个
if (resultIndex === result.length - 1) {
ctn.scrollTop = 0;
}
} else if (ctn.scrollTop > 0) {
ctn.scrollTop -= step;
}
Expand Down Expand Up @@ -65,7 +65,6 @@ export const useKeyboardResult = () => {
// 将焦点切换到 Options
const onKeyDown = (event: KeyboardEvent) => {
if (focusKey !== 'result') return;
console.log(resultIndex);

switch (event.key) {
case 'Tab':
Expand All @@ -74,7 +73,7 @@ export const useKeyboardResult = () => {
break;
case 'ArrowDown':
event.preventDefault();
scrollResultContainer(resultIndex + 1);
scrollResultContainer();
switchResultIndex();
break;
case 'ArrowUp':
Expand All @@ -83,7 +82,7 @@ export const useKeyboardResult = () => {
if (resultIndex === 0) {
focusOnOptions();
} else {
scrollResultContainer(resultIndex - 1, true);
scrollResultContainer(true);
switchResultIndex(true);
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/contentScripts/searchBar/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SearchInput from './SearchInput';
import SearchResult from './SearchResult';

import styles from './style.less';
import { isDev } from '@/utils';
import { isDevSearchBar } from '@/utils';

const SearchBar: FC = () => {
const { visible, searchBarRef } = useContext(SearchBarService);
Expand Down Expand Up @@ -77,7 +77,7 @@ const SearchBar: FC = () => {

export default () => (
<YuqueTokenService.Provider value={useYuqueTokenService()}>
<SearchBarService.Provider value={useSearchBarService(isDev)}>
<SearchBarService.Provider value={useSearchBarService(isDevSearchBar)}>
<SearchService.Provider value={useSearchService()}>
<SearchBar />
</SearchService.Provider>
Expand Down
8 changes: 5 additions & 3 deletions src/contentScripts/searchBar/app/useSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
distinctUntilChanged,
} from 'rxjs/operators';

import { getServiceToken, request } from '@/utils';
import { getServiceToken, isDevSearchBar, request } from '@/utils';

/**
* SearchInput 需要的状态
Expand Down Expand Up @@ -92,8 +92,10 @@ export const useSearchService = () => {

// TEST 用于测试 list 的代码
useEffect(() => {
// @ts-ignore
onSearchEvent({ target: { value: '设计' } });
if (isDevSearchBar) {
// @ts-ignore
onSearchEvent({ target: { value: '设计' } });
}
}, []);

return {
Expand Down
1 change: 1 addition & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const isDev = process.env.NODE_ENV === 'development';
export const isTest = process.env.NODE_ENV === 'test';
export const isDevSearchBar = process.env.SEARCH_BAR === '1';

export const yuqueToken = isTest
? process.env.YUQUE_TOKEN!
Expand Down

0 comments on commit fce42d1

Please sign in to comment.