From 84a7f12e7f017e5c76c0403d9ce8222779d7b34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Fri, 24 Nov 2023 03:26:28 +0900 Subject: [PATCH] =?UTF-8?q?enhance(frontend):=20=E3=83=96=E3=83=A9?= =?UTF-8?q?=E3=82=A6=E3=82=B6=E3=81=AE=E4=BA=92=E6=8F=9B=E6=80=A7=E5=90=91?= =?UTF-8?q?=E4=B8=8A=20(MisskeyIO#257)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/nyaize.ts | 52 +++++++++++++++++--- packages/frontend/src/workers/test-webgl2.ts | 6 +-- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/frontend/src/scripts/nyaize.ts b/packages/frontend/src/scripts/nyaize.ts index 62833b4de3..8b905ce156 100644 --- a/packages/frontend/src/scripts/nyaize.ts +++ b/packages/frontend/src/scripts/nyaize.ts @@ -3,14 +3,32 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -const enRegex1 = /(?<=n)a/gi; -const enRegex2 = /(?<=morn)ing/gi; -const enRegex3 = /(?<=every)one/gi; -const koRegex1 = /[나-낳]/g; -const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm; -const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm; +let enRegex1: RegExp; +let enRegex2: RegExp; +let enRegex3: RegExp; +let koRegex1: RegExp; +let koRegex2: RegExp; +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 // ja-JP .replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ') @@ -25,3 +43,23 @@ export function nyaize(text: string): string { .replace(koRegex2, '다냥') .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); +} diff --git a/packages/frontend/src/workers/test-webgl2.ts b/packages/frontend/src/workers/test-webgl2.ts index 8f57e5039b..1f3f640e6b 100644 --- a/packages/frontend/src/workers/test-webgl2.ts +++ b/packages/frontend/src/workers/test-webgl2.ts @@ -3,9 +3,9 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -const canvas = globalThis.OffscreenCanvas && new OffscreenCanvas(1, 1); -// 環境によってはOffscreenCanvasが存在しないため -// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition +const canvas = typeof OffscreenCanvas !== 'undefined' + ? new OffscreenCanvas(1, 1) + : undefined; const gl = canvas?.getContext('webgl2'); if (gl) { postMessage({ result: true });