editor setText() does minimum change for undo

This commit is contained in:
Steven Hugg 2019-03-27 09:02:01 -04:00
parent 6cea0772bf
commit c794a43004
5 changed files with 42 additions and 13 deletions

View File

@ -13,6 +13,13 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<link href="https://fonts.googleapis.com/css?family=Exo" rel="stylesheet">
<style type="text/css" media="screen">
body {
font-family: 'Exo', sans-serif;
}
</style>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
if (window.location.host.endsWith('8bitworkshop.com')) {

View File

@ -109,6 +109,8 @@ TODO:
- update nested data, palette/tile refs properly
- throw errors when bad/no refs
- careful with mouse capture out of frame
- per-View keyboard shortcuts
- global undo
WEB WORKER FORMAT

View File

@ -81,8 +81,9 @@ export class SourceEditor implements ProjectView {
parent.appendChild(div);
var asmOverride = text && this.mode=='verilog' && /__asm\b([\s\S]+?)\b__endasm\b/.test(text);
this.newEditor(div, asmOverride);
if (text)
if (text) {
this.setText(text); // TODO: this calls setCode() and builds... it shouldn't
}
this.setupEditor();
return div;
}
@ -141,8 +142,19 @@ export class SourceEditor implements ProjectView {
}
setText(text:string) {
this.editor.setValue(text); // calls setCode()
this.editor.clearHistory();
var i,j;
var oldtext = this.editor.getValue();
if (oldtext != text) {
// find minimum range to undo
for (var i=0; i<oldtext.length && i<text.length && text[i] == oldtext[i]; i++) { }
for (var j=0; j<oldtext.length && j<text.length && text[text.length-1-j] == oldtext[oldtext.length-1-j]; j++) { }
//console.log(i,j,oldtext.substring(i,oldtext.length-j));
this.replaceSelection(i, oldtext.length-j, text.substring(i, text.length-j)); // calls setCode()
// clear history if setting empty editor
if (oldtext == '') {
this.editor.clearHistory();
}
}
}
insertText(text:string) {
@ -150,6 +162,11 @@ export class SourceEditor implements ProjectView {
this.editor.replaceRange(text, cur, cur);
}
replaceSelection(start:number, end:number, text:string) {
this.editor.setSelection(this.editor.posFromIndex(start), this.editor.posFromIndex(end));
this.editor.replaceSelection(text);
}
getValue() : string {
return this.editor.getValue();
}
@ -349,11 +366,6 @@ export class SourceEditor implements ProjectView {
return -1;
}
replaceSelection(start:number, end:number, text:string) {
this.editor.setSelection(end, start);
this.editor.replaceSelection(text);
}
}
///

View File

@ -28,16 +28,24 @@ export class ProjectWindows {
this.id2createfn[id] = createfn;
}
createOrShow(id:string) : ProjectView {
create(id:string) : ProjectView {
var wnd = this.id2window[id];
if (!wnd) {
console.log("creating window",id);
wnd = this.id2window[id] = this.id2createfn[id](id);
}
var div = this.id2div[id];
if (!div) {
var data = this.project.getFile(id)+""; // TODO: binary files
div = this.id2div[id] = wnd.createDiv(this.containerdiv, data);
$(div).hide();
}
return wnd;
}
createOrShow(id:string) : ProjectView {
var wnd = this.create(id);
var div = this.id2div[id];
if (this.activewnd != wnd) {
if (this.activediv)
$(this.activediv).hide();
@ -103,9 +111,10 @@ export class ProjectWindows {
this.createOrShow(this.activeid);
}
}
updateFile(fileid : string, data : FileData) {
var wnd = this.id2window[fileid];
updateFile(fileid:string, data:FileData) {
// is there an editor? we should create one...
var wnd = this.create(fileid);
if (wnd && wnd.setText && typeof data === 'string') {
wnd.setText(data);
} else {

View File

@ -1912,7 +1912,6 @@ function executeBuildSteps() {
var toolfn = TOOLS[step.tool];
if (!toolfn) throw "no tool named " + step.tool;
step.params = PLATFORM_PARAMS[getBasePlatform(platform)];
console.log(step.platform + " " + step.tool);
try {
step.result = toolfn(step);
} catch (e) {