Fix text -> blocks serialization fixes.

This commit is contained in:
John Preston
2026-06-08 11:55:33 +04:00
parent 43cc9199ec
commit dccc509860
3 changed files with 70 additions and 19 deletions
@@ -3135,8 +3135,9 @@ std::optional<int> State::normalizeTextOnlyListItemForInsertion(
return -1;
}
std::optional<int> State::normalizeTextOnlyQuoteForInsertion(
const BlockContainerPath &container) {
std::optional<int> State::normalizeTextOnlyQuoteSurface(
const BlockContainerPath &container,
bool keepEmptyParagraph) {
if (container.steps.empty()) {
return std::nullopt;
}
@@ -3159,14 +3160,19 @@ std::optional<int> State::normalizeTextOnlyQuoteForInsertion(
auto paragraph = MakeParagraphBlock();
paragraph.text = std::move(owner->text);
owner->text = RichText();
if (!BlockIsEmpty(paragraph)) {
clearTemporaryDownParagraph();
owner->blocks.push_back(std::move(paragraph));
clearTemporaryDownParagraph();
if (keepEmptyParagraph || !BlockIsEmpty(paragraph)) {
owner->blocks.insert(owner->blocks.begin(), std::move(paragraph));
return 0;
}
return -1;
}
std::optional<int> State::normalizeTextOnlyQuoteForInsertion(
const BlockContainerPath &container) {
return normalizeTextOnlyQuoteSurface(container, false);
}
bool State::shouldReplaceActiveTextOnlyBlock(
const TextNodeDescriptor &descriptor,
const std::vector<Block> &blocks) const {
@@ -3259,6 +3265,32 @@ auto State::resolveActiveTextInsertTarget()
},
};
}
if (descriptor->leaf.kind == LeafKind::BlockText) {
if (const auto owner = block(descriptor->leaf.block);
owner
&& owner->kind == BlockKind::Quote
&& !owner->pullquote
&& owner->blocks.empty()) {
const auto container = BlockChildrenContainer(
descriptor->leaf.block);
if (!normalizeTextOnlyQuoteSurface(container, true)) {
return std::nullopt;
}
return ActiveTextInsertTarget{
.leaf = {
.kind = LeafKind::BlockText,
.block = {
.container = container,
.index = 0,
},
},
.anchor = {
.container = container,
.blockIndex = 0,
},
};
}
}
return ActiveTextInsertTarget{
.leaf = descriptor->leaf,
.anchor = descriptor->insertionAnchor,
@@ -499,6 +499,9 @@ private:
[[nodiscard]] InsertionAnchor resolveActiveInsertionTarget() const;
[[nodiscard]] std::optional<int> normalizeTextOnlyListItemForInsertion(
const BlockContainerPath &container);
[[nodiscard]] std::optional<int> normalizeTextOnlyQuoteSurface(
const BlockContainerPath &container,
bool keepEmptyParagraph);
[[nodiscard]] std::optional<int> normalizeTextOnlyQuoteForInsertion(
const BlockContainerPath &container);
[[nodiscard]] bool shouldReplaceActiveTextOnlyBlock(
@@ -629,6 +629,19 @@ struct SerializeContext {
: std::nullopt;
}
[[nodiscard]] bool AppendSerializedParagraphBlock(
QVector<MTPPageBlock> *blocks,
const RichText &text,
const QString &anchorId,
SerializeContext *context) {
const auto paragraph = SerializeParagraphBlock(text, anchorId, context);
if (!paragraph) {
return false;
}
blocks->push_back(*paragraph);
return true;
}
[[nodiscard]] std::optional<MTPPageTableCell> SerializeTableCell(
const TableCell &cell,
SerializeContext *context) {
@@ -759,10 +772,15 @@ struct SerializeContext {
*caption))
: std::nullopt;
}
if (HasRichTextContent(block.text)) {
auto blocks = QVector<MTPPageBlock>();
if (HasRichTextContent(block.text)
&& !AppendSerializedParagraphBlock(
&blocks,
block.text,
QString(),
context)) {
return std::nullopt;
}
auto blocks = QVector<MTPPageBlock>();
const auto nested = SerializeBlocks(block.blocks, context);
if (!nested) {
return std::nullopt;
@@ -793,14 +811,13 @@ struct SerializeContext {
flags |= Flag::f_num;
auto blocks = QVector<MTPPageBlock>();
if (HasRichTextContent(item.text) || !item.anchorId.isEmpty()) {
const auto paragraph = SerializeParagraphBlock(
item.text,
item.anchorId,
context);
if (!paragraph) {
if (!AppendSerializedParagraphBlock(
&blocks,
item.text,
item.anchorId,
context)) {
return std::nullopt;
}
blocks.push_back(*paragraph);
}
const auto nested = SerializeBlocks(item.blocks, context);
if (!nested) {
@@ -858,14 +875,13 @@ struct SerializeContext {
}
auto blocks = QVector<MTPPageBlock>();
if (HasRichTextContent(item.text) || !item.anchorId.isEmpty()) {
const auto paragraph = SerializeParagraphBlock(
item.text,
item.anchorId,
context);
if (!paragraph) {
if (!AppendSerializedParagraphBlock(
&blocks,
item.text,
item.anchorId,
context)) {
return std::nullopt;
}
blocks.push_back(*paragraph);
}
const auto nested = SerializeBlocks(item.blocks, context);
if (!nested) {