Used ui.fileList in wiki.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3194 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2016-04-09 18:24:16 +00:00
parent 41a12fd1a7
commit 4b2877dbad
2 changed files with 68 additions and 80 deletions

View File

@ -1,39 +1,69 @@
"use strict"; "use strict";
function fileList(title, prefix, editCallback) { function fileList(settings) {
terminal.setEcho(false); terminal.setEcho(false);
terminal.clear(); terminal.clear();
terminal.print(title); terminal.print(settings.title);
if (core.user.credentials.permissions.authenticated) { if (core.user.credentials.permissions.authenticated) {
terminal.print({command: "new"}); terminal.print({command: "new"});
} }
let prefix = settings.prefix || "";
let makeSaveCallback = function(oldName, oldValue) { let makeSaveCallback = function(oldName, oldValue) {
return function(newName, newValue) { return function(newName, newValue) {
print(newName, " ", newValue);
return database.set(prefix + newName, newValue); return database.set(prefix + newName, newValue);
} }
} }
let backCallback = function() { 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) { return database.getAll().then(function(entries) {
terminal.readLine().then(function(input) { terminal.readLine().then(function(input) {
if (input == "new") { 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:") { } else if (input.substring(0, "open:".length) == "open:") {
var title = input.substring("open:".length + prefix.length); let name = input.substring("open:".length + prefix.length);
database.get(prefix + title).then(function(contents) { terminal.setHash(name);
editCallback(title, contents, makeSaveCallback(title, contents), backCallback); database.get(prefix + name).then(function(contents) {
settings.edit({
name: name,
value: contents,
save: makeSaveCallback(name, contents),
back: backCallback
});
}); });
} else if (input == "home") { } else if (input == "home") {
filelist(title, prefix, editCallback); filelist(settings);
} else if (input.substring(0, "delete:".length) == "delete:") { } else if (input.substring(0, "delete:".length) == "delete:") {
terminal.clear(); terminal.clear();
var title = input.substring(7); var name = input.substring(7);
terminal.print("Are you sure you want to delete '", title.substring(prefix.length), "'?"); terminal.print("Are you sure you want to delete '", name.substring(prefix.length), "'?");
terminal.print({command: "confirmDelete:" + title, value: "delete it"}); terminal.print({command: "confirmDelete:" + name, value: "delete it"});
terminal.print({command: "home", value: "cancel"}); terminal.print({command: "home", value: "cancel"});
terminal.readLine().then(function(input) { terminal.readLine().then(function(input) {
if (input == "home") { if (input == "home") {
@ -66,26 +96,30 @@ function fileList(title, prefix, editCallback) {
}); });
} }
function testEdit(name, value, save, back) { function testEdit(event) {
terminal.clear(); terminal.clear();
terminal.print("testEdit ", name, " ", value); terminal.print("testEdit ", event.name, " ", event.value);
terminal.print({command: "++"}); terminal.print({command: "++"});
terminal.print({command: "--"}); terminal.print({command: "--"});
terminal.print({command: "back"}); terminal.print({command: "back"});
terminal.readLine().then(function(command) { terminal.readLine().then(function(command) {
if (command == "back") { if (command == "back") {
terminal.print("calling back"); terminal.print("calling back");
back(); event.back();
} else if (command == "++") { } 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 == "--") { } 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) { if (imports.terminal) {
fileList("Test File List", "fileList_", testEdit); fileList({
title: "Test File List",
prefix: "fileList_",
edit: testEdit,
});
} }
exports.fileList = fileList; exports.fileList = fileList;

View File

@ -1,81 +1,31 @@
"use strict"; "use strict";
//! {"require": ["ui"]}
terminal.setEcho(false); terminal.setEcho(false);
terminal.setTitle("Live Markdeep Editor"); terminal.setTitle("Live Markdeep Editor");
core.register("onInput", function(input) { let gEditEvent = null;
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);
}
});
function renderIndex() { function back() {
terminal.split([{name: "terminal"}]); terminal.split([{name: "terminal"}]);
terminal.clear(); if (gEditEvent) {
terminal.print("Live Markdeep Editor"); gEditEvent.back();
if (core.user.credentials.permissions.authenticated) {
terminal.print({command: "new page"});
}
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) { core.register("onWindowMessage", function(event) {
if (event.message.ready) { 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) { } else if (event.message.index) {
renderIndex(); back();
} else { } else {
database.set(event.message.title, event.message.contents).then(function() { gEditEvent.save(event.message.title, event.message.contents).then(back);
renderIndex();
});
} }
}); });
function editPage(title, contents) { function editPage(event) {
gPage = {title: title, contents: contents}; gEditEvent = event;
terminal.split([{name: "terminal", type: "vertical"}]); terminal.split([{name: "terminal", type: "vertical"}]);
terminal.clear(); terminal.clear();
terminal.print({iframe: `<html> terminal.print({iframe: `<html>
@ -173,4 +123,8 @@ function editPage(title, contents) {
</html>`, name: "iframe", style: "flex: 1 1 auto; border: 0; width: 100%"}); </html>`, name: "iframe", style: "flex: 1 1 auto; border: 0; width: 100%"});
} }
renderIndex(); // XXX: Why do I need .js?
require("ui.js").fileList({
title: "Live Markdeep Editor",
edit: editPage,
});