Skip to content

Commit

Permalink
feat: add quick edit
Browse files Browse the repository at this point in the history
  • Loading branch information
sylingd committed Jul 3, 2024
1 parent 431b297 commit d55decc
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/pages/background/core/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function save(o: Rule) {
!o.isFunction &&
[RULE_TYPE.MODIFY_RECV_HEADER, RULE_TYPE.MODIFY_SEND_HEADER, RULE_TYPE.REDIRECT].includes(o.ruleType)
) {
const writeValue = o.ruleType === RULE_TYPE.REDIRECT ? o.action : (o.action as RULE_ACTION_OBJ).value;
const writeValue = o.ruleType === RULE_TYPE.REDIRECT ? o.to : (o.action as RULE_ACTION_OBJ).value;
const key = `rule_switch_${getVirtualKey(o)}`;
const engine = getLocal();
engine.get(key).then((result) => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/options/sections/options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const checkPrefs: { [key: string]: string } = {
'is-debug': t('debug_mode_enable'),
'rule-switch': t('rule-switch'),
'rule-history': t('rule-history'),
'quick-edit': t('quick-edit'),
};

interface SelectItem {
Expand Down
115 changes: 115 additions & 0 deletions src/pages/popup/rule/quick-edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { IconEdit } from '@douyinfe/semi-icons';
import { Button, Form, Input, Modal } from '@douyinfe/semi-ui';
import { css } from '@emotion/css';
import { useLatest } from 'ahooks';
import React, { ReactNode, useCallback } from 'react';
import { RULE_TYPE } from '@/share/core/constant';
import { RULE_ACTION_OBJ, Rule } from '@/share/core/types';
import { t } from '@/share/core/utils';
import useOption from '@/share/hooks/use-option';
import Api from '@/share/pages/api';
import { Toast } from '@/share/pages/toast';

interface QuickEditProps {
rule: Rule;
}

const QuickEdit = ({ rule }: QuickEditProps) => {
const { isFunction, ruleType } = rule;
const ruleRef = useLatest(rule);

const isEnable = useOption('quick-edit', false);

const handleEdit = useCallback(() => {
const newRule = { ...ruleRef.current };

let content: ReactNode = null;
if (newRule.ruleType === RULE_TYPE.REDIRECT) {
content = (
<Form.Slot label={t('redirectTo')}>
<Input defaultValue={newRule.to} onChange={(v) => (newRule.to = v)} />
</Form.Slot>
);
}

if ([RULE_TYPE.MODIFY_RECV_HEADER, RULE_TYPE.MODIFY_SEND_HEADER].includes(newRule.ruleType)) {
newRule.action = { ...(newRule.action as RULE_ACTION_OBJ) };
content = (
<>
<Form.Slot label={t('headerName')}>
<Input
defaultValue={(newRule.action as RULE_ACTION_OBJ).name}
onChange={(v) => ((newRule.action as RULE_ACTION_OBJ).name = v)}
/>
</Form.Slot>
<Form.Slot label={t('headerValue')}>
<Input
defaultValue={(newRule.action as RULE_ACTION_OBJ).value}
onChange={(v) => ((newRule.action as RULE_ACTION_OBJ).value = v)}
/>
</Form.Slot>
</>
);
}

Modal.confirm({
icon: null,
closable: false,
className: css`
.semi-modal {
margin: 0;
position: fixed;
bottom: 0;
width: 100%;
> .semi-modal-content {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
border-bottom: 0;
border-left: 0;
border-right: 0;
> .semi-modal-footer {
margin-top: 0;
margin-bottom: 12px;
}
}
}
`,
content,
onOk: async () => {
if (newRule.ruleType === 'redirect' && (!newRule.to || newRule.to === '')) {
Toast().error(t('redirect_empty'));
return;
}
if (
(newRule.ruleType === 'modifySendHeader' || newRule.ruleType === 'modifyReceiveHeader') &&
(typeof newRule.action !== 'object' || newRule.action.name === '')
) {
Toast().error(t('header_empty'));
return;
}
try {
await Api.saveRule(newRule);
Toast().success(t('saved'));
} catch (e) {
Toast().error(e.message);
}
},
});
}, []);

if (!isEnable) {
return null;
}

if (
isFunction ||
![RULE_TYPE.MODIFY_RECV_HEADER, RULE_TYPE.MODIFY_SEND_HEADER, RULE_TYPE.REDIRECT].includes(ruleType)
) {
return null;
}

return <Button theme="borderless" type="tertiary" size="small" icon={<IconEdit />} onClick={handleEdit} />;
};

export default QuickEdit;
10 changes: 7 additions & 3 deletions src/pages/popup/rule/rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import RuleDetail from '@/share/components/rule-detail';
import notify from '@/share/core/notify';
import RuleContentSwitcher from '@/share/components/rule-content-switcher';
import { textEllipsis } from '@/share/pages/styles';
import QuickEdit from './quick-edit';

const Rules = () => {
const { keys } = useMarkCommon('rule');
Expand Down Expand Up @@ -112,9 +113,12 @@ const Rules = () => {
<Popover showArrow position="top" content={<RuleDetail rule={item} />} style={{ maxWidth: '300px' }}>
<div className={cx(textEllipsis, 'name')}>{item.name}</div>
</Popover>
<RuleContentSwitcher rule={item} type={item.ruleType} size="small" add={false}>
<Button theme="borderless" type="tertiary" size="small" icon={<IconBranch />} />
</RuleContentSwitcher>
<div className="actions">
<QuickEdit rule={item} />
<RuleContentSwitcher rule={item} type={item.ruleType} size="small" add={false}>
<Button theme="borderless" type="tertiary" size="small" icon={<IconBranch />} />
</RuleContentSwitcher>
</div>
</div>
))}
</div>
Expand Down
1 change: 1 addition & 0 deletions src/share/core/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const defaultPrefValue: PrefValue = {
'dark-mode': 'auto',
'rule-switch': true,
'rule-history': true,
'quick-edit': true,
};

export enum APIs {
Expand Down
3 changes: 3 additions & 0 deletions src/share/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ export interface PrefValue {
'modify-body': boolean; // Enable modify received body feature
'is-debug': boolean;
'dark-mode': 'auto' | 'on' | 'off';
'rule-switch': boolean; // Enable rule quick switch
'rule-history': boolean; // Auto save rule history into quick switch
'quick-edit': boolean; // Quick edit rule in popup panel
}

0 comments on commit d55decc

Please sign in to comment.