]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wpview/plugin.js
WordPress 4.3
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wpview / plugin.js
1 /* global tinymce */
2
3 /**
4  * WordPress View plugin.
5  */
6 tinymce.PluginManager.add( 'wpview', function( editor ) {
7         var $ = editor.$,
8                 selected,
9                 Env = tinymce.Env,
10                 VK = tinymce.util.VK,
11                 TreeWalker = tinymce.dom.TreeWalker,
12                 toRemove = false,
13                 firstFocus = true,
14                 _noop = function() { return false; },
15                 isios = /iPad|iPod|iPhone/.test( navigator.userAgent ),
16                 cursorInterval,
17                 lastKeyDownNode,
18                 setViewCursorTries,
19                 focus,
20                 execCommandView,
21                 execCommandBefore,
22                 toolbar;
23
24         function getView( node ) {
25                 return getParent( node, 'wpview-wrap' );
26         }
27
28         /**
29          * Returns the node or a parent of the node that has the passed className.
30          * Doing this directly is about 40% faster
31          */
32         function getParent( node, className ) {
33                 while ( node && node.parentNode ) {
34                         if ( node.className && ( ' ' + node.className + ' ' ).indexOf( ' ' + className + ' ' ) !== -1 ) {
35                                 return node;
36                         }
37
38                         node = node.parentNode;
39                 }
40
41                 return false;
42         }
43
44         function _stop( event ) {
45                 event.stopPropagation();
46         }
47
48         function setViewCursor( before, view ) {
49                 var location = before ? 'before' : 'after',
50                         offset = before ? 0 : 1;
51                 deselect();
52                 editor.selection.setCursorLocation( editor.dom.select( '.wpview-selection-' + location, view )[0], offset );
53                 editor.nodeChanged();
54         }
55
56         function handleEnter( view, before, key ) {
57                 var dom = editor.dom,
58                         padNode = dom.create( 'p' );
59
60                 if ( ! ( Env.ie && Env.ie < 11 ) ) {
61                         padNode.innerHTML = '<br data-mce-bogus="1">';
62                 }
63
64                 if ( before ) {
65                         view.parentNode.insertBefore( padNode, view );
66                 } else {
67                         dom.insertAfter( padNode, view );
68                 }
69
70                 deselect();
71
72                 if ( before && key === VK.ENTER ) {
73                         setViewCursor( before, view );
74                 } else {
75                         editor.selection.setCursorLocation( padNode, 0 );
76                 }
77
78                 editor.nodeChanged();
79         }
80
81         function removeView( view ) {
82                 editor.undoManager.transact( function() {
83                         handleEnter( view );
84                         wp.mce.views.remove( editor, view );
85                 });
86         }
87
88         function select( viewNode ) {
89                 var clipboard,
90                         dom = editor.dom;
91
92                 if ( ! viewNode ) {
93                         return;
94                 }
95
96                 if ( viewNode !== selected ) {
97                         // Make sure that the editor is focused.
98                         // It is possible that the editor is not focused when the mouse event fires
99                         // without focus, the selection will not work properly.
100                         editor.getBody().focus();
101
102                         deselect();
103                         selected = viewNode;
104                         dom.setAttrib( viewNode, 'data-mce-selected', 1 );
105
106                         clipboard = dom.create( 'div', {
107                                 'class': 'wpview-clipboard',
108                                 'contenteditable': 'true'
109                         }, wp.mce.views.getText( viewNode ) );
110
111                         editor.dom.select( '.wpview-body', viewNode )[0].appendChild( clipboard );
112
113                         // Both of the following are necessary to prevent manipulating the selection/focus
114                         dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
115                         dom.bind( selected, 'beforedeactivate focusin focusout', _stop );
116
117                         // select the hidden div
118                         if ( isios ) {
119                                 editor.selection.select( clipboard );
120                         } else {
121                                 editor.selection.select( clipboard, true );
122                         }
123                 }
124
125                 editor.nodeChanged();
126                 editor.fire( 'wpview-selected', viewNode );
127         }
128
129         /**
130          * Deselect a selected view and remove clipboard
131          */
132         function deselect() {
133                 var clipboard,
134                         dom = editor.dom;
135
136                 if ( selected ) {
137                         clipboard = editor.dom.select( '.wpview-clipboard', selected )[0];
138                         dom.unbind( clipboard );
139                         dom.remove( clipboard );
140
141                         dom.unbind( selected, 'beforedeactivate focusin focusout click mouseup', _stop );
142                         dom.setAttrib( selected, 'data-mce-selected', null );
143                 }
144
145                 selected = null;
146         }
147
148         // Check if the `wp.mce` API exists.
149         if ( typeof wp === 'undefined' || ! wp.mce ) {
150                 return {
151                         getView: _noop
152                 };
153         }
154
155         function resetViewsCallback( match, viewText ) {
156                 return '<p>' + window.decodeURIComponent( viewText ) + '</p>';
157         }
158
159         // Replace the view tags with the view string
160         function resetViews( content ) {
161                 return content.replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:[\s\S]+?wpview-selection-after[^>]+>[^<>]*<\/p>\s*|\.)<\/div>/g, resetViewsCallback )
162                         .replace( /<p [^>]*?data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, resetViewsCallback );
163         }
164
165         // Prevent adding undo levels on changes inside a view wrapper
166         editor.on( 'BeforeAddUndo', function( event ) {
167                 if ( event.level.content ) {
168                         event.level.content = resetViews( event.level.content );
169                 }
170         });
171
172         // When the editor's content changes, scan the new content for
173         // matching view patterns, and transform the matches into
174         // view wrappers.
175         editor.on( 'BeforeSetContent', function( event ) {
176                 var node;
177
178                 if ( ! event.selection ) {
179                         wp.mce.views.unbind();
180                 }
181
182                 if ( ! event.content ) {
183                         return;
184                 }
185
186                 if ( ! event.load ) {
187                         if ( selected ) {
188                                 removeView( selected );
189                         }
190
191                         node = editor.selection.getNode();
192
193                         if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) {
194                                 // When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh.
195                                 node = editor.dom.getParent( node, 'p' );
196
197                                 if ( node && /^[\s\uFEFF\u00A0]*$/.test( $( node ).text() || '' ) ) {
198                                         // Make sure there are no empty inline elements in the <p>
199                                         node.innerHTML = '';
200                                 } else {
201                                         return;
202                                 }
203                         }
204                 }
205
206                 event.content = wp.mce.views.setMarkers( event.content );
207         });
208
209         // When pasting strip all tags and check if the string is an URL.
210         // Then replace the pasted content with the cleaned URL.
211         editor.on( 'pastePreProcess', function( event ) {
212                 var pastedStr = event.content;
213
214                 if ( pastedStr ) {
215                         pastedStr = tinymce.trim( pastedStr.replace( /<[^>]+>/g, '' ) );
216
217                         if ( /^https?:\/\/\S+$/i.test( pastedStr ) ) {
218                                 event.content = pastedStr;
219                         }
220                 }
221         });
222
223         // When the editor's content has been updated and the DOM has been
224         // processed, render the views in the document.
225         editor.on( 'SetContent', function() {
226                 wp.mce.views.render();
227         });
228
229         // Set the cursor before or after a view when clicking next to it.
230         editor.on( 'click', function( event ) {
231                 var x = event.clientX,
232                         y = event.clientY,
233                         body = editor.getBody(),
234                         bodyRect = body.getBoundingClientRect(),
235                         first = body.firstChild,
236                         last = body.lastChild,
237                         firstRect, lastRect, view;
238
239                 if ( ! first || ! last ) {
240                         return;
241                 }
242
243                 firstRect = first.getBoundingClientRect();
244                 lastRect = last.getBoundingClientRect();
245
246                 if ( y < firstRect.top && ( view = getView( first ) ) ) {
247                         setViewCursor( true, view );
248                         event.preventDefault();
249                 } else if ( y > lastRect.bottom && ( view = getView( last ) ) ) {
250                         setViewCursor( false, view );
251                         event.preventDefault();
252                 } else if ( x < bodyRect.left || x > bodyRect.right ) {
253                         tinymce.each( editor.dom.select( '.wpview-wrap' ), function( view ) {
254                                 var rect = view.getBoundingClientRect();
255
256                                 if ( y < rect.top ) {
257                                         return false;
258                                 }
259
260                                 if ( y >= rect.top && y <= rect.bottom ) {
261                                         if ( x < bodyRect.left ) {
262                                                 setViewCursor( true, view );
263                                                 event.preventDefault();
264                                         } else if ( x > bodyRect.right ) {
265                                                 setViewCursor( false, view );
266                                                 event.preventDefault();
267                                         }
268
269                                         return false;
270                                 }
271                         });
272                 }
273         });
274
275         editor.on( 'init', function() {
276                 var scrolled = false,
277                         selection = editor.selection,
278                         MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
279
280                 // When a view is selected, ensure content that is being pasted
281                 // or inserted is added to a text node (instead of the view).
282                 editor.on( 'BeforeSetContent', function() {
283                         var walker, target,
284                                 view = getView( selection.getNode() );
285
286                         // If the selection is not within a view, bail.
287                         if ( ! view ) {
288                                 return;
289                         }
290
291                         if ( ! view.nextSibling || getView( view.nextSibling ) ) {
292                                 // If there are no additional nodes or the next node is a
293                                 // view, create a text node after the current view.
294                                 target = editor.getDoc().createTextNode('');
295                                 editor.dom.insertAfter( target, view );
296                         } else {
297                                 // Otherwise, find the next text node.
298                                 walker = new TreeWalker( view.nextSibling, view.nextSibling );
299                                 target = walker.next();
300                         }
301
302                         // Select the `target` text node.
303                         selection.select( target );
304                         selection.collapse( true );
305                 });
306
307                 editor.dom.bind( editor.getDoc(), 'touchmove', function() {
308                         scrolled = true;
309                 });
310
311                 editor.on( 'mousedown mouseup click touchend', function( event ) {
312                         var view = getView( event.target );
313
314                         firstFocus = false;
315
316                         // Contain clicks inside the view wrapper
317                         if ( view ) {
318                                 event.stopImmediatePropagation();
319                                 event.preventDefault();
320
321                                 if ( event.type === 'touchend' && scrolled ) {
322                                         scrolled = false;
323                                 } else {
324                                         select( view );
325                                 }
326
327                                 // Returning false stops the ugly bars from appearing in IE11 and stops the view being selected as a range in FF.
328                                 // Unfortunately, it also inhibits the dragging of views to a new location.
329                                 return false;
330                         } else {
331                                 if ( event.type === 'touchend' || event.type === 'mousedown' ) {
332                                         deselect();
333                                 }
334                         }
335
336                         if ( event.type === 'touchend' && scrolled ) {
337                                 scrolled = false;
338                         }
339                 }, true );
340
341                 if ( MutationObserver ) {
342                         new MutationObserver( function() {
343                                 editor.fire( 'wp-body-class-change' );
344                         } )
345                         .observe( editor.getBody(), {
346                                 attributes: true,
347                                 attributeFilter: ['class']
348                         } );
349                 }
350         });
351
352         // Empty the wpview wrap and marker nodes
353         function emptyViewNodes( rootNode ) {
354                 $( 'div[data-wpview-text], p[data-wpview-marker]', rootNode ).each( function( i, node ) {
355                         node.innerHTML = '.';
356                 });
357         }
358
359         // Run that before the DOM cleanup
360         editor.on( 'PreProcess', function( event ) {
361                 emptyViewNodes( event.node );
362         }, true );
363
364         editor.on( 'hide', function() {
365                 wp.mce.views.unbind();
366                 deselect();
367                 emptyViewNodes();
368         });
369
370         editor.on( 'PostProcess', function( event ) {
371                 if ( event.content ) {
372                         event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]+)"[^>]*>[\s\S]*?<\/div>/g, resetViewsCallback )
373                                 .replace( /<p [^>]*?data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, resetViewsCallback );
374                 }
375         });
376
377         // Excludes arrow keys, delete, backspace, enter, space bar.
378         // Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode
379         function isSpecialKey( key ) {
380                 return ( ( key <= 47 && key !== VK.SPACEBAR && key !== VK.ENTER && key !== VK.DELETE && key !== VK.BACKSPACE && ( key < 37 || key > 40 ) ) ||
381                         key >= 224 || // OEM or non-printable
382                         ( key >= 144 && key <= 150 ) || // Num Lock, Scroll Lock, OEM
383                         ( key >= 91 && key <= 93 ) || // Windows keys
384                         ( key >= 112 && key <= 135 ) ); // F keys
385         }
386
387         // (De)select views when arrow keys are used to navigate the content of the editor.
388         editor.on( 'keydown', function( event ) {
389                 var key = event.keyCode,
390                         dom = editor.dom,
391                         selection = editor.selection,
392                         node, view, cursorBefore, cursorAfter,
393                         range, clonedRange, tempRange;
394
395                 if ( selected ) {
396                         // Ignore key presses that involve the command or control key, but continue when in combination with backspace or v.
397                         // Also ignore the F# keys.
398                         if ( ( ( event.metaKey || event.ctrlKey ) && key !== VK.BACKSPACE && key !== 86 ) || ( key >= 112 && key <= 123 ) ) {
399                                 // Remove the view when pressing cmd/ctrl+x on keyup, otherwise the browser can't copy the content.
400                                 if ( ( event.metaKey || event.ctrlKey ) && key === 88 ) {
401                                         toRemove = selected;
402                                 }
403                                 return;
404                         }
405
406                         view = getView( selection.getNode() );
407
408                         // If the caret is not within the selected view, deselect the view and bail.
409                         if ( view !== selected ) {
410                                 deselect();
411                                 return;
412                         }
413
414                         if ( key === VK.LEFT ) {
415                                 setViewCursor( true, view );
416                                 event.preventDefault();
417                         } else if ( key === VK.UP ) {
418                                 if ( view.previousSibling ) {
419                                         if ( getView( view.previousSibling ) ) {
420                                                 setViewCursor( true, view.previousSibling );
421                                         } else {
422                                                 deselect();
423                                                 selection.select( view.previousSibling, true );
424                                                 selection.collapse();
425                                         }
426                                 } else {
427                                         setViewCursor( true, view );
428                                 }
429                                 event.preventDefault();
430                         } else if ( key === VK.RIGHT ) {
431                                 setViewCursor( false, view );
432                                 event.preventDefault();
433                         } else if ( key === VK.DOWN ) {
434                                 if ( view.nextSibling ) {
435                                         if ( getView( view.nextSibling ) ) {
436                                                 setViewCursor( false, view.nextSibling );
437                                         } else {
438                                                 deselect();
439                                                 selection.setCursorLocation( view.nextSibling, 0 );
440                                         }
441                                 } else {
442                                         setViewCursor( false, view );
443                                 }
444
445                                 event.preventDefault();
446                         // Ignore keys that don't insert anything.
447                         } else if ( ! isSpecialKey( key ) ) {
448                                 removeView( selected );
449
450                                 if ( key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) {
451                                         event.preventDefault();
452                                 }
453                         }
454                 } else {
455                         if ( event.metaKey || event.ctrlKey || ( key >= 112 && key <= 123 ) ) {
456                                 return;
457                         }
458
459                         node = selection.getNode();
460                         lastKeyDownNode = node;
461                         view = getView( node );
462
463                         // Make sure we don't delete part of a view.
464                         // If the range ends or starts with the view, we'll need to trim it.
465                         if ( ! selection.isCollapsed() ) {
466                                 range = selection.getRng();
467
468                                 if ( view = getView( range.endContainer ) ) {
469                                         clonedRange = range.cloneRange();
470                                         selection.select( view.previousSibling, true );
471                                         selection.collapse();
472                                         tempRange = selection.getRng();
473                                         clonedRange.setEnd( tempRange.endContainer, tempRange.endOffset );
474                                         selection.setRng( clonedRange );
475                                 } else if ( view = getView( range.startContainer ) ) {
476                                         clonedRange = range.cloneRange();
477                                         clonedRange.setStart( view.nextSibling, 0 );
478                                         selection.setRng( clonedRange );
479                                 }
480                         }
481
482                         if ( ! view ) {
483                                 // Make sure we don't eat any content.
484                                 if ( event.keyCode === VK.BACKSPACE ) {
485                                         if ( editor.dom.isEmpty( node ) ) {
486                                                 if ( view = getView( node.previousSibling ) ) {
487                                                         setViewCursor( false, view );
488                                                         editor.dom.remove( node );
489                                                         event.preventDefault();
490                                                 }
491                                         } else if ( ( range = selection.getRng() ) &&
492                                                         range.startOffset === 0 &&
493                                                         range.endOffset === 0 &&
494                                                         ( view = getView( node.previousSibling ) ) ) {
495                                                 setViewCursor( false, view );
496                                                 event.preventDefault();
497                                         }
498                                 }
499                                 return;
500                         }
501
502                         if ( ! ( ( cursorBefore = dom.hasClass( view, 'wpview-selection-before' ) ) ||
503                                         ( cursorAfter = dom.hasClass( view, 'wpview-selection-after' ) ) ) ) {
504                                 return;
505                         }
506
507                         if ( isSpecialKey( key ) ) {
508                                 // ignore
509                                 return;
510                         }
511
512                         if ( ( cursorAfter && key === VK.UP ) || ( cursorBefore && key === VK.BACKSPACE ) ) {
513                                 if ( view.previousSibling ) {
514                                         if ( getView( view.previousSibling ) ) {
515                                                 setViewCursor( false, view.previousSibling );
516                                         } else {
517                                                 if ( dom.isEmpty( view.previousSibling ) && key === VK.BACKSPACE ) {
518                                                         dom.remove( view.previousSibling );
519                                                 } else {
520                                                         selection.select( view.previousSibling, true );
521                                                         selection.collapse();
522                                                 }
523                                         }
524                                 } else {
525                                         setViewCursor( true, view );
526                                 }
527                                 event.preventDefault();
528                         } else if ( cursorAfter && ( key === VK.DOWN || key === VK.RIGHT ) ) {
529                                 if ( view.nextSibling ) {
530                                         if ( getView( view.nextSibling ) ) {
531                                                 setViewCursor( key === VK.RIGHT, view.nextSibling );
532                                         } else {
533                                                 selection.setCursorLocation( view.nextSibling, 0 );
534                                         }
535                                 }
536                                 event.preventDefault();
537                         } else if ( cursorBefore && ( key === VK.UP || key ===  VK.LEFT ) ) {
538                                 if ( view.previousSibling ) {
539                                         if ( getView( view.previousSibling ) ) {
540                                                 setViewCursor( key === VK.UP, view.previousSibling );
541                                         } else {
542                                                 selection.select( view.previousSibling, true );
543                                                 selection.collapse();
544                                         }
545                                 }
546                                 event.preventDefault();
547                         } else if ( cursorBefore && key === VK.DOWN ) {
548                                 if ( view.nextSibling ) {
549                                         if ( getView( view.nextSibling ) ) {
550                                                 setViewCursor( true, view.nextSibling );
551                                         } else {
552                                                 selection.setCursorLocation( view.nextSibling, 0 );
553                                         }
554                                 } else {
555                                         setViewCursor( false, view );
556                                 }
557                                 event.preventDefault();
558                         } else if ( ( cursorAfter && key === VK.LEFT ) || ( cursorBefore && key === VK.RIGHT ) ) {
559                                 select( view );
560                                 event.preventDefault();
561                         } else if ( cursorAfter && key === VK.BACKSPACE ) {
562                                 removeView( view );
563                                 event.preventDefault();
564                         } else if ( cursorAfter ) {
565                                 handleEnter( view );
566                         } else if ( cursorBefore ) {
567                                 handleEnter( view , true, key );
568                         }
569
570                         if ( key === VK.ENTER ) {
571                                 event.preventDefault();
572                         }
573                 }
574         });
575
576         editor.on( 'keyup', function() {
577                 if ( toRemove ) {
578                         removeView( toRemove );
579                         toRemove = false;
580                 }
581         });
582
583         editor.on( 'focus', function() {
584                 var view;
585
586                 focus = true;
587                 editor.dom.addClass( editor.getBody(), 'has-focus' );
588
589                 // Edge case: show the fake caret when the editor is focused for the first time
590                 // and the first element is a view.
591                 if ( firstFocus && ( view = getView( editor.getBody().firstChild ) ) ) {
592                         setViewCursor( true, view );
593                 }
594
595                 firstFocus = false;
596         } );
597
598         editor.on( 'blur', function() {
599                 focus = false;
600                 editor.dom.removeClass( editor.getBody(), 'has-focus' );
601         } );
602
603         editor.on( 'NodeChange', function( event ) {
604                 var dom = editor.dom,
605                         views = editor.dom.select( '.wpview-wrap' ),
606                         className = event.element.className,
607                         view = getView( event.element ),
608                         lKDN = lastKeyDownNode;
609
610                 lastKeyDownNode = false;
611
612                 clearInterval( cursorInterval );
613
614                 // This runs a lot and is faster than replacing each class separately
615                 tinymce.each( views, function ( view ) {
616                         if ( view.className ) {
617                                 view.className = view.className.replace( / ?\bwpview-(?:selection-before|selection-after|cursor-hide)\b/g, '' );
618                         }
619                 });
620
621                 if ( focus && view ) {
622                         if ( ( className === 'wpview-selection-before' || className === 'wpview-selection-after' ) &&
623                                 editor.selection.isCollapsed() ) {
624
625                                 setViewCursorTries = 0;
626
627                                 deselect();
628
629                                 // Make sure the cursor arrived in the right node.
630                                 // This is necessary for Firefox.
631                                 if ( lKDN === view.previousSibling ) {
632                                         setViewCursor( true, view );
633                                         return;
634                                 } else if ( lKDN === view.nextSibling ) {
635                                         setViewCursor( false, view );
636                                         return;
637                                 }
638
639                                 dom.addClass( view, className );
640
641                                 cursorInterval = setInterval( function() {
642                                         if ( dom.hasClass( view, 'wpview-cursor-hide' ) ) {
643                                                 dom.removeClass( view, 'wpview-cursor-hide' );
644                                         } else {
645                                                 dom.addClass( view, 'wpview-cursor-hide' );
646                                         }
647                                 }, 500 );
648                         // If the cursor lands anywhere else in the view, set the cursor before it.
649                         // Only try this once to prevent a loop. (You never know.)
650                         } else if ( ! getParent( event.element, 'wpview-clipboard' ) && ! setViewCursorTries ) {
651                                 deselect();
652                                 setViewCursorTries++;
653                                 setViewCursor( true, view );
654                         }
655                 }
656         });
657
658         editor.on( 'BeforeExecCommand', function() {
659                 var node = editor.selection.getNode(),
660                         view;
661
662                 if ( node && ( ( execCommandBefore = node.className === 'wpview-selection-before' ) || node.className === 'wpview-selection-after' ) && ( view = getView( node ) ) ) {
663                         handleEnter( view, execCommandBefore );
664                         execCommandView = view;
665                 }
666         });
667
668         editor.on( 'ExecCommand', function() {
669                 var toSelect, node;
670
671                 if ( selected ) {
672                         toSelect = selected;
673                         deselect();
674                         select( toSelect );
675                 }
676
677                 if ( execCommandView ) {
678                         node = execCommandView[ execCommandBefore ? 'previousSibling' : 'nextSibling' ];
679
680                         if ( node && node.nodeName === 'P' && editor.dom.isEmpty( node ) ) {
681                                 editor.dom.remove( node );
682                                 setViewCursor( execCommandBefore, execCommandView );
683                         }
684
685                         execCommandView = false;
686                 }
687         });
688
689         editor.on( 'ResolveName', function( event ) {
690                 if ( editor.dom.hasClass( event.target, 'wpview-wrap' ) ) {
691                         event.name = editor.dom.getAttrib( event.target, 'data-wpview-type' ) || 'wpview';
692                         event.stopPropagation();
693                 } else if ( getView( event.target ) ) {
694                         event.preventDefault();
695                         event.stopPropagation();
696                 }
697         });
698
699         editor.addButton( 'wp_view_edit', {
700                 tooltip: 'Edit ', // trailing space is needed, used for context
701                 icon: 'dashicon dashicons-edit',
702                 onclick: function() {
703                         selected && wp.mce.views.edit( editor, selected );
704                 }
705         } );
706
707         editor.addButton( 'wp_view_remove', {
708                 tooltip: 'Remove',
709                 icon: 'dashicon dashicons-no',
710                 onclick: function() {
711                         selected && removeView( selected );
712                 }
713         } );
714
715         editor.once( 'preinit', function() {
716                 toolbar = editor.wp._createToolbar( [
717                         'wp_view_edit',
718                         'wp_view_remove'
719                 ] );
720         } );
721
722         editor.on( 'wptoolbar', function( event ) {
723                 if ( selected ) {
724                         event.element = selected;
725                         event.toolbar = toolbar;
726                 }
727         } );
728
729         // Add to editor.wp
730         editor.wp = editor.wp || {};
731         editor.wp.getView = getView;
732         editor.wp.setViewCursor = setViewCursor;
733
734         // Keep for back-compat.
735         return {
736                 getView: getView
737         };
738 });