240 lines
No EOL
7 KiB
C++
240 lines
No EOL
7 KiB
C++
import Crafter.CppDOM;
|
|
import std;
|
|
using namespace Crafter;
|
|
using namespace Crafter::CppDOMBindings;
|
|
|
|
// Structure to represent a note
|
|
struct Note {
|
|
std::uint32_t id;
|
|
std::string content;
|
|
};
|
|
|
|
void RenderNotes();
|
|
|
|
// Global vector to store notes
|
|
std::vector<Note>* notes = new std::vector<Note>();
|
|
std::vector<HtmlElementView>* noteButtons = new std::vector<HtmlElementView>();
|
|
|
|
// Create the head section
|
|
HtmlElementView* head = new HtmlElementView("head", R"(
|
|
<title>Note Taking App</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 0 auto;
|
|
background-color: #121212;
|
|
color: #e0e0e0;
|
|
width: 100vw;
|
|
height: 100vh;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
.app-container {
|
|
background-color: #1e1e1e;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 2px 10px rgba(0,0,0,0.5);
|
|
width: 50vw;
|
|
max-width: 600px;
|
|
}
|
|
h1 {
|
|
color: #ffffff;
|
|
text-align: center;
|
|
}
|
|
.input-section {
|
|
margin-bottom: 20px;
|
|
padding: 15px;
|
|
background-color: #2d2d2d;
|
|
border-radius: 5px;
|
|
}
|
|
.input-section textarea {
|
|
width: 100%;
|
|
height: 80px;
|
|
border: 1px solid #444;
|
|
border-radius: 4px;
|
|
resize: vertical;
|
|
background-color: #333;
|
|
color: #fff;
|
|
}
|
|
.input-section button {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
margin-top: 10px;
|
|
}
|
|
.input-section button:hover {
|
|
background-color: #45a049;
|
|
}
|
|
.notes-section h2 {
|
|
color: #ffffff;
|
|
border-bottom: 2px solid #4CAF50;
|
|
padding-bottom: 5px;
|
|
}
|
|
.notes-list {
|
|
margin-top: 15px;
|
|
}
|
|
.note-item {
|
|
padding: 15px;
|
|
margin-bottom: 10px;
|
|
background-color: #2d2d2d;
|
|
border-left: 4px solid #4CAF50;
|
|
border-radius: 4px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: flex-start;
|
|
}
|
|
.note-content {
|
|
flex-grow: 1;
|
|
word-wrap: break-word;
|
|
color: #e0e0e0;
|
|
}
|
|
.delete-btn {
|
|
background-color: #f44336;
|
|
color: white;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
margin-left: 10px;
|
|
}
|
|
.delete-btn:hover {
|
|
background-color: #da190b;
|
|
}
|
|
#error, #loading {
|
|
padding: 10px;
|
|
margin: 10px 0;
|
|
border-radius: 4px;
|
|
}
|
|
#error {
|
|
background-color: #780000;
|
|
color: #ff9999;
|
|
border: 1px solid #ff5555;
|
|
}
|
|
#loading {
|
|
background-color: #003366;
|
|
color: #99ccff;
|
|
border: 1px solid #6699cc;
|
|
}
|
|
</style>
|
|
)");
|
|
|
|
// Create the body section
|
|
HtmlElementView* body = new HtmlElementView("body", R"(
|
|
<div class="app-container">
|
|
<h1>📝 Note Taking App</h1>
|
|
|
|
<div class="input-section">
|
|
<h2>Add New Note</h2>
|
|
<textarea id="noteInput" placeholder="Enter your note here..."></textarea>
|
|
<br>
|
|
<button id="addNoteBtn">Add Note</button>
|
|
</div>
|
|
|
|
<div id="loading"></div>
|
|
|
|
<div class="notes-section">
|
|
<h2>Your Notes</h2>
|
|
<div id="notesContainer"></div>
|
|
</div>
|
|
</div>
|
|
)");
|
|
HtmlElementView* addNoteBtn = new HtmlElementView("addNoteBtn");
|
|
HtmlElementView* noteInput = new HtmlElementView("noteInput");
|
|
HtmlElementView* loadingIndicator = new HtmlElementView("loading");
|
|
HtmlElementView* notesContainer = new HtmlElementView("notesContainer");
|
|
|
|
void FetchNotes() {
|
|
loadingIndicator->SetInnerHTML("<p>Loading notes...</p>");
|
|
|
|
Fetch("http://localhost:3000/getNotes", [](std::string input) {
|
|
notes->clear();
|
|
std::istringstream stream(input);
|
|
std::string line;
|
|
|
|
while (std::getline(stream, line)) {
|
|
// Skip empty lines
|
|
if (line.empty()) continue;
|
|
|
|
// First line is ID
|
|
std::uint32_t id = std::stoi(line);
|
|
|
|
// Read content until the next empty line
|
|
std::string content;
|
|
while (std::getline(stream, line) && !line.empty()) {
|
|
if (!content.empty()) content += "\n"; // preserve line breaks
|
|
content += line;
|
|
}
|
|
|
|
notes->push_back(Note{id, content});
|
|
}
|
|
|
|
loadingIndicator->SetInnerHTML("<p>Notes loaded!</p>");
|
|
RenderNotes();
|
|
});
|
|
}
|
|
|
|
// Function to delete a note via the backend
|
|
void DeleteNote(std::uint32_t id) {
|
|
loadingIndicator->SetInnerHTML("<p>Deleting note...</p>");
|
|
|
|
// Make POST request to delete note
|
|
Fetch("http://localhost:3000/deleteNote", std::to_string(id), [](std::string content) {
|
|
FetchNotes();
|
|
loadingIndicator->SetInnerHTML("<p>Note deleted!</p>");
|
|
});
|
|
}
|
|
|
|
// Function to render all notes to the DOM
|
|
void RenderNotes() {
|
|
if (notes->empty()) {
|
|
notesContainer->SetInnerHTML("<p>No notes yet. Add one below!</p>");
|
|
return;
|
|
}
|
|
|
|
std::string html = "<div class='notes-list'>";
|
|
noteButtons->clear();
|
|
for (const Note& note : *notes) {
|
|
html += std::format(
|
|
R"(<div class='note-item' id='note-{}'>
|
|
<div class='note-content'>{}</div>
|
|
<button id="note-{}-delete" class='delete-btn'>Delete</button>
|
|
</div>)",
|
|
note.id, note.content, note.id
|
|
);
|
|
}
|
|
html += "</div>";
|
|
|
|
notesContainer->SetInnerHTML(html);
|
|
|
|
for (const Note& note : *notes) {
|
|
HtmlElementView& view = noteButtons->emplace_back(std::format("note-{}-delete", note.id));
|
|
view.AddClickListener([id = note.id](MouseEvent event) {
|
|
DeleteNote(id);
|
|
});
|
|
}
|
|
}
|
|
|
|
// Main function
|
|
int main() {
|
|
FetchNotes();
|
|
|
|
// Add click listener to add note button
|
|
addNoteBtn->AddClickListener([&](MouseEvent event) {
|
|
std::string noteValue = noteInput->GetValue();
|
|
std::cout << noteValue << std::endl;
|
|
if (!noteValue.empty()) {
|
|
loadingIndicator->SetInnerHTML("<p>Saving note...</p>");
|
|
Fetch("http://localhost:3000/createNote", noteValue, [noteValue](std::string content) {
|
|
std::cout << noteValue << std::endl;
|
|
notes->push_back({static_cast<std::uint32_t>(std::stoi(content)), noteValue});
|
|
noteInput->SetValue("");
|
|
RenderNotes();
|
|
loadingIndicator->SetInnerHTML("<p>Saved note!</p>");
|
|
});
|
|
}
|
|
});
|
|
} |