From 463446795dd04b274072a7ddcfdcb77c6f661b1e Mon Sep 17 00:00:00 2001 From: KokiSakano <61353435+KokiSakano@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:47:49 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20init.ts=E5=91=A8=E3=82=8A=E3=81=AEe?= =?UTF-8?q?slint=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=A8=E5=9E=8B=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20(#10157)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor/miLocalStorageのメソッドに戻り値追加 * refactor/miLocalStorageのキーとしてdebugがconfig.tsに存在するので追加 * fix/JSON.parseにnullは入らないのでnullの時は分岐させてnullにする * refactor/修正したファイルの型調整+記法の統一 * fix/型のためにlangがnullの時はhtmlの言語の設定をしない * refactor/catchで何もしないと警告が出るので修正 * refactor/細かい点の修正 * refactor/変数の二重定義になっていた二重定義になっていたので修正 * refactor/importの整理(通常のimportは最初に処理されるので影響はない想定) * fix/vueファイルに型を与えてインポート時の型エラーを防ぐ * refactor/開発環境のみで利用するので,eslintの設定を変更する * fix/vueの定義を最小限にする * fallback language to 'en-US' * remove accounts migration * fix:vueの型定義ファイルを消す --------- Co-authored-by: tamaina --- packages/frontend/src/config.ts | 11 ++++--- packages/frontend/src/init.ts | 45 ++++++++++---------------- packages/frontend/src/local-storage.ts | 7 ++-- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/packages/frontend/src/config.ts b/packages/frontend/src/config.ts index 073b21a0a..24f60910d 100644 --- a/packages/frontend/src/config.ts +++ b/packages/frontend/src/config.ts @@ -1,21 +1,22 @@ -import { miLocalStorage } from "./local-storage"; +import { miLocalStorage } from './local-storage'; const address = new URL(location.href); -const siteName = (document.querySelector('meta[property="og:site_name"]') as HTMLMetaElement)?.content; +const siteName = document.querySelector('meta[property="og:site_name"]')?.content; export const host = address.host; export const hostname = address.hostname; export const url = address.origin; export const apiUrl = url + '/api'; export const wsUrl = url.replace('http://', 'ws://').replace('https://', 'wss://') + '/streaming'; -export const lang = miLocalStorage.getItem('lang'); +export const lang = miLocalStorage.getItem('lang') ?? 'en-US'; export const langs = _LANGS_; -export let locale = JSON.parse(miLocalStorage.getItem('locale')); +const preParseLocale = miLocalStorage.getItem('locale'); +export let locale = preParseLocale ? JSON.parse(preParseLocale) : null; export const version = _VERSION_; export const instanceName = siteName === 'Misskey' ? host : siteName; export const ui = miLocalStorage.getItem('ui'); export const debug = miLocalStorage.getItem('debug') === 'true'; -export function updateLocale(newLocale) { +export function updateLocale(newLocale): void { locale = newLocale; } diff --git a/packages/frontend/src/init.ts b/packages/frontend/src/init.ts index 5b3e7ec93..91cdc15ef 100644 --- a/packages/frontend/src/init.ts +++ b/packages/frontend/src/init.ts @@ -6,18 +6,6 @@ import 'vite/modulepreload-polyfill'; import '@/style.scss'; -//#region account indexedDB migration -import { set } from '@/scripts/idb-proxy'; - -{ - const accounts = miLocalStorage.getItem('accounts'); - if (accounts) { - set('accounts', JSON.parse(accounts)); - miLocalStorage.removeItem('accounts'); - } -} -//#endregion - import { computed, createApp, watch, markRaw, version as vueVersion, defineAsyncComponent } from 'vue'; import { compareVersions } from 'compare-versions'; import JSON5 from 'json5'; @@ -42,11 +30,11 @@ import { reloadChannel } from '@/scripts/unison-reload'; import { reactionPicker } from '@/scripts/reaction-picker'; import { getUrlWithoutLoginId } from '@/scripts/login-id'; import { getAccountFromId } from '@/scripts/get-account-from-id'; -import { deckStore } from './ui/deck/deck-store'; -import { miLocalStorage } from './local-storage'; -import { claimAchievement, claimedAchievements } from './scripts/achievements'; -import { fetchCustomEmojis } from './custom-emojis'; -import { mainRouter } from './router'; +import { deckStore } from '@/ui/deck/deck-store'; +import { miLocalStorage } from '@/local-storage'; +import { claimAchievement, claimedAchievements } from '@/scripts/achievements'; +import { fetchCustomEmojis } from '@/custom-emojis'; +import { mainRouter } from '@/router'; console.info(`Misskey v${version}`); @@ -55,7 +43,9 @@ if (_DEV_) { console.info(`vue ${vueVersion}`); + // eslint-disable-next-line @typescript-eslint/no-explicit-any (window as any).$i = $i; + // eslint-disable-next-line @typescript-eslint/no-explicit-any (window as any).$store = defaultStore; window.addEventListener('error', event => { @@ -184,7 +174,7 @@ fetchInstanceMetaPromise.then(() => { try { await fetchCustomEmojis(); -} catch (err) {} +} catch (err) { /* empty */ } const app = createApp( new URLSearchParams(window.location.search).has('zen') ? defineAsyncComponent(() => import('@/ui/zen.vue')) : @@ -212,20 +202,20 @@ await deckStore.ready; // https://github.com/misskey-dev/misskey/pull/8575#issuecomment-1114239210 // なぜかinit.tsの内容が2回実行されることがあるため、mountするdivを1つに制限する -const rootEl = (() => { +const rootEl = ((): HTMLElement => { const MISSKEY_MOUNT_DIV_ID = 'misskey_app'; - const currentEl = document.getElementById(MISSKEY_MOUNT_DIV_ID); + const currentRoot = document.getElementById(MISSKEY_MOUNT_DIV_ID); - if (currentEl) { + if (currentRoot) { console.warn('multiple import detected'); - return currentEl; + return currentRoot; } - const rootEl = document.createElement('div'); - rootEl.id = MISSKEY_MOUNT_DIV_ID; - document.body.appendChild(rootEl); - return rootEl; + const root = document.createElement('div'); + root.id = MISSKEY_MOUNT_DIV_ID; + document.body.appendChild(root); + return root; })(); app.mount(rootEl); @@ -256,8 +246,7 @@ if (lastVersion !== version) { popup(defineAsyncComponent(() => import('@/components/MkUpdated.vue')), {}, {}, 'closed'); } } - } catch (err) { - } + } catch (err) { /* empty */ } } await defaultStore.ready; diff --git a/packages/frontend/src/local-storage.ts b/packages/frontend/src/local-storage.ts index 9a288f264..441a35747 100644 --- a/packages/frontend/src/local-storage.ts +++ b/packages/frontend/src/local-storage.ts @@ -24,6 +24,7 @@ type Keys = 'customCss' | 'message_drafts' | 'scratchpad' | + 'debug' | `miux:${string}` | `ui:folder:${string}` | `themes:${string}` | @@ -32,7 +33,7 @@ type Keys = 'emojis' // DEPRECATED, stored in indexeddb (13.9.0~); export const miLocalStorage = { - getItem: (key: Keys) => window.localStorage.getItem(key), - setItem: (key: Keys, value: string) => window.localStorage.setItem(key, value), - removeItem: (key: Keys) => window.localStorage.removeItem(key), + getItem: (key: Keys): string | null => window.localStorage.getItem(key), + setItem: (key: Keys, value: string): void => window.localStorage.setItem(key, value), + removeItem: (key: Keys): void => window.localStorage.removeItem(key), };