SPA
This commit is contained in:
parent
c51dd48a73
commit
937b9fb48f
9 changed files with 180 additions and 90 deletions
|
|
@ -8,23 +8,73 @@ No permission is granted to copy, modify, distribute, or create derivative works
|
|||
|
||||
export module Catcrafts:Blog_impl;
|
||||
import :Blog;
|
||||
import :Root;
|
||||
import :Views;
|
||||
import std;
|
||||
|
||||
namespace Catcrafts {
|
||||
std::string RenderBlog() {
|
||||
using namespace Crafter::CppDOMBindings;
|
||||
|
||||
export namespace Catcrafts {
|
||||
std::vector<HtmlElementView>* blogButtons = new std::vector<HtmlElementView>();
|
||||
|
||||
void RenderBlog() {
|
||||
delete blogButtons;
|
||||
blogButtons = new std::vector<HtmlElementView>();
|
||||
std::string html = "";
|
||||
for(const BlogPost& post : *posts) {
|
||||
// For preview, we'll limit the content to first 200 characters
|
||||
std::string previewContent = post.content;
|
||||
if(previewContent.length() > 200) {
|
||||
// Find the last space before 200 characters to avoid cutting words
|
||||
std::size_t lastSpace = previewContent.find_last_of(' ', 200);
|
||||
if(lastSpace != std::string::npos) {
|
||||
previewContent = previewContent.substr(0, lastSpace) + "...";
|
||||
} else {
|
||||
previewContent = previewContent.substr(0, 200) + "...";
|
||||
}
|
||||
}
|
||||
html += std::format(R"(
|
||||
<div class="post fade-in">
|
||||
<div class="post-header">
|
||||
<h2 class="post-title">{}</h2>
|
||||
<h2 class="post-title"><a>{}</a></h2>
|
||||
<span class="post-date">{}</span>
|
||||
</div>
|
||||
<div class="post-content">
|
||||
{}
|
||||
</div>
|
||||
</div>)", post.name, post.date, post.content);
|
||||
<div class="post-footer">
|
||||
<a id="blog-button-{}" class="btn">Read Full Post</a>
|
||||
</div>
|
||||
</div>)", post.name, post.date, previewContent, post.slug);
|
||||
}
|
||||
return html;
|
||||
main->SetInnerHTML(html);
|
||||
|
||||
for(const BlogPost& post : *posts) {
|
||||
HtmlElementView& view = blogButtons->emplace_back(std::format("blog-button-{}", post.slug));
|
||||
view.AddClickListener([slug = post.slug](Crafter::MouseEvent e) {
|
||||
PushState("{}", "", std::format("/blog/{}", slug));
|
||||
RenderRoot(std::format("/blog/{}", slug));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void RenderBlogPost(const std::string_view slug) {
|
||||
for(const BlogPost& post : *posts) {
|
||||
if(post.slug == slug) {
|
||||
main->SetInnerHTML(std::format(R"(
|
||||
<div class="post fade-in">
|
||||
<div class="post-header">
|
||||
<h1 class="post-title">{}</h1>
|
||||
<span class="post-date">{}</span>
|
||||
</div>
|
||||
<div class="post-content">
|
||||
{}
|
||||
</div>
|
||||
</div>)", post.name, post.date, post.content));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
main->SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue