Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring(modules): Events module is util now #1582

Merged
merged 6 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- `Fix` - Fix unexpected behavior on an empty link pasting [#1348](https://github.com/codex-team/editor.js/issues/1348).
- `Fix` - Fix SanitizerConfig type definition [#1513](https://github.com/codex-team/editor.js/issues/1513)
- `Refactoring` - The Listeners module now is a util.
- `Refactoring` - The Events module now is a util.
- `Fix` - Editor Config now immutable [#1552](https://github.com/codex-team/editor.js/issues/1552).
- `Refactoring` - Shortcuts module is util now.
- `Fix` - Fix bubbling on BlockManagers' listener [#1433](https://github.com/codex-team/editor.js/issues/1433).
Expand Down
11 changes: 10 additions & 1 deletion src/components/__module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EditorModules } from '../types-internal/editor-modules';
import { EditorConfig } from '../../types';
import { ModuleConfig } from '../types-internal/module-config';
import Listeners from './utils/listeners';
import EventsDispatcher from './utils/events';

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

/**
* Editor event dispatcher class
*/
protected eventsDispatcher: EventsDispatcher;

/**
* Util for bind/unbind DOM event listeners
*/
Expand Down Expand Up @@ -86,14 +92,17 @@ export default class Module<T extends ModuleNodes = {}> {

/**
* @class
*
* @param {EditorConfig} config - Editor's config
* @param {EventsDispatcher} eventsDispatcher - Editor's event dispatcher
*/
constructor({ config }: ModuleConfig) {
constructor({ config, eventsDispatcher }: ModuleConfig) {
if (new.target === Module) {
throw new TypeError('Constructors for abstract class Module are not allowed.');
}

this.config = config;
this.eventsDispatcher = eventsDispatcher;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EditorConfig, SanitizerConfig } from '../../types';
import { EditorModules } from '../types-internal/editor-modules';
import I18n from './i18n';
import { CriticalError } from './errors/critical';
import EventsDispatcher from './utils/events';

/**
* @typedef {Core} Core - editor core class
Expand Down Expand Up @@ -53,6 +54,11 @@ export default class Core {
*/
public isReady: Promise<void>;

/**
* Event Dispatcher util
*/
private eventsDispatcher: EventsDispatcher = new EventsDispatcher();

/**
* @param {EditorConfig} config - user configuration
*
Expand Down Expand Up @@ -338,6 +344,7 @@ export default class Core {
*/
this.moduleInstances[Module.displayName] = new Module({
config: this.configuration,
eventsDispatcher: this.eventsDispatcher,
});
} catch (e) {
_.log(`Module ${Module.displayName} skipped because`, 'warn', e);
Expand Down
8 changes: 4 additions & 4 deletions src/components/modules/api/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Events } from '../../../../types/api';
import Module from '../../__module';
import { Events } from '../../../../types/api';

/**
* @class EventsAPI
Expand All @@ -26,7 +26,7 @@ export default class EventsAPI extends Module {
* @param {Function} callback - event handler
*/
public on(eventName, callback): void {
this.Editor.Events.on(eventName, callback);
this.eventsDispatcher.on(eventName, callback);
}

/**
Expand All @@ -36,7 +36,7 @@ export default class EventsAPI extends Module {
* @param {object} data - event's data
*/
public emit(eventName, data): void {
this.Editor.Events.emit(eventName, data);
this.eventsDispatcher.emit(eventName, data);
}

/**
Expand All @@ -46,6 +46,6 @@ export default class EventsAPI extends Module {
* @param {Function} callback - event handler
*/
public off(eventName, callback): void {
this.Editor.Events.off(eventName, callback);
this.eventsDispatcher.off(eventName, callback);
}
}
4 changes: 2 additions & 2 deletions src/components/modules/toolbar/blockSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default class BlockSettings extends Module<BlockSettingsNodes> {
this.addDefaultSettings();

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

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

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

/** Clear cached buttons */
this.buttons = [];
Expand Down
10 changes: 8 additions & 2 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import BoldInlineTool from '../inline-tools/inline-tool-bold';
import ItalicInlineTool from '../inline-tools/inline-tool-italic';
import LinkInlineTool from '../inline-tools/inline-tool-link';
import Stub from '../tools/stub';
import { ModuleConfig } from '../../types-internal/module-config';
import EventsDispatcher from '../utils/events';

/**
* @module Editor.js Tools Submodule
Expand Down Expand Up @@ -198,9 +200,13 @@ export default class Tools extends Module {
* @class
*
* @param {EditorConfig} config - Editor's configuration
* @param {EventsDispatcher} eventsDispatcher - Editor's event dispatcher
*/
constructor({ config }) {
super({ config });
constructor({ config, eventsDispatcher }: ModuleConfig) {
super({
config,
eventsDispatcher,
});

this.toolsClasses = {};

Expand Down
9 changes: 0 additions & 9 deletions src/components/modules/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Module from '../__module';
* Use external module CodeX Tooltip
*/
import CodeXTooltips, { TooltipContent, TooltipOptions } from 'codex-tooltip';
import { ModuleConfig } from '../../types-internal/module-config';

/**
* Tooltip
Expand All @@ -20,14 +19,6 @@ export default class Tooltip extends Module {
*/
private lib: CodeXTooltips = new CodeXTooltips();

/**
* @class
* @param {EditorConfig} - Editor's config
*/
constructor({ config }: ModuleConfig) {
super({ config });
}

/**
* Shows tooltip on element with passed HTML content
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import Module from '../__module';

/**
* @module eventDispatcher
* @class EventDispatcher
*
* Has two important methods:
* - {Function} on - appends subscriber to the event. If event doesn't exist - creates new one
* - {Function} emit - fires all subscribers with data
* - {Function off - unsubsribes callback
* - {Function off - unsubscribes callback
*
* @version 1.0.0
*
* @typedef {Events} Events
* @property {object} subscribers - all subscribers grouped by event name
*/
export default class Events extends Module {
export default class EventsDispatcher {
/**
* Object with events` names as key and array of callback functions as value
*
Expand Down
4 changes: 0 additions & 4 deletions src/types-internal/editor-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import Toolbar from '../components/modules/toolbar/index';
import InlineToolbar from '../components/modules/toolbar/inline';
import Toolbox from '../components/modules/toolbar/toolbox';
import BlockSettings from '../components/modules/toolbar/blockSettings';
import Events from '../components/modules/events';
import Shortcuts from '../components/utils/shortcuts';
import Paste from '../components/modules/paste';
import Notifier from '../components/modules/notifier';
import Tooltip from '../components/modules/tooltip';
Expand Down Expand Up @@ -48,8 +46,6 @@ export interface EditorModules {
Toolbox: Toolbox;
BlockSettings: BlockSettings;
ConversionToolbar: ConversionToolbar;
Events: Events;
Shortcuts: Shortcuts;
Paste: Paste;
DragNDrop: DragNDrop;
ModificationsObserver: ModificationsObserver;
Expand Down
4 changes: 3 additions & 1 deletion src/types-internal/module-config.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {EditorConfig} from '../../types/index';
import { EditorConfig } from '../../types/index';
import EventsDispatcher from '../components/utils/events';

/**
* Describes object passed to Editor modules constructor
*/
export interface ModuleConfig {
config: EditorConfig;
eventsDispatcher: EventsDispatcher;
}
2 changes: 0 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ declare class EditorJS {

public blocks: Blocks;
public caret: Caret;
public events: Events;
public listeners: Listeners;
public sanitizer: Sanitizer;
public saver: Saver;
public selection: Selection;
Expand Down