Skip to content

Commit

Permalink
enhance(frontend): ブラウザの互換性向上 (MisskeyIO#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
u1-liquid authored and yuriha-chan committed Dec 6, 2023
1 parent 3092321 commit 970afdf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
52 changes: 45 additions & 7 deletions packages/frontend/src/scripts/nyaize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('ナ', 'ニャ')
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions packages/frontend/src/workers/test-webgl2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 970afdf

Please sign in to comment.