enhance(frontend): ブラウザの互換性向上 (MisskeyIO#257)

This commit is contained in:
まっちゃとーにゅ 2023-11-24 03:26:28 +09:00 committed by GitHub
parent 6157c35f9c
commit 84a7f12e7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 10 deletions

View file

@ -3,14 +3,32 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
const enRegex1 = /(?<=n)a/gi; let enRegex1: RegExp;
const enRegex2 = /(?<=morn)ing/gi; let enRegex2: RegExp;
const enRegex3 = /(?<=every)one/gi; let enRegex3: RegExp;
const koRegex1 = /[나-낳]/g; let koRegex1: RegExp;
const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm; let koRegex2: RegExp;
const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm; let koRegex3: RegExp;
let fallback: boolean = true;
export function nyaize(text: string): string { try {
enRegex1 = new RegExp('(?<=n)a', 'gi');
enRegex2 = new RegExp('(?<=morn)ing', 'gi');
enRegex3 = new RegExp('(?<=every)one', 'gi');
koRegex1 = new RegExp('[나-낳]', 'g');
koRegex2 = new RegExp('(다$)|(다(?= ))|(다(?=!))|(다(?=\\?))|(다(?=\\.))', 'gm');
koRegex3 = new RegExp('(야$)|(야(?= ))|(야(?=!))|(야(?=\\?))|(야(?=\\.))', 'gm');
fallback = false;
} catch {
enRegex1 = new RegExp('na', 'gi');
enRegex2 = new RegExp('morning', 'gi');
enRegex3 = new RegExp('everyone', 'gi');
koRegex1 = new RegExp('[나-낳]', 'g');
koRegex2 = new RegExp('다$', 'gm');
koRegex3 = new RegExp('야$', 'gm');
}
function convertNormal(text: string): string {
return text return text
// ja-JP // ja-JP
.replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ') .replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ')
@ -25,3 +43,23 @@ export function nyaize(text: string): string {
.replace(koRegex2, '다냥') .replace(koRegex2, '다냥')
.replace(koRegex3, '냥'); .replace(koRegex3, '냥');
} }
function convertFallback(text: string): string {
return text
// ja-JP
.replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ')
// en-US
.replace(enRegex1, x => x === 'NA' ? 'NYA' : 'nya')
.replace(enRegex2, x => x === 'MORNING' ? 'MORNYAN' : 'mornyan')
.replace(enRegex3, x => x === 'EVERYONE' ? 'EVERYNYAN' : 'everynyan')
// ko-KR
.replace(koRegex1, match => String.fromCharCode(
match.charCodeAt(0)! + '냐'.charCodeAt(0) - '나'.charCodeAt(0),
))
.replace(koRegex2, '다냥').replaceAll('다 ', '다냥 ').replaceAll('다!', '다냥!').replaceAll('다?', '다냥?').replaceAll('다.', '다냥.')
.replace(koRegex3, '냥').replaceAll('야 ', '냥 ').replaceAll('야!', '냥!').replaceAll('야?', '냥?').replaceAll('야.', '냥.');
}
export function nyaize(text: string): string {
return !fallback ? convertNormal(text) : convertFallback(text);
}

View file

@ -3,9 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
const canvas = globalThis.OffscreenCanvas && new OffscreenCanvas(1, 1); const canvas = typeof OffscreenCanvas !== 'undefined'
// 環境によってはOffscreenCanvasが存在しないため ? new OffscreenCanvas(1, 1)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition : undefined;
const gl = canvas?.getContext('webgl2'); const gl = canvas?.getContext('webgl2');
if (gl) { if (gl) {
postMessage({ result: true }); postMessage({ result: true });