From 9677f742c80c7e5ef79779ebef97d39320dd3c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 4 Jan 2023 18:21:18 +0800 Subject: [PATCH] refactor(status): remove null fields --- components/publish/PublishLanguagePicker.vue | 34 +++++++------------- components/publish/PublishWidget.vue | 4 +-- composables/statusDrafts.ts | 33 ++++++++++--------- composables/tiptap.ts | 2 +- types/index.ts | 6 ++-- types/utils.ts | 5 +++ 6 files changed, 38 insertions(+), 46 deletions(-) diff --git a/components/publish/PublishLanguagePicker.vue b/components/publish/PublishLanguagePicker.vue index 57e1a7a3..bd7f139b 100644 --- a/components/publish/PublishLanguagePicker.vue +++ b/components/publish/PublishLanguagePicker.vue @@ -3,7 +3,7 @@ import ISO6391 from 'iso-639-1' import Fuse from 'fuse.js' let { modelValue } = $defineModel<{ - modelValue: string | null | undefined + modelValue: string }>() const { t } = useI18n() @@ -11,17 +11,14 @@ const { t } = useI18n() const languageKeyword = $ref('') const languageList: { - code: string | null + code: string nativeName: string - name?: string -}[] = [{ - code: null, - nativeName: t('language.none'), -}, ...ISO6391.getAllCodes().map(code => ({ + name: string +}[] = ISO6391.getAllCodes().map(code => ({ code, nativeName: ISO6391.getNativeName(code), name: ISO6391.getName(code), -}))] +})) const fuse = new Fuse(languageList, { keys: ['code', 'nativeName', 'name'], @@ -32,15 +29,11 @@ const languages = $computed(() => languageKeyword.trim() ? fuse.search(languageKeyword).map(r => r.item) : [...languageList].sort(({ code: a }, { code: b }) => { - return a === modelValue - ? -1 - : b === modelValue - ? 1 - : (a === null ? -1 : b === null ? 1 : a.localeCompare(b)) + return a === modelValue ? -1 : b === modelValue ? 1 : a.localeCompare(b) }), ) -function chooseLanguage(language: string | null) { +function chooseLanguage(language: string) { modelValue = language } @@ -57,16 +50,11 @@ function chooseLanguage(language: string | null) { - {{ nativeName }} - - + /> diff --git a/components/publish/PublishWidget.vue b/components/publish/PublishWidget.vue index 4d35d581..cb1c4ed1 100644 --- a/components/publish/PublishWidget.vue +++ b/components/publish/PublishWidget.vue @@ -333,9 +333,7 @@ defineExpose({ diff --git a/composables/statusDrafts.ts b/composables/statusDrafts.ts index ec4c93bf..6c0e040f 100644 --- a/composables/statusDrafts.ts +++ b/composables/statusDrafts.ts @@ -1,32 +1,35 @@ -import type { Account, Status } from 'masto' +import type { Account, CreateStatusParams, Status } from 'masto' import { STORAGE_KEY_DRAFTS } from '~/constants' import type { Draft, DraftMap } from '~/types' +import type { Mutable } from '~/types/utils' export const currentUserDrafts = process.server ? computed(() => ({})) : useUserLocalStorage(STORAGE_KEY_DRAFTS, () => ({})) -export function getDefaultDraft(options: Partial> = {}): Draft { +export function getDefaultDraft(options: Partial & Omit> = {}): Draft { const { - status = '', - inReplyToId, - visibility = 'public', attachments = [], initialText = '', - sensitive = false, - spoilerText = '', + + status, + inReplyToId, + visibility, + sensitive, + spoilerText, language, } = options return { - params: { - status, - inReplyToId, - visibility, - sensitive, - spoilerText, - language, - }, attachments, initialText, + + params: { + status: status || '', + inReplyToId, + visibility: visibility || 'public', + sensitive: sensitive ?? false, + spoilerText: spoilerText || '', + language: language || 'en', + }, } } diff --git a/composables/tiptap.ts b/composables/tiptap.ts index 1886e6a5..fa610a31 100644 --- a/composables/tiptap.ts +++ b/composables/tiptap.ts @@ -18,7 +18,7 @@ import { CustomEmoji } from './tiptap/custom-emoji' import { Emoji } from './tiptap/emoji' export interface UseTiptapOptions { - content: Ref + content: Ref placeholder: Ref onSubmit: () => void onFocus: () => void diff --git a/types/index.ts b/types/index.ts index 67c2962e..7c38edf1 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,6 +1,6 @@ import type { Account, AccountCredentials, Attachment, CreateStatusParams, Emoji, Instance, MastoClient, Notification, PushSubscription, Status } from 'masto' import type { Ref } from 'vue' -import type { Mutable } from './utils' +import type { MarkNonNullable, Mutable } from './utils' export interface AppInfo { id: string @@ -59,9 +59,7 @@ export type TranslateFn = ReturnType['t'] export interface Draft { editingStatus?: Status initialText?: string - params: Omit, 'status'> & { - status?: Exclude - } + params: MarkNonNullable, 'status' | 'language' | 'sensitive' | 'spoilerText' | 'visibility'> attachments: Attachment[] } export type DraftMap = Record diff --git a/types/utils.ts b/types/utils.ts index e3bcb6cc..b95658fb 100644 --- a/types/utils.ts +++ b/types/utils.ts @@ -1,3 +1,8 @@ export type Mutable = { -readonly[P in keyof T]: T[P] } + +export type Overwrite = Omit & O +export type MarkNonNullable = Overwrite +}>