X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2Fpackagist%2Fludivine-ckeditor-component.git;a=blobdiff_plain;f=sources%2Fplugins%2Flink%2Fdialogs%2Flink.js;fp=sources%2Fplugins%2Flink%2Fdialogs%2Flink.js;h=4c609289a559c19fbfd207cedac9b8875cb674eb;hp=914471fd1a0e8591d62e0a66897d2af5032d784f;hb=1794320dcfdfcd19572fb1676294f9853a6bbc20;hpb=7183f6a6a21ad9124e70c997e0168459f377a9f2 diff --git a/sources/plugins/link/dialogs/link.js b/sources/plugins/link/dialogs/link.js index 914471f..4c60928 100644 --- a/sources/plugins/link/dialogs/link.js +++ b/sources/plugins/link/dialogs/link.js @@ -10,6 +10,110 @@ var plugin = CKEDITOR.plugins.link, initialLinkText; + function createRangeForLink( editor, link ) { + var range = editor.createRange(); + + range.setStartBefore( link ); + range.setEndAfter( link ); + + return range; + } + + function insertLinksIntoSelection( editor, data ) { + var attributes = plugin.getLinkAttributes( editor, data ), + ranges = editor.getSelection().getRanges(), + style = new CKEDITOR.style( { + element: 'a', + attributes: attributes.set + } ), + rangesToSelect = [], + range, + text, + nestedLinks, + i, + j; + + style.type = CKEDITOR.STYLE_INLINE; // need to override... dunno why. + + for ( i = 0; i < ranges.length; i++ ) { + range = ranges[ i ]; + + // Use link URL as text with a collapsed cursor. + if ( range.collapsed ) { + // Short mailto link text view (http://dev.ckeditor.com/ticket/5736). + text = new CKEDITOR.dom.text( data.linkText || ( data.type == 'email' ? + data.email.address : attributes.set[ 'data-cke-saved-href' ] ), editor.document ); + range.insertNode( text ); + range.selectNodeContents( text ); + } else if ( initialLinkText !== data.linkText ) { + text = new CKEDITOR.dom.text( data.linkText, editor.document ); + + // Shrink range to preserve block element. + range.shrink( CKEDITOR.SHRINK_TEXT ); + + // Use extractHtmlFromRange to remove markup within the selection. Also this method is a little + // smarter than range#deleteContents as it plays better e.g. with table cells. + editor.editable().extractHtmlFromRange( range ); + + range.insertNode( text ); + } + + // Editable links nested within current range should be removed, so that the link is applied to whole selection. + nestedLinks = range._find( 'a' ); + + for ( j = 0; j < nestedLinks.length; j++ ) { + nestedLinks[ j ].remove( true ); + } + + + // Apply style. + style.applyToRange( range, editor ); + + rangesToSelect.push( range ); + } + + editor.getSelection().selectRanges( rangesToSelect ); + } + + function editLinksInSelection( editor, selectedElements, data ) { + var attributes = plugin.getLinkAttributes( editor, data ), + ranges = [], + element, + href, + textView, + newText, + i; + + for ( i = 0; i < selectedElements.length; i++ ) { + // We're only editing an existing link, so just overwrite the attributes. + element = selectedElements[ i ]; + href = element.data( 'cke-saved-href' ); + textView = element.getHtml(); + + element.setAttributes( attributes.set ); + element.removeAttributes( attributes.removed ); + + + if ( data.linkText && initialLinkText != data.linkText ) { + // Display text has been changed. + newText = data.linkText; + } else if ( href == textView || data.type == 'email' && textView.indexOf( '@' ) != -1 ) { + // Update text view when user changes protocol (http://dev.ckeditor.com/ticket/4612). + // Short mailto link text view (http://dev.ckeditor.com/ticket/5736). + newText = data.type == 'email' ? data.email.address : attributes.set[ 'data-cke-saved-href' ]; + } + + if ( newText ) { + element.setText( newText ); + } + + ranges.push( createRangeForLink( editor, element ) ); + } + + // We changed the content, so need to select it again. + editor.getSelection().selectRanges( ranges ); + } + // Handles the event when the "Target" selection box is changed. var targetChanged = function() { var dialog = this.getDialog(), @@ -163,7 +267,7 @@ label: commonLang.protocol, 'default': 'http://', items: [ - // Force 'ltr' for protocol names in BIDI. (#5433) + // Force 'ltr' for protocol names in BIDI. (http://dev.ckeditor.com/ticket/5433) [ 'http://\u200E', 'http://' ], [ 'https://\u200E', 'https://' ], [ 'ftp://\u200E', 'ftp://' ], @@ -236,7 +340,7 @@ }, commit: function( data ) { // IE will not trigger the onChange event if the mouse has been used - // to carry all the operations #4724 + // to carry all the operations http://dev.ckeditor.com/ticket/4724 this.onChange(); if ( !data.url ) @@ -819,33 +923,30 @@ onShow: function() { var editor = this.getParentEditor(), selection = editor.getSelection(), - selectedElement = selection.getSelectedElement(), displayTextField = this.getContentElement( 'info', 'linkDisplayText' ).getElement().getParent().getParent(), - element = null; + elements = plugin.getSelectedLink( editor, true ), + firstLink = elements[ 0 ] || null; // Fill in all the relevant fields if there's already one link selected. - if ( ( element = plugin.getSelectedLink( editor ) ) && element.hasAttribute( 'href' ) ) { + if ( firstLink && firstLink.hasAttribute( 'href' ) ) { // Don't change selection if some element is already selected. // For example - don't destroy fake selection. - if ( !selectedElement ) { - selection.selectElement( element ); - selectedElement = element; + if ( !selection.getSelectedElement() && !selection.isInTable() ) { + selection.selectElement( firstLink ); } - } else { - element = null; } + var data = plugin.parseLinkAttributes( editor, firstLink ); + // Here we'll decide whether or not we want to show Display Text field. - if ( plugin.showDisplayTextForElement( selectedElement, editor ) ) { + if ( elements.length <= 1 && plugin.showDisplayTextForElement( firstLink, editor ) ) { displayTextField.show(); } else { displayTextField.hide(); } - var data = plugin.parseLinkAttributes( editor, element ); - // Record down the selected element in the dialog. - this._.selectedElement = element; + this._.selectedElements = elements; this.setupContent( data ); }, @@ -855,77 +956,12 @@ // Collect data from fields. this.commitContent( data ); - var selection = editor.getSelection(), - attributes = plugin.getLinkAttributes( editor, data ), - bm, - nestedLinks; - - if ( !this._.selectedElement ) { - var range = selection.getRanges()[ 0 ], - text; - - // Use link URL as text with a collapsed cursor. - if ( range.collapsed ) { - // Short mailto link text view (#5736). - text = new CKEDITOR.dom.text( data.linkText || ( data.type == 'email' ? - data.email.address : attributes.set[ 'data-cke-saved-href' ] ), editor.document ); - range.insertNode( text ); - range.selectNodeContents( text ); - } else if ( initialLinkText !== data.linkText ) { - text = new CKEDITOR.dom.text( data.linkText, editor.document ); - - // Shrink range to preserve block element. - range.shrink( CKEDITOR.SHRINK_TEXT ); - - // Use extractHtmlFromRange to remove markup within the selection. Also this method is a little - // smarter than range#deleteContents as it plays better e.g. with table cells. - editor.editable().extractHtmlFromRange( range ); - - range.insertNode( text ); - } - - // Editable links nested within current range should be removed, so that the link is applied to whole selection. - nestedLinks = range._find( 'a' ); - - for ( var i = 0; i < nestedLinks.length; i++ ) { - nestedLinks[ i ].remove( true ); - } - - // Apply style. - var style = new CKEDITOR.style( { - element: 'a', - attributes: attributes.set - } ); - - style.type = CKEDITOR.STYLE_INLINE; // need to override... dunno why. - style.applyToRange( range, editor ); - range.select(); + if ( !this._.selectedElements.length ) { + insertLinksIntoSelection( editor, data ); } else { - // We're only editing an existing link, so just overwrite the attributes. - var element = this._.selectedElement, - href = element.data( 'cke-saved-href' ), - textView = element.getHtml(), - newText; - - element.setAttributes( attributes.set ); - element.removeAttributes( attributes.removed ); - - if ( data.linkText && initialLinkText != data.linkText ) { - // Display text has been changed. - newText = data.linkText; - } else if ( href == textView || data.type == 'email' && textView.indexOf( '@' ) != -1 ) { - // Update text view when user changes protocol (#4612). - // Short mailto link text view (#5736). - newText = data.type == 'email' ? data.email.address : attributes.set[ 'data-cke-saved-href' ]; - } - - if ( newText ) { - element.setText( newText ); - // We changed the content, so need to select it again. - selection.selectElement( element ); - } + editLinksInSelection( editor, this._.selectedElements, data ); - delete this._.selectedElement; + delete this._.selectedElements; } }, onLoad: function() {