mirror of
https://github.com/flarum/core.git
synced 2025-08-03 23:17:43 +02:00
Use newly extracted KeyboardNavigatable util for emoji dropdown
Refs flarum/core#264.
This commit is contained in:
48
extensions/mentions/js/forum/dist/extension.js
vendored
48
extensions/mentions/js/forum/dist/extension.js
vendored
@@ -131,8 +131,8 @@ if (typeof module != 'undefined' && typeof module.exports != 'undefined') {
|
|||||||
;
|
;
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
System.register('flarum/mentions/addComposerAutocomplete', ['flarum/extend', 'flarum/components/ComposerBody', 'flarum/helpers/avatar', 'flarum/helpers/username', 'flarum/helpers/highlight', 'flarum/utils/string', 'flarum/mentions/components/AutocompleteDropdown'], function (_export, _context) {
|
System.register('flarum/mentions/addComposerAutocomplete', ['flarum/extend', 'flarum/components/ComposerBody', 'flarum/helpers/avatar', 'flarum/helpers/username', 'flarum/helpers/highlight', 'flarum/utils/KeyboardNavigatable', 'flarum/utils/string', 'flarum/mentions/components/AutocompleteDropdown'], function (_export, _context) {
|
||||||
var extend, ComposerBody, avatar, usernameHelper, highlight, truncate, AutocompleteDropdown;
|
var extend, ComposerBody, avatar, usernameHelper, highlight, KeyboardNavigatable, truncate, AutocompleteDropdown;
|
||||||
function addComposerAutocomplete() {
|
function addComposerAutocomplete() {
|
||||||
extend(ComposerBody.prototype, 'config', function (original, isInitialized) {
|
extend(ComposerBody.prototype, 'config', function (original, isInitialized) {
|
||||||
if (isInitialized) return;
|
if (isInitialized) return;
|
||||||
@@ -158,7 +158,16 @@ System.register('flarum/mentions/addComposerAutocomplete', ['flarum/extend', 'fl
|
|||||||
dropdown.hide();
|
dropdown.hide();
|
||||||
};
|
};
|
||||||
|
|
||||||
$textarea.after($container).on('keydown', dropdown.navigate.bind(dropdown)).on('click keyup', function (e) {
|
this.navigator = new KeyboardNavigatable();
|
||||||
|
this.navigator.when(function () {
|
||||||
|
return dropdown.active;
|
||||||
|
}).onUp(function () {
|
||||||
|
return dropdown.navigate(-1);
|
||||||
|
}).onDown(function () {
|
||||||
|
return dropdown.navigate(1);
|
||||||
|
}).onSelect(dropdown.complete.bind(dropdown)).onCancel(dropdown.hide.bind(dropdown)).bindTo($textarea);
|
||||||
|
|
||||||
|
$textarea.after($container).on('click keyup', function (e) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
// Up, down, enter, tab, escape, left, right.
|
// Up, down, enter, tab, escape, left, right.
|
||||||
@@ -311,6 +320,8 @@ System.register('flarum/mentions/addComposerAutocomplete', ['flarum/extend', 'fl
|
|||||||
usernameHelper = _flarumHelpersUsername.default;
|
usernameHelper = _flarumHelpersUsername.default;
|
||||||
}, function (_flarumHelpersHighlight) {
|
}, function (_flarumHelpersHighlight) {
|
||||||
highlight = _flarumHelpersHighlight.default;
|
highlight = _flarumHelpersHighlight.default;
|
||||||
|
}, function (_flarumUtilsKeyboardNavigatable) {
|
||||||
|
KeyboardNavigatable = _flarumUtilsKeyboardNavigatable.default;
|
||||||
}, function (_flarumUtilsString) {
|
}, function (_flarumUtilsString) {
|
||||||
truncate = _flarumUtilsString.truncate;
|
truncate = _flarumUtilsString.truncate;
|
||||||
}, function (_flarumMentionsComponentsAutocompleteDropdown) {
|
}, function (_flarumMentionsComponentsAutocompleteDropdown) {
|
||||||
@@ -743,39 +754,20 @@ System.register('flarum/mentions/components/AutocompleteDropdown', ['flarum/Comp
|
|||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: 'navigate',
|
key: 'navigate',
|
||||||
value: function navigate(e) {
|
value: function navigate(delta) {
|
||||||
var _this2 = this;
|
var _this2 = this;
|
||||||
|
|
||||||
if (!this.active) return;
|
|
||||||
|
|
||||||
switch (e.which) {
|
|
||||||
case 40:case 38:
|
|
||||||
// Down/Up
|
|
||||||
this.keyWasJustPressed = true;
|
this.keyWasJustPressed = true;
|
||||||
this.setIndex(this.index + (e.which === 40 ? 1 : -1), true);
|
this.setIndex(this.index + delta, true);
|
||||||
clearTimeout(this.keyWasJustPressedTimeout);
|
clearTimeout(this.keyWasJustPressedTimeout);
|
||||||
this.keyWasJustPressedTimeout = setTimeout(function () {
|
this.keyWasJustPressedTimeout = setTimeout(function () {
|
||||||
return _this2.keyWasJustPressed = false;
|
return _this2.keyWasJustPressed = false;
|
||||||
}, 500);
|
}, 500);
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 13:case 9:
|
|
||||||
// Enter/Tab
|
|
||||||
this.$('li').eq(this.index).find('button').click();
|
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 27:
|
|
||||||
// Escape
|
|
||||||
this.hide();
|
|
||||||
e.stopPropagation();
|
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// no default
|
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
key: 'complete',
|
||||||
|
value: function complete() {
|
||||||
|
this.$('li').eq(this.index).find('button').click();
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: 'setIndex',
|
key: 'setIndex',
|
||||||
|
@@ -5,6 +5,7 @@ import ComposerBody from 'flarum/components/ComposerBody';
|
|||||||
import avatar from 'flarum/helpers/avatar';
|
import avatar from 'flarum/helpers/avatar';
|
||||||
import usernameHelper from 'flarum/helpers/username';
|
import usernameHelper from 'flarum/helpers/username';
|
||||||
import highlight from 'flarum/helpers/highlight';
|
import highlight from 'flarum/helpers/highlight';
|
||||||
|
import KeyboardNavigatable from 'flarum/utils/KeyboardNavigatable';
|
||||||
import { truncate } from 'flarum/utils/string';
|
import { truncate } from 'flarum/utils/string';
|
||||||
|
|
||||||
import AutocompleteDropdown from 'flarum/mentions/components/AutocompleteDropdown';
|
import AutocompleteDropdown from 'flarum/mentions/components/AutocompleteDropdown';
|
||||||
@@ -34,9 +35,17 @@ export default function addComposerAutocomplete() {
|
|||||||
dropdown.hide();
|
dropdown.hide();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.navigator = new KeyboardNavigatable();
|
||||||
|
this.navigator
|
||||||
|
.when(() => dropdown.active)
|
||||||
|
.onUp(() => dropdown.navigate(-1))
|
||||||
|
.onDown(() => dropdown.navigate(1))
|
||||||
|
.onSelect(dropdown.complete.bind(dropdown))
|
||||||
|
.onCancel(dropdown.hide.bind(dropdown))
|
||||||
|
.bindTo($textarea);
|
||||||
|
|
||||||
$textarea
|
$textarea
|
||||||
.after($container)
|
.after($container)
|
||||||
.on('keydown', dropdown.navigate.bind(dropdown))
|
|
||||||
.on('click keyup', function(e) {
|
.on('click keyup', function(e) {
|
||||||
// Up, down, enter, tab, escape, left, right.
|
// Up, down, enter, tab, escape, left, right.
|
||||||
if ([9, 13, 27, 40, 38, 37, 39].indexOf(e.which) !== -1) return;
|
if ([9, 13, 27, 40, 38, 37, 39].indexOf(e.which) !== -1) return;
|
||||||
|
@@ -28,32 +28,15 @@ export default class AutocompleteDropdown extends Component {
|
|||||||
this.active = false;
|
this.active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
navigate(e) {
|
navigate(delta) {
|
||||||
if (!this.active) return;
|
|
||||||
|
|
||||||
switch (e.which) {
|
|
||||||
case 40: case 38: // Down/Up
|
|
||||||
this.keyWasJustPressed = true;
|
this.keyWasJustPressed = true;
|
||||||
this.setIndex(this.index + (e.which === 40 ? 1 : -1), true);
|
this.setIndex(this.index + delta, true);
|
||||||
clearTimeout(this.keyWasJustPressedTimeout);
|
clearTimeout(this.keyWasJustPressedTimeout);
|
||||||
this.keyWasJustPressedTimeout = setTimeout(() => this.keyWasJustPressed = false, 500);
|
this.keyWasJustPressedTimeout = setTimeout(() => this.keyWasJustPressed = false, 500);
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 13: case 9: // Enter/Tab
|
|
||||||
this.$('li').eq(this.index).find('button').click();
|
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 27: // Escape
|
|
||||||
this.hide();
|
|
||||||
e.stopPropagation();
|
|
||||||
e.preventDefault();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// no default
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
complete() {
|
||||||
|
this.$('li').eq(this.index).find('button').click();
|
||||||
}
|
}
|
||||||
|
|
||||||
setIndex(index, scrollToItem) {
|
setIndex(index, scrollToItem) {
|
||||||
|
Reference in New Issue
Block a user