Skip to content

Commit

Permalink
add: "do not respond in text box" option
Browse files Browse the repository at this point in the history
  • Loading branch information
chunibyocola committed Apr 25, 2021
1 parent 84acec1 commit 8f48391
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 6 deletions.
6 changes: 6 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
"optionsTranslateDirectlyWhilePinning": {
"message": "Translate directly while translate panel pinning"
},
"optionsDoNotRespondInTextBox": {
"message": "Do not respond in text box"
},
"optionsDoNotRespondInTextBoxDescription": {
"message": "Extension will not show button and will not auto translate if you select some text in text box."
},
"themePreset": {
"message": "Preset"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
"optionsTranslateDirectlyWhilePinning": {
"message": "翻訳パネルの固定中に直接翻訳する"
},
"optionsDoNotRespondInTextBox": {
"message": "テキストボックスに応答しない"
},
"optionsDoNotRespondInTextBoxDescription": {
"message": "テキストボックスでテキストを選択するでも、拡張機能は「ボタンを表示」や「自動翻訳」をしません。"
},
"themePreset": {
"message": "プリセット"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
"optionsTranslateDirectlyWhilePinning": {
"message": "翻译面板固定时直接翻译"
},
"optionsDoNotRespondInTextBox": {
"message": "不在文本框中响应"
},
"optionsDoNotRespondInTextBoxDescription": {
"message": "在文本框内选择文本时,扩展不会展示按钮和自动翻译。"
},
"themePreset": {
"message": "预设"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
"optionsTranslateDirectlyWhilePinning": {
"message": "翻譯面板固定時直接翻譯"
},
"optionsDoNotRespondInTextBox": {
"message": "不在文本框中響應"
},
"optionsDoNotRespondInTextBoxDescription": {
"message": "在文本框內選擇文本時,擴充功能不會展示按鈕和自動翻譯。"
},
"themePreset": {
"message": "預設"
},
Expand Down
10 changes: 7 additions & 3 deletions src/components/TsBtn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import './style.css';
import { sendAudio, sendSeparate } from '../../public/send';
import { stSetText } from '../../redux/actions/singleTranslateActions';
import { getOptions } from '../../public/options';
import { debounce } from '../../public/utils';
import { debounce, isTextBox } from '../../public/utils';

const initText = '';
const initPos = { x: 5, y: 5 };
Expand All @@ -34,7 +34,8 @@ const useOptionsDependency = [
'hideButtonAfterFixedTime',
'hideButtonFixedTime',
'respondToSeparateWindow',
'translateDirectlyWhilePinning'
'translateDirectlyWhilePinning',
'doNotRespondInTextBox'
];

const calculateBtnPos = ({ x, y }) => {
Expand Down Expand Up @@ -74,7 +75,8 @@ const TsBtn = ({ multipleTranslateMode }) => {
hideButtonAfterFixedTime,
hideButtonFixedTime,
respondToSeparateWindow,
translateDirectlyWhilePinning
translateDirectlyWhilePinning,
doNotRespondInTextBox
} = useOptions(useOptionsDependency);

const isEnableTranslate = useIsEnable('translate', window.location.host);
Expand Down Expand Up @@ -147,6 +149,8 @@ const TsBtn = ({ multipleTranslateMode }) => {
useGetSelection(({ text, pos }) => {
if (!isEnableTranslate) { return; }

if (doNotRespondInTextBox && isTextBox(document.activeElement)) { return; }

const nextPos = calculateBtnPos(pos);

if ((translateWithKeyPress && ctrlPressing.current) || translateDirectly || (pinning && translateDirectlyWhilePinning)) {
Expand Down
3 changes: 2 additions & 1 deletion src/constants/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const defaultOptions = {
recentTranslateToList: [],
rememberHistoryPanelStatus: false,
historyPanelStatus: { pin: false, width: 200 },
translateDirectlyWhilePinning: false
translateDirectlyWhilePinning: false,
doNotRespondInTextBox: false
};

export default defaultOptions;
4 changes: 3 additions & 1 deletion src/entry/options/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const Options = () => {
translatePanelWidth,
translatePanelFontSize,
rememberHistoryPanelStatus,
translateDirectlyWhilePinning
translateDirectlyWhilePinning,
doNotRespondInTextBox
} = useOptions(useOptionsDependency);

const updateStorage = useCallback((key, value) => (setLocalStorage({[key]: value})), []);
Expand Down Expand Up @@ -129,6 +130,7 @@ const Options = () => {
translateHostList={translateHostList}
respondToSeparateWindow={respondToSeparateWindow}
translateDirectlyWhilePinning={translateDirectlyWhilePinning}
doNotRespondInTextBox={doNotRespondInTextBox}
/>
<div className='sub-title' id='history'>{getMessage('optionsHistory')}</div>
<History
Expand Down
12 changes: 11 additions & 1 deletion src/entry/options/Options/sections/Translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const Translate = ({
translateBlackListMode,
translateHostList,
respondToSeparateWindow,
translateDirectlyWhilePinning
translateDirectlyWhilePinning,
doNotRespondInTextBox
}) => {
return (
<div className='opt-section'>
Expand Down Expand Up @@ -105,6 +106,15 @@ const Translate = ({
onClick={() => updateStorage('translateDirectlyWhilePinning', !translateDirectlyWhilePinning)}
/>
</div>
<div className='opt-section-row'>
<OptionToggle
id='do-not-respond-in-text-box'
message='optionsDoNotRespondInTextBox'
checked={doNotRespondInTextBox}
onClick={() => updateStorage('doNotRespondInTextBox', !doNotRespondInTextBox)}
/>
<div className='item-description'>{getMessage('optionsDoNotRespondInTextBoxDescription')}</div>
</div>
<div className='opt-section-row'>
<div className='options-mode'>
{getMessage('optionsDomainfilter')}
Expand Down
4 changes: 4 additions & 0 deletions src/public/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ export const mouseDrag = (mouseMoveCallback, mouseUpCallback) => {
document.onselectstart = () => { return false; };
document.addEventListener('mousemove', mouseMoveListener, true);
document.addEventListener('mouseup', mouseUpListener, true);
};

export const isTextBox = (element) => {
return (element.tagName === 'INPUT' && element.type === 'text') || element.tagName === 'TEXTAREA' || element.isContentEditable;
};

0 comments on commit 8f48391

Please sign in to comment.