2013-11-15 00:18:10 -05:00
|
|
|
/* global tinymce */
|
2014-07-15 20:10:17 -04:00
|
|
|
|
2012-09-23 20:13:18 -04:00
|
|
|
/**
|
|
|
|
* WordPress View plugin.
|
|
|
|
*/
|
2014-03-05 02:01:14 -05:00
|
|
|
tinymce.PluginManager.add( 'wpview', function( editor ) {
|
2015-04-04 19:00:26 -04:00
|
|
|
var $ = editor.$,
|
|
|
|
selected,
|
2014-07-03 23:59:15 -04:00
|
|
|
Env = tinymce.Env,
|
2014-03-05 02:01:14 -05:00
|
|
|
VK = tinymce.util.VK,
|
2012-10-11 23:28:22 -04:00
|
|
|
TreeWalker = tinymce.dom.TreeWalker,
|
2014-07-03 23:59:15 -04:00
|
|
|
toRemove = false,
|
2014-07-06 21:21:15 -04:00
|
|
|
firstFocus = true,
|
2014-07-16 13:55:16 -04:00
|
|
|
_noop = function() { return false; },
|
2014-08-17 16:11:15 -04:00
|
|
|
isios = /iPad|iPod|iPhone/.test( navigator.userAgent ),
|
2015-03-11 15:12:28 -04:00
|
|
|
cursorInterval,
|
|
|
|
lastKeyDownNode,
|
|
|
|
setViewCursorTries,
|
|
|
|
focus,
|
|
|
|
execCommandView,
|
|
|
|
execCommandBefore,
|
|
|
|
toolbar;
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
function getView( node ) {
|
2014-07-12 18:51:15 -04:00
|
|
|
return getParent( node, 'wpview-wrap' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the node or a parent of the node that has the passed className.
|
|
|
|
* Doing this directly is about 40% faster
|
|
|
|
*/
|
|
|
|
function getParent( node, className ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
while ( node && node.parentNode ) {
|
2014-07-15 20:10:17 -04:00
|
|
|
if ( node.className && ( ' ' + node.className + ' ' ).indexOf( ' ' + className + ' ' ) !== -1 ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function _stop( event ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
function setViewCursor( before, view ) {
|
|
|
|
var location = before ? 'before' : 'after',
|
|
|
|
offset = before ? 0 : 1;
|
2014-07-08 00:15:14 -04:00
|
|
|
deselect();
|
2014-07-03 23:59:15 -04:00
|
|
|
editor.selection.setCursorLocation( editor.dom.select( '.wpview-selection-' + location, view )[0], offset );
|
|
|
|
editor.nodeChanged();
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
function handleEnter( view, before, key ) {
|
2014-07-03 23:59:15 -04:00
|
|
|
var dom = editor.dom,
|
|
|
|
padNode = dom.create( 'p' );
|
|
|
|
|
2014-07-12 20:34:15 -04:00
|
|
|
if ( ! ( Env.ie && Env.ie < 11 ) ) {
|
|
|
|
padNode.innerHTML = '<br data-mce-bogus="1">';
|
|
|
|
}
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2014-07-12 20:34:15 -04:00
|
|
|
if ( before ) {
|
|
|
|
view.parentNode.insertBefore( padNode, view );
|
|
|
|
} else {
|
|
|
|
dom.insertAfter( padNode, view );
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
deselect();
|
2014-07-12 20:34:15 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
if ( before && key === VK.ENTER ) {
|
2014-07-12 20:34:15 -04:00
|
|
|
setViewCursor( before, view );
|
|
|
|
} else {
|
|
|
|
editor.selection.setCursorLocation( padNode, 0 );
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
editor.nodeChanged();
|
|
|
|
}
|
|
|
|
|
2014-07-19 03:30:15 -04:00
|
|
|
function removeView( view ) {
|
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
handleEnter( view );
|
2015-02-26 14:32:24 -05:00
|
|
|
wp.mce.views.remove( editor, view );
|
2014-07-19 03:30:15 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
function select( viewNode ) {
|
|
|
|
var clipboard,
|
|
|
|
dom = editor.dom;
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-12-01 19:26:22 -05:00
|
|
|
if ( ! viewNode ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
if ( viewNode !== selected ) {
|
|
|
|
// Make sure that the editor is focused.
|
|
|
|
// It is possible that the editor is not focused when the mouse event fires
|
|
|
|
// without focus, the selection will not work properly.
|
|
|
|
editor.getBody().focus();
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
deselect();
|
|
|
|
selected = viewNode;
|
|
|
|
dom.setAttrib( viewNode, 'data-mce-selected', 1 );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
clipboard = dom.create( 'div', {
|
|
|
|
'class': 'wpview-clipboard',
|
|
|
|
'contenteditable': 'true'
|
|
|
|
}, wp.mce.views.getText( viewNode ) );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
editor.dom.select( '.wpview-body', viewNode )[0].appendChild( clipboard );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
// Both of the following are necessary to prevent manipulating the selection/focus
|
|
|
|
dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
|
|
|
|
dom.bind( selected, 'beforedeactivate focusin focusout', _stop );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
// select the hidden div
|
|
|
|
if ( isios ) {
|
|
|
|
editor.selection.select( clipboard );
|
|
|
|
} else {
|
|
|
|
editor.selection.select( clipboard, true );
|
|
|
|
}
|
2014-08-10 00:22:15 -04:00
|
|
|
}
|
|
|
|
|
2014-06-06 00:43:17 -04:00
|
|
|
editor.nodeChanged();
|
2014-08-10 00:22:15 -04:00
|
|
|
editor.fire( 'wpview-selected', viewNode );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* Deselect a selected view and remove clipboard
|
|
|
|
*/
|
|
|
|
function deselect() {
|
|
|
|
var clipboard,
|
|
|
|
dom = editor.dom;
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( selected ) {
|
|
|
|
clipboard = editor.dom.select( '.wpview-clipboard', selected )[0];
|
|
|
|
dom.unbind( clipboard );
|
|
|
|
dom.remove( clipboard );
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
dom.unbind( selected, 'beforedeactivate focusin focusout click mouseup', _stop );
|
2014-06-05 22:12:14 -04:00
|
|
|
dom.setAttrib( selected, 'data-mce-selected', null );
|
2014-03-19 22:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
selected = null;
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// Check if the `wp.mce` API exists.
|
|
|
|
if ( typeof wp === 'undefined' || ! wp.mce ) {
|
2014-07-16 13:55:16 -04:00
|
|
|
return {
|
|
|
|
getView: _noop
|
|
|
|
};
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2015-06-01 14:09:26 -04:00
|
|
|
function resetViewsCallback( match, viewText ) {
|
|
|
|
return '<p>' + window.decodeURIComponent( viewText ) + '</p>';
|
|
|
|
}
|
2015-04-04 19:00:26 -04:00
|
|
|
|
2015-06-01 14:09:26 -04:00
|
|
|
// Replace the view tags with the view string
|
|
|
|
function resetViews( content ) {
|
|
|
|
return content.replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:[\s\S]+?wpview-selection-after[^>]+>[^<>]*<\/p>\s*|\.)<\/div>/g, resetViewsCallback )
|
|
|
|
.replace( /<p [^>]*?data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, resetViewsCallback );
|
2014-04-11 20:46:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent adding undo levels on changes inside a view wrapper
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'BeforeAddUndo', function( event ) {
|
2015-04-04 19:00:26 -04:00
|
|
|
if ( event.level.content ) {
|
2015-06-01 14:09:26 -04:00
|
|
|
event.level.content = resetViews( event.level.content );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
|
|
|
});
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// When the editor's content changes, scan the new content for
|
|
|
|
// matching view patterns, and transform the matches into
|
|
|
|
// view wrappers.
|
2014-04-11 20:46:14 -04:00
|
|
|
editor.on( 'BeforeSetContent', function( event ) {
|
2015-03-19 03:08:26 -04:00
|
|
|
var node;
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2015-04-04 19:00:26 -04:00
|
|
|
if ( ! event.selection ) {
|
|
|
|
wp.mce.views.unbind();
|
|
|
|
}
|
|
|
|
|
2015-03-19 03:08:26 -04:00
|
|
|
if ( ! event.content ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:34:28 -04:00
|
|
|
if ( ! event.load ) {
|
2015-04-18 16:48:27 -04:00
|
|
|
if ( selected ) {
|
|
|
|
removeView( selected );
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:34:28 -04:00
|
|
|
node = editor.selection.getNode();
|
|
|
|
|
|
|
|
if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) {
|
|
|
|
// When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh.
|
|
|
|
node = editor.dom.getParent( node, 'p' );
|
|
|
|
|
2015-04-20 16:57:28 -04:00
|
|
|
if ( node && /^[\s\uFEFF\u00A0]*$/.test( $( node ).text() || '' ) ) {
|
2015-03-20 16:34:28 -04:00
|
|
|
// Make sure there are no empty inline elements in the <p>
|
|
|
|
node.innerHTML = '';
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
event.content = wp.mce.views.setMarkers( event.content );
|
2014-06-11 22:49:16 -04:00
|
|
|
});
|
2014-05-10 19:36:18 -04:00
|
|
|
|
2015-03-19 03:08:26 -04:00
|
|
|
// When pasting strip all tags and check if the string is an URL.
|
|
|
|
// Then replace the pasted content with the cleaned URL.
|
|
|
|
editor.on( 'pastePreProcess', function( event ) {
|
|
|
|
var pastedStr = event.content;
|
|
|
|
|
|
|
|
if ( pastedStr ) {
|
|
|
|
pastedStr = tinymce.trim( pastedStr.replace( /<[^>]+>/g, '' ) );
|
|
|
|
|
|
|
|
if ( /^https?:\/\/\S+$/i.test( pastedStr ) ) {
|
|
|
|
event.content = pastedStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// When the editor's content has been updated and the DOM has been
|
|
|
|
// processed, render the views in the document.
|
2014-07-03 23:59:15 -04:00
|
|
|
editor.on( 'SetContent', function() {
|
2014-04-11 20:46:14 -04:00
|
|
|
wp.mce.views.render();
|
2014-03-05 02:01:14 -05:00
|
|
|
});
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
// Set the cursor before or after a view when clicking next to it.
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'click', function( event ) {
|
2014-07-03 23:59:15 -04:00
|
|
|
var x = event.clientX,
|
|
|
|
y = event.clientY,
|
|
|
|
body = editor.getBody(),
|
|
|
|
bodyRect = body.getBoundingClientRect(),
|
|
|
|
first = body.firstChild,
|
|
|
|
last = body.lastChild,
|
2015-03-25 14:55:29 -04:00
|
|
|
firstRect, lastRect, view;
|
|
|
|
|
|
|
|
if ( ! first || ! last ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
firstRect = first.getBoundingClientRect();
|
|
|
|
lastRect = last.getBoundingClientRect();
|
2014-07-03 23:59:15 -04:00
|
|
|
|
|
|
|
if ( y < firstRect.top && ( view = getView( first ) ) ) {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( y > lastRect.bottom && ( view = getView( last ) ) ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
event.preventDefault();
|
2015-01-11 15:42:23 -05:00
|
|
|
} else if ( x < bodyRect.left || x > bodyRect.right ) {
|
2014-07-03 23:59:15 -04:00
|
|
|
tinymce.each( editor.dom.select( '.wpview-wrap' ), function( view ) {
|
|
|
|
var rect = view.getBoundingClientRect();
|
|
|
|
|
2015-01-11 15:42:23 -05:00
|
|
|
if ( y < rect.top ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
if ( y >= rect.top && y <= rect.bottom ) {
|
|
|
|
if ( x < bodyRect.left ) {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( x > bodyRect.right ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-01-11 15:42:23 -05:00
|
|
|
|
|
|
|
return false;
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
|
|
|
});
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'init', function() {
|
2014-08-10 00:22:15 -04:00
|
|
|
var scrolled = false,
|
2014-08-26 00:46:15 -04:00
|
|
|
selection = editor.selection,
|
|
|
|
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// When a view is selected, ensure content that is being pasted
|
|
|
|
// or inserted is added to a text node (instead of the view).
|
|
|
|
editor.on( 'BeforeSetContent', function() {
|
|
|
|
var walker, target,
|
2014-07-03 23:59:15 -04:00
|
|
|
view = getView( selection.getNode() );
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
// If the selection is not within a view, bail.
|
|
|
|
if ( ! view ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
if ( ! view.nextSibling || getView( view.nextSibling ) ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
// If there are no additional nodes or the next node is a
|
|
|
|
// view, create a text node after the current view.
|
|
|
|
target = editor.getDoc().createTextNode('');
|
|
|
|
editor.dom.insertAfter( target, view );
|
|
|
|
} else {
|
|
|
|
// Otherwise, find the next text node.
|
|
|
|
walker = new TreeWalker( view.nextSibling, view.nextSibling );
|
|
|
|
target = walker.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the `target` text node.
|
|
|
|
selection.select( target );
|
|
|
|
selection.collapse( true );
|
|
|
|
});
|
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
editor.dom.bind( editor.getDoc(), 'touchmove', function() {
|
|
|
|
scrolled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'mousedown mouseup click touchend', function( event ) {
|
2014-08-17 16:11:15 -04:00
|
|
|
var view = getView( event.target );
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-07-08 00:15:14 -04:00
|
|
|
firstFocus = false;
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// Contain clicks inside the view wrapper
|
|
|
|
if ( view ) {
|
2014-08-10 00:22:15 -04:00
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
if ( event.type === 'touchend' && scrolled ) {
|
|
|
|
scrolled = false;
|
|
|
|
} else {
|
|
|
|
select( view );
|
|
|
|
}
|
2014-07-24 20:56:15 -04:00
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
// Returning false stops the ugly bars from appearing in IE11 and stops the view being selected as a range in FF.
|
|
|
|
// Unfortunately, it also inhibits the dragging of views to a new location.
|
2014-03-05 02:01:14 -05:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-08-17 16:11:15 -04:00
|
|
|
if ( event.type === 'touchend' || event.type === 'mousedown' ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
deselect();
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2014-08-10 00:22:15 -04:00
|
|
|
|
|
|
|
if ( event.type === 'touchend' && scrolled ) {
|
|
|
|
scrolled = false;
|
|
|
|
}
|
|
|
|
}, true );
|
2014-08-26 00:46:15 -04:00
|
|
|
|
|
|
|
if ( MutationObserver ) {
|
|
|
|
new MutationObserver( function() {
|
|
|
|
editor.fire( 'wp-body-class-change' );
|
|
|
|
} )
|
|
|
|
.observe( editor.getBody(), {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['class']
|
|
|
|
} );
|
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
});
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2015-06-01 14:09:26 -04:00
|
|
|
// Empty the wpview wrap and marker nodes
|
|
|
|
function emptyViewNodes( rootNode ) {
|
|
|
|
$( 'div[data-wpview-text], p[data-wpview-marker]', rootNode ).each( function( i, node ) {
|
|
|
|
node.innerHTML = '.';
|
2014-03-05 02:01:14 -05:00
|
|
|
});
|
2015-04-04 19:00:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 14:09:26 -04:00
|
|
|
// Run that before the DOM cleanup
|
2015-04-04 19:00:26 -04:00
|
|
|
editor.on( 'PreProcess', function( event ) {
|
2015-06-01 14:09:26 -04:00
|
|
|
emptyViewNodes( event.node );
|
2015-04-04 19:00:26 -04:00
|
|
|
}, true );
|
|
|
|
|
|
|
|
editor.on( 'hide', function() {
|
|
|
|
wp.mce.views.unbind();
|
2015-04-21 17:32:26 -04:00
|
|
|
deselect();
|
2015-06-01 14:09:26 -04:00
|
|
|
emptyViewNodes();
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'PostProcess', function( event ) {
|
|
|
|
if ( event.content ) {
|
|
|
|
event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]+)"[^>]*>[\s\S]*?<\/div>/g, resetViewsCallback )
|
|
|
|
.replace( /<p [^>]*?data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, resetViewsCallback );
|
|
|
|
}
|
2014-04-22 14:21:16 -04:00
|
|
|
});
|
|
|
|
|
2014-08-18 23:27:17 -04:00
|
|
|
// Excludes arrow keys, delete, backspace, enter, space bar.
|
2014-08-25 16:41:16 -04:00
|
|
|
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode
|
2014-08-18 23:27:17 -04:00
|
|
|
function isSpecialKey( key ) {
|
|
|
|
return ( ( key <= 47 && key !== VK.SPACEBAR && key !== VK.ENTER && key !== VK.DELETE && key !== VK.BACKSPACE && ( key < 37 || key > 40 ) ) ||
|
2014-09-25 20:21:16 -04:00
|
|
|
key >= 224 || // OEM or non-printable
|
2014-08-25 16:41:16 -04:00
|
|
|
( key >= 144 && key <= 150 ) || // Num Lock, Scroll Lock, OEM
|
2014-08-18 23:27:17 -04:00
|
|
|
( key >= 91 && key <= 93 ) || // Windows keys
|
2014-08-25 16:41:16 -04:00
|
|
|
( key >= 112 && key <= 135 ) ); // F keys
|
2014-08-18 23:27:17 -04:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
// (De)select views when arrow keys are used to navigate the content of the editor.
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'keydown', function( event ) {
|
2014-07-18 20:34:15 -04:00
|
|
|
var key = event.keyCode,
|
2014-07-03 23:59:15 -04:00
|
|
|
dom = editor.dom,
|
|
|
|
selection = editor.selection,
|
2014-07-18 20:34:15 -04:00
|
|
|
node, view, cursorBefore, cursorAfter,
|
2014-07-19 03:30:15 -04:00
|
|
|
range, clonedRange, tempRange;
|
2014-07-18 20:34:15 -04:00
|
|
|
|
|
|
|
if ( selected ) {
|
2014-07-19 03:30:15 -04:00
|
|
|
// Ignore key presses that involve the command or control key, but continue when in combination with backspace or v.
|
|
|
|
// Also ignore the F# keys.
|
|
|
|
if ( ( ( event.metaKey || event.ctrlKey ) && key !== VK.BACKSPACE && key !== 86 ) || ( key >= 112 && key <= 123 ) ) {
|
|
|
|
// Remove the view when pressing cmd/ctrl+x on keyup, otherwise the browser can't copy the content.
|
|
|
|
if ( ( event.metaKey || event.ctrlKey ) && key === 88 ) {
|
|
|
|
toRemove = selected;
|
2014-07-18 20:34:15 -04:00
|
|
|
}
|
|
|
|
return;
|
2014-07-06 21:21:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
view = getView( selection.getNode() );
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
// If the caret is not within the selected view, deselect the view and bail.
|
|
|
|
if ( view !== selected ) {
|
|
|
|
deselect();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
if ( key === VK.LEFT ) {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( key === VK.UP ) {
|
|
|
|
if ( view.previousSibling ) {
|
|
|
|
if ( getView( view.previousSibling ) ) {
|
|
|
|
setViewCursor( true, view.previousSibling );
|
2014-07-03 23:59:15 -04:00
|
|
|
} else {
|
2014-07-18 20:34:15 -04:00
|
|
|
deselect();
|
2014-07-03 23:59:15 -04:00
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-18 20:34:15 -04:00
|
|
|
setViewCursor( true, view );
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
2014-07-18 20:34:15 -04:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( key === VK.RIGHT ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( key === VK.DOWN ) {
|
|
|
|
if ( view.nextSibling ) {
|
|
|
|
if ( getView( view.nextSibling ) ) {
|
|
|
|
setViewCursor( false, view.nextSibling );
|
|
|
|
} else {
|
|
|
|
deselect();
|
|
|
|
selection.setCursorLocation( view.nextSibling, 0 );
|
|
|
|
}
|
2014-07-03 23:59:15 -04:00
|
|
|
} else {
|
2014-07-18 20:34:15 -04:00
|
|
|
setViewCursor( false, view );
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
2014-08-18 23:27:17 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
event.preventDefault();
|
|
|
|
// Ignore keys that don't insert anything.
|
2014-08-18 23:27:17 -04:00
|
|
|
} else if ( ! isSpecialKey( key ) ) {
|
2014-07-19 03:30:15 -04:00
|
|
|
removeView( selected );
|
2014-07-18 20:34:15 -04:00
|
|
|
|
|
|
|
if ( key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) {
|
|
|
|
event.preventDefault();
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 20:34:15 -04:00
|
|
|
} else {
|
|
|
|
if ( event.metaKey || event.ctrlKey || ( key >= 112 && key <= 123 ) ) {
|
|
|
|
return;
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
node = selection.getNode();
|
|
|
|
lastKeyDownNode = node;
|
|
|
|
view = getView( node );
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
// Make sure we don't delete part of a view.
|
|
|
|
// If the range ends or starts with the view, we'll need to trim it.
|
|
|
|
if ( ! selection.isCollapsed() ) {
|
|
|
|
range = selection.getRng();
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
if ( view = getView( range.endContainer ) ) {
|
|
|
|
clonedRange = range.cloneRange();
|
2014-07-06 21:21:15 -04:00
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
2014-07-18 20:34:15 -04:00
|
|
|
tempRange = selection.getRng();
|
|
|
|
clonedRange.setEnd( tempRange.endContainer, tempRange.endOffset );
|
|
|
|
selection.setRng( clonedRange );
|
|
|
|
} else if ( view = getView( range.startContainer ) ) {
|
|
|
|
clonedRange = range.cloneRange();
|
|
|
|
clonedRange.setStart( view.nextSibling, 0 );
|
|
|
|
selection.setRng( clonedRange );
|
2014-07-06 21:21:15 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-08 18:56:14 -04:00
|
|
|
|
2014-07-19 03:30:15 -04:00
|
|
|
if ( ! view ) {
|
|
|
|
// Make sure we don't eat any content.
|
|
|
|
if ( event.keyCode === VK.BACKSPACE ) {
|
|
|
|
if ( editor.dom.isEmpty( node ) ) {
|
|
|
|
if ( view = getView( node.previousSibling ) ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
editor.dom.remove( node );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} else if ( ( range = selection.getRng() ) &&
|
|
|
|
range.startOffset === 0 &&
|
|
|
|
range.endOffset === 0 &&
|
|
|
|
( view = getView( node.previousSibling ) ) ) {
|
2014-07-18 20:34:15 -04:00
|
|
|
setViewCursor( false, view );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2014-07-06 21:21:15 -04:00
|
|
|
}
|
2014-07-18 20:34:15 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-19 22:48:14 -04:00
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
if ( ! ( ( cursorBefore = dom.hasClass( view, 'wpview-selection-before' ) ) ||
|
|
|
|
( cursorAfter = dom.hasClass( view, 'wpview-selection-after' ) ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-19 22:48:14 -04:00
|
|
|
|
2014-08-18 23:27:17 -04:00
|
|
|
if ( isSpecialKey( key ) ) {
|
|
|
|
// ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
if ( ( cursorAfter && key === VK.UP ) || ( cursorBefore && key === VK.BACKSPACE ) ) {
|
|
|
|
if ( view.previousSibling ) {
|
|
|
|
if ( getView( view.previousSibling ) ) {
|
|
|
|
setViewCursor( false, view.previousSibling );
|
|
|
|
} else {
|
|
|
|
if ( dom.isEmpty( view.previousSibling ) && key === VK.BACKSPACE ) {
|
|
|
|
dom.remove( view.previousSibling );
|
|
|
|
} else {
|
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorAfter && ( key === VK.DOWN || key === VK.RIGHT ) ) {
|
|
|
|
if ( view.nextSibling ) {
|
|
|
|
if ( getView( view.nextSibling ) ) {
|
|
|
|
setViewCursor( key === VK.RIGHT, view.nextSibling );
|
|
|
|
} else {
|
|
|
|
selection.setCursorLocation( view.nextSibling, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorBefore && ( key === VK.UP || key === VK.LEFT ) ) {
|
|
|
|
if ( view.previousSibling ) {
|
|
|
|
if ( getView( view.previousSibling ) ) {
|
|
|
|
setViewCursor( key === VK.UP, view.previousSibling );
|
|
|
|
} else {
|
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorBefore && key === VK.DOWN ) {
|
|
|
|
if ( view.nextSibling ) {
|
|
|
|
if ( getView( view.nextSibling ) ) {
|
|
|
|
setViewCursor( true, view.nextSibling );
|
|
|
|
} else {
|
|
|
|
selection.setCursorLocation( view.nextSibling, 0 );
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-03 23:59:15 -04:00
|
|
|
setViewCursor( false, view );
|
2014-03-19 22:48:14 -04:00
|
|
|
}
|
2014-07-18 20:34:15 -04:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( ( cursorAfter && key === VK.LEFT ) || ( cursorBefore && key === VK.RIGHT ) ) {
|
|
|
|
select( view );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorAfter && key === VK.BACKSPACE ) {
|
2014-07-19 03:30:15 -04:00
|
|
|
removeView( view );
|
2014-07-18 20:34:15 -04:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorAfter ) {
|
|
|
|
handleEnter( view );
|
|
|
|
} else if ( cursorBefore ) {
|
|
|
|
handleEnter( view , true, key );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( key === VK.ENTER ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
editor.on( 'keyup', function() {
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( toRemove ) {
|
2014-07-19 03:30:15 -04:00
|
|
|
removeView( toRemove );
|
2014-03-05 02:01:14 -05:00
|
|
|
toRemove = false;
|
|
|
|
}
|
2014-07-03 23:59:15 -04:00
|
|
|
});
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-07-06 21:21:15 -04:00
|
|
|
editor.on( 'focus', function() {
|
|
|
|
var view;
|
2014-07-08 18:56:14 -04:00
|
|
|
|
2014-07-06 21:21:15 -04:00
|
|
|
focus = true;
|
|
|
|
editor.dom.addClass( editor.getBody(), 'has-focus' );
|
|
|
|
|
|
|
|
// Edge case: show the fake caret when the editor is focused for the first time
|
|
|
|
// and the first element is a view.
|
|
|
|
if ( firstFocus && ( view = getView( editor.getBody().firstChild ) ) ) {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
}
|
|
|
|
|
|
|
|
firstFocus = false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.on( 'blur', function() {
|
|
|
|
focus = false;
|
|
|
|
editor.dom.removeClass( editor.getBody(), 'has-focus' );
|
|
|
|
} );
|
|
|
|
|
2014-07-15 20:10:17 -04:00
|
|
|
editor.on( 'NodeChange', function( event ) {
|
2014-07-03 23:59:15 -04:00
|
|
|
var dom = editor.dom,
|
|
|
|
views = editor.dom.select( '.wpview-wrap' ),
|
|
|
|
className = event.element.className,
|
|
|
|
view = getView( event.element ),
|
|
|
|
lKDN = lastKeyDownNode;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-07-03 23:59:15 -04:00
|
|
|
lastKeyDownNode = false;
|
|
|
|
|
|
|
|
clearInterval( cursorInterval );
|
|
|
|
|
2014-07-18 20:34:15 -04:00
|
|
|
// This runs a lot and is faster than replacing each class separately
|
|
|
|
tinymce.each( views, function ( view ) {
|
|
|
|
if ( view.className ) {
|
|
|
|
view.className = view.className.replace( / ?\bwpview-(?:selection-before|selection-after|cursor-hide)\b/g, '' );
|
|
|
|
}
|
|
|
|
});
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
if ( focus && view ) {
|
|
|
|
if ( ( className === 'wpview-selection-before' || className === 'wpview-selection-after' ) &&
|
|
|
|
editor.selection.isCollapsed() ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
setViewCursorTries = 0;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
deselect();
|
2014-07-08 00:15:14 -04:00
|
|
|
|
2014-08-10 00:22:15 -04:00
|
|
|
// Make sure the cursor arrived in the right node.
|
|
|
|
// This is necessary for Firefox.
|
|
|
|
if ( lKDN === view.previousSibling ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
setViewCursor( true, view );
|
2014-08-10 00:22:15 -04:00
|
|
|
return;
|
|
|
|
} else if ( lKDN === view.nextSibling ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
return;
|
2014-07-08 00:15:14 -04:00
|
|
|
}
|
2014-08-10 00:22:15 -04:00
|
|
|
|
|
|
|
dom.addClass( view, className );
|
|
|
|
|
|
|
|
cursorInterval = setInterval( function() {
|
|
|
|
if ( dom.hasClass( view, 'wpview-cursor-hide' ) ) {
|
|
|
|
dom.removeClass( view, 'wpview-cursor-hide' );
|
|
|
|
} else {
|
|
|
|
dom.addClass( view, 'wpview-cursor-hide' );
|
|
|
|
}
|
|
|
|
}, 500 );
|
|
|
|
// If the cursor lands anywhere else in the view, set the cursor before it.
|
|
|
|
// Only try this once to prevent a loop. (You never know.)
|
|
|
|
} else if ( ! getParent( event.element, 'wpview-clipboard' ) && ! setViewCursorTries ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
deselect();
|
2014-08-10 00:22:15 -04:00
|
|
|
setViewCursorTries++;
|
|
|
|
setViewCursor( true, view );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-15 22:19:15 -04:00
|
|
|
editor.on( 'BeforeExecCommand', function() {
|
|
|
|
var node = editor.selection.getNode(),
|
2014-07-15 20:10:17 -04:00
|
|
|
view;
|
|
|
|
|
2014-09-25 20:21:16 -04:00
|
|
|
if ( node && ( ( execCommandBefore = node.className === 'wpview-selection-before' ) || node.className === 'wpview-selection-after' ) && ( view = getView( node ) ) ) {
|
|
|
|
handleEnter( view, execCommandBefore );
|
2014-07-15 22:19:15 -04:00
|
|
|
execCommandView = view;
|
2014-07-15 20:10:17 -04:00
|
|
|
}
|
2014-07-15 22:19:15 -04:00
|
|
|
});
|
2014-07-15 20:10:17 -04:00
|
|
|
|
2014-07-15 22:19:15 -04:00
|
|
|
editor.on( 'ExecCommand', function() {
|
|
|
|
var toSelect, node;
|
|
|
|
|
|
|
|
if ( selected ) {
|
|
|
|
toSelect = selected;
|
|
|
|
deselect();
|
|
|
|
select( toSelect );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( execCommandView ) {
|
2014-09-25 20:21:16 -04:00
|
|
|
node = execCommandView[ execCommandBefore ? 'previousSibling' : 'nextSibling' ];
|
2014-07-15 22:19:15 -04:00
|
|
|
|
|
|
|
if ( node && node.nodeName === 'P' && editor.dom.isEmpty( node ) ) {
|
|
|
|
editor.dom.remove( node );
|
2014-09-25 20:21:16 -04:00
|
|
|
setViewCursor( execCommandBefore, execCommandView );
|
2014-07-15 22:19:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
execCommandView = false;
|
2014-07-15 20:10:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'ResolveName', function( event ) {
|
2014-07-08 00:15:14 -04:00
|
|
|
if ( editor.dom.hasClass( event.target, 'wpview-wrap' ) ) {
|
|
|
|
event.name = editor.dom.getAttrib( event.target, 'data-wpview-type' ) || 'wpview';
|
|
|
|
event.stopPropagation();
|
|
|
|
} else if ( getView( event.target ) ) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
editor.addButton( 'wp_view_edit', {
|
|
|
|
tooltip: 'Edit ', // trailing space is needed, used for context
|
|
|
|
icon: 'dashicon dashicons-edit',
|
|
|
|
onclick: function() {
|
|
|
|
selected && wp.mce.views.edit( editor, selected );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.addButton( 'wp_view_remove', {
|
|
|
|
tooltip: 'Remove',
|
|
|
|
icon: 'dashicon dashicons-no',
|
|
|
|
onclick: function() {
|
|
|
|
selected && removeView( selected );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-03-11 18:41:26 -04:00
|
|
|
editor.once( 'preinit', function() {
|
2015-08-25 00:38:20 -04:00
|
|
|
if ( editor.wp && editor.wp._createToolbar ) {
|
|
|
|
toolbar = editor.wp._createToolbar( [
|
|
|
|
'wp_view_edit',
|
|
|
|
'wp_view_remove'
|
|
|
|
] );
|
|
|
|
}
|
2015-03-11 18:41:26 -04:00
|
|
|
} );
|
|
|
|
|
2015-03-11 15:12:28 -04:00
|
|
|
editor.on( 'wptoolbar', function( event ) {
|
|
|
|
if ( selected ) {
|
|
|
|
event.element = selected;
|
|
|
|
event.toolbar = toolbar;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Add to editor.wp
|
|
|
|
editor.wp = editor.wp || {};
|
|
|
|
editor.wp.getView = getView;
|
2015-07-29 09:49:24 -04:00
|
|
|
editor.wp.setViewCursor = setViewCursor;
|
2015-03-11 15:12:28 -04:00
|
|
|
|
|
|
|
// Keep for back-compat.
|
2014-03-05 02:01:14 -05:00
|
|
|
return {
|
2014-07-09 18:06:15 -04:00
|
|
|
getView: getView
|
2014-03-05 02:01:14 -05:00
|
|
|
};
|
|
|
|
});
|