Skip to content

Commit b2f32ec

Browse files
committed
fix: do delete and duplicate operation in batch so that undo and redo works as expected
1 parent d7cf691 commit b2f32ec

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,26 @@ define(function (require, exports, module) {
9494
// this is the indentation on the line
9595
const indent = editor.getTextBetween({ line: range.from.line, ch: 0 }, range.from);
9696

97-
// make sure there is only indentation and no text before it
98-
if (indent.trim() === "") {
99-
// this is the position where we need to insert
100-
// we're giving the char as 0 because since we insert a new line using '\n'
101-
// that's why writing any char value will not work, as the line is empty
102-
// and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
103-
// So, the logic is to just append the indent before the text at this insertPos
104-
const insertPos = {
105-
line: range.from.line + (range.to.line - range.from.line + 1),
106-
ch: 0
107-
};
97+
editor.document.batchOperation(function () {
98+
// make sure there is only indentation and no text before it
99+
if (indent.trim() === "") {
100+
// this is the position where we need to insert
101+
// we're giving the char as 0 because since we insert a new line using '\n'
102+
// that's why writing any char value will not work, as the line is empty
103+
// and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
104+
// So, the logic is to just append the indent before the text at this insertPos
105+
const insertPos = {
106+
line: range.from.line + (range.to.line - range.from.line + 1),
107+
ch: 0
108+
};
108109

109-
editor.replaceRange("\n", range.to);
110-
editor.replaceRange(indent + text, insertPos);
111-
} else {
112-
// if there is some text, we just add the duplicated text right next to it
113-
editor.replaceRange(text, range.from);
114-
}
110+
editor.replaceRange("\n", range.to);
111+
editor.replaceRange(indent + text, insertPos);
112+
} else {
113+
// if there is some text, we just add the duplicated text right next to it
114+
editor.replaceRange(text, range.from);
115+
}
116+
});
115117
}
116118

117119
/**
@@ -137,14 +139,16 @@ define(function (require, exports, module) {
137139
return;
138140
}
139141

140-
editor.replaceRange("", range.from, range.to);
142+
editor.document.batchOperation(function () {
143+
editor.replaceRange("", range.from, range.to);
141144

142-
// since we remove content from the source, we want to clear the extra line
143-
if(range.from.line !== 0) {
144-
const prevLineText = editor.getLine(range.from.line - 1);
145-
const chPrevLine = prevLineText ? prevLineText.length : 0;
146-
editor.replaceRange("", {line: range.from.line - 1, ch: chPrevLine}, range.from);
147-
}
145+
// since we remove content from the source, we want to clear the extra line
146+
if(range.from.line !== 0) {
147+
const prevLineText = editor.getLine(range.from.line - 1);
148+
const chPrevLine = prevLineText ? prevLineText.length : 0;
149+
editor.replaceRange("", {line: range.from.line - 1, ch: chPrevLine}, range.from);
150+
}
151+
});
148152
}
149153

150154
/**

0 commit comments

Comments
 (0)