2012-09-24 02:13:18 +02:00
|
|
|
/**
|
|
|
|
* WordPress View plugin.
|
|
|
|
*/
|
2016-05-17 20:49:30 +02:00
|
|
|
( function( tinymce, wp ) {
|
|
|
|
tinymce.PluginManager.add( 'wpview', function( editor ) {
|
|
|
|
function noop () {}
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2016-05-31 00:05:27 +02:00
|
|
|
if ( ! wp || ! wp.mce || ! wp.mce.views ) {
|
2016-05-17 20:49:30 +02:00
|
|
|
return {
|
|
|
|
getView: noop
|
|
|
|
};
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Check if a node is a view or not.
|
|
|
|
function isView( node ) {
|
|
|
|
return editor.dom.hasClass( node, 'wpview' );
|
2014-07-13 02:34:15 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Replace view tags with their text.
|
|
|
|
function resetViews( content ) {
|
|
|
|
function callback( match, $1 ) {
|
|
|
|
return '<p>' + window.decodeURIComponent( $1 ) + '</p>';
|
2015-03-11 20:12:28 +01:00
|
|
|
}
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( ! content ) {
|
|
|
|
return content;
|
2015-04-18 22:48:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:49:30 +02: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-11 01:36:18 +02:00
|
|
|
}
|
2014-06-12 04:49:16 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Scan new content for matching view patterns and replace them with markers.
|
|
|
|
editor.on( 'beforesetcontent', function( event ) {
|
|
|
|
var node;
|
2015-03-19 08:08:26 +01:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( ! event.selection ) {
|
|
|
|
wp.mce.views.unbind();
|
2015-03-19 08:08:26 +01:00
|
|
|
}
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( ! event.content ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( ! event.load ) {
|
|
|
|
node = editor.selection.getNode();
|
2014-08-10 06:22:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02: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 08:01:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02: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-30 02:54:11 +01:00
|
|
|
}
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2014-08-10 06:22:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
event.content = wp.mce.views.setMarkers( event.content );
|
|
|
|
} );
|
2014-04-22 20:21:16 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Replace any new markers nodes with views.
|
|
|
|
editor.on( 'setcontent', function() {
|
|
|
|
wp.mce.views.render();
|
|
|
|
} );
|
2014-08-19 05:27:17 +02:00
|
|
|
|
2016-05-17 20:49:30 +02: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-19 02:34:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Replace views with their text.
|
|
|
|
editor.on( 'postprocess', function( event ) {
|
|
|
|
event.content = resetViews( event.content );
|
|
|
|
} );
|
2014-07-07 03:21:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02: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-04 05:59:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02: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-19 02:34:15 +02:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// Clean up URLs for easier processing.
|
|
|
|
editor.on( 'pastepreprocess', function( event ) {
|
|
|
|
var content = event.content;
|
2014-08-19 05:27:17 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( content ) {
|
|
|
|
content = tinymce.trim( content.replace( /<[^>]+>/g, '' ) );
|
2014-07-19 02:34:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( /^https?:\/\/\S+$/i.test( content ) ) {
|
|
|
|
event.content = content;
|
2014-07-04 05:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2016-05-17 20:49:30 +02: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-07 03:21:15 +02:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-07-09 00:56:14 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
// See `media` plugin.
|
|
|
|
editor.on( 'click keyup', function() {
|
|
|
|
var node = editor.selection.getNode();
|
2014-03-20 03:48:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( isView( node ) ) {
|
|
|
|
if ( editor.dom.getAttrib( node, 'data-mce-selected' ) ) {
|
|
|
|
node.setAttribute( 'data-mce-selected', '2' );
|
|
|
|
}
|
2014-07-19 02:34:15 +02:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-03-20 03:48:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02: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-19 05:27:17 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( isView( node ) ) {
|
|
|
|
wp.mce.views.edit( editor, node );
|
2014-07-19 02:34:15 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-07-19 02:34:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
editor.addButton( 'wp_view_remove', {
|
|
|
|
tooltip: 'Remove',
|
|
|
|
icon: 'dashicon dashicons-no',
|
|
|
|
onclick: function() {
|
|
|
|
editor.fire( 'cut' );
|
2014-07-19 02:34:15 +02:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-07-04 05:59:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
editor.once( 'preinit', function() {
|
|
|
|
var toolbar;
|
2014-07-08 06:15:14 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
if ( editor.wp && editor.wp._createToolbar ) {
|
|
|
|
toolbar = editor.wp._createToolbar( [
|
|
|
|
'wp_view_edit',
|
|
|
|
'wp_view_remove'
|
|
|
|
] );
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
editor.on( 'wptoolbar', function( event ) {
|
|
|
|
if ( isView( event.element ) ) {
|
|
|
|
event.toolbar = toolbar;
|
2014-08-10 06:22:15 +02:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2016-05-17 20:49:30 +02:00
|
|
|
} );
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
editor.wp = editor.wp || {};
|
|
|
|
editor.wp.getView = noop;
|
|
|
|
editor.wp.setViewCursor = noop;
|
2014-07-16 04:19:15 +02:00
|
|
|
|
2016-05-17 20:49:30 +02:00
|
|
|
return {
|
|
|
|
getView: noop
|
|
|
|
};
|
2015-03-11 20:12:28 +01:00
|
|
|
} );
|
2016-05-17 20:49:30 +02:00
|
|
|
} )( window.tinymce, window.wp );
|