2012-09-23 20:13:18 -04:00
|
|
|
/**
|
|
|
|
* WordPress View plugin.
|
|
|
|
*/
|
2016-05-17 14:49:30 -04:00
|
|
|
( function( tinymce, wp ) {
|
|
|
|
tinymce.PluginManager.add( 'wpview', function( editor ) {
|
|
|
|
function noop () {}
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2016-05-30 18:05:27 -04:00
|
|
|
if ( ! wp || ! wp.mce || ! wp.mce.views ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
return {
|
|
|
|
getView: noop
|
|
|
|
};
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Check if a node is a view or not.
|
|
|
|
function isView( node ) {
|
|
|
|
return editor.dom.hasClass( node, 'wpview' );
|
2014-07-12 20:34:15 -04:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Replace view tags with their text.
|
|
|
|
function resetViews( content ) {
|
|
|
|
function callback( match, $1 ) {
|
|
|
|
return '<p>' + window.decodeURIComponent( $1 ) + '</p>';
|
2015-03-11 15:12:28 -04:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( ! content ) {
|
|
|
|
return content;
|
2015-04-18 16:48:27 -04:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
return content
|
|
|
|
.replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g, callback )
|
|
|
|
.replace( /<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g, callback );
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2016-07-26 19:13:28 -04:00
|
|
|
editor.on( 'init', function() {
|
|
|
|
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
|
|
|
|
|
|
|
if ( MutationObserver ) {
|
|
|
|
new MutationObserver( function() {
|
|
|
|
editor.fire( 'wp-body-class-change' );
|
|
|
|
} )
|
|
|
|
.observe( editor.getBody(), {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['class']
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass on body class name changes from the editor to the wpView iframes.
|
|
|
|
editor.on( 'wp-body-class-change', function() {
|
|
|
|
var className = editor.getBody().className;
|
|
|
|
|
|
|
|
editor.$( 'iframe[class="wpview-sandbox"]' ).each( function( i, iframe ) {
|
|
|
|
// Make sure it is a local iframe
|
|
|
|
// jshint scripturl: true
|
|
|
|
if ( ! iframe.src || iframe.src === 'javascript:""' ) {
|
|
|
|
try {
|
|
|
|
iframe.contentWindow.document.body.className = className;
|
|
|
|
} catch( er ) {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} );
|
|
|
|
});
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Scan new content for matching view patterns and replace them with markers.
|
|
|
|
editor.on( 'beforesetcontent', function( event ) {
|
|
|
|
var node;
|
2015-03-19 03:08:26 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( ! event.selection ) {
|
|
|
|
wp.mce.views.unbind();
|
2015-03-19 03:08:26 -04:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( ! event.content ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( ! event.load ) {
|
|
|
|
node = editor.selection.getNode();
|
2014-08-10 00:22:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) {
|
|
|
|
// When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh.
|
|
|
|
node = editor.dom.getParent( node, 'p' );
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( node && /^[\s\uFEFF\u00A0]*$/.test( editor.$( node ).text() || '' ) ) {
|
|
|
|
// Make sure there are no empty inline elements in the <p>
|
|
|
|
node.innerHTML = '';
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-29 20:54:11 -05:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2014-08-10 00:22:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
event.content = wp.mce.views.setMarkers( event.content );
|
|
|
|
} );
|
2014-04-22 14:21:16 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Replace any new markers nodes with views.
|
|
|
|
editor.on( 'setcontent', function() {
|
|
|
|
wp.mce.views.render();
|
|
|
|
} );
|
2014-08-18 23:27:17 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Empty view nodes for easier processing.
|
|
|
|
editor.on( 'preprocess', function( event ) {
|
|
|
|
editor.$( 'div[data-wpview-text], p[data-wpview-marker]', event.node ).each( function( i, node ) {
|
|
|
|
node.innerHTML = '.';
|
|
|
|
} );
|
|
|
|
}, true );
|
2014-07-18 20:34:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Replace views with their text.
|
|
|
|
editor.on( 'postprocess', function( event ) {
|
|
|
|
event.content = resetViews( event.content );
|
|
|
|
} );
|
2014-07-06 21:21:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Replace views with their text inside undo levels.
|
|
|
|
// This also prevents that new levels are added when there are changes inside the views.
|
|
|
|
editor.on( 'beforeaddundo', function( event ) {
|
|
|
|
event.level.content = resetViews( event.level.content );
|
|
|
|
} );
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Make sure views are copied as their text.
|
|
|
|
editor.on( 'drop objectselected', function( event ) {
|
|
|
|
if ( isView( event.targetClone ) ) {
|
|
|
|
event.targetClone = editor.getDoc().createTextNode(
|
|
|
|
window.decodeURIComponent( editor.dom.getAttrib( event.targetClone, 'data-wpview-text' ) )
|
|
|
|
);
|
2014-07-18 20:34:15 -04:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Clean up URLs for easier processing.
|
|
|
|
editor.on( 'pastepreprocess', function( event ) {
|
|
|
|
var content = event.content;
|
2014-08-18 23:27:17 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( content ) {
|
|
|
|
content = tinymce.trim( content.replace( /<[^>]+>/g, '' ) );
|
2014-07-18 20:34:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( /^https?:\/\/\S+$/i.test( content ) ) {
|
|
|
|
event.content = content;
|
2014-07-03 23:59:15 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// Show the view type in the element path.
|
|
|
|
editor.on( 'resolvename', function( event ) {
|
|
|
|
if ( isView( event.target ) ) {
|
|
|
|
event.name = editor.dom.getAttrib( event.target, 'data-wpview-type' ) || 'object';
|
2014-07-06 21:21:15 -04:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-07-08 18:56:14 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
// See `media` plugin.
|
|
|
|
editor.on( 'click keyup', function() {
|
|
|
|
var node = editor.selection.getNode();
|
2014-03-19 22:48:14 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( isView( node ) ) {
|
|
|
|
if ( editor.dom.getAttrib( node, 'data-mce-selected' ) ) {
|
|
|
|
node.setAttribute( 'data-mce-selected', '2' );
|
|
|
|
}
|
2014-07-18 20:34:15 -04:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-03-19 22:48:14 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.addButton( 'wp_view_edit', {
|
|
|
|
tooltip: 'Edit ', // trailing space is needed, used for context
|
|
|
|
icon: 'dashicon dashicons-edit',
|
|
|
|
onclick: function() {
|
|
|
|
var node = editor.selection.getNode();
|
2014-08-18 23:27:17 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( isView( node ) ) {
|
|
|
|
wp.mce.views.edit( editor, node );
|
2014-07-18 20:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-07-18 20:34:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.addButton( 'wp_view_remove', {
|
|
|
|
tooltip: 'Remove',
|
|
|
|
icon: 'dashicon dashicons-no',
|
|
|
|
onclick: function() {
|
|
|
|
editor.fire( 'cut' );
|
2014-07-18 20:34:15 -04:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-07-03 23:59:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.once( 'preinit', function() {
|
|
|
|
var toolbar;
|
2014-07-08 00:15:14 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
if ( editor.wp && editor.wp._createToolbar ) {
|
|
|
|
toolbar = editor.wp._createToolbar( [
|
|
|
|
'wp_view_edit',
|
|
|
|
'wp_view_remove'
|
|
|
|
] );
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.on( 'wptoolbar', function( event ) {
|
|
|
|
if ( isView( event.element ) ) {
|
|
|
|
event.toolbar = toolbar;
|
2014-08-10 00:22:15 -04:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2016-05-17 14:49:30 -04:00
|
|
|
} );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.wp = editor.wp || {};
|
|
|
|
editor.wp.getView = noop;
|
|
|
|
editor.wp.setViewCursor = noop;
|
2014-07-15 22:19:15 -04:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
return {
|
|
|
|
getView: noop
|
|
|
|
};
|
2015-03-11 15:12:28 -04:00
|
|
|
} );
|
2016-05-17 14:49:30 -04:00
|
|
|
} )( window.tinymce, window.wp );
|