Mercurial Hosting > freedit
changeset 52:9f8ebc757814
add convert urls
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 23 Nov 2022 23:29:16 -0700 |
parents | 78b2d6995244 |
children | cac477dd1f82 |
files | src/bbcode/bbcode.css src/bbcode/bbcode.js src/bbcode/icons/media_link.svg src/bbcode/preview.js.luan src/new_post.js.luan src/new_thread.html.luan src/save_edit.js.luan src/thread.html.luan |
diffstat | 8 files changed, 75 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/src/bbcode/bbcode.css Tue Nov 22 22:57:43 2022 -0700 +++ b/src/bbcode/bbcode.css Wed Nov 23 23:29:16 2022 -0700 @@ -19,6 +19,7 @@ div[bbcode_editor] div[buttons] { display: flex; justify-content: flex-end; + gap: 4px; } div[bbcode_editor] button { border: 0;
--- a/src/bbcode/bbcode.js Tue Nov 22 22:57:43 2022 -0700 +++ b/src/bbcode/bbcode.js Wed Nov 23 23:29:16 2022 -0700 @@ -70,6 +70,8 @@ <button type=button ol title="Numbered list"><img src="/bbcode/icons/format_list_numbered.svg"></button> <button type=button code_block title="Code block"><img src="/bbcode/icons/code_blocks.svg"></button> <button type=button code_inline title="Inline code"><img src="/bbcode/icons/code.svg"></button> + <button type=button convert_urls checked title="Convert URLs"><img src="/bbcode/icons/media_link.svg"></button> + <input type=hidden name=convert_urls value=true> <button type=button more checked title="More..."><img src="/bbcode/icons/more_horiz.svg"></button> <input type=file> <button type=button upload title="Upload File"><img src="/bbcode/icons/file_upload.svg"></button> @@ -92,10 +94,14 @@ div.innerHTML = html; let enabledInPreview = []; + let convertUrls = true; if(save) { let button = div.querySelector('button[save]'); - button.onclick = save; + button.onclick = function(event) { + event.convertUrls = convertUrls; + save(event); + }; enabledInPreview.push(button); } if(cancel) { @@ -114,25 +120,33 @@ textarea.oninput = fixTextarea; fixTextarea(); + function toggle(button) { + let checked = button.getAttribute('checked') !== null; + checked = !checked; // toggle + if( checked ) { + button.setAttribute('checked',''); + } else { + button.removeAttribute('checked'); + } + return checked; + } + let moreButton = div.querySelector('button[more]'); function more() { - let button = moreButton; - let checked = button.getAttribute('checked') !== null; - let buttons = button.parentNode.querySelectorAll('button'); + let checked = toggle(moreButton); + let buttons = moreButton.parentNode.querySelectorAll('button'); if( checked ) { - button.removeAttribute('checked'); for( let b of buttons ) { - if( b === button ) + if( b === moreButton ) + break; + b.removeAttribute('hidden'); + } + } else { + for( let b of buttons ) { + if( b === moreButton ) break; b.setAttribute('hidden',''); } - } else { - button.setAttribute('checked',''); - for( let b of buttons ) { - if( b === button ) - break; - b.removeAttribute('hidden'); - } } }; moreButton.onclick = more; @@ -140,38 +154,43 @@ enabledInPreview.push(moreButton); let divPreview = div.querySelector('div[preview]'); - function contextPreview(html) { + function contextPreview(bbcode,html) { + textarea.value = bbcode; divPreview.innerHTML = html; } let previewButton = div.querySelector('button[preview]'); function preview() { - let button = previewButton; - let checked = button.getAttribute('checked') !== null; - let buttons = button.parentNode.querySelectorAll('button'); + let checked = toggle(previewButton); + let buttons = previewButton.parentNode.querySelectorAll('button'); if( checked ) { - button.removeAttribute('checked'); + for( let b of buttons ) { + if( !enabledInPreview.includes(b) ) + b.disabled = true; + } + textarea.style.display = 'none'; + divPreview.style.display = 'block'; + let url = '/bbcode/preview.js?text=' + encodeURIComponent(textarea.value) + '&convert_urls=' + convertUrls; + ajax( url, null, {preview: contextPreview} ); + } else { for( let b of buttons ) { b.disabled = false; } textarea.style.display = 'initial'; divPreview.style.display = 'none'; textarea.focus(); - } else { - window.enabledInPreview = enabledInPreview; - button.setAttribute('checked',''); - for( let b of buttons ) { - if( !enabledInPreview.includes(b) ) - b.disabled = true; - } - textarea.style.display = 'none'; - divPreview.style.display = 'block'; - let url = '/bbcode/preview.js?text=' + encodeURIComponent(textarea.value); - ajax( url, null, {preview: contextPreview} ); } } previewButton.onclick = preview; enabledInPreview.push(previewButton); + let convertUrlsButton = div.querySelector('button[convert_urls]'); + let convertUrlsInput = div.querySelector('input[name="convert_urls"]'); + function toggleConvertUrls() { + convertUrls = toggle(convertUrlsButton); + convertUrlsInput.value = convertUrls; + } + convertUrlsButton.onclick = toggleConvertUrls; + function add(tag,openTag,closeTag) { let button = div.querySelector('button['+tag+']'); button.onclick = function() {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bbcode/icons/media_link.svg Wed Nov 23 23:29:16 2022 -0700 @@ -0,0 +1,1 @@ +<svg xmlns="http://www.w3.org/2000/svg" height="48" width="48"><path d="M15.7 37.85v-8.2l6.6 4.1ZM26 18.75q-2.3 0-3.9-1.6t-1.6-3.9q0-2.3 1.6-3.9t3.9-1.6h2v2h-2q-1.45 0-2.475 1.025Q22.5 11.8 22.5 13.25q0 1.45 1.025 2.475Q24.55 16.75 26 16.75h2v2Zm6 0v-2h2q1.45 0 2.475-1.025Q37.5 14.7 37.5 13.25q0-1.45-1.025-2.475Q35.45 9.75 34 9.75h-2v-2h2q2.3 0 3.9 1.6t1.6 3.9q0 2.3-1.6 3.9t-3.9 1.6Zm-5.5-4.5v-2h7v2Zm6.15 13.25v-4H43V5H17v18.5h-3V5q0-1.25.875-2.125T17 2h26q1.25 0 2.125.875T46 5v19.5q0 1.25-.875 2.125T43 27.5ZM5 46q-1.25 0-2.125-.875T2 43V24.5q0-1.25.875-2.125T5 21.5h26q1.25 0 2.125.875T34 24.5V43q0 1.25-.875 2.125T31 46Zm0-3h26V24.5H5V43Zm25-28.75Zm-12 19.5Z"/></svg> \ No newline at end of file
--- a/src/bbcode/preview.js.luan Tue Nov 22 22:57:43 2022 -0700 +++ b/src/bbcode/preview.js.luan Wed Nov 23 23:29:16 2022 -0700 @@ -6,14 +6,19 @@ local output_of = Io.output_of or error() local Http = require "luan:http/Http.luan" local Bbcode = require "site:/bbcode/Bbcode.luan" +local preprocess = Bbcode.preprocess or error() local bbcode_to_html = Bbcode.to_html or error() return function() local text = Http.request.parameters.text or error() + local convert_urls = Http.request.parameters.convert_urls or error() + if convert_urls == "true" then + text = preprocess(text) + end local html = output_of(function() bbcode_to_html(text) end) Io.stdout = Http.response.text_writer() %> - context.preview(<%= json_string(html) %>); + context.preview( <%= json_string(text) %>, <%= json_string(html) %> ); <% end
--- a/src/new_post.js.luan Tue Nov 22 22:57:43 2022 -0700 +++ b/src/new_post.js.luan Wed Nov 23 23:29:16 2022 -0700 @@ -15,6 +15,8 @@ local Shared = require "site:/lib/Shared.luan" local base_url = Shared.base_url or error() local show_post = Shared.show_post or error() +local Bbcode = require "site:/bbcode/Bbcode.luan" +local preprocess = Bbcode.preprocess or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "new_post.js" @@ -23,6 +25,10 @@ local user = User.current() or error() local root_id = Http.request.parameters.root or error() local text = Http.request.parameters.text or error() + local convert_urls = Http.request.parameters.convert_urls or error() + if convert_urls == "true" then + text = preprocess(text) + end local post = Post.new{ root_id = to_number(root_id) or error() content = text @@ -35,7 +41,6 @@ local js = output_of(function() %> added( '<%=post.id%>', <%= json_string(html) %>, <%=Post.thread_size(root_id)%> ); -console.log(<%=post.id%>); <% end) local url = base_url().."/thread.html?root="..root_id
--- a/src/new_thread.html.luan Tue Nov 22 22:57:43 2022 -0700 +++ b/src/new_thread.html.luan Wed Nov 23 23:29:16 2022 -0700 @@ -10,6 +10,8 @@ local forum_title = Forum.title or error() local Post = require "site:/lib/Post.luan" local User = require "site:/lib/User.luan" +local Bbcode = require "site:/bbcode/Bbcode.luan" +local preprocess = Bbcode.preprocess or error() return function() @@ -18,6 +20,10 @@ if Http.request.method == "POST" then local subject = Http.request.parameters.subject or error() local content = Http.request.parameters.bbcode or error() + local convert_urls = Http.request.parameters.convert_urls or error() + if convert_urls == "true" then + content = preprocess(content) + end local post = Post.new_thread(user,subject,content) Http.response.send_redirect("/thread.html?root="..post.id) return
--- a/src/save_edit.js.luan Tue Nov 22 22:57:43 2022 -0700 +++ b/src/save_edit.js.luan Wed Nov 23 23:29:16 2022 -0700 @@ -7,6 +7,7 @@ local Http = require "luan:http/Http.luan" local Post = require "site:/lib/Post.luan" local Bbcode = require "site:/bbcode/Bbcode.luan" +local preprocess = Bbcode.preprocess or error() local bbcode_to_html = Bbcode.to_html or error() local Db = require "site:/lib/Db.luan" local Shared = require "site:/lib/Shared.luan" @@ -18,6 +19,10 @@ return function() local post = Http.request.parameters.post or error() local text = Http.request.parameters.text or error() + local convert_urls = Http.request.parameters.convert_urls or error() + if convert_urls == "true" then + text = preprocess(text) + end Db.run_in_transaction( function() post = Post.get_by_id(post) or error() post.author_is_current() or error()
--- a/src/thread.html.luan Tue Nov 22 22:57:43 2022 -0700 +++ b/src/thread.html.luan Wed Nov 23 23:29:16 2022 -0700 @@ -91,7 +91,7 @@ let post = postDiv.getAttribute('post'); let text = postDiv.querySelector('textarea').value; cancelEdit(event); - let postData = 'post=' + post + '&text=' + encodeURIComponent(text); + let postData = 'post=' + post + '&text=' + encodeURIComponent(text) + '&convert_urls=' + event.convertUrls; ajax('save_edit.js',postData); } function updated(postId,html) { @@ -105,7 +105,7 @@ let textarea = postDiv.querySelector('textarea') let text = textarea.value; textarea.value = ''; - let postData = 'root=<%=root_id%>&text=' + encodeURIComponent(text); + let postData = 'root=<%=root_id%>&text=' + encodeURIComponent(text) + '&convert_urls=' + event.convertUrls; ajax('new_post.js',postData); } function added(postId,html,thread_size) {