@@ -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