mirror of
https://github.com/KevinMidboe/rohnenedre.git
synced 2025-10-29 17:50:37 +00:00
Initial commit. State 04.2021.
This commit is contained in:
178
wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
Normal file
178
wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
Normal file
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* Text pattern plugin for TinyMCE
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* This plugin can automatically format text patterns as you type. It includes two patterns:
|
||||
* - Unordered list (`* ` and `- `).
|
||||
* - Ordered list (`1. ` and `1) `).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
( 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;
|
||||
|
||||
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 ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
|
||||
watchEnter();
|
||||
}
|
||||
}, 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();
|
||||
}
|
||||
} );
|
||||
|
||||
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 watchEnter() {
|
||||
var rng = editor.selection.getRng(),
|
||||
start = rng.startContainer,
|
||||
node = firstTextNode( 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 && tinymce.trim( text ) === pattern.start ) {
|
||||
return;
|
||||
}
|
||||
|
||||
refNode = node;
|
||||
refPattern = pattern;
|
||||
}
|
||||
|
||||
function enter() {
|
||||
if ( refNode ) {
|
||||
editor.undoManager.add();
|
||||
|
||||
editor.undoManager.transact( function() {
|
||||
editor.formatter.apply( refPattern.format, {}, refNode );
|
||||
refNode.replaceData( 0, refNode.data.length, tinymce.trim( refNode.data.slice( refPattern.start.length ) ) );
|
||||
} );
|
||||
|
||||
// We need to wait for native events to be triggered.
|
||||
setTimeout( function() {
|
||||
canUndo = 'enter';
|
||||
} );
|
||||
}
|
||||
|
||||
refNode = null;
|
||||
refPattern = null;
|
||||
}
|
||||
} );
|
||||
} )( 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(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(),i=g.startContainer;i&&d(i)===i&&(e=i.parentNode,f=i.data,a.each(l,function(a){var d=f.match(a.regExp);if(d&&g.startOffset===d[0].length)return c.undoManager.add(),c.undoManager.transact(function(){i.deleteData(0,d[0].length),e.innerHTML||e.appendChild(document.createElement("br")),c.selection.setCursorLocation(e),c.execCommand(a.cmd)}),b(function(){h="space"}),!1}))}function f(){var b,e,f=c.selection.getRng(),g=f.startContainer,h=d(g),k=m.length;if(h){for(b=h.data;k--;)if(0===b.indexOf(m[k].start)){e=m[k];break}e&&(h!==g||a.trim(b)!==e.start)&&(i=h,j=e)}}function g(){i&&(c.undoManager.add(),c.undoManager.transact(function(){c.formatter.apply(j.format,{},i),i.replaceData(0,i.data.length,a.trim(i.data.slice(j.start.length)))}),b(function(){h="enter"})),i=null,j=null}var h,i,j,k=a.util.VK,l=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],m=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"}];c.on("selectionchange",function(){h=null}),c.on("keydown",function(a){(h&&27===a.keyCode||"space"===h&&a.keyCode===k.BACKSPACE)&&(c.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),a.keyCode!==k.ENTER||k.modifierPressed(a)||f()},!0),c.on("keyup",function(a){a.keyCode!==k.SPACEBAR||a.ctrlKey||a.metaKey||a.altKey?a.keyCode!==k.ENTER||k.modifierPressed(a)||g():e()})})}(window.tinymce,window.setTimeout);
|
||||
Reference in New Issue
Block a user