TinyMCE: wptextpattern: headings on enter

Fixes #31441.

Built from https://develop.svn.wordpress.org/trunk@33452


git-svn-id: http://core.svn.wordpress.org/trunk@33419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ella Iseulde Van Dorpe 2015-07-27 23:16:25 +00:00
parent 4a73f82a28
commit f78068c152
4 changed files with 92 additions and 62 deletions

View File

@ -14,39 +14,19 @@
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
var $$ = editor.$,
VK = tinymce.util.VK,
patterns = [],
canUndo = false;
/**
* Add a pattern to format with a callback.
*
* @since 4.3.0
*
* @param {RegExp} regExp RegEx pattern.
* @param {Function} callback Callback.
*/
function add( regExp, callback ) {
patterns.push( {
regExp: regExp,
callback: callback
} );
}
add( /^[*-]\s/, function() {
this.execCommand( 'InsertUnorderedList' );
} );
add( /^1[.)]\s/, function() {
this.execCommand( 'InsertOrderedList' );
} );
add( /^>\s/, function() {
this.formatter.toggle( 'blockquote' );
} );
add( /^(#{2,6})\s/, function() {
this.formatter.toggle( 'h' + arguments[1].length );
} );
canUndo = false,
spacePatterns = [
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
],
enterPatterns = [
{ start: '##', format: 'h2' },
{ start: '###', format: 'h3' },
{ start: '####', format: 'h4' },
{ start: '#####', format: 'h5' },
{ start: '######', format: 'h6' },
{ start: '>', format: 'blockquote' }
];
editor.on( 'selectionchange', function() {
canUndo = false;
@ -57,24 +37,21 @@
editor.undoManager.undo();
event.preventDefault();
}
} );
if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
enter();
}
}, true );
editor.on( 'keyup', function( event ) {
var rng, node, text, parent, child;
if ( event.keyCode !== VK.SPACEBAR ) {
return;
if ( event.keyCode === VK.SPACEBAR || ! VK.modifierPressed( event ) ) {
space();
}
} );
rng = editor.selection.getRng();
node = rng.startContainer;
if ( ! node || node.nodeType !== 3 ) {
return;
}
text = node.nodeValue;
parent = editor.dom.getParent( node, 'p' );
function firstNode( node ) {
var parent = editor.dom.getParent( node, 'p' ),
child;
if ( ! parent ) {
return;
@ -92,20 +69,26 @@
return;
}
if ( ! child.nodeValue ) {
if ( ! child.data ) {
child = child.nextSibling;
}
if ( child !== node ) {
return child;
}
function space() {
var rng = editor.selection.getRng(),
node = rng.startContainer,
text;
if ( firstNode( node ) !== node ) {
return;
}
tinymce.each( patterns, function( pattern ) {
var args,
replace = text.replace( pattern.regExp, function() {
args = arguments;
return '';
} );
text = node.data;
tinymce.each( spacePatterns, function( pattern ) {
var replace = text.replace( pattern.regExp, '' );
if ( text === replace ) {
return;
@ -118,12 +101,13 @@
editor.undoManager.add();
editor.undoManager.transact( function() {
var $$parent;
var parent = node.parentNode,
$$parent;
if ( replace ) {
$$( node ).replaceWith( document.createTextNode( replace ) );
} else {
$$parent = $$( node.parentNode );
$$parent = $$( parent );
$$( node ).remove();
@ -133,8 +117,7 @@
}
editor.selection.setCursorLocation( parent );
pattern.callback.apply( editor, args );
editor.execCommand( pattern.cmd );
} );
// We need to wait for native events to be triggered.
@ -144,6 +127,53 @@
return false;
} );
}
function enter() {
var selection = editor.selection,
rng = selection.getRng(),
offset = rng.startOffset,
start = rng.startContainer,
node = firstNode( start ),
i = enterPatterns.length,
text, pattern;
if ( ! node ) {
return;
}
text = node.data;
while ( i-- ) {
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
pattern = enterPatterns[ i ];
break;
}
}
if ( ! pattern ) {
return;
}
if ( node === start ) {
if ( tinymce.trim( text ) === pattern.start ) {
return;
}
offset = Math.max( 0, offset - pattern.start.length );
}
editor.undoManager.add();
editor.undoManager.transact( function() {
node.deleteData( 0, pattern.start.length );
editor.formatter.apply( pattern.format, {}, start );
rng.setStart( start, offset );
rng.collapse( true );
selection.setRng( rng );
} );
}
} );
} )( window.tinymce, window.setTimeout );

View File

@ -1 +1 @@
!function(a,b){a.PluginManager.add("wptextpattern",function(c){function d(a,b){g.push({regExp:a,callback:b})}var e=c.$,f=a.util.VK,g=[],h=!1;d(/^[*-]\s/,function(){this.execCommand("InsertUnorderedList")}),d(/^1[.)]\s/,function(){this.execCommand("InsertOrderedList")}),d(/^>\s/,function(){this.formatter.toggle("blockquote")}),d(/^(#{2,6})\s/,function(){this.formatter.toggle("h"+arguments[1].length)}),c.on("selectionchange",function(){h=!1}),c.on("keydown",function(a){!h||a.keyCode!==f.BACKSPACE&&27!==a.keyCode||(c.undoManager.undo(),a.preventDefault())}),c.on("keyup",function(d){var i,j,k,l,m;if(d.keyCode===f.SPACEBAR&&(i=c.selection.getRng(),j=i.startContainer,j&&3===j.nodeType&&(k=j.nodeValue,l=c.dom.getParent(j,"p")))){for(;(m=l.firstChild)&&3!==m.nodeType;)l=m;m&&(m.nodeValue||(m=m.nextSibling),m===j&&a.each(g,function(a){var d,f=k.replace(a.regExp,function(){return d=arguments,""});if(k!==f&&i.startOffset===k.length-f.length)return c.undoManager.add(),c.undoManager.transact(function(){var b;f?e(j).replaceWith(document.createTextNode(f)):(b=e(j.parentNode),e(j).remove(),b.html()||b.append("<br>")),c.selection.setCursorLocation(l),a.callback.apply(c,d)}),b(function(){h=!0}),!1}))}})})}(window.tinymce,window.setTimeout);
!function(a,b){a.PluginManager.add("wptextpattern",function(c){function d(a){var b,d=c.dom.getParent(a,"p");if(d){for(;(b=d.firstChild)&&3!==b.nodeType;)d=b;if(b)return b.data||(b=b.nextSibling),b}}function e(){var e,f=c.selection.getRng(),h=f.startContainer;d(h)===h&&(e=h.data,a.each(j,function(a){var d=e.replace(a.regExp,"");if(e!==d&&f.startOffset===e.length-d.length)return c.undoManager.add(),c.undoManager.transact(function(){var b,e=h.parentNode;d?g(h).replaceWith(document.createTextNode(d)):(b=g(e),g(h).remove(),b.html()||b.append("<br>")),c.selection.setCursorLocation(e),c.execCommand(a.cmd)}),b(function(){i=!0}),!1}))}function f(){var b,e,f=c.selection,g=f.getRng(),h=g.startOffset,i=g.startContainer,j=d(i),l=k.length;if(j){for(b=j.data;l--;)if(0===b.indexOf(k[l].start)){e=k[l];break}if(e){if(j===i){if(a.trim(b)===e.start)return;h=Math.max(0,h-e.start.length)}c.undoManager.add(),c.undoManager.transact(function(){j.deleteData(0,e.start.length),c.formatter.apply(e.format,{},i),g.setStart(i,h),g.collapse(!0),f.setRng(g)})}}}var g=c.$,h=a.util.VK,i=!1,j=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],k=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"}];c.on("selectionchange",function(){i=!1}),c.on("keydown",function(a){!i||a.keyCode!==h.BACKSPACE&&27!==a.keyCode||(c.undoManager.undo(),a.preventDefault()),a.keyCode!==h.ENTER||h.modifierPressed(a)||f()},!0),c.on("keyup",function(a){a.keyCode!==h.SPACEBAR&&h.modifierPressed(a)||e()})})}(window.tinymce,window.setTimeout);

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.3-beta4-33451';
$wp_version = '4.3-beta4-33452';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.