js-translit/main.js

61 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let translitEnabled = true;
function toggleTranslit() {
translitEnabled = document.getElementById("translitCheckbox").checked;
}
function translit() {
if (!translitEnabled) return;
const dic1 = {
A: "А", a: "а", B: "Б", b: "б", V: "В", v: "в", G: "Г", g: "г",
D: "Д", d: "д", E: "Е", e: "е", Z: "З", z: "з", I: "И", i: "и",
J: "Й", j: "й", K: "К", k: "к", L: "Л", l: "л", M: "М", m: "м",
N: "Н", n: "н", O: "О", o: "о", P: "П", p: "п", R: "Р", r: "р",
S: "С", s: "с", T: "Т", t: "т", U: "У", u: "у", F: "Ф", f: "ф",
X: "Х", x: "х", H: "Х", h: "х", C: "Ц", c: "ц", W: "В", w: "в",
"\u0022": "ъ", Y: "Ы", y: "ы", "\u0027": "ь", Ä: "Э", ä: "э",
Ö: "Ё", ö: "ё", Ü: "Ю", ü: "ю", Q: "Я", q: "я"
};
const dic2 = {
ЙЕ: "Э", Йе: "Э", йе: "э",
ЙО: "Ё", Йо: "Ё", йо: "ё", ЫО: "Ё", Ыо: "Ё", ыо: "ё",
ЙУ: "Ю", Йу: "Ю", йу: "ю", ЫУ: "Ю", Ыу: "Ю", ыу: "ю",
ЙА: "Я", Йа: "Я", йа: "я", ЫА: "Я", Ыа: "Я", ыа: "я",
ЦХ: "Ч", Цх: "Ч", цх: "ч",
СХ: "Ш", Сх: "Ш", сх: "ш",
ЗХ: "Ж", Зх: "Ж", зх: "ж",
ШХ: "Щ", Шх: "Щ", шх: "щ",
"ьь": "Ь", "ъъ": "Ъ"
};
const ctl = document.getElementById("transtxt");
let str = ctl.value;
const pos = ctl.selectionStart;
let tmp = str.slice(pos - 1, pos);
if (dic1.hasOwnProperty(tmp)) {
tmp = dic1[tmp];
str = str.substring(0, pos - 1) + tmp + str.substring(pos);
ctl.value = str;
ctl.selectionEnd = pos;
}
tmp = str.slice(pos - 2, pos);
if (dic2.hasOwnProperty(tmp)) {
tmp = dic2[tmp];
str = str.substring(0, pos - 2) + tmp + str.substring(pos);
ctl.value = str;
ctl.selectionEnd = pos - 1;
}
}
function copytxt() {
const copyctl = document.getElementById("transtxt");
copyctl.select();
navigator.clipboard.writeText(copyctl.value);
}
function cleartxt() {
document.getElementById("transtxt").value = "";
}