Skip to content

Commit

Permalink
Support language independent keyboard shortcuts (T1027453) (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-kotov-dx authored Jan 14, 2022
1 parent a20c2c3 commit 6e75e94
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
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);

quillMock.root.addEventListener = nativeAddEventListener;
});
});
});

0 comments on commit 6e75e94

Please sign in to comment.