@@ -626,14 +626,48 @@ define(function (require, exports, module) {
626626 return checkAndIncrement ( ) ;
627627 }
628628
629+ /**
630+ * This function updates the src attribute of an image element in the source code
631+ * @param {Number } tagId - the data-brackets-id of the image element
632+ * @param {String } newSrcValue - the new src value to set
633+ */
634+ function _updateImageSrcAttribute ( tagId , newSrcValue ) {
635+ const editor = _getEditorAndValidate ( tagId ) ;
636+ if ( ! editor ) {
637+ return ;
638+ }
639+
640+ const range = _getElementRange ( editor , tagId ) ;
641+ if ( ! range ) {
642+ return ;
643+ }
644+
645+ const { startPos, endPos } = range ;
646+ const elementText = editor . getTextBetween ( startPos , endPos ) ;
647+
648+ // parse it using DOM parser so that we can update the src attribute
649+ const parser = new DOMParser ( ) ;
650+ const doc = parser . parseFromString ( elementText , "text/html" ) ;
651+ const imgElement = doc . querySelector ( 'img' ) ;
652+
653+ if ( imgElement ) {
654+ imgElement . setAttribute ( 'src' , newSrcValue ) ;
655+ const updatedElementText = imgElement . outerHTML ;
656+
657+ editor . document . batchOperation ( function ( ) {
658+ editor . replaceRange ( updatedElementText , startPos , endPos ) ;
659+ } ) ;
660+ }
661+ }
662+
629663 /**
630664 * This function is called when 'use this image' button is clicked in the image ribbon gallery
631665 * this is responsible to download the image in the appropriate place
632666 * and also change the src attribute of the element
633667 * @param {Object } message - the message object which stores all the required data for this operation
634668 */
635669 function _handleUseThisImage ( message ) {
636- const { imageUrl, filename } = message ;
670+ const { imageUrl, filename, tagId } = message ;
637671 const extnName = message . extnName || "jpg" ;
638672
639673 const projectRoot = ProjectManager . getProjectRoot ( ) ;
@@ -658,6 +692,8 @@ define(function (require, exports, module) {
658692 { encoding : window . fs . BYTE_ARRAY_ENCODING } , ( err ) => {
659693 if ( err ) {
660694 console . error ( 'Failed to save image:' , err ) ;
695+ } else {
696+ _updateImageSrcAttribute ( tagId , uniqueFilename ) ;
661697 }
662698 } ) ;
663699 } )
0 commit comments