2 * WordPress View plugin.
4 ( function( tinymce, wp ) {
5 tinymce.PluginManager.add( 'wpview', function( editor ) {
8 if ( ! wp || ! wp.mce || ! wp.mce.views ) {
14 // Check if a node is a view or not.
15 function isView( node ) {
16 return editor.dom.hasClass( node, 'wpview' );
19 // Replace view tags with their text.
20 function resetViews( content ) {
21 function callback( match, $1 ) {
22 return '<p>' + window.decodeURIComponent( $1 ) + '</p>';
30 .replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g, callback )
31 .replace( /<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, callback );
34 editor.on( 'init', function() {
35 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
37 if ( MutationObserver ) {
38 new MutationObserver( function() {
39 editor.fire( 'wp-body-class-change' );
41 .observe( editor.getBody(), {
43 attributeFilter: ['class']
47 // Pass on body class name changes from the editor to the wpView iframes.
48 editor.on( 'wp-body-class-change', function() {
49 var className = editor.getBody().className;
51 editor.$( 'iframe[class="wpview-sandbox"]' ).each( function( i, iframe ) {
52 // Make sure it is a local iframe
53 // jshint scripturl: true
54 if ( ! iframe.src || iframe.src === 'javascript:""' ) {
56 iframe.contentWindow.document.body.className = className;
63 // Scan new content for matching view patterns and replace them with markers.
64 editor.on( 'beforesetcontent', function( event ) {
67 if ( ! event.selection ) {
68 wp.mce.views.unbind();
71 if ( ! event.content ) {
76 node = editor.selection.getNode();
78 if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) {
79 // When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh.
80 node = editor.dom.getParent( node, 'p' );
82 if ( node && /^[\s\uFEFF\u00A0]*$/.test( editor.$( node ).text() || '' ) ) {
83 // Make sure there are no empty inline elements in the <p>
91 event.content = wp.mce.views.setMarkers( event.content );
94 // Replace any new markers nodes with views.
95 editor.on( 'setcontent', function( event ) {
96 if ( event.load && ! event.initial && editor.quirks.refreshContentEditable ) {
97 // Make sure there is a selection in Gecko browsers.
98 // Or it will refresh the content internally which resets the iframes.
99 editor.quirks.refreshContentEditable();
102 wp.mce.views.render();
105 // Empty view nodes for easier processing.
106 editor.on( 'preprocess hide', function( event ) {
107 editor.$( 'div[data-wpview-text], p[data-wpview-marker]', event.node ).each( function( i, node ) {
108 node.innerHTML = '.';
112 // Replace views with their text.
113 editor.on( 'postprocess', function( event ) {
114 event.content = resetViews( event.content );
117 // Replace views with their text inside undo levels.
118 // This also prevents that new levels are added when there are changes inside the views.
119 editor.on( 'beforeaddundo', function( event ) {
120 event.level.content = resetViews( event.level.content );
123 // Make sure views are copied as their text.
124 editor.on( 'drop objectselected', function( event ) {
125 if ( isView( event.targetClone ) ) {
126 event.targetClone = editor.getDoc().createTextNode(
127 window.decodeURIComponent( editor.dom.getAttrib( event.targetClone, 'data-wpview-text' ) )
132 // Clean up URLs for easier processing.
133 editor.on( 'pastepreprocess', function( event ) {
134 var content = event.content;
137 content = tinymce.trim( content.replace( /<[^>]+>/g, '' ) );
139 if ( /^https?:\/\/\S+$/i.test( content ) ) {
140 event.content = content;
145 // Show the view type in the element path.
146 editor.on( 'resolvename', function( event ) {
147 if ( isView( event.target ) ) {
148 event.name = editor.dom.getAttrib( event.target, 'data-wpview-type' ) || 'object';
152 // See `media` plugin.
153 editor.on( 'click keyup', function() {
154 var node = editor.selection.getNode();
156 if ( isView( node ) ) {
157 if ( editor.dom.getAttrib( node, 'data-mce-selected' ) ) {
158 node.setAttribute( 'data-mce-selected', '2' );
163 editor.addButton( 'wp_view_edit', {
164 tooltip: 'Edit ', // trailing space is needed, used for context
165 icon: 'dashicon dashicons-edit',
166 onclick: function() {
167 var node = editor.selection.getNode();
169 if ( isView( node ) ) {
170 wp.mce.views.edit( editor, node );
175 editor.addButton( 'wp_view_remove', {
177 icon: 'dashicon dashicons-no',
178 onclick: function() {
179 editor.fire( 'cut' );
183 editor.once( 'preinit', function() {
186 if ( editor.wp && editor.wp._createToolbar ) {
187 toolbar = editor.wp._createToolbar( [
192 editor.on( 'wptoolbar', function( event ) {
193 if ( isView( event.element ) ) {
194 event.toolbar = toolbar;
200 editor.wp = editor.wp || {};
201 editor.wp.getView = noop;
202 editor.wp.setViewCursor = noop;
208 } )( window.tinymce, window.wp );