2018-06-12 20:08:27 +03:00
|
|
|
import Component from '@ember/component';
|
2018-06-21 19:57:45 +03:00
|
|
|
import Key from 'mobiledoc-kit/utils/key';
|
2018-06-12 20:08:27 +03:00
|
|
|
import {computed} from '@ember/object';
|
2019-02-24 12:07:09 +03:00
|
|
|
import {kgStyle} from '../helpers/kg-style';
|
2018-06-12 20:08:27 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2019-05-01 17:13:49 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2018-06-12 20:08:27 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2019-05-01 17:13:49 +03:00
|
|
|
koenigUi: service(),
|
|
|
|
|
2018-06-12 20:08:27 +03:00
|
|
|
tagName: 'figcaption',
|
|
|
|
classNameBindings: ['figCaptionClass'],
|
|
|
|
|
|
|
|
caption: '',
|
2018-08-08 16:45:01 +03:00
|
|
|
captureInput: false,
|
2018-06-12 20:08:27 +03:00
|
|
|
placeholder: '',
|
|
|
|
|
|
|
|
_keypressHandler: null,
|
|
|
|
_keydownHandler: null,
|
|
|
|
|
|
|
|
update() {},
|
2018-06-21 13:03:41 +03:00
|
|
|
addParagraphAfterCard() {},
|
|
|
|
moveCursorToNextSection() {},
|
|
|
|
moveCursorToPrevSection() {},
|
2018-06-12 20:08:27 +03:00
|
|
|
|
|
|
|
figCaptionClass: computed(function () {
|
2018-08-08 14:53:35 +03:00
|
|
|
return `${kgStyle(['figcaption'])} w-100 relative`;
|
2018-06-12 20:08:27 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (this.captureInput && !this._keypressHandler) {
|
|
|
|
this._attachHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.captureInput && this._keypressHandler) {
|
|
|
|
this._detachHandlers();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
2019-05-01 17:13:49 +03:00
|
|
|
this.koenigUi.captionLostFocus(this);
|
2018-06-12 20:08:27 +03:00
|
|
|
this._detachHandlers();
|
|
|
|
},
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
actions: {
|
|
|
|
registerEditor(editor) {
|
|
|
|
let commands = {
|
|
|
|
ENTER: run.bind(this, this._enter),
|
|
|
|
ESC: run.bind(this, this._escape),
|
|
|
|
UP: run.bind(this, this._upOrLeft),
|
|
|
|
LEFT: run.bind(this, this._upOrLeft),
|
|
|
|
DOWN: run.bind(this, this._rightOrDown),
|
|
|
|
RIGHT: run.bind(this, this._rightOrDown)
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(commands).forEach((str) => {
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str,
|
|
|
|
run() {
|
|
|
|
return commands[str](editor, str);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editor = editor;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEnter() {
|
|
|
|
this.addParagraphAfterCard();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-05-01 17:13:49 +03:00
|
|
|
// events ------------------------------------------------------------------
|
|
|
|
|
|
|
|
focusIn() {
|
|
|
|
this.koenigUi.captionGainedFocus(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
focusOut() {
|
|
|
|
this.koenigUi.captionLostFocus(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
// private -----------------------------------------------------------------
|
|
|
|
|
2018-06-12 20:08:27 +03:00
|
|
|
_attachHandlers() {
|
|
|
|
if (!this._keypressHandler) {
|
|
|
|
this._keypressHandler = run.bind(this, this._handleKeypress);
|
|
|
|
window.addEventListener('keypress', this._keypressHandler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_detachHandlers() {
|
|
|
|
window.removeEventListener('keypress', this._keypressHandler);
|
|
|
|
this._keypressHandler = null;
|
|
|
|
this._keydownHandler = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// only fires if the card is selected, moves focus to the caption input so
|
|
|
|
// that it's possible to start typing without explicitly focusing the input
|
|
|
|
_handleKeypress(event) {
|
2018-06-21 19:57:45 +03:00
|
|
|
let key = new Key(event);
|
2018-08-08 14:53:35 +03:00
|
|
|
let {editor} = this;
|
|
|
|
|
2018-08-11 20:26:30 +03:00
|
|
|
if (event.target.matches('[data-kg="editor"]') && editor && !editor._hasFocus() && key.isPrintableKey() && !key.isEnter()) {
|
2018-08-08 14:53:35 +03:00
|
|
|
editor.focus();
|
|
|
|
editor.run((postEditor) => {
|
2018-08-11 20:26:30 +03:00
|
|
|
postEditor.insertText(editor.post.tailPosition(), key.toString());
|
2018-08-08 14:53:35 +03:00
|
|
|
});
|
2018-06-12 20:08:27 +03:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
/* key commands ----------------------------------------------------------*/
|
|
|
|
|
|
|
|
_enter() {
|
|
|
|
this.send('handleEnter');
|
|
|
|
},
|
|
|
|
|
|
|
|
_escape(editor) {
|
2018-12-13 18:22:47 +03:00
|
|
|
editor.element.blur();
|
2018-08-08 14:53:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_upOrLeft(editor, key) {
|
|
|
|
let {isCollapsed, head} = editor.range;
|
|
|
|
|
|
|
|
if (isCollapsed && head.isEqual(head.section.headPosition())) {
|
|
|
|
return this.moveCursorToPrevSection();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're simulating a text input so up/down move the cursor to the
|
|
|
|
// beginning/end of the input
|
|
|
|
if (isCollapsed && key === 'UP') {
|
|
|
|
return editor.selectRange(head.section.headPosition().toRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_rightOrDown(editor, key) {
|
|
|
|
let {isCollapsed, tail} = editor.range;
|
|
|
|
|
|
|
|
if (isCollapsed && tail.isEqual(tail.section.tailPosition())) {
|
|
|
|
return this.moveCursorToNextSection();
|
2018-06-12 20:08:27 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// we're simulating a text input so up/down move the cursor to the
|
|
|
|
// beginning/end of the input
|
|
|
|
if (isCollapsed && key === 'DOWN') {
|
|
|
|
return editor.selectRange(tail.section.tailPosition().toRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-06-12 20:08:27 +03:00
|
|
|
}
|
|
|
|
});
|