Added richtext clipboard to Emscripten and fixed emscripten compilation warning

This commit is contained in:
Yousef Khadadeh 2025-11-12 09:00:03 +03:00
parent 8830386dc0
commit 9eeee0dfff
5 changed files with 66 additions and 26 deletions

View file

@ -1,4 +1,5 @@
#include "TextBox.hpp"
#include "Helpers/Networking/ByteStream.hpp"
#include "TextStyleModifier.hpp"
#include "cereal/archives/portable_binary.hpp"
#include <limits>
@ -16,7 +17,7 @@
namespace RichText {
std::string TextData::get_serialized() {
std::string TextData::get_serialized() const {
std::stringstream s;
{
cereal::PortableBinaryOutputArchive a(s);
@ -54,6 +55,33 @@ void TextData::load(cereal::PortableBinaryInputArchive& a) {
}
}
TextData TextData::deserialize_string(std::string& s) {
if(s.empty()) {
RichText::TextData textData;
textData.paragraphs.emplace_back();
return textData;
}
RichText::TextData textData;
ByteMemStream ss(s.data(), s.length());
{
cereal::PortableBinaryInputArchive a(ss);
a(textData);
}
return textData;
}
std::string TextData::get_plain_text() const {
if(paragraphs.empty())
return "";
std::string toRet;
for(auto& p : paragraphs) {
toRet += p.text;
toRet += '\n';
}
toRet.pop_back();
return toRet;
}
bool TextPosition::operator<(const TextPosition& o) const {
return (this->fParagraphIndex < o.fParagraphIndex) || (this->fParagraphIndex == o.fParagraphIndex && this->fTextByteIndex < o.fTextByteIndex);
}
@ -151,11 +179,11 @@ void TextBox::process_mouse_left_button(Cursor& cur, const Vector2f& pos, bool c
}
}
std::pair<std::string, std::string> TextBox::process_copy(Cursor& cur) {
return {get_text_between(cur.selectionBeginPos, cur.selectionEndPos), get_rich_text_data_between(cur.selectionBeginPos, cur.selectionEndPos).get_serialized()};
std::pair<std::string, TextData> TextBox::process_copy(Cursor& cur) {
return {get_text_between(cur.selectionBeginPos, cur.selectionEndPos), get_rich_text_data_between(cur.selectionBeginPos, cur.selectionEndPos)};
}
std::pair<std::string, std::string> TextBox::process_cut(Cursor& cur) {
std::pair<std::string, TextData> TextBox::process_cut(Cursor& cur) {
auto toRet = process_copy(cur);
if(cur.selectionBeginPos != cur.selectionEndPos) {
cur.selectionEndPos = cur.selectionBeginPos = cur.pos = remove(cur.selectionBeginPos, cur.selectionEndPos);
@ -175,17 +203,11 @@ void TextBox::process_text_input(Cursor& cur, const std::string& in, const std::
}
}
void TextBox::process_rich_text_input(Cursor& cur, const std::string& serializedRichText) {
if(!serializedRichText.empty()) {
void TextBox::process_rich_text_input(Cursor& cur, const TextData& richText) {
if(!richText.paragraphs.empty() && !(richText.paragraphs.size() == 1 && richText.paragraphs[0].text.empty())) {
if(cur.selectionBeginPos != cur.selectionEndPos)
cur.selectionEndPos = cur.selectionBeginPos = cur.pos = remove(cur.selectionBeginPos, cur.selectionEndPos);
RichText::TextData textData;
std::stringstream ss(serializedRichText);
{
cereal::PortableBinaryInputArchive a(ss);
a(textData);
}
cur.selectionEndPos = cur.selectionBeginPos = cur.pos = insert_rich_text(cur.pos, textData);
cur.selectionEndPos = cur.selectionBeginPos = cur.pos = insert_rich_text(cur.pos, richText);
cur.previousX = std::nullopt;
inputChangedTextBox = true;
}

View file

@ -48,7 +48,9 @@ class TextData {
a(text, pStyleData);
}
};
std::string get_serialized();
std::string get_serialized() const;
static TextData deserialize_string(std::string& s);
std::string get_plain_text() const;
void save(cereal::PortableBinaryOutputArchive& a) const;
void load(cereal::PortableBinaryInputArchive& a);
TextStyleModContainer tStyleMods;
@ -133,13 +135,13 @@ class TextBox {
void process_mouse_left_button(Cursor& cur, const Vector2f& pos, bool clicked, bool held, bool shift);
void process_key_input(Cursor& cur, InputKey in, bool ctrl, bool shift, const std::optional<TextStyleModifier::ModifierMap>& inputModMap = std::nullopt);
std::pair<std::string, std::string> process_copy(Cursor& cur);
std::pair<std::string, std::string> process_cut(Cursor& cur);
std::pair<std::string, TextData> process_copy(Cursor& cur);
std::pair<std::string, TextData> process_cut(Cursor& cur);
std::string get_text_between(TextPosition p1, TextPosition p2);
std::string get_string();
void set_tab_space_width(unsigned newTabWidth);
void process_text_input(Cursor& cur, const std::string& in, const std::optional<TextStyleModifier::ModifierMap>& inputModMap = std::nullopt);
void process_rich_text_input(Cursor& cur, const std::string& serializedRichText);
void process_rich_text_input(Cursor& cur, const TextData& richText);
private:
std::pair<TextPosition, TextPosition> get_start_end_text_pos(TextPosition p1, TextPosition p2);