From b39db090f5d16617256b2b291a0cf9bbd895d0f3 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Wed, 12 Jan 2022 15:21:01 +0000 Subject: [PATCH] Message handling improvements -Define message keys in a single place -Add fallback for missing messages -Slight reduction in filesize --- messages.ts | 82 ++++++++++++++++++++++++++++++++++++++++++ src/index.d.ts | 68 +++-------------------------------- src/store/mutations.ts | 58 ++---------------------------- src/store/state.ts | 65 ++------------------------------- src/util.ts | 29 +++++++++++++-- 5 files changed, 118 insertions(+), 184 deletions(-) create mode 100644 messages.ts diff --git a/messages.ts b/messages.ts new file mode 100644 index 0000000..1f1e99d --- /dev/null +++ b/messages.ts @@ -0,0 +1,82 @@ +/* + * Copyright 2022 James Lyne + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const globalMessages = [ + 'chatNoMessages', + 'chatTitle', + 'chatLogin', + 'chatSend', + 'chatPlaceholder', + 'chatErrorDisabled', + 'chatErrorUnknown', + 'serversHeading', + 'worldsSkeleton', + 'playersSkeleton', + 'playersTitle', + 'playersTitleHidden', + 'playersTitleOtherWorld', + 'playersSearchPlaceholder', + 'playersSearchSkeleton', + 'followingHeading', + 'followingUnfollow', + 'followingTitleUnfollow', + 'followingHidden', + 'linkTitle', + 'loadingTitle', + 'locationRegion', + 'locationChunk', + 'contextMenuCopyLink', + 'contextMenuCenterHere', + 'toggleTitle', + 'mapTitle', + 'layersTitle', + 'copyToClipboardSuccess', + 'copyToClipboardError', + 'loginTitle', + 'loginHeading', + 'loginUsernameLabel', + 'loginPasswordLabel', + 'loginSubmit', + 'loginErrorUnknown', + 'loginErrorDisabled', + 'loginErrorIncorrect', + 'loginSuccess', + 'registerHeading', + 'registerDescription', + 'registerConfirmPasswordLabel', + 'registerCodeLabel', + 'registerSubmit', + 'registerErrorUnknown', + 'registerErrorDisabled', + 'registerErrorVerifyFailed', + 'registerErrorIncorrect', + 'logoutTitle', + 'logoutErrorUnknown', + 'logoutSuccess', + 'closeTitle', +] as const; + +export const serverMessages = [ + 'chatPlayerJoin', + 'chatPlayerQuit', + 'chatAnonymousJoin', + 'chatAnonymousQuit', + 'chatErrorNotAllowed', + 'chatErrorRequiresLogin', + 'chatErrorCooldown', + 'worldsHeading', + 'playersHeading', +] as const; diff --git a/src/index.d.ts b/src/index.d.ts index 1f8fda5..922779b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -21,6 +21,7 @@ import {Coords, DoneCallback, InternalTiles, PathOptions, PointTuple, PolylineOp import {CoordinatesControlOptions} from "@/leaflet/control/CoordinatesControl"; import {ClockControlOptions} from "@/leaflet/control/ClockControl"; import {LogoControlOptions} from "@/leaflet/control/LogoControl"; +import {globalMessages, serverMessages} from "../messages"; declare module "*.png" { const value: any; @@ -80,72 +81,13 @@ interface LiveAtlasServerDefinition { } // Messages defined directly in LiveAtlas and used for all servers -interface LiveAtlasGlobalMessageConfig { - chatNoMessages: string; - chatTitle: string; - chatLogin: string; - chatSend: string; - chatPlaceholder: string; - chatErrorDisabled: string; - chatErrorUnknown: string; - serversHeading: string; - worldsSkeleton: string; - playersSkeleton: string; - playersTitle: string; - playersTitleHidden: string; - playersTitleOtherWorld: string; - playersSearchPlaceholder: string; - playersSearchSkeleton: string; - followingHeading: string; - followingUnfollow: string; - followingTitleUnfollow: string; - followingHidden: string; - linkTitle: string; - loadingTitle: string; - locationRegion: string; - locationChunk: string; - contextMenuCopyLink: string; - contextMenuCenterHere: string; - toggleTitle: string; - mapTitle: string; - layersTitle: string; - copyToClipboardSuccess: string; - copyToClipboardError: string; - loginTitle: string; - loginHeading: string; - loginUsernameLabel: string; - loginPasswordLabel: string; - loginSubmit: string; - loginErrorUnknown: string; - loginErrorDisabled: string; - loginErrorIncorrect: string; - loginSuccess: string; - registerHeading: string; - registerDescription: string; - registerConfirmPasswordLabel: string; - registerCodeLabel: string; - registerSubmit: string; - registerErrorUnknown: string; - registerErrorDisabled: string; - registerErrorVerifyFailed: string; - registerErrorIncorrect: string; - logoutTitle: string; - logoutErrorUnknown: string; - logoutSuccess: string; - closeTitle: string; +type LiveAtlasGlobalMessageConfig = { + [K in typeof globalMessages[number]]: string; } // Messages defined by dynmap configuration responses and can vary per server -interface LiveAtlasServerMessageConfig { - chatPlayerJoin: string; - chatPlayerQuit: string; - chatAnonymousJoin: string; - chatAnonymousQuit: string; - chatErrorNotAllowed: string; - chatErrorRequiresLogin: string; - chatErrorCooldown: string; - worldsHeading: string; - playersHeading: string; +type LiveAtlasServerMessageConfig = { + [K in typeof serverMessages[number]]: string; } type LiveAtlasMessageConfig = LiveAtlasGlobalMessageConfig & LiveAtlasServerMessageConfig; diff --git a/src/store/mutations.ts b/src/store/mutations.ts index 21f0396..0a5e3ad 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -48,6 +48,7 @@ import { } from "@/index"; import DynmapMapProvider from "@/providers/DynmapMapProvider"; import Pl3xmapMapProvider from "@/providers/Pl3xmapMapProvider"; +import {getGlobalMessages} from "@/util"; export type CurrentMapPayload = { worldName: string; @@ -126,62 +127,7 @@ export const mutations: MutationTree & Mutations = { console.warn('Failed to load saved UI settings', e); } - const messages: LiveAtlasGlobalMessageConfig = { - chatTitle: messageConfig.chatTitle || '', - chatLogin: messageConfig.chatLogin || '', - chatNoMessages: messageConfig.chatNoMessages || '', - chatSend: messageConfig.chatSend || '', - chatPlaceholder: messageConfig.chatPlaceholder || '', - chatErrorDisabled: messageConfig.chatErrorDisabled || '', - chatErrorUnknown: messageConfig.chatErrorUnknown || '', - serversHeading: messageConfig.serversHeading || '', - worldsSkeleton: messageConfig.worldsSkeleton || '', - playersSkeleton: messageConfig.playersSkeleton || '', - playersTitle: messageConfig.playersTitle || '', - playersTitleHidden: messageConfig.playersTitleHidden || '', - playersTitleOtherWorld: messageConfig.playersTitleOtherWorld || '', - playersSearchPlaceholder: messageConfig.playersSearchPlaceholder || '', - playersSearchSkeleton: messageConfig.playersSearchSkeleton || '', - followingHeading: messageConfig.followingHeading || '', - followingHidden: messageConfig.followingHidden || '', - followingUnfollow: messageConfig.followingUnfollow || '', - followingTitleUnfollow: messageConfig.followingTitleUnfollow || '', - linkTitle: messageConfig.linkTitle || '', - loadingTitle: messageConfig.loadingTitle || '', - locationRegion: messageConfig.locationRegion || '', - locationChunk: messageConfig.locationChunk || '', - contextMenuCopyLink: messageConfig.contextMenuCopyLink || '', - contextMenuCenterHere: messageConfig.contextMenuCenterHere || '', - toggleTitle: messageConfig.toggleTitle || '', - mapTitle: messageConfig.mapTitle || '', - layersTitle: messageConfig.layersTitle || '', - copyToClipboardSuccess: messageConfig.copyToClipboardSuccess || '', - copyToClipboardError: messageConfig.copyToClipboardError || '', - loginTitle: messageConfig.loginTitle || '', - loginHeading: messageConfig.loginHeading || '', - loginUsernameLabel: messageConfig.loginUsernameLabel || '', - loginPasswordLabel: messageConfig.loginPasswordLabel || '', - loginSubmit: messageConfig.loginSubmit || '', - loginErrorUnknown: messageConfig.loginErrorUnknown || '', - loginErrorDisabled: messageConfig.loginErrorDisabled || '', - loginErrorIncorrect: messageConfig.loginErrorIncorrect || '', - loginSuccess: messageConfig.loginSuccess || '', - registerHeading: messageConfig.registerHeading || '', - registerDescription: messageConfig.registerDescription || '', - registerConfirmPasswordLabel: messageConfig.registerConfirmPasswordLabel || '', - registerCodeLabel: messageConfig.registerCodeLabel || '', - registerSubmit: messageConfig.registerSubmit || '', - registerErrorUnknown: messageConfig.registerErrorUnknown || '', - registerErrorDisabled: messageConfig.registerErrorDisabled || '', - registerErrorVerifyFailed: messageConfig.registerErrorVerifyFailed || '', - registerErrorIncorrect: messageConfig.registerErrorIncorrect || '', - logoutTitle: messageConfig.logoutTitle || '', - logoutErrorUnknown: messageConfig.logoutErrorUnknown || '', - logoutSuccess: messageConfig.logoutSuccess || '', - closeTitle: messageConfig.closeTitle || '', - } - - state.messages = Object.assign(state.messages, messages); + state.messages = Object.assign(state.messages, getGlobalMessages(messageConfig)); if(typeof uiConfig.playersAboveMarkers === 'boolean') { state.ui.playersAboveMarkers = uiConfig.playersAboveMarkers; diff --git a/src/store/state.ts b/src/store/state.ts index 0dfa80f..3d6f889 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -39,6 +39,7 @@ import { LiveAtlasMarkerSetContents } from "@/index"; import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition"; +import {getMessages} from "@/util"; export type State = { version: string; @@ -112,69 +113,7 @@ export const state: State = { }, configurationHash: undefined, - messages: { - chatPlayerJoin: '', - chatPlayerQuit: '', - chatAnonymousJoin: '', - chatAnonymousQuit: '', - chatNoMessages: '', - chatTitle: '', - chatLogin: '', - chatSend: '', - chatPlaceholder: '', - chatErrorNotAllowed: '', - chatErrorRequiresLogin: '', - chatErrorCooldown: '', - chatErrorDisabled: '', - chatErrorUnknown: '', - serversHeading: '', - worldsHeading: '', - worldsSkeleton: '', - playersSkeleton: '', - playersHeading: '', - playersTitle: '', - playersTitleHidden: '', - playersTitleOtherWorld: '', - playersSearchPlaceholder: '', - playersSearchSkeleton: '', - followingHeading: '', - followingUnfollow: '', - followingTitleUnfollow: '', - followingHidden: '', - linkTitle: '', - loadingTitle: '', - locationRegion: '', - locationChunk: '', - contextMenuCopyLink: '', - contextMenuCenterHere: '', - toggleTitle: '', - mapTitle: '', - layersTitle: '', - copyToClipboardSuccess: '', - copyToClipboardError: '', - loginTitle: '', - loginHeading: '', - loginUsernameLabel: '', - loginPasswordLabel: '', - loginSubmit: '', - loginErrorUnknown: '', - loginErrorDisabled: '', - loginErrorIncorrect: '', - loginSuccess: '', - registerHeading: '', - registerDescription: '', - registerConfirmPasswordLabel: '', - registerCodeLabel: '', - registerSubmit: '', - registerErrorUnknown: '', - registerErrorDisabled: '', - registerErrorVerifyFailed: '', - registerErrorIncorrect: '', - logoutTitle: '', - logoutErrorUnknown: '', - logoutSuccess: '', - closeTitle: '', - }, + messages: getMessages(), loggedIn: false, loginRequired: false, diff --git a/src/util.ts b/src/util.ts index 8909d5a..8136f0e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -16,8 +16,15 @@ import {useStore} from "@/store"; import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition"; -import {HeadQueueEntry, LiveAtlasPlayer, LiveAtlasPlayerImageSize} from "@/index"; +import { + HeadQueueEntry, + LiveAtlasGlobalMessageConfig, + LiveAtlasMessageConfig, + LiveAtlasPlayer, + LiveAtlasPlayerImageSize, +} from "@/index"; import {notify} from "@kyvg/vue3-notification"; +import {globalMessages, serverMessages} from "../messages"; const headCache = new Map(), headUnresolvedCache = new Map>(), @@ -212,10 +219,28 @@ export const decodeHTMLEntities = (text: string) => { return decodeTextarea.textContent || ''; } - export const clipboardSuccess = () => () => notify(useStore().state.messages.copyToClipboardSuccess); export const clipboardError = () => (e: Error) => { notify({ type: 'error', text: useStore().state.messages.copyToClipboardError }); console.error('Error copying to clipboard', e); }; + +export const getMessages = (config: any = {}) => { + return Object.assign(_getMessages(globalMessages, config), + _getMessages(serverMessages, config)) as LiveAtlasMessageConfig; +} + +export const getGlobalMessages = (config: any = {}) => { + return _getMessages(globalMessages, config) as LiveAtlasGlobalMessageConfig; +} + +const _getMessages = (messageKeys: any, config: any = {}) => { + const messages: any = {}; + + for(const key of messageKeys) { + messages[key] = config[key] || `Missing message: ${key}`; + } + + return messages as LiveAtlasGlobalMessageConfig; +}