Skip to content

Commit 5fc4515

Browse files
committed
feat: implement live preview duplicate option functionality
1 parent 72c423f commit 5fc4515

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ function RemoteFunctions(config) {
224224
console.log("handle select parent option button was clicked");
225225
}
226226

227-
// TODO: need to implement
228-
function _handleDuplicateOptionClick(e) {
229-
console.log("handle duplicate option button was clicked");
230-
}
231-
232227
/**
233228
* This function gets called when the delete button is clicked
234229
* it sends a message to the editor using postMessage to delete the element from the source code
@@ -242,12 +237,31 @@ function RemoteFunctions(config) {
242237
livePreviewEditEnabled: true,
243238
element: element,
244239
event: event,
245-
tagId: tagId,
240+
tagId: Number(tagId),
246241
delete: true
247242
});
248243
}
249244
}
250245

246+
/**
247+
* For duplicate button. Read '_handleDeleteOptionClick' jsdoc to understand more on how this works
248+
* @param {Event} event
249+
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
250+
*/
251+
function _handleDuplicateOptionClick(event, element) {
252+
const tagId = element.getAttribute("data-brackets-id");
253+
if (tagId) {
254+
window._Brackets_MessageBroker.send({
255+
livePreviewEditEnabled: true,
256+
element: element,
257+
event: event,
258+
tagId: Number(tagId),
259+
duplicate: true
260+
});
261+
}
262+
}
263+
264+
251265
/**
252266
* This function will get triggered when from the multiple advance DOM buttons, one is clicked
253267
* this function just checks which exact button was clicked and call the required function

src/LiveDevelopment/livePreviewEdit.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)