Skip to content

Commit

Permalink
feat: global customModules and customOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
KillerCodeMonkey committed May 2, 2020
1 parent 5d3c973 commit 0ae143d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ See [Quill Configuration](https://quilljs.com/docs/configuration/) for a full li

The `QuillModule` exports the `defaultModules` if you want to extend them :).

### Custom Modules and options/formats

- use customOptions for adding for example custom font sizes or other options/formats
- use customModules for adding and overwriting modules, e.g. image-resize or your own modules

## QuillEditorComponent

### Hint
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ngx-quill",
"license": "MIT",
"version": "9.0.0",
"version": "9.1.0",
"author": {
"name": "Bengt Weiße"
},
Expand Down
14 changes: 14 additions & 0 deletions src/quill-editor.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,14 @@ describe('QuillEditor - base config', () => {
declarations: [TestComponent, TestToolbarComponent],
imports: [FormsModule, QuillModule],
providers: QuillModule.forRoot({
customModules: [{
path: 'modules/custom',
implementation: CustomModule
}],
customOptions: [{
import: 'attributors/style/size',
whitelist: ['14']
}],
bounds: 'body',
debug: false,
format: 'object',
Expand All @@ -1122,6 +1130,8 @@ describe('QuillEditor - base config', () => {

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 @@ -1141,6 +1151,10 @@ 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(fixture).toMatchSnapshot()
})
Expand Down
18 changes: 5 additions & 13 deletions src/quill-editor.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {DOCUMENT, isPlatformServer} from '@angular/common'
import {DomSanitizer} from '@angular/platform-browser'

import {QUILL_CONFIG_TOKEN, QuillConfig, QuillModules} from './quill-editor.interfaces'
import {QUILL_CONFIG_TOKEN, QuillConfig, QuillModules, CustomOption, CustomModule} from './quill-editor.interfaces'

import Quill, { Delta } from 'quill'
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -31,16 +31,6 @@ import {defaultModules} from './quill-defaults'

import {getFormat} from './helpers'

export interface CustomOption {
import: string
whitelist: any[]
}

export interface CustomModule {
path: string
implementation: any
}

export interface Range {
index: number
length: number
Expand Down Expand Up @@ -244,13 +234,15 @@ export class QuillEditorComponent implements AfterViewInit, ControlValueAccessor
this.addClasses(this.classes)
}

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

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

Expand Down
12 changes: 12 additions & 0 deletions src/quill-editor.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { InjectionToken } from '@angular/core'

export interface CustomOption {
import: string
whitelist: any[]
}

export interface CustomModule {
implementation: any
path: string
}

export type QuillToolbarConfig = Array<Array< string | {
indent?: string
list?: string
Expand Down Expand Up @@ -41,6 +51,8 @@ export type QuillFormat = 'object' | 'json' | 'html' | 'text'

export interface QuillConfig {
bounds?: HTMLElement | string
customModules?: CustomModule[]
customOptions?: CustomOption[]
debug?: 'error' | 'warn' | 'log' | false
format?: QuillFormat
formats?: string[]
Expand Down
11 changes: 10 additions & 1 deletion src/quill-view.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ describe('Basic QuillViewComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
QuillModule.forRoot()
QuillModule.forRoot({
customModules: [{
path: 'modules/custom',
implementation: CustomModule
}],
customOptions: [{
import: 'attributors/style/size',
whitelist: ['14']
}],
})
],
providers: QuillModule.forRoot().providers
})
Expand Down
8 changes: 5 additions & 3 deletions src/quill-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@angular/core'

import { defaultModules } from './quill-defaults'
import { CustomOption, CustomModule } from './quill-editor.component'
import { CustomOption, CustomModule } from './quill-editor.interfaces'
import {getFormat} from './helpers'

@Component({
Expand Down Expand Up @@ -88,13 +88,15 @@ export class QuillViewComponent implements AfterViewInit, OnChanges {
const modules = Object.assign({}, this.modules || (this.config.modules || defaultModules))
modules.toolbar = false

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

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

Expand Down
13 changes: 13 additions & 0 deletions src/quill.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CommonModule } from '@angular/common'
import { ModuleWithProviders, NgModule } from '@angular/core'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Quill = require('quill')

import { defaultModules } from './quill-defaults'
import { QuillEditorComponent } from './quill-editor.component'
Expand All @@ -19,6 +21,17 @@ import { QuillViewComponent } from './quill-view.component'
})
export class QuillModule {
static forRoot(config?: QuillConfig): ModuleWithProviders {
if (config) {
config.customOptions?.forEach((customOption) => {
const newCustomOption = Quill.import(customOption.import)
newCustomOption.whitelist = customOption.whitelist
Quill.register(newCustomOption, true)
})

config.customModules?.forEach(({implementation, path}) => {
Quill.register(path, implementation)
})
}
return {
ngModule: QuillModule,
providers: [
Expand Down

0 comments on commit 0ae143d

Please sign in to comment.