@@ -2,6 +2,41 @@ define(function (require, exports, module) {
22 const EditorManager = require ( "editor/EditorManager" ) ;
33 const HTMLInstrumentation = require ( "LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation" ) ;
44
5+ /**
6+ * This function is responsible to duplicate an element from the source code
7+ * @param {Number } tagId - the data-brackets-id of the DOM element
8+ */
9+ function _duplicateElementInSourceByTagId ( tagId ) {
10+ const editor = EditorManager . getActiveEditor ( ) ;
11+ if ( ! editor || ! tagId ) {
12+ return ;
13+ }
14+
15+ // this will give us the start pos and end pos of the DOM element in the source code
16+ // can be referenced using range.from and range.to
17+ const range = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
18+ if ( ! range ) {
19+ return ;
20+ }
21+
22+ // this is the actual source code for the element that we need to duplicate
23+ const text = editor . getTextBetween ( range . from , range . to ) ;
24+ // this is the indentation on the line
25+ const indent = editor . getTextBetween ( { line : range . from . line , ch : 0 } , range . from ) ;
26+
27+ // this is the position where we need to insert
28+ // we're giving the char as 0 because since we insert a new line using '\n'
29+ // that's why writing any char value will not work, as the line is empty
30+ // and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
31+ // So, the logic is to just append the indent before the text at this insertPos
32+ const insertPos = {
33+ line : range . from . line + ( range . to . line - range . from . line + 1 ) ,
34+ ch : 0
35+ } ;
36+
37+ editor . replaceRange ( '\n' , range . to ) ;
38+ editor . replaceRange ( indent + text , insertPos ) ;
39+ }
540
641 /**
742 * This function is responsible to delete an element from the source code
@@ -16,7 +51,7 @@ define(function (require, exports, module) {
1651 // this will give us the start pos and end pos of the DOM element in the source code
1752 // can be referenced using range.from and range.to
1853 const range = HTMLInstrumentation . getPositionFromTagId ( editor , tagId ) ;
19- if ( ! range ) {
54+ if ( ! range ) {
2055 return ;
2156 }
2257
@@ -47,7 +82,9 @@ define(function (require, exports, module) {
4782
4883 // just call the required functions
4984 if ( message . delete ) {
50- _deleteElementInSourceByTagId ( Number ( message . tagId ) ) ;
85+ _deleteElementInSourceByTagId ( message . tagId ) ;
86+ } else if ( message . duplicate ) {
87+ _duplicateElementInSourceByTagId ( message . tagId ) ;
5188 }
5289 }
5390
0 commit comments