Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ide/pixeleditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export function parseHexWords(s: string): number[] {
}

export function replaceHexWords(s: string, words: UintArray, bpw: number): string {
// convert 'hex ...' format to 0x prefixed values for regex matching
s = convertToHexStatements(s);
var result = "";
var m;
var li = 0;
Expand Down
28 changes: 18 additions & 10 deletions src/ide/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ async function getSkeletonFile(fileid: string): Promise<string> {
try {
return await $.get("presets/" + getBasePlatform(platform_id) + "/skeleton." + ext, 'text');
} catch (e) {
console.log(e+"");
console.log(e + "");
return null;
}
}
Expand Down Expand Up @@ -707,19 +707,27 @@ export function getCurrentEditorFilename(): string {

function _revertFile(e) {
var wnd = projectWindows.getActive();
if (wnd && wnd.setText) {
var fn = projectWindows.getActiveID();
$.get("presets/" + getBasePlatform(platform_id) + "/" + fn, (text) => {
var fn = projectWindows.getActiveID();
var isBinary = wnd && wnd.setData && !wnd.setText;
if (wnd && (wnd.setText || wnd.setData)) {
var url = "presets/" + getBasePlatform(platform_id) + "/" + fn;
getWithBinary(url, (data) => {
if (data == null) {
if (repo_id) alertError("Can only revert built-in examples. If you want to revert all files, You can pull from the repository.");
else alertError("Can only revert built-in examples.");
return;
}
bootbox.confirm("Reset '" + DOMPurify.sanitize(fn) + "' to default?", (ok) => {
if (ok) {
wnd.setText(text);
if (isBinary) {
wnd.setData(data as Uint8Array);
current_project.updateFile(fn, data as Uint8Array);
} else {
wnd.setText(data as string);
}
}
});
}, 'text')
.fail(() => {
if (repo_id) alertError("Can only revert built-in examples. If you want to revert all files, You can pull from the repository.");
else alertError("Can only revert built-in examples.");
});
}, isBinary ? 'arraybuffer' : 'text');
} else {
alertError("Cannot revert the active window. Please choose a text file.");
}
Expand Down
8 changes: 5 additions & 3 deletions src/ide/views/asseteditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,11 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
setVisible?(showing: boolean): void {
// TODO: make into toolbar?
if (showing) {
// limit undo/redo to since opening this editor
projectWindows.undofiles = [];
projectWindows.redofiles = [];
// ensure asset editor is safe to perform synchronous reads/writes
projectWindows.flushAllWindows();
// limit undo/redo to since opening this asset editor
projectWindows.undoStack = [];
projectWindows.redoStack = [];
if (Mousetrap.bind) {
Mousetrap.bind('mod+z', (e) => { projectWindows.undoStep(); return false; });
Mousetrap.bind('mod+shift+z', (e) => { projectWindows.redoStep(); return false; });
Expand Down
2 changes: 2 additions & 0 deletions src/ide/views/baseviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ProjectView {
getPath?(): string;
getValue?(): string;
setText?(text: string): void;
setData?(data: any): void;
insertLinesBefore?(text: string): void;
getCursorPC?(): number;
getSourceFile?(): SourceFile;
Expand All @@ -20,6 +21,7 @@ export interface ProjectView {
recreateOnResize?: boolean;
undoStep?(): void;
redoStep?(): void;
flushChanges?(): void;
replaceTextRange?(from: number, to: number, text: string): void;
};

Expand Down
8 changes: 8 additions & 0 deletions src/ide/views/editors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ export class SourceEditor implements ProjectView {
}, this.refreshDelayMsec);
}

flushChanges() {
if (this.updateTimer) {
clearTimeout(this.updateTimer);
this.updateTimer = null;
current_project.updateFile(this.path, this.editor.state.doc.toString());
}
}

inspectUnderCursor(update: ViewUpdate) {
// TODO: handle multi-select
const range = update.state.selection.main;
Expand Down
108 changes: 75 additions & 33 deletions src/ide/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { ProjectView } from "./views/baseviews";
type WindowCreateFunction = (id: string) => ProjectView;
type WindowShowFunction = (id: string, view: ProjectView) => void;

interface UndoEntry {
fileid: string;
data?: Uint8Array;
}

export class ProjectWindows {
containerdiv: HTMLElement;
project: CodeProject;
Expand All @@ -19,16 +24,16 @@ export class ProjectWindows {
activewnd: ProjectView;
activediv: HTMLElement;
lasterrors: WorkerError[];
undofiles: string[];
redofiles: string[];
undoStack: UndoEntry[];
redoStack: UndoEntry[];
titlePrefix: string;
alerting: boolean;

constructor(containerdiv: HTMLElement, project: CodeProject) {
this.containerdiv = containerdiv;
this.project = project;
this.undofiles = [];
this.redofiles = [];
this.undoStack = [];
this.redoStack = [];
}
// TODO: delete windows ever?

Expand Down Expand Up @@ -154,52 +159,80 @@ export class ProjectWindows {
}

updateFile(fileid: string, data: FileData) {
// is there an editor? if so, use it
var wnd = this.id2window[fileid];
if (wnd && wnd.setText && typeof data === 'string') {
wnd.setText(data);
this.undofiles.push(fileid);
this.redofiles = [];
} else {
if (data instanceof Uint8Array) {
var prev = this.project.getFile(fileid);
this.undoStack.push({ fileid, data: prev instanceof Uint8Array ? new Uint8Array(prev) : undefined });
this.project.updateFile(fileid, data);
} else {
var wnd = this.id2window[fileid];
if (wnd && wnd.setText && typeof data === 'string') {
wnd.setText(data);
this.undoStack.push({ fileid });
} else {
this.project.updateFile(fileid, data);
return;
}
}
this.redoStack = [];
}

replaceTextRange(fileid: string, from: number, to: number, text: string) {
var wnd = this.id2window[fileid] || this.create(fileid);
wnd.replaceTextRange(from, to, text);
this.undofiles.push(fileid);
this.redofiles = [];
this.undoStack.push({ fileid });
this.redoStack = [];
}

undoStep() {
var fileid = this.undofiles.pop();
var wnd = this.id2window[fileid];
if (wnd && wnd.undoStep) {
wnd.undoStep();
if (wnd.getValue) {
this.project.updateFile(fileid, wnd.getValue());
}
this.redofiles.push(fileid);
this.refresh(false);
} else {
var entry = this.undoStack.pop();
if (!entry) {
this.showAlert("No more steps to undo.");
return;
}
if (entry.data) {
var current = this.project.getFile(entry.fileid);
this.redoStack.push({ fileid: entry.fileid, data: current instanceof Uint8Array ? new Uint8Array(current) : undefined });
this.project.updateFile(entry.fileid, entry.data);
} else {
var wnd = this.id2window[entry.fileid];
if (wnd && wnd.undoStep) {
wnd.undoStep();
if (wnd.getValue) {
this.project.updateFile(entry.fileid, wnd.getValue());
}
this.redoStack.push({ fileid: entry.fileid });
} else {
this.showAlert("No more steps to undo.");
return;
}
}
this.refresh(false);
}

redoStep() {
var fileid = this.redofiles.pop();
var wnd = this.id2window[fileid];
if (wnd && wnd.redoStep) {
wnd.redoStep();
if (wnd.getValue) {
this.project.updateFile(fileid, wnd.getValue());
}
this.undofiles.push(fileid);
this.refresh(false);
} else {
var entry = this.redoStack.pop();
if (!entry) {
this.showAlert("No more steps to redo.");
return;
}
if (entry.data) {
var current = this.project.getFile(entry.fileid);
this.undoStack.push({ fileid: entry.fileid, data: current instanceof Uint8Array ? new Uint8Array(current) : undefined });
this.project.updateFile(entry.fileid, entry.data);
} else {
var wnd = this.id2window[entry.fileid];
if (wnd && wnd.redoStep) {
wnd.redoStep();
if (wnd.getValue) {
this.project.updateFile(entry.fileid, wnd.getValue());
}
this.undoStack.push({ fileid: entry.fileid });
} else {
this.showAlert("No more steps to redo.");
return;
}
}
this.refresh(false);
}

showAlert(msg: string) {
Expand All @@ -208,6 +241,15 @@ export class ProjectWindows {
bootbox.alert(msg, () => { this.alerting = false; });
}

flushAllWindows() {
for (var fileid in this.id2window) {
var wnd = this.id2window[fileid];
if (wnd && wnd.flushChanges) {
wnd.flushChanges();
}
}
}

updateAllOpenWindows(store) {
for (var fileid in this.id2window) {
var wnd = this.id2window[fileid];
Expand Down
Loading