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

Support language independent keyboard shortcuts (T1027453) #30

Merged
merged 18 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 17 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
36 changes: 29 additions & 7 deletions modules/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ class Keyboard extends Module {
if (typeof handler === 'function') {
handler = { handler };
}
const keys = Array.isArray(binding.key) ? binding.key : [binding.key];

const keyPropery = binding.which ? 'which' : 'key';
const keys = Array.isArray(binding[keyPropery])
? binding[keyPropery]
: [binding[keyPropery]];

keys.forEach(key => {
const singleBinding = {
...binding,
Expand Down Expand Up @@ -225,7 +230,10 @@ class Keyboard extends Module {
suffix: suffixText,
event: evt,
};
const prevented = matches.some(binding => {

let prevented = false;

matches.some(binding => {
if (
binding.collapsed != null &&
binding.collapsed !== curContext.collapsed
Expand Down Expand Up @@ -263,8 +271,20 @@ class Keyboard extends Module {
if (binding.suffix != null && !binding.suffix.test(curContext.suffix)) {
return false;
}
return binding.handler.call(this, range, curContext, binding) !== true;

const handlerResult = binding.handler.call(
this,
range,
curContext,
binding,
);
const preventAfterAllMatches = handlerResult?.preventAfterAllMatches;

prevented = handlerResult !== true || preventAfterAllMatches;

return prevented && !preventAfterAllMatches;
});

if (prevented) {
evt.preventDefault();
}
Expand Down Expand Up @@ -375,9 +395,9 @@ class Keyboard extends Module {

Keyboard.DEFAULTS = {
bindings: {
bold: makeFormatHandler('bold'),
italic: makeFormatHandler('italic'),
underline: makeFormatHandler('underline'),
bold: makeFormatHandler('bold', 66),
italic: makeFormatHandler('italic', 73),
underline: makeFormatHandler('underline', 85),
indent: {
// highlight tab or tab at beginning of list, indent or blockquote
key: 'tab',
Expand Down Expand Up @@ -665,12 +685,14 @@ function makeEmbedArrowHandler(key, shiftKey) {
};
}

function makeFormatHandler(format) {
function makeFormatHandler(format, which) {
return {
key: format[0],
which,
shortKey: true,
handler(range, context) {
this.quill.format(format, !context.format[format], Quill.sources.USER);
return { preventAfterAllMatches: true };
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "devextreme-quill",
"version": "1.5.10",
"version": "1.5.11",
"description": "Core of the DevExtreme HtmlEditor",
"author": "Developer Express Inc.",
"main": "dist/dx-quill.js",
Expand Down
68 changes: 68 additions & 0 deletions test/unit/modules/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,72 @@ describe('Keyboard', function() {
});
});
});

describe('bindings', function() {
it('which modifier', function() {
const quillMock = {
root: document.createElement('div'),
once: (eventName, handler) => {
handler();
},
hasFocus: () => true,
getSelection: () => {
return { index: 0, length: 0 };
},
getFormat: () => {
return {};
},
getLine: () => {
return [{ length: () => 0 }, 0];
},
getLeaf: () => {
return [0, 0];
},
};
const fakeEvent = {
key: 'b',
which: 66,
code: 'KeyB',
shiftKey: false,
metaKey: false,
ctrlKey: true,
altKey: false,
};
let counter = 0;

const nativeAddEventListener = quillMock.root.addEventListener;

quillMock.root.addEventListener = function(type, handler) {
const modifiedHandler = () => {
handler(fakeEvent);
};

nativeAddEventListener.call(this, type, modifiedHandler);
};

// eslint-disable-next-line no-new
new Keyboard(quillMock, {
bindings: {
66: {
key: 'b',
which: 66,
ctrlKey: true,
handler() {
counter += 1;
},
},
},
});

const keydownEvent = new KeyboardEvent('keydown', {
key: 'n',
});

quillMock.root.dispatchEvent(keydownEvent);

expect(counter).toBe(1);

window.addEventListener = nativeAddEventListener;
});
});
});