alpha version

This commit is contained in:
lxsang 2019-11-29 21:07:31 +00:00
parent fca268070f
commit 086756621e
6 changed files with 304 additions and 75 deletions

View File

@ -1,5 +1,5 @@
(function() {
var Book, Booklet, BookletChapter, BookletEntry, BookletFile, BookletFolder, BookletSection;
var Booklet, BookletBook, BookletChapter, BookletEntry, BookletFile, BookletFolder, BookletSection, NS;
Booklet = class Booklet extends this.OS.GUI.BaseApplication {
constructor(args) {
@ -11,13 +11,20 @@
me = this;
this.tree = this.find("toc-ui");
this.currentToc = void 0;
this.dirty = false;
this.emux = false;
this.on("treeselect", function(e) {
console.log(e.treepath);
if ((me.currentToc === e) || (e === void 0) || (e.treepath === 0)) {
return me.reloadEditor();
}
return me.open(e);
e.treepath = e.path;
return me.load(e).then(function() {
return me.open(e);
}).catch(function(msg) {
e.loaded = true;
me.open(e);
return me.error(__("Error when loading '{0}': {1}", e.name, msg));
});
});
this.initEditor();
this.resizeContent();
@ -40,13 +47,14 @@
if (!me.currentToc) {
return;
}
return me.currentToc.descFile.dirty = true;
me.currentToc.descFile.dirty = true;
return me.dirty = true;
});
}
newChapter() {
var ch;
if (!(this.currentToc && this.currentToc.type === "book")) {
if (!(this.currentToc && this.currentToc.type === "Book")) {
return this.error(__("No book selected"));
}
ch = new BookletChapter(this.book);
@ -56,7 +64,7 @@
newSection() {
var sec;
if (!(this.currentToc && this.currentToc.type === "chapter")) {
if (!(this.currentToc && this.currentToc.type === "Chapter")) {
return this.error(__("No chapter selected"));
}
sec = new BookletSection(this.currentToc);
@ -66,7 +74,7 @@
newFile() {
var file;
if (!(this.currentToc && this.currentToc.type === "section")) {
if (!(this.currentToc && this.currentToc.type === "Section")) {
return this.error(__("No section selected"));
}
file = new BookletFile(this.currentToc);
@ -94,12 +102,33 @@
});
}
load(entry) {
var me;
me = this;
return new Promise(function(r, e) {
if (entry.loaded) {
return r();
}
return entry.descFile.meta(function(d) {
if (d.error) {
return e(d.error);
}
return entry.descFile.read(function(data) {
entry.descFile.cache = data;
entry.loaded = true;
entry.descFile.dirty = false;
return r();
});
});
});
}
contextMenu() {
if (!this.currentToc) {
return void 0;
}
switch (this.currentToc.type) {
case "book":
case "Book":
return [
{
text: __("New chapter"),
@ -110,7 +139,7 @@
dataid: "delete"
}
];
case "chapter":
case "Chapter":
return [
{
text: __("New section"),
@ -121,7 +150,7 @@
dataid: "delete"
}
];
case "section":
case "Section":
return [
{
text: __("New file"),
@ -132,7 +161,7 @@
dataid: "delete"
}
];
case "file":
case "File":
return [
{
text: __("Delete file"),
@ -267,10 +296,20 @@
me = this;
switch (e) {
case `${this.name}-Open`:
return this.openDialog("FileDiaLog", function(d, f) {
return console.log(`${d}/${f}`.asFileHandler());
}, __("Open file"), {
mimes: me.meta().mimes
return this.checkForDirty(function() {
return me.openDialog("FileDiaLog", function(d, f) {
me.book = new BookletBook(d);
return me.book.read(d).then(function() {
me.book.treepath = me.book.path;
me.tree.set("selectedItem", void 0);
me.displayToc();
return me.notify(__("Book loaded"));
}).catch(function(msg) {
return me.error(__("Cannot load book: {0}", msg));
});
}, __("Open file"), {
mimes: ['dir']
});
});
case `${this.name}-New`:
return this.openDialog("FileDiaLog", function(d, f) {
@ -290,6 +329,7 @@
}
me.displayToc();
return me.book.save().then(function() {
me.dirty = false;
return me.notify(__("Book saved"));
}).catch(function(e) {
return me.error(__("Can't save the book : {0}", e));
@ -297,18 +337,36 @@
}
}
open(toc) {
this.emux = true;
this.saveContext();
this.currentToc = toc;
this.reloadEditor();
this.displayToc();
return this.emux = false;
checkForDirty(f) {
if (!this.dirty) {
return f();
}
return this._gui.openDialog("YesNoDialog", function(d) {
console.log(d);
if (d) {
return f();
}
}, __("Continue ?"), {
text: __("Book is unsaved, you want to continue ?")
});
}
open(toc) {
var me;
me = this;
me.emux = true;
me.saveContext();
me.currentToc = toc;
me.reloadEditor();
me.displayToc();
return me.emux = false;
}
openBook(metaFile) {}
newAt(folder) {
this.tree.set("selectedItem", false);
this.book = new Book(folder);
this.book = new BookletBook(folder);
this.book.treepath = this.book.path;
this.currentToc = void 0;
this.reloadEditor();
@ -327,8 +385,9 @@
this.OS.register("Booklet", Booklet);
BookletEntry = class BookletEntry {
constructor() {
this.name = "Untitled";
constructor(name) {
this.name = name;
this.loaded = true;
}
save() {}
@ -346,6 +405,12 @@
if (!(t && t.length === 2)) {
return this.name;
}
if (this.hasMeta && this.metaFile) {
this.metaFile.dirty = true;
}
if (this.parent && this.parent.hasMeta && this.parent.metaFile) {
this.parent.metaFile.dirty = true;
}
return this.name = t[1].trim();
}
@ -388,16 +453,20 @@
BookletFolder = class BookletFolder extends BookletEntry {
constructor(type, path1, hasMeta) {
super();
super("Untitle");
this.type = type;
this.path = path1;
this.hasMeta = hasMeta;
this.init();
}
init() {
this.cnt = 0;
this.nodes = [];
if (this.hasMeta) {
this.metaFile = `${this.path}/meta.json`.asFileHandler();
}
this.descFile = `${this.path}/INTRO.md`.asFileHandler();
return this.descFile = `${this.path}/INTRO.md`.asFileHandler();
}
add(chap) {
@ -431,6 +500,49 @@
});
}
read(folderPath) {
var me;
me = this;
return new Promise(function(r, e) {
me.path = folderPath;
me.init();
me.loaded = false;
return me.metaFile.meta(function(d) {
if (d.error) {
return e(d.error);
}
return me.metaFile.read(function(data) {
var fn, i, j, len, list, ref, v;
// load all child
me.name = data.name;
list = [];
ref = data.entries;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
list[i] = v;
}
fn = function(l) {
var el, obj;
if (l.length === 0) {
me.cnt = data.cnt;
return r();
}
el = (l.splice(0, 1))[0];
console.log("create", el.type);
obj = new NS[el.type](me);
obj.name = el.name;
return obj.read(el.path).then(function() {
return fn(l);
}).catch(function(msg) {
return fn(l);
});
};
return fn(list);
}, "json");
});
});
}
size() {
return this.nodes.length;
}
@ -464,7 +576,7 @@
return new Promise(function(r, e) {
var i, j, len, list, ref, v;
list = [];
if (me.type !== 'section') {
if (me.type !== 'Section') {
ref = me.nodes;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
@ -496,7 +608,7 @@
var me;
me = this;
return new Promise(function(r, e) {
var entries, i, j, len, ref, v;
var data, entries, i, j, len, ref, v;
if (!me.metaFile.dirty) {
return r();
}
@ -504,9 +616,19 @@
ref = me.nodes;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
entries[i] = v.path;
entries[i] = {
name: v.name,
path: v.path,
type: v.type
};
}
me.metaFile.cache = entries;
data = {
name: me.name,
entries: entries,
cnt: me.cnt,
meta: me.hasMeta
};
me.metaFile.cache = data;
return me.metaFile.write("object", function(d) {
if (d.error) {
return e(d.error);
@ -585,9 +707,9 @@
};
Book = class Book extends BookletFolder {
BookletBook = class BookletBook extends BookletFolder {
constructor(path) {
super('book', path, true);
super('Book', path, true);
}
save() {
@ -612,7 +734,7 @@
constructor(book) {
var path;
path = `${book.path}/c_${book.cnt}`;
super('chapter', path, true);
super('Chapter', path, true);
book.add(this);
}
@ -622,7 +744,7 @@
constructor(chapter) {
var path;
path = `${chapter.path}/s_${chapter.cnt}`;
super("section", path, true);
super("Section", path, true);
chapter.add(this);
}
@ -630,10 +752,10 @@
BookletFile = class BookletFile extends BookletEntry {
constructor(section) {
super();
super("Untitle file");
this.section = section;
this.hasMeta = false;
this.type = "file";
this.type = "File";
this.path = `${this.section.path}/f_${this.section.cnt}.md`;
this.descFile = this.path.asFileHandler();
this.section.add(this);
@ -657,6 +779,16 @@
});
}
read(p) {
var me;
me = this;
return new Promise(function(r, e) {
me.loaded = false;
me.treepath = p;
return r();
});
}
toc() {
this.updateName();
return this;
@ -664,4 +796,11 @@
};
NS = {
Book: BookletBook,
Chapter: BookletChapter,
Section: BookletSection,
File: BookletFile
};
}).call(this);

View File

@ -9,5 +9,5 @@
"version":"0.0.1-a",
"category":"Other",
"iconclass":"fa fa-adn",
"mimes":["application/json"]
"mimes":["dir"]
}

View File

@ -1,6 +1,7 @@
class BookletEntry
constructor: () ->
@name = "Untitled"
constructor: (@name) ->
@loaded = true
save: () ->
@ -13,6 +14,8 @@ class BookletEntry
return @name unless @descFile.dirty
t = (new RegExp "^\s*#+(.*)\n", "g").exec @descFile.cache
return @name 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()
remove: () ->
@ -35,7 +38,10 @@ class BookletEntry
class BookletFolder extends BookletEntry
constructor: (@type, @path, @hasMeta) ->
super()
super "Untitle"
@init()
init: () ->
@cnt = 0
@nodes = []
@metaFile = "#{@path}/meta.json".asFileHandler() if @hasMeta
@ -60,7 +66,34 @@ class BookletFolder extends BookletEntry
r()
.catch (msg) ->
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) ->
# load all child
me.name = data.name
list = []
list[i] = v for v,i in data.entries
fn = (l) ->
if l.length is 0
me.cnt = data.cnt
return r()
el = (l.splice 0, 1)[0]
#console.log "create", el.type
obj = new NS[el.type]( me )
obj.name = el.name
obj.read(el.path).then () ->
fn l
.catch (msg) ->
fn l
return fn list
, "json"
size: () ->
return @nodes.length
@ -81,7 +114,7 @@ class BookletFolder extends BookletEntry
me = @
return new Promise (r, e) ->
list = []
list[i] = v for v, i in me.nodes if me.type isnt 'section'
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
@ -98,12 +131,18 @@ class BookletFolder extends BookletEntry
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
entries[i] = {name: v.name, path:v.path, type:v.type} for v,i in me.nodes
data = {
name: me.name,
entries: entries,
cnt: me.cnt,
meta: me.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
#console.log "saved " + me.metaFile.path
r()
@ -115,7 +154,7 @@ class BookletFolder extends BookletEntry
me.descFile.write "text/plain", (d) ->
return e d.error if d.error
me.descFile.dirty = false
console.log "saved " + me.descFile.path
#console.log "saved " + me.descFile.path
r()
.catch (msg) -> e msg
@ -141,9 +180,9 @@ class BookletFolder extends BookletEntry
@
class Book extends BookletFolder
class BookletBook extends BookletFolder
constructor: (path) ->
super 'book', path, true
super 'Book', path, true
save:() ->
me = @
@ -158,22 +197,22 @@ class Book extends BookletFolder
class BookletChapter extends BookletFolder
constructor: (book) ->
path = "#{book.path}/c_#{book.cnt}"
super 'chapter', path, true
super 'Chapter', path, true
book.add @
class BookletSection extends BookletFolder
constructor: (chapter) ->
path = "#{chapter.path}/s_#{chapter.cnt}"
super "section", path, true
super "Section", path, true
chapter.add @
class BookletFile extends BookletEntry
constructor: (@section) ->
super()
super "Untitle file"
@hasMeta = false
@type = "file"
@type = "File"
@path = "#{@section.path}/f_#{@section.cnt}.md"
@descFile = @path.asFileHandler()
@section.add @
@ -185,9 +224,22 @@ class BookletFile extends BookletEntry
me.descFile.write "text/plain", (d) ->
return e d.error if d.error
me.descFile.dirty = false
console.log "saved" + me.descFile.path
#console.log "saved" + me.descFile.path
r()
read: (p) ->
me = @
return new Promise (r, e) ->
me.loaded = false
me.treepath = p
r()
toc: () ->
@updateName()
@
@
NS =
Book: BookletBook
Chapter: BookletChapter
Section: BookletSection
File: BookletFile

View File

@ -6,10 +6,17 @@ class Booklet extends this.OS.GUI.BaseApplication
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)
me.open e
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)
@initEditor()
@resizeContent()
@ -24,21 +31,22 @@ class Booklet extends this.OS.GUI.BaseApplication
return if me.emux
return unless me.currentToc
me.currentToc.descFile.dirty = true
me.dirty = true
newChapter: () ->
return @error __("No book selected") unless @currentToc and @currentToc.type is "book"
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"
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"
return @error __("No section selected") unless @currentToc and @currentToc.type is "Section"
file = new BookletFile(@currentToc)
@displayToc()
file.treepath = file.path
@ -57,25 +65,37 @@ class Booklet extends this.OS.GUI.BaseApplication
me.error e
fn()
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()
contextMenu: () ->
return undefined unless @currentToc
switch @currentToc.type
when "book"
when "Book"
return [
{ text: __("New chapter"), dataid: "newChapter" },
{ text: __("Delete book"), dataid: "delete" }
]
when "chapter"
when "Chapter"
return [
{ text: __("New section"), dataid: "newSection" },
{ text: __("Delete chapter"), dataid: "delete" }
]
when "section"
when "Section"
return [
{ text: __("New file"), dataid: "newFile" },
{ text: __("Delete section"), dataid: "delete" }
]
when "file"
when "File"
return [
{ text: __("Delete file"), dataid: "delete" }
]
@ -157,9 +177,17 @@ class Booklet extends this.OS.GUI.BaseApplication
me = @
switch e
when "#{@name}-Open"
@openDialog "FileDiaLog", ( d, f ) ->
console.log "#{d}/#{f}".asFileHandler()
, __("Open file"), { mimes: me.meta().mimes }
@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'] }
when "#{@name}-New"
@openDialog "FileDiaLog", ( d, f ) ->
me.newAt "#{d}/#{f}"
@ -169,24 +197,34 @@ class Booklet extends this.OS.GUI.BaseApplication
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)
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 ?") }
open: (toc) ->
@emux = true
@saveContext()
@currentToc = toc
@reloadEditor()
@displayToc()
@emux = false
me = @
me.emux = true
me.saveContext()
me.currentToc = toc
me.reloadEditor()
me.displayToc()
me.emux = false
openBook: (metaFile) ->
newAt: (folder) ->
@tree.set "selectedItem", false
@book = new Book(folder)
@book = new BookletBook(folder)
@book.treepath = @book.path
@currentToc = undefined
@reloadEditor()

View File

@ -9,5 +9,5 @@
"version":"0.0.1-a",
"category":"Other",
"iconclass":"fa fa-adn",
"mimes":["application/json"]
"mimes":["dir"]
}

View File

@ -10,4 +10,4 @@
"category":"Other",
"iconclass":"fa fa-adn",
"mimes":["none"]
}
}