Skip to content

Commit

Permalink
fix: register config modules/options only once
Browse files Browse the repository at this point in the history
  • Loading branch information
KillerCodeMonkey committed May 3, 2020
1 parent 0ae143d commit da1bac8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/quill-editor.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,14 @@ describe('Advanced QuillEditorComponent', () => {

describe('QuillEditor - base config', () => {
let fixture: ComponentFixture<TestComponent>
let registerSpy: jasmine.Spy

beforeAll(() => {
registerSpy = spyOn(QuillNamespace, 'register').and.callThrough()
})

beforeEach(() => {

TestBed.configureTestingModule({
declarations: [TestComponent, TestToolbarComponent],
imports: [FormsModule, QuillModule],
Expand Down Expand Up @@ -1126,11 +1132,12 @@ describe('QuillEditor - base config', () => {
trackChanges: 'all'
}).providers
}).compileComponents()

expect(registerSpy).toHaveBeenCalledWith('modules/custom', CustomModule)
})

it('renders editor with config', async () => {
fixture = TestBed.createComponent(TestComponent)
const spy = spyOn(QuillNamespace, 'register').and.callThrough()
const spy2 = spyOn(QuillNamespace, 'import').and.callThrough()
fixture.detectChanges()
await fixture.whenStable()
Expand All @@ -1152,9 +1159,8 @@ describe('QuillEditor - base config', () => {
expect(JSON.stringify(fixture.componentInstance.title)).toEqual(JSON.stringify({ ops: [{ attributes: { bold: true }, insert: `content`}, {'insert':'\n'}] }))
expect(editor.root.dataset.placeholder).toEqual('placeholder')
expect(spy2).toHaveBeenCalledWith('attributors/style/size')
expect(spy).toHaveBeenCalledWith({'attrName': 'size', 'keyName': 'font-size', 'scope': 5, 'whitelist': ['14']}, true)
expect(spy).toHaveBeenCalledWith('formats/size', {'attrName': 'size', 'keyName': 'font-size', 'scope': 5, 'whitelist': ['14']}, true)
expect(spy).toHaveBeenCalledWith('modules/custom', CustomModule)
expect(registerSpy).toHaveBeenCalledWith({'attrName': 'size', 'keyName': 'font-size', 'scope': 5, 'whitelist': ['14']}, true)
expect(registerSpy).toHaveBeenCalledWith('formats/size', {'attrName': 'size', 'keyName': 'font-size', 'scope': 5, 'whitelist': ['14']}, true)

expect(fixture).toMatchSnapshot()
})
Expand Down
6 changes: 2 additions & 4 deletions src/quill-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,13 @@ export class QuillEditorComponent implements AfterViewInit, ControlValueAccessor
this.addClasses(this.classes)
}

const customOptions = [...(this.config.customOptions || []), ...this.customOptions]
customOptions.forEach((customOption) => {
this.customOptions.forEach((customOption) => {
const newCustomOption = QuillNamespace.import(customOption.import)
newCustomOption.whitelist = customOption.whitelist
QuillNamespace.register(newCustomOption, true)
})

const customModules = [...(this.config.customModules || []), ...this.customModules]
customModules.forEach(({implementation, path}) => {
this.customModules.forEach(({implementation, path}) => {
QuillNamespace.register(path, implementation)
})

Expand Down
6 changes: 2 additions & 4 deletions src/quill-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,13 @@ export class QuillViewComponent implements AfterViewInit, OnChanges {
const modules = Object.assign({}, this.modules || (this.config.modules || defaultModules))
modules.toolbar = false

const customOptions = [...(this.config.customOptions || []), ...this.customOptions]
customOptions.forEach((customOption) => {
this.customOptions.forEach((customOption) => {
const newCustomOption = QuillNamespace.import(customOption.import)
newCustomOption.whitelist = customOption.whitelist
QuillNamespace.register(newCustomOption, true)
})

const customModules = [...(this.config.customModules || []), ...this.customModules]
customModules.forEach(({implementation, path}) => {
this.customModules.forEach(({implementation, path}) => {
QuillNamespace.register(path, implementation)
})

Expand Down
7 changes: 7 additions & 0 deletions src/quill.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { QuillViewComponent } from './quill-view.component'
export class QuillModule {
static forRoot(config?: QuillConfig): ModuleWithProviders {
if (config) {
// Only register custom options and modules once
config.customOptions?.forEach((customOption) => {
const newCustomOption = Quill.import(customOption.import)
newCustomOption.whitelist = customOption.whitelist
Expand All @@ -31,7 +32,13 @@ export class QuillModule {
config.customModules?.forEach(({implementation, path}) => {
Quill.register(path, implementation)
})

// set default modules as modules if not modules key passed on custom config
if (!config.modules) {
config.modules = defaultModules
}
}

return {
ngModule: QuillModule,
providers: [
Expand Down

0 comments on commit da1bac8

Please sign in to comment.