first commit
This commit is contained in:
348
wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
Normal file
348
wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
Normal file
@ -0,0 +1,348 @@
|
||||
/**
|
||||
* Text pattern plugin for TinyMCE
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* This plugin can automatically format text patterns as you type. It includes several groups of patterns.
|
||||
*
|
||||
* Start of line patterns:
|
||||
* As-you-type:
|
||||
* - Unordered list (`* ` and `- `).
|
||||
* - Ordered list (`1. ` and `1) `).
|
||||
*
|
||||
* On enter:
|
||||
* - h2 (## ).
|
||||
* - h3 (### ).
|
||||
* - h4 (#### ).
|
||||
* - h5 (##### ).
|
||||
* - h6 (###### ).
|
||||
* - blockquote (> ).
|
||||
* - hr (---).
|
||||
*
|
||||
* Inline patterns:
|
||||
* - <code> (`) (backtick).
|
||||
*
|
||||
* If the transformation in unwanted, the user can undo the change by pressing backspace,
|
||||
* using the undo shortcut, or the undo button in the toolbar.
|
||||
*
|
||||
* Setting for the patterns can be overridden by plugins by using the `tiny_mce_before_init` PHP filter.
|
||||
* The setting name is `wptextpattern` and the value is an object containing override arrays for each
|
||||
* patterns group. There are three groups: "space", "enter", and "inline". Example (PHP):
|
||||
*
|
||||
* add_filter( 'tiny_mce_before_init', 'my_mce_init_wptextpattern' );
|
||||
* function my_mce_init_wptextpattern( $init ) {
|
||||
* $init['wptextpattern'] = wp_json_encode( array(
|
||||
* 'inline' => array(
|
||||
* array( 'delimiter' => '**', 'format' => 'bold' ),
|
||||
* array( 'delimiter' => '__', 'format' => 'italic' ),
|
||||
* ),
|
||||
* ) );
|
||||
*
|
||||
* return $init;
|
||||
* }
|
||||
*
|
||||
* Note that setting this will override the default text patterns. You will need to include them
|
||||
* in your settings array if you want to keep them working.
|
||||
*/
|
||||
( function( tinymce, setTimeout ) {
|
||||
if ( tinymce.Env.ie && tinymce.Env.ie < 9 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes characters for use in a Regular Expression.
|
||||
*
|
||||
* @param {String} string Characters to escape
|
||||
*
|
||||
* @return {String} Escaped characters
|
||||
*/
|
||||
function escapeRegExp( string ) {
|
||||
return string.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' );
|
||||
}
|
||||
|
||||
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
||||
var VK = tinymce.util.VK;
|
||||
var settings = editor.settings.wptextpattern || {};
|
||||
|
||||
var spacePatterns = settings.space || [
|
||||
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
|
||||
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
|
||||
];
|
||||
|
||||
var enterPatterns = settings.enter || [
|
||||
{ start: '##', format: 'h2' },
|
||||
{ start: '###', format: 'h3' },
|
||||
{ start: '####', format: 'h4' },
|
||||
{ start: '#####', format: 'h5' },
|
||||
{ start: '######', format: 'h6' },
|
||||
{ start: '>', format: 'blockquote' },
|
||||
{ regExp: /^(-){3,}$/, element: 'hr' }
|
||||
];
|
||||
|
||||
var inlinePatterns = settings.inline || [
|
||||
{ delimiter: '`', format: 'code' }
|
||||
];
|
||||
|
||||
var canUndo;
|
||||
|
||||
editor.on( 'selectionchange', function() {
|
||||
canUndo = null;
|
||||
} );
|
||||
|
||||
editor.on( 'keydown', function( event ) {
|
||||
if ( ( canUndo && event.keyCode === 27 /* ESCAPE */ ) || ( canUndo === 'space' && event.keyCode === VK.BACKSPACE ) ) {
|
||||
editor.undoManager.undo();
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
}
|
||||
|
||||
if ( VK.metaKeyPressed( event ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( event.keyCode === VK.ENTER ) {
|
||||
enter();
|
||||
// Wait for the browser to insert the character.
|
||||
} else if ( event.keyCode === VK.SPACEBAR ) {
|
||||
setTimeout( space );
|
||||
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
|
||||
setTimeout( inline );
|
||||
}
|
||||
}, true );
|
||||
|
||||
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;
|
||||
|
||||
// We need a non-empty text node with an offset greater than zero.
|
||||
if ( ! node || node.nodeType !== 3 || ! node.data.length || ! offset ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var string = node.data.slice( 0, offset );
|
||||
var lastChar = node.data.charAt( offset - 1 );
|
||||
|
||||
tinymce.each( inlinePatterns, function( p ) {
|
||||
// Character before selection should be delimiter.
|
||||
if ( lastChar !== p.delimiter.slice( -1 ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var escDelimiter = escapeRegExp( p.delimiter );
|
||||
var delimiterFirstChar = p.delimiter.charAt( 0 );
|
||||
var regExp = new RegExp( '(.*)' + escDelimiter + '.+' + escDelimiter + '$' );
|
||||
var match = string.match( regExp );
|
||||
|
||||
if ( ! match ) {
|
||||
return;
|
||||
}
|
||||
|
||||
startOffset = match[1].length;
|
||||
endOffset = offset - p.delimiter.length;
|
||||
|
||||
var before = string.charAt( startOffset - 1 );
|
||||
var after = string.charAt( startOffset + p.delimiter.length );
|
||||
|
||||
// test*test* => format applied.
|
||||
// test *test* => applied.
|
||||
// test* test* => not applied.
|
||||
if ( startOffset && /\S/.test( before ) ) {
|
||||
if ( /\s/.test( after ) || before === delimiterFirstChar ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Do not replace when only whitespace and delimiter characters.
|
||||
if ( ( new RegExp( '^[\\s' + escapeRegExp( delimiterFirstChar ) + ']+$' ) ).test( string.slice( startOffset, endOffset ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
pattern = p;
|
||||
|
||||
return false;
|
||||
} );
|
||||
|
||||
if ( ! pattern ) {
|
||||
return;
|
||||
}
|
||||
|
||||
format = editor.formatter.get( pattern.format );
|
||||
|
||||
if ( format && format[0].inline ) {
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
node.insertData( offset, '\uFEFF' );
|
||||
|
||||
node = node.splitText( startOffset );
|
||||
zero = node.splitText( offset - startOffset );
|
||||
|
||||
node.deleteData( 0, pattern.delimiter.length );
|
||||
node.deleteData( node.data.length - pattern.delimiter.length, pattern.delimiter.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';
|
||||
|
||||
editor.once( 'selectionchange', function() {
|
||||
var offset;
|
||||
|
||||
if ( zero ) {
|
||||
offset = zero.data.indexOf( '\uFEFF' );
|
||||
|
||||
if ( offset !== -1 ) {
|
||||
zero.deleteData( offset, offset + 1 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
function firstTextNode( node ) {
|
||||
var parent = editor.dom.getParent( node, 'p' ),
|
||||
child;
|
||||
|
||||
if ( ! parent ) {
|
||||
return;
|
||||
}
|
||||
|
||||
while ( child = parent.firstChild ) {
|
||||
if ( child.nodeType !== 3 ) {
|
||||
parent = child;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! child ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! child.data ) {
|
||||
if ( child.nextSibling && child.nextSibling.nodeType === 3 ) {
|
||||
child = child.nextSibling;
|
||||
} else {
|
||||
child = null;
|
||||
}
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
function space() {
|
||||
var rng = editor.selection.getRng(),
|
||||
node = rng.startContainer,
|
||||
parent,
|
||||
text;
|
||||
|
||||
if ( ! node || firstTextNode( node ) !== node ) {
|
||||
return;
|
||||
}
|
||||
|
||||
parent = node.parentNode;
|
||||
text = node.data;
|
||||
|
||||
tinymce.each( spacePatterns, function( pattern ) {
|
||||
var match = text.match( pattern.regExp );
|
||||
|
||||
if ( ! match || rng.startOffset !== match[0].length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
node.deleteData( 0, match[0].length );
|
||||
|
||||
if ( ! parent.innerHTML ) {
|
||||
parent.appendChild( document.createElement( 'br' ) );
|
||||
}
|
||||
|
||||
editor.selection.setCursorLocation( parent );
|
||||
editor.execCommand( pattern.cmd );
|
||||
} );
|
||||
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'space';
|
||||
} );
|
||||
|
||||
return false;
|
||||
} );
|
||||
}
|
||||
|
||||
function enter() {
|
||||
var rng = editor.selection.getRng(),
|
||||
start = rng.startContainer,
|
||||
node = firstTextNode( start ),
|
||||
i = enterPatterns.length,
|
||||
text, pattern, parent;
|
||||
|
||||
if ( ! node ) {
|
||||
return;
|
||||
}
|
||||
|
||||
text = node.data;
|
||||
|
||||
while ( i-- ) {
|
||||
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 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( node === start && tinymce.trim( text ) === pattern.start ) {
|
||||
return;
|
||||
}
|
||||
|
||||
editor.once( 'keyup', function() {
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
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 ) {
|
||||
parent = node.parentNode && node.parentNode.parentNode;
|
||||
|
||||
if ( parent ) {
|
||||
parent.replaceChild( document.createElement( pattern.element ), node.parentNode );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'enter';
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
function ltrim( text ) {
|
||||
return text ? text.replace( /^\s+/, '' ) : '';
|
||||
}
|
||||
} );
|
||||
} )( window.tinymce, window.setTimeout );
|
1
wp-includes/js/tinymce/plugins/wptextpattern/plugin.min.js
vendored
Normal file
1
wp-includes/js/tinymce/plugins/wptextpattern/plugin.min.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
!function(u,p){function h(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}u.Env.ie&&u.Env.ie<9||u.PluginManager.add("wptextpattern",function(s){var f,d=u.util.VK,e=s.settings.wptextpattern||{},t=e.space||[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],l=e.enter||[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"},{regExp:/^(-){3,}$/,element:"hr"}],a=e.inline||[{delimiter:"`",format:"code"}];function c(){var r,i,o,t,d,l,e=s.selection.getRng(),n=e.startContainer,c=e.startOffset;n&&3===n.nodeType&&n.data.length&&c&&(d=n.data.slice(0,c),l=n.data.charAt(c-1),u.each(a,function(e){if(l===e.delimiter.slice(-1)){var t=h(e.delimiter),n=e.delimiter.charAt(0),t=new RegExp("(.*)"+t+".+"+t+"$"),t=d.match(t);if(t){r=t[1].length,i=c-e.delimiter.length;var t=d.charAt(r-1),a=d.charAt(r+e.delimiter.length);if(!(r&&/\S/.test(t)&&(/\s/.test(a)||t===n)||new RegExp("^[\\s"+h(n)+"]+$").test(d.slice(r,i))))return o=e,!1}}}),o)&&(e=s.formatter.get(o.format))&&e[0].inline&&(s.undoManager.add(),s.undoManager.transact(function(){n.insertData(c,"\ufeff"),n=n.splitText(r),t=n.splitText(c-r),n.deleteData(0,o.delimiter.length),n.deleteData(n.data.length-o.delimiter.length,o.delimiter.length),s.formatter.apply(o.format,{},n),s.selection.setCursorLocation(t,1)}),p(function(){f="space",s.once("selectionchange",function(){var e;t&&-1!==(e=t.data.indexOf("\ufeff"))&&t.deleteData(e,e+1)})}))}function g(e){var t,n=s.dom.getParent(e,"p");if(n){for(;(t=n.firstChild)&&3!==t.nodeType;)n=t;if(t)return t=t.data?t:t.nextSibling&&3===t.nextSibling.nodeType?t.nextSibling:null}}function m(){var n,a,r=s.selection.getRng(),i=r.startContainer;i&&g(i)===i&&(n=i.parentNode,a=i.data,u.each(t,function(e){var t=a.match(e.regExp);if(t&&r.startOffset===t[0].length)return s.undoManager.add(),s.undoManager.transact(function(){i.deleteData(0,t[0].length),n.innerHTML||n.appendChild(document.createElement("br")),s.selection.setCursorLocation(n),s.execCommand(e.cmd)}),p(function(){f="space"}),!1}))}s.on("selectionchange",function(){f=null}),s.on("keydown",function(e){if((f&&27===e.keyCode||"space"===f&&e.keyCode===d.BACKSPACE)&&(s.undoManager.undo(),e.preventDefault(),e.stopImmediatePropagation()),!d.metaKeyPressed(e))if(e.keyCode===d.ENTER){var t,n,a,r=s.selection.getRng().startContainer,i=g(r),o=l.length;if(i){for(t=i.data;o--;)if(l[o].start){if(0===t.indexOf(l[o].start)){n=l[o];break}}else if(l[o].regExp&&l[o].regExp.test(t)){n=l[o];break}!n||i===r&&u.trim(t)===n.start||s.once("keyup",function(){s.undoManager.add(),s.undoManager.transact(function(){var e;n.format?(s.formatter.apply(n.format,{},i),i.replaceData(0,i.data.length,(e=i.data.slice(n.start.length))?e.replace(/^\s+/,""):"")):n.element&&(a=i.parentNode&&i.parentNode.parentNode)&&a.replaceChild(document.createElement(n.element),i.parentNode)}),p(function(){f="enter"})})}}else e.keyCode===d.SPACEBAR?p(m):47<e.keyCode&&!(91<=e.keyCode&&e.keyCode<=93)&&p(c)},!0)})}(window.tinymce,window.setTimeout);
|
Reference in New Issue
Block a user