forked from andyburke/autonomous.contact
feature: essays
fix: multiple rendering of things when sending
This commit is contained in:
parent
376b7fdc24
commit
b6f661c6ec
10 changed files with 537 additions and 60 deletions
|
|
@ -1,16 +1,275 @@
|
|||
<style>
|
||||
#essays-container {
|
||||
padding: 1rem;
|
||||
max-width: 60rem;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.essay-container {
|
||||
position: relative;
|
||||
display: grid;
|
||||
grid-template-rows: auto auto auto auto 1fr;
|
||||
grid-template-columns: 1fr;
|
||||
grid-template-areas:
|
||||
"preview"
|
||||
"title"
|
||||
"info"
|
||||
"content"
|
||||
"newessay"
|
||||
"replies";
|
||||
padding: 1rem;
|
||||
border: 1px solid var(--border-subtle);
|
||||
overflow-y: hidden;
|
||||
transition: all ease-in-out 0.33s;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.essay-container .media-preview-container {
|
||||
grid-area: preview;
|
||||
max-height: 600px;
|
||||
}
|
||||
|
||||
.essay-container .media-preview-container img {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
.essay-container .info-container {
|
||||
grid-area: info;
|
||||
display: flex;
|
||||
margin-left: 1.8rem;
|
||||
}
|
||||
|
||||
.essay-container .info-container .avatar-container {
|
||||
display: block;
|
||||
margin: 0.5rem;
|
||||
width: 3rem;
|
||||
height: 3rem;
|
||||
border-radius: 20%;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.essay-container .info-container .avatar-container img {
|
||||
min-width: 100%;
|
||||
min-height: 100%;
|
||||
}
|
||||
|
||||
.essay-container .info-container .username-container {
|
||||
display: block;
|
||||
margin: 0 0.5rem;
|
||||
font-weight: bold;
|
||||
align-content: center;
|
||||
}
|
||||
|
||||
.essay-container .info-container .datetime-container {
|
||||
display: block;
|
||||
margin: 0 0.5rem;
|
||||
align-content: center;
|
||||
}
|
||||
|
||||
.essay-container .info-container .datetime-container .long {
|
||||
font-size: x-small;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.essay-container .info-container .datetime-container .short {
|
||||
font-size: xx-small;
|
||||
visibility: hidden;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.essay-container .title-container {
|
||||
grid-area: title;
|
||||
padding-left: 1rem;
|
||||
margin: 1rem 0;
|
||||
font-size: xx-large;
|
||||
}
|
||||
|
||||
.essay-container .content-container {
|
||||
grid-area: content;
|
||||
padding-left: 0.25rem;
|
||||
margin: 1rem 0;
|
||||
font-size: large;
|
||||
}
|
||||
|
||||
.essay-container .new-essay-container {
|
||||
grid-area: newessay;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.essay-container .replies-container {
|
||||
grid-area: replies;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id="essays" class="tab">
|
||||
<input
|
||||
type="radio"
|
||||
name="top-level-tabs"
|
||||
id="essays-tab-input"
|
||||
id="essay-tab-input"
|
||||
class="tab-switch"
|
||||
data-view="essays"
|
||||
data-view="essay"
|
||||
/>
|
||||
<label for="essays-tab-input" class="tab-label mockup"
|
||||
<label for="essay-tab-input" class="tab-label"
|
||||
><div class="icon essay"></div>
|
||||
<div class="label">Essays</div></label
|
||||
>
|
||||
<div class="label">Essays</div>
|
||||
</label>
|
||||
<div class="tab-content">
|
||||
<!-- #include file="./README.md" -->
|
||||
<div id="essays-container" class="container">
|
||||
<!-- #include file="./README.md" -->
|
||||
<!-- #include file="./new_essay.html" -->
|
||||
|
||||
<div id="essays-list"></div>
|
||||
<script>
|
||||
const essays_list = document.getElementById("essays-list");
|
||||
|
||||
async function render_essay(essay, position = "beforeend") {
|
||||
const creator = await USERS.get(essay.creator_id);
|
||||
const existing_element =
|
||||
document.getElementById(essay.id) ??
|
||||
document.getElementById(essay.meta?.temp_id ?? "") ??
|
||||
document.querySelector(`[data-temp_id="${essay.meta?.temp_id ?? ""}"]`);
|
||||
const essay_datetime = datetime_to_local(essay.timestamps.created);
|
||||
|
||||
const html_content = `<div class="essay-container" data-creator_id="${creator.id}" data-essay_id="${essay.id}" data-temp_id="${essay.meta?.temp_id ?? ""}">
|
||||
<div class="media-preview-container">
|
||||
${essay.data?.media?.length ? essay.data.media.map((url) => `<img src="${url}" />`).join("\n") : ""}
|
||||
</div>
|
||||
|
||||
<div class="info-container">
|
||||
<div class="avatar-container">
|
||||
<img src="${creator.meta?.avatar ?? "/images/default_avatar.gif"}" alt="user avatar" />
|
||||
</div>
|
||||
<div class="username-container">
|
||||
<span class="username">${creator.username}</span>
|
||||
</div>
|
||||
<div class="datetime-container">
|
||||
<span class="long">${essay_datetime.long}</span>
|
||||
<span class="short">${essay_datetime.short}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="title-container">${essay.data.title}</div>
|
||||
<div class="content-container">${htmlify(md_to_html(essay.data.essay))}</div>
|
||||
</div>`;
|
||||
|
||||
if (existing_element) {
|
||||
const template = document.createElement("template");
|
||||
template.innerHTML = html_content;
|
||||
existing_element.replaceWith(template.content.firstChild);
|
||||
existing_element.classList.remove("sending");
|
||||
} else {
|
||||
const target_container =
|
||||
document.querySelector(
|
||||
`.essay-container[data-essay_id='${essay.parent_id}'] > .replies-container`,
|
||||
) ?? essays_list;
|
||||
target_container.insertAdjacentHTML(position, html_content);
|
||||
}
|
||||
}
|
||||
|
||||
async function append_essays(essays) {
|
||||
let last_essay_id = essays_list.dataset.last_essay_id ?? "";
|
||||
for await (const essay of essays) {
|
||||
// if the last essay is undefined, it becomes this event, otherwise, if this event's id is newer,
|
||||
// it becomes the latest essay
|
||||
last_essay_id =
|
||||
essay.id > last_essay_id && essay.id.indexOf("TEMP") !== 0
|
||||
? essay.id
|
||||
: last_essay_id;
|
||||
|
||||
// if the last essay has been updated, update the content's dataset to reflect that
|
||||
if (last_essay_id !== essays_list.dataset.last_essay_id) {
|
||||
essays_list.dataset.last_essay_id = last_essay_id;
|
||||
}
|
||||
|
||||
await render_essay(essay);
|
||||
}
|
||||
|
||||
const new_essay_forms = document.querySelectorAll(".essay-creation-form");
|
||||
for (const new_essay_form of new_essay_forms) {
|
||||
new_essay_form.action =
|
||||
"/api/topics/" + document.body.dataset?.topic + "/events";
|
||||
}
|
||||
|
||||
essays_list.scrollTop = 0;
|
||||
}
|
||||
|
||||
// TODO: we need some abortcontroller handling here or something
|
||||
// similar for when we change topics, this is the most basic
|
||||
// first pass outline
|
||||
let essay_polling_abort_controller = null;
|
||||
async function poll_for_new_essays() {
|
||||
const essays_list = document.getElementById("essays-list");
|
||||
const topic_id = document.body.dataset.topic;
|
||||
const last_essay_id = essays_list.dataset.last_essay_id;
|
||||
|
||||
if (!topic_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const message_polling_url = `/api/topics/${topic_id}/events?type=essay&limit=100&sort=newest&wait=true${last_essay_id ? `&after_id=${last_essay_id}` : ""}`;
|
||||
|
||||
essay_polling_abort_controller =
|
||||
essay_polling_abort_controller || new AbortController();
|
||||
|
||||
api.fetch(message_polling_url, {
|
||||
signal: essay_polling_abort_controller.signal,
|
||||
})
|
||||
.then(async (new_events_response) => {
|
||||
const new_events = (await new_events_response.json()) ?? [];
|
||||
await append_essays(new_events.reverse(), "afterbegin");
|
||||
poll_for_new_essays(topic_id);
|
||||
})
|
||||
.catch((error) => {
|
||||
// TODO: poll again? back off?
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
async function load_active_topic_for_essays() {
|
||||
const topic_id = document.body.dataset.topic;
|
||||
if (!topic_id) return;
|
||||
|
||||
const user = document.body.dataset.user
|
||||
? JSON.parse(document.body.dataset.user)
|
||||
: null;
|
||||
if (!user) return;
|
||||
|
||||
const essays_list = document.getElementById("essays-list");
|
||||
|
||||
if (essay_polling_abort_controller) {
|
||||
essay_polling_abort_controller.abort();
|
||||
essay_polling_abort_controller = null;
|
||||
delete essays_list.dataset.last_essay_id;
|
||||
}
|
||||
|
||||
const topic_response = await api.fetch(`/api/topics/${topic_id}`);
|
||||
if (!topic_response.ok) {
|
||||
const error = await topic_response.json();
|
||||
alert(error.message ?? JSON.stringify(error));
|
||||
return;
|
||||
}
|
||||
|
||||
const topic = await topic_response.json();
|
||||
|
||||
essays_list.innerHTML = "";
|
||||
|
||||
const events_response = await api.fetch(
|
||||
`/api/topics/${topic_id}/events?type=essay&limit=100&sort=newest`,
|
||||
);
|
||||
if (!events_response.ok) {
|
||||
const error = await events_response.json();
|
||||
alert(error.message ?? JSON.stringify(error));
|
||||
return;
|
||||
}
|
||||
|
||||
const events = await events_response.json();
|
||||
|
||||
await append_essays(events);
|
||||
poll_for_new_essays();
|
||||
}
|
||||
document.addEventListener("topic_changed", load_active_topic_for_essays);
|
||||
document.addEventListener("user_logged_in", load_active_topic_for_essays);
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
118
public/tabs/essays/new_essay.html
Normal file
118
public/tabs/essays/new_essay.html
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<style>
|
||||
.new-essay-container input[type="file"] {
|
||||
display: none;
|
||||
visibility: hidden;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.new-essay-container .essay-title-limit-counter,
|
||||
.new-essay-container .essay-limit-counter {
|
||||
font-size: smaller;
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
|
||||
.new-essay-container .file-attach-label {
|
||||
display: block;
|
||||
text-align: right;
|
||||
margin-top: -2.5rem;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.new-essay-container .file-attach-label .icon {
|
||||
display: inline-block;
|
||||
}
|
||||
</style>
|
||||
<div class="new-essay-container" data-requires-permission="topics.essays.create">
|
||||
<label>
|
||||
<input type="checkbox" collapse-toggle />
|
||||
<i class="icon plus" style="display: inline-block; margin-right: 0.5rem"></i>
|
||||
<span>New Essay</span>
|
||||
</label>
|
||||
<form
|
||||
data-smart="true"
|
||||
method="POST"
|
||||
class="essay-creation-form collapsible"
|
||||
style="
|
||||
margin-top: 1rem
|
||||
width: 100%;
|
||||
transition: all 0.5s;
|
||||
"
|
||||
on_reply="async (essay) => { await render_essay(essay, 'afterbegin'); document.getElementById(essay.id)?.classList.remove('sending'); }"
|
||||
on_parsed="async (essay) => { await render_essay(essay, 'afterbegin'); document.getElementById(essay.id)?.classList.add('sending'); }"
|
||||
>
|
||||
<input type="hidden" name="type" value="essay" />
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="id"
|
||||
generator="(_input, form) => 'TEMP-' + form.__submitted_at.toISOString()"
|
||||
reset-on-submit
|
||||
/>
|
||||
<input
|
||||
type="hidden"
|
||||
name="meta.temp_id"
|
||||
generator="(_input, form) => 'TEMP-' + form.__submitted_at.toISOString()"
|
||||
reset-on-submit
|
||||
/>
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="creator_id"
|
||||
generator="() => { return JSON.parse( document.body.dataset.user ?? '{}' ).id; }"
|
||||
/>
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="timestamps.created"
|
||||
generator="(_input, form) => form.__submitted_at.toISOString()"
|
||||
reset-on-submit
|
||||
/>
|
||||
<input
|
||||
type="hidden"
|
||||
name="timestamps.updated"
|
||||
generator="(_input, form) => form.__submitted_at.toISOString()"
|
||||
reset-on-submit
|
||||
/>
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="parent_id"
|
||||
generator="(_input, form) => { const parent_essay = form.closest( '.essay-container' ); return parent_essay?.dataset?.essay_id; }"
|
||||
/>
|
||||
|
||||
<textarea
|
||||
type="text"
|
||||
name="data.title"
|
||||
value=""
|
||||
maxlength="128"
|
||||
rows="2"
|
||||
placeholder="Essay title..."
|
||||
reset-on-submit
|
||||
></textarea>
|
||||
<div class="essay-title-limit-counter" data-limit-counter-for="data.title">0 / 128</div>
|
||||
|
||||
<textarea
|
||||
type="text"
|
||||
name="data.essay"
|
||||
value=""
|
||||
maxlength="65536"
|
||||
rows="16"
|
||||
placeholder=" Compose your thoughts here."
|
||||
reset-on-submit
|
||||
></textarea>
|
||||
<div class="essay-limit-counter" data-limit-counter-for="data.essay">0 / 65536</div>
|
||||
|
||||
<label class="file-attach-label">
|
||||
<input
|
||||
aria-label="Upload and share file"
|
||||
type="file"
|
||||
multiple
|
||||
data-smartforms-save-to-home="true"
|
||||
name="data.media"
|
||||
reset-on-submit
|
||||
/>
|
||||
<div class="icon attachment"></div>
|
||||
</label>
|
||||
<input type="submit" value="Publish It!" />
|
||||
</form>
|
||||
</div>
|
||||
Loading…
Add table
Add a link
Reference in a new issue