Skip to content

Commit b380545

Browse files
committed
feat: change the img src attribute when user clicks on use this image button
1 parent 9d07639 commit b380545

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,14 +2369,17 @@ function RemoteFunctions(config = {}) {
23692369
},
23702370

23712371
_useImage: function(imageUrl, filename, extnName) {
2372-
// to use the image we send the message to the editor instance
2373-
// this is handled inside liveDevProtocol.js file
2372+
// send the message to the editor instance to save the image and update the source code
2373+
const tagId = this.element.getAttribute("data-brackets-id");
2374+
23742375
window._Brackets_MessageBroker.send({
23752376
livePreviewEditEnabled: true,
23762377
useImage: true,
23772378
imageUrl: imageUrl,
23782379
filename: filename,
2379-
extnName: extnName
2380+
extnName: extnName,
2381+
element: this.element,
2382+
tagId: Number(tagId)
23802383
});
23812384
},
23822385

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)