Skip to content

Commit

Permalink
fix: indent more make blockquote (close #242) (#246)
Browse files Browse the repository at this point in the history
* fix: unintended squire shortcuts applied

* feat: add keymap ctrl+],[ to indent/outdent

* refactor: apply code review (#246)
  • Loading branch information
kyuwoo-choi authored and seonim-ryu committed Jan 2, 2020
1 parent fc56d47 commit 7aca288
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 25 deletions.
2 changes: 1 addition & 1 deletion apps/core/src/js/commandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import $ from 'jquery';
import util from 'tui-code-snippet';

import Command from './command';
import {isMac} from './util';

const isMac = /Mac/.test(navigator.platform);
const KEYMAP_OS_INDEX = isMac ? 1 : 0;

/**
Expand Down
4 changes: 1 addition & 3 deletions apps/core/src/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ class ToastUIEditor {

this.mdEditor = MarkdownEditor.factory(this.layout.getMdEditorContainerEl(), this.eventManager);
this.preview = new MarkdownPreview(this.layout.getPreviewEl(), this.eventManager, this.convertor);
this.wwEditor = WysiwygEditor.factory(this.layout.getWwEditorContainerEl(), this.eventManager, {
useCommandShortcut: this.options.useCommandShortcut
});
this.wwEditor = WysiwygEditor.factory(this.layout.getWwEditorContainerEl(), this.eventManager);
this.toMarkOptions = null;

this.changePreviewStyle(this.options.previewStyle);
Expand Down
3 changes: 1 addition & 2 deletions apps/core/src/js/squireExt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import util from 'tui-code-snippet';
import Squire from 'squire-rte';

import domUtils from './domUtils';
import {isMac} from './util';

const FIND_BLOCK_TAGNAME_RX = /\b(H[\d]|LI|P|BLOCKQUOTE|TD)\b/;
const isIElt11 = /Trident\/[456]\./.test(navigator.userAgent);
Expand Down Expand Up @@ -325,7 +326,6 @@ class SquireExt extends Squire {
}

blockCommandShortcuts() {
const isMac = /Mac/.test(navigator.platform);
const meta = isMac ? 'meta' : 'ctrl';
const keys = ['b', 'i', 'u', 'shift-7', 'shift-5', 'shift-6', 'shift-8', 'shift-9', '[', ']'];

Expand All @@ -338,4 +338,3 @@ class SquireExt extends Squire {
}

export default SquireExt;

5 changes: 4 additions & 1 deletion apps/core/src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function sendHostName() {
});
}

const isMac = /Mac/.test(navigator.platform);

module.exports = {
sendHostName
sendHostName,
isMac
};
4 changes: 2 additions & 2 deletions apps/core/src/js/wwListManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class WwListManager {
}

_initKeyHandler() {
this.wwe.addKeyEventHandler('TAB', (ev, range) => {
this.wwe.addKeyEventHandler(['TAB', 'CTRL+]', 'META+]'], (ev, range) => {
let isNeedNext;

if (range.collapsed) {
Expand All @@ -89,7 +89,7 @@ class WwListManager {
return isNeedNext;
});

this.wwe.addKeyEventHandler('SHIFT+TAB', (ev, range) => {
this.wwe.addKeyEventHandler(['SHIFT+TAB', 'CTRL+[', 'META+['], (ev, range) => {
let isNeedNext;

if (range.collapsed) {
Expand Down
25 changes: 11 additions & 14 deletions apps/core/src/js/wysiwygEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ class WysiwygEditor {
* Creates an instance of WysiwygEditor.
* @param {jQuery} $el element to insert editor
* @param {EventManager} eventManager EventManager instance
* @param {object} [options={}] - option object
* @param {boolean} [options.useCommandShortcut=true] - whether to use squire command shortcuts
* @memberof WysiwygEditor
*/
constructor($el, eventManager, options = {}) {
constructor($el, eventManager) {
this.componentManager = new ComponentManager(this);
this.eventManager = eventManager;
this.$editorContainerEl = $el;
Expand All @@ -55,10 +53,6 @@ class WysiwygEditor {
this._keyEventHandlers = {};
this._managers = {};

this._options = $.extend({
'useCommandShortcut': true
}, options);

this._initEvent();
this._initDefaultKeyEventHandler();

Expand All @@ -80,9 +74,7 @@ class WysiwygEditor {
'HR': false
}
});
if (!this._options.useCommandShortcut) {
this.editor.blockCommandShortcuts();
}
this.editor.blockCommandShortcuts();

this._clipboardManager = new WwClipboardManager(this);
this._initSquireEvent();
Expand Down Expand Up @@ -126,7 +118,7 @@ class WysiwygEditor {
* addKeyEventHandler
* Add key event handler
* @memberof WysiwygEditor
* @param {string} keyMap keyMap string
* @param {string|Array.<string>} keyMap - keyMap string or array of string
* @param {function} handler handler
*/
addKeyEventHandler(keyMap, handler) {
Expand All @@ -135,11 +127,16 @@ class WysiwygEditor {
keyMap = 'DEFAULT';
}

if (!this._keyEventHandlers[keyMap]) {
this._keyEventHandlers[keyMap] = [];
if (!util.isArray(keyMap)) {
keyMap = [keyMap];
}

this._keyEventHandlers[keyMap].push(handler);
keyMap.forEach(key => {
if (!this._keyEventHandlers[key]) {
this._keyEventHandlers[key] = [];
}
this._keyEventHandlers[key].push(handler);
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/core/test/unit/squireExt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import $ from 'jquery';

import SquireExt from '../../src/js/squireExt';
import {isMac} from '../../src/js/util';

describe('SquireExt', () => {
let sqe;
Expand Down Expand Up @@ -294,7 +295,6 @@ describe('SquireExt', () => {

describe('blockCommandShortcut()', () => {
it('blocks key handlers', () => {
const isMac = /Mac/.test(navigator.platform);
const meta = isMac ? 'meta' : 'ctrl';
const spyOriginal = jasmine.createSpy('original');
const spy = jasmine.createSpy('replace');
Expand Down
37 changes: 36 additions & 1 deletion apps/core/test/unit/wysiwygEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import $ from 'jquery';
import WysiwygEditor from '../../src/js/wysiwygEditor';
import EventManager from '../../src/js/eventManager';
import ListManager from '../../src/js/wwListManager';
import {isMac} from '../../src/js/util';

describe('WysiwygEditor', () => {
let $container, em, wwe;
Expand All @@ -27,7 +28,7 @@ describe('WysiwygEditor', () => {
// we need to wait squire input event process
afterEach(done => {
setTimeout(() => {
$('body').empty();
$container.remove();
done();
});
});
Expand Down Expand Up @@ -65,6 +66,20 @@ describe('WysiwygEditor', () => {
expect(handler).toHaveBeenCalled();
});

it('should take multiple keymaps as an array', () => {
const handler = jasmine.createSpy('keyEventHandler');
wwe.addKeyEventHandler(['HOME', 'END'], handler);
em.emit('wysiwygKeyEvent', {
keyMap: 'HOME',
data: {keyCode: 0}
});
em.emit('wysiwygKeyEvent', {
keyMap: 'END',
data: {keyCode: 0}
});
expect(handler.calls.count()).toBe(2);
});

it('run particular keymap and default', () => {
const handler = jasmine.createSpy('keyEventHandler');
wwe.addKeyEventHandler('HOME', handler);
Expand Down Expand Up @@ -529,4 +544,24 @@ describe('WysiwygEditor', () => {
expect(wwe._getLastLiString('BLOCKQUOTE>DIV')).toEqual('');
});
});

it('should blocks squire default key handlers', () => {
const sqe = wwe.getEditor();
const meta = isMac ? 'meta' : 'ctrl';
const spyOriginal = jasmine.createSpy('original');
const spy = jasmine.createSpy('replace');
const keyEvent = {
keyCode: 66,
preventDefault: spy
};
keyEvent[`${meta}Key`] = true;

Object.getPrototypeOf(sqe._keyHandlers)[`${meta}-b`] = spyOriginal;
sqe.fireEvent('keydown', keyEvent);

setTimeout(() => {
expect(spy).toHaveBeenCalled();
expect(spyOriginal).not.toHaveBeenCalled();
}, 1);
});
});

0 comments on commit 7aca288

Please sign in to comment.