Skip to content

Commit 73172fa

Browse files
committed
fix: operations doesn't work for empty tags as we were relying on the end tag
1 parent 422bada commit 73172fa

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ define(function (require, exports, module) {
152152
}
153153

154154
const startPos = startRange.from;
155-
const endPos = endRange.close.to;
155+
// for empty tags endRange.close might not exist, for ex: img tag
156+
const endPos = endRange.close ? endRange.close.to : endRange.open.to;
156157

157158
const text = editor.getTextBetween(startPos, endPos);
158159

@@ -205,7 +206,8 @@ define(function (require, exports, module) {
205206
}
206207

207208
const startPos = startRange.from;
208-
const endPos = endRange.close.to;
209+
// for empty tags endRange.close might not exist, for ex: img tag
210+
const endPos = endRange.close ? endRange.close.to : endRange.open.to;
209211

210212
// this is the actual source code for the element that we need to duplicate
211213
const text = editor.getTextBetween(startPos, endPos);
@@ -265,7 +267,8 @@ define(function (require, exports, module) {
265267
}
266268

267269
const startPos = startRange.from;
268-
const endPos = endRange.close.to;
270+
// for empty tags endRange.close might not exist, for ex: img tag
271+
const endPos = endRange.close ? endRange.close.to : endRange.open.to;
269272

270273
editor.document.batchOperation(function () {
271274
editor.replaceRange("", startPos, endPos);
@@ -385,12 +388,12 @@ define(function (require, exports, module) {
385388

386389
const sourceRange = {
387390
from: sourceStartRange.from,
388-
to: sourceEndRange.close.to
391+
to: sourceEndRange.close ? sourceEndRange.close.to : sourceEndRange.open.to
389392
};
390393

391394
const targetRange = {
392395
from: targetStartRange.from,
393-
to: targetEndRange.close.to
396+
to: targetEndRange.close ? targetEndRange.close.to : targetEndRange.open.to
394397
};
395398

396399
const sourceText = editor.getTextBetween(sourceRange.from, sourceRange.to);
@@ -430,7 +433,9 @@ define(function (require, exports, module) {
430433
if (updatedSourceEndRange) {
431434
const updatedSourceRange = {
432435
from: updatedSourceStartRange.from,
433-
to: updatedSourceEndRange.close.to
436+
to: updatedSourceEndRange.close
437+
? updatedSourceEndRange.close.to
438+
: updatedSourceEndRange.open.to
434439
};
435440
editor.replaceRange("", updatedSourceRange.from, updatedSourceRange.to);
436441
_cleanupAfterRemoval(editor, updatedSourceRange);
@@ -461,7 +466,7 @@ define(function (require, exports, module) {
461466

462467
const updatedTargetRange = {
463468
from: updatedTargetStartRange.from,
464-
to: updatedTargetEndRange.close.to
469+
to: updatedTargetEndRange.close ? updatedTargetEndRange.close.to : updatedTargetEndRange.open.to
465470
};
466471

467472
if (insertAfter) {

0 commit comments

Comments
 (0)