2013-11-15 06:18:10 +01:00
|
|
|
/* global tinymce */
|
2014-07-16 02:10:17 +02:00
|
|
|
|
2012-09-24 02:13:18 +02:00
|
|
|
/**
|
|
|
|
* WordPress View plugin.
|
|
|
|
*/
|
2014-03-05 08:01:14 +01:00
|
|
|
tinymce.PluginManager.add( 'wpview', function( editor ) {
|
|
|
|
var selected,
|
2014-07-04 05:59:15 +02:00
|
|
|
Env = tinymce.Env,
|
2014-03-05 08:01:14 +01:00
|
|
|
VK = tinymce.util.VK,
|
2012-10-12 05:28:22 +02:00
|
|
|
TreeWalker = tinymce.dom.TreeWalker,
|
2014-07-04 05:59:15 +02:00
|
|
|
toRemove = false,
|
2014-07-07 03:21:15 +02:00
|
|
|
firstFocus = true,
|
2014-07-16 19:55:16 +02:00
|
|
|
_noop = function() { return false; },
|
2014-07-16 04:19:15 +02:00
|
|
|
cursorInterval, lastKeyDownNode, setViewCursorTries, focus, execCommandView;
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
function getView( node ) {
|
2014-07-13 00:51:15 +02: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 06:15:14 +02:00
|
|
|
while ( node && node.parentNode ) {
|
2014-07-16 02:10:17 +02:00
|
|
|
if ( node.className && ( ' ' + node.className + ' ' ).indexOf( ' ' + className + ' ' ) !== -1 ) {
|
2014-07-08 06:15:14 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text/shortcode string for a view.
|
|
|
|
*
|
2014-07-04 05:59:15 +02:00
|
|
|
* @param view The view wrapper's node
|
2014-03-05 08:01:14 +01:00
|
|
|
* @returns string The text/shoercode string of the view
|
|
|
|
*/
|
|
|
|
function getViewText( view ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
if ( view = getView( view ) ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
return window.decodeURIComponent( editor.dom.getAttrib( view, 'data-wpview-text' ) || '' );
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the view's original text/shortcode string
|
|
|
|
*
|
|
|
|
* @param view The view wrapper's HTML id or node
|
|
|
|
* @param text The text string to be set
|
|
|
|
*/
|
|
|
|
function setViewText( view, text ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
view = getView( view );
|
2014-03-05 08:01:14 +01:00
|
|
|
|
|
|
|
if ( view ) {
|
|
|
|
editor.dom.setAttrib( view, 'data-wpview-text', window.encodeURIComponent( text || '' ) );
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
function _stop( event ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
function setViewCursor( before, view ) {
|
|
|
|
var location = before ? 'before' : 'after',
|
|
|
|
offset = before ? 0 : 1;
|
2014-07-08 06:15:14 +02:00
|
|
|
deselect();
|
2014-07-04 05:59:15 +02:00
|
|
|
editor.selection.setCursorLocation( editor.dom.select( '.wpview-selection-' + location, view )[0], offset );
|
|
|
|
editor.nodeChanged();
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
function handleEnter( view, before, key ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
var dom = editor.dom,
|
|
|
|
padNode = dom.create( 'p' );
|
|
|
|
|
2014-07-13 02:34:15 +02:00
|
|
|
if ( ! ( Env.ie && Env.ie < 11 ) ) {
|
|
|
|
padNode.innerHTML = '<br data-mce-bogus="1">';
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-07-13 02:34:15 +02:00
|
|
|
if ( before ) {
|
|
|
|
view.parentNode.insertBefore( padNode, view );
|
|
|
|
} else {
|
|
|
|
dom.insertAfter( padNode, view );
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deselect();
|
2014-07-13 02:34:15 +02:00
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
if ( before && key === VK.ENTER ) {
|
2014-07-13 02:34:15 +02:00
|
|
|
setViewCursor( before, view );
|
|
|
|
} else {
|
|
|
|
editor.selection.setCursorLocation( padNode, 0 );
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
editor.nodeChanged();
|
|
|
|
}
|
|
|
|
|
2014-07-19 09:30:15 +02:00
|
|
|
function removeView( view ) {
|
|
|
|
// TODO: trigger an event to run a clean up function.
|
|
|
|
// Maybe `jQuery( view ).trigger( 'remove' );`?
|
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
handleEnter( view );
|
|
|
|
editor.dom.remove( view );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
function select( viewNode ) {
|
|
|
|
var clipboard,
|
|
|
|
dom = editor.dom;
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// Bail if node is already selected.
|
|
|
|
if ( viewNode === selected ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-07-22 23:53:16 +02:00
|
|
|
// 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();
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
deselect();
|
|
|
|
selected = viewNode;
|
2014-06-06 04:12:14 +02:00
|
|
|
dom.setAttrib( viewNode, 'data-mce-selected', 1 );
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
clipboard = dom.create( 'div', {
|
|
|
|
'class': 'wpview-clipboard',
|
|
|
|
'contenteditable': 'true'
|
|
|
|
}, getViewText( viewNode ) );
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
editor.dom.select( '.wpview-body', viewNode )[0].appendChild( clipboard );
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// Both of the following are necessary to prevent manipulating the selection/focus
|
2014-04-12 02:46:14 +02:00
|
|
|
dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
|
|
|
|
dom.bind( selected, 'beforedeactivate focusin focusout', _stop );
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// select the hidden div
|
|
|
|
editor.selection.select( clipboard, true );
|
2014-06-06 06:43:17 +02:00
|
|
|
editor.nodeChanged();
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
/**
|
|
|
|
* Deselect a selected view and remove clipboard
|
|
|
|
*/
|
|
|
|
function deselect() {
|
|
|
|
var clipboard,
|
|
|
|
dom = editor.dom;
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( selected ) {
|
|
|
|
clipboard = editor.dom.select( '.wpview-clipboard', selected )[0];
|
|
|
|
dom.unbind( clipboard );
|
|
|
|
dom.remove( clipboard );
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
dom.unbind( selected, 'beforedeactivate focusin focusout click mouseup', _stop );
|
2014-06-06 04:12:14 +02:00
|
|
|
dom.setAttrib( selected, 'data-mce-selected', null );
|
2014-03-20 03:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
selected = null;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// Check if the `wp.mce` API exists.
|
|
|
|
if ( typeof wp === 'undefined' || ! wp.mce ) {
|
2014-07-16 19:55:16 +02:00
|
|
|
return {
|
|
|
|
getViewText: _noop,
|
|
|
|
setViewText: _noop,
|
|
|
|
getView: _noop
|
|
|
|
};
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-04-12 02:46:14 +02:00
|
|
|
// Remove the content of view wrappers from HTML string
|
|
|
|
function emptyViews( content ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
return content.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?: |\u00a0)*<\/p><\/div>/g, '$1' );
|
2014-04-12 02:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent adding undo levels on changes inside a view wrapper
|
2014-03-05 08:01:14 +01:00
|
|
|
editor.on( 'BeforeAddUndo', function( event ) {
|
2014-04-12 02:46:14 +02:00
|
|
|
if ( event.lastLevel && emptyViews( event.level.content ) === emptyViews( event.lastLevel.content ) ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// When the editor's content changes, scan the new content for
|
|
|
|
// matching view patterns, and transform the matches into
|
|
|
|
// view wrappers.
|
2014-04-12 02:46:14 +02:00
|
|
|
editor.on( 'BeforeSetContent', function( event ) {
|
2014-06-12 04:49:16 +02:00
|
|
|
var node;
|
|
|
|
|
2014-04-12 02:46:14 +02:00
|
|
|
if ( ! event.content ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-19 09:30:15 +02:00
|
|
|
if ( selected ) {
|
|
|
|
removeView( selected );
|
|
|
|
}
|
|
|
|
|
2014-04-12 02:46:14 +02:00
|
|
|
if ( ! event.initial ) {
|
|
|
|
wp.mce.views.unbind( editor );
|
|
|
|
}
|
|
|
|
|
2014-06-12 04:49:16 +02:00
|
|
|
node = editor.selection.getNode();
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-06-12 04:49:16 +02:00
|
|
|
// When a url is pasted, only try to embed it when pasted in an empty paragrapgh.
|
|
|
|
if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/i ) &&
|
|
|
|
( node.nodeName !== 'P' || node.parentNode !== editor.getBody() || ! editor.dom.isEmpty( node ) ) ) {
|
|
|
|
return;
|
2014-05-11 01:36:18 +02:00
|
|
|
}
|
2014-06-12 04:49:16 +02:00
|
|
|
|
|
|
|
event.content = wp.mce.views.toViews( event.content );
|
|
|
|
});
|
2014-05-11 01:36:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// When the editor's content has been updated and the DOM has been
|
|
|
|
// processed, render the views in the document.
|
2014-07-04 05:59:15 +02:00
|
|
|
editor.on( 'SetContent', function() {
|
2014-04-12 02:46:14 +02:00
|
|
|
wp.mce.views.render();
|
2014-03-05 08:01:14 +01:00
|
|
|
});
|
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
// Set the cursor before or after a view when clicking next to it.
|
2014-03-05 08:01:14 +01:00
|
|
|
editor.on( 'click', function( event ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
var x = event.clientX,
|
|
|
|
y = event.clientY,
|
|
|
|
body = editor.getBody(),
|
|
|
|
bodyRect = body.getBoundingClientRect(),
|
|
|
|
first = body.firstChild,
|
|
|
|
firstRect = first.getBoundingClientRect(),
|
|
|
|
last = body.lastChild,
|
|
|
|
lastRect = last.getBoundingClientRect(),
|
|
|
|
view;
|
|
|
|
|
|
|
|
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();
|
|
|
|
} else {
|
|
|
|
tinymce.each( editor.dom.select( '.wpview-wrap' ), function( view ) {
|
|
|
|
var rect = view.getBoundingClientRect();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'init', function() {
|
|
|
|
var selection = editor.selection;
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-03-05 08:01:14 +01: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-04 05:59:15 +02:00
|
|
|
view = getView( selection.getNode() );
|
2014-03-05 08:01:14 +01:00
|
|
|
|
|
|
|
// If the selection is not within a view, bail.
|
|
|
|
if ( ! view ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
if ( ! view.nextSibling || getView( view.nextSibling ) ) {
|
2014-03-05 08:01:14 +01: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-06-06 07:04:14 +02:00
|
|
|
editor.dom.bind( editor.getBody().parentNode, 'mousedown mouseup click', function( event ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
var view = getView( event.target ),
|
2014-04-09 02:58:15 +02:00
|
|
|
deselectEventType;
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-07-08 06:15:14 +02:00
|
|
|
firstFocus = false;
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
// Contain clicks inside the view wrapper
|
|
|
|
if ( view ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2014-04-09 02:58:15 +02:00
|
|
|
// Hack to try and keep the block resize handles from appearing. They will show on mousedown and then be removed on mouseup.
|
2014-07-04 05:59:15 +02:00
|
|
|
if ( Env.ie <= 10 ) {
|
2014-04-09 02:58:15 +02:00
|
|
|
deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
select( view );
|
|
|
|
|
2014-03-20 03:48:14 +01:00
|
|
|
if ( event.type === 'click' && ! event.metaKey && ! event.ctrlKey ) {
|
|
|
|
if ( editor.dom.hasClass( event.target, 'edit' ) ) {
|
|
|
|
wp.mce.views.edit( view );
|
|
|
|
} else if ( editor.dom.hasClass( event.target, 'remove' ) ) {
|
2014-07-19 09:30:15 +02:00
|
|
|
removeView( view );
|
2012-10-12 05:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-03-20 03:48:14 +01: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 08:01:14 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-04-09 02:58:15 +02:00
|
|
|
// Fix issue with deselecting a view in IE8. Without this hack, clicking content above the view wouldn't actually deselect it
|
|
|
|
// and the caret wouldn't be placed at the mouse location
|
2014-07-04 05:59:15 +02:00
|
|
|
if ( Env.ie && Env.ie <= 8 ) {
|
2014-04-09 02:58:15 +02:00
|
|
|
deselectEventType = 'mouseup';
|
|
|
|
} else {
|
|
|
|
deselectEventType = 'mousedown';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event.type === deselectEventType ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
deselect();
|
2013-12-30 02:54:11 +01:00
|
|
|
}
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
editor.on( 'PreProcess', function( event ) {
|
2014-07-16 01:39:15 +02:00
|
|
|
// Empty the wpview wrap nodes
|
2014-07-04 05:59:15 +02:00
|
|
|
tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
|
2014-07-16 01:39:15 +02:00
|
|
|
node.textContent = node.innerText = '\u00a0';
|
2014-03-05 08:01:14 +01:00
|
|
|
});
|
|
|
|
});
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-04-22 20:21:16 +02:00
|
|
|
editor.on( 'PostProcess', function( event ) {
|
|
|
|
if ( event.content ) {
|
|
|
|
event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]*)"[^>]*>[\s\S]*?<\/div>/g, function( match, shortcode ) {
|
|
|
|
if ( shortcode ) {
|
|
|
|
return '<p>' + window.decodeURIComponent( shortcode ) + '</p>';
|
|
|
|
}
|
|
|
|
return ''; // If error, remove the view wrapper
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
// (De)select views when arrow keys are used to navigate the content of the editor.
|
2014-03-05 08:01:14 +01:00
|
|
|
editor.on( 'keydown', function( event ) {
|
2014-07-19 02:34:15 +02:00
|
|
|
var key = event.keyCode,
|
2014-07-04 05:59:15 +02:00
|
|
|
dom = editor.dom,
|
|
|
|
selection = editor.selection,
|
2014-07-19 02:34:15 +02:00
|
|
|
node, view, cursorBefore, cursorAfter,
|
2014-07-19 09:30:15 +02:00
|
|
|
range, clonedRange, tempRange;
|
2014-07-19 02:34:15 +02:00
|
|
|
|
|
|
|
if ( selected ) {
|
2014-07-19 09:30:15 +02: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-19 02:34:15 +02:00
|
|
|
}
|
|
|
|
return;
|
2014-07-07 03:21:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
view = getView( selection.getNode() );
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
// If the caret is not within the selected view, deselect the view and bail.
|
|
|
|
if ( view !== selected ) {
|
|
|
|
deselect();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2014-07-19 02:34:15 +02: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-04 05:59:15 +02:00
|
|
|
} else {
|
2014-07-19 02:34:15 +02:00
|
|
|
deselect();
|
2014-07-04 05:59:15 +02:00
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-19 02:34:15 +02:00
|
|
|
setViewCursor( true, view );
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
2014-07-19 02:34:15 +02: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-04 05:59:15 +02:00
|
|
|
} else {
|
2014-07-19 02:34:15 +02:00
|
|
|
setViewCursor( false, view );
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
2014-07-19 02:34:15 +02:00
|
|
|
event.preventDefault();
|
|
|
|
// Ignore keys that don't insert anything.
|
|
|
|
} else if ( ( key > 47 || VK.SPACEBAR || key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) && key !== 144 && key !== 145 ) {
|
2014-07-19 09:30:15 +02:00
|
|
|
removeView( selected );
|
2014-07-19 02:34:15 +02:00
|
|
|
|
|
|
|
if ( key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) {
|
|
|
|
event.preventDefault();
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 02:34:15 +02:00
|
|
|
} else {
|
|
|
|
if ( event.metaKey || event.ctrlKey || ( key >= 112 && key <= 123 ) ) {
|
|
|
|
return;
|
2013-12-30 02:54:11 +01:00
|
|
|
}
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
node = selection.getNode();
|
|
|
|
lastKeyDownNode = node;
|
|
|
|
view = getView( node );
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-07-19 02:34:15 +02: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 08:01:14 +01:00
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
if ( view = getView( range.endContainer ) ) {
|
|
|
|
clonedRange = range.cloneRange();
|
2014-07-07 03:21:15 +02:00
|
|
|
selection.select( view.previousSibling, true );
|
|
|
|
selection.collapse();
|
2014-07-19 02:34:15 +02: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-07 03:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-09 00:56:14 +02:00
|
|
|
|
2014-07-19 09:30:15 +02: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-19 02:34:15 +02:00
|
|
|
setViewCursor( false, view );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2014-07-07 03:21:15 +02:00
|
|
|
}
|
2014-07-19 02:34:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-20 03:48:14 +01:00
|
|
|
|
2014-07-19 02:34:15 +02:00
|
|
|
if ( ! ( ( cursorBefore = dom.hasClass( view, 'wpview-selection-before' ) ) ||
|
|
|
|
( cursorAfter = dom.hasClass( view, 'wpview-selection-after' ) ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-20 03:48:14 +01:00
|
|
|
|
2014-07-19 02:34:15 +02: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-04 05:59:15 +02:00
|
|
|
setViewCursor( false, view );
|
2014-03-20 03:48:14 +01:00
|
|
|
}
|
2014-07-19 02:34:15 +02: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 09:30:15 +02:00
|
|
|
removeView( view );
|
2014-07-19 02:34:15 +02:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( cursorAfter ) {
|
|
|
|
handleEnter( view );
|
|
|
|
} else if ( cursorBefore ) {
|
|
|
|
handleEnter( view , true, key );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( key === VK.ENTER ) {
|
2014-03-18 05:57:14 +01:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
editor.on( 'keyup', function() {
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( toRemove ) {
|
2014-07-19 09:30:15 +02:00
|
|
|
removeView( toRemove );
|
2014-03-05 08:01:14 +01:00
|
|
|
toRemove = false;
|
|
|
|
}
|
2014-07-04 05:59:15 +02:00
|
|
|
});
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-07-07 03:21:15 +02:00
|
|
|
editor.on( 'focus', function() {
|
|
|
|
var view;
|
2014-07-09 00:56:14 +02:00
|
|
|
|
2014-07-07 03:21:15 +02: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-16 02:10:17 +02:00
|
|
|
editor.on( 'NodeChange', function( event ) {
|
2014-07-04 05:59:15 +02:00
|
|
|
var dom = editor.dom,
|
|
|
|
views = editor.dom.select( '.wpview-wrap' ),
|
|
|
|
className = event.element.className,
|
|
|
|
view = getView( event.element ),
|
|
|
|
lKDN = lastKeyDownNode;
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-07-04 05:59:15 +02:00
|
|
|
lastKeyDownNode = false;
|
|
|
|
|
|
|
|
clearInterval( cursorInterval );
|
|
|
|
|
2014-07-19 02:34:15 +02: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-04 05:59:15 +02:00
|
|
|
|
2014-07-08 06:15:14 +02:00
|
|
|
if ( focus ) {
|
|
|
|
if ( view ) {
|
2014-07-16 04:19:15 +02:00
|
|
|
if ( ( className === 'wpview-selection-before' || className === 'wpview-selection-after' ) && editor.selection.isCollapsed() ) {
|
2014-07-08 06:15:14 +02:00
|
|
|
setViewCursorTries = 0;
|
|
|
|
|
|
|
|
deselect();
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-07-08 06:15:14 +02:00
|
|
|
// Make sure the cursor arrived in the right node.
|
|
|
|
// This is necessary for Firefox.
|
|
|
|
if ( lKDN === view.previousSibling ) {
|
|
|
|
setViewCursor( true, view );
|
|
|
|
return;
|
|
|
|
} else if ( lKDN === view.nextSibling ) {
|
|
|
|
setViewCursor( false, view );
|
|
|
|
return;
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
2014-07-08 06:15:14 +02: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.)
|
2014-07-13 02:34:15 +02:00
|
|
|
} else if ( ! getParent( event.element, 'wpview-clipboard' ) && ! setViewCursorTries ) {
|
2014-07-08 06:15:14 +02:00
|
|
|
deselect();
|
|
|
|
setViewCursorTries++;
|
|
|
|
setViewCursor( true, view );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deselect();
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-09-24 02:13:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-16 04:19:15 +02:00
|
|
|
editor.on( 'BeforeExecCommand', function() {
|
|
|
|
var node = editor.selection.getNode(),
|
2014-07-16 02:10:17 +02:00
|
|
|
view;
|
|
|
|
|
2014-07-16 04:19:15 +02:00
|
|
|
if ( node && ( node.className === 'wpview-selection-before' || node.className === 'wpview-selection-after' ) && ( view = getView( node ) ) ) {
|
|
|
|
handleEnter( view );
|
|
|
|
execCommandView = view;
|
2014-07-16 02:10:17 +02:00
|
|
|
}
|
2014-07-16 04:19:15 +02:00
|
|
|
});
|
2014-07-16 02:10:17 +02:00
|
|
|
|
2014-07-16 04:19:15 +02:00
|
|
|
editor.on( 'ExecCommand', function() {
|
|
|
|
var toSelect, node;
|
|
|
|
|
|
|
|
if ( selected ) {
|
|
|
|
toSelect = selected;
|
|
|
|
deselect();
|
|
|
|
select( toSelect );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( execCommandView ) {
|
|
|
|
node = execCommandView.nextSibling;
|
|
|
|
|
|
|
|
if ( node && node.nodeName === 'P' && editor.dom.isEmpty( node ) ) {
|
|
|
|
editor.dom.remove( node );
|
|
|
|
setViewCursor( false, execCommandView );
|
|
|
|
}
|
|
|
|
|
|
|
|
execCommandView = false;
|
2014-07-16 02:10:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'ResolveName', function( event ) {
|
2014-07-08 06:15:14 +02: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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
return {
|
|
|
|
getViewText: getViewText,
|
2014-07-10 00:06:15 +02:00
|
|
|
setViewText: setViewText,
|
|
|
|
getView: getView
|
2014-03-05 08:01:14 +01:00
|
|
|
};
|
|
|
|
});
|