Skip to content

Commit 6e9df29

Browse files
committed
feat: implement live preview edit text functionality
1 parent a7e3140 commit 6e9df29

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,16 +1553,14 @@ function RemoteFunctions(config) {
15531553
// If content has changed, send the edit to the editor
15541554
if (newContent !== element._originalContent && element.hasAttribute("data-brackets-id")) {
15551555
const tagId = element.getAttribute("data-brackets-id");
1556-
1557-
// Create a text edit operation
1558-
// const edit = {
1559-
// type: "textReplace",
1560-
// parentID: element.parentNode.getAttribute("data-brackets-id"),
1561-
// tagID: tagId,
1562-
// content: newContent
1563-
// };
1564-
//
1565-
// todo: send the edited text to phoenix to change in text editor
1556+
window._Brackets_MessageBroker.send({
1557+
livePreviewEditEnabled: true,
1558+
element: element,
1559+
oldContent: element._originalContent,
1560+
newContent: newContent,
1561+
tagId: Number(tagId),
1562+
livePreviewTextEdit: true
1563+
});
15661564
}
15671565

15681566
// Clean up

src/LiveDevelopment/livePreviewEdit.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,46 @@ define(function (require, exports, module) {
22
const EditorManager = require("editor/EditorManager");
33
const HTMLInstrumentation = require("LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation");
44

5+
/**
6+
* this function handles the text edit in the source code when user updates the text in the live preview
7+
* @param {Object} message - the message object
8+
* {
9+
* livePreviewEditEnabled: true,
10+
element: the DOM element that was modified,
11+
oldContent: the text that was present before the edit,
12+
newContent: the new text,
13+
tagId: data-brackets-id of the DOM element,
14+
livePreviewTextEdit: true
15+
}
16+
*
17+
* The logic is: get the text in the editor using the tagId. split that text using the old content
18+
* join the text back and add the new content in between
19+
*/
20+
function _editTextInSource(message) {
21+
const editor = EditorManager.getActiveEditor();
22+
if (!editor || !message.tagId) {
23+
return;
24+
}
25+
26+
// this will give us the start pos and end pos of the DOM element in the source code
27+
// can be referenced using range.from and range.to
28+
const range = HTMLInstrumentation.getPositionFromTagId(editor, message.tagId);
29+
if (!range) {
30+
return;
31+
}
32+
33+
// this is the actual source code for the element that we need to duplicate
34+
const text = editor.getTextBetween(range.from, range.to);
35+
// split the text as we want to remove the old content from the source code
36+
// for ex: if we have <h1>hello</h1> then splitting from hello will give us [<h1>, </h1>]
37+
const splittedText = text.split(message.oldContent);
38+
39+
// so now we just merge the whole thing back replacing the old content with the new one
40+
const finalText = splittedText[0] + message.newContent + splittedText[1];
41+
42+
editor.replaceRange(finalText, range.from, range.to);
43+
}
44+
545
/**
646
* This function is responsible to duplicate an element from the source code
747
* @param {Number} tagId - the data-brackets-id of the DOM element
@@ -67,13 +107,10 @@ define(function (require, exports, module) {
67107
* this object will be in the format
68108
* {
69109
livePreviewEditEnabled: true,
70-
element: element,
71-
event: event,
72110
tagId: tagId,
73-
delete: true
111+
delete || duplicate || livePreviewTextEdit: true
74112
}
75-
* here element is the actual DOM element that is clicked, and tagId is the data-brackets-id
76-
* and 'delete: true is just an example, it might be 'duplicate' or 'select-parent' also
113+
* these are the main properties that are passed through the message
77114
*/
78115
function handleLivePreviewEditOperation(message) {
79116
if (!message.element || !message.tagId) {
@@ -85,6 +122,8 @@ define(function (require, exports, module) {
85122
_deleteElementInSourceByTagId(message.tagId);
86123
} else if (message.duplicate) {
87124
_duplicateElementInSourceByTagId(message.tagId);
125+
} else if (message.livePreviewTextEdit) {
126+
_editTextInSource(message);
88127
}
89128
}
90129

0 commit comments

Comments
 (0)