2018-01-30 13:01:07 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import layout from '../templates/components/koenig-toolbar';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
2018-02-01 19:48:16 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2018-01-30 13:01:07 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
|
|
|
|
// initially rendered offscreen with opacity 0 so that sizing is available
|
|
|
|
// shown when passed in an uncollapsed selected range
|
|
|
|
// display is delayed until the mouse button is lifted
|
|
|
|
// positioned so that it's always fully within the editor container
|
|
|
|
// animation occurs via CSS transitions
|
|
|
|
// position is kept after hiding, it's made inoperable by CSS pointer-events
|
|
|
|
|
2018-04-06 19:49:16 +03:00
|
|
|
// pixels that should be added to separate toolbar from positioning rect
|
|
|
|
export const TOOLBAR_MARGIN = 15;
|
|
|
|
|
|
|
|
// pixels that should be added to the `left` property of the tick adjustment styles
|
|
|
|
// TODO: handle via CSS?
|
|
|
|
const TICK_ADJUSTMENT = 8;
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
|
|
|
|
|
|
|
attributeBindings: ['style'],
|
2018-04-04 12:12:31 +03:00
|
|
|
classNames: ['absolute', 'z-999'],
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// public attrs
|
2018-05-14 17:53:34 +03:00
|
|
|
editor: null,
|
2018-01-31 17:49:20 +03:00
|
|
|
editorRange: null,
|
2018-02-20 18:14:28 +03:00
|
|
|
activeMarkupTagNames: null,
|
|
|
|
activeSectionTagNames: null,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// internal properties
|
|
|
|
showToolbar: false,
|
|
|
|
top: null,
|
|
|
|
left: -1000,
|
|
|
|
right: null,
|
|
|
|
|
|
|
|
// private properties
|
|
|
|
_isMouseDown: false,
|
|
|
|
_hasSelectedRange: false,
|
2018-02-01 19:48:16 +03:00
|
|
|
_onMousedownHandler: null,
|
2018-03-15 18:15:53 +03:00
|
|
|
_onMousemoveHandler: null,
|
2018-02-01 19:48:16 +03:00
|
|
|
_onMouseupHandler: null,
|
|
|
|
_onResizeHandler: null,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-02-20 18:14:28 +03:00
|
|
|
// closure actions
|
|
|
|
toggleMarkup() {},
|
|
|
|
toggleSection() {},
|
2018-05-15 14:08:58 +03:00
|
|
|
toggleHeaderSection() {},
|
2018-04-06 19:49:16 +03:00
|
|
|
editLink() {},
|
2018-02-20 18:14:28 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
/* computed properties -------------------------------------------------- */
|
|
|
|
|
2018-04-04 12:12:31 +03:00
|
|
|
style: computed('showToolbar', 'top', 'left', 'right', function () {
|
2018-01-30 13:01:07 +03:00
|
|
|
let position = this.getProperties('top', 'left', 'right');
|
|
|
|
let styles = Object.keys(position).map((style) => {
|
|
|
|
if (position[style] !== null) {
|
|
|
|
return `${style}: ${position[style]}px`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-04 12:12:31 +03:00
|
|
|
// ensure hidden toolbar is non-interactive
|
2018-05-01 19:13:53 +03:00
|
|
|
if (this.showToolbar) {
|
2018-04-04 12:12:31 +03:00
|
|
|
styles.push('pointer-events: auto !important');
|
|
|
|
} else {
|
|
|
|
styles.push('pointer-events: none !important');
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
return htmlSafe(styles.compact().join('; '));
|
|
|
|
}),
|
|
|
|
|
|
|
|
/* lifecycle hooks ------------------------------------------------------ */
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// track mousedown/mouseup on the window so that we're sure to get the
|
|
|
|
// events even when they start outside of this component or end outside
|
|
|
|
// the window
|
2018-02-01 19:48:16 +03:00
|
|
|
this._onMousedownHandler = run.bind(this, this._handleMousedown);
|
2018-01-30 13:01:07 +03:00
|
|
|
window.addEventListener('mousedown', this._onMousedownHandler);
|
2018-02-01 19:48:16 +03:00
|
|
|
this._onMouseupHandler = run.bind(this, this._handleMouseup);
|
2018-01-30 13:01:07 +03:00
|
|
|
window.addEventListener('mouseup', this._onMouseupHandler);
|
2018-02-01 19:48:16 +03:00
|
|
|
this._onResizeHandler = run.bind(this, this._handleResize);
|
|
|
|
window.addEventListener('resize', this._onResizeHandler);
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
2018-05-01 19:13:53 +03:00
|
|
|
let range = this.editorRange;
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
if (range && !range.isCollapsed) {
|
|
|
|
this._hasSelectedRange = true;
|
|
|
|
} else {
|
|
|
|
this._hasSelectedRange = false;
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:52:40 +03:00
|
|
|
if ((this._hasSelectedRange && !this.showToolbar) || (!this._hasSelectedRange && this.showToolbar)) {
|
|
|
|
this._toggleVisibility.perform();
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._removeStyleElement();
|
2018-02-01 19:48:16 +03:00
|
|
|
run.cancel(this._throttleResize);
|
2018-01-30 13:01:07 +03:00
|
|
|
window.removeEventListener('mousedown', this._onMousedownHandler);
|
2018-03-15 18:15:53 +03:00
|
|
|
window.removeEventListener('mousemove', this._onMousemoveHandler);
|
2018-01-30 13:01:07 +03:00
|
|
|
window.removeEventListener('mouseup', this._onMouseupHandler);
|
2018-02-01 19:48:16 +03:00
|
|
|
window.removeEventListener('resize', this._onResizeHandler);
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
2018-02-20 18:14:28 +03:00
|
|
|
actions: {
|
|
|
|
toggleMarkup(markupName) {
|
2018-05-01 19:13:53 +03:00
|
|
|
if (markupName === 'em' && this.activeMarkupTagNames.isI) {
|
2018-03-15 20:25:10 +03:00
|
|
|
markupName = 'i';
|
|
|
|
}
|
|
|
|
|
2018-02-20 18:14:28 +03:00
|
|
|
this.toggleMarkup(markupName);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSection(sectionName) {
|
2018-05-14 17:53:34 +03:00
|
|
|
let range = this.editorRange;
|
|
|
|
this.editor.run((postEditor) => {
|
2018-05-15 14:08:58 +03:00
|
|
|
this.toggleSection(sectionName, postEditor);
|
|
|
|
postEditor.setRange(range);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleHeaderSection(headingTagName) {
|
|
|
|
let range = this.editorRange;
|
|
|
|
this.editor.run((postEditor) => {
|
2018-05-17 17:00:43 +03:00
|
|
|
this.toggleHeaderSection(headingTagName, postEditor, {force: true});
|
2018-05-14 17:53:34 +03:00
|
|
|
postEditor.setRange(range);
|
|
|
|
});
|
2018-04-06 19:49:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
editLink() {
|
2018-05-01 19:13:53 +03:00
|
|
|
this.editLink(this.editorRange);
|
2018-02-20 18:14:28 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-03-15 17:35:39 +03:00
|
|
|
/* private methods ------------------------------------------------------ */
|
|
|
|
|
2018-03-15 18:15:53 +03:00
|
|
|
_toggleVisibility: task(function* (skipMousemove = false) {
|
2018-05-14 16:52:40 +03:00
|
|
|
// double-taps will often trigger before the selection change event so
|
|
|
|
// we want to keep the truthy mousemove skip around so that re-triggers
|
|
|
|
// within the 50ms timeout do not reset it
|
|
|
|
if (skipMousemove) {
|
|
|
|
this._skipMousemove = true;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:15:53 +03:00
|
|
|
// debounce for 50ms to account for "click to deselect" otherwise we
|
2018-01-30 13:01:07 +03:00
|
|
|
// run twice and the fade out animation jumps position
|
|
|
|
yield timeout(50);
|
|
|
|
|
|
|
|
// return early if the editorRange hasn't changed, this prevents
|
|
|
|
// re-rendering unnecessarily which can cause minor position jumps when
|
|
|
|
// styles are toggled because getBoundingClientRect on getSelection
|
|
|
|
// changes slightly depending on the style of selected text
|
2018-05-14 16:52:40 +03:00
|
|
|
if (this._hasSelectedRange && this.editorRange === this._lastRange) {
|
2018-01-30 13:01:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a range, show the toolbnar once the mouse is lifted
|
|
|
|
if (this._hasSelectedRange && !this._isMouseDown) {
|
2018-05-14 16:52:40 +03:00
|
|
|
this._showToolbar(this._skipMousemove);
|
2018-02-01 19:48:16 +03:00
|
|
|
} else {
|
|
|
|
this._hideToolbar();
|
|
|
|
}
|
2018-05-14 16:52:40 +03:00
|
|
|
|
|
|
|
this._skipMousemove = false;
|
2018-02-01 19:48:16 +03:00
|
|
|
}).restartable(),
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-02-01 19:48:16 +03:00
|
|
|
_handleMousedown(event) {
|
|
|
|
// we only care about the left mouse button
|
|
|
|
if (event.which === 1) {
|
|
|
|
this._isMouseDown = true;
|
|
|
|
}
|
|
|
|
},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-03-15 18:15:53 +03:00
|
|
|
_handleMousemove() {
|
2018-05-14 16:52:40 +03:00
|
|
|
if (this._hasSelectedRange && !this.showToolbar) {
|
2018-03-15 18:15:53 +03:00
|
|
|
this.set('showToolbar', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._removeMousemoveHandler();
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeMousemoveHandler() {
|
|
|
|
window.removeEventListener('mousemove', this._onMousemoveHandler);
|
|
|
|
this._onMousemoveHandler = null;
|
|
|
|
},
|
|
|
|
|
2018-02-01 19:48:16 +03:00
|
|
|
_handleMouseup(event) {
|
|
|
|
if (event.which === 1) {
|
|
|
|
this._isMouseDown = false;
|
2018-03-15 18:15:53 +03:00
|
|
|
// we want to skip the mousemove handler here because we know the
|
|
|
|
// selection (if there was one) was via the mouse and we don't want
|
|
|
|
// to wait for another mousemove before showing the toolbar
|
2018-05-01 19:13:53 +03:00
|
|
|
this._toggleVisibility.perform(true);
|
2018-02-01 19:48:16 +03:00
|
|
|
}
|
|
|
|
},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-02-01 19:48:16 +03:00
|
|
|
_handleResize() {
|
2018-05-01 19:13:53 +03:00
|
|
|
if (this.showToolbar) {
|
2018-02-01 19:48:16 +03:00
|
|
|
this._throttleResize = run.throttle(this, this._positionToolbar, 100);
|
|
|
|
}
|
|
|
|
},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-03-15 18:15:53 +03:00
|
|
|
_showToolbar(skipMousemove) {
|
2018-02-01 19:48:16 +03:00
|
|
|
this._positionToolbar();
|
2018-03-15 18:15:53 +03:00
|
|
|
|
|
|
|
if (skipMousemove) {
|
|
|
|
this.set('showToolbar', true);
|
|
|
|
}
|
|
|
|
|
2018-05-01 19:13:53 +03:00
|
|
|
if (!this.showToolbar && !this._onMousemoveHandler) {
|
2018-03-15 18:15:53 +03:00
|
|
|
this._onMousemoveHandler = run.bind(this, this._handleMousemove);
|
|
|
|
window.addEventListener('mousemove', this._onMousemoveHandler);
|
|
|
|
}
|
2018-02-01 19:48:16 +03:00
|
|
|
|
|
|
|
// track displayed range so that we don't re-position unnecessarily
|
2018-05-01 19:13:53 +03:00
|
|
|
this._lastRange = this.editorRange;
|
2018-02-01 19:48:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_hideToolbar() {
|
2018-05-28 18:54:16 +03:00
|
|
|
if (!this.isDestroyed || !this.isDestroying) {
|
|
|
|
this.set('showToolbar', false);
|
|
|
|
}
|
2018-02-01 19:48:16 +03:00
|
|
|
this._lastRange = null;
|
2018-03-15 18:15:53 +03:00
|
|
|
this._removeMousemoveHandler();
|
2018-02-01 19:48:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_positionToolbar() {
|
|
|
|
let containerRect = this.element.parentNode.getBoundingClientRect();
|
|
|
|
let range = window.getSelection().getRangeAt(0);
|
|
|
|
let rangeRect = range.getBoundingClientRect();
|
|
|
|
let {width, height} = this.element.getBoundingClientRect();
|
|
|
|
let newPosition = {};
|
|
|
|
|
|
|
|
// rangeRect is relative to the viewport so we need to subtract the
|
|
|
|
// container measurements to get a position relative to the container
|
|
|
|
newPosition = {
|
2018-04-06 19:49:16 +03:00
|
|
|
top: rangeRect.top - containerRect.top - height - TOOLBAR_MARGIN,
|
2018-02-01 19:48:16 +03:00
|
|
|
left: rangeRect.left - containerRect.left + rangeRect.width / 2 - width / 2,
|
|
|
|
right: null
|
|
|
|
};
|
|
|
|
|
|
|
|
let tickPosition = 50;
|
|
|
|
// don't overflow left boundary
|
|
|
|
if (newPosition.left < 0) {
|
|
|
|
newPosition.left = 0;
|
|
|
|
|
|
|
|
// calculate the tick percentage position
|
|
|
|
let absTickPosition = rangeRect.left - containerRect.left + rangeRect.width / 2;
|
|
|
|
tickPosition = absTickPosition / width * 100;
|
|
|
|
if (tickPosition < 5) {
|
|
|
|
tickPosition = 5;
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
}
|
2018-02-01 19:48:16 +03:00
|
|
|
// same for right boundary
|
|
|
|
if (newPosition.left + width > containerRect.width) {
|
|
|
|
newPosition.left = null;
|
|
|
|
newPosition.right = 0;
|
|
|
|
|
|
|
|
// calculate the tick percentage position
|
|
|
|
let absTickPosition = rangeRect.right - containerRect.right - rangeRect.width / 2;
|
|
|
|
tickPosition = 100 + absTickPosition / width * 100;
|
|
|
|
if (tickPosition > 95) {
|
|
|
|
tickPosition = 95;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the tick is a pseudo-element so we the only way we can affect it's
|
|
|
|
// style is by adding a style element to the head
|
|
|
|
this._removeStyleElement(); // reset to base styles
|
|
|
|
if (tickPosition !== 50) {
|
2018-06-15 12:29:10 +03:00
|
|
|
this._addStyleElement(tickPosition);
|
2018-02-01 19:48:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the toolbar position
|
|
|
|
this.setProperties(newPosition);
|
|
|
|
},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-06-15 12:29:10 +03:00
|
|
|
_addStyleElement(tickPosition) {
|
|
|
|
let beforeStyle = `left: calc(${tickPosition}% - ${TICK_ADJUSTMENT + 2}px);`;
|
|
|
|
let afterStyle = `left: calc(${tickPosition}% - ${TICK_ADJUSTMENT}px);`;
|
2018-01-30 13:01:07 +03:00
|
|
|
let styleElement = document.createElement('style');
|
|
|
|
styleElement.id = `${this.elementId}-style`;
|
2018-06-15 12:29:10 +03:00
|
|
|
styleElement.innerHTML = `
|
|
|
|
#${this.elementId} > ul:before { ${beforeStyle} }
|
|
|
|
#${this.elementId} > ul:after { ${afterStyle} }
|
|
|
|
`;
|
2018-01-30 13:01:07 +03:00
|
|
|
document.head.appendChild(styleElement);
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeStyleElement() {
|
|
|
|
let styleElement = document.querySelector(`#${this.elementId}-style`);
|
|
|
|
if (styleElement) {
|
|
|
|
styleElement.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|