Skip to content

Commit 0eee7dd

Browse files
committed
feat: send data to AI when element is edited with AI
1 parent cd1b780 commit 0eee7dd

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ function RemoteFunctions(config) {
17361736
if (event.key === 'Enter' && !event.shiftKey) {
17371737
event.preventDefault();
17381738
if (textarea.value.trim()) {
1739-
this._handleSend(textarea.value.trim());
1739+
this._handleSend(event, textarea.value.trim());
17401740
}
17411741
} else if (event.key === 'Escape') {
17421742
event.preventDefault();
@@ -1751,15 +1751,27 @@ function RemoteFunctions(config) {
17511751
event.preventDefault();
17521752
event.stopPropagation();
17531753
if (textarea && textarea.value.trim()) {
1754-
this._handleSend(textarea.value.trim());
1754+
this._handleSend(event, textarea.value.trim());
17551755
}
17561756
});
17571757
}
17581758
},
17591759

1760-
_handleSend: function(prompt) {
1761-
// TODO: need to implement the logic for backend handling here
1762-
console.log('AI Prompt:', prompt, 'for element:', this.element);
1760+
_handleSend: function(event, prompt) {
1761+
const element = this.element;
1762+
if(!element) {
1763+
return;
1764+
}
1765+
const tagId = element.getAttribute("data-brackets-id");
1766+
1767+
window._Brackets_MessageBroker.send({
1768+
livePreviewEditEnabled: true,
1769+
event: event,
1770+
element: element,
1771+
prompt: prompt,
1772+
tagId: Number(tagId),
1773+
AISend: true
1774+
});
17631775
this.remove();
17641776
},
17651777

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,42 @@ define(function (require, exports, module) {
526526
}
527527
}
528528

529+
function _getRequiredDataForAI(message) {
530+
// this is to get the currently live document that is being served in the live preview
531+
const editor = _getEditorAndValidate(message.tagId);
532+
if (!editor) {
533+
return;
534+
}
535+
536+
const range = _getElementRange(editor, message.tagId);
537+
if (!range) {
538+
return;
539+
}
540+
541+
const { startPos, endPos } = range;
542+
// this is the actual source code for the element that we need to duplicate
543+
const text = editor.getTextBetween(startPos, endPos);
544+
const fileName = editor.document.file.name;
545+
const filePath = editor.document.file.fullPath;
546+
547+
const AIData = {
548+
editor: editor, // the editor instance that is being served in the live preview
549+
fileName: fileName,
550+
filePath: filePath, // the complete absolute path
551+
tagId: message.tagId, // the data-brackets-id of the element which was selected for AI edit
552+
range: {startPos, endPos}, // the start and end position text in the source code for that element
553+
text: text, // the actual source code in between the start and the end pos
554+
prompt: message.prompt // the prompt that user typed
555+
};
556+
557+
return AIData;
558+
}
559+
560+
function _editWithAI(message) {
561+
const AIData = _getRequiredDataForAI(message);
562+
// write the AI implementation here...@abose
563+
}
564+
529565
/**
530566
* This is the main function that is exported.
531567
* it will be called by LiveDevProtocol when it receives a message from RemoteFunctions.js
@@ -537,9 +573,11 @@ define(function (require, exports, module) {
537573
* {
538574
livePreviewEditEnabled: true,
539575
tagId: tagId,
540-
delete || duplicate || livePreviewTextEdit: true
576+
delete || duplicate || livePreviewTextEdit || AISend: true
541577
undoLivePreviewOperation: true (this property is available only for undo operation)
542578
579+
prompt: prompt (only for AI)
580+
543581
sourceId: sourceId, (these are for move (drag & drop))
544582
targetId: targetId,
545583
insertAfter: boolean, (whether to insert after the target element)
@@ -569,6 +607,8 @@ define(function (require, exports, module) {
569607
_duplicateElementInSourceByTagId(message.tagId);
570608
} else if (message.livePreviewTextEdit) {
571609
_editTextInSource(message);
610+
} else if (message.AISend) {
611+
_editWithAI(message);
572612
}
573613
}
574614

0 commit comments

Comments
 (0)