Skip to content

Commit 24f093e

Browse files
committed
Fix handling for true values and add CHANGELOG
1 parent 0768009 commit 24f093e

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# [Unreleased]
22

3+
- Prevent overriding of theme's default toolbar settings mistakenly [#4120](https://github.com/quilljs/quill/pull/4120)
4+
35
# 2.0.0
46

57
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/).

packages/quill/src/core/quill.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ function expandModuleConfig(config: Record<string, unknown> | undefined) {
766766
...expanded,
767767
[key]: value === true ? {} : value,
768768
}),
769-
{},
769+
{} as Record<string, unknown>,
770770
);
771771
}
772772

@@ -784,55 +784,53 @@ function expandConfig(
784784
if (!container) {
785785
throw new Error('Invalid Quill container');
786786
}
787-
788-
const userOptions={...options};
789787

790788
const shouldUseDefaultTheme =
791-
!userOptions.theme || userOptions.theme === Quill.DEFAULTS.theme;
789+
!options.theme || options.theme === Quill.DEFAULTS.theme;
792790
const theme = shouldUseDefaultTheme
793791
? Theme
794-
: Quill.import(`themes/${userOptions.theme}`);
792+
: Quill.import(`themes/${options.theme}`);
795793
if (!theme) {
796-
throw new Error(`Invalid theme ${userOptions.theme}. Did you register it?`);
794+
throw new Error(`Invalid theme ${options.theme}. Did you register it?`);
797795
}
798796

799797
const { modules: quillModuleDefaults, ...quillDefaults } = Quill.DEFAULTS;
800798
const { modules: themeModuleDefaults, ...themeDefaults } = theme.DEFAULTS;
801799

800+
let userModuleOptions = expandModuleConfig(options.modules);
802801
// Special case toolbar shorthand
803-
804802
if (
805-
userOptions.modules != null &&
806-
userOptions.modules.toolbar &&
807-
userOptions.modules.toolbar.constructor !== Object
803+
userModuleOptions != null &&
804+
userModuleOptions.toolbar &&
805+
userModuleOptions.toolbar.constructor !== Object
808806
) {
809-
userOptions.modules.toolbar = {
810-
container: userOptions.modules.toolbar
807+
userModuleOptions = {
808+
...userModuleOptions,
809+
toolbar: { container: userModuleOptions.toolbar },
811810
};
812811
}
813812

814813
const modules: ExpandedQuillOptions['modules'] = merge(
815814
{},
816815
expandModuleConfig(quillModuleDefaults),
817816
expandModuleConfig(themeModuleDefaults),
818-
expandModuleConfig(userOptions.modules),
817+
userModuleOptions,
819818
);
820819

821-
822820
const config = {
823821
...quillDefaults,
824822
...omitUndefinedValuesFromOptions(themeDefaults),
825-
...omitUndefinedValuesFromOptions(userOptions),
823+
...omitUndefinedValuesFromOptions(options),
826824
};
827825

828-
let registry = userOptions.registry;
826+
let registry = options.registry;
829827
if (registry) {
830-
if (userOptions.formats) {
828+
if (options.formats) {
831829
debug.warn('Ignoring "formats" option because "registry" is specified');
832830
}
833831
} else {
834-
registry = userOptions.formats
835-
? createRegistryWithFormats(userOptions.formats, config.registry, debug)
832+
registry = options.formats
833+
? createRegistryWithFormats(options.formats, config.registry, debug)
836834
: config.registry;
837835
}
838836

packages/quill/test/unit/core/quill.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,22 @@ describe('Quill', () => {
764764
});
765765
});
766766

767+
test('toolbar container shorthand with theme options', () => {
768+
const config = expandConfig(`#${testContainerId}`, {
769+
modules: {
770+
toolbar: document.querySelector(`#${testContainerId}`),
771+
},
772+
theme: 'snow',
773+
});
774+
for (const [format, handler] of Object.entries(
775+
Snow.DEFAULTS.modules.toolbar!.handlers ?? {},
776+
)) {
777+
console.log('===format', format);
778+
// @ts-expect-error
779+
expect(config.modules.toolbar.handlers[format]).toBe(handler);
780+
}
781+
});
782+
767783
test('toolbar format array', () => {
768784
const config = expandConfig(`#${testContainerId}`, {
769785
modules: {

0 commit comments

Comments
 (0)