changeset 15:0edde02b908c

edit in place
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Jul 2022 23:27:54 -0600
parents 7d0c96408abf
children bff178656073
files src/edit.js.luan src/lib/Bbcode.luan src/lib/Db.luan src/save_edit.js.luan src/thread.html.luan
diffstat 5 files changed, 100 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edit.js.luan	Tue Jul 05 23:27:54 2022 -0600
@@ -0,0 +1,33 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local output_of = Io.output_of or error()
+local Http = require "luan:http/Http.luan"
+local Post = require "site:/lib/Post.luan"
+
+
+local function form(post)
+%>
+					<p><textarea name=content><%=html_encode(post.content)%></textarea></p>
+					<p>
+						<a href="javascript:saveEdit('<%= post.id %>')">save</a>
+						<a href="javascript:cancelEdit('<%= post.id %>')">cancel</a>
+					</p>
+<%
+end
+
+return function()
+	local post = Http.request.parameters.post or error()
+	post = Post.get_by_id(post) or error()
+	local html = output_of(function() form(post) end)
+	Io.stdout = Http.response.text_writer()
+%>
+let postDiv = document.querySelector('[post="<%=post.id%>"]');
+postDiv.querySelector('[output]').style.display = 'none';
+postDiv.querySelector('[edit]').innerHTML = <%= json_string(html) %>;
+<%
+end
--- a/src/lib/Bbcode.luan	Mon Jul 04 17:04:14 2022 -0600
+++ b/src/lib/Bbcode.luan	Tue Jul 05 23:27:54 2022 -0600
@@ -114,7 +114,7 @@
 
 function Bbcode.to_html(bbcode)
 	bbcode = bbcode_parse(bbcode)
-	%><div post><% to_html(bbcode) %></div><%
+	%><div message><% to_html(bbcode) %></div><%
 end
 
 
--- a/src/lib/Db.luan	Mon Jul 04 17:04:14 2022 -0600
+++ b/src/lib/Db.luan	Tue Jul 05 23:27:54 2022 -0600
@@ -1,8 +1,11 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local new_error = Luan.new_error or error()
 local Lucene = require "luan:lucene/Lucene.luan"
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Db"
 
 
 local dir = uri("site:/private/local/lucene")
@@ -16,4 +19,8 @@
 Db.indexed_fields.post_is_root = Lucene.type.string
 Db.indexed_fields.post_root_id = Lucene.type.long
 
+function Db.not_in_transaction()
+	logger.error(new_error("not in transaction"))
+end
+
 return Db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/save_edit.js.luan	Tue Jul 05 23:27:54 2022 -0600
@@ -0,0 +1,34 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local output_of = Io.output_of or error()
+local Http = require "luan:http/Http.luan"
+local Post = require "site:/lib/Post.luan"
+local Bbcode = require "site:/lib/Bbcode.luan"
+local bbcode_preprocess = Bbcode.preprocess or error()
+local bbcode_to_html = Bbcode.to_html or error()
+local Db = require "site:/lib/Db.luan"
+local User = require "site:/lib/User.luan"
+
+
+return function()
+	local post = Http.request.parameters.post or error()
+	local text = Http.request.parameters.text or error()
+	text = bbcode_preprocess(text)
+	Db.run_in_transaction( function()
+		post = Post.get_by_id(post) or error()
+		local user = User.current() or error()
+		user.name = post.author_name or error()
+		post.content = text
+		post.save()
+	end )
+	local html = output_of(function() bbcode_to_html(post.content) end)
+	Io.stdout = Http.response.text_writer()
+%>
+cancelEdit('<%=post.id%>');
+let postDiv = document.querySelector('[post="<%=post.id%>"]');
+postDiv.querySelector('[message]').innerHTML = <%= json_string(html) %>;
+<%
+end
--- a/src/thread.html.luan	Mon Jul 04 17:04:14 2022 -0600
+++ b/src/thread.html.luan	Tue Jul 05 23:27:54 2022 -0600
@@ -28,10 +28,24 @@
 <%		head() %>
 		<title><%=forum_title%>: <%=subject_html%></title>
 		<style>
-			[post] {
+			[message] {
 				white-space: pre-wrap;
 			}
 		</style>
+		<script>
+			function cancelEdit(post) {
+				let postDiv = document.querySelector('[post="'+post+'"]');
+				postDiv.querySelector('[output]').style.display = 'block';
+				postDiv.querySelector('[edit]').innerHTML = '';
+			}
+
+			function saveEdit(post) {
+				let postDiv = document.querySelector('[post="'+post+'"]');
+				let text = postDiv.querySelector('textarea').value;
+				let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
+				ajax("save_edit.js",postData);
+			}
+		</script>
 	</head>
 	<body>
 <%		header() %>
@@ -39,11 +53,16 @@
 			<h1><%=subject_html%></h1>
 <%	for _, post in ipairs(posts) do %>
 			<hr>
-			<% bbcode_to_html(post.content) %>
-			<p>
-				<a href="/reply.html?parent=<%=post.id%>">reply</a>
-				- <a href="/edit.html?post=<%=post.id%>">edit</a>
-			</p>
+			<div post="<%=post.id%>">
+				<div output>
+					<% bbcode_to_html(post.content) %>
+					<p>
+						<a href="/reply.html?parent=<%=post.id%>">reply</a>
+						- <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
+					</p>
+				</div>
+				<div edit></div>
+			</div>
 <%	end %>
 		</div>
 <%		footer() %>