antosdk-apps/Booklet/coffees/main.coffee

269 lines
9.4 KiB
CoffeeScript
Raw Normal View History

2019-11-24 20:33:14 +01:00
class Booklet extends this.OS.GUI.BaseApplication
constructor: ( args ) ->
super "Booklet", args
main: () ->
2019-11-26 22:35:25 +01:00
me = @
@tree = @find "toc-ui"
@currentToc = undefined
2019-11-29 22:07:31 +01:00
@dirty = false
2019-11-28 23:11:46 +01:00
@emux = false
2019-11-28 18:35:55 +01:00
@on "treeselect", (e) ->
2019-11-28 23:11:46 +01:00
return me.reloadEditor() if (me.currentToc is e) or (e is undefined) or (e.treepath is 0)
2019-11-29 22:07:31 +01:00
e.treepath = e.path
me.load(e).then ()->
me.open e
.catch (msg) ->
e.loaded = true
me.open e
me.error __("Error when loading '{0}': {1}", e.name, msg)
2019-11-28 18:35:55 +01:00
2019-11-24 20:33:14 +01:00
@initEditor()
@resizeContent()
2019-11-26 22:35:25 +01:00
@tree.contextmenuHandler = (e, m) ->
menus = me.contextMenu()
return unless menus
m.set "items", menus
m.set "onmenuselect", (evt) ->
me[evt.item.data.dataid]()
m.show e
2019-11-28 18:35:55 +01:00
@editor.codemirror.on "change", () ->
2019-11-28 23:11:46 +01:00
return if me.emux
2019-11-28 18:35:55 +01:00
return unless me.currentToc
me.currentToc.descFile.dirty = true
2019-11-29 22:07:31 +01:00
me.dirty = true
2019-11-26 22:35:25 +01:00
newChapter: () ->
2019-11-29 22:07:31 +01:00
return @error __("No book selected") unless @currentToc and @currentToc.type is "Book"
2019-11-28 18:35:55 +01:00
ch = new BookletChapter(@book)
@displayToc()
2019-11-28 23:11:46 +01:00
ch.treepath = ch.path
2019-11-26 22:35:25 +01:00
2019-11-28 18:35:55 +01:00
newSection: () ->
2019-11-29 22:07:31 +01:00
return @error __("No chapter selected") unless @currentToc and @currentToc.type is "Chapter"
2019-11-28 18:35:55 +01:00
sec = new BookletSection(@currentToc)
@displayToc()
2019-11-28 23:11:46 +01:00
sec.treepath = sec.path
2019-11-28 18:35:55 +01:00
newFile: () ->
2019-11-29 22:07:31 +01:00
return @error __("No section selected") unless @currentToc and @currentToc.type is "Section"
2019-11-28 18:35:55 +01:00
file = new BookletFile(@currentToc)
@displayToc()
2019-11-28 23:11:46 +01:00
file.treepath = file.path
delete: () ->
me = @
return @error __("No entrie select") unless @currentToc
fn = () ->
me.currentToc = undefined
me.displayToc()
me.reloadEditor()
@currentToc.remove().then () ->
me.notify __("Entrie deleted")
fn()
.catch (e) ->
me.error e
fn()
2019-11-29 22:07:31 +01:00
load: (entry) ->
me = @
return new Promise (r, e) ->
return r() if entry.loaded
entry.descFile.meta (d) ->
return e d.error if d.error
entry.descFile.read (data) ->
entry.descFile.cache = data
entry.loaded = true
entry.descFile.dirty = false
r()
2019-11-26 22:35:25 +01:00
contextMenu: () ->
return undefined unless @currentToc
switch @currentToc.type
2019-11-29 22:07:31 +01:00
when "Book"
2019-11-26 22:35:25 +01:00
return [
{ text: __("New chapter"), dataid: "newChapter" },
2019-11-28 23:11:46 +01:00
{ text: __("Delete book"), dataid: "delete" }
2019-11-26 22:35:25 +01:00
]
2019-11-29 22:07:31 +01:00
when "Chapter"
2019-11-26 22:35:25 +01:00
return [
{ text: __("New section"), dataid: "newSection" },
2019-11-28 23:11:46 +01:00
{ text: __("Delete chapter"), dataid: "delete" }
2019-11-26 22:35:25 +01:00
]
2019-11-29 22:07:31 +01:00
when "Section"
2019-11-26 22:35:25 +01:00
return [
{ text: __("New file"), dataid: "newFile" },
2019-11-28 23:11:46 +01:00
{ text: __("Delete section"), dataid: "delete" }
]
2019-11-29 22:07:31 +01:00
when "File"
2019-11-28 23:11:46 +01:00
return [
{ text: __("Delete file"), dataid: "delete" }
2019-11-26 22:35:25 +01:00
]
return undefined
2019-11-24 20:33:14 +01:00
2019-11-30 23:04:36 +01:00
shareFile: (mimes,f) ->
me = @
me.openDialog "FileDiaLog", (d, n, p) ->
p.asFileHandler().publish (r) ->
return me.error __("Cannot export file for embedding to text") if r.error
f r.result
, __("Select a file"), { mimes: mimes }
2019-11-24 20:33:14 +01:00
initEditor: ()->
markarea = @find "markarea"
@container = @find "mycontainer"
@previewOn = false
@editormux = false
me = @
@editor = new SimpleMDE
element: markarea
autofocus: true
tabSize: 4
indentWithTabs: true
toolbar: [
"bold", "italic", "heading", "|", "quote", "code",
"unordered-list", "ordered-list", "|", "link",
2019-11-30 23:04:36 +01:00
"image", "table", "horizontal-rule",
{
name: "image",
className: "fa fa-file-image-o",
action: (e) ->
me.shareFile ["image/.*"], (path) ->
doc = me.editor.codemirror.getDoc()
doc.replaceSelection "![](#{me._api.handler.shared}/#{path})"
},
{
name:"Youtube",
className: "fa fa-youtube",
action: (e) ->
doc = me.editor.codemirror.getDoc()
doc.replaceSelection "[[youtube:]]"
},
{
name: "3d object",
className: "fa fa-file-image-o",
action: (e) ->
me.shareFile ["text/wavefront-obj"], (path) ->
doc = me.editor.codemirror.getDoc()
doc.replaceSelection "[[3DModel:#{me._api.handler.shared}/#{path}]]"
},
"|",
2019-11-24 20:33:14 +01:00
{
2019-11-30 23:04:36 +01:00
name: __("Preview"),
2019-11-24 20:33:14 +01:00
className: "fa fa-eye no-disable",
action: (e) ->
SimpleMDE.togglePreview e
2019-11-30 23:04:36 +01:00
#/console.log me.select ".editor-preview editor-preview-active"
renderMathInElement me.find "mycontainer"
2019-11-24 20:33:14 +01:00
}
]
@on "hboxchange", (e) -> me.resizeContent()
@bindKey "ALT-N", () -> me.actionFile "#{me.name}-New"
@bindKey "ALT-O", () -> me.actionFile "#{me.name}-Open"
2019-11-28 18:35:55 +01:00
@bindKey "CTRL-S", () -> me.actionFile "#{me.name}-Save"
2019-11-24 20:33:14 +01:00
2019-11-26 22:35:25 +01:00
reloadEditor: () ->
2019-11-28 18:35:55 +01:00
if @currentToc is undefined
@editor.value ""
return @scheme.set "apptitle", @name
@editor.value @currentToc.descFile.cache || ""
@scheme.set "apptitle", "Booklet - #{@currentToc.descFile.path}"
2019-11-24 20:33:14 +01:00
2019-11-26 22:35:25 +01:00
saveContext: () ->
2019-11-28 18:35:55 +01:00
return unless @currentToc
@currentToc.descFile.cache = @editor.value()
2019-11-24 20:33:14 +01:00
resizeContent: () ->
children = ($ @container).children()
titlebar = (($ @scheme).find ".afx-window-top")[0]
toolbar = children[1]
statusbar = children[4]
cheight = ($ @scheme).height() - ($ titlebar).height() - ($ toolbar).height() - ($ statusbar).height() - 40
($ children[2]).css("height", cheight + "px")
menu: () ->
me = @
menu = [{
text: "__(File)",
child: [
{ text: "__(New booklet)", dataid: "#{@name}-New", shortcut: "A-N" },
{ text: "__(Open a booklet)", dataid: "#{@name}-Open", shortcut: "A-O" }
2019-11-28 18:35:55 +01:00
{ text: "__(Save a booklet)", dataid: "#{@name}-Save", shortcut: "C-S" }
2019-11-24 20:33:14 +01:00
],
onmenuselect: (e) -> me.actionFile e.item.data.dataid
}]
menu
actionFile: (e) ->
me = @
switch e
when "#{@name}-Open"
2019-11-29 22:07:31 +01:00
@checkForDirty () ->
me.openDialog "FileDiaLog", ( d, f ) ->
me.book = new BookletBook(d)
me.book.read(d).then () ->
me.book.treepath = me.book.path
me.tree.set "selectedItem", undefined
me.displayToc()
me.notify __("Book loaded")
.catch (msg) ->
me.error __("Cannot load book: {0}", msg)
, __("Open file"), { mimes: ['dir'] }
2019-11-24 20:33:14 +01:00
when "#{@name}-New"
2019-11-26 22:35:25 +01:00
@openDialog "FileDiaLog", ( d, f ) ->
2019-11-28 18:35:55 +01:00
me.newAt "#{d}/#{f}"
, __("Open file"), { mimes: ['dir'], file: { basename: __("BookName") }}
when "#{@name}-Save"
2019-11-28 23:11:46 +01:00
return unless me.book
me.saveContext() if me.currentToc
me.displayToc()
me.book.save().then () ->
2019-11-29 22:07:31 +01:00
me.dirty = false
2019-11-28 23:11:46 +01:00
me.notify __("Book saved")
.catch (e) ->
me.error __("Can't save the book : {0}", e)
2019-11-26 22:35:25 +01:00
2019-11-29 22:07:31 +01:00
checkForDirty: (f) ->
return f() unless @dirty
@_gui.openDialog "YesNoDialog", (d) ->
# console.log d
if d
f()
, __("Continue ?"), { text: __("Book is unsaved, you want to continue ?") }
2019-11-28 18:35:55 +01:00
open: (toc) ->
2019-11-29 22:07:31 +01:00
me = @
me.emux = true
me.saveContext()
me.currentToc = toc
me.reloadEditor()
me.displayToc()
me.emux = false
2019-11-28 23:11:46 +01:00
openBook: (metaFile) ->
2019-11-26 22:35:25 +01:00
newAt: (folder) ->
2019-11-28 18:35:55 +01:00
@tree.set "selectedItem", false
2019-11-29 22:07:31 +01:00
@book = new BookletBook(folder)
2019-11-28 18:35:55 +01:00
@book.treepath = @book.path
@currentToc = undefined
@reloadEditor()
2019-11-26 22:35:25 +01:00
@displayToc()
displayToc: () ->
2019-11-28 18:35:55 +01:00
@book.toc()
@tree.set "data", @book
2019-11-26 22:35:25 +01:00
2019-11-30 23:04:36 +01:00
cleanup: (evt) ->
return unless @dirty
me = @
evt.preventDefault()
@checkForDirty () ->
me.dirty = false
me.quit()
2019-11-24 20:33:14 +01:00
Booklet.dependencies = [ "mde/simplemde.min" ]
this.OS.register "Booklet", Booklet