Skip to content

Commit 6a97831

Browse files
author
xcodebuild
authored
Merge pull request #4 from xcodebuild/feature/hover-template
feat: fast template
2 parents 37a94ca + a1818f6 commit 6a97831

File tree

6 files changed

+70
-51
lines changed

6 files changed

+70
-51
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 1.2.1
2+
3+
- 规则编辑增加悬浮的快速模板按钮 @xcodebuild
4+
- Quick template button for rule editing @xcodebuild
5+

src/renderer/extensions/rule-editor/components/card/index.tsx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useEffect, useRef } from 'react';
22
import { Icon as LegacyIcon } from '@ant-design/compatible';
3-
import { Select } from 'antd';
3+
import { Popover, Select, Button, Dropdown, Menu } from 'antd';
44
import { useTranslation } from 'react-i18next';
55
import { findDOMNode } from 'react-dom';
6+
import { MenuOutlined } from '@ant-design/icons';
67

78
interface Props {
89
onFinish: (val: Option) => void;
@@ -116,36 +117,38 @@ export const Card = (props: Props) => {
116117
}, []);
117118

118119
return (
119-
<Select
120-
showSearch
121-
// defaultValue="add-cors"
122-
onChange={val => {
123-
const option = options.find(item => item.value === val) as Option;
124-
onFinish(option);
125-
}}
126-
onBlur={onCancel}
127-
ref={selectRef}
128-
style={{
129-
position: 'fixed',
130-
top: y + 'px',
131-
left: x + 'px',
132-
width: '300px',
133-
}}
134-
showArrow={false}
135-
className="ligthproxy-card"
120+
<Dropdown
121+
overlay={
122+
<Menu>
123+
{options.map(item => {
124+
return (
125+
<Menu.Item key={item.value} onClick={() => {
126+
const option = options.find(findItem => findItem.value === item.value) as Option;
127+
onFinish(option);
128+
}}>
129+
<div className="iproxy-select-item">
130+
<span className="iproxy-select-icon">
131+
<LegacyIcon type={item.icon}></LegacyIcon>
132+
</span>
133+
<span className="iproxy-select-title">{t(item.title)}</span>
134+
</div>
135+
</Menu.Item>
136+
);
137+
})}
138+
</Menu>
139+
}
136140
>
137-
{options.map(item => {
138-
return (
139-
<Select.Option value={item.value} key={item.value}>
140-
<div className="iproxy-select-item">
141-
<span className="iproxy-select-icon">
142-
<LegacyIcon type={item.icon}></LegacyIcon>
143-
</span>
144-
<span className="iproxy-select-title">{t(item.title)}</span>
145-
</div>
146-
</Select.Option>
147-
);
148-
})}
149-
</Select>
141+
<Button
142+
style={{
143+
left: x,
144+
top: y,
145+
position: 'absolute',
146+
}}
147+
size="small"
148+
shape="circle"
149+
icon={<MenuOutlined />}>
150+
</Button>
151+
</Dropdown>
152+
150153
);
151154
};

src/renderer/extensions/rule-editor/components/editor/index.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ export const Editor = (props: Props) => {
2828
const onSaveRef = useRef(onSave);
2929
onSaveRef.current = onSave;
3030

31+
const containerRef = useRef<HTMLDivElement | null>(null);
32+
33+
useEffect(() => {
34+
// display a fast button when hover on line
35+
if (containerRef.current) {
36+
const onMouseOver = (e: MouseEvent) => {
37+
if (!e.target) return;
38+
const ele = e.target as HTMLElement;
39+
if (ele.className === 'view-line') {
40+
// find line
41+
requestAnimationFrame(() => {
42+
const rect = ele.getBoundingClientRect();
43+
setShowCardsPos({ x: rect.x - 200, y: rect.y });
44+
});
45+
}
46+
};
47+
containerRef.current.addEventListener('mouseover', onMouseOver);
48+
return () => {
49+
containerRef.current?.removeEventListener('mouseover', onMouseOver);
50+
};
51+
}
52+
}, [containerRef]);
53+
3154
useEffect(() => {
3255
const resizeEditor = () => {
3356
editorRef.current?.editor?.layout();
@@ -41,22 +64,6 @@ export const Editor = (props: Props) => {
4164

4265
const handleChange = (val: string) => {
4366
onChange(val);
44-
if (editorRef.current) {
45-
const editor = editorRef.current.editor;
46-
47-
const model = editor?.getModel();
48-
const position = editor?.getPosition();
49-
if (model && position) {
50-
const lineContent = model.getLineContent(position.lineNumber);
51-
if (lineContent.replace(/^\s+/, '').replace(/\s+$/, '') === '/') {
52-
// trigger / cards
53-
54-
const top = editor?.getTopForPosition(position.lineNumber, position.column) || 0;
55-
const offset = editor?.getScrollTop() || 0;
56-
setShowCardsPos({ x: 205, y: top - offset + 20 });
57-
}
58-
}
59-
}
6067
};
6168

6269
const handleFinished = (option: Option) => {
@@ -96,12 +103,11 @@ export const Editor = (props: Props) => {
96103
});
97104

98105
return (
99-
<div className="iproxy-rule-editor-container">
106+
<div ref={containerRef} className="iproxy-rule-editor-container">
100107
<div
101108
onDoubleClick={() => remote.getCurrentWindow().maximize()}
102109
className="iproxy-editor-actionbar drag"
103110
>
104-
<span className="tip">{t('Type / to insert rule')}</span>
105111
</div>
106112

107113
<div className="iproxy-code-editor-container no-drag">
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
.whistle-status-bar-item > * {
22
vertical-align: middle;
33
}
4+
5+
.hover-menu {
6+
.anticon {
7+
margin-right: 5px;
8+
}
9+
}

src/renderer/extensions/whistle/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class WhistleExntension extends Extension {
333333
}, []);
334334

335335
const menu = (
336-
<Menu>
336+
<Menu className="hover-menu">
337337
<Menu.Item onClick={this.toggleSystemProxy.bind(this)}>
338338
<DesktopOutlined />
339339
{onlineState === 'ready' ? t('Disable system proxy') : t('Enable system proxy')}

src/renderer/i18n.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const i18nResources = {
2020
'Online but not system proxy': '代理已启动但不是系统代理',
2121
'Online & system proxy ready': '代理已经启动 & 设置为系统代理',
2222
'Default rule to keep some daily-software works behind proxy': '初始规则可以让一些日常使用软件在代理下工作',
23-
'Type / to insert rule': '输入 / 插入规则',
2423
'Disable system proxy': '禁用系统代理',
2524
'Enable system proxy': '启用系统代理',
2625
search: '搜索',

0 commit comments

Comments
 (0)