From 24f093e720906133a7ba556de894ab8c43baa06c Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Fri, 19 Apr 2024 14:40:13 +0800 Subject: [PATCH] Fix handling for true values and add CHANGELOG --- CHANGELOG.md | 2 ++ packages/quill/src/core/quill.ts | 36 ++++++++++----------- packages/quill/test/unit/core/quill.spec.ts | 16 +++++++++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad2b09df6..fdbdc9d3e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # [Unreleased] +- Prevent overriding of theme's default toolbar settings mistakenly [#4120](https://github.com/quilljs/quill/pull/4120) + # 2.0.0 We are thrilled to announce the release of Quill 2.0! Please check out the [announcement post](https://slab.com/blog/announcing-quill-2-0/). diff --git a/packages/quill/src/core/quill.ts b/packages/quill/src/core/quill.ts index c93c465d48..2bfc29c00b 100644 --- a/packages/quill/src/core/quill.ts +++ b/packages/quill/src/core/quill.ts @@ -766,7 +766,7 @@ function expandModuleConfig(config: Record | undefined) { ...expanded, [key]: value === true ? {} : value, }), - {}, + {} as Record, ); } @@ -784,30 +784,29 @@ function expandConfig( if (!container) { throw new Error('Invalid Quill container'); } - - const userOptions={...options}; const shouldUseDefaultTheme = - !userOptions.theme || userOptions.theme === Quill.DEFAULTS.theme; + !options.theme || options.theme === Quill.DEFAULTS.theme; const theme = shouldUseDefaultTheme ? Theme - : Quill.import(`themes/${userOptions.theme}`); + : Quill.import(`themes/${options.theme}`); if (!theme) { - throw new Error(`Invalid theme ${userOptions.theme}. Did you register it?`); + throw new Error(`Invalid theme ${options.theme}. Did you register it?`); } const { modules: quillModuleDefaults, ...quillDefaults } = Quill.DEFAULTS; const { modules: themeModuleDefaults, ...themeDefaults } = theme.DEFAULTS; + let userModuleOptions = expandModuleConfig(options.modules); // Special case toolbar shorthand - if ( - userOptions.modules != null && - userOptions.modules.toolbar && - userOptions.modules.toolbar.constructor !== Object + userModuleOptions != null && + userModuleOptions.toolbar && + userModuleOptions.toolbar.constructor !== Object ) { - userOptions.modules.toolbar = { - container: userOptions.modules.toolbar + userModuleOptions = { + ...userModuleOptions, + toolbar: { container: userModuleOptions.toolbar }, }; } @@ -815,24 +814,23 @@ function expandConfig( {}, expandModuleConfig(quillModuleDefaults), expandModuleConfig(themeModuleDefaults), - expandModuleConfig(userOptions.modules), + userModuleOptions, ); - const config = { ...quillDefaults, ...omitUndefinedValuesFromOptions(themeDefaults), - ...omitUndefinedValuesFromOptions(userOptions), + ...omitUndefinedValuesFromOptions(options), }; - let registry = userOptions.registry; + let registry = options.registry; if (registry) { - if (userOptions.formats) { + if (options.formats) { debug.warn('Ignoring "formats" option because "registry" is specified'); } } else { - registry = userOptions.formats - ? createRegistryWithFormats(userOptions.formats, config.registry, debug) + registry = options.formats + ? createRegistryWithFormats(options.formats, config.registry, debug) : config.registry; } diff --git a/packages/quill/test/unit/core/quill.spec.ts b/packages/quill/test/unit/core/quill.spec.ts index ca9f413afc..1d7cdf20a0 100644 --- a/packages/quill/test/unit/core/quill.spec.ts +++ b/packages/quill/test/unit/core/quill.spec.ts @@ -764,6 +764,22 @@ describe('Quill', () => { }); }); + test('toolbar container shorthand with theme options', () => { + const config = expandConfig(`#${testContainerId}`, { + modules: { + toolbar: document.querySelector(`#${testContainerId}`), + }, + theme: 'snow', + }); + for (const [format, handler] of Object.entries( + Snow.DEFAULTS.modules.toolbar!.handlers ?? {}, + )) { + console.log('===format', format); + // @ts-expect-error + expect(config.modules.toolbar.handlers[format]).toBe(handler); + } + }); + test('toolbar format array', () => { const config = expandConfig(`#${testContainerId}`, { modules: {