TinyMCE: textpattern: clean up
* Use editor.once instead of storing into variables. * Add pattern for hr. See #33300. git-svn-id: https://develop.svn.wordpress.org/trunk@36719 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
918da6f8c2
commit
37fda5a8b9
@ -25,7 +25,8 @@
|
||||
{ start: '####', format: 'h4' },
|
||||
{ start: '#####', format: 'h5' },
|
||||
{ start: '######', format: 'h6' },
|
||||
{ start: '>', format: 'blockquote' }
|
||||
{ start: '>', format: 'blockquote' },
|
||||
{ regExp: /^\s*(?:(?:\* ?){3,}|(?:_ ?){3,}|(?:- ?){3,})\s*$/, element: 'hr' }
|
||||
];
|
||||
|
||||
var inlinePatterns = [
|
||||
@ -37,10 +38,7 @@
|
||||
];
|
||||
|
||||
var canUndo;
|
||||
var refNode;
|
||||
var refPattern;
|
||||
var chars = [];
|
||||
var zeroWidthSpaceNode;
|
||||
|
||||
tinymce.each( inlinePatterns, function( pattern ) {
|
||||
tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) {
|
||||
@ -51,17 +49,7 @@
|
||||
} );
|
||||
|
||||
editor.on( 'selectionchange', function() {
|
||||
var offset;
|
||||
|
||||
canUndo = null;
|
||||
|
||||
if ( zeroWidthSpaceNode ) {
|
||||
offset = zeroWidthSpaceNode.data.indexOf( '\u200b' );
|
||||
|
||||
if ( offset !== -1 ) {
|
||||
zeroWidthSpaceNode.deleteData( offset, offset + 1 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
editor.on( 'keydown', function( event ) {
|
||||
@ -72,15 +60,13 @@
|
||||
}
|
||||
|
||||
if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
|
||||
watchEnter();
|
||||
enter();
|
||||
}
|
||||
}, true );
|
||||
|
||||
editor.on( 'keyup', function( event ) {
|
||||
if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) {
|
||||
space();
|
||||
} else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
|
||||
enter();
|
||||
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
|
||||
inline();
|
||||
}
|
||||
@ -155,7 +141,18 @@
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'space';
|
||||
zeroWidthSpaceNode = zero;
|
||||
|
||||
editor.once( 'selectionchange', function() {
|
||||
var offset;
|
||||
|
||||
if ( zero ) {
|
||||
offset = zero.data.indexOf( '\u200b' );
|
||||
|
||||
if ( offset !== -1 ) {
|
||||
zero.deleteData( offset, offset + 1 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
}
|
||||
}
|
||||
@ -233,7 +230,7 @@
|
||||
} );
|
||||
}
|
||||
|
||||
function watchEnter() {
|
||||
function enter() {
|
||||
var rng = editor.selection.getRng(),
|
||||
start = rng.startContainer,
|
||||
node = firstTextNode( start ),
|
||||
@ -247,10 +244,17 @@
|
||||
text = node.data;
|
||||
|
||||
while ( i-- ) {
|
||||
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
|
||||
pattern = enterPatterns[ i ];
|
||||
break;
|
||||
}
|
||||
if ( enterPatterns[ i ].start ) {
|
||||
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
|
||||
pattern = enterPatterns[ i ];
|
||||
break;
|
||||
}
|
||||
} else if ( enterPatterns[ i ].regExp ) {
|
||||
if ( enterPatterns[ i ].regExp.test( text ) ) {
|
||||
pattern = enterPatterns[ i ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! pattern ) {
|
||||
@ -261,31 +265,27 @@
|
||||
return;
|
||||
}
|
||||
|
||||
refNode = node;
|
||||
refPattern = pattern;
|
||||
}
|
||||
|
||||
function ltrim( text ) {
|
||||
return text ? text.replace( /^\s+/, '' ) : '';
|
||||
}
|
||||
|
||||
function enter() {
|
||||
if ( refNode ) {
|
||||
editor.once( 'keyup', function() {
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
editor.formatter.apply( refPattern.format, {}, refNode );
|
||||
refNode.replaceData( 0, refNode.data.length, ltrim( refNode.data.slice( refPattern.start.length ) ) );
|
||||
if ( pattern.format ) {
|
||||
editor.formatter.apply( pattern.format, {}, node );
|
||||
node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) );
|
||||
} else if ( pattern.element ) {
|
||||
editor.getBody().replaceChild( document.createElement( pattern.element ), node.parentNode );
|
||||
}
|
||||
} );
|
||||
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'enter';
|
||||
} );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
refNode = null;
|
||||
refPattern = null;
|
||||
function ltrim( text ) {
|
||||
return text ? text.replace( /^\s+/, '' ) : '';
|
||||
}
|
||||
} );
|
||||
} )( window.tinymce, window.setTimeout );
|
||||
|
@ -306,8 +306,14 @@
|
||||
} );
|
||||
|
||||
QUnit.test( 'Inline: no change.', function( assert ) {
|
||||
type( '******', function() {
|
||||
assert.equal( editor.getContent(), '<p>******</p>' );
|
||||
type( 'test ******', function() {
|
||||
assert.equal( editor.getContent(), '<p>test ******</p>' );
|
||||
}, assert.async() );
|
||||
} );
|
||||
|
||||
QUnit.test( 'Horizontal Rule', function( assert ) {
|
||||
type( ' --- \n', function() {
|
||||
assert.equal( editor.getContent(), '<hr />\n<p> </p>' );
|
||||
}, assert.async() );
|
||||
} );
|
||||
} )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );
|
||||
|
Loading…
Reference in New Issue
Block a user