This commit is contained in:
lxsang
2019-11-26 21:35:25 +00:00
parent 46b048f221
commit 446480d7be
5 changed files with 374 additions and 114 deletions

View File

@ -1,5 +1,5 @@
class BookletEntry
constructor: (@name) ->
constructor: () ->
@markAsDirty()
save: () ->
@ -9,10 +9,16 @@ class BookletEntry
markAsClean: () -> @dirty = false
toc: () ->
titleFromFile:(file) ->
content = file.cache
title = (new RegExp "^#+(.*)\n", "g").exec content
return "Untitled" unless title and title.length is 2
return title[1].trim()
class BookletFolder extends BookletEntry
constructor: (name) ->
super name
constructor: () ->
super()
save: (apif) ->
@ -22,8 +28,8 @@ class BookletFolder extends BookletEntry
class Book extends BookletFolder
constructor: (@path, name) ->
super name
constructor: (@path) ->
super()
@chapters = []
@metaFile = "#{@path}/meta.json".asFileHandler()
@descFile = "#{@path}/book.md".asFileHandler()
@ -51,16 +57,15 @@ class Book extends BookletFolder
toc: () ->
return {
name: @name,
path: @path,
description: @descFile.path,
meta: @metaFile.path,
entries: v.toc() for v in @chapters
target: @,
name: @titleFromFile(@descFile),
nodes: v.toc() for v in @chapters,
type: 'book'
}
class BookletChapter extends BookletFolder
constructor: (@book, name) ->
super name
constructor: (@book) ->
super()
@book.addChapter @
@sections = []
@path = "#{@book.path}/#{@book.size()}"
@ -76,16 +81,30 @@ class BookletChapter extends BookletFolder
toc: () ->
return {
name: @name,
path: @path,
meta: @metaFile.path,
description: @descFile.path,
entries: v.toc() for v in @sections
target: @,
name: @titleFromFile(@descFile),
nodes: v.toc() for v in @sections,
type: 'chapter'
}
save:(handle) ->
v.save handle for v in @sections
me = @
if @dirty
if @descFile.dirty
@descFile.write "text/plain", (r) ->
handle.error __("Fail to save file {0}: {1}", me.descFile.path, r.error) if r.error
@metaFile.cache = @toc()
@metaFile.dirty = true
@metaFile.write "object", (r) ->
return handle.error __("Fail to write book meta: {0}", r.error)
me.markAsClean
handle.notify __("chapter saved")
class BookletSection extends BookletFolder
constructor: (@chapter, name) ->
super name
constructor: (@chapter) ->
super()
@chapter.addSection @
@path = "#{@chapter.path}/#{@chapter.size()}"
@files = []
@ -97,26 +116,46 @@ class BookletSection extends BookletFolder
toc: () ->
return {
name: @name,
path: @path,
description: @descFile.path,
entries: v.toc() for v in @files
target: @,
name: @titleFromFile(@descFile),
nodes: v.toc() for v in @files,
type: 'section'
}
save: () ->
v.save handle for v in @sections
me = @
if @dirty
if @descFile.dirty
@descFile.write "text/plain", (r) ->
handle.error __("Fail to save file {0}: {1}", me.descFile.path, r.error) if r.error
me.markAsClean
handle.notify __("section saved")
size: () ->
return @files.length
class BookletFile extends BookletEntry
constructor: (@section) ->
super ""
super()
@section.addFile @
@path = "#{@section.path}/#{@section.size()}.md"
@handle = @path.asFileHandler()
@descFile = @path.asFileHandler()
getTitle: () ->
console.log "hello"
save: (handle) ->
v.save @descFile for v in @sections
me = @
if @dirty
if @descFile.dirty
@descFile.write "text/plain", (r) ->
handle.error __("Fail to save file {0}: {1}", me.descFile.path, r.error) if r.error
me.markAsClean
handle.notify __("Book saved")
toc: () ->
return {
name: @name,
path: @path
target: @,
name: @titleFromFile(@handle),
type: 'file'
}

View File

@ -3,8 +3,46 @@ class Booklet extends this.OS.GUI.BaseApplication
super "Booklet", args
main: () ->
me = @
@tree = @find "toc-ui"
@currentToc = undefined
@tree.set "ontreeselect", (e) ->
me.saveContext()
me.currfile = e.target.descFile
me.reloadEditor()
me.currentToc = e
@initEditor()
@resizeContent()
@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
newChapter: () ->
console.log @currentToc
contextMenu: () ->
return undefined unless @currentToc
switch @currentToc.type
when "book"
return [
{ text: __("New chapter"), dataid: "newChapter" },
{ text: __("Delete book"), dataid: "deleteBook" }
]
when "chapter"
return [
{ text: __("New section"), dataid: "newSection" },
{ text: __("Delete chapter"), dataid: "deleteChapter" }
]
when "section"
return [
{ text: __("New file"), dataid: "newFile" },
{ text: __("Delete section"), dataid: "deleteSection" }
]
return undefined
initEditor: ()->
markarea = @find "markarea"
@ -45,21 +83,12 @@ class Booklet extends this.OS.GUI.BaseApplication
@bindKey "ALT-N", () -> me.actionFile "#{me.name}-New"
@bindKey "ALT-O", () -> me.actionFile "#{me.name}-Open"
@createBook()
reloadEditor: () ->
@editor.value @currfile.cache
@scheme.set "apptitle", "Booklet - #{@currfile.basename}"
createBook: () ->
book = new Book("home://test", "mybook")
c1 = new BookletChapter(book, "Chapter one")
c2 = new BookletChapter(book, "Chapter two")
sec1 = new BookletSection(c1, "section 1 in c1")
sec2 = new BookletSection(c1, "section 2 in c1")
sec3 = new BookletSection(c2, "section 1 in c2")
f1 = new BookletFile(sec1)
f2 = new BookletFile(sec2)
f3 = new BookletFile(sec3)
f4 = new BookletFile(sec1)
console.log(book.toc())
saveContext: () ->
@currfile.cache = @editor.value()
resizeContent: () ->
children = ($ @container).children()
@ -83,20 +112,29 @@ class Booklet extends this.OS.GUI.BaseApplication
menu
actionFile: (e) ->
console.log "Action file fired"
###
me = @
switch e
when "#{@name}-Open"
@openDialog "FileDiaLog", ( d, f ) ->
me.open "#{d}/#{f}".asFileHandler()
, __("Open file")
, __("Open file"), { mimes: me.meta().mimes }
when "#{@name}-New"
@currfile = "Untitled".asFileHandler()
@currfile.cache = ""
@editor.value("")
###
@openDialog "FileDiaLog", ( d, f ) ->
me.newAt d
, __("Open file"), { mimes: ['dir'] }
open: (file) ->
newAt: (folder) ->
@book = new Book(folder)
@displayToc()
displayToc: () ->
toc = @book.toc()
console.log toc
@tree.set "data", toc
Booklet.dependencies = [ "mde/simplemde.min" ]
this.OS.register "Booklet", Booklet