From f7dff673adb99a5ff10e281135f6538a15255b38 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 25 Nov 2022 14:01:28 +0000 Subject: [PATCH] feat: respect current server's char limit --- components/publish/PublishWidget.vue | 3 +-- composables/tiptap.ts | 3 +-- composables/users.ts | 9 +++++++-- constants/index.ts | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/publish/PublishWidget.vue b/components/publish/PublishWidget.vue index dcf84815..561d7634 100644 --- a/components/publish/PublishWidget.vue +++ b/components/publish/PublishWidget.vue @@ -3,7 +3,6 @@ import type { CreateStatusParams, StatusVisibility } from 'masto' import { fileOpen } from 'browser-fs-access' import { useDropZone } from '@vueuse/core' import { EditorContent } from '@tiptap/vue-3' -import { POST_CHARS_LIMIT } from '~~/constants' const { draftKey, @@ -174,7 +173,7 @@ onUnmounted(() => { :class="isExpanded ? 'min-h-120px' : ''" />
- {{ POST_CHARS_LIMIT - editor?.storage.characterCount.characters() }} + {{ characterLimit - editor?.storage.characterCount.characters() }}
diff --git a/composables/tiptap.ts b/composables/tiptap.ts index dc751ffd..bcc914a7 100644 --- a/composables/tiptap.ts +++ b/composables/tiptap.ts @@ -10,7 +10,6 @@ import { Plugin } from 'prosemirror-state' import type { Ref } from 'vue' import { HashSuggestion, MentionSuggestion } from './tiptap/suggestion' -import { POST_CHARS_LIMIT } from '~/constants' export interface UseTiptapOptions { content: Ref @@ -44,7 +43,7 @@ export function useTiptap(options: UseTiptapOptions) { placeholder, }), CharacterCount.configure({ - limit: POST_CHARS_LIMIT, + limit: characterLimit.value, }), CodeBlock, Extension.create({ diff --git a/composables/users.ts b/composables/users.ts index 12b8ac67..a35d060f 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -1,7 +1,7 @@ -import type { AccountCredentials } from 'masto' import { login as loginMasto } from 'masto' +import type { AccountCredentials, Instance } from 'masto' import type { UserLogin } from '~/types' -import { DEFAULT_SERVER, STORAGE_KEY_CURRENT_USER, STORAGE_KEY_USERS } from '~/constants' +import { DEFAULT_POST_CHARS_LIMIT, DEFAULT_SERVER, STORAGE_KEY_CURRENT_USER, STORAGE_KEY_SERVER, STORAGE_KEY_USERS } from '~/constants' const users = useLocalStorage(STORAGE_KEY_USERS, [], { deep: true }) const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, '') @@ -21,6 +21,10 @@ export const currentServer = computed(() => currentUser.value?.server || export const useUsers = () => users +export const currentInstance = useLocalStorage>(STORAGE_KEY_SERVER, {}, { deep: true }) + +export const characterLimit = computed(() => currentInstance.value.configuration?.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT) + export async function loginTo(user: UserLogin & { account?: AccountCredentials }) { const existing = users.value.findIndex(u => u.server === user.server && u.token === user.token) if (existing !== -1) { @@ -37,6 +41,7 @@ export async function loginTo(user: UserLogin & { account?: AccountCredentials } }) const me = await masto.accounts.verifyCredentials() user.account = me + currentInstance.value = await masto.instances.fetch() users.value.push(user) currentUserId.value = me.id diff --git a/constants/index.ts b/constants/index.ts index f28125ce..af94e552 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -4,10 +4,10 @@ export const HOST_DOMAIN = process.dev ? 'http://localhost:3000' : 'https://elk.zone' -export const POST_CHARS_LIMIT = 500 - +export const DEFAULT_POST_CHARS_LIMIT = 500 export const DEFAULT_SERVER = 'mas.to' +export const STORAGE_KEY_SERVER = 'elk-current-server' export const STORAGE_KEY_DRAFTS = 'elk-drafts' export const STORAGE_KEY_USERS = 'elk-users' export const STORAGE_KEY_CURRENT_USER = 'elk-current-user'