2013-11-15 00:18:10 -05:00
|
|
|
/* global tinymce */
|
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 ) {
|
|
|
|
var selected,
|
|
|
|
VK = tinymce.util.VK,
|
2012-10-11 23:28:22 -04:00
|
|
|
TreeWalker = tinymce.dom.TreeWalker,
|
2014-03-05 02:01:14 -05:00
|
|
|
toRemove = false;
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
function getParentView( node ) {
|
|
|
|
while ( node && node.nodeName !== 'BODY' ) {
|
|
|
|
if ( isView( node ) ) {
|
|
|
|
return node;
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isView( node ) {
|
|
|
|
return node && /\bwpview-wrap\b/.test( node.className );
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPadNode() {
|
|
|
|
return editor.dom.create( 'p', { 'data-wpview-pad': 1 },
|
|
|
|
( tinymce.Env.ie && tinymce.Env.ie < 11 ) ? '' : '<br data-mce-bogus="1" />' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text/shortcode string for a view.
|
|
|
|
*
|
|
|
|
* @param view The view wrapper's HTML id or node
|
|
|
|
* @returns string The text/shoercode string of the view
|
|
|
|
*/
|
|
|
|
function getViewText( view ) {
|
|
|
|
view = getParentView( typeof view === 'string' ? editor.dom.get( view ) : view );
|
|
|
|
|
|
|
|
if ( view ) {
|
|
|
|
return window.decodeURIComponent( editor.dom.getAttrib( view, 'data-wpview-text' ) || '' );
|
|
|
|
}
|
|
|
|
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 ) {
|
|
|
|
view = getParentView( typeof view === 'string' ? editor.dom.get( view ) : view );
|
|
|
|
|
|
|
|
if ( view ) {
|
|
|
|
editor.dom.setAttrib( view, 'data-wpview-text', window.encodeURIComponent( text || '' ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
function _stop( event ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2012-10-11 23:28:22 -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-03-05 02:01:14 -05:00
|
|
|
// Bail if node is already selected.
|
|
|
|
if ( viewNode === selected ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
deselect();
|
|
|
|
selected = viewNode;
|
|
|
|
dom.addClass( viewNode, 'selected' );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
clipboard = dom.create( 'div', {
|
|
|
|
'class': 'wpview-clipboard',
|
|
|
|
'contenteditable': 'true'
|
|
|
|
}, getViewText( viewNode ) );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
// Prepend inside the wrapper
|
|
|
|
viewNode.insertBefore( clipboard, viewNode.firstChild );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// Both of the following are necessary to prevent manipulating the selection/focus
|
2014-04-11 20:46:14 -04:00
|
|
|
dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
|
|
|
|
dom.bind( selected, 'beforedeactivate focusin focusout', _stop );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-18 00:57:14 -04: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 02:01:14 -05:00
|
|
|
// select the hidden div
|
|
|
|
editor.selection.select( clipboard, true );
|
|
|
|
}
|
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 );
|
|
|
|
dom.removeClass( selected, 'selected' );
|
2014-03-19 22:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
selected = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectSiblingView( node, direction ) {
|
|
|
|
var body = editor.getBody(),
|
|
|
|
sibling = direction === 'previous' ? 'previousSibling' : 'nextSibling';
|
|
|
|
|
|
|
|
while ( node && node.parentNode !== body ) {
|
|
|
|
if ( node[sibling] ) {
|
|
|
|
// The caret will be in another element
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
node = node.parentNode;
|
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
if ( isView( node[sibling] ) ) {
|
|
|
|
select( node[sibling] );
|
|
|
|
return true;
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
return false;
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// Check if the `wp.mce` API exists.
|
|
|
|
if ( typeof wp === 'undefined' || ! wp.mce ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
// Remove the content of view wrappers from HTML string
|
|
|
|
function emptyViews( content ) {
|
|
|
|
return content.replace(/(<div[^>]+wpview-wrap[^>]+>)[\s\S]+?data-wpview-end[^>]*><\/ins><\/div>/g, '$1</div>' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent adding undo levels on changes inside a view wrapper
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'BeforeAddUndo', function( event ) {
|
2014-04-11 20:46:14 -04:00
|
|
|
if ( event.lastLevel && emptyViews( event.level.content ) === emptyViews( event.lastLevel.content ) ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
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 ) {
|
|
|
|
if ( ! event.content ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
if ( ! event.initial ) {
|
|
|
|
wp.mce.views.unbind( editor );
|
|
|
|
}
|
|
|
|
|
|
|
|
event.content = wp.mce.views.toViews( event.content );
|
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.
|
|
|
|
editor.on( 'SetContent', function( event ) {
|
|
|
|
var body, padNode;
|
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
wp.mce.views.render();
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
// Add padding <p> if the noneditable node is last
|
|
|
|
if ( event.load || ! event.set ) {
|
|
|
|
body = editor.getBody();
|
|
|
|
|
|
|
|
if ( isView( body.lastChild ) ) {
|
|
|
|
padNode = createPadNode();
|
|
|
|
body.appendChild( padNode );
|
2014-04-11 20:46:14 -04:00
|
|
|
|
|
|
|
if ( ! event.initial ) {
|
|
|
|
editor.selection.setCursorLocation( padNode, 0 );
|
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Detect mouse down events that are adjacent to a view when a view is the first view or the last view
|
|
|
|
editor.on( 'click', function( event ) {
|
|
|
|
var body = editor.getBody(),
|
|
|
|
doc = editor.getDoc(),
|
|
|
|
scrollTop = doc.documentElement.scrollTop || body.scrollTop || 0,
|
|
|
|
x, y, firstNode, lastNode, padNode;
|
|
|
|
|
|
|
|
if ( event.target.nodeName === 'HTML' && ! event.metaKey && ! event.ctrlKey ) {
|
|
|
|
firstNode = body.firstChild;
|
|
|
|
lastNode = body.lastChild;
|
|
|
|
x = event.clientX;
|
|
|
|
y = event.clientY;
|
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
// Detect clicks above or to the left if the first node is a wpview
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( isView( firstNode ) && ( ( x < firstNode.offsetLeft && y < ( firstNode.offsetHeight - scrollTop ) ) ||
|
|
|
|
y < firstNode.offsetTop ) ) {
|
|
|
|
|
|
|
|
padNode = createPadNode();
|
|
|
|
body.insertBefore( padNode, firstNode );
|
2014-03-19 22:48:14 -04:00
|
|
|
|
|
|
|
// Detect clicks to the right and below the last view
|
2014-03-05 02:01:14 -05:00
|
|
|
} else if ( isView( lastNode ) && ( x > ( lastNode.offsetLeft + lastNode.offsetWidth ) ||
|
|
|
|
( ( scrollTop + y ) - ( lastNode.offsetTop + lastNode.offsetHeight ) ) > 0 ) ) {
|
|
|
|
|
|
|
|
padNode = createPadNode();
|
|
|
|
body.appendChild( padNode );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( padNode ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
// Make sure that a selected view is deselected so that focus and selection are handled properly
|
|
|
|
deselect();
|
|
|
|
editor.getBody().focus();
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.selection.setCursorLocation( padNode, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on( 'init', function() {
|
|
|
|
var selection = editor.selection;
|
|
|
|
// 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,
|
|
|
|
view = getParentView( selection.getNode() );
|
|
|
|
|
|
|
|
// If the selection is not within a view, bail.
|
|
|
|
if ( ! view ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! view.nextSibling || isView( view.nextSibling ) ) {
|
|
|
|
// 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 );
|
|
|
|
});
|
|
|
|
|
|
|
|
// When the selection's content changes, scan any new content
|
|
|
|
// for matching views.
|
|
|
|
//
|
|
|
|
// Runs on paste and on inserting nodes/html.
|
|
|
|
editor.on( 'SetContent', function( e ) {
|
|
|
|
if ( ! e.context ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var node = selection.getNode();
|
|
|
|
|
|
|
|
if ( ! node.innerHTML ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node.innerHTML = wp.mce.views.toViews( node.innerHTML );
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.dom.bind( editor.getBody(), 'mousedown mouseup click', function( event ) {
|
2014-04-08 20:58:15 -04:00
|
|
|
var view = getParentView( event.target ),
|
|
|
|
deselectEventType;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
// Contain clicks inside the view wrapper
|
|
|
|
if ( view ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2014-04-08 20:58:15 -04:00
|
|
|
// Hack to try and keep the block resize handles from appearing. They will show on mousedown and then be removed on mouseup.
|
|
|
|
if ( tinymce.Env.ie <= 10 ) {
|
|
|
|
deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
select( view );
|
|
|
|
|
2014-03-19 22:48:14 -04: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' ) ) {
|
|
|
|
editor.dom.remove( view );
|
2012-10-11 23:28:22 -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-04-08 20:58:15 -04: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-04-09 11:29:17 -04:00
|
|
|
if ( tinymce.Env.ie && tinymce.Env.ie <= 8 ) {
|
2014-04-08 20:58:15 -04:00
|
|
|
deselectEventType = 'mouseup';
|
|
|
|
} else {
|
|
|
|
deselectEventType = 'mousedown';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event.type === deselectEventType ) {
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'PreProcess', function( event ) {
|
|
|
|
var dom = editor.dom;
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
// Remove empty padding nodes
|
|
|
|
tinymce.each( dom.select( 'p[data-wpview-pad]', event.node ), function( node ) {
|
|
|
|
if ( dom.isEmpty( node ) ) {
|
|
|
|
dom.remove( node );
|
|
|
|
} else {
|
|
|
|
dom.setAttrib( node, 'data-wpview-pad', null );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Replace the wpview node with the wpview string/shortcode?
|
|
|
|
tinymce.each( dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
|
|
|
|
// Empty the wrap node
|
|
|
|
if ( 'textContent' in node ) {
|
2014-05-06 00:51:14 -04:00
|
|
|
node.textContent = '\u00a0';
|
2014-03-05 02:01:14 -05:00
|
|
|
} else {
|
2014-05-06 00:51:14 -04:00
|
|
|
node.innerText = '\u00a0';
|
2012-10-11 19:52:09 -04:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
});
|
|
|
|
});
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-05-06 00:51:14 -04: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-03-05 02:01:14 -05:00
|
|
|
editor.on( 'keydown', function( event ) {
|
|
|
|
var keyCode = event.keyCode,
|
2014-03-18 00:57:14 -04:00
|
|
|
body = editor.getBody(),
|
|
|
|
view, padNode;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
// If a view isn't selected, let the event go on its merry way.
|
|
|
|
if ( ! selected ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let keypresses that involve the command or control keys through.
|
|
|
|
// Also, let any of the F# keys through.
|
|
|
|
if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) ) {
|
|
|
|
if ( ( event.metaKey || event.ctrlKey ) && keyCode === 88 ) {
|
|
|
|
toRemove = selected;
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
view = getParentView( editor.selection.getNode() );
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-18 00:57:14 -04:00
|
|
|
// If the caret is not within the selected view, deselect the
|
|
|
|
// view and bail.
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( view !== selected ) {
|
|
|
|
deselect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
// Deselect views with the arrow keys
|
2014-03-18 00:57:14 -04:00
|
|
|
if ( keyCode === VK.LEFT || keyCode === VK.UP ) {
|
|
|
|
deselect();
|
|
|
|
// Handle case where two views are stacked on top of one another
|
|
|
|
if ( isView( view.previousSibling ) ) {
|
|
|
|
select( view.previousSibling );
|
|
|
|
// Handle case where view is the first node
|
2014-03-19 22:48:14 -04:00
|
|
|
} else if ( ! view.previousSibling ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
padNode = createPadNode();
|
|
|
|
body.insertBefore( padNode, body.firstChild );
|
|
|
|
editor.selection.setCursorLocation( body.firstChild, 0 );
|
|
|
|
// Handle default case
|
|
|
|
} else {
|
|
|
|
editor.selection.select( view.previousSibling, true );
|
|
|
|
editor.selection.collapse();
|
|
|
|
}
|
|
|
|
} else if ( keyCode === VK.RIGHT || keyCode === VK.DOWN ) {
|
|
|
|
deselect();
|
|
|
|
// Handle case where the next node is another wpview
|
|
|
|
if ( isView( view.nextSibling ) ) {
|
|
|
|
select( view.nextSibling );
|
|
|
|
// Handle case were the view is that last node
|
2014-03-19 22:48:14 -04:00
|
|
|
} else if ( ! view.nextSibling ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
padNode = createPadNode();
|
|
|
|
body.appendChild( padNode );
|
|
|
|
editor.selection.setCursorLocation( body.lastChild, 0 );
|
|
|
|
// Handle default case where the next node is a non-wpview
|
|
|
|
} else {
|
2014-03-19 22:48:14 -04:00
|
|
|
editor.selection.setCursorLocation( view.nextSibling, 0 );
|
2014-03-18 00:57:14 -04:00
|
|
|
}
|
|
|
|
} else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
|
|
|
|
// If delete or backspace is pressed, delete the view.
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.dom.remove( selected );
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
// Select views when arrow keys are used to navigate the content of the editor.
|
2014-03-18 00:57:14 -04:00
|
|
|
editor.on( 'keydown', function( event ) {
|
|
|
|
var keyCode = event.keyCode,
|
2014-03-19 22:48:14 -04:00
|
|
|
dom = editor.dom,
|
2014-03-18 00:57:14 -04:00
|
|
|
range = editor.selection.getRng(),
|
2014-03-19 22:48:14 -04:00
|
|
|
startNode = range.startContainer,
|
2014-03-18 00:57:14 -04:00
|
|
|
body = editor.getBody(),
|
2014-03-19 22:48:14 -04:00
|
|
|
node, container;
|
2014-03-18 00:57:14 -04:00
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
if ( ! startNode || startNode === body || event.metaKey || event.ctrlKey ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:48:14 -04:00
|
|
|
if ( keyCode === VK.UP || keyCode === VK.LEFT ) {
|
|
|
|
if ( keyCode === VK.LEFT && ( ! range.collapsed || range.startOffset !== 0 ) ) {
|
|
|
|
// Not at the beginning of the current range
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ( node = dom.getParent( startNode, dom.isBlock ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( selectSiblingView( node, 'previous' ) ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
2014-03-19 22:48:14 -04:00
|
|
|
} else if ( keyCode === VK.DOWN || keyCode === VK.RIGHT ) {
|
|
|
|
if ( ! ( node = dom.getParent( startNode, dom.isBlock ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( keyCode === VK.RIGHT ) {
|
|
|
|
container = range.endContainer;
|
|
|
|
|
|
|
|
if ( ! range.collapsed || ( range.startOffset === 0 && container.length ) ||
|
|
|
|
container.nextSibling ||
|
|
|
|
( container.nodeType === 3 && range.startOffset !== container.length ) ) { // Not at the end of the current range
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a child element
|
|
|
|
while ( container && container !== node && container !== body ) {
|
|
|
|
if ( container.nextSibling ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
container = container.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( selectSiblingView( node, 'next' ) ) {
|
2014-03-18 00:57:14 -04:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
editor.on( 'keyup', function( event ) {
|
|
|
|
var padNode,
|
|
|
|
keyCode = event.keyCode,
|
|
|
|
body = editor.getBody(),
|
|
|
|
range;
|
|
|
|
|
|
|
|
if ( toRemove ) {
|
|
|
|
editor.dom.remove( toRemove );
|
|
|
|
toRemove = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
|
|
|
|
// Make sure there is padding if the last element is a view
|
|
|
|
if ( isView( body.lastChild ) ) {
|
|
|
|
padNode = createPadNode();
|
|
|
|
body.appendChild( padNode );
|
|
|
|
|
|
|
|
if ( body.childNodes.length === 2 ) {
|
|
|
|
editor.selection.setCursorLocation( padNode, 0 );
|
|
|
|
}
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
range = editor.selection.getRng();
|
|
|
|
|
|
|
|
// Allow an initial element in the document to be removed when it is before a view
|
|
|
|
if ( body.firstChild === range.startContainer && range.collapsed === true &&
|
|
|
|
isView( range.startContainer.nextSibling ) && range.startOffset === 0 ) {
|
|
|
|
|
|
|
|
editor.dom.remove( range.startContainer );
|
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
return {
|
|
|
|
getViewText: getViewText,
|
|
|
|
setViewText: setViewText
|
|
|
|
};
|
|
|
|
});
|