Skip to content

Commit d945d4c

Browse files
khaydarovneSpecc
andauthored
refactoring(modules): Events module is util now (codex-team#1582)
* refactoring(modules): events module is util now * Update changelog * remove redundant line * refactor * Update editor-modules.d.ts Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
1 parent c4ebdee commit d945d4c

11 files changed

Lines changed: 38 additions & 30 deletions

File tree

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `Fix` - Fix unexpected behavior on an empty link pasting [#1348](https://github.com/codex-team/editor.js/issues/1348).
1616
- `Fix` - Fix SanitizerConfig type definition [#1513](https://github.com/codex-team/editor.js/issues/1513)
1717
- `Refactoring` - The Listeners module now is a util.
18+
- `Refactoring` - The Events module now is a util.
1819
- `Fix` - Editor Config now immutable [#1552](https://github.com/codex-team/editor.js/issues/1552).
1920
- `Refactoring` - Shortcuts module is util now.
2021
- `Fix` - Fix bubbling on BlockManagers' listener [#1433](https://github.com/codex-team/editor.js/issues/1433).

src/components/__module.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EditorModules } from '../types-internal/editor-modules';
22
import { EditorConfig } from '../../types';
33
import { ModuleConfig } from '../types-internal/module-config';
44
import Listeners from './utils/listeners';
5+
import EventsDispatcher from './utils/events';
56

67
/**
78
* The type <T> of the Module generic.
@@ -39,6 +40,11 @@ export default class Module<T extends ModuleNodes = {}> {
3940
*/
4041
protected config: EditorConfig;
4142

43+
/**
44+
* Editor event dispatcher class
45+
*/
46+
protected eventsDispatcher: EventsDispatcher;
47+
4248
/**
4349
* Util for bind/unbind DOM event listeners
4450
*/
@@ -86,14 +92,17 @@ export default class Module<T extends ModuleNodes = {}> {
8692

8793
/**
8894
* @class
95+
*
8996
* @param {EditorConfig} config - Editor's config
97+
* @param {EventsDispatcher} eventsDispatcher - Editor's event dispatcher
9098
*/
91-
constructor({ config }: ModuleConfig) {
99+
constructor({ config, eventsDispatcher }: ModuleConfig) {
92100
if (new.target === Module) {
93101
throw new TypeError('Constructors for abstract class Module are not allowed.');
94102
}
95103

96104
this.config = config;
105+
this.eventsDispatcher = eventsDispatcher;
97106
}
98107

99108
/**

src/components/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EditorConfig, SanitizerConfig } from '../../types';
44
import { EditorModules } from '../types-internal/editor-modules';
55
import I18n from './i18n';
66
import { CriticalError } from './errors/critical';
7+
import EventsDispatcher from './utils/events';
78

89
/**
910
* @typedef {Core} Core - editor core class
@@ -53,6 +54,11 @@ export default class Core {
5354
*/
5455
public isReady: Promise<void>;
5556

57+
/**
58+
* Event Dispatcher util
59+
*/
60+
private eventsDispatcher: EventsDispatcher = new EventsDispatcher();
61+
5662
/**
5763
* @param {EditorConfig} config - user configuration
5864
*
@@ -338,6 +344,7 @@ export default class Core {
338344
*/
339345
this.moduleInstances[Module.displayName] = new Module({
340346
config: this.configuration,
347+
eventsDispatcher: this.eventsDispatcher,
341348
});
342349
} catch (e) {
343350
_.log(`Module ${Module.displayName} skipped because`, 'warn', e);

src/components/modules/api/events.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Events } from '../../../../types/api';
21
import Module from '../../__module';
2+
import { Events } from '../../../../types/api';
33

44
/**
55
* @class EventsAPI
@@ -26,7 +26,7 @@ export default class EventsAPI extends Module {
2626
* @param {Function} callback - event handler
2727
*/
2828
public on(eventName, callback): void {
29-
this.Editor.Events.on(eventName, callback);
29+
this.eventsDispatcher.on(eventName, callback);
3030
}
3131

3232
/**
@@ -36,7 +36,7 @@ export default class EventsAPI extends Module {
3636
* @param {object} data - event's data
3737
*/
3838
public emit(eventName, data): void {
39-
this.Editor.Events.emit(eventName, data);
39+
this.eventsDispatcher.emit(eventName, data);
4040
}
4141

4242
/**
@@ -46,6 +46,6 @@ export default class EventsAPI extends Module {
4646
* @param {Function} callback - event handler
4747
*/
4848
public off(eventName, callback): void {
49-
this.Editor.Events.off(eventName, callback);
49+
this.eventsDispatcher.off(eventName, callback);
5050
}
5151
}

src/components/modules/toolbar/blockSettings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export default class BlockSettings extends Module<BlockSettingsNodes> {
147147
this.addDefaultSettings();
148148

149149
/** Tell to subscribers that block settings is opened */
150-
this.Editor.Events.emit(this.events.opened);
150+
this.eventsDispatcher.emit(this.events.opened);
151151

152152
this.flipper.activate(this.blockTunesButtons);
153153
}
@@ -183,7 +183,7 @@ export default class BlockSettings extends Module<BlockSettingsNodes> {
183183
this.nodes.defaultSettings.innerHTML = '';
184184

185185
/** Tell to subscribers that block settings is closed */
186-
this.Editor.Events.emit(this.events.closed);
186+
this.eventsDispatcher.emit(this.events.closed);
187187

188188
/** Clear cached buttons */
189189
this.buttons = [];

src/components/modules/tools.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import BoldInlineTool from '../inline-tools/inline-tool-bold';
1414
import ItalicInlineTool from '../inline-tools/inline-tool-italic';
1515
import LinkInlineTool from '../inline-tools/inline-tool-link';
1616
import Stub from '../tools/stub';
17+
import { ModuleConfig } from '../../types-internal/module-config';
18+
import EventsDispatcher from '../utils/events';
1719

1820
/**
1921
* @module Editor.js Tools Submodule
@@ -198,9 +200,13 @@ export default class Tools extends Module {
198200
* @class
199201
*
200202
* @param {EditorConfig} config - Editor's configuration
203+
* @param {EventsDispatcher} eventsDispatcher - Editor's event dispatcher
201204
*/
202-
constructor({ config }) {
203-
super({ config });
205+
constructor({ config, eventsDispatcher }: ModuleConfig) {
206+
super({
207+
config,
208+
eventsDispatcher,
209+
});
204210

205211
this.toolsClasses = {};
206212

src/components/modules/tooltip.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Module from '../__module';
55
* Use external module CodeX Tooltip
66
*/
77
import CodeXTooltips, { TooltipContent, TooltipOptions } from 'codex-tooltip';
8-
import { ModuleConfig } from '../../types-internal/module-config';
98

109
/**
1110
* Tooltip
@@ -20,14 +19,6 @@ export default class Tooltip extends Module {
2019
*/
2120
private lib: CodeXTooltips = new CodeXTooltips();
2221

23-
/**
24-
* @class
25-
* @param {EditorConfig} - Editor's config
26-
*/
27-
constructor({ config }: ModuleConfig) {
28-
super({ config });
29-
}
30-
3122
/**
3223
* Shows tooltip on element with passed HTML content
3324
*
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import Module from '../__module';
2-
31
/**
4-
* @module eventDispatcher
2+
* @class EventDispatcher
53
*
64
* Has two important methods:
75
* - {Function} on - appends subscriber to the event. If event doesn't exist - creates new one
86
* - {Function} emit - fires all subscribers with data
9-
* - {Function off - unsubsribes callback
7+
* - {Function off - unsubscribes callback
108
*
119
* @version 1.0.0
1210
*
1311
* @typedef {Events} Events
1412
* @property {object} subscribers - all subscribers grouped by event name
1513
*/
16-
export default class Events extends Module {
14+
export default class EventsDispatcher {
1715
/**
1816
* Object with events` names as key and array of callback functions as value
1917
*

src/types-internal/editor-modules.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import Toolbar from '../components/modules/toolbar/index';
44
import InlineToolbar from '../components/modules/toolbar/inline';
55
import Toolbox from '../components/modules/toolbar/toolbox';
66
import BlockSettings from '../components/modules/toolbar/blockSettings';
7-
import Events from '../components/modules/events';
8-
import Shortcuts from '../components/utils/shortcuts';
97
import Paste from '../components/modules/paste';
108
import Notifier from '../components/modules/notifier';
119
import Tooltip from '../components/modules/tooltip';
@@ -48,8 +46,6 @@ export interface EditorModules {
4846
Toolbox: Toolbox;
4947
BlockSettings: BlockSettings;
5048
ConversionToolbar: ConversionToolbar;
51-
Events: Events;
52-
Shortcuts: Shortcuts;
5349
Paste: Paste;
5450
DragNDrop: DragNDrop;
5551
ModificationsObserver: ModificationsObserver;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import {EditorConfig} from '../../types/index';
1+
import { EditorConfig } from '../../types/index';
2+
import EventsDispatcher from '../components/utils/events';
23

34
/**
45
* Describes object passed to Editor modules constructor
56
*/
67
export interface ModuleConfig {
78
config: EditorConfig;
9+
eventsDispatcher: EventsDispatcher;
810
}

0 commit comments

Comments
 (0)