Skip to content

Commit 7c09577

Browse files
committed
Improve asset editor source undo
Asset editor replaces text ranges instead of the entire source file. Undo inside the source code now scrolls asset editor edits into view, and selects the text being edited, so that what is being (un)edited is much clearer. Also, undo history is no longer burdened by full file edits.
1 parent f2e2935 commit 7c09577

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/ide/pixeleditor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,11 @@ export class TextDataNode extends CodeProjectDataNode {
490490
// TODO: reload editors?
491491
var datastr = this.text.substring(this.start, this.end);
492492
datastr = replaceHexWords(datastr, this.words, this.bpw);
493-
this.text = this.text.substring(0, this.start) + datastr + this.text.substring(this.end);
494-
this.end = this.start + datastr.length;
495493
if (this.project) {
496-
this.project.updateFile(this.fileid, this.text);
497-
//this.project.replaceTextRange(this.fileid, this.start, this.end, datastr);
494+
this.project.replaceTextRange(this.fileid, this.start, this.end, datastr);
498495
}
496+
this.text = this.text.substring(0, this.start) + datastr + this.text.substring(this.end);
497+
this.end = this.start + datastr.length;
499498
return true;
500499
}
501500
updateRight() {

src/ide/views/baseviews.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface ProjectView {
2020
recreateOnResize?: boolean;
2121
undoStep?(): void;
2222
redoStep?(): void;
23+
replaceTextRange?(from: number, to: number, text: string): void;
2324
};
2425

2526
// detect mobile (https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device)

src/ide/views/editors.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ export class SourceEditor implements ProjectView {
299299
}
300300
}
301301

302+
replaceTextRange(from: number, to: number, text: string) {
303+
const fromline = this.editor.state.doc.lineAt(from).number;
304+
const toline = this.editor.state.doc.lineAt(to).number;
305+
this.editor.dispatch({
306+
changes: { from, to, insert: text },
307+
annotations: isolateHistory.of("full"),
308+
selection: { anchor: from, head: to },
309+
effects: [
310+
EditorView.scrollIntoView(this.editor.state.doc.line(fromline).from, { y: "start", yMargin: 100/*pixels*/ }),
311+
]
312+
});
313+
}
314+
302315
insertText(text: string) {
303316
const main = this.editor.state.selection.main;
304317
this.editor.dispatch({

src/ide/windows.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ export class ProjectWindows {
138138
}
139139
}
140140

141+
replaceTextRange(fileid: string, from: number, to: number, text: string) {
142+
var wnd = this.id2window[fileid];
143+
wnd.replaceTextRange(from, to, text);
144+
this.undofiles.push(fileid);
145+
this.redofiles = [];
146+
}
147+
141148
undoStep() {
142149
var fileid = this.undofiles.pop();
143150
var wnd = this.id2window[fileid];

0 commit comments

Comments
 (0)