view src/save_edit.js.luan @ 51:78b2d6995244

add preview
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Nov 2022 22:57:43 -0700
parents 96f0c3d65698
children 9f8ebc757814
line wrap: on
line source

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:/bbcode/Bbcode.luan"
local bbcode_to_html = Bbcode.to_html or error()
local Db = require "site:/lib/Db.luan"
local Shared = require "site:/lib/Shared.luan"
local base_url = Shared.base_url or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "save_edit.js"


return function()
	local post = Http.request.parameters.post or error()
	local text = Http.request.parameters.text or error()
	Db.run_in_transaction( function()
		post = Post.get_by_id(post) or error()
		post.author_is_current() or error()
		post.content = text
		post.save()
	end )
	local html = output_of(function() bbcode_to_html(post.content) end)
	local js = output_of(function()
%>
updated( '<%=post.id%>', <%= json_string(html) %> );
<%
	end)
	local url = base_url().."/thread.html?root="..post.root_id
	Http.push(url,js)
end