TinyMCE: inline text patterns
First run. See #33300. Built from https://develop.svn.wordpress.org/trunk@36627 git-svn-id: http://core.svn.wordpress.org/trunk@36594 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b23b89c8a9
commit
3d68488ff6
|
@ -12,23 +12,56 @@
|
|||
*/
|
||||
( function( tinymce, setTimeout ) {
|
||||
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
||||
var VK = tinymce.util.VK,
|
||||
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' }
|
||||
],
|
||||
canUndo, refNode, refPattern;
|
||||
var VK = tinymce.util.VK;
|
||||
|
||||
var spacePatterns = [
|
||||
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
|
||||
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
|
||||
];
|
||||
|
||||
var enterPatterns = [
|
||||
{ start: '##', format: 'h2' },
|
||||
{ start: '###', format: 'h3' },
|
||||
{ start: '####', format: 'h4' },
|
||||
{ start: '#####', format: 'h5' },
|
||||
{ start: '######', format: 'h6' },
|
||||
{ start: '>', format: 'blockquote' }
|
||||
];
|
||||
|
||||
var inlinePatterns = [
|
||||
{ start: '*', end: '*', format: 'italic' },
|
||||
{ start: '**', end: '**', format: 'bold' },
|
||||
{ start: '_', end: '_', format: 'italic' },
|
||||
{ start: '__', end: '__', format: 'bold' },
|
||||
{ start: '`', end: '`', format: 'code' }
|
||||
];
|
||||
|
||||
var canUndo;
|
||||
var refNode;
|
||||
var refPattern;
|
||||
var chars = [];
|
||||
var zeroWidthSpaceNode;
|
||||
|
||||
tinymce.each( inlinePatterns, function( pattern ) {
|
||||
tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) {
|
||||
if ( tinymce.inArray( chars, c ) === -1 ) {
|
||||
chars.push( c );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
|
||||
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 ) {
|
||||
|
@ -48,9 +81,85 @@
|
|||
space();
|
||||
} else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
|
||||
enter();
|
||||
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
|
||||
inline();
|
||||
}
|
||||
} );
|
||||
|
||||
function inline() {
|
||||
var rng = editor.selection.getRng();
|
||||
var node = rng.startContainer;
|
||||
var offset = rng.startOffset;
|
||||
var startOffset;
|
||||
var endOffset;
|
||||
var pattern;
|
||||
var format;
|
||||
var zero;
|
||||
|
||||
if ( node.nodeType !== 3 || ! node.data.length || ! offset ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( tinymce.inArray( chars, node.data.charAt( offset - 1 ) ) === -1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
function findStart( node ) {
|
||||
var i = inlinePatterns.length;
|
||||
var offset;
|
||||
|
||||
while ( i-- ) {
|
||||
pattern = inlinePatterns[ i ];
|
||||
offset = node.data.indexOf( pattern.end );
|
||||
|
||||
if ( offset !== -1 ) {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
startOffset = findStart( node );
|
||||
endOffset = node.data.lastIndexOf( pattern.end );
|
||||
|
||||
if ( startOffset === endOffset || endOffset === -1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( endOffset - startOffset <= pattern.start.length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( node.data.slice( startOffset + pattern.start.length, endOffset ).indexOf( pattern.start.slice( 0, 1 ) ) !== -1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
format = editor.formatter.get( pattern.format );
|
||||
|
||||
if ( format && format[0].inline ) {
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
node.insertData( offset, '\u200b' );
|
||||
|
||||
node = node.splitText( startOffset );
|
||||
zero = node.splitText( offset - startOffset );
|
||||
|
||||
node.deleteData( 0, pattern.start.length );
|
||||
node.deleteData( node.data.length - pattern.end.length, pattern.end.length );
|
||||
|
||||
editor.formatter.apply( pattern.format, {}, node );
|
||||
|
||||
editor.selection.setCursorLocation( zero, 1 );
|
||||
} );
|
||||
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'space';
|
||||
zeroWidthSpaceNode = zero;
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
function firstTextNode( node ) {
|
||||
var parent = editor.dom.getParent( node, 'p' ),
|
||||
child;
|
||||
|
@ -157,11 +266,7 @@
|
|||
}
|
||||
|
||||
function ltrim( text ) {
|
||||
if ( text ) {
|
||||
return text.replace( /^\s+/, '' );
|
||||
}
|
||||
|
||||
return '';
|
||||
return text ? text.replace( /^\s+/, '' ) : '';
|
||||
}
|
||||
|
||||
function enter() {
|
||||
|
|
|
@ -1 +1 @@
|
|||
!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&&3===b.nextSibling.nodeType?b.nextSibling:null),b}}function e(){var e,f,g=c.selection.getRng(),h=g.startContainer;h&&d(h)===h&&(e=h.parentNode,f=h.data,a.each(m,function(a){var d=f.match(a.regExp);if(d&&g.startOffset===d[0].length)return c.undoManager.add(),c.undoManager.transact(function(){h.deleteData(0,d[0].length),e.innerHTML||e.appendChild(document.createElement("br")),c.selection.setCursorLocation(e),c.execCommand(a.cmd)}),b(function(){i="space"}),!1}))}function f(){var b,e,f=c.selection.getRng(),g=f.startContainer,h=d(g),i=n.length;if(h){for(b=h.data;i--;)if(0===b.indexOf(n[i].start)){e=n[i];break}e&&(h!==g||a.trim(b)!==e.start)&&(j=h,k=e)}}function g(a){return a?a.replace(/^\s+/,""):""}function h(){j&&(c.undoManager.add(),c.undoManager.transact(function(){c.formatter.apply(k.format,{},j),j.replaceData(0,j.data.length,g(j.data.slice(k.start.length)))}),b(function(){i="enter"})),j=null,k=null}var i,j,k,l=a.util.VK,m=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],n=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"}];c.on("selectionchange",function(){i=null}),c.on("keydown",function(a){(i&&27===a.keyCode||"space"===i&&a.keyCode===l.BACKSPACE)&&(c.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),a.keyCode!==l.ENTER||l.modifierPressed(a)||f()},!0),c.on("keyup",function(a){a.keyCode!==l.SPACEBAR||a.ctrlKey||a.metaKey||a.altKey?a.keyCode!==l.ENTER||l.modifierPressed(a)||h():e()})})}(window.tinymce,window.setTimeout);
|
||||
!function(a,b){a.PluginManager.add("wptextpattern",function(c){function d(){function d(a){for(var b,c=q.length;c--;)if(g=q[c],b=a.data.indexOf(g.end),-1!==b)return b}var e,f,g,h,i,k=c.selection.getRng(),l=k.startContainer,n=k.startOffset;3===l.nodeType&&l.data.length&&n&&-1!==a.inArray(r,l.data.charAt(n-1))&&(e=d(l),f=l.data.lastIndexOf(g.end),e!==f&&-1!==f&&(f-e<=g.start.length||-1===l.data.slice(e+g.start.length,f).indexOf(g.start.slice(0,1))&&(h=c.formatter.get(g.format),h&&h[0].inline&&(c.undoManager.add(),c.undoManager.transact(function(){l.insertData(n,"\u200b"),l=l.splitText(e),i=l.splitText(n-e),l.deleteData(0,g.start.length),l.deleteData(l.data.length-g.end.length,g.end.length),c.formatter.apply(g.format,{},l),c.selection.setCursorLocation(i,1)}),b(function(){j="space",m=i})))))}function e(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&&3===b.nextSibling.nodeType?b.nextSibling:null),b}}function f(){var d,f,g=c.selection.getRng(),h=g.startContainer;h&&e(h)===h&&(d=h.parentNode,f=h.data,a.each(o,function(a){var e=f.match(a.regExp);if(e&&g.startOffset===e[0].length)return c.undoManager.add(),c.undoManager.transact(function(){h.deleteData(0,e[0].length),d.innerHTML||d.appendChild(document.createElement("br")),c.selection.setCursorLocation(d),c.execCommand(a.cmd)}),b(function(){j="space"}),!1}))}function g(){var b,d,f=c.selection.getRng(),g=f.startContainer,h=e(g),i=p.length;if(h){for(b=h.data;i--;)if(0===b.indexOf(p[i].start)){d=p[i];break}d&&(h!==g||a.trim(b)!==d.start)&&(k=h,l=d)}}function h(a){return a?a.replace(/^\s+/,""):""}function i(){k&&(c.undoManager.add(),c.undoManager.transact(function(){c.formatter.apply(l.format,{},k),k.replaceData(0,k.data.length,h(k.data.slice(l.start.length)))}),b(function(){j="enter"})),k=null,l=null}var j,k,l,m,n=a.util.VK,o=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],p=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"}],q=[{start:"*",end:"*",format:"italic"},{start:"**",end:"**",format:"bold"},{start:"_",end:"_",format:"italic"},{start:"__",end:"__",format:"bold"},{start:"`",end:"`",format:"code"}],r=[];a.each(q,function(b){a.each((b.start+b.end).split(""),function(b){-1===a.inArray(r,b)&&r.push(b)})}),c.on("selectionchange",function(){var a;j=null,m&&(a=m.data.indexOf("\u200b"),-1!==a&&m.deleteData(a,a+1))}),c.on("keydown",function(a){(j&&27===a.keyCode||"space"===j&&a.keyCode===n.BACKSPACE)&&(c.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),a.keyCode!==n.ENTER||n.modifierPressed(a)||g()},!0),c.on("keyup",function(a){a.keyCode!==n.SPACEBAR||a.ctrlKey||a.metaKey||a.altKey?a.keyCode!==n.ENTER||n.modifierPressed(a)?a.keyCode>47&&!(a.keyCode>=91&&a.keyCode<=93)&&d():i():f()})})}(window.tinymce,window.setTimeout);
|
Binary file not shown.
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.5-alpha-36626';
|
||||
$wp_version = '4.5-alpha-36627';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue