0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

🐝 Card menu fixes (#597)

closes: https://github.com/TryGhost/Ghost/issues/8106

- Fixes the keyboard selection problems of the '+' menu.
- Makes the active state of the keyboard selection blank until a key is pressed.
- Fixes it so that the '+' menu executes the tool on Enter.
This commit is contained in:
Ryan McCarvill 2017-03-21 01:40:20 +13:00 committed by Kevin Ansfield
parent c3a6792299
commit a88b8bc00a
3 changed files with 77 additions and 53 deletions

View file

@ -3,6 +3,7 @@ import computed from 'ember-computed';
import run from 'ember-runloop'; import run from 'ember-runloop';
import Tools from '../options/default-tools'; import Tools from '../options/default-tools';
import layout from '../templates/components/koenig-plus-menu'; import layout from '../templates/components/koenig-plus-menu';
import Range from 'mobiledoc-kit/utils/cursor/range';
import $ from 'jquery'; import $ from 'jquery';
const ROW_LENGTH = 4; const ROW_LENGTH = 4;
@ -15,7 +16,7 @@ export default Component.extend({
return this.get('isOpen') || this.get('isButton'); return this.get('isOpen') || this.get('isButton');
}), }),
toolsLength: 0, toolsLength: 0,
selected: 0, selected: -1,
selectedTool: null, selectedTool: null,
query: '', query: '',
range: null, range: null,
@ -44,13 +45,13 @@ export default Component.extend({
}); });
this.set('toolsLength', i); this.set('toolsLength', i);
tools.sort((a, b) => a.order > b.order); tools.sort((a, b) => a.order > b.order);
if (selected > -1) {
let selectedTool = tools[selected] || tools[0]; let selectedTool = tools[selected] || tools[0];
if (selectedTool) { if (selectedTool) {
this.set('selectedTool', selectedTool); this.set('selectedTool', selectedTool);
selectedTool.selected = true; selectedTool.selected = true;
}
} }
return tools; return tools;
}), }),
init() { init() {
@ -74,45 +75,6 @@ export default Component.extend({
}, 200); }, 200);
}); });
input.keydown(({keyCode}) => {
let item = this.get('selected');
let length = this.get('toolsLength');
switch (keyCode) {
case 27: // escape
return this.send('closeMenu');
case 37: // left
if (item > 0) {
this.set('selected', item - 1);
} else {
this.set('selected', length - 1);
}
break;
case 38: // up
if (item > ROW_LENGTH) {
this.set('selected', item - ROW_LENGTH);
} else {
this.set('selected', 0);
}
break;
case 39: // right
if (item < length) {
this.set('selected', item + 1);
} else {
this.set('selected', 1);
}
break;
case 40: // down
if (item + ROW_LENGTH < length) {
this.set('selected', item + ROW_LENGTH);
} else {
this.set('selected', length - 1);
}
break;
case 13: // enter
alert('enter');
}
});
editor.cursorDidChange(() => { editor.cursorDidChange(() => {
if (!editor.range || !editor.range.head.section) { if (!editor.range || !editor.range.head.section) {
return; return;
@ -152,6 +114,10 @@ export default Component.extend({
startOffset: editor.range.head.offset, startOffset: editor.range.head.offset,
endOffset: editor.range.head.offset endOffset: editor.range.head.offset
}); });
this.set('selected', -1);
this.set('selectedTool', null);
this.propertyDidChange('toolbar'); this.propertyDidChange('toolbar');
run.schedule('afterRender', this, run.schedule('afterRender', this,
@ -169,8 +135,50 @@ export default Component.extend({
closeMenuKeepButton: function () { // eslint-disable-line closeMenuKeepButton: function () { // eslint-disable-line
this.set('isOpen', false); this.set('isOpen', false);
}, },
updateSelection: function (event) { // eslint-disable-line selectTool: function () { // eslint-disable-line
// alert(event); let {section} = this.get('range');
let editor = this.get('editor');
editor.range = Range.create(section, 0, section, 0);
this.get('selectedTool').onClick(editor);
this.send('closeMenuKeepButton');
},
moveSelectionLeft: function () { // eslint-disable-line
let item = this.get('selected');
let length = this.get('toolsLength');
if (item > 0) {
this.set('selected', item - 1);
} else {
this.set('selected', length - 1);
}
},
moveSelectionUp: function () { // eslint-disable-line
let item = this.get('selected');
if (item > ROW_LENGTH) {
this.set('selected', item - ROW_LENGTH);
} else {
this.set('selected', 0);
}
},
moveSelectionRight: function () { // eslint-disable-line
let item = this.get('selected');
let length = this.get('toolsLength');
if (item < length) {
this.set('selected', item + 1);
} else {
this.set('selected', 0);
}
},
moveSelectionDown: function () { // eslint-disable-line
let item = this.get('selected');
if (item < 0) {
item = 0;
}
let length = this.get('toolsLength');
if (item + ROW_LENGTH < length) {
this.set('selected', item + ROW_LENGTH);
} else {
this.set('selected', length - 1);
}
} }
} }
}); });

View file

@ -41,10 +41,12 @@ export default Component.extend({
this.set('toolsLength', i); this.set('toolsLength', i);
tools.sort((a, b) => a.order > b.order); tools.sort((a, b) => a.order > b.order);
let selectedTool = tools[selected] || tools[0]; let selectedTool = tools[selected];
if (selectedTool) { if (selected > -1) {
this.set('selectedTool', selectedTool); if (selectedTool) {
selectedTool.selected = true; this.set('selectedTool', selectedTool);
selectedTool.selected = true;
}
} }
return tools; return tools;
@ -109,6 +111,8 @@ export default Component.extend({
startOffset: editor.range.head.offset, startOffset: editor.range.head.offset,
endOffset: editor.range.head.offset endOffset: editor.range.head.offset
}); });
this.set('selected', -1);
this.set('selectedTool', null);
editor.registerKeyCommand({ editor.registerKeyCommand({
str: 'LEFT', str: 'LEFT',
@ -156,6 +160,9 @@ export default Component.extend({
name: 'slash', name: 'slash',
run() { run() {
let item = self.get('selected'); let item = self.get('selected');
if (item < 0) {
item = 0;
}
let length = self.get('toolsLength'); let length = self.get('toolsLength');
if (item + ROW_LENGTH < length) { if (item + ROW_LENGTH < length) {
self.set('selected', item + ROW_LENGTH); self.set('selected', item + ROW_LENGTH);

View file

@ -5,7 +5,16 @@
<div class="gh-cardmenu"> <div class="gh-cardmenu">
<div class="gh-cardmenu-search"> <div class="gh-cardmenu-search">
{{inline-svg "search.svg"}} {{inline-svg "search.svg"}}
{{gh-input query class="gh-input gh-cardmenu-search-input" placeholder="Search for a card..." type="text" update=(action (mut query)) key-press=(action "updateSelection")}} {{gh-input query class="gh-input gh-cardmenu-search-input" placeholder="Search for a card..." type="text" update=(action (mut query))
keyEvents=(hash
27=(action "closeMenu")
13=(action "selectTool")
37=(action "moveSelectionLeft")
38=(action "moveSelectionUp")
39=(action "moveSelectionRight")
40=(action "moveSelectionDown")
)
}}
</div> </div>
<div class="gh-cardmenu-divider"> <div class="gh-cardmenu-divider">
Primary Primary