Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
[IMP] Core: add execCustomCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Goaman committed Jun 5, 2020
1 parent 281e77e commit d7ea373
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/core/src/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ export class Dispatcher {
// Call command handler.
await command.handler(args);

// Call command hooks.
const hooks = this.commandHooks[commandId] || [];
const globalHooks = this.commandHooks['*'] || [];
for (const hookCallback of [...hooks, ...globalHooks]) {
await hookCallback(args, commandId);
}
await this._dispatchHooks(commandId, args);
}
}

/**
* Trigger the dispatcher for a custom command.
*/
async dispatchCustom(): Promise<void> {
return this._dispatchHooks('@custom');
}

/**
* Register all handlers declared in a plugin, and match them with their
* corresponding command.
Expand All @@ -82,4 +84,15 @@ export class Dispatcher {
}
this.commandHooks[id].push(hook);
}

/**
* Dispatch to all registred `commandHooks`.
*/
private async _dispatchHooks(commandId: CommandIdentifier, args?): Promise<void> {
const hooks = this.commandHooks[commandId] || [];
const globalHooks = this.commandHooks['*'] || [];
for (const hookCallback of [...hooks, ...globalHooks]) {
await hookCallback(args, commandId);
}
}
}
10 changes: 10 additions & 0 deletions packages/core/src/JWEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ export class JWEditor {
await this.dispatcher.dispatch(commandName, params);
}

/**
* Execute arbitrary code in `callback`, then dispatch the event.
*/
async execCustomCommand<P extends JWPlugin, C extends Commands<P> = Commands<P>>(
callback: () => Promise<void>,
): Promise<void> {
await callback();
await this.dispatcher.dispatchCustom();
}

/**
* Stop this editor instance.
*/
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/JWeditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import JWEditor, { Mode } from '../src/JWEditor';
import { JWPlugin, JWPluginConfig } from '../src/JWPlugin';
import { expect } from 'chai';
import { ModeError } from '../../utils/src/errors';
import { testEditor } from '../../utils/src/testUtils';
import { BasicEditor } from '../../../bundles/BasicEditor';
import { Layout } from '../../plugin-layout/src/Layout';

describe('core', () => {
describe('JWEditor', () => {
Expand Down Expand Up @@ -226,5 +229,23 @@ describe('core', () => {
});
});
});
describe('execCustomCommand', () => {
it('should execute a custom command and trigger plugins', async () => {
await testEditor(BasicEditor, {
contentBefore: '<div>ab[]</div>',
stepFunction: async editor => {
await editor.execCustomCommand(async () => {
const layout = editor.plugins.get(Layout);
const domEngine = layout.engines.dom;
domEngine.components
.get('editable')[0]
.firstLeaf()
.remove();
});
},
contentAfter: '<div>b[]</div>',
});
});
});
});
});

0 comments on commit d7ea373

Please sign in to comment.