booklet now compatible with new API

This commit is contained in:
lxsang
2020-06-06 23:49:13 +02:00
parent d20db3e0fb
commit ae39688216
126 changed files with 281 additions and 18179 deletions

View File

@ -1,6 +1,6 @@
class BookletEntry
constructor: (@name) ->
constructor: (@text) ->
@loaded = true
save: () ->
@ -11,30 +11,28 @@ class BookletEntry
updateName:() ->
return @name unless @descFile.dirty
# return @text unless @descFile.dirty
t = (new RegExp "^\s*#+(.*)\n", "g").exec @descFile.cache
return @name unless t and t.length is 2
return @text unless t and t.length is 2
@metaFile.dirty = true if @hasMeta and @metaFile
@parent.metaFile.dirty = true if @parent and @parent.hasMeta and @parent.metaFile
@name = t[1].trim()
@text = t[1].trim()
remove: () ->
me = @
return new Promise (r, e) ->
f = me.path.asFileHandler()
f.meta (d) ->
if d.error
return r() unless me.parent
return me.parent.removeChild(me).then () ->
r()
.catch (msg) -> e msg
else
f.remove (ret) ->
return e ret.error if ret.error
return r() unless me.parent
me.parent.removeChild(me).then () ->
return new Promise (r, e) =>
f = @path.asFileHandle()
f.meta().then (d) =>
f.remove().then (ret) =>
return r() unless @parent
@parent.removeChild(@).then () ->
r()
.catch (msg) -> e msg
.catch (msg) -> e __e msg
.catch (msg) -> e __e msg
.catch (msg) =>
return r() unless @parent
return @parent.removeChild(@).then () ->
r()
.catch (msg) -> e __e msg
class BookletFolder extends BookletEntry
constructor: (@type, @path, @hasMeta) ->
@ -44,8 +42,8 @@ class BookletFolder extends BookletEntry
init: () ->
@cnt = 0
@nodes = []
@metaFile = "#{@path}/meta.json".asFileHandler() if @hasMeta
@descFile = "#{@path}/INTRO.md".asFileHandler()
@metaFile = "#{@path}/meta.json".asFileHandle() if @hasMeta
@descFile = "#{@path}/INTRO.md".asFileHandle()
add: (chap) ->
chap.parent = @
@ -55,124 +53,125 @@ class BookletFolder extends BookletEntry
@cnt = @cnt + 1
removeChild: (child) ->
me = @
#v.treepath = v.path for v in @nodes if @nodes
return new Promise (r, e) ->
me.nodes.splice (me.nodes.indexOf child), 1
#if me.nodes.includes child
return r() unless me.hasMeta and me.metaFile
me.metaFile.dirty = true
me.updateMeta().then () ->
return new Promise (r, e) =>
@nodes.splice (@nodes.indexOf child), 1
#if @nodes.includes child
return r() unless @hasMeta and @metaFile
@metaFile.dirty = true
@updateMeta().then () ->
r()
.catch (msg) ->
e msg
.catch (msg) -> e __e msg
read: (folderPath) ->
me = @
return new Promise (r, e) ->
me.path = folderPath
me.init()
me.loaded = false
me.metaFile.meta (d) ->
return e d.error if d.error
me.metaFile.read (data) ->
return new Promise (r, e) =>
@path = folderPath
@init()
@loaded = false
@metaFile.meta().then (d) =>
@metaFile.read("json").then (data) =>
# load all child
me.name = data.name
@text = data.name
list = []
list[i] = v for v,i in data.entries
fn = (l) ->
fn = (l) =>
if l.length is 0
me.cnt = data.cnt
@cnt = data.cnt
return r()
el = (l.splice 0, 1)[0]
#console.log "create", el.type
obj = new NS[el.type]( me )
obj = new NS[el.type]( @ )
obj.name = el.name
obj.read(el.path).then () ->
obj.read(el.path).then () =>
fn l
.catch (msg) ->
.catch (msg) =>
fn l
return fn list
, "json"
.catch (msg) -> e __e msg
.catch (msg) -> e __e msg
size: () ->
return @nodes.length
mkdir: () ->
me = @
return new Promise (r, e) ->
dir = me.path.asFileHandler()
dir.meta (d) ->
return r() unless d.error
return new Promise (r, e) =>
dir = @path.asFileHandle()
dir.meta().then (d) =>
return r()
.catch (ex) =>
bname = dir.basename
dir = dir.parent().asFileHandler()
dir.mk bname, (result) ->
e __("Error when create directory: {0}", result.error) if result.error
r()
dir = dir.parent().asFileHandle()
dir.mk bname
.then (result) => r()
.catch (msg) -> e __e msg
mkdirs: () ->
me = @
return new Promise (r, e) ->
return new Promise (r, e) =>
list = []
list[i] = v for v, i in me.nodes if me.type isnt 'Section'
me.mkdir().then () ->
fn = (l) ->
list[i] = v for v, i in @nodes if @type isnt 'Section'
@mkdir().then () =>
fn = (l) =>
return r() if l.length is 0
el = (l.splice 0, 1)[0]
el.mkdirs().then () ->
el.mkdirs().then () =>
fn l
.catch (msg) -> e msg
.catch (msg) -> e __e msg
return fn list
.catch (msg) -> e msg
.catch (msg) -> e __e msg
updateMeta: () ->
me = @
return new Promise (r, e) ->
return r() unless me.metaFile.dirty
return new Promise (r, e) =>
return r() unless @metaFile.dirty
entries = []
entries[i] = {name: v.name, path:v.path, type:v.type} for v,i in me.nodes
entries[i] = {name: v.name, path:v.path, type:v.type} for v,i in @nodes
data = {
name: me.name,
name: @text,
entries: entries,
cnt: me.cnt,
meta: me.hasMeta
cnt: @cnt,
meta: @hasMeta
}
me.metaFile.cache = data
me.metaFile.write "object", (d) ->
return e d.error if d.error
me.metaFile.dirty = false
#console.log "saved " + me.metaFile.path
r()
@metaFile.cache = data
@metaFile.write "object"
.then (d) =>
@metaFile.dirty = false
r()
.catch (msg) -> e __e msg
update: () ->
me = @
return new Promise (r, e) ->
me.updateMeta().then () ->
return r() unless me.descFile.dirty
me.descFile.write "text/plain", (d) ->
return e d.error if d.error
me.descFile.dirty = false
#console.log "saved " + me.descFile.path
r()
.catch (msg) -> e msg
return new Promise (r, e) =>
@updateMeta().then () =>
return r() unless @descFile.dirty
@descFile.write "text/plain"
.then (d) =>
@descFile.dirty = false
#console.log "saved " + @descFile.path
r()
.catch (msg) -> e __e msg
.catch (msg) -> e __e msg
updateAll: () ->
me = @
return new Promise (r, e) ->
return new Promise (r, e) =>
list = []
list[i] = v for v, i in me.nodes
me.update().then () ->
fn = (l) ->
list[i] = v for v, i in @nodes
@update().then () =>
fn = (l) =>
return r() if l.length is 0
el = (l.splice 0, 1)[0]
el.updateAll().then () ->
el.updateAll().then () =>
fn l
.catch (msg) -> e msg
.catch (msg) -> e __e msg
return fn list
.catch (msg) -> e msg
.catch (msg) -> e __e msg
toc: () ->
@updateName()
@ -185,13 +184,13 @@ class BookletBook extends BookletFolder
super 'Book', path, true
save:() ->
me = @
return new Promise (r, e) ->
me.mkdirs().then () ->
me.updateAll().then () ->
return new Promise (r, e) =>
@mkdirs().then () =>
@updateAll().then () =>
r()
.catch (msg) -> e msg
. catch (msg) -> e msg
.catch (msg) -> e __e msg
. catch (msg) -> e __e msg
class BookletChapter extends BookletFolder
@ -214,24 +213,26 @@ class BookletFile extends BookletEntry
@hasMeta = false
@type = "File"
@path = "#{@section.path}/f_#{@section.cnt}.md"
@descFile = @path.asFileHandler()
@descFile = @path.asFileHandle()
@section.add @
updateAll: ()->
me = @
return new Promise (r, e) ->
return r() unless me.descFile.dirty
me.descFile.write "text/plain", (d) ->
return e d.error if d.error
me.descFile.dirty = false
#console.log "saved" + me.descFile.path
r()
return new Promise (r, e) =>
return r() unless @descFile.dirty
@descFile.write "text/plain"
.then (d) =>
@descFile.dirty = false
#console.log "saved" + @descFile.path
r()
.catch (msg) => e __e msg
read: (p) ->
me = @
return new Promise (r, e) ->
me.loaded = false
me.treepath = p
return new Promise (r, e) =>
@loaded = false
@treepath = p
@path = p
@descFile = @path.asFileHandle()
r()
toc: () ->

View File

@ -1,37 +1,38 @@
class Booklet extends this.OS.GUI.BaseApplication
class Booklet extends this.OS.application.BaseApplication
constructor: ( args ) ->
super "Booklet", args
main: () ->
me = @
@tree = @find "toc-ui"
@currentToc = undefined
@dirty = false
@emux = false
@on "treeselect", (e) ->
return me.reloadEditor() if (me.currentToc is e) or (e is undefined) or (e.treepath is 0)
@on "treeselect", (evt) =>
e = evt.data.item.data
return @reloadEditor() if (@currentToc is e) or (e is undefined) or (e.treepath is 0)
e.treepath = e.path
me.load(e).then ()->
me.open e
.catch (msg) ->
@load(e).then ()=>
@open e
.catch (msg) =>
e.loaded = true
me.open e
me.error __("Error when loading '{0}': {1}", e.name, msg)
@open e
@error __("Error when loading '{0}': {1}", e.text, msg.toString()), msg
@initEditor()
@resizeContent()
@tree.contextmenuHandler = (e, m) ->
menus = me.contextMenu()
@tree.contextmenuHandle = (e, m) =>
menus = @contextMenu()
return unless menus
m.set "items", menus
m.set "onmenuselect", (evt) ->
me[evt.item.data.dataid]()
m.items = menus
m.onmenuselect = (evt) =>
@[evt.data.item.data.dataid]()
m.show e
@editor.codemirror.on "change", () ->
return if me.emux
return unless me.currentToc
me.currentToc.descFile.dirty = true
me.dirty = true
@editor.codemirror.on "change", () =>
return if @emux
return unless @currentToc
@currentToc.descFile.dirty = true
@dirty = true
newChapter: () ->
return @error __("No book selected") unless @currentToc and @currentToc.type is "Book"
@ -52,30 +53,32 @@ class Booklet extends this.OS.GUI.BaseApplication
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 = () =>
@currentToc = undefined
@displayToc()
@reloadEditor()
@currentToc.remove().then () =>
@notify __("Entrie deleted")
fn()
.catch (e) ->
me.error e
.catch (e) =>
@error e.toString(), e
fn()
load: (entry) ->
me = @
return new Promise (r, e) ->
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.meta().then (d) =>
entry.descFile.read().then (data) =>
entry.descFile.cache = data
entry.loaded = true
entry.descFile.dirty = false
r()
.catch (msg) -> e __e msg
.catch (msg) -> e __e msg
contextMenu: () ->
return undefined unless @currentToc
@ -102,19 +105,22 @@ class Booklet extends this.OS.GUI.BaseApplication
return undefined
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
@openDialog "FileDialog", { title: __("Select a file"), mimes: mimes }
.then (d) =>
d.file.path.asFileHandle().publish().then (r) ->
f r.result
, __("Select a file"), { mimes: mimes }
.catch (msg) =>
return @error __("Cannot export file for embedding to text"), msg
.catch (msg) =>
return @error msg.toString(), msg
initEditor: ()->
markarea = @find "markarea"
@container = @find "mycontainer"
@previewOn = false
@editormux = false
me = @
@editor = new SimpleMDE
element: markarea
autofocus: true
@ -127,25 +133,25 @@ class Booklet extends this.OS.GUI.BaseApplication
{
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})"
action: (e) =>
@shareFile ["image/.*"], (path) =>
doc = @editor.codemirror.getDoc()
doc.replaceSelection "![](#{@_api.handler.shared}/#{path})"
},
{
name:"Youtube",
className: "fa fa-youtube",
action: (e) ->
doc = me.editor.codemirror.getDoc()
action: (e) =>
doc = @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}]]"
action: (e) =>
@shareFile ["text/wavefront-obj"], (path) =>
doc = @editor.codemirror.getDoc()
doc.replaceSelection "[[3DModel:#{@_api.handler.shared}/#{path}]]"
},
"|",
{
@ -153,21 +159,21 @@ class Booklet extends this.OS.GUI.BaseApplication
className: "fa fa-eye no-disable",
action: (e) ->
SimpleMDE.togglePreview e
#/console.log me.select ".editor-preview editor-preview-active"
renderMathInElement me.find "mycontainer"
#/console.log @select ".editor-preview editor-preview-active"
renderMathInElement @find "mycontainer"
}
]
@on "hboxchange", (e) -> me.resizeContent()
@bindKey "ALT-N", () -> me.actionFile "#{me.name}-New"
@bindKey "ALT-O", () -> me.actionFile "#{me.name}-Open"
@bindKey "CTRL-S", () -> me.actionFile "#{me.name}-Save"
@on "hboxchange", (e) => @resizeContent()
@bindKey "ALT-N", () => @actionFile "#{@name}-New"
@bindKey "ALT-O", () => @actionFile "#{@name}-Open"
@bindKey "CTRL-S", () => @actionFile "#{@name}-Save"
reloadEditor: () ->
if @currentToc is undefined
@editor.value ""
return @scheme.set "apptitle", @name
return @scheme.apptitle = @name
@editor.value @currentToc.descFile.cache || ""
@scheme.set "apptitle", "Booklet - #{@currentToc.descFile.path}"
@scheme.apptitle = "Booklet - #{@currentToc.descFile.path}"
saveContext: () ->
return unless @currentToc
@ -183,69 +189,71 @@ class Booklet extends this.OS.GUI.BaseApplication
menu: () ->
me = @
menu = [{
text: "__(File)",
child: [
nodes: [
{ text: "__(New booklet)", dataid: "#{@name}-New", shortcut: "A-N" },
{ text: "__(Open a booklet)", dataid: "#{@name}-Open", shortcut: "A-O" }
{ text: "__(Save a booklet)", dataid: "#{@name}-Save", shortcut: "C-S" }
],
onmenuselect: (e) -> me.actionFile e.item.data.dataid
onchildselect: (e) => @actionFile e.data.item.data.dataid
}]
menu
actionFile: (e) ->
me = @
switch e
when "#{@name}-Open"
@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'] }
@checkForDirty () =>
@openDialog "FileDialog", { title:__("Open file"), mimes: ['dir'] }
.then (d) =>
@book = new BookletBook(d.file.path)
@book.read(d.file.path).then () =>
@book.treepath = @book.path
@tree.selectedItem = undefined
@displayToc()
@notify __("Book loaded")
.catch (msg) =>
@error __("Cannot load book: {0}", msg.toString()), msg
.catch (msg) => @error msg.toString(), msg
when "#{@name}-New"
@openDialog "FileDiaLog", ( d, f ) ->
me.newAt "#{d}/#{f}"
, __("Open file"), { mimes: ['dir'], file: { basename: __("BookName") }}
@openDialog "FileDialog", { title: __("Open file"), mimes: ['dir'], file: { basename: __("BookName") }}
.then (d) =>
@newAt "#{d.file.path}/#{d.name}"
.catch (msg) => @error msg.toString(), msg
when "#{@name}-Save"
return unless me.book
me.saveContext() if me.currentToc
me.displayToc()
me.book.save().then () ->
me.dirty = false
me.notify __("Book saved")
.catch (e) ->
me.error __("Can't save the book : {0}", e)
return unless @book
@saveContext() if @currentToc
@displayToc()
@book.save().then () =>
@dirty = false
@notify __("Book saved")
.catch (e) =>
@error __("Can't save the book : {0}", e.toString()), e
checkForDirty: (f) ->
return f() unless @dirty
@_gui.openDialog "YesNoDialog", (d) ->
@ask {title: __("Continue ?"), text: __("Book is unsaved, you want to continue ?") }
.then (d) =>
# console.log d
if d
f()
, __("Continue ?"), { text: __("Book is unsaved, you want to continue ?") }
open: (toc) ->
me = @
me.emux = true
me.saveContext()
me.currentToc = toc
me.reloadEditor()
me.displayToc()
me.emux = false
openBook: (metaFile) ->
open: (toc) ->
console.log toc
@emux = true
@saveContext()
@currentToc = toc
@reloadEditor()
@displayToc()
@emux = false
newAt: (folder) ->
@tree.set "selectedItem", false
@tree.selectedItem = undefined
@book = new BookletBook(folder)
@book.treepath = @book.path
@currentToc = undefined
@ -254,16 +262,19 @@ class Booklet extends this.OS.GUI.BaseApplication
displayToc: () ->
@book.toc()
@tree.set "data", @book
@tree.data = @book
@tree.expandAll()
cleanup: (evt) ->
return unless @dirty
me = @
evt.preventDefault()
@checkForDirty () ->
me.dirty = false
me.quit()
@checkForDirty () =>
@dirty = false
@quit()
Booklet.dependencies = [ "mde/simplemde.min" ]
Booklet.dependencies = [
"os://scripts/mde/simplemde.min.js",
"os://scripts/mde/simplemde.min.css"
]
this.OS.register "Booklet", Booklet