fix(extensions):Add missing api adapters for ComposerExtension methods

This commit is contained in:
Juan Tejada 2016-01-04 10:50:24 -05:00
parent 5030787d88
commit 141b2e73c9
2 changed files with 75 additions and 13 deletions

View file

@ -72,7 +72,7 @@ describe('ComposerExtensionAdapter', ()=> {
});
});
describe('adaptMethod', ()=> {
describe('adaptContenteditableMethod', ()=> {
it('adapts correctly when signature is (editor, ...)', ()=> {
const methodSpy = jasmine.createSpy('methodSpy');
const extension = {
@ -80,7 +80,7 @@ describe('ComposerExtensionAdapter', ()=> {
methodSpy(editor, ev, other);
},
};
adapter.adaptMethod(extension, 'method');
adapter.adaptContenteditableMethod(extension, 'method');
extension.method({editor, event, extra});
expect(methodSpy).toHaveBeenCalledWith(editor, event, extra);
});
@ -92,7 +92,7 @@ describe('ComposerExtensionAdapter', ()=> {
methodSpy(ev, editableNode, sel, other);
},
};
adapter.adaptMethod(extension, 'method');
adapter.adaptContenteditableMethod(extension, 'method');
extension.method({editor, event, extra});
expect(methodSpy).toHaveBeenCalledWith(event, node, selection, extra);
});
@ -104,7 +104,7 @@ describe('ComposerExtensionAdapter', ()=> {
methodSpy(editableNode, sel, ev, other);
},
};
adapter.adaptMethod(extension, 'method');
adapter.adaptContenteditableMethod(extension, 'method');
extension.method({editor, event, extra});
expect(methodSpy).toHaveBeenCalledWith(node, selection, event, extra);
});
@ -116,9 +116,37 @@ describe('ComposerExtensionAdapter', ()=> {
methodSpy(editor, mutations);
},
};
adapter.adaptMethod(extension, 'method');
adapter.adaptContenteditableMethod(extension, 'method');
extension.method({editor, mutations: []});
expect(methodSpy).toHaveBeenCalledWith(editor, []);
});
});
describe('adaptComposerMethod', ()=> {
it('adapts correctly for finalizeSessionBeforeSending', ()=> {
const methodSpy = jasmine.createSpy('methodSpy');
const session = 'session';
const extension = {
finalizeSessionBeforeSending(sess) {
methodSpy(sess);
},
};
adapter.adaptComposerMethod(extension, 'finalizeSessionBeforeSending');
extension.finalizeSessionBeforeSending({session});
expect(methodSpy).toHaveBeenCalledWith(session);
});
it('adapts correctly for other composer extension methods', ()=> {
const methodSpy = jasmine.createSpy('methodSpy');
const draft = 'draft';
const extension = {
warningsForSending(dr) {
methodSpy(dr);
},
};
adapter.adaptComposerMethod(extension, 'warningsForSending');
extension.warningsForSending({draft});
expect(methodSpy).toHaveBeenCalledWith(draft);
});
});
});

View file

@ -3,7 +3,7 @@ import DOMUtils from '../dom-utils';
import {deprecate} from '../deprecate-utils';
import {getFunctionArgs} from './extension-utils';
export function isUsingOutdatedAPI(func) {
export function isUsingOutdatedContenteditableApi(func) {
// Might not always be true, but it is our best guess
const firstArg = getFunctionArgs(func)[0];
if (func.length > 1) return true; // Not using a named arguments hash
@ -14,9 +14,33 @@ export function isUsingOutdatedAPI(func) {
);
}
export function adaptMethod(extension, method, original = extension[method]) {
export function isUsingOutdatedComposerApi(func) {
const firstArg = getFunctionArgs(func)[0];
return (
firstArg.includes('dr') ||
firstArg.includes('sess') ||
firstArg.includes('prox')
);
}
export function adaptComposerMethod(extension, method) {
const original = extension[method];
if (!original || !isUsingOutdatedComposerApi(original)) return;
if (method === 'finalizeSessionBeforeSending') {
extension[method] = (argsObj)=> {
original(argsObj.session);
};
} else {
extension[method] = (argsObj)=> {
original(argsObj.draft);
};
}
}
export function adaptContenteditableMethod(extension, method, original = extension[method]) {
// Check if it is using old API
if (!original || !isUsingOutdatedAPI(original)) return;
if (!original || !isUsingOutdatedContenteditableApi(original)) return;
let deprecatedArgs = '';
extension[method] = (argsObj)=> {
@ -50,7 +74,7 @@ export function adaptMethod(extension, method, original = extension[method]) {
export function adaptOnInput(extension) {
if (extension.onContentChanged != null) return;
adaptMethod(extension, 'onContentChanged', extension.onInput);
adaptContenteditableMethod(extension, 'onContentChanged', extension.onInput);
}
export function adaptOnTabDown(extension) {
@ -93,7 +117,7 @@ export function adaptOnMouseUp(extension) {
}
export default function adaptExtension(extension) {
const standardMethods = [
const contenteditableMethods = [
'onContentChanged',
'onBlur',
'onFocus',
@ -101,14 +125,24 @@ export default function adaptExtension(extension) {
'onKeyDown',
'onShowContextMenu',
];
standardMethods.forEach(
method => adaptMethod(extension, method)
contenteditableMethods.forEach(
method => adaptContenteditableMethod(extension, method)
);
// Special cases
// Special contenteditable cases
adaptOnInput(extension);
adaptOnTabDown(extension);
adaptOnMouseUp(extension);
const composerMethods = [
'warningsForSending',
'prepareNewDraft',
'finalizeSessionBeforeSending',
];
composerMethods.forEach(
method => adaptComposerMethod(extension, method)
);
return extension;
}