diff --git a/packages/cory/ui/ui.js b/packages/cory/ui/ui.js index ef8cf19b..e7d35d1c 100644 --- a/packages/cory/ui/ui.js +++ b/packages/cory/ui/ui.js @@ -1,39 +1,69 @@ "use strict"; -function fileList(title, prefix, editCallback) { +function fileList(settings) { + terminal.setEcho(false); terminal.clear(); - terminal.print(title); + terminal.print(settings.title); if (core.user.credentials.permissions.authenticated) { terminal.print({command: "new"}); } + let prefix = settings.prefix || ""; + let makeSaveCallback = function(oldName, oldValue) { return function(newName, newValue) { - print(newName, " ", newValue); return database.set(prefix + newName, newValue); } } let backCallback = function() { - return fileList(title, prefix, editCallback); + terminal.setHash(""); + return fileList(settings); } + let hashChange = function(event) { + var name = event.hash.substring(1); + if (name.length) { + database.get(prefix + name).then(function(value) { + settings.edit({ + name: name, + value: value, + save: makeSaveCallback(name, value), + back: backCallback, + }); + }); + } + }; + core.register("hashChange", hashChange); + return database.getAll().then(function(entries) { terminal.readLine().then(function(input) { if (input == "new") { - editCallback("untitled", "", makeSaveCallback("untitled", ""), backCallback); + terminal.setHash(name); + settings.edit({ + name: "untitled", + value: "", + save: makeSaveCallback("untitled", ""), + back: backCallback + }); } else if (input.substring(0, "open:".length) == "open:") { - var title = input.substring("open:".length + prefix.length); - database.get(prefix + title).then(function(contents) { - editCallback(title, contents, makeSaveCallback(title, contents), backCallback); + let name = input.substring("open:".length + prefix.length); + terminal.setHash(name); + database.get(prefix + name).then(function(contents) { + settings.edit({ + name: name, + value: contents, + save: makeSaveCallback(name, contents), + back: backCallback + }); }); } else if (input == "home") { - filelist(title, prefix, editCallback); + filelist(settings); } else if (input.substring(0, "delete:".length) == "delete:") { terminal.clear(); - var title = input.substring(7); - terminal.print("Are you sure you want to delete '", title.substring(prefix.length), "'?"); - terminal.print({command: "confirmDelete:" + title, value: "delete it"}); + var name = input.substring(7); + terminal.print("Are you sure you want to delete '", name.substring(prefix.length), "'?"); + terminal.print({command: "confirmDelete:" + name, value: "delete it"}); terminal.print({command: "home", value: "cancel"}); terminal.readLine().then(function(input) { if (input == "home") { @@ -66,26 +96,30 @@ function fileList(title, prefix, editCallback) { }); } -function testEdit(name, value, save, back) { +function testEdit(event) { terminal.clear(); - terminal.print("testEdit ", name, " ", value); + terminal.print("testEdit ", event.name, " ", event.value); terminal.print({command: "++"}); terminal.print({command: "--"}); terminal.print({command: "back"}); terminal.readLine().then(function(command) { if (command == "back") { terminal.print("calling back"); - back(); + event.back(); } else if (command == "++") { - save(name, (parseInt(value || "0") + 1).toString()).then(back); + event.save(event.name, (parseInt(event.value || "0") + 1).toString()).then(event.back); } else if (command == "--") { - save(name, (parseInt(value || "0") - 1).toString()).then(back); + event.save(event.name, (parseInt(event.value || "0") - 1).toString()).then(event.back); } }); } if (imports.terminal) { - fileList("Test File List", "fileList_", testEdit); + fileList({ + title: "Test File List", + prefix: "fileList_", + edit: testEdit, + }); } exports.fileList = fileList; \ No newline at end of file diff --git a/packages/cory/wiki/wiki.js b/packages/cory/wiki/wiki.js index b8ed984e..e5ba0316 100644 --- a/packages/cory/wiki/wiki.js +++ b/packages/cory/wiki/wiki.js @@ -1,81 +1,31 @@ "use strict"; +//! {"require": ["ui"]} + terminal.setEcho(false); terminal.setTitle("Live Markdeep Editor"); -core.register("onInput", function(input) { - if (input == "new page") { - editPage("new", ""); - } else if (input == "submit") { - submitNewPost().then(renderBlog); - } else if (input == "home") { - renderIndex(); - } else if (input.substring(0, 5) == "open:") { - var title = input.substring(5); - database.get(title).then(function(contents) { - editPage(title, contents); - }); - } else if (input.substring(0, 7) == "delete:") { - terminal.clear(); - var title = input.substring(7); - terminal.print("Are you sure you want to delete page '", title, "'?"); - terminal.print({command: "confirmDelete:" + title, value: "delete it"}); - terminal.print({command: "home", value: "cancel"}); - } else if (input.substring(0, 14) == "confirmDelete:") { - var title = input.substring(14); - database.remove(title).then(renderIndex); - } -}); +let gEditEvent = null; -function renderIndex() { +function back() { terminal.split([{name: "terminal"}]); - terminal.clear(); - terminal.print("Live Markdeep Editor"); - if (core.user.credentials.permissions.authenticated) { - terminal.print({command: "new page"}); + if (gEditEvent) { + gEditEvent.back(); } - - database.getAll().then(function(entries) { - for (var i = 0; i < entries.length; i++) { - if (core.user.credentials.permissions.authenticated) { - terminal.print( - "* ", - {style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i]}}, - " (", - {command: "delete:" + entries[i], value: "x"}, - ")"); - } else { - terminal.print( - "* ", - {style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i]}}); - } - } - }); } -var gPage = null; - -core.register("hashChange", function(event) { - var title = event.hash.substring(1); - database.get(title).then(function(contents) { - editPage(title, contents); - }); -}); - core.register("onWindowMessage", function(event) { if (event.message.ready) { - terminal.postMessageToIframe("iframe", {title: gPage.title, contents: gPage.contents}); + terminal.postMessageToIframe("iframe", {title: gEditEvent.name, contents: gEditEvent.value}); } else if (event.message.index) { - renderIndex(); + back(); } else { - database.set(event.message.title, event.message.contents).then(function() { - renderIndex(); - }); + gEditEvent.save(event.message.title, event.message.contents).then(back); } }); -function editPage(title, contents) { - gPage = {title: title, contents: contents}; +function editPage(event) { + gEditEvent = event; terminal.split([{name: "terminal", type: "vertical"}]); terminal.clear(); terminal.print({iframe: ` @@ -173,4 +123,8 @@ function editPage(title, contents) { `, name: "iframe", style: "flex: 1 1 auto; border: 0; width: 100%"}); } -renderIndex(); \ No newline at end of file +// XXX: Why do I need .js? +require("ui.js").fileList({ + title: "Live Markdeep Editor", + edit: editPage, +}); \ No newline at end of file