From 419f417f71e9cb8db198cc9f756a162aa08ed2ee Mon Sep 17 00:00:00 2001 From: Georgy Berezhnoy Date: Sat, 11 Sep 2021 15:18:43 +0300 Subject: [PATCH 1/4] Fix: pass user configuration to Tool prepare method --- src/components/modules/tools.ts | 5 +++-- test/cypress/tests/modules/Tools.spec.ts | 28 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/components/modules/tools.ts b/src/components/modules/tools.ts index 2b4c9bcc4..b96e6ef07 100644 --- a/src/components/modules/tools.ts +++ b/src/components/modules/tools.ts @@ -1,7 +1,7 @@ import Paragraph from '../../tools/paragraph/dist/bundle'; import Module from '../__module'; import * as _ from '../utils'; -import { SanitizerConfig, ToolConstructable, ToolSettings } from '../../../types'; +import {SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings} from '../../../types'; import BoldInlineTool from '../inline-tools/inline-tool-bold'; import ItalicInlineTool from '../inline-tools/inline-tool-italic'; import LinkInlineTool from '../inline-tools/inline-tool-link'; @@ -279,7 +279,7 @@ export default class Tools extends Module { }[] { const toolPreparationList: { function: (data: { toolName: string }) => void | Promise; - data: { toolName: string }; + data: { toolName: string; config: ToolConfig }; }[] = []; Object @@ -290,6 +290,7 @@ export default class Tools extends Module { function: _.isFunction(settings.class.prepare) ? settings.class.prepare : (): void => {}, data: { toolName, + config: settings.config, }, }); }); diff --git a/test/cypress/tests/modules/Tools.spec.ts b/test/cypress/tests/modules/Tools.spec.ts index 7e15a34f6..208916b59 100644 --- a/test/cypress/tests/modules/Tools.spec.ts +++ b/test/cypress/tests/modules/Tools.spec.ts @@ -72,6 +72,32 @@ describe('Tools module', () => { expect(err).to.be.instanceOf(Error); }); + + // eslint-disable-next-line cypress/no-async-tests + it('should call Tools prepare method with user config', async () => { + class WithSuccessfulPrepare { + // eslint-disable-next-line @typescript-eslint/no-empty-function + public static prepare = cy.stub() + } + + const config = { + property: 'value', + }; + + const module = constructModule({ + defaultBlock: 'withSuccessfulPrepare', + tools: { + withSuccessfulPrepare: { + class: WithSuccessfulPrepare as any, + config, + }, + }, + }); + + await module.prepare(); + + expect(WithSuccessfulPrepare.prepare).to.be.calledWithExactly({ toolName: 'withSuccessfulPrepare', config }); + }); }); context('collection accessors', () => { @@ -173,7 +199,7 @@ describe('Tools module', () => { expect(module.unavailable).to.be.instanceOf(Map); }); - it('should contain only ready to use Tools', () => { + it('should contain unavailable Tools', () => { expect(module.unavailable.has('withSuccessfulPrepare')).to.be.false; expect(module.unavailable.has('withoutPrepare')).to.be.false; expect(module.unavailable.has('withFailedPrepare')).to.be.true; From 792eea612ea46d09029127584e7cc7ac5e93c49a Mon Sep 17 00:00:00 2001 From: Georgy Berezhnoy Date: Sat, 11 Sep 2021 15:23:26 +0300 Subject: [PATCH 2/4] Fix lint --- src/components/modules/tools.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/modules/tools.ts b/src/components/modules/tools.ts index b96e6ef07..609449770 100644 --- a/src/components/modules/tools.ts +++ b/src/components/modules/tools.ts @@ -1,7 +1,7 @@ import Paragraph from '../../tools/paragraph/dist/bundle'; import Module from '../__module'; import * as _ from '../utils'; -import {SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings} from '../../../types'; +import { SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings } from '../../../types'; import BoldInlineTool from '../inline-tools/inline-tool-bold'; import ItalicInlineTool from '../inline-tools/inline-tool-italic'; import LinkInlineTool from '../inline-tools/inline-tool-link'; From 047a88e9b84c0154ff01fda5ffa9d966b309e644 Mon Sep 17 00:00:00 2001 From: Georgy Berezhnoy Date: Thu, 16 Sep 2021 17:04:05 +0300 Subject: [PATCH 3/4] update types --- src/components/modules/tools.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/modules/tools.ts b/src/components/modules/tools.ts index 609449770..5ff3cf7a0 100644 --- a/src/components/modules/tools.ts +++ b/src/components/modules/tools.ts @@ -274,8 +274,8 @@ export default class Tools extends Module { * @param config - tools config */ private getListOfPrepareFunctions(config: {[name: string]: ToolSettings}): { - function: (data: { toolName: string }) => void | Promise; - data: { toolName: string }; + function: (data: { toolName: string; config: ToolConfig }) => void | Promise; + data: { toolName: string; config: ToolConfig }; }[] { const toolPreparationList: { function: (data: { toolName: string }) => void | Promise; From 33d9859eb94be73e8371108ff65119c89b3982e6 Mon Sep 17 00:00:00 2001 From: Georgy Berezhnoy Date: Thu, 16 Sep 2021 17:14:28 +0300 Subject: [PATCH 4/4] Update changelog --- docs/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4396c9018..2c783388d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 2.22.3 + +- `Fix` — Tool config is passed to `prepare` method [editor-js/embed#68](https://github.com/editor-js/embed/issues/68) + ### 2.22.2 - `Improvement` — Inline Toolbar might be used for any contenteditable element inside Editor.js zone