Skip to content

Commit

Permalink
Fix handling for true values and add CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Apr 19, 2024
1 parent 0768009 commit 060808a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 17 additions & 19 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ function expandModuleConfig(config: Record<string, unknown> | undefined) {
...expanded,
[key]: value === true ? {} : value,
}),
{},
{} as Record<string, unknown>,
);
}

Expand All @@ -784,55 +784,53 @@ 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 },
};
}

const modules: ExpandedQuillOptions['modules'] = merge(
{},
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;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/quill/test/unit/core/quill.spec.ts
Original file line number Diff line number Diff line change
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 060808a

Please sign in to comment.