input box trans
This commit is contained in:
@@ -11,10 +11,11 @@ import {
|
||||
OPT_MOUSEKEY_MOUSEOVER,
|
||||
DEFAULT_INPUT_RULE,
|
||||
DEFAULT_TRANS_APIS,
|
||||
DEFAULT_INPUT_SHORTCUT,
|
||||
} from "../config";
|
||||
import Content from "../views/Content";
|
||||
import { updateFetchPool, clearFetchPool } from "./fetch";
|
||||
import { debounce, genEventName } from "./utils";
|
||||
import { debounce, genEventName, removeEndchar } from "./utils";
|
||||
import { stepShortcutRegister } from "./shortcut";
|
||||
import { apiTranslate } from "../apis";
|
||||
import { tryDetectLang } from ".";
|
||||
@@ -257,7 +258,7 @@ export class Translator {
|
||||
};
|
||||
|
||||
_registerInput = () => {
|
||||
const {
|
||||
let {
|
||||
triggerShortcut,
|
||||
translator,
|
||||
fromLang,
|
||||
@@ -269,6 +270,11 @@ export class Translator {
|
||||
translator
|
||||
];
|
||||
|
||||
if (triggerShortcut.length === 0) {
|
||||
triggerShortcut = DEFAULT_INPUT_SHORTCUT;
|
||||
triggerCount = 1;
|
||||
}
|
||||
|
||||
stepShortcutRegister(
|
||||
triggerShortcut,
|
||||
() => {
|
||||
@@ -278,12 +284,17 @@ export class Translator {
|
||||
let timer;
|
||||
|
||||
if (this._inputNodeNames.includes(node.nodeName)) {
|
||||
text = node.value?.trim() || "";
|
||||
text = node.value || "";
|
||||
} else {
|
||||
text = node.textContent?.trim() || "";
|
||||
text = node.textContent || "";
|
||||
}
|
||||
|
||||
if (!text) {
|
||||
// todo: remove multiple char
|
||||
if (triggerShortcut.length === 1 && triggerShortcut[0].length === 1) {
|
||||
text = removeEndchar(text, triggerShortcut[0], triggerCount);
|
||||
}
|
||||
|
||||
if (!text.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -179,3 +179,18 @@ export const isSameSet = (a, b) => {
|
||||
const s = new Set([...a, ...b]);
|
||||
return s.size === a.size && s.size === b.size;
|
||||
};
|
||||
|
||||
/**
|
||||
* 去掉字符串末尾某个字符
|
||||
* @param {*} s
|
||||
* @param {*} c
|
||||
* @param {*} count
|
||||
* @returns
|
||||
*/
|
||||
export const removeEndchar = (s, c, count = 1) => {
|
||||
let i = s.length;
|
||||
while (i > s.length - count && s[i - 1] === c) {
|
||||
i--;
|
||||
}
|
||||
return s.slice(0, i);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user