plugin.js
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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 );