Make WordPress Core

Ticket #22407: 22407.patch

File 22407.patch, 3.0 KB (added by azaozz, 12 years ago)
  • wp-includes/js/tinymce/plugins/wpview/editor_plugin_src.js

     
    1111                init : function( editor, url ) {
    1212                        var wpView = this;
    1313
     14                        this.editor = editor;
     15
    1416                        // Check if the `wp.mce` API exists.
    1517                        if ( typeof wp === 'undefined' || ! wp.mce )
    1618                                return;
     
    1820                        editor.onPreInit.add( function( editor ) {
    1921                                // Add elements so we can set `contenteditable` to false.
    2022                                editor.schema.addValidElements('div[*],span[*]');
     23
     24                                editor.undoManager.onBeforeAdd.addToTop(function(undoManager, o){
     25                                        o.content = wp.mce.view.toText( o.content );
     26
     27                                        console.log( 'undo.onBeforeAdd = '+o.content )
     28                                });
     29
     30                                editor.undoManager.onRedo.addToTop(function(undoManager, o){
     31                                        // Add back any views that were converted to text before deleting them
     32                                        o.content = wp.mce.view.toViews( o.content );
     33
     34                                        console.log( 'onRedo = '+o.content )
     35                                });
    2136                        });
    2237
    2338                        // When the editor's content changes, scan the new content for
     
    129144
    130145                                // If delete or backspace is pressed, delete the view.
    131146                                if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
    132                                         if ( (instance = wp.mce.view.instance( selected )) ) {
     147                                        if ( instance = wp.mce.view.instance( selected ) ) {
     148                                                var dom = editor.dom, parent = selected.parentNode, html, temp, viewText;
     149
     150                                                // Get the view's HTML as string and convert it to text mode
     151                                                html = wp.mce.view.toText( dom.getOuterHTML(selected) );
     152
     153                                                // Create a temporary span to hold the view's text and replace the view with the temp span
     154                                                temp = dom.create('span', {}, html);
     155                                                dom.replace( temp, selected );
     156
     157                                                // Run any "remove" callbacks
     158                                                // (At this point the view has alredy been removed from the dom. Could that cause problems?)
    133159                                                instance.remove();
    134160                                                wpView.deselect();
     161
     162                                                // Get the text for the view again in case it has changed
     163                                                viewText = temp.innerHTML;
     164                                                // Remove the temp <span> leaving its content only
     165                                                dom.remove(temp, true);
     166
     167                                                // Cancel the undo level up to this point
     168                                                editor.execCommand('mceEndUndoLevel', false, {}, {skip_undo: true});
     169
     170                                                // Finally delete the view's text. This will be saved in the undo level.
     171                                                parent.innerHTML = parent.innerHTML.replace(viewText, '');
     172                                                editor.nodeChanged();
    135173                                        }
    136174                                }
    137175
     
    140178                                if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) )
    141179                                        return;
    142180
    143                                 event.preventDefault();
     181                                editor.dom.event.prevent(event);
    144182                        });
    145183                },
    146184
    147185                getParentView : function( node ) {
    148                         while ( node ) {
     186                        while ( node && node.nodeName != 'BODY' ) {
    149187                                if ( this.isView( node ) )
    150188                                        return node;
    151189
     
    185223
    186224        // Register plugin
    187225        tinymce.PluginManager.add( 'wpview', tinymce.plugins.wpView );
    188 })();
    189  No newline at end of file
     226})();