2015-03-15 19:16:29 -04:00
|
|
|
( function( tinymce, wp, twemoji ) {
|
2015-03-14 19:49:30 -04:00
|
|
|
tinymce.PluginManager.add( 'wpemoji', function( editor ) {
|
2015-03-12 19:03:26 -04:00
|
|
|
var typing,
|
2015-03-12 19:00:27 -04:00
|
|
|
isMacWebKit = tinymce.Env.mac && tinymce.Env.webkit;
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
if ( ! wp || ! wp.emoji || ! wp.emoji.replaceEmoji ) {
|
2015-03-11 18:49:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
function setImgAttr( image ) {
|
|
|
|
image.className = 'emoji';
|
|
|
|
image.setAttribute( 'data-mce-resize', 'false' );
|
|
|
|
image.setAttribute( 'data-mce-placeholder', '1' );
|
|
|
|
image.setAttribute( 'data-wp-emoji', image.alt );
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceEmoji( node ) {
|
|
|
|
wp.emoji.parse( node, { className: 'emoji _inserted-emoji' } );
|
|
|
|
tinymce.each( editor.dom.$( 'img._inserted-emoji', node ), setImgAttr );
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
editor.on( 'keydown keyup', function( event ) {
|
|
|
|
typing = event.type === 'keydown';
|
|
|
|
} );
|
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
editor.on( 'input', function( event ) {
|
|
|
|
if ( typing ) {
|
2015-03-11 18:49:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
var bookmark,
|
|
|
|
selection = editor.selection,
|
|
|
|
node = selection.getNode();
|
2015-03-12 19:00:27 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
if ( twemoji.test( node.textContent || node.innerText ) ) {
|
|
|
|
if ( isMacWebKit ) {
|
|
|
|
bookmark = selection.getBookmark();
|
|
|
|
}
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
replaceEmoji( node );
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
if ( isMacWebKit ) {
|
|
|
|
selection.moveToBookmark( bookmark );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
editor.on( 'setcontent', function( event ) {
|
|
|
|
var selection = editor.selection,
|
|
|
|
node = selection.getNode();
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
if ( twemoji.test( node.textContent || node.innerText ) ) {
|
|
|
|
replaceEmoji( node );
|
2015-03-12 19:00:27 -04:00
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
// In IE all content in the editor is left selected aftrer wp.emoji.parse()...
|
|
|
|
// Collapse the selection to the beginning.
|
|
|
|
if ( tinymce.Env.ie && tinymce.Env.ie < 9 && event.load && node && node.nodeName === 'BODY' ) {
|
|
|
|
selection.collapse( true );
|
|
|
|
}
|
2015-03-12 19:00:27 -04:00
|
|
|
}
|
2015-03-11 18:49:28 -04:00
|
|
|
} );
|
|
|
|
|
2015-03-15 19:16:29 -04:00
|
|
|
// Convert Twemoji compatible pasted emoji repacement images into our format.
|
|
|
|
editor.on( 'PastePostProcess', function( event ) {
|
|
|
|
if ( twemoji ) {
|
|
|
|
tinymce.each( editor.dom.$( 'img.emoji', event.node ), function( image ) {
|
|
|
|
if ( image.alt && twemoji.test( image.alt ) ) {
|
|
|
|
setImgAttr( image );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
editor.on( 'postprocess', function( event ) {
|
|
|
|
if ( event.content ) {
|
|
|
|
event.content = event.content.replace( /<img[^>]+data-wp-emoji="([^"]+)"[^>]*>/g, function( match, emoji ) {
|
|
|
|
return emoji;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.on( 'resolvename', function( event ) {
|
|
|
|
if ( event.target.nodeName === 'IMG' && editor.dom.getAttrib( event.target, 'data-wp-emoji' ) ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
2015-03-15 19:16:29 -04:00
|
|
|
} )( window.tinymce, window.wp, window.twemoji );
|