input box trans

This commit is contained in:
Gabe Yuan
2023-09-14 10:59:50 +08:00
parent 14ca13e31d
commit 76f54461e7
6 changed files with 42 additions and 8 deletions

View File

@@ -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;
}

View File

@@ -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);
};