2018-01-30 13:01:07 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
|
|
|
|
// public attrs
|
2018-05-01 15:47:13 +03:00
|
|
|
classNames: ['gh-koenig-editor', 'relative', 'w-100', 'vh-100', 'overflow-x-hidden', 'overflow-y-auto', 'z-0'],
|
2018-01-30 13:01:07 +03:00
|
|
|
title: '',
|
|
|
|
titlePlaceholder: '',
|
|
|
|
body: null,
|
|
|
|
bodyPlaceholder: '',
|
|
|
|
bodyAutofocus: false,
|
|
|
|
|
|
|
|
// internal properties
|
|
|
|
_title: null,
|
|
|
|
_editor: null,
|
2018-06-20 15:11:12 +03:00
|
|
|
_mousedownY: 0,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// closure actions
|
|
|
|
onTitleChange() {},
|
|
|
|
onTitleBlur() {},
|
|
|
|
onBodyChange() {},
|
2018-06-14 18:55:02 +03:00
|
|
|
onEditorCreated() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
onWordCountChange() {},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
actions: {
|
2018-02-04 22:35:44 +03:00
|
|
|
focusTitle() {
|
|
|
|
this._title.focus();
|
|
|
|
},
|
|
|
|
|
2018-06-20 15:11:12 +03:00
|
|
|
// triggered when a mousedown is registered on .gh-koenig-editor-pane
|
|
|
|
trackMousedown(event) {
|
|
|
|
this._mousedownY = event.clientY;
|
|
|
|
},
|
|
|
|
|
|
|
|
// triggered when a mouseup is registered on .gh-koenig-editor-pane
|
2018-01-30 13:01:07 +03:00
|
|
|
focusEditor(event) {
|
2018-05-15 19:17:51 +03:00
|
|
|
if (event.target.classList.contains('gh-koenig-editor-pane')) {
|
|
|
|
let editorCanvas = this._editor.element;
|
|
|
|
let {bottom} = editorCanvas.getBoundingClientRect();
|
|
|
|
|
2018-06-20 15:11:12 +03:00
|
|
|
// if a mousedown and subsequent mouseup occurs below the editor
|
|
|
|
// canvas, focus the editor and put the cursor at the end of the
|
|
|
|
// document
|
|
|
|
if (this._mousedownY > bottom && event.clientY > bottom) {
|
2018-05-15 19:17:51 +03:00
|
|
|
let {post} = this._editor;
|
|
|
|
let range = post.toRange();
|
2018-05-23 16:20:03 +03:00
|
|
|
let {tailSection} = range;
|
2018-05-15 19:17:51 +03:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
this._editor.focus();
|
|
|
|
|
2018-05-23 16:20:03 +03:00
|
|
|
// we should always have a visible cursor when focusing
|
|
|
|
// at the bottom so create an empty paragraph if last
|
|
|
|
// section is a card
|
|
|
|
if (tailSection.isCardSection) {
|
|
|
|
this._editor.run((postEditor) => {
|
2018-05-15 19:17:51 +03:00
|
|
|
let newSection = postEditor.builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
tailSection = newSection;
|
2018-05-23 16:20:03 +03:00
|
|
|
});
|
|
|
|
}
|
2018-05-15 19:17:51 +03:00
|
|
|
|
2018-05-23 16:20:03 +03:00
|
|
|
this._editor.selectRange(tailSection.tailPosition());
|
2018-05-15 19:17:51 +03:00
|
|
|
|
|
|
|
// ensure we're scrolled to the bottom
|
|
|
|
this.element.scrollTop = this.element.scrollHeight;
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* title related actions -------------------------------------------- */
|
|
|
|
|
|
|
|
onTitleCreated(titleElement) {
|
|
|
|
this._title = titleElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
onTitleChange(newTitle) {
|
|
|
|
this.onTitleChange(newTitle);
|
|
|
|
},
|
|
|
|
|
|
|
|
onTitleFocusOut() {
|
|
|
|
this.onTitleBlur();
|
|
|
|
},
|
|
|
|
|
|
|
|
onTitleKeydown(event) {
|
|
|
|
let value = event.target.value;
|
|
|
|
let selectionStart = event.target.selectionStart;
|
|
|
|
|
|
|
|
// enter will always focus the editor
|
|
|
|
// down arrow will only focus the editor when the cursor is at the
|
|
|
|
// end of the input to preserve the default OS behaviour
|
|
|
|
if (
|
|
|
|
event.key === 'Enter' ||
|
|
|
|
event.key === 'Tab' ||
|
2018-02-04 22:35:44 +03:00
|
|
|
((event.key === 'ArrowDown' || event.key === 'ArrowRight') && (!value || selectionStart === value.length))
|
2018-01-30 13:01:07 +03:00
|
|
|
) {
|
|
|
|
event.preventDefault();
|
2018-03-14 16:07:50 +03:00
|
|
|
|
|
|
|
// on Enter we also want to create a blank para if necessary
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
this._addParaAtTop();
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
this._editor.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* body related actions --------------------------------------------- */
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
onEditorCreated(koenig) {
|
|
|
|
this._setupEditor(koenig);
|
|
|
|
this.onEditorCreated(koenig);
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onBodyChange(newMobiledoc) {
|
|
|
|
this.onBodyChange(newMobiledoc);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* public methods ------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* internal methods ----------------------------------------------------- */
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
_setupEditor(koenig) {
|
2018-01-30 13:01:07 +03:00
|
|
|
let component = this;
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
this._koenig = koenig;
|
|
|
|
this._editor = koenig.editor;
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// focus the title when pressing SHIFT+TAB
|
2018-06-14 18:55:02 +03:00
|
|
|
this._editor.registerKeyCommand({
|
2018-01-30 13:01:07 +03:00
|
|
|
str: 'SHIFT+TAB',
|
|
|
|
run() {
|
2018-02-04 22:35:44 +03:00
|
|
|
component.send('focusTitle');
|
2018-01-30 13:01:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-03-14 16:07:50 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_addParaAtTop() {
|
|
|
|
if (!this._editor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let editor = this._editor;
|
|
|
|
let section = editor.post.toRange().head.section;
|
|
|
|
|
|
|
|
// create a blank paragraph at the top of the editor unless it's already
|
|
|
|
// a blank paragraph
|
|
|
|
if (section.isListItem || !section.isBlank || section.text !== '') {
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let {builder} = postEditor;
|
|
|
|
let newPara = builder.createMarkupSection('p');
|
2018-06-14 15:01:54 +03:00
|
|
|
let sections = section.isListItem ? section.parent.parent.sections : section.parent.sections;
|
2018-03-14 16:07:50 +03:00
|
|
|
|
2018-06-14 15:01:54 +03:00
|
|
|
postEditor.insertSectionBefore(sections, newPara, section);
|
2018-03-14 16:07:50 +03:00
|
|
|
});
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
}
|
|
|
|
});
|