2018-02-03 19:54:19 +03:00
|
|
|
import Component from '@ember/component';
|
2018-06-07 13:34:50 +03:00
|
|
|
import {CARD_MENU} from '../options/cards';
|
2018-08-09 19:55:11 +03:00
|
|
|
import {assign} from '@ember/polyfills';
|
2018-06-07 13:34:50 +03:00
|
|
|
import {computed, set} from '@ember/object';
|
2018-02-03 19:54:19 +03:00
|
|
|
import {htmlSafe} from '@ember/string';
|
2018-06-07 13:34:50 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2018-02-03 19:54:19 +03:00
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
|
2018-05-16 18:24:41 +03:00
|
|
|
const ROW_LENGTH = 3;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
// public attrs
|
2018-05-01 15:47:13 +03:00
|
|
|
classNames: 'absolute',
|
2018-02-03 19:54:19 +03:00
|
|
|
attributeBindings: ['style'],
|
|
|
|
editor: null,
|
|
|
|
editorRange: null,
|
|
|
|
|
|
|
|
// public properties
|
|
|
|
showMenu: false,
|
|
|
|
top: 0,
|
2018-06-07 13:34:50 +03:00
|
|
|
itemSections: null,
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
// private properties
|
|
|
|
_openRange: null,
|
|
|
|
_query: '',
|
|
|
|
_onWindowMousedownHandler: null,
|
2020-04-21 12:12:47 +03:00
|
|
|
_yOffset: 16,
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
// closure actions
|
|
|
|
replaceWithCardSection() {},
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
// computed properties
|
2018-02-03 19:54:19 +03:00
|
|
|
style: computed('top', function () {
|
2018-05-01 19:13:53 +03:00
|
|
|
return htmlSafe(`top: ${this.top}px`);
|
2018-02-03 19:54:19 +03:00
|
|
|
}),
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
// create a 2-dimensional array of items based on the ROW_LENGTH, eg
|
|
|
|
// [
|
|
|
|
// [item1, item1, item3]
|
|
|
|
// [item4, item5],
|
|
|
|
// [item6, item7, item8]
|
|
|
|
// [item9]
|
|
|
|
// ]
|
|
|
|
// - used for arrow key movement of selected item
|
|
|
|
itemMap: computed('itemSections', function () {
|
|
|
|
let map = [];
|
|
|
|
|
|
|
|
this.itemSections.forEach((section) => {
|
|
|
|
let iterations = Math.ceil(section.items.length / ROW_LENGTH);
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
let startIndex = i * ROW_LENGTH;
|
|
|
|
map.push(section.items.slice(startIndex, startIndex + ROW_LENGTH));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}),
|
|
|
|
|
2018-02-03 19:54:19 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
2018-07-11 17:08:51 +03:00
|
|
|
|
|
|
|
// re-register the / text input handler if the editor changes such as
|
2019-02-22 06:17:33 +03:00
|
|
|
// when a "New post" is clicked from the sidebar or a different post
|
2018-07-11 17:08:51 +03:00
|
|
|
// is loaded via search
|
|
|
|
if (this.editor !== this._lastEditor) {
|
|
|
|
this.editor.onTextInput({
|
|
|
|
name: 'slash_menu',
|
|
|
|
text: '/',
|
|
|
|
run: run.bind(this, this._showMenu)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this._lastEditor = this.editor;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
// re-position the menu and update the query if necessary when the
|
|
|
|
// cursor position changes
|
2018-07-11 17:08:51 +03:00
|
|
|
let editorRange = this.editorRange;
|
2018-02-03 19:54:19 +03:00
|
|
|
if (editorRange !== this._lastEditorRange) {
|
|
|
|
this._handleCursorChange(editorRange);
|
|
|
|
}
|
|
|
|
this._lastEditorRange = editorRange;
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
window.removeEventListener('mousedown', this._onMousedownHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2018-07-17 19:22:28 +03:00
|
|
|
itemClicked(item, event) {
|
2018-02-03 19:54:19 +03:00
|
|
|
let range = this._openRange.head.section.toRange();
|
2018-07-17 16:32:49 +03:00
|
|
|
let [, ...params] = this._query.split(/\s/);
|
2018-08-09 19:55:11 +03:00
|
|
|
let payload = assign({}, item.payload);
|
2018-06-07 13:34:50 +03:00
|
|
|
|
2018-07-17 19:22:28 +03:00
|
|
|
// make sure the click doesn't propagate and get picked up by the
|
|
|
|
// newly inserted card which can then remove itself because it
|
|
|
|
// looks like a click outside of an empty card
|
2018-07-18 13:41:01 +03:00
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
2018-07-17 19:22:28 +03:00
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
// params are order-dependent and listed in CARD_MENU for each card
|
2018-07-17 16:32:49 +03:00
|
|
|
if (!isEmpty(item.params) && !isEmpty(params)) {
|
2018-06-07 13:34:50 +03:00
|
|
|
item.params.forEach((param, i) => {
|
|
|
|
payload[param] = params[i];
|
|
|
|
});
|
|
|
|
}
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
if (item.type === 'card') {
|
2018-06-07 13:34:50 +03:00
|
|
|
this.replaceWithCardSection(item.replaceArg, range, payload);
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this._hideMenu();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleCursorChange(editorRange) {
|
|
|
|
// update menu position to match cursor position
|
|
|
|
this._positionMenu(editorRange);
|
|
|
|
|
2018-05-01 19:13:53 +03:00
|
|
|
if (this.showMenu && editorRange) {
|
2018-04-25 17:19:09 +03:00
|
|
|
let {head: {section}} = editorRange;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
2018-04-25 17:19:09 +03:00
|
|
|
// close the menu if we're on a non-slash section (eg, when / is deleted)
|
2018-05-14 17:48:29 +03:00
|
|
|
if (section && (section.text || section.text === '') && section.text.indexOf('/') !== 0) {
|
2018-04-25 17:19:09 +03:00
|
|
|
this._hideMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the query when the menu is open and cursor is in our open range
|
|
|
|
if (section === this._openRange.head.section) {
|
|
|
|
let query = section.text.substring(
|
|
|
|
this._openRange.head.offset,
|
|
|
|
editorRange.head.offset
|
|
|
|
);
|
|
|
|
this._updateQuery(query);
|
|
|
|
}
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateQuery(query) {
|
2018-06-07 13:34:50 +03:00
|
|
|
this._query = query;
|
|
|
|
|
|
|
|
// match everything before a space to a card. Keeps the relevant
|
|
|
|
// card selected when providing attributes to a card, eg:
|
|
|
|
// /twitter https://twitter.com/EffinBirds/status/1001765208958881792
|
2019-04-29 18:08:40 +03:00
|
|
|
let card = query.split(/\s/)[0].replace(/^\//, '');
|
2018-06-07 13:34:50 +03:00
|
|
|
|
|
|
|
let matchedItems = CARD_MENU.map((section) => {
|
2018-02-03 19:54:19 +03:00
|
|
|
// show all items before anything is typed
|
2018-06-07 13:34:50 +03:00
|
|
|
if (!card) {
|
|
|
|
return section;
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// show icons where there's a match of the begining of one of the
|
|
|
|
// "item.matches" strings
|
2018-06-07 13:34:50 +03:00
|
|
|
let matches = section.items.filter(item => item.matches.any(match => match.indexOf(card) === 0));
|
|
|
|
if (matches.length > 0) {
|
|
|
|
return {title: section.title, items: matches};
|
|
|
|
}
|
|
|
|
}).compact();
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
// we need a copy to avoid modifying the object references
|
2019-01-30 17:44:53 +03:00
|
|
|
let sections = JSON.parse(JSON.stringify(matchedItems || []));
|
2018-02-03 19:54:19 +03:00
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
if (sections.length) {
|
|
|
|
set(sections[0].items[0], 'selected', true);
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
this.set('itemSections', sections);
|
2018-02-03 19:54:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_showMenu() {
|
2018-05-01 19:13:53 +03:00
|
|
|
let editorRange = this.editorRange;
|
2018-02-03 19:54:19 +03:00
|
|
|
let {head: {section}} = editorRange;
|
|
|
|
|
|
|
|
// only show the menu if the slash is on an otherwise empty paragraph
|
2018-05-01 19:13:53 +03:00
|
|
|
if (!this.showMenu && editorRange.isCollapsed && section && !section.isListItem && section.text === '/') {
|
2018-02-03 19:54:19 +03:00
|
|
|
this.set('showMenu', true);
|
|
|
|
|
|
|
|
// ensure all items are shown before we have a query filter
|
|
|
|
this._updateQuery('');
|
|
|
|
|
|
|
|
// store a ref to the range when the menu was triggered so that we
|
|
|
|
// can query text after the slash
|
2018-05-01 19:13:53 +03:00
|
|
|
this._openRange = this.editorRange;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
// set up key handlers for selection & closing
|
|
|
|
this._registerKeyboardNavHandlers();
|
|
|
|
|
|
|
|
// watch the window for mousedown events so that we can close the
|
|
|
|
// menu when we detect a click outside. This is preferable to
|
|
|
|
// watching the range because the range will change and remove the
|
|
|
|
// menu before click events on the buttons are registered
|
|
|
|
this._onWindowMousedownHandler = run.bind(this, (event) => {
|
|
|
|
this._handleWindowMousedown(event);
|
|
|
|
});
|
|
|
|
window.addEventListener('mousedown', this._onWindowMousedownHandler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_hideMenu() {
|
2018-05-01 19:13:53 +03:00
|
|
|
if (this.showMenu) {
|
2018-02-03 19:54:19 +03:00
|
|
|
this.set('showMenu', false);
|
|
|
|
this._unregisterKeyboardNavHandlers();
|
|
|
|
window.removeEventListener('mousedown', this._onWindowMousedownHandler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleWindowMousedown(event) {
|
|
|
|
// clicks outside the menu should always close
|
|
|
|
if (!event.target.closest(`#${this.elementId}`)) {
|
|
|
|
this._hideMenu();
|
|
|
|
|
|
|
|
// clicks on the menu but not on a button should be ignored so that the
|
|
|
|
// cursor position isn't lost
|
2018-05-02 19:31:17 +03:00
|
|
|
} else if (!event.target.closest('[data-kg="cardmenu-card"]')) {
|
2018-02-03 19:54:19 +03:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_positionMenu(range) {
|
|
|
|
if (!range) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let {head: {section}} = range;
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
if (section && section.renderNode.element) {
|
2018-02-03 19:54:19 +03:00
|
|
|
let containerRect = this.element.parentNode.getBoundingClientRect();
|
|
|
|
let selectedElement = section.renderNode.element;
|
|
|
|
let selectedElementRect = selectedElement.getBoundingClientRect();
|
2020-04-21 12:12:47 +03:00
|
|
|
let top = selectedElementRect.top + selectedElementRect.height - containerRect.top + this._yOffset;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
this.set('top', top);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_registerKeyboardNavHandlers() {
|
|
|
|
// ESC = close menu
|
|
|
|
// ARROWS = selection
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'ESC',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._hideMenu)
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'ENTER',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._performAction)
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'UP',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._moveSelection, 'up')
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'DOWN',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._moveSelection, 'down')
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'LEFT',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._moveSelection, 'left')
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'RIGHT',
|
|
|
|
name: 'slash-menu',
|
|
|
|
run: run.bind(this, this._moveSelection, 'right')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_performAction() {
|
|
|
|
let selectedItem = this._getSelectedItem();
|
|
|
|
|
|
|
|
if (selectedItem) {
|
2018-07-17 16:32:49 +03:00
|
|
|
this.send('itemClicked', selectedItem);
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getSelectedItem() {
|
2018-06-07 13:34:50 +03:00
|
|
|
let sections = this.itemSections;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
if (sections.length <= 0) {
|
2018-02-03 19:54:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
for (let section of sections) {
|
2020-10-05 11:56:21 +03:00
|
|
|
let item = section.items.find(sectionItem => sectionItem.selected);
|
2018-06-07 13:34:50 +03:00
|
|
|
if (item) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 19:54:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_moveSelection(direction) {
|
2018-06-07 13:34:50 +03:00
|
|
|
let itemMap = this.itemMap;
|
2018-02-03 19:54:19 +03:00
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
if (isEmpty(itemMap)) {
|
2018-02-03 19:54:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
let selectedItem = this._getSelectedItem();
|
|
|
|
let selectedRow = itemMap.find(row => row.includes(selectedItem));
|
|
|
|
let selectedRowIndex = itemMap.indexOf(selectedRow);
|
|
|
|
let selectedItemIndex = selectedRow.indexOf(selectedItem);
|
|
|
|
let lastRowIndex = itemMap.length - 1;
|
|
|
|
let lastItemIndex = selectedRow.length - 1;
|
|
|
|
|
2018-02-03 19:54:19 +03:00
|
|
|
set(selectedItem, 'selected', false);
|
|
|
|
|
|
|
|
if (direction === 'right') {
|
2018-06-07 13:34:50 +03:00
|
|
|
selectedItemIndex += 1;
|
|
|
|
if (selectedItemIndex > lastItemIndex) {
|
|
|
|
if (selectedRowIndex < lastRowIndex) {
|
|
|
|
selectedRowIndex += 1;
|
|
|
|
} else {
|
|
|
|
selectedRowIndex = 0;
|
|
|
|
}
|
|
|
|
selectedItemIndex = 0;
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
} else if (direction === 'left') {
|
2018-06-07 13:34:50 +03:00
|
|
|
selectedItemIndex -= 1;
|
|
|
|
if (selectedItemIndex < 0) {
|
|
|
|
if (selectedRowIndex > 0) {
|
|
|
|
selectedRowIndex -= 1;
|
|
|
|
} else {
|
|
|
|
selectedRowIndex = itemMap.length - 1;
|
|
|
|
}
|
|
|
|
selectedItemIndex = itemMap[selectedRowIndex].length - 1;
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
} else if (direction === 'up') {
|
2018-06-07 13:34:50 +03:00
|
|
|
selectedRowIndex -= 1;
|
|
|
|
if (selectedRowIndex < 0) {
|
|
|
|
selectedRowIndex = lastRowIndex;
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
} else if (direction === 'down') {
|
2018-06-07 13:34:50 +03:00
|
|
|
selectedRowIndex += 1;
|
|
|
|
if (selectedRowIndex > lastRowIndex) {
|
|
|
|
selectedRowIndex = 0;
|
2018-02-03 19:54:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
if (selectedItemIndex > itemMap[selectedRowIndex].length - 1) {
|
|
|
|
selectedItemIndex = itemMap[selectedRowIndex].length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(itemMap[selectedRowIndex][selectedItemIndex], 'selected', true);
|
2018-02-03 19:54:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_unregisterKeyboardNavHandlers() {
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-02-03 19:54:19 +03:00
|
|
|
editor.unregisterKeyCommands('slash-menu');
|
|
|
|
}
|
|
|
|
});
|