comparison src/edit.js.luan @ 19:da006d1c1eba

use contentEditable
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 13 Jul 2022 08:47:13 -0600
parents 94e26bffd4fb
children 66fd3784e60e
comparison
equal deleted inserted replaced
18:94e26bffd4fb 19:da006d1c1eba
3 local Html = require "luan:Html.luan" 3 local Html = require "luan:Html.luan"
4 local html_encode = Html.encode or error() 4 local html_encode = Html.encode or error()
5 local Parsers = require "luan:Parsers.luan" 5 local Parsers = require "luan:Parsers.luan"
6 local json_string = Parsers.json_string or error() 6 local json_string = Parsers.json_string or error()
7 local Io = require "luan:Io.luan" 7 local Io = require "luan:Io.luan"
8 local output_of = Io.output_of or error()
9 local Http = require "luan:http/Http.luan" 8 local Http = require "luan:http/Http.luan"
10 local Post = require "site:/lib/Post.luan" 9 local Post = require "site:/lib/Post.luan"
11 10
12 11
13 local function form(post)
14 %>
15 <p><textarea name=content><%=html_encode(post.content)%></textarea></p>
16 <p>
17 <a href="javascript:saveEdit('<%= post.id %>')">save</a>
18 <a href="javascript:cancelEdit('<%= post.id %>')">cancel</a>
19 </p>
20 <%
21 end
22
23 return function() 12 return function()
24 local post = Http.request.parameters.post or error() 13 local post = Http.request.parameters.post or error()
25 post = Post.get_by_id(post) or error() 14 post = Post.get_by_id(post) or error()
26 local html = output_of(function() form(post) end)
27 Io.stdout = Http.response.text_writer() 15 Io.stdout = Http.response.text_writer()
28 %> 16 %>
29 let postDiv = document.querySelector('[post="<%=post.id%>"]'); 17 let postDiv = document.querySelector('[post="<%=post.id%>"]');
30 postDiv.querySelector('[output]').style.display = 'none'; 18 postDiv.querySelector('[output]').style.display = 'none';
31 postDiv.querySelector('[edit]').innerHTML = <%= json_string(html) %>; 19 postDiv.querySelector('[edit]').innerHTML = document.querySelector('[hidden][edit]').innerHTML;
32 postDiv.querySelector('textarea').focus(); 20 let contentEditable = postDiv.querySelector('[contentEditable]');
21 contentEditable.innerHTML = <%= json_string(html_encode(post.content)) %>;
22 contentEditable.focus();
33 <% 23 <%
34 end 24 end