Skip to content

Commit 260ba80

Browse files
committed
fix: get actual indent instead of assuming it to be 4 spaces
1 parent 8e47517 commit 260ba80

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -430,21 +430,15 @@ define(function (require, exports, module) {
430430
if (sourceBeforeTarget) {
431431
// this handles the case when source is before target: insert first, then remove
432432
if (insertInside) {
433-
// Insert as child inside the target element
434-
const targetText = editor.getTextBetween(targetRangeObj.from, targetRangeObj.to);
435-
const targetElement = targetText.trim();
436-
437-
// Find the position just after the opening tag
438-
const openingTagMatch = targetElement.match(/^<[^>]*>/);
439-
if (openingTagMatch) {
440-
const openingTag = openingTagMatch[0];
433+
const matchingTagInfo = CodeMirror.findMatchingTag(editor._codeMirror, targetRangeObj.from);
434+
if (matchingTagInfo && matchingTagInfo.open) {
441435
const insertPos = {
442-
line: targetRangeObj.from.line,
443-
ch: targetRangeObj.from.ch + openingTag.length
436+
line: matchingTagInfo.open.to.line,
437+
ch: matchingTagInfo.open.to.ch
444438
};
445439

446-
// Add proper indentation for child element
447-
const childIndent = targetIndent + " "; // 4 spaces more than parent
440+
const indentInfo = editor._detectIndent();
441+
const childIndent = targetIndent + indentInfo.indent;
448442
_insertElementWithIndentation(editor, insertPos, true, childIndent, sourceText);
449443
}
450444
} else if (insertAfter) {
@@ -489,21 +483,15 @@ define(function (require, exports, module) {
489483
};
490484

491485
if (insertInside) {
492-
// Insert as child inside the target element
493-
const targetText = editor.getTextBetween(updatedTargetRangeObj.from, updatedTargetRangeObj.to);
494-
const targetElement = targetText.trim();
495-
496-
// Find the position just after the opening tag
497-
const openingTagMatch = targetElement.match(/^<[^>]*>/);
498-
if (openingTagMatch) {
499-
const openingTag = openingTagMatch[0];
486+
const matchingTagInfo = CodeMirror.findMatchingTag(editor._codeMirror, updatedTargetRangeObj.from);
487+
if (matchingTagInfo && matchingTagInfo.open) {
500488
const insertPos = {
501-
line: updatedTargetRangeObj.from.line,
502-
ch: updatedTargetRangeObj.from.ch + openingTag.length
489+
line: matchingTagInfo.open.to.line,
490+
ch: matchingTagInfo.open.to.ch
503491
};
504492

505-
// Add proper indentation for child element
506-
const childIndent = targetIndent + " "; // 4 spaces more than parent
493+
const indentInfo = editor._detectIndent();
494+
const childIndent = targetIndent + indentInfo.indent;
507495
_insertElementWithIndentation(editor, insertPos, true, childIndent, sourceText);
508496
}
509497
} else if (insertAfter) {

0 commit comments

Comments
 (0)