mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2025-07-23 17:29:51 +02:00
fix
This commit is contained in:
@ -11,28 +11,62 @@ class BookletEntry
|
||||
|
||||
updateName:() ->
|
||||
return @name unless @descFile.dirty
|
||||
t = (new RegExp "^\s*#+(.*)[\n,$]", "g").exec @descFile.cache
|
||||
t = (new RegExp "^\s*#+(.*)\n", "g").exec @descFile.cache
|
||||
return @name unless t and t.length is 2
|
||||
@name = 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 () ->
|
||||
r()
|
||||
.catch (msg) -> e msg
|
||||
|
||||
class BookletFolder extends BookletEntry
|
||||
constructor: (@type, @path, @hasMeta) ->
|
||||
super()
|
||||
@cnt = 0
|
||||
@nodes = []
|
||||
@metaFile = "#{@path}/meta.json".asFileHandler() if @hasMeta
|
||||
@descFile = "#{@path}/#{@type}.md".asFileHandler()
|
||||
@descFile = "#{@path}/INTRO.md".asFileHandler()
|
||||
|
||||
add: (chap) ->
|
||||
chap.parent = @
|
||||
@nodes.push chap
|
||||
@metaFile.dirty = true if @hasMeta and @metaFile
|
||||
chap.metaFile.dirty = true if chap.metaFile and chap.hasMeta
|
||||
@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 () ->
|
||||
r()
|
||||
.catch (msg) ->
|
||||
e msg
|
||||
|
||||
|
||||
size: () ->
|
||||
return @nodes.length
|
||||
|
||||
mkdir: () ->
|
||||
me = @
|
||||
console.log "making:" + me.path
|
||||
return new Promise (r, e) ->
|
||||
dir = me.path.asFileHandler()
|
||||
dir.meta (d) ->
|
||||
@ -47,66 +81,113 @@ class BookletFolder extends BookletEntry
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
list = []
|
||||
list[i] = v for v, i in me.nodes if me.hasMeta
|
||||
console.log list
|
||||
list[i] = v for v, i in me.nodes if me.type isnt 'section'
|
||||
me.mkdir().then () ->
|
||||
fn = (l) ->
|
||||
return r() if l.length is 0
|
||||
el = (l.splice 0, 1)[0]
|
||||
el.mkdirs().then () ->
|
||||
fn l
|
||||
.catch (msg) -> e msg
|
||||
return fn list
|
||||
.catch (msg) -> e msg
|
||||
|
||||
|
||||
save:(handle) ->
|
||||
@mkdirs().then ()->
|
||||
handle.notify __("All directories are created")
|
||||
.catch (msg) ->
|
||||
handle.error msg
|
||||
updateMeta: () ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
return r() unless me.metaFile.dirty
|
||||
entries = []
|
||||
entries[i] = v.path for v,i in me.nodes
|
||||
me.metaFile.cache = entries
|
||||
me.metaFile.write "object", (d) ->
|
||||
return e d.error if d.error
|
||||
me.metaFile.dirty = false
|
||||
console.log "saved " + me.metaFile.path
|
||||
r()
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
updateAll: () ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
list = []
|
||||
list[i] = v for v, i in me.nodes
|
||||
me.update().then () ->
|
||||
fn = (l) ->
|
||||
return r() if l.length is 0
|
||||
el = (l.splice 0, 1)[0]
|
||||
el.updateAll().then () ->
|
||||
fn l
|
||||
.catch (msg) -> e msg
|
||||
return fn list
|
||||
.catch (msg) -> e msg
|
||||
|
||||
toc: () ->
|
||||
@updateName()
|
||||
v.toc() for v in @nodes
|
||||
@
|
||||
|
||||
remove: (apif) ->
|
||||
|
||||
|
||||
class Book extends BookletFolder
|
||||
constructor: (path) ->
|
||||
super 'book', path, true
|
||||
|
||||
|
||||
save:() ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
me.mkdirs().then () ->
|
||||
me.updateAll().then () ->
|
||||
r()
|
||||
.catch (msg) -> e msg
|
||||
. catch (msg) -> e msg
|
||||
|
||||
|
||||
class BookletChapter extends BookletFolder
|
||||
constructor: (book) ->
|
||||
path = "#{book.path}/c_#{book.size()}"
|
||||
path = "#{book.path}/c_#{book.cnt}"
|
||||
super 'chapter', path, true
|
||||
book.add @
|
||||
|
||||
|
||||
class BookletSection extends BookletFolder
|
||||
constructor: (chapter) ->
|
||||
path = "#{chapter.path}/s_#{chapter.size()}"
|
||||
super "section", path, false
|
||||
path = "#{chapter.path}/s_#{chapter.cnt}"
|
||||
super "section", path, true
|
||||
chapter.add @
|
||||
|
||||
|
||||
class BookletFile extends BookletEntry
|
||||
constructor: (@section) ->
|
||||
super()
|
||||
@section.add @
|
||||
@path = "#{@section.path}/f_#{@section.size()}.md"
|
||||
@hasMeta = false
|
||||
@type = "file"
|
||||
@path = "#{@section.path}/f_#{@section.cnt}.md"
|
||||
@descFile = @path.asFileHandler()
|
||||
@section.add @
|
||||
|
||||
save: (handle) ->
|
||||
v.save @descFile for v in @sections
|
||||
updateAll: ()->
|
||||
me = @
|
||||
if @descFile.dirty
|
||||
@descFile.write "text/plain", (r) ->
|
||||
handle.error __("Fail to save file {0}: {1}", me.descFile.path, r.error) if r.error
|
||||
@descFile.dirty = false
|
||||
handle.notify __("Book saved")
|
||||
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()
|
||||
|
||||
toc: () ->
|
||||
@updateName()
|
||||
@
|
@ -6,8 +6,9 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me = @
|
||||
@tree = @find "toc-ui"
|
||||
@currentToc = undefined
|
||||
@emux = false
|
||||
@on "treeselect", (e) ->
|
||||
return if (me.currentToc is e) or (e is undefined) or (e.treepath is 0)
|
||||
return me.reloadEditor() if (me.currentToc is e) or (e is undefined) or (e.treepath is 0)
|
||||
me.open e
|
||||
|
||||
@initEditor()
|
||||
@ -20,6 +21,7 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me[evt.item.data.dataid]()
|
||||
m.show e
|
||||
@editor.codemirror.on "change", () ->
|
||||
return if me.emux
|
||||
return unless me.currentToc
|
||||
me.currentToc.descFile.dirty = true
|
||||
|
||||
@ -27,34 +29,55 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
return @error __("No book selected") unless @currentToc and @currentToc.type is "book"
|
||||
ch = new BookletChapter(@book)
|
||||
@displayToc()
|
||||
ch.treepath = ch.path
|
||||
|
||||
newSection: () ->
|
||||
return @error __("No chapter selected") unless @currentToc and @currentToc.type is "chapter"
|
||||
sec = new BookletSection(@currentToc)
|
||||
@displayToc()
|
||||
sec.treepath = sec.path
|
||||
|
||||
newFile: () ->
|
||||
return @error __("No section selected") unless @currentToc and @currentToc.type is "section"
|
||||
file = new BookletFile(@currentToc)
|
||||
@displayToc()
|
||||
|
||||
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()
|
||||
|
||||
contextMenu: () ->
|
||||
return undefined unless @currentToc
|
||||
switch @currentToc.type
|
||||
when "book"
|
||||
return [
|
||||
{ text: __("New chapter"), dataid: "newChapter" },
|
||||
{ text: __("Delete book"), dataid: "deleteBook" }
|
||||
{ text: __("Delete book"), dataid: "delete" }
|
||||
]
|
||||
when "chapter"
|
||||
return [
|
||||
{ text: __("New section"), dataid: "newSection" },
|
||||
{ text: __("Delete chapter"), dataid: "deleteChapter" }
|
||||
{ text: __("Delete chapter"), dataid: "delete" }
|
||||
]
|
||||
when "section"
|
||||
return [
|
||||
{ text: __("New file"), dataid: "newFile" },
|
||||
{ text: __("Delete section"), dataid: "deleteSection" }
|
||||
{ text: __("Delete section"), dataid: "delete" }
|
||||
]
|
||||
when "file"
|
||||
return [
|
||||
{ text: __("Delete file"), dataid: "delete" }
|
||||
]
|
||||
return undefined
|
||||
|
||||
@ -142,13 +165,24 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me.newAt "#{d}/#{f}"
|
||||
, __("Open file"), { mimes: ['dir'], file: { basename: __("BookName") }}
|
||||
when "#{@name}-Save"
|
||||
me.book.save(me)
|
||||
return unless me.book
|
||||
me.saveContext() if me.currentToc
|
||||
me.displayToc()
|
||||
me.book.save().then () ->
|
||||
me.notify __("Book saved")
|
||||
.catch (e) ->
|
||||
me.error __("Can't save the book : {0}", e)
|
||||
|
||||
open: (toc) ->
|
||||
@emux = true
|
||||
@saveContext()
|
||||
@currentToc = toc
|
||||
@reloadEditor()
|
||||
@displayToc()
|
||||
@emux = false
|
||||
|
||||
openBook: (metaFile) ->
|
||||
|
||||
|
||||
newAt: (folder) ->
|
||||
@tree.set "selectedItem", false
|
||||
|
Reference in New Issue
Block a user