Edit File by line
/home/barbar84/www/wp-inclu.../js/tinymce/plugins/wptextpa...
File: plugin.js
/**
[0] Fix | Delete
* Text pattern plugin for TinyMCE
[1] Fix | Delete
*
[2] Fix | Delete
* @since 4.3.0
[3] Fix | Delete
*
[4] Fix | Delete
* This plugin can automatically format text patterns as you type. It includes several groups of patterns.
[5] Fix | Delete
*
[6] Fix | Delete
* Start of line patterns:
[7] Fix | Delete
* As-you-type:
[8] Fix | Delete
* - Unordered list (`* ` and `- `).
[9] Fix | Delete
* - Ordered list (`1. ` and `1) `).
[10] Fix | Delete
*
[11] Fix | Delete
* On enter:
[12] Fix | Delete
* - h2 (## ).
[13] Fix | Delete
* - h3 (### ).
[14] Fix | Delete
* - h4 (#### ).
[15] Fix | Delete
* - h5 (##### ).
[16] Fix | Delete
* - h6 (###### ).
[17] Fix | Delete
* - blockquote (> ).
[18] Fix | Delete
* - hr (---).
[19] Fix | Delete
*
[20] Fix | Delete
* Inline patterns:
[21] Fix | Delete
* - <code> (`) (backtick).
[22] Fix | Delete
*
[23] Fix | Delete
* If the transformation in unwanted, the user can undo the change by pressing backspace,
[24] Fix | Delete
* using the undo shortcut, or the undo button in the toolbar.
[25] Fix | Delete
*
[26] Fix | Delete
* Setting for the patterns can be overridden by plugins by using the `tiny_mce_before_init` PHP filter.
[27] Fix | Delete
* The setting name is `wptextpattern` and the value is an object containing override arrays for each
[28] Fix | Delete
* patterns group. There are three groups: "space", "enter", and "inline". Example (PHP):
[29] Fix | Delete
*
[30] Fix | Delete
* add_filter( 'tiny_mce_before_init', 'my_mce_init_wptextpattern' );
[31] Fix | Delete
* function my_mce_init_wptextpattern( $init ) {
[32] Fix | Delete
* $init['wptextpattern'] = wp_json_encode( array(
[33] Fix | Delete
* 'inline' => array(
[34] Fix | Delete
* array( 'delimiter' => '**', 'format' => 'bold' ),
[35] Fix | Delete
* array( 'delimiter' => '__', 'format' => 'italic' ),
[36] Fix | Delete
* ),
[37] Fix | Delete
* ) );
[38] Fix | Delete
*
[39] Fix | Delete
* return $init;
[40] Fix | Delete
* }
[41] Fix | Delete
*
[42] Fix | Delete
* Note that setting this will override the default text patterns. You will need to include them
[43] Fix | Delete
* in your settings array if you want to keep them working.
[44] Fix | Delete
*/
[45] Fix | Delete
( function( tinymce, setTimeout ) {
[46] Fix | Delete
if ( tinymce.Env.ie && tinymce.Env.ie < 9 ) {
[47] Fix | Delete
return;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Escapes characters for use in a Regular Expression.
[52] Fix | Delete
*
[53] Fix | Delete
* @param {String} string Characters to escape
[54] Fix | Delete
*
[55] Fix | Delete
* @return {String} Escaped characters
[56] Fix | Delete
*/
[57] Fix | Delete
function escapeRegExp( string ) {
[58] Fix | Delete
return string.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
[62] Fix | Delete
var VK = tinymce.util.VK;
[63] Fix | Delete
var settings = editor.settings.wptextpattern || {};
[64] Fix | Delete
[65] Fix | Delete
var spacePatterns = settings.space || [
[66] Fix | Delete
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
[67] Fix | Delete
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
[68] Fix | Delete
];
[69] Fix | Delete
[70] Fix | Delete
var enterPatterns = settings.enter || [
[71] Fix | Delete
{ start: '##', format: 'h2' },
[72] Fix | Delete
{ start: '###', format: 'h3' },
[73] Fix | Delete
{ start: '####', format: 'h4' },
[74] Fix | Delete
{ start: '#####', format: 'h5' },
[75] Fix | Delete
{ start: '######', format: 'h6' },
[76] Fix | Delete
{ start: '>', format: 'blockquote' },
[77] Fix | Delete
{ regExp: /^(-){3,}$/, element: 'hr' }
[78] Fix | Delete
];
[79] Fix | Delete
[80] Fix | Delete
var inlinePatterns = settings.inline || [
[81] Fix | Delete
{ delimiter: '`', format: 'code' }
[82] Fix | Delete
];
[83] Fix | Delete
[84] Fix | Delete
var canUndo;
[85] Fix | Delete
[86] Fix | Delete
editor.on( 'selectionchange', function() {
[87] Fix | Delete
canUndo = null;
[88] Fix | Delete
} );
[89] Fix | Delete
[90] Fix | Delete
editor.on( 'keydown', function( event ) {
[91] Fix | Delete
if ( ( canUndo && event.keyCode === 27 /* ESCAPE */ ) || ( canUndo === 'space' && event.keyCode === VK.BACKSPACE ) ) {
[92] Fix | Delete
editor.undoManager.undo();
[93] Fix | Delete
event.preventDefault();
[94] Fix | Delete
event.stopImmediatePropagation();
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
if ( VK.metaKeyPressed( event ) ) {
[98] Fix | Delete
return;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
if ( event.keyCode === VK.ENTER ) {
[102] Fix | Delete
enter();
[103] Fix | Delete
// Wait for the browser to insert the character.
[104] Fix | Delete
} else if ( event.keyCode === VK.SPACEBAR ) {
[105] Fix | Delete
setTimeout( space );
[106] Fix | Delete
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
[107] Fix | Delete
setTimeout( inline );
[108] Fix | Delete
}
[109] Fix | Delete
}, true );
[110] Fix | Delete
[111] Fix | Delete
function inline() {
[112] Fix | Delete
var rng = editor.selection.getRng();
[113] Fix | Delete
var node = rng.startContainer;
[114] Fix | Delete
var offset = rng.startOffset;
[115] Fix | Delete
var startOffset;
[116] Fix | Delete
var endOffset;
[117] Fix | Delete
var pattern;
[118] Fix | Delete
var format;
[119] Fix | Delete
var zero;
[120] Fix | Delete
[121] Fix | Delete
// We need a non-empty text node with an offset greater than zero.
[122] Fix | Delete
if ( ! node || node.nodeType !== 3 || ! node.data.length || ! offset ) {
[123] Fix | Delete
return;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
var string = node.data.slice( 0, offset );
[127] Fix | Delete
var lastChar = node.data.charAt( offset - 1 );
[128] Fix | Delete
[129] Fix | Delete
tinymce.each( inlinePatterns, function( p ) {
[130] Fix | Delete
// Character before selection should be delimiter.
[131] Fix | Delete
if ( lastChar !== p.delimiter.slice( -1 ) ) {
[132] Fix | Delete
return;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
var escDelimiter = escapeRegExp( p.delimiter );
[136] Fix | Delete
var delimiterFirstChar = p.delimiter.charAt( 0 );
[137] Fix | Delete
var regExp = new RegExp( '(.*)' + escDelimiter + '.+' + escDelimiter + '$' );
[138] Fix | Delete
var match = string.match( regExp );
[139] Fix | Delete
[140] Fix | Delete
if ( ! match ) {
[141] Fix | Delete
return;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
startOffset = match[1].length;
[145] Fix | Delete
endOffset = offset - p.delimiter.length;
[146] Fix | Delete
[147] Fix | Delete
var before = string.charAt( startOffset - 1 );
[148] Fix | Delete
var after = string.charAt( startOffset + p.delimiter.length );
[149] Fix | Delete
[150] Fix | Delete
// test*test* => format applied.
[151] Fix | Delete
// test *test* => applied.
[152] Fix | Delete
// test* test* => not applied.
[153] Fix | Delete
if ( startOffset && /\S/.test( before ) ) {
[154] Fix | Delete
if ( /\s/.test( after ) || before === delimiterFirstChar ) {
[155] Fix | Delete
return;
[156] Fix | Delete
}
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
// Do not replace when only whitespace and delimiter characters.
[160] Fix | Delete
if ( ( new RegExp( '^[\\s' + escapeRegExp( delimiterFirstChar ) + ']+$' ) ).test( string.slice( startOffset, endOffset ) ) ) {
[161] Fix | Delete
return;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
pattern = p;
[165] Fix | Delete
[166] Fix | Delete
return false;
[167] Fix | Delete
} );
[168] Fix | Delete
[169] Fix | Delete
if ( ! pattern ) {
[170] Fix | Delete
return;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
format = editor.formatter.get( pattern.format );
[174] Fix | Delete
[175] Fix | Delete
if ( format && format[0].inline ) {
[176] Fix | Delete
editor.undoManager.add();
[177] Fix | Delete
[178] Fix | Delete
editor.undoManager.transact( function() {
[179] Fix | Delete
node.insertData( offset, '\uFEFF' );
[180] Fix | Delete
[181] Fix | Delete
node = node.splitText( startOffset );
[182] Fix | Delete
zero = node.splitText( offset - startOffset );
[183] Fix | Delete
[184] Fix | Delete
node.deleteData( 0, pattern.delimiter.length );
[185] Fix | Delete
node.deleteData( node.data.length - pattern.delimiter.length, pattern.delimiter.length );
[186] Fix | Delete
[187] Fix | Delete
editor.formatter.apply( pattern.format, {}, node );
[188] Fix | Delete
[189] Fix | Delete
editor.selection.setCursorLocation( zero, 1 );
[190] Fix | Delete
} );
[191] Fix | Delete
[192] Fix | Delete
// We need to wait for native events to be triggered.
[193] Fix | Delete
setTimeout( function() {
[194] Fix | Delete
canUndo = 'space';
[195] Fix | Delete
[196] Fix | Delete
editor.once( 'selectionchange', function() {
[197] Fix | Delete
var offset;
[198] Fix | Delete
[199] Fix | Delete
if ( zero ) {
[200] Fix | Delete
offset = zero.data.indexOf( '\uFEFF' );
[201] Fix | Delete
[202] Fix | Delete
if ( offset !== -1 ) {
[203] Fix | Delete
zero.deleteData( offset, offset + 1 );
[204] Fix | Delete
}
[205] Fix | Delete
}
[206] Fix | Delete
} );
[207] Fix | Delete
} );
[208] Fix | Delete
}
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
function firstTextNode( node ) {
[212] Fix | Delete
var parent = editor.dom.getParent( node, 'p' ),
[213] Fix | Delete
child;
[214] Fix | Delete
[215] Fix | Delete
if ( ! parent ) {
[216] Fix | Delete
return;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
while ( child = parent.firstChild ) {
[220] Fix | Delete
if ( child.nodeType !== 3 ) {
[221] Fix | Delete
parent = child;
[222] Fix | Delete
} else {
[223] Fix | Delete
break;
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
if ( ! child ) {
[228] Fix | Delete
return;
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
if ( ! child.data ) {
[232] Fix | Delete
if ( child.nextSibling && child.nextSibling.nodeType === 3 ) {
[233] Fix | Delete
child = child.nextSibling;
[234] Fix | Delete
} else {
[235] Fix | Delete
child = null;
[236] Fix | Delete
}
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
return child;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
function space() {
[243] Fix | Delete
var rng = editor.selection.getRng(),
[244] Fix | Delete
node = rng.startContainer,
[245] Fix | Delete
parent,
[246] Fix | Delete
text;
[247] Fix | Delete
[248] Fix | Delete
if ( ! node || firstTextNode( node ) !== node ) {
[249] Fix | Delete
return;
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
parent = node.parentNode;
[253] Fix | Delete
text = node.data;
[254] Fix | Delete
[255] Fix | Delete
tinymce.each( spacePatterns, function( pattern ) {
[256] Fix | Delete
var match = text.match( pattern.regExp );
[257] Fix | Delete
[258] Fix | Delete
if ( ! match || rng.startOffset !== match[0].length ) {
[259] Fix | Delete
return;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
editor.undoManager.add();
[263] Fix | Delete
[264] Fix | Delete
editor.undoManager.transact( function() {
[265] Fix | Delete
node.deleteData( 0, match[0].length );
[266] Fix | Delete
[267] Fix | Delete
if ( ! parent.innerHTML ) {
[268] Fix | Delete
parent.appendChild( document.createElement( 'br' ) );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
editor.selection.setCursorLocation( parent );
[272] Fix | Delete
editor.execCommand( pattern.cmd );
[273] Fix | Delete
} );
[274] Fix | Delete
[275] Fix | Delete
// We need to wait for native events to be triggered.
[276] Fix | Delete
setTimeout( function() {
[277] Fix | Delete
canUndo = 'space';
[278] Fix | Delete
} );
[279] Fix | Delete
[280] Fix | Delete
return false;
[281] Fix | Delete
} );
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
function enter() {
[285] Fix | Delete
var rng = editor.selection.getRng(),
[286] Fix | Delete
start = rng.startContainer,
[287] Fix | Delete
node = firstTextNode( start ),
[288] Fix | Delete
i = enterPatterns.length,
[289] Fix | Delete
text, pattern, parent;
[290] Fix | Delete
[291] Fix | Delete
if ( ! node ) {
[292] Fix | Delete
return;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
text = node.data;
[296] Fix | Delete
[297] Fix | Delete
while ( i-- ) {
[298] Fix | Delete
if ( enterPatterns[ i ].start ) {
[299] Fix | Delete
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
[300] Fix | Delete
pattern = enterPatterns[ i ];
[301] Fix | Delete
break;
[302] Fix | Delete
}
[303] Fix | Delete
} else if ( enterPatterns[ i ].regExp ) {
[304] Fix | Delete
if ( enterPatterns[ i ].regExp.test( text ) ) {
[305] Fix | Delete
pattern = enterPatterns[ i ];
[306] Fix | Delete
break;
[307] Fix | Delete
}
[308] Fix | Delete
}
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
if ( ! pattern ) {
[312] Fix | Delete
return;
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
if ( node === start && tinymce.trim( text ) === pattern.start ) {
[316] Fix | Delete
return;
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
editor.once( 'keyup', function() {
[320] Fix | Delete
editor.undoManager.add();
[321] Fix | Delete
[322] Fix | Delete
editor.undoManager.transact( function() {
[323] Fix | Delete
if ( pattern.format ) {
[324] Fix | Delete
editor.formatter.apply( pattern.format, {}, node );
[325] Fix | Delete
node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) );
[326] Fix | Delete
} else if ( pattern.element ) {
[327] Fix | Delete
parent = node.parentNode && node.parentNode.parentNode;
[328] Fix | Delete
[329] Fix | Delete
if ( parent ) {
[330] Fix | Delete
parent.replaceChild( document.createElement( pattern.element ), node.parentNode );
[331] Fix | Delete
}
[332] Fix | Delete
}
[333] Fix | Delete
} );
[334] Fix | Delete
[335] Fix | Delete
// We need to wait for native events to be triggered.
[336] Fix | Delete
setTimeout( function() {
[337] Fix | Delete
canUndo = 'enter';
[338] Fix | Delete
} );
[339] Fix | Delete
} );
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
function ltrim( text ) {
[343] Fix | Delete
return text ? text.replace( /^\s+/, '' ) : '';
[344] Fix | Delete
}
[345] Fix | Delete
} );
[346] Fix | Delete
} )( window.tinymce, window.setTimeout );
[347] Fix | Delete
[348] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function