Skip to content

Commit

Permalink
Prevent overriding of theme's default toolbar settings (#4120)
Browse files Browse the repository at this point in the history
* theme toolbar's handlers overwritten fix

* Fix handling for true values and add CHANGELOG

---------

Co-authored-by: david ruty <david.ruty@arche-mc2.fr>
Co-authored-by: Zihua Li <i@zihua.li>
  • Loading branch information
3 people committed Apr 22, 2024
1 parent 9062cb0 commit 69e0433
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions 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/).
Expand Down
27 changes: 15 additions & 12 deletions packages/quill/src/core/quill.ts
Expand Up @@ -766,7 +766,7 @@ function expandModuleConfig(config: Record<string, unknown> | undefined) {
...expanded,
[key]: value === true ? {} : value,
}),
{},
{} as Record<string, unknown>,
);
}

Expand Down Expand Up @@ -797,23 +797,26 @@ function expandConfig(
const { modules: quillModuleDefaults, ...quillDefaults } = Quill.DEFAULTS;
const { modules: themeModuleDefaults, ...themeDefaults } = theme.DEFAULTS;

const modules: ExpandedQuillOptions['modules'] = merge(
{},
expandModuleConfig(quillModuleDefaults),
expandModuleConfig(themeModuleDefaults),
expandModuleConfig(options.modules),
);
let userModuleOptions = expandModuleConfig(options.modules);
// Special case toolbar shorthand
if (
modules != null &&
modules.toolbar &&
modules.toolbar.constructor !== Object
userModuleOptions != null &&
userModuleOptions.toolbar &&
userModuleOptions.toolbar.constructor !== Object
) {
modules.toolbar = {
container: modules.toolbar,
userModuleOptions = {
...userModuleOptions,
toolbar: { container: userModuleOptions.toolbar },
};
}

const modules: ExpandedQuillOptions['modules'] = merge(
{},
expandModuleConfig(quillModuleDefaults),
expandModuleConfig(themeModuleDefaults),
userModuleOptions,
);

const config = {
...quillDefaults,
...omitUndefinedValuesFromOptions(themeDefaults),
Expand Down
15 changes: 15 additions & 0 deletions packages/quill/test/unit/core/quill.spec.ts
Expand Up @@ -764,6 +764,21 @@ 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 ?? {},
)) {
// @ts-expect-error
expect(config.modules.toolbar.handlers[format]).toBe(handler);
}
});

test('toolbar format array', () => {
const config = expandConfig(`#${testContainerId}`, {
modules: {
Expand Down

0 comments on commit 69e0433

Please sign in to comment.