WordPress/wp-includes/js/tinymce/plugins/wpemoji/plugin.js
Andrew Ozz a7fd4a3774 Emoji:
- Add the styling for the replacement images to the admin CSS.
- Revert to using `.emoji` as replacement image class.
- When pasting in the editor, convert emoji images to our format so we can replace them with chars on saving.
- Some more clean up of both the plugin and wp-emoji.js.
See #31242.
Built from https://develop.svn.wordpress.org/trunk@31786


git-svn-id: http://core.svn.wordpress.org/trunk@31766 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-03-15 23:16:29 +00:00

89 lines
2.4 KiB
JavaScript

( function( tinymce, wp, twemoji ) {
tinymce.PluginManager.add( 'wpemoji', function( editor ) {
var typing,
isMacWebKit = tinymce.Env.mac && tinymce.Env.webkit;
if ( ! wp || ! wp.emoji || ! wp.emoji.replaceEmoji ) {
return;
}
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 );
}
editor.on( 'keydown keyup', function( event ) {
typing = event.type === 'keydown';
} );
editor.on( 'input', function( event ) {
if ( typing ) {
return;
}
var bookmark,
selection = editor.selection,
node = selection.getNode();
if ( twemoji.test( node.textContent || node.innerText ) ) {
if ( isMacWebKit ) {
bookmark = selection.getBookmark();
}
replaceEmoji( node );
if ( isMacWebKit ) {
selection.moveToBookmark( bookmark );
}
}
});
editor.on( 'setcontent', function( event ) {
var selection = editor.selection,
node = selection.getNode();
if ( twemoji.test( node.textContent || node.innerText ) ) {
replaceEmoji( node );
// 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 );
}
}
} );
// 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 );
}
});
}
});
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();
}
} );
} );
} )( window.tinymce, window.wp, window.twemoji );