Mercurial Hosting > freedit
changeset 21:33731231093a
fix bbcode editing
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 17 Jul 2022 08:24:52 -0600 |
parents | 3ea49246d6a7 |
children | d87a8f9f43d7 |
files | src/lib/Bbcode.luan src/save_edit.js.luan src/site.js src/thread.html.luan |
diffstat | 4 files changed, 36 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/Bbcode.luan Wed Jul 13 22:00:00 2022 -0600 +++ b/src/lib/Bbcode.luan Sun Jul 17 08:24:52 2022 -0600 @@ -9,10 +9,13 @@ local bbcode_parse = Parsers.bbcode_parse or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() +local html_parse = Html.parse or error() local Table = require "luan:Table.luan" local is_list = Table.is_list or error() +local concat = Table.concat or error() local String = require "luan:String.luan" local gsub = String.gsub or error() +local matches = String.matches or error() local User = require "site:/lib/User.luan" local Shared = require "site:/lib/Shared.luan" local list_to_set = Shared.list_to_set or error() @@ -212,4 +215,33 @@ end) end +function Bbcode.remove_html(text) + local ends_with_newline = matches(text,[[\n$]]) + local t = {} + local html = html_parse(text) + local is_first = true + for _, v in ipairs(html) do + if type(v) == "string" then + t[#t+1] = v + else + local name = v.name + if name == "div" then + if not is_first then + t[#t+1] = "\n" + end + elseif name == "/div" or name == "br" then + -- ignore + else + error("unexpected tag: "..name) + end + end + is_first = false + end + if not ends_with_newline then + t[#t+1] = "\n" + end + return concat(t) +end + + return Bbcode
--- a/src/save_edit.js.luan Wed Jul 13 22:00:00 2022 -0600 +++ b/src/save_edit.js.luan Sun Jul 17 08:24:52 2022 -0600 @@ -7,7 +7,7 @@ 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 remove_html = Bbcode.remove_html or error() local bbcode_to_html = Bbcode.to_html or error() local Db = require "site:/lib/Db.luan" @@ -15,7 +15,7 @@ return function() local post = Http.request.parameters.post or error() local text = Http.request.parameters.text or error() - text = bbcode_preprocess(text) + text = remove_html(text) Db.run_in_transaction( function() post = Post.get_by_id(post) or error() post.author_is_current() or error()
--- a/src/site.js Wed Jul 13 22:00:00 2022 -0600 +++ b/src/site.js Sun Jul 17 08:24:52 2022 -0600 @@ -31,26 +31,3 @@ err += '\nstack = ' + error.stack; ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); }; - -function editorEnter() { - let s = getSelection(); - let r = s.getRangeAt(0); - r.deleteContents(); - let t = document.createTextNode('\n'); - r.insertNode(t); - let isFucked = getSelection().getRangeAt(0).collapsed; - //console.log(isFucked); - r.collapse(); - if(isFucked) { - s.removeAllRanges(); - s.addRange(r); - } -} - -function editorKey() { - //console.log(event); - if( event.code === 'Enter' ) { - editorEnter(); - return false; - } -}
--- a/src/thread.html.luan Wed Jul 13 22:00:00 2022 -0600 +++ b/src/thread.html.luan Sun Jul 17 08:24:52 2022 -0600 @@ -73,7 +73,7 @@ function saveEdit(a) { let postDiv = getPostDiv(a); let post = postDiv.getAttribute('post'); - let text = postDiv.querySelector('[contentEditable]').textContent; + let text = postDiv.querySelector('[contentEditable]').innerHTML; let postData = 'post=' + post + '&text=' + encodeURIComponent(text); ajax("save_edit.js",postData); } @@ -134,7 +134,7 @@ <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span> <span hidden undelete><%deletePost()%></span> <div hidden edit> - <div contentEditable onkeypress="return editorKey()"></div> + <div contentEditable></div> <p> <button onclick="saveEdit(this)">save</button> <button onclick="cancelEdit(this)">cancel</button>