From 699eafd0e403d9d643f7db555ff6bd7edc936182 Mon Sep 17 00:00:00 2001 From: zzxming Date: Mon, 25 Nov 2024 16:22:55 +0800 Subject: [PATCH] fix: only display warn when lang is not config --- packages/fluent-editor/src/config/editor.config.ts | 1 + packages/fluent-editor/src/fluent-editor.ts | 8 ++++++-- packages/fluent-editor/src/utils/debounce.ts | 3 ++- packages/fluent-editor/src/utils/is.ts | 5 +++++ packages/fluent-editor/src/utils/method.ts | 5 ----- 5 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 packages/fluent-editor/src/utils/is.ts diff --git a/packages/fluent-editor/src/config/editor.config.ts b/packages/fluent-editor/src/config/editor.config.ts index ca0239c..cde8576 100644 --- a/packages/fluent-editor/src/config/editor.config.ts +++ b/packages/fluent-editor/src/config/editor.config.ts @@ -8,6 +8,7 @@ export const LANG_CONF = { 'zh-CN': ZH_CN, } export const CHANGE_LANGUAGE_EVENT = 'change-language' +export const defaultLanguage = 'en-US' // Image export const IMAGE_UPLOADER_MIME_TYPES = [ diff --git a/packages/fluent-editor/src/fluent-editor.ts b/packages/fluent-editor/src/fluent-editor.ts index 8f0c756..1b91128 100644 --- a/packages/fluent-editor/src/fluent-editor.ts +++ b/packages/fluent-editor/src/fluent-editor.ts @@ -2,7 +2,7 @@ import type { ExpandedQuillOptions, Module, Parchment as TypeParchment } from 'q import type { IEditorConfig } from './config/types' import Quill from 'quill' import { FontStyle, LineHeightStyle, SizeStyle, TextIndentStyle } from './attributors' -import { CHANGE_LANGUAGE_EVENT, getListValue, ICONS_CONFIG, inputFile, LANG_CONF } from './config' +import { CHANGE_LANGUAGE_EVENT, defaultLanguage, getListValue, ICONS_CONFIG, inputFile, LANG_CONF } from './config' import Counter from './counter' // 字符统计 import CustomClipboard from './custom-clipboard' // 粘贴板 import CustomImage from './custom-image/BlotFormatter' // 图片 @@ -22,6 +22,7 @@ import Strike from './strike' // 删除线 import CustomSyntax from './syntax' // 代码块高亮 import BetterTable from './table/better-table' // 表格 import Toolbar from './toolbar' // 工具栏 +import { isUndefined } from './utils/is' import Video from './video' // 视频 // import GlobalLink from './global-link' // 全局链接 // import QuickMenu from './quick-menu' // 快捷菜单 @@ -30,9 +31,12 @@ export interface I18NOptions { langText: Record } function resolveLanguageOption(options: Partial): I18NOptions { + if (isUndefined(options.lang)) { + options.lang = defaultLanguage + } if (!(options.lang in LANG_CONF)) { console.warn(`The language ${options.lang} is not supported. Use the default language: en-US`) - options.lang = 'en-US' + options.lang = defaultLanguage } return { lang: options.lang, diff --git a/packages/fluent-editor/src/utils/debounce.ts b/packages/fluent-editor/src/utils/debounce.ts index 6c8f4f4..9c6dfd5 100644 --- a/packages/fluent-editor/src/utils/debounce.ts +++ b/packages/fluent-editor/src/utils/debounce.ts @@ -1,4 +1,5 @@ -import { isObject, root } from './method' +import { isObject } from './is' +import { root } from './method' export function debounce(func, wait, options = undefined) { let lastArgs, diff --git a/packages/fluent-editor/src/utils/is.ts b/packages/fluent-editor/src/utils/is.ts new file mode 100644 index 0000000..93d5798 --- /dev/null +++ b/packages/fluent-editor/src/utils/is.ts @@ -0,0 +1,5 @@ +export function isObject(value): value is object { + const type = typeof value + return value != null && (type === 'object' || type === 'function') +} +export const isUndefined = (val: unknown): val is undefined => val === undefined diff --git a/packages/fluent-editor/src/utils/method.ts b/packages/fluent-editor/src/utils/method.ts index d1d2d44..c542a05 100644 --- a/packages/fluent-editor/src/utils/method.ts +++ b/packages/fluent-editor/src/utils/method.ts @@ -20,11 +20,6 @@ const freeSelf export const root = freeGlobalThis || freeGlobal || freeSelf || new Function('return this')() -export function isObject(value) { - const type = typeof value - return value != null && (type === 'object' || type === 'function') -} - export function compareArray(arr1, arr2) { if (arr1.length !== arr2.length) { return false