antosdk-apps/TinyEditor/build/debug/main.js

145 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-07-24 19:18:07 +02:00
(function() {
2020-05-21 21:56:16 +02:00
void 0;
2018-07-24 19:18:07 +02:00
var TinyEditor;
TinyEditor = class TinyEditor extends this.OS.GUI.BaseApplication {
constructor(args) {
super("TinyEditor", args);
}
main() {
this.editor = this.find("editor");
2020-05-21 21:56:16 +02:00
this.bindKey("ALT-N", () => {
return this.newFile();
2018-07-24 19:18:07 +02:00
});
2020-05-21 21:56:16 +02:00
this.bindKey("ALT-O", () => {
return this.openFile();
2018-07-24 19:18:07 +02:00
});
2020-05-21 21:56:16 +02:00
this.bindKey("CTRL-S", () => {
return this.saveFile();
2018-07-24 19:18:07 +02:00
});
2020-05-21 22:24:30 +02:00
this.filehandle = this.args && this.args.length > 0 ? this.args[0].path.asFileHandle() : null;
2020-05-21 21:56:16 +02:00
$(this.editor).on('input', (e) => {
if (this.filehandle.dirty === true) {
2018-07-24 19:18:07 +02:00
return;
}
2020-05-21 21:56:16 +02:00
this.filehandle.dirty = true;
return this.scheme.set("apptitle", `${this.filehandle.path}*`);
2018-07-24 19:18:07 +02:00
});
return this.read();
}
menu() {
2020-05-21 21:56:16 +02:00
var m;
2018-07-24 19:18:07 +02:00
m = [
{
text: "__(File)",
child: [
{
text: "__(New)",
dataid: "new",
shortcut: 'A-N'
},
{
text: "__(Open)",
dataid: "open",
shortcut: 'A-O'
},
{
text: "__(Save)",
dataid: "save",
shortcut: 'C-S'
}
],
2020-05-21 21:56:16 +02:00
onchildselect: (e) => {
switch (e.data.item.get("data").dataid) {
2018-07-24 19:18:07 +02:00
case "new":
2020-05-21 21:56:16 +02:00
return this.newFile();
2018-07-24 19:18:07 +02:00
case "open":
2020-05-21 21:56:16 +02:00
return this.openFile();
2018-07-24 19:18:07 +02:00
case "save":
2020-05-21 21:56:16 +02:00
return this.saveFile();
2018-07-24 19:18:07 +02:00
}
}
}
];
return m;
}
newFile() {
2020-05-21 21:56:16 +02:00
this.filehandle = null;
2018-07-24 19:18:07 +02:00
return this.read();
}
2018-07-25 16:20:13 +02:00
openFile() {
2020-05-21 21:56:16 +02:00
return this.openDialog("FileDialog", {
title: __("Open file")
}).then((d) => {
this.filehandle = d.file.path.asFileHandle();
return this.read();
});
2018-07-24 19:18:07 +02:00
}
saveFile() {
2020-05-21 21:56:16 +02:00
this.filehandle.cache = this.editor.value;
if (this.filehandle.path !== "Untitled") {
2018-07-24 19:18:07 +02:00
return this.write();
}
2020-05-21 21:56:16 +02:00
return this.openDialog("FileDialog", {
title: __("Save as"),
file: this.filehandle
}).then((f) => {
var d;
d = f.file.path.asFileHandle();
if (f.file.type === "file") {
d = d.parent();
}
this.filehandle.setPath(`${d.path}/${f.name}`);
return this.write();
2018-07-24 19:18:07 +02:00
});
}
read() {
this.editor.value = "";
2020-05-21 21:56:16 +02:00
if (this.filehandle === null) {
this.filehandle = "Untitled".asFileHandle();
2018-07-24 19:18:07 +02:00
this.scheme.set("apptitle", "Untitled");
return;
}
2020-05-21 21:56:16 +02:00
return this.filehandle.read().then((d) => {
this.scheme.set("apptitle", this.filehandle.path);
return this.editor.value = d;
}).catch((e) => {
return this.error(__("Unable to read file content"));
2018-07-24 19:18:07 +02:00
});
}
write() {
2020-05-21 21:56:16 +02:00
return this.filehandle.write("text/plain").then((d) => {
this.filehandle.dirty = false;
return this.scheme.set("apptitle", `${this.filehandle.path}`);
}).catch((e) => {
return this.error(__("Error saving file {0}", this.filehandle.path), e);
2018-07-24 19:18:07 +02:00
});
}
cleanup(e) {
2020-05-21 21:56:16 +02:00
if (!this.filehandle.dirty) {
2018-07-24 19:18:07 +02:00
return;
}
e.preventDefault();
2020-05-21 21:56:16 +02:00
return this.ask({
title: "__(Quit)",
text: "__(Quit without saving?)"
}).then(() => {
this.filehandle.dirty = false;
return this.quit();
2018-07-24 19:18:07 +02:00
});
}
};
this.OS.register("TinyEditor", TinyEditor);
}).call(this);