2017-01-10 19:22:40 +01:00
|
|
|
/**
|
|
|
|
* Inline toolbar
|
|
|
|
*
|
|
|
|
* Contains from tools:
|
|
|
|
* Bold, Italic, Underline and Anchor
|
|
|
|
*
|
|
|
|
* @author Codex Team
|
|
|
|
* @version 1.0
|
|
|
|
*/
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
module.exports = (function (inline) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
let editor = codex.editor;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
inline.buttonsOpened = null;
|
|
|
|
inline.actionsOpened = null;
|
|
|
|
inline.wrappersOffset = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saving selection that need for execCommand for styling
|
|
|
|
*
|
|
|
|
*/
|
2017-01-11 19:19:00 +01:00
|
|
|
inline.storedSelection = null;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
*
|
|
|
|
* Open inline toobar
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.show = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var currentNode = editor.content.currentNode,
|
2017-01-11 19:19:00 +01:00
|
|
|
tool = currentNode.dataset.tool,
|
|
|
|
plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tool allowed to open inline toolbar
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
plugin = editor.tools[tool];
|
2017-01-11 19:19:00 +01:00
|
|
|
|
|
|
|
if (!plugin.showInlineToolbar)
|
|
|
|
return;
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var selectedText = inline.getSelectionText(),
|
|
|
|
toolbar = editor.nodes.inlineToolbar.wrapper;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
if (selectedText.length > 0) {
|
|
|
|
|
|
|
|
/** Move toolbar and open */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.move();
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/** Open inline toolbar */
|
|
|
|
toolbar.classList.add('opened');
|
|
|
|
|
|
|
|
/** show buttons of inline toolbar */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.showButtons();
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
*
|
|
|
|
* Closes inline toolbar
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.close = function () {
|
|
|
|
|
|
|
|
var toolbar = editor.nodes.inlineToolbar.wrapper;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
toolbar.classList.remove('opened');
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Moving toolbar
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.move = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
if (!this.wrappersOffset) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
this.wrappersOffset = this.getWrappersOffset();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var coords = this.getSelectionCoords(),
|
|
|
|
defaultOffset = 0,
|
2017-02-01 18:25:59 +01:00
|
|
|
toolbar = editor.nodes.inlineToolbar.wrapper,
|
2016-12-07 19:25:31 +01:00
|
|
|
newCoordinateX,
|
|
|
|
newCoordinateY;
|
|
|
|
|
2018-05-25 16:49:46 +02:00
|
|
|
if (!coords) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
if (toolbar.offsetHeight === 0) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
defaultOffset = 40;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newCoordinateX = coords.x - this.wrappersOffset.left;
|
|
|
|
newCoordinateY = coords.y + window.scrollY - this.wrappersOffset.top - defaultOffset - toolbar.offsetHeight;
|
|
|
|
|
|
|
|
toolbar.style.transform = `translate3D(${Math.floor(newCoordinateX)}px, ${Math.floor(newCoordinateY)}px, 0)`;
|
|
|
|
|
|
|
|
/** Close everything */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.closeButtons();
|
|
|
|
editor.toolbar.inline.closeAction();
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Tool Clicked
|
|
|
|
*/
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.toolClicked = function (event, type) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For simple tools we use default browser function
|
|
|
|
* For more complicated tools, we should write our own behavior
|
|
|
|
*/
|
|
|
|
switch (type) {
|
2017-02-01 18:25:59 +01:00
|
|
|
case 'createLink' : editor.toolbar.inline.createLinkAction(event, type); break;
|
|
|
|
default : editor.toolbar.inline.defaultToolAction(type); break;
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* highlight buttons
|
|
|
|
* after making some action
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.nodes.inlineToolbar.buttons.childNodes.forEach(editor.toolbar.inline.hightlight);
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Saving wrappers offset in DOM
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.getWrappersOffset = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var wrapper = editor.nodes.wrapper,
|
2016-12-07 19:25:31 +01:00
|
|
|
offset = this.getOffset(wrapper);
|
|
|
|
|
|
|
|
this.wrappersOffset = offset;
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Calculates offset of DOM element
|
|
|
|
*
|
|
|
|
* @param el
|
|
|
|
* @returns {{top: number, left: number}}
|
|
|
|
*/
|
|
|
|
inline.getOffset = function ( el ) {
|
|
|
|
|
|
|
|
var _x = 0;
|
|
|
|
var _y = 0;
|
|
|
|
|
|
|
|
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
_x += (el.offsetLeft + el.clientLeft);
|
|
|
|
_y += (el.offsetTop + el.clientTop);
|
|
|
|
el = el.offsetParent;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
return { top: _y, left: _x };
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Calculates position of selected text
|
|
|
|
* @returns {{x: number, y: number}}
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.getSelectionCoords = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var sel = document.selection, range;
|
|
|
|
var x = 0, y = 0;
|
|
|
|
|
|
|
|
if (sel) {
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
if (sel.type != 'Control') {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
range = sel.createRange();
|
|
|
|
range.collapse(true);
|
|
|
|
x = range.boundingLeft;
|
|
|
|
y = range.boundingTop;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (window.getSelection) {
|
|
|
|
|
|
|
|
sel = window.getSelection();
|
|
|
|
|
|
|
|
if (sel.rangeCount) {
|
|
|
|
|
|
|
|
range = sel.getRangeAt(0).cloneRange();
|
|
|
|
if (range.getClientRects) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
range.collapse(true);
|
|
|
|
var rect = range.getClientRects()[0];
|
2017-01-11 19:19:00 +01:00
|
|
|
|
|
|
|
if (!rect) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2017-01-11 19:19:00 +01:00
|
|
|
return;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2017-01-11 19:19:00 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
x = rect.left;
|
|
|
|
y = rect.top;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
return { x: x, y: y };
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Returns selected text as String
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.getSelectionText = function () {
|
|
|
|
|
|
|
|
var selectedText = '';
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
// all modern browsers and IE9+
|
|
|
|
if (window.getSelection) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
selectedText = window.getSelection().toString();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return selectedText;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Opens buttons block */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.showButtons = function () {
|
|
|
|
|
|
|
|
var buttons = editor.nodes.inlineToolbar.buttons;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
buttons.classList.add('opened');
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.buttonsOpened = true;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/** highlight buttons */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.nodes.inlineToolbar.buttons.childNodes.forEach(editor.toolbar.inline.hightlight);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Makes buttons disappear */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.closeButtons = function () {
|
|
|
|
|
|
|
|
var buttons = editor.nodes.inlineToolbar.buttons;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
buttons.classList.remove('opened');
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.buttonsOpened = false;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Open buttons defined action if exist */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.showActions = function () {
|
|
|
|
|
|
|
|
var action = editor.nodes.inlineToolbar.actions;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
action.classList.add('opened');
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.actionsOpened = true;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Close actions block */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.closeAction = function () {
|
|
|
|
|
|
|
|
var action = editor.nodes.inlineToolbar.actions;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
action.innerHTML = '';
|
|
|
|
action.classList.remove('opened');
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.actionsOpened = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for keydowns in inline toolbar "Insert link..." input
|
|
|
|
*/
|
|
|
|
let inlineToolbarAnchorInputKeydown_ = function (event) {
|
|
|
|
|
|
|
|
if (event.keyCode != editor.core.keys.ENTER) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let editable = editor.content.currentNode,
|
|
|
|
storedSelection = editor.toolbar.inline.storedSelection;
|
|
|
|
|
|
|
|
editor.toolbar.inline.restoreSelection(editable, storedSelection);
|
|
|
|
editor.toolbar.inline.setAnchor(this.value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preventing events that will be able to happen
|
|
|
|
*/
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
editor.toolbar.inline.clearRange();
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Action for link creation or for setting anchor */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.createLinkAction = function (event) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var isActive = this.isLinkActive();
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var editable = editor.content.currentNode,
|
|
|
|
storedSelection = editor.toolbar.inline.saveSelection(editable);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
/** Save globally selection */
|
|
|
|
editor.toolbar.inline.storedSelection = storedSelection;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
if (isActive) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changing stored selection. if we want to remove anchor from word
|
|
|
|
* we should remove anchor from whole word, not only selected part.
|
|
|
|
* The solution is than we get the length of current link
|
|
|
|
* Change start position to - end of selection minus length of anchor
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.restoreSelection(editable, storedSelection);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.defaultToolAction('unlink');
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/** Create input and close buttons */
|
2017-02-01 18:25:59 +01:00
|
|
|
var action = editor.draw.inputForLink();
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.nodes.inlineToolbar.actions.appendChild(action);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.closeButtons();
|
|
|
|
editor.toolbar.inline.showActions();
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* focus to input
|
|
|
|
* Solution: https://developer.mozilla.org/ru/docs/Web/API/HTMLElement/focus
|
|
|
|
* Prevents event after showing input and when we need to focus an input which is in unexisted form
|
|
|
|
*/
|
|
|
|
action.focus();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
/** Callback to link action */
|
2017-02-13 18:54:18 +01:00
|
|
|
editor.listeners.add(action, 'keydown', inlineToolbarAnchorInputKeydown_, false);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.isLinkActive = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var isActive = false;
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.nodes.inlineToolbar.buttons.childNodes.forEach(function (tool) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var dataType = tool.dataset.type;
|
|
|
|
|
|
|
|
if (dataType == 'link' && tool.classList.contains('hightlighted')) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
isActive = true;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return isActive;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** default action behavior of tool */
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.defaultToolAction = function (type) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
document.execCommand(type, false, null);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Sets URL
|
|
|
|
*
|
|
|
|
* @param {String} url - URL
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.setAnchor = function (url) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
document.execCommand('createLink', false, url);
|
|
|
|
|
|
|
|
/** Close after URL inserting */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.toolbar.inline.closeAction();
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Saves selection
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.saveSelection = function (containerEl) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var range = window.getSelection().getRangeAt(0),
|
|
|
|
preSelectionRange = range.cloneRange(),
|
|
|
|
start;
|
|
|
|
|
|
|
|
preSelectionRange.selectNodeContents(containerEl);
|
|
|
|
preSelectionRange.setEnd(range.startContainer, range.startOffset);
|
|
|
|
|
|
|
|
start = preSelectionRange.toString().length;
|
|
|
|
|
|
|
|
return {
|
|
|
|
start: start,
|
|
|
|
end: start + range.toString().length
|
|
|
|
};
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Sets to previous selection (Range)
|
|
|
|
*
|
|
|
|
* @param {Element} containerEl - editable element where we restore range
|
|
|
|
* @param {Object} savedSel - range basic information to restore
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.restoreSelection = function (containerEl, savedSel) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var range = document.createRange(),
|
|
|
|
charIndex = 0;
|
|
|
|
|
|
|
|
range.setStart(containerEl, 0);
|
|
|
|
range.collapse(true);
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var nodeStack = [ containerEl ],
|
2016-12-07 19:25:31 +01:00
|
|
|
node,
|
|
|
|
foundStart = false,
|
|
|
|
stop = false,
|
|
|
|
nextCharIndex;
|
|
|
|
|
|
|
|
while (!stop && (node = nodeStack.pop())) {
|
|
|
|
|
|
|
|
if (node.nodeType == 3) {
|
|
|
|
|
|
|
|
nextCharIndex = charIndex + node.length;
|
|
|
|
|
|
|
|
if (!foundStart && savedSel.start >= charIndex && savedSel.start <= nextCharIndex) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
range.setStart(node, savedSel.start - charIndex);
|
|
|
|
foundStart = true;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
if (foundStart && savedSel.end >= charIndex && savedSel.end <= nextCharIndex) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
range.setEnd(node, savedSel.end - charIndex);
|
|
|
|
stop = true;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
charIndex = nextCharIndex;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
} else {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var i = node.childNodes.length;
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
while (i--) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
nodeStack.push(node.childNodes[i]);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var sel = window.getSelection();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Removes all ranges from window selection
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.clearRange = function () {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var selection = window.getSelection();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
selection.removeAllRanges();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* sets or removes hightlight
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.hightlight = function (tool) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var dataType = tool.dataset.type;
|
|
|
|
|
|
|
|
if (document.queryCommandState(dataType)) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
|
|
|
editor.toolbar.inline.setButtonHighlighted(tool);
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
} else {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
|
|
|
editor.toolbar.inline.removeButtonsHighLight(tool);
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* hightlight for anchors
|
|
|
|
*/
|
|
|
|
var selection = window.getSelection(),
|
|
|
|
tag = selection.anchorNode.parentNode;
|
|
|
|
|
|
|
|
if (tag.tagName == 'A' && dataType == 'link') {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
|
|
|
editor.toolbar.inline.setButtonHighlighted(tool);
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Mark button if text is already executed
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.setButtonHighlighted = function (button) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
button.classList.add('hightlighted');
|
|
|
|
|
|
|
|
/** At link tool we also change icon */
|
|
|
|
if (button.dataset.type == 'link') {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var icon = button.childNodes[0];
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
icon.classList.remove('ce-icon-link');
|
|
|
|
icon.classList.add('ce-icon-unlink');
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Removes hightlight
|
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
inline.removeButtonsHighLight = function (button) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
button.classList.remove('hightlighted');
|
|
|
|
|
|
|
|
/** At link tool we also change icon */
|
|
|
|
if (button.dataset.type == 'link') {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
var icon = button.childNodes[0];
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
icon.classList.remove('ce-icon-unlink');
|
|
|
|
icon.classList.add('ce-icon-link');
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return inline;
|
|
|
|
|
2018-05-25 16:49:46 +02:00
|
|
|
})({});
|