Ticket #5367: 5367.patch

File 5367.patch, 5.9 KB (added by Garry Yao, 13 years ago)
  • _source/core/editor.js

     
    755755                        this.fire( 'insertHtml', data );
    756756                },
    757757
     758                /*
     759                 * Insert text content into the currently selected position in the
     760                 * editor, in WYSIWYG mode, styles of the selected element will be applied to the inserted text,
     761                 * spaces around the text will be leaving untouched.
     762                 * <strong>Note:</strong> two subsequent line-breaks will be translated into
     763                 * one enter-key in effect, result depends on {@link CKEDITOR.config.enterMode};
     764                 * A single line-break will be translated into per shift-enter-key correspondingly.
     765                 * @param {String} text Text to be inserted into the editor.
     766                 * @example
     767                 * CKEDITOR.instances.editor1.<b>insertText( ' line1 \n\n line2' )</b>;
     768                 */
     769                insertText : function( text )
     770                {
     771                        this.fire( 'insertText', text );
     772                },
     773
    758774                /**
    759775                 * Inserts an element into the currently selected position in the
    760776                 * editor.
  • _source/plugins/wysiwygarea/plugin.js

     
    102102                }
    103103        }
    104104
     105        function doInsertText( doc, text )
     106        {
     107                var selection = doc.getSelection(),
     108                        range = selection.getRanges()[ 0 ];
     109
     110                range.insertNode( new CKEDITOR.dom.text( text, doc ) );
     111                range.collapse();
     112                range.select();
     113        }
     114
     115        function doEnter( editor, mode, times, forceMode )
     116        {
     117                while ( times-- )
     118                {
     119                        CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
     120                                        ( editor, mode, null, forceMode );
     121                }
     122        }
     123
     124        function onInsertText( evt )
     125        {
     126                if ( this.mode == 'wysiwyg' )
     127                {
     128                        this.focus();
     129                        this.fire( 'saveSnapshot' );
     130
     131                        var text = evt.data,
     132                                        mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
     133                                        isEnterBrMode = mode == CKEDITOR.ENTER_BR,
     134                                        doc = this.document,
     135                                        self = this,
     136                                        line;
     137
     138                        text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
     139
     140                        var startIndex = 0;
     141                        text.replace( /\n+/g, function( match, lastIndex )
     142                        {
     143                                line = text.substring( startIndex, lastIndex );
     144                                startIndex = lastIndex + match.length;
     145                                line.length && doInsertText( doc, line );
     146
     147                                var lineBreakNums = match.length,
     148                                        // Duo consequence line-break as a enter block.
     149                                                enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
     150                                        // Per link-break as a enter br.
     151                                                enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;
     152
     153                                // Line-breaks are converted to editor enter key strokes.
     154                                doEnter( self, mode, enterBlockTimes );
     155                                doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
     156                        } );
     157
     158                        // Insert the last text line of text.
     159                        line = text.substring( startIndex, text.length );
     160                        line.length && doInsertText( doc, line );
     161
     162                        this.fire( 'saveSnapshot' );
     163                }
     164        }
     165
    105166        function onInsertElement( evt )
    106167        {
    107168                if ( this.mode == 'wysiwyg' )
     
    9611022
    9621023                                        editor.on( 'insertHtml', onInsertHtml, null, null, 20 );
    9631024                                        editor.on( 'insertElement', onInsertElement, null, null, 20 );
     1025                                        editor.on( 'insertText', onInsertText, null, null, 20 );
    9641026                                        // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
    9651027                                        editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
    9661028                                });
  • _source/plugins/pastetext/plugin.js

     
    3737                }
    3838        };
    3939
    40         function doInsertText( doc, text )
    41         {
    42                 // Native text insertion.
    43                 if ( CKEDITOR.env.ie )
    44                 {
    45                         var selection = doc.selection;
    46                         if ( selection.type == 'Control' )
    47                                 selection.clear();
    48                         selection.createRange().pasteHTML( text );
    49                 }
    50                 else
    51                         doc.execCommand( 'inserthtml', false, text );
    52         }
    53 
    5440        // Register the plugin.
    5541        CKEDITOR.plugins.add( 'pastetext',
    5642        {
     
    8470                requires : [ 'clipboard' ]
    8571        });
    8672
    87         function doEnter( editor, mode, times, forceMode )
    88         {
    89                 while ( times-- )
    90                 {
    91                         CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
    92                                         ( editor, mode, null, forceMode );
    93                 }
    94         }
    95 
    96         CKEDITOR.editor.prototype.insertText = function( text )
    97         {
    98                 this.focus();
    99                 this.fire( 'saveSnapshot' );
    100 
    101                 var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
    102                         isEnterBrMode = mode == CKEDITOR.ENTER_BR,
    103                         doc = this.document.$,
    104                         self = this,
    105                         line;
    106 
    107                 text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
    108 
    109                 var startIndex = 0;
    110                 text.replace( /\n+/g, function( match, lastIndex )
    111                  {
    112                         line = text.substring( startIndex, lastIndex );
    113                         startIndex = lastIndex + match.length;
    114                         line.length && doInsertText( doc, line );
    115 
    116                         var lineBreakNums = match.length,
    117                                 // Duo consequence line-break as a enter block.
    118                                 enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
    119                                 // Per link-break as a enter br.
    120                                 enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;
    121 
    122                         // Line-breaks are converted to editor enter key strokes.
    123                         doEnter( self, mode, enterBlockTimes );
    124                         doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
    125                  });
    126 
    127                 // Insert the last text line of text.
    128                 line = text.substring( startIndex, text.length );
    129                 line.length && doInsertText( doc, line );
    130 
    131                 this.fire( 'saveSnapshot' );
    132         };
    13373})();
    13474
    13575
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy