Skip to content

Commit

Permalink
Added comment element.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Oct 1, 2023
1 parent d751320 commit 70257b8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions nui/include/nui/frontend/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <nui/frontend/elements/caption.hpp>
#include <nui/frontend/elements/cite.hpp>
#include <nui/frontend/elements/code.hpp>
#include <nui/frontend/elements/comment.hpp>
#include <nui/frontend/elements/data.hpp>
#include <nui/frontend/elements/datalist.hpp>
#include <nui/frontend/elements/dd.hpp>
Expand Down
48 changes: 48 additions & 0 deletions nui/include/nui/frontend/elements/comment.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <nui/frontend/elements/impl/html_element_incl.hpp>
#include <nui/frontend/event_system/observed_value.hpp>
#include <nui/frontend/event_system/event_context.hpp>

#include <string_view>

namespace Nui::Elements
{
struct comment : HtmlElement
{
comment(comment const&) = default;
comment(comment&&) = default;
comment(std::string_view content)
: HtmlElement{"", &CommentElementBridge, Attribute{content, {}, {}}}
{}
comment(Nui::Observed<std::string>& obs)
: HtmlElement{
"",
&CommentElementBridge,
Attribute{
obs.value(),
[&obs](std::weak_ptr<Dom::ChildlessElement>&& element) {
const auto eventId = globalEventContext.registerEvent(Event{
[element, &obs](auto eventId) {
if (auto shared = element.lock(); shared)
{
shared->setNodeValue(obs.value());
return true;
}
obs.unattachEvent(eventId);
return false;
},
[element]() {
return !element.expired();
}});
obs.attachEvent(eventId);
return eventId;
},
[&obs](EventContext::EventIdType const& id) {
obs.unattachEvent(id);
},
},
}
{}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ namespace Nui
.call<Nui::val>("createTextNode", Nui::val{element.attributes()[0].stringData()});
},
};

constexpr auto CommentElementBridge = HtmlElementBridge{
.createElement =
+[](HtmlElement const& element) {
return Nui::val::global("document")
.call<Nui::val>("createComment", Nui::val{element.attributes()[0].stringData()});
},
};
}

0 comments on commit 70257b8

Please sign in to comment.