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
|
|
|
|
|
|
|
|
const TOOLBAR_TOP_MARGIN = 15;
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
|
|
|
|
|
|
|
attributeBindings: ['style'],
|
|
|
|
|
|
|
|
// public attrs
|
|
|
|
classNames: ['koenig-toolbar'],
|
|
|
|
classNameBindings: ['showToolbar:koenig-toolbar--visible'],
|
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-01-30 13:01:07 +03:00
|
|
|
/* computed properties -------------------------------------------------- */
|
|
|
|
|
|
|
|
style: computed('top', 'left', 'right', function () {
|
|
|
|
let position = this.getProperties('top', 'left', 'right');
|
|
|
|
let styles = Object.keys(position).map((style) => {
|
|
|
|
if (position[style] !== null) {
|
|
|
|
return `${style}: ${position[style]}px`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
let range = this.get('editorRange');
|
|
|
|
|
|
|
|
if (range && !range.isCollapsed) {
|
|
|
|
this._hasSelectedRange = true;
|
|
|
|
} else {
|
|
|
|
this._hasSelectedRange = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('_toggleVisibility').perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
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-03-15 20:25:10 +03:00
|
|
|
if (markupName === 'em' && this.get('activeMarkupTagNames.isI')) {
|
|
|
|
markupName = 'i';
|
|
|
|
}
|
|
|
|
|
2018-02-20 18:14:28 +03:00
|
|
|
this.toggleMarkup(markupName);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSection(sectionName) {
|
|
|
|
this.toggleSection(sectionName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-03-15 17:35:39 +03:00
|
|
|
/* private methods ------------------------------------------------------ */
|
|
|
|
|
2018-03-15 18:15:53 +03:00
|
|
|
_toggleVisibility: task(function* (skipMousemove = false) {
|
|
|
|
// 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
|
|
|
|
if (this.get('editorRange') === this._lastRange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a range, show the toolbnar once the mouse is lifted
|
|
|
|
if (this._hasSelectedRange && !this._isMouseDown) {
|
2018-03-15 18:15:53 +03:00
|
|
|
this._showToolbar(skipMousemove);
|
2018-02-01 19:48:16 +03:00
|
|
|
} else {
|
|
|
|
this._hideToolbar();
|
|
|
|
}
|
|
|
|
}).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() {
|
|
|
|
if (!this.get('showToolbar')) {
|
|
|
|
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
|
|
|
|
this.get('_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() {
|
|
|
|
if (this.get('showToolbar')) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.get('showToolbar') && !this._onMousemoveHandler) {
|
|
|
|
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
|
|
|
|
this._lastRange = this.get('editorRange');
|
|
|
|
},
|
|
|
|
|
|
|
|
_hideToolbar() {
|
|
|
|
this.set('showToolbar', false);
|
|
|
|
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 = {
|
|
|
|
top: rangeRect.top - containerRect.top - height - TOOLBAR_TOP_MARGIN,
|
|
|
|
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) {
|
|
|
|
this._addStyleElement(`left: ${tickPosition}%`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the toolbar position
|
|
|
|
this.setProperties(newPosition);
|
|
|
|
},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
_addStyleElement(styles) {
|
|
|
|
let styleElement = document.createElement('style');
|
|
|
|
styleElement.id = `${this.elementId}-style`;
|
|
|
|
styleElement.innerHTML = `#${this.elementId}:after { ${styles} }`;
|
|
|
|
document.head.appendChild(styleElement);
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeStyleElement() {
|
|
|
|
let styleElement = document.querySelector(`#${this.elementId}-style`);
|
|
|
|
if (styleElement) {
|
|
|
|
styleElement.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|