antosdk-apps/TinyEditor/coffees/main.coffee

84 lines
2.8 KiB
CoffeeScript
Raw Permalink Normal View History

2020-06-05 17:45:07 +02:00
class TinyEditor extends this.OS.application.BaseApplication
2018-07-24 19:18:07 +02:00
constructor: ( args ) ->
super "TinyEditor", args
main: () ->
@editor = @find "editor"
2020-05-21 21:56:16 +02:00
@bindKey "ALT-N", () => @newFile()
@bindKey "ALT-O", () => @openFile()
@bindKey "CTRL-S", () => @saveFile()
2020-05-21 22:24:30 +02:00
@filehandle = if @args and @args.length > 0 then @args[0].path.asFileHandle() else null
2020-05-21 21:56:16 +02:00
$(@editor).on 'input', (e) =>
return if @filehandle.dirty is true
@filehandle.dirty = true
2020-06-05 17:45:07 +02:00
@scheme.apptitle = "#{@filehandle.path}*"
2018-07-24 19:18:07 +02:00
@read()
menu: () ->
m = [
{
text: "__(File)",
2020-06-05 17:45:07 +02:00
nodes: [
2018-07-24 19:18:07 +02:00
{ 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) =>
2020-06-05 17:45:07 +02:00
switch e.data.item.data.dataid
2020-05-21 21:56:16 +02:00
when "new" then @newFile()
when "open" then @openFile()
when "save" then @saveFile()
2018-07-24 19:18:07 +02:00
}
]
m
newFile: () ->
2020-05-21 21:56:16 +02:00
@filehandle = null
2018-07-24 19:18:07 +02:00
@read()
2018-07-25 16:20:13 +02:00
openFile: () ->
2020-05-21 21:56:16 +02:00
@openDialog "FileDialog", { title: __("Open file") }
.then (d) =>
@filehandle = d.file.path.asFileHandle()
@read()
,
2018-07-24 19:18:07 +02:00
saveFile: () ->
2020-05-21 21:56:16 +02:00
@filehandle.cache = @editor.value
return @write() unless @filehandle.path is "Untitled"
@openDialog("FileDialog", {
title: __("Save as"),
file: @filehandle
}).then (f) =>
d = f.file.path.asFileHandle()
d = d.parent() if f.file.type is "file"
@filehandle.setPath "#{d.path}/#{f.name}"
@write()
2018-07-24 19:18:07 +02:00
read: () ->
@editor.value = ""
2020-05-21 21:56:16 +02:00
if @filehandle is null
@filehandle = "Untitled".asFileHandle()
2020-06-05 17:45:07 +02:00
@scheme.apptitle = "Untitled"
2018-07-24 19:18:07 +02:00
return
2020-05-21 21:56:16 +02:00
@filehandle.read().then (d) =>
2020-06-05 17:45:07 +02:00
@scheme.apptitle = @filehandle.path
2020-05-21 21:56:16 +02:00
@editor.value = d
.catch (e) => @error __("Unable to read file content")
2018-07-24 19:18:07 +02:00
write: () ->
2020-05-21 21:56:16 +02:00
@filehandle.write("text/plain").then (d) =>
@filehandle.dirty = false
2020-06-05 17:45:07 +02:00
@scheme.apptitle = "#{@filehandle.path}"
2020-05-21 21:56:16 +02:00
.catch (e) => @error __("Error saving file {0}", @filehandle.path), e
2018-07-24 19:18:07 +02:00
cleanup: (e) ->
2020-05-21 21:56:16 +02:00
return unless @filehandle.dirty
2018-07-24 19:18:07 +02:00
e.preventDefault()
2020-05-21 21:56:16 +02:00
@ask { title: "__(Quit)", text: "__(Quit without saving?)" }
2020-06-05 17:45:07 +02:00
.then (d) =>
return unless d
2020-05-21 21:56:16 +02:00
@filehandle.dirty = false
@quit()
2018-07-24 19:18:07 +02:00
this.OS.register "TinyEditor", TinyEditor