mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2026-08-07 12:15:19 +00:00
7207 lines
197 KiB
C++
7207 lines
197 KiB
C++
/*
|
|
This file is part of Telegram Desktop,
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
For license and copyright information please follow this link:
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
*/
|
|
#include "iv/editor/iv_editor_widget.h"
|
|
|
|
#include "base/qthelp_url.h"
|
|
#include "base/qt/qt_common_adapters.h"
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
|
#include "chat_helpers/message_field.h"
|
|
#include "core/mime_type.h"
|
|
#include "data/data_msg_id.h"
|
|
#include "data/data_types.h"
|
|
#include "data/stickers/data_custom_emoji.h"
|
|
#include "iv/editor/iv_editor_text_entities.h"
|
|
#include "iv/markdown/iv_markdown_article_paint.h"
|
|
#include "iv/markdown/iv_markdown_prepare_links.h"
|
|
#include "iv/markdown/iv_markdown_prepare_native_richtext.h"
|
|
#include "lang/lang_keys.h"
|
|
#include "main/main_session.h"
|
|
#include "main/session/session_show.h"
|
|
#include "menu/menu_checked_action.h"
|
|
#include "spellcheck/spellcheck_highlight_syntax.h"
|
|
#include "storage/storage_media_prepare.h"
|
|
#include "ui/chat/attach/attach_prepare.h"
|
|
#include "ui/chat/chat_style.h"
|
|
#include "ui/chat/chat_theme.h"
|
|
#include "ui/image/image_location.h"
|
|
#include "ui/layers/generic_box.h"
|
|
#include "ui/painter.h"
|
|
#include "ui/text/text_entity.h"
|
|
#include "ui/text/text_utilities.h"
|
|
#include "ui/ui_utility.h"
|
|
#include "ui/widgets/elastic_scroll.h"
|
|
#include "ui/widgets/checkbox.h"
|
|
#include "ui/widgets/fields/input_field.h"
|
|
#include "ui/widgets/menu/menu_action.h"
|
|
#include "ui/widgets/menu/menu_separator.h"
|
|
#include "ui/widgets/popup_menu.h"
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "styles/palette.h"
|
|
#include "styles/style_boxes.h"
|
|
#include "styles/style_chat.h"
|
|
#include "styles/style_iv.h"
|
|
#include "styles/style_layers.h"
|
|
#include "styles/style_menu_icons.h"
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
#include <QtCore/QDate>
|
|
#include <QtCore/QEvent>
|
|
#include <QtCore/QMimeData>
|
|
#include <QtCore/QPointer>
|
|
#include <QtGui/QClipboard>
|
|
#include <QtGui/QContextMenuEvent>
|
|
#include <QtGui/QFocusEvent>
|
|
#include <QtGui/QInputMethodEvent>
|
|
#include <QtGui/QKeyEvent>
|
|
#include <QtGui/QMouseEvent>
|
|
#include <QtGui/QResizeEvent>
|
|
#include <QtGui/QTextBlock>
|
|
#include <QtGui/QTouchEvent>
|
|
#include <QtGui/QWheelEvent>
|
|
#include <QtGui/QTextCursor>
|
|
#include <QtGui/QTextDocument>
|
|
#include <QtWidgets/QApplication>
|
|
#include <QtWidgets/QMenu>
|
|
#include <QtWidgets/QTextEdit>
|
|
#include <QAction>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace Iv::Editor {
|
|
namespace {
|
|
|
|
[[nodiscard]] std::vector<Ui::Text::SpecialColor> HighlightColors(
|
|
not_null<const Ui::ChatStyle*> style) {
|
|
auto result = Ui::SyntaxHighlightColors(style);
|
|
|
|
const auto &fg = style->lightButtonFg();
|
|
const auto &bg = style->lightButtonBgOver();
|
|
result.push_back({ &fg->p, &fg->p, &bg->b, &bg->b });
|
|
|
|
Ensures(result.size() == Markdown::kNativeIvLinkSpecialColorIndex);
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] std::unique_ptr<Ui::ChatTheme> CreateStandaloneChatTheme() {
|
|
const auto palette = style::main_palette::get();
|
|
return std::make_unique<Ui::ChatTheme>(Ui::ChatThemeDescriptor{
|
|
.preparePalette = [=](style::palette ©) {
|
|
copy = *palette;
|
|
},
|
|
.backgroundData = {
|
|
.colors = { palette->windowBg()->c },
|
|
},
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] const style::margins &EditorBodyPadding() {
|
|
return st::ivEditorBodyPadding;
|
|
}
|
|
|
|
[[nodiscard]] bool MatchesKeySequence(
|
|
QKeyEvent *e,
|
|
const QKeySequence &sequence) {
|
|
const auto searchKey = (e->modifiers() | e->key())
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
return sequence.matches(QKeySequence(searchKey))
|
|
== QKeySequence::ExactMatch;
|
|
}
|
|
|
|
constexpr auto kRetainedLeafFieldLimit = 50;
|
|
thread_local Widget *PreservingExternalFieldRestore = nullptr;
|
|
using ToolbarFormatAction = Widget::ToolbarFormatAction;
|
|
using ToolbarLinkMode = Widget::ToolbarLinkMode;
|
|
using TextFormattingAction = State::TextFormattingAction;
|
|
using TextNodeSpan = State::TextNodeSpan;
|
|
using StateBlockContainerKind = State::BlockContainerKind;
|
|
using StateBlockContainerPath = State::BlockContainerPath;
|
|
using StateBlockPath = State::BlockPath;
|
|
using StateLeafKind = State::LeafKind;
|
|
using StateLeafPath = State::LeafPath;
|
|
using PreparedBlockContainerKind = Markdown::PreparedEditBlockContainerKind;
|
|
using PreparedBlockContainerPath = Markdown::PreparedEditBlockContainerPath;
|
|
using PreparedBlockContainerStep = Markdown::PreparedEditBlockContainerStep;
|
|
using PreparedBlockPath = Markdown::PreparedEditBlockPath;
|
|
using PreparedBlockRange = Markdown::PreparedEditBlockRange;
|
|
using PreparedListItemRange = Markdown::PreparedEditListItemRange;
|
|
using PreparedSelection = Markdown::PreparedEditSelection;
|
|
using PreparedSelectionKind = Markdown::PreparedEditSelectionKind;
|
|
|
|
struct TextRange {
|
|
int offset = 0;
|
|
int length = 0;
|
|
};
|
|
|
|
[[nodiscard]] const QString *ToolbarActionTag(ToolbarFormatAction action) {
|
|
switch (action) {
|
|
case ToolbarFormatAction::Bold:
|
|
return &Ui::InputField::kTagBold;
|
|
case ToolbarFormatAction::Italic:
|
|
return &Ui::InputField::kTagItalic;
|
|
case ToolbarFormatAction::Underline:
|
|
return &Ui::InputField::kTagUnderline;
|
|
case ToolbarFormatAction::StrikeOut:
|
|
return &Ui::InputField::kTagStrikeOut;
|
|
case ToolbarFormatAction::Spoiler:
|
|
return &Ui::InputField::kTagSpoiler;
|
|
case ToolbarFormatAction::Subscript:
|
|
return &Ui::InputField::kTagIvSubscript;
|
|
case ToolbarFormatAction::Superscript:
|
|
return &Ui::InputField::kTagIvSuperscript;
|
|
case ToolbarFormatAction::Marked:
|
|
return &Ui::InputField::kTagIvMarked;
|
|
case ToolbarFormatAction::Math:
|
|
case ToolbarFormatAction::Undo:
|
|
case ToolbarFormatAction::Redo:
|
|
case ToolbarFormatAction::PlainText:
|
|
case ToolbarFormatAction::Link:
|
|
case ToolbarFormatAction::Count:
|
|
return nullptr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
[[nodiscard]] std::optional<TextFormattingAction> BroaderFormattingAction(
|
|
ToolbarFormatAction action) {
|
|
switch (action) {
|
|
case ToolbarFormatAction::Bold:
|
|
return TextFormattingAction::Bold;
|
|
case ToolbarFormatAction::Italic:
|
|
return TextFormattingAction::Italic;
|
|
case ToolbarFormatAction::Underline:
|
|
return TextFormattingAction::Underline;
|
|
case ToolbarFormatAction::StrikeOut:
|
|
return TextFormattingAction::StrikeOut;
|
|
case ToolbarFormatAction::Spoiler:
|
|
return TextFormattingAction::Spoiler;
|
|
case ToolbarFormatAction::PlainText:
|
|
return TextFormattingAction::PlainText;
|
|
case ToolbarFormatAction::Undo:
|
|
case ToolbarFormatAction::Redo:
|
|
case ToolbarFormatAction::Subscript:
|
|
case ToolbarFormatAction::Superscript:
|
|
case ToolbarFormatAction::Marked:
|
|
case ToolbarFormatAction::Link:
|
|
case ToolbarFormatAction::Math:
|
|
case ToolbarFormatAction::Count:
|
|
return std::nullopt;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
[[nodiscard]] bool RangeInsideText(
|
|
const QString &text,
|
|
int offset,
|
|
int length) {
|
|
return (offset >= 0)
|
|
&& (length >= 0)
|
|
&& (offset <= text.size())
|
|
&& ((offset + length) <= text.size());
|
|
}
|
|
|
|
[[nodiscard]] bool TagContains(QStringView tags, QStringView tagId) {
|
|
return TextUtilities::SplitTags(tags).contains(tagId);
|
|
}
|
|
|
|
[[nodiscard]] bool HasFullTextTag(
|
|
const TextWithTags &textWithTags,
|
|
const QString &tag) {
|
|
if (tag.isEmpty() || textWithTags.text.isEmpty()) {
|
|
return false;
|
|
}
|
|
auto ranges = std::vector<TextRange>();
|
|
ranges.reserve(textWithTags.tags.size());
|
|
for (const auto &existing : textWithTags.tags) {
|
|
if (existing.length <= 0
|
|
|| !RangeInsideText(
|
|
textWithTags.text,
|
|
existing.offset,
|
|
existing.length)
|
|
|| !TagContains(existing.id, tag)) {
|
|
continue;
|
|
}
|
|
ranges.push_back({
|
|
.offset = existing.offset,
|
|
.length = existing.length,
|
|
});
|
|
}
|
|
if (ranges.empty()) {
|
|
return false;
|
|
}
|
|
std::sort(ranges.begin(), ranges.end(), [](const auto &a, const auto &b) {
|
|
if (a.offset != b.offset) {
|
|
return a.offset < b.offset;
|
|
}
|
|
return a.length < b.length;
|
|
});
|
|
auto coveredTill = 0;
|
|
for (const auto &range : ranges) {
|
|
if (range.offset > coveredTill) {
|
|
return false;
|
|
}
|
|
coveredTill = std::max(coveredTill, range.offset + range.length);
|
|
if (coveredTill >= textWithTags.text.size()) {
|
|
return true;
|
|
}
|
|
}
|
|
return (coveredTill >= textWithTags.text.size());
|
|
}
|
|
|
|
[[nodiscard]] bool SplitTextSpan(
|
|
const TextWithEntities &text,
|
|
int from,
|
|
int till,
|
|
TextWithEntities *before,
|
|
TextWithEntities *selected,
|
|
TextWithEntities *after) {
|
|
if (!before || !selected || !after) {
|
|
return false;
|
|
}
|
|
const auto textSize = int(text.text.size());
|
|
from = std::clamp(from, 0, textSize);
|
|
till = std::clamp(till, from, textSize);
|
|
if (from >= till) {
|
|
return false;
|
|
}
|
|
*before = Ui::Text::Mid(text, 0, from);
|
|
*selected = Ui::Text::Mid(text, from, till - from);
|
|
if (selected->text.isEmpty()) {
|
|
return false;
|
|
}
|
|
*after = Ui::Text::Mid(text, till);
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] PreparedBlockContainerPath ToPreparedBlockContainerPath(
|
|
const StateBlockContainerPath &path) {
|
|
auto result = PreparedBlockContainerPath();
|
|
result.steps.reserve(path.steps.size());
|
|
for (const auto &step : path.steps) {
|
|
auto converted = PreparedBlockContainerStep();
|
|
converted.blockIndex = step.blockIndex;
|
|
converted.listItemIndex = step.listItemIndex;
|
|
switch (step.kind) {
|
|
case StateBlockContainerKind::Root:
|
|
continue;
|
|
case StateBlockContainerKind::BlockChildren:
|
|
converted.kind = PreparedBlockContainerKind::BlockChildren;
|
|
break;
|
|
case StateBlockContainerKind::ListItemChildren:
|
|
converted.kind = PreparedBlockContainerKind::ListItemChildren;
|
|
break;
|
|
}
|
|
result.steps.push_back(converted);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] PreparedBlockPath ToPreparedBlockPath(
|
|
const StateBlockPath &path) {
|
|
return {
|
|
.container = ToPreparedBlockContainerPath(path.container),
|
|
.index = path.index,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] bool PreparedContainerHasPrefix(
|
|
const PreparedBlockContainerPath &path,
|
|
const PreparedBlockContainerPath &prefix) {
|
|
if (path.steps.size() < prefix.steps.size()) {
|
|
return false;
|
|
}
|
|
return std::equal(
|
|
prefix.steps.begin(),
|
|
prefix.steps.end(),
|
|
path.steps.begin());
|
|
}
|
|
|
|
[[nodiscard]] bool IndexInRange(int index, int from, int till) {
|
|
return (index >= from) && (index < till);
|
|
}
|
|
|
|
[[nodiscard]] bool PreparedPathInBlockRange(
|
|
const PreparedBlockPath &path,
|
|
const PreparedBlockRange &range) {
|
|
if (path.container == range.container) {
|
|
return IndexInRange(path.index, range.from, range.till);
|
|
}
|
|
if (!PreparedContainerHasPrefix(path.container, range.container)
|
|
|| (path.container.steps.size() <= range.container.steps.size())) {
|
|
return false;
|
|
}
|
|
const auto &step = path.container.steps[range.container.steps.size()];
|
|
return IndexInRange(step.blockIndex, range.from, range.till);
|
|
}
|
|
|
|
[[nodiscard]] bool PreparedPathInListItemRange(
|
|
const PreparedBlockPath &path,
|
|
const PreparedListItemRange &range) {
|
|
if (!PreparedContainerHasPrefix(path.container, range.block.container)
|
|
|| (path.container.steps.size() <= range.block.container.steps.size())) {
|
|
return false;
|
|
}
|
|
const auto &step = path.container.steps[range.block.container.steps.size()];
|
|
return (step.kind == PreparedBlockContainerKind::ListItemChildren)
|
|
&& (step.blockIndex == range.block.index)
|
|
&& IndexInRange(step.listItemIndex, range.from, range.till);
|
|
}
|
|
|
|
[[nodiscard]] const std::vector<RichPage::Block> *BlockContainer(
|
|
const RichPage &page,
|
|
const StateBlockContainerPath &path) {
|
|
const auto *current = &page.blocks;
|
|
for (const auto &step : path.steps) {
|
|
if (!current) {
|
|
return nullptr;
|
|
}
|
|
switch (step.kind) {
|
|
case StateBlockContainerKind::Root:
|
|
break;
|
|
case StateBlockContainerKind::BlockChildren: {
|
|
if (step.blockIndex < 0 || step.blockIndex >= int(current->size())) {
|
|
return nullptr;
|
|
}
|
|
current = &(*current)[step.blockIndex].blocks;
|
|
} break;
|
|
case StateBlockContainerKind::ListItemChildren: {
|
|
if (step.blockIndex < 0 || step.blockIndex >= int(current->size())) {
|
|
return nullptr;
|
|
}
|
|
const auto &block = (*current)[step.blockIndex];
|
|
if (step.listItemIndex < 0
|
|
|| step.listItemIndex >= int(block.listItems.size())) {
|
|
return nullptr;
|
|
}
|
|
current = &block.listItems[step.listItemIndex].blocks;
|
|
} break;
|
|
}
|
|
}
|
|
return current;
|
|
}
|
|
|
|
[[nodiscard]] const RichPage::Block *BlockFromPath(
|
|
const RichPage &page,
|
|
const StateBlockPath &path) {
|
|
const auto *container = BlockContainer(page, path.container);
|
|
if (!container || path.index < 0 || path.index >= int(container->size())) {
|
|
return nullptr;
|
|
}
|
|
return &(*container)[path.index];
|
|
}
|
|
|
|
[[nodiscard]] const RichPage::RichText *RichTextFromPath(
|
|
const RichPage &page,
|
|
const StateLeafPath &path) {
|
|
const auto block = BlockFromPath(page, path.block);
|
|
if (!block) {
|
|
return nullptr;
|
|
}
|
|
switch (path.kind) {
|
|
case StateLeafKind::BlockText:
|
|
return &block->text;
|
|
case StateLeafKind::BlockCaption:
|
|
return &block->caption;
|
|
case StateLeafKind::ListItemText:
|
|
if (path.listItemIndex < 0
|
|
|| path.listItemIndex >= int(block->listItems.size())) {
|
|
return nullptr;
|
|
}
|
|
return &block->listItems[path.listItemIndex].text;
|
|
case StateLeafKind::TableCellText:
|
|
if (path.tableRowIndex < 0
|
|
|| path.tableRowIndex >= int(block->tableRows.size())) {
|
|
return nullptr;
|
|
}
|
|
if (path.tableCellIndex < 0
|
|
|| path.tableCellIndex
|
|
>= int(block->tableRows[path.tableRowIndex].cells.size())) {
|
|
return nullptr;
|
|
}
|
|
return &block->tableRows[path.tableRowIndex].cells[path.tableCellIndex]
|
|
.text;
|
|
case StateLeafKind::MathFormula:
|
|
return nullptr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
using TableGridOccupancyRow = std::vector<char>;
|
|
using TableGridOccupancy = std::vector<TableGridOccupancyRow>;
|
|
|
|
struct TableGridCellReference {
|
|
int rowIndex = -1;
|
|
int cellIndex = -1;
|
|
int rowFrom = -1;
|
|
int rowTill = -1;
|
|
int columnFrom = -1;
|
|
int columnTill = -1;
|
|
};
|
|
|
|
struct TableGrid {
|
|
std::vector<TableGridCellReference> cells;
|
|
TableGridOccupancy occupancy;
|
|
int rowCount = 0;
|
|
int columnCount = 0;
|
|
};
|
|
|
|
[[nodiscard]] int NormalizeTableSpan(int span) {
|
|
return std::max(span, 1);
|
|
}
|
|
|
|
[[nodiscard]] int ClampTableRowspan(
|
|
int rawRowspan,
|
|
int row,
|
|
int rowCount) {
|
|
if ((row < 0) || (row >= rowCount) || (rowCount <= 0)) {
|
|
return 0;
|
|
}
|
|
const auto remainingRows = int64(rowCount) - row;
|
|
return int(std::min<int64>(NormalizeTableSpan(rawRowspan), remainingRows));
|
|
}
|
|
|
|
[[nodiscard]] int ClampTableColspan(
|
|
int rawColspan,
|
|
int column,
|
|
int maxColumns) {
|
|
if ((column < 0) || (column >= maxColumns) || (maxColumns <= 0)) {
|
|
return 0;
|
|
}
|
|
const auto remainingColumns = int64(maxColumns) - column;
|
|
return int(std::min<int64>(
|
|
NormalizeTableSpan(rawColspan),
|
|
remainingColumns));
|
|
}
|
|
|
|
[[nodiscard]] bool CanOccupyTableSlots(
|
|
const TableGridOccupancy &occupancy,
|
|
int row,
|
|
int column,
|
|
int rowspan,
|
|
int colspan) {
|
|
if ((row < 0)
|
|
|| (column < 0)
|
|
|| (rowspan <= 0)
|
|
|| (colspan <= 0)
|
|
|| (row >= int(occupancy.size()))) {
|
|
return false;
|
|
}
|
|
const auto rowLimit = int(std::min<int64>(
|
|
int64(row) + rowspan,
|
|
occupancy.size()));
|
|
const auto columnLimit64 = int64(column) + colspan;
|
|
if (columnLimit64 <= column) {
|
|
return false;
|
|
}
|
|
const auto columnLimit = int(std::min<int64>(
|
|
columnLimit64,
|
|
std::numeric_limits<int>::max()));
|
|
for (auto currentRow = row; currentRow < rowLimit; ++currentRow) {
|
|
const auto &occupied = occupancy[currentRow];
|
|
const auto occupiedLimit = std::min(columnLimit, int(occupied.size()));
|
|
for (auto currentColumn = column;
|
|
currentColumn < occupiedLimit;
|
|
++currentColumn) {
|
|
if (occupied[currentColumn]) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] int FirstAvailableTableColumn(
|
|
const TableGridOccupancy &occupancy,
|
|
int row,
|
|
int rowspan,
|
|
int colspan,
|
|
int maxColumns) {
|
|
if ((row < 0)
|
|
|| (row >= int(occupancy.size()))
|
|
|| (rowspan <= 0)
|
|
|| (colspan <= 0)
|
|
|| (maxColumns <= 0)) {
|
|
return -1;
|
|
}
|
|
for (auto column = 0; column < maxColumns; ++column) {
|
|
const auto effectiveColspan = ClampTableColspan(
|
|
colspan,
|
|
column,
|
|
maxColumns);
|
|
if (effectiveColspan <= 0) {
|
|
continue;
|
|
}
|
|
if (CanOccupyTableSlots(
|
|
occupancy,
|
|
row,
|
|
column,
|
|
rowspan,
|
|
effectiveColspan)) {
|
|
return column;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void MarkTableSlots(
|
|
TableGridOccupancy *occupancy,
|
|
int row,
|
|
int column,
|
|
int rowspan,
|
|
int colspan) {
|
|
if ((row < 0)
|
|
|| (column < 0)
|
|
|| (rowspan <= 0)
|
|
|| (colspan <= 0)
|
|
|| (row >= int(occupancy->size()))) {
|
|
return;
|
|
}
|
|
const auto rowLimit = int(std::min<int64>(
|
|
int64(row) + rowspan,
|
|
occupancy->size()));
|
|
const auto columnLimit64 = int64(column) + colspan;
|
|
if (columnLimit64 <= column) {
|
|
return;
|
|
}
|
|
const auto columnLimit = int(std::min<int64>(
|
|
columnLimit64,
|
|
std::numeric_limits<int>::max()));
|
|
for (auto currentRow = row; currentRow < rowLimit; ++currentRow) {
|
|
auto &occupied = (*occupancy)[currentRow];
|
|
if (columnLimit > int(occupied.size())) {
|
|
occupied.resize(columnLimit, false);
|
|
}
|
|
for (auto currentColumn = column;
|
|
currentColumn < columnLimit;
|
|
++currentColumn) {
|
|
occupied[currentColumn] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] int TableGridColumnCount(const TableGridOccupancy &occupancy) {
|
|
auto result = 0;
|
|
for (const auto &row : occupancy) {
|
|
result = std::max(result, int(row.size()));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] int TableMaxColumns(const RichPage::Block &table) {
|
|
auto result = 0;
|
|
for (const auto &row : table.tableRows) {
|
|
auto columns = 0;
|
|
for (const auto &cell : row.cells) {
|
|
columns += NormalizeTableSpan(cell.colspan);
|
|
}
|
|
result = std::max(result, columns);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] TableGrid BuildTableGrid(const RichPage::Block &table) {
|
|
auto result = TableGrid();
|
|
result.rowCount = int(table.tableRows.size());
|
|
result.occupancy = TableGridOccupancy(result.rowCount);
|
|
const auto maxColumns = TableMaxColumns(table);
|
|
if (result.rowCount <= 0 || maxColumns <= 0) {
|
|
return result;
|
|
}
|
|
for (auto rowIndex = 0; rowIndex != result.rowCount; ++rowIndex) {
|
|
const auto &row = table.tableRows[rowIndex];
|
|
for (auto cellIndex = 0, cellCount = int(row.cells.size());
|
|
cellIndex != cellCount;
|
|
++cellIndex) {
|
|
const auto &cell = row.cells[cellIndex];
|
|
const auto normalizedColspan = NormalizeTableSpan(cell.colspan);
|
|
const auto rowspan = ClampTableRowspan(
|
|
cell.rowspan,
|
|
rowIndex,
|
|
result.rowCount);
|
|
if (rowspan <= 0) {
|
|
continue;
|
|
}
|
|
const auto column = FirstAvailableTableColumn(
|
|
result.occupancy,
|
|
rowIndex,
|
|
rowspan,
|
|
normalizedColspan,
|
|
maxColumns);
|
|
if (column < 0) {
|
|
continue;
|
|
}
|
|
const auto colspan = ClampTableColspan(
|
|
normalizedColspan,
|
|
column,
|
|
maxColumns);
|
|
if (colspan <= 0) {
|
|
continue;
|
|
}
|
|
result.cells.push_back({
|
|
.rowIndex = rowIndex,
|
|
.cellIndex = cellIndex,
|
|
.rowFrom = rowIndex,
|
|
.rowTill = rowIndex + rowspan,
|
|
.columnFrom = column,
|
|
.columnTill = column + colspan,
|
|
});
|
|
MarkTableSlots(
|
|
&result.occupancy,
|
|
rowIndex,
|
|
column,
|
|
rowspan,
|
|
colspan);
|
|
}
|
|
}
|
|
result.columnCount = TableGridColumnCount(result.occupancy);
|
|
return result;
|
|
}
|
|
|
|
template <typename Range>
|
|
[[nodiscard]] bool TableGridCellIntersectsRange(
|
|
const TableGridCellReference &cell,
|
|
const Range &range) {
|
|
return (cell.rowFrom < range.rowTill)
|
|
&& (cell.rowTill > range.rowFrom)
|
|
&& (cell.columnFrom < range.columnTill)
|
|
&& (cell.columnTill > range.columnFrom);
|
|
}
|
|
|
|
template <typename Range>
|
|
[[nodiscard]] std::vector<TableGridCellReference> SelectedTableGridCells(
|
|
const TableGrid &grid,
|
|
const Range &range) {
|
|
auto result = std::vector<TableGridCellReference>();
|
|
result.reserve(grid.cells.size());
|
|
for (const auto &cell : grid.cells) {
|
|
if (TableGridCellIntersectsRange(cell, range)) {
|
|
result.push_back(cell);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] bool TableGridCellMatchesLeaf(
|
|
const TableGridCellReference &cell,
|
|
const StateLeafPath &leaf,
|
|
const StateBlockPath &block) {
|
|
return (leaf.block == block)
|
|
&& (leaf.kind == StateLeafKind::TableCellText)
|
|
&& (leaf.tableRowIndex == cell.rowIndex)
|
|
&& (leaf.tableCellIndex == cell.cellIndex);
|
|
}
|
|
|
|
[[nodiscard]] bool LeafSelectedStructurally(
|
|
const RichPage &page,
|
|
const StateLeafPath &leaf,
|
|
const PreparedSelection &selection) {
|
|
const auto path = ToPreparedBlockPath(leaf.block);
|
|
switch (selection.kind) {
|
|
case PreparedSelectionKind::Blocks:
|
|
return PreparedPathInBlockRange(path, selection.blocks);
|
|
case PreparedSelectionKind::ListItems:
|
|
if (leaf.kind == StateLeafKind::ListItemText
|
|
&& (path == selection.listItems.block)
|
|
&& IndexInRange(
|
|
leaf.listItemIndex,
|
|
selection.listItems.from,
|
|
selection.listItems.till)) {
|
|
return true;
|
|
}
|
|
return PreparedPathInListItemRange(path, selection.listItems);
|
|
case PreparedSelectionKind::TableRows:
|
|
return (leaf.kind == StateLeafKind::TableCellText)
|
|
&& (path == selection.tableRows.block)
|
|
&& IndexInRange(
|
|
leaf.tableRowIndex,
|
|
selection.tableRows.from,
|
|
selection.tableRows.till);
|
|
case PreparedSelectionKind::TableCells: {
|
|
if (leaf.kind != StateLeafKind::TableCellText
|
|
|| (path != selection.tableCells.block)) {
|
|
return false;
|
|
}
|
|
const auto owner = BlockFromPath(page, leaf.block);
|
|
if (!owner || owner->kind != RichPage::BlockKind::Table) {
|
|
return false;
|
|
}
|
|
for (const auto &reference : SelectedTableGridCells(
|
|
BuildTableGrid(*owner),
|
|
selection.tableCells)) {
|
|
if (TableGridCellMatchesLeaf(reference, leaf, leaf.block)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
case PreparedSelectionKind::None:
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
[[nodiscard]] bool BlockSelectedStructurally(
|
|
const StateBlockPath &path,
|
|
const PreparedSelection &selection) {
|
|
const auto prepared = ToPreparedBlockPath(path);
|
|
switch (selection.kind) {
|
|
case PreparedSelectionKind::Blocks:
|
|
return PreparedPathInBlockRange(prepared, selection.blocks);
|
|
case PreparedSelectionKind::ListItems:
|
|
return PreparedPathInListItemRange(prepared, selection.listItems);
|
|
case PreparedSelectionKind::TableRows:
|
|
case PreparedSelectionKind::TableCells:
|
|
case PreparedSelectionKind::None:
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
[[nodiscard]] bool MediaBlockSupportsSpoiler(
|
|
const RichPage::Block &block) {
|
|
switch (block.kind) {
|
|
case RichPage::BlockKind::Photo:
|
|
case RichPage::BlockKind::Video:
|
|
case RichPage::BlockKind::Audio:
|
|
case RichPage::BlockKind::Map:
|
|
return true;
|
|
case RichPage::BlockKind::GroupedMedia:
|
|
return ranges::any_of(
|
|
block.mediaItems,
|
|
[](const RichPage::GroupedMediaItem &item) {
|
|
return (item.kind == RichPage::BlockKind::Photo)
|
|
|| (item.kind == RichPage::BlockKind::Video)
|
|
|| (item.kind == RichPage::BlockKind::Audio)
|
|
|| (item.kind == RichPage::BlockKind::Map);
|
|
});
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] bool MediaBlockHasSpoiler(
|
|
const RichPage::Block &block) {
|
|
if (block.kind == RichPage::BlockKind::GroupedMedia) {
|
|
auto any = false;
|
|
for (const auto &item : block.mediaItems) {
|
|
if ((item.kind != RichPage::BlockKind::Photo)
|
|
&& (item.kind != RichPage::BlockKind::Video)
|
|
&& (item.kind != RichPage::BlockKind::Audio)
|
|
&& (item.kind != RichPage::BlockKind::Map)) {
|
|
continue;
|
|
}
|
|
any = true;
|
|
if (!item.spoiler) {
|
|
return false;
|
|
}
|
|
}
|
|
return any;
|
|
}
|
|
return block.spoiler;
|
|
}
|
|
|
|
[[nodiscard]] StateBlockContainerPath BlockChildrenContainer(
|
|
StateBlockPath path) {
|
|
auto result = std::move(path.container);
|
|
result.steps.push_back({
|
|
.kind = StateBlockContainerKind::BlockChildren,
|
|
.blockIndex = path.index,
|
|
});
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] StateBlockContainerPath ListItemChildrenContainer(
|
|
StateBlockPath path,
|
|
int itemIndex) {
|
|
auto result = std::move(path.container);
|
|
result.steps.push_back({
|
|
.kind = StateBlockContainerKind::ListItemChildren,
|
|
.blockIndex = path.index,
|
|
.listItemIndex = itemIndex,
|
|
});
|
|
return result;
|
|
}
|
|
|
|
template <typename Callback>
|
|
void EnumerateBlockPaths(
|
|
const RichPage &page,
|
|
const StateBlockContainerPath &container,
|
|
Callback &&callback) {
|
|
const auto *blocks = BlockContainer(page, container);
|
|
if (!blocks) {
|
|
return;
|
|
}
|
|
for (auto index = 0, count = int(blocks->size()); index != count; ++index) {
|
|
const auto path = StateBlockPath{
|
|
.container = container,
|
|
.index = index,
|
|
};
|
|
const auto &block = (*blocks)[index];
|
|
callback(path, block);
|
|
EnumerateBlockPaths(page, BlockChildrenContainer(path), callback);
|
|
for (auto itemIndex = 0, itemCount = int(block.listItems.size());
|
|
itemIndex != itemCount;
|
|
++itemIndex) {
|
|
EnumerateBlockPaths(
|
|
page,
|
|
ListItemChildrenContainer(path, itemIndex),
|
|
callback);
|
|
}
|
|
}
|
|
}
|
|
|
|
void EnableQTextEditLineMetrics(style::TextStyle &style) {
|
|
style.qtextEditLineMetrics = true;
|
|
}
|
|
|
|
void EnableQTextEditLineMetrics(style::Markdown &style) {
|
|
EnableQTextEditLineMetrics(style.body);
|
|
EnableQTextEditLineMetrics(style.heading1);
|
|
EnableQTextEditLineMetrics(style.heading2);
|
|
EnableQTextEditLineMetrics(style.heading3);
|
|
EnableQTextEditLineMetrics(style.heading4);
|
|
EnableQTextEditLineMetrics(style.heading5);
|
|
EnableQTextEditLineMetrics(style.heading6);
|
|
EnableQTextEditLineMetrics(style.code);
|
|
EnableQTextEditLineMetrics(style.displayMath.fallbackStyle);
|
|
EnableQTextEditLineMetrics(style.table.headerStyle);
|
|
EnableQTextEditLineMetrics(style.table.bodyStyle);
|
|
EnableQTextEditLineMetrics(style.details.summaryStyle);
|
|
EnableQTextEditLineMetrics(style.embedPost.authorStyle);
|
|
EnableQTextEditLineMetrics(style.embedPost.dateStyle);
|
|
EnableQTextEditLineMetrics(style.placeholder.labelStyle);
|
|
EnableQTextEditLineMetrics(style.audio.titleStyle);
|
|
EnableQTextEditLineMetrics(style.audio.subtitleStyle);
|
|
EnableQTextEditLineMetrics(style.channel.titleStyle);
|
|
EnableQTextEditLineMetrics(style.channel.subtitleStyle);
|
|
EnableQTextEditLineMetrics(style.channel.button.textStyle);
|
|
EnableQTextEditLineMetrics(style.relatedArticle.titleStyle);
|
|
EnableQTextEditLineMetrics(style.relatedArticle.subtitleStyle);
|
|
EnableQTextEditLineMetrics(style.relatedArticle.footerStyle);
|
|
}
|
|
|
|
[[nodiscard]] style::Markdown CreateEditorMarkdownStyle() {
|
|
auto result = st::messageMarkdown;
|
|
result.pageMaxWidth = st::defaultMarkdown.pageMaxWidth;
|
|
EnableQTextEditLineMetrics(result);
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] int CompareSelectionPositions(
|
|
Markdown::MarkdownArticleSelectionPosition a,
|
|
Markdown::MarkdownArticleSelectionPosition b) {
|
|
if (a.segment != b.segment) {
|
|
return (a.segment < b.segment) ? -1 : 1;
|
|
}
|
|
if (a.offset != b.offset) {
|
|
return (a.offset < b.offset) ? -1 : 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
[[nodiscard]] Markdown::MarkdownArticleSelection NormalizeSelection(
|
|
Markdown::MarkdownArticleSelection selection) {
|
|
if (selection.empty()) {
|
|
return {};
|
|
}
|
|
if (CompareSelectionPositions(selection.from, selection.to) > 0) {
|
|
std::swap(selection.from, selection.to);
|
|
}
|
|
return selection;
|
|
}
|
|
|
|
[[nodiscard]] Markdown::MarkdownArticleSelectionEndpoint MakeSelectionEndpoint(
|
|
const Markdown::MarkdownArticleHitTestResult &hit) {
|
|
return {
|
|
.segment = hit.segmentIndex,
|
|
.direct = hit.direct,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] bool RedirectTextToField(const QString &text) {
|
|
for (const auto &ch : text) {
|
|
if (ch.unicode() >= 32) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
struct InlineFieldTrimResult {
|
|
TextWithTags text;
|
|
int left = 0;
|
|
};
|
|
|
|
[[nodiscard]] InlineFieldTrimResult TrimInlineFieldText(
|
|
TextWithTags text,
|
|
bool trimLeft) {
|
|
auto from = 0;
|
|
auto till = int(text.text.size());
|
|
if (trimLeft) {
|
|
while (from < till && text.text[from].isSpace()) {
|
|
++from;
|
|
}
|
|
}
|
|
while (till > from && text.text[till - 1].isSpace()) {
|
|
--till;
|
|
}
|
|
if (from == 0 && till == text.text.size()) {
|
|
return { std::move(text), 0 };
|
|
}
|
|
text.text = text.text.mid(from, till - from);
|
|
for (auto i = text.tags.begin(); i != text.tags.end();) {
|
|
const auto tagFrom = i->offset;
|
|
const auto tagTill = i->offset + i->length;
|
|
const auto clippedFrom = std::max(tagFrom, from);
|
|
const auto clippedTill = std::min(tagTill, till);
|
|
if (clippedTill <= clippedFrom || i->length <= 0) {
|
|
i = text.tags.erase(i);
|
|
} else {
|
|
i->offset = clippedFrom - from;
|
|
i->length = clippedTill - clippedFrom;
|
|
++i;
|
|
}
|
|
}
|
|
return { std::move(text), from };
|
|
}
|
|
|
|
[[nodiscard]] int MapEditorOffsetToRichOffset(
|
|
const std::vector<RichTextEditorOffsetReplacement> &replacements,
|
|
int offset) {
|
|
auto delta = 0;
|
|
for (const auto &replacement : replacements) {
|
|
if (replacement.richLength <= 0) {
|
|
continue;
|
|
}
|
|
const auto richStart = replacement.richOffset;
|
|
const auto editorStart = richStart + delta;
|
|
const auto editorEnd = editorStart + replacement.editorLength;
|
|
if (offset < editorStart) {
|
|
break;
|
|
} else if (offset <= editorEnd) {
|
|
return richStart
|
|
+ ((offset == editorEnd) ? replacement.richLength : 0);
|
|
}
|
|
delta += replacement.editorLength - replacement.richLength;
|
|
}
|
|
return offset - delta;
|
|
}
|
|
|
|
[[nodiscard]] auto ClipboardPasteInsertContext(
|
|
std::optional<State::ActiveTextInsertContext> context)
|
|
-> std::optional<State::ActiveTextInsertContext> {
|
|
if (context) {
|
|
context->selected = TextWithEntities();
|
|
}
|
|
return context;
|
|
}
|
|
|
|
[[nodiscard]] std::optional<Ui::PreparedList> PreparedMediaFromClipboard(
|
|
not_null<const QMimeData*> data,
|
|
bool premium) {
|
|
const auto hasImage = data->hasImage();
|
|
const auto urls = Core::ReadMimeUrls(data);
|
|
if (!urls.empty()) {
|
|
auto list = Storage::PrepareMediaList(
|
|
urls,
|
|
st::sendMediaPreviewSize,
|
|
premium);
|
|
if (list.error != Ui::PreparedList::Error::NonLocalUrl) {
|
|
return std::move(list);
|
|
} else if (!hasImage) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
if (auto read = Core::ReadMimeImage(data)) {
|
|
return Storage::PrepareMediaFromImage(
|
|
std::move(read.image),
|
|
std::move(read.content),
|
|
st::sendMediaPreviewSize);
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
[[nodiscard]] bool CanPrepareMediaFromClipboard(
|
|
not_null<const QMimeData*> data) {
|
|
using State = Storage::MimeDataState;
|
|
const auto state = Storage::ComputeMimeDataState(data.get());
|
|
return (state == State::Image)
|
|
|| (state == State::PhotoFiles)
|
|
|| (state == State::MediaFiles);
|
|
}
|
|
|
|
[[nodiscard]] QString ValidateInstantViewEditorLink(QString link) {
|
|
const auto normal = qthelp::validate_url(link);
|
|
if (!normal.isEmpty()) {
|
|
return normal;
|
|
}
|
|
link = link.trimmed();
|
|
const auto hasPayload = [&](const QString &prefix) {
|
|
return link.startsWith(prefix)
|
|
&& !link.mid(prefix.size()).trimmed().isEmpty();
|
|
};
|
|
if (hasPayload(u"mailto:"_q)
|
|
|| hasPayload(u"tel:"_q)
|
|
|| (link.startsWith(u"#"_q)
|
|
&& !Markdown::NormalizeFragmentId(link).isEmpty())) {
|
|
return link;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
void EditMathBox(
|
|
not_null<Ui::GenericBox*> box,
|
|
QString startSource,
|
|
bool editingExisting,
|
|
std::optional<bool> separateLine,
|
|
Fn<void(QString, bool)> callback,
|
|
Fn<void(bool)> setExternalInteractionActive,
|
|
Fn<void()> restoreFocus) {
|
|
Expects(callback != nullptr);
|
|
Expects(setExternalInteractionActive != nullptr);
|
|
|
|
setExternalInteractionActive(true);
|
|
box->boxClosing() | rpl::on_next([=] {
|
|
setExternalInteractionActive(false);
|
|
if (restoreFocus) {
|
|
restoreFocus();
|
|
}
|
|
}, box->lifetime());
|
|
|
|
const auto source = box->addRow(
|
|
object_ptr<Ui::InputField>(
|
|
box,
|
|
st::defaultInputField,
|
|
Ui::InputField::Mode::MultiLine,
|
|
tr::lng_formatting_math_source_placeholder(),
|
|
startSource),
|
|
st::markdownLinkFieldPadding);
|
|
source->setSubmitSettings(Ui::InputField::SubmitSettings::Enter);
|
|
source->setMinHeight(source->st().heightMin);
|
|
const auto separateLineField = separateLine
|
|
? box->addRow(
|
|
object_ptr<Ui::Checkbox>(
|
|
box,
|
|
tr::lng_formatting_math_separate_line(tr::now),
|
|
*separateLine,
|
|
st::defaultBoxCheckbox),
|
|
st::markdownMathCheckboxMargin)
|
|
: nullptr;
|
|
auto checkboxHeight = separateLineField
|
|
? separateLineField->heightValue()
|
|
: rpl::single(0);
|
|
rpl::combine(
|
|
source->topValue(),
|
|
box->getDelegate()->contentHeightMaxValue(),
|
|
std::move(checkboxHeight)
|
|
) | rpl::on_next([=](int top, int contentHeight, int checkboxHeight) {
|
|
const auto checkboxBlock = separateLineField
|
|
? (checkboxHeight
|
|
+ st::markdownMathCheckboxMargin.top()
|
|
+ st::markdownMathCheckboxMargin.bottom())
|
|
: 0;
|
|
source->setMaxHeight(std::max(
|
|
source->st().heightMin,
|
|
std::min(
|
|
st::markdownMathFieldMaxHeight,
|
|
contentHeight
|
|
- top
|
|
- st::markdownLinkFieldPadding.bottom()
|
|
- checkboxBlock)));
|
|
}, source->lifetime());
|
|
|
|
const auto submit = [=] {
|
|
auto sourceText = source->getLastText().trimmed();
|
|
sourceText.replace('\r', ' ');
|
|
sourceText.replace('\n', ' ');
|
|
if (sourceText.isEmpty()) {
|
|
source->showError();
|
|
return;
|
|
}
|
|
const auto weak = base::make_weak(box);
|
|
callback(
|
|
sourceText,
|
|
separateLineField && separateLineField->checked());
|
|
if (weak) {
|
|
box->closeBox();
|
|
}
|
|
};
|
|
source->submits(
|
|
) | rpl::on_next(submit, source->lifetime());
|
|
|
|
box->setTitle(editingExisting
|
|
? tr::lng_formatting_math_edit_title()
|
|
: tr::lng_formatting_math_create_title());
|
|
box->addButton(tr::lng_settings_save(), submit);
|
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
|
|
|
box->verticalLayout()->resizeToWidth(st::boxWidth);
|
|
box->verticalLayout()->moveToLeft(0, 0);
|
|
box->setWidth(st::boxWidth);
|
|
|
|
box->setFocusCallback([=] {
|
|
if (!startSource.isEmpty()) {
|
|
source->selectAll();
|
|
}
|
|
source->setFocusFast();
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] bool ImeEventProducesInput(
|
|
const QInputMethodEvent &e,
|
|
const QTextCursor &cursor) {
|
|
return !e.commitString().isEmpty()
|
|
|| e.preeditString() != cursor.block().layout()->preeditAreaText()
|
|
|| e.replacementLength() > 0;
|
|
}
|
|
|
|
using PreparedEditBlockContainerPath
|
|
= Markdown::PreparedEditBlockContainerPath;
|
|
using PreparedEditBlockContainerStep
|
|
= Markdown::PreparedEditBlockContainerStep;
|
|
using PreparedEditBlockContainerKind
|
|
= Markdown::PreparedEditBlockContainerKind;
|
|
using PreparedEditBlockPath = Markdown::PreparedEditBlockPath;
|
|
using PreparedEditBlockSource = Markdown::PreparedEditBlockSource;
|
|
using PreparedEditHit = Markdown::PreparedEditHit;
|
|
using PreparedEditHitKind = Markdown::PreparedEditHitKind;
|
|
using PreparedEditLeafKind = Markdown::PreparedEditLeafKind;
|
|
using PreparedEditLeafSource = Markdown::PreparedEditLeafSource;
|
|
using PreparedEditListItemSource = Markdown::PreparedEditListItemSource;
|
|
using PreparedEditSelection = Markdown::PreparedEditSelection;
|
|
using PreparedEditSelectionKind = Markdown::PreparedEditSelectionKind;
|
|
using PreparedEditTableCellRange = Markdown::PreparedEditTableCellRange;
|
|
using PreparedEditTableCellSource = Markdown::PreparedEditTableCellSource;
|
|
using PreparedEditTableRowSource = Markdown::PreparedEditTableRowSource;
|
|
using ApplyResult = State::ApplyResult;
|
|
using PreparedMutationKind = State::PreparedMutationKind;
|
|
|
|
[[nodiscard]] bool SnapshotEquals(
|
|
const State::Snapshot &a,
|
|
const State::Snapshot &b) {
|
|
return RichPagesEqual(a.richPage, b.richPage)
|
|
&& (a.activeLeaf == b.activeLeaf)
|
|
&& (a.temporaryDownParagraph == b.temporaryDownParagraph);
|
|
}
|
|
|
|
struct NormalizedIntegerRange {
|
|
int from = -1;
|
|
int till = -1;
|
|
|
|
[[nodiscard]] bool empty() const {
|
|
return (from < 0) || (till <= from);
|
|
}
|
|
};
|
|
|
|
[[nodiscard]] NormalizedIntegerRange NormalizeIntegerRange(int a, int b) {
|
|
if (a < 0 || b < 0) {
|
|
return {};
|
|
}
|
|
return {
|
|
.from = std::min(a, b),
|
|
.till = std::max(a, b) + 1,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditSelection BlockSelectionFromIndexes(
|
|
PreparedEditBlockContainerPath container,
|
|
int first,
|
|
int second) {
|
|
const auto range = NormalizeIntegerRange(first, second);
|
|
if (range.empty()) {
|
|
return {};
|
|
}
|
|
return {
|
|
.kind = PreparedEditSelectionKind::Blocks,
|
|
.blocks = {
|
|
.container = std::move(container),
|
|
.from = range.from,
|
|
.till = range.till,
|
|
},
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] bool SingleRootPlainTextFieldSelectAllPassthrough(
|
|
const RichPage &page,
|
|
const std::optional<StateLeafPath> &leaf,
|
|
bool fieldHidden) {
|
|
if (fieldHidden
|
|
|| !leaf
|
|
|| (page.blocks.size() != 1)
|
|
|| (leaf->kind != StateLeafKind::BlockText)
|
|
|| !leaf->block.container.steps.empty()
|
|
|| (leaf->block.index != 0)) {
|
|
return false;
|
|
}
|
|
switch (page.blocks[0].kind) {
|
|
case RichPage::BlockKind::Paragraph:
|
|
case RichPage::BlockKind::Heading:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] int CompareIntegers(int a, int b) {
|
|
return (a < b) ? -1 : (a > b) ? 1 : 0;
|
|
}
|
|
|
|
[[nodiscard]] int ComparePreparedEditBlockContainerSteps(
|
|
const PreparedEditBlockContainerStep &a,
|
|
const PreparedEditBlockContainerStep &b) {
|
|
if (const auto result = CompareIntegers(
|
|
static_cast<int>(a.kind),
|
|
static_cast<int>(b.kind))) {
|
|
return result;
|
|
} else if (const auto result = CompareIntegers(
|
|
a.blockIndex,
|
|
b.blockIndex)) {
|
|
return result;
|
|
}
|
|
return CompareIntegers(a.listItemIndex, b.listItemIndex);
|
|
}
|
|
|
|
[[nodiscard]] int ComparePreparedEditBlockContainerPaths(
|
|
const PreparedEditBlockContainerPath &a,
|
|
const PreparedEditBlockContainerPath &b) {
|
|
const auto common = std::min(a.steps.size(), b.steps.size());
|
|
for (auto i = size_t(); i != common; ++i) {
|
|
if (const auto result = ComparePreparedEditBlockContainerSteps(
|
|
a.steps[i],
|
|
b.steps[i])) {
|
|
return result;
|
|
}
|
|
}
|
|
return CompareIntegers(int(a.steps.size()), int(b.steps.size()));
|
|
}
|
|
|
|
[[nodiscard]] int ComparePreparedEditBlockPaths(
|
|
const PreparedEditBlockPath &a,
|
|
const PreparedEditBlockPath &b) {
|
|
if (const auto result = ComparePreparedEditBlockContainerPaths(
|
|
a.container,
|
|
b.container)) {
|
|
return result;
|
|
}
|
|
return CompareIntegers(a.index, b.index);
|
|
}
|
|
|
|
[[nodiscard]] bool SamePreparedEditBlockPath(
|
|
const PreparedEditBlockPath &a,
|
|
const PreparedEditBlockPath &b) {
|
|
return (ComparePreparedEditBlockPaths(a, b) == 0);
|
|
}
|
|
|
|
[[nodiscard]] bool ValidPreparedEditBlockPath(
|
|
const PreparedEditBlockPath &path) {
|
|
return (path.index >= 0);
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditBlockSource PreparedEditBlockSourceFromPath(
|
|
PreparedEditBlockPath path) {
|
|
return { .path = std::move(path) };
|
|
}
|
|
|
|
enum class StructuralOwnerKind {
|
|
None,
|
|
Block,
|
|
ListItem,
|
|
TableRow,
|
|
TableCell,
|
|
};
|
|
|
|
struct StructuralOwner {
|
|
StructuralOwnerKind kind = StructuralOwnerKind::None;
|
|
std::optional<PreparedEditBlockSource> block;
|
|
std::optional<PreparedEditListItemSource> listItem;
|
|
std::optional<PreparedEditTableRowSource> tableRow;
|
|
std::optional<PreparedEditTableCellSource> tableCell;
|
|
|
|
[[nodiscard]] bool valid() const {
|
|
return (kind != StructuralOwnerKind::None);
|
|
}
|
|
};
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromBlock(
|
|
const PreparedEditBlockSource &source) {
|
|
if (!ValidPreparedEditBlockPath(source.path)) {
|
|
return {};
|
|
}
|
|
return {
|
|
.kind = StructuralOwnerKind::Block,
|
|
.block = source,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromListItem(
|
|
const PreparedEditListItemSource &source) {
|
|
if (!ValidPreparedEditBlockPath(source.block)
|
|
|| source.listItemIndex < 0) {
|
|
return {};
|
|
}
|
|
return {
|
|
.kind = StructuralOwnerKind::ListItem,
|
|
.block = PreparedEditBlockSourceFromPath(source.block),
|
|
.listItem = source,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromTableRow(
|
|
const PreparedEditTableRowSource &source) {
|
|
if (!ValidPreparedEditBlockPath(source.block)
|
|
|| source.tableRowIndex < 0) {
|
|
return {};
|
|
}
|
|
return {
|
|
.kind = StructuralOwnerKind::TableRow,
|
|
.block = PreparedEditBlockSourceFromPath(source.block),
|
|
.tableRow = source,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditTableRowSource PreparedEditTableRowFromCell(
|
|
const PreparedEditTableCellSource &source) {
|
|
return {
|
|
.block = source.block,
|
|
.tableRowIndex = source.tableRowIndex,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromTableCell(
|
|
const PreparedEditTableCellSource &source) {
|
|
if (!ValidPreparedEditBlockPath(source.block)
|
|
|| source.tableRowIndex < 0
|
|
|| source.tableCellIndex < 0
|
|
|| source.column < 0
|
|
|| source.colspan <= 0
|
|
|| source.rowspan <= 0) {
|
|
return {};
|
|
}
|
|
return {
|
|
.kind = StructuralOwnerKind::TableCell,
|
|
.block = PreparedEditBlockSourceFromPath(source.block),
|
|
.tableRow = PreparedEditTableRowFromCell(source),
|
|
.tableCell = source,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromLeaf(
|
|
const PreparedEditLeafSource &source) {
|
|
if (!ValidPreparedEditBlockPath(source.block)) {
|
|
return {};
|
|
}
|
|
switch (source.kind) {
|
|
case PreparedEditLeafKind::ListItemText:
|
|
return StructuralOwnerFromListItem({
|
|
.block = source.block,
|
|
.listItemIndex = source.listItemIndex,
|
|
});
|
|
case PreparedEditLeafKind::TableCellText:
|
|
return {};
|
|
case PreparedEditLeafKind::BlockText:
|
|
case PreparedEditLeafKind::BlockCaption:
|
|
case PreparedEditLeafKind::MathFormula:
|
|
return StructuralOwnerFromBlock(
|
|
PreparedEditBlockSourceFromPath(source.block));
|
|
}
|
|
return {};
|
|
}
|
|
|
|
[[nodiscard]] StructuralOwner StructuralOwnerFromHit(
|
|
const PreparedEditHit &hit) {
|
|
if (!hit.valid()) {
|
|
return {};
|
|
}
|
|
switch (hit.kind) {
|
|
case PreparedEditHitKind::Block:
|
|
if (hit.block) {
|
|
return StructuralOwnerFromBlock(*hit.block);
|
|
}
|
|
break;
|
|
case PreparedEditHitKind::ListItem:
|
|
if (hit.listItem) {
|
|
return StructuralOwnerFromListItem(*hit.listItem);
|
|
}
|
|
break;
|
|
case PreparedEditHitKind::TableRow:
|
|
if (hit.tableRow) {
|
|
return StructuralOwnerFromTableRow(*hit.tableRow);
|
|
}
|
|
break;
|
|
case PreparedEditHitKind::TableCell:
|
|
if (hit.tableCell) {
|
|
return StructuralOwnerFromTableCell(*hit.tableCell);
|
|
}
|
|
break;
|
|
case PreparedEditHitKind::Leaf:
|
|
if (hit.leaf) {
|
|
return StructuralOwnerFromLeaf(*hit.leaf);
|
|
}
|
|
break;
|
|
case PreparedEditHitKind::None:
|
|
break;
|
|
}
|
|
return hit.leaf ? StructuralOwnerFromLeaf(*hit.leaf) : StructuralOwner();
|
|
}
|
|
|
|
[[nodiscard]] std::optional<PreparedEditTableCellSource> TableCellFromOwner(
|
|
const StructuralOwner &owner) {
|
|
return owner.tableCell;
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditTableCellRange TableRangeFromCell(
|
|
const PreparedEditTableCellSource &source) {
|
|
if (source.tableRowIndex < 0
|
|
|| source.column < 0
|
|
|| source.rowspan <= 0
|
|
|| source.colspan <= 0) {
|
|
return {};
|
|
}
|
|
return {
|
|
.block = source.block,
|
|
.rowFrom = source.tableRowIndex,
|
|
.rowTill = source.tableRowIndex + source.rowspan,
|
|
.columnFrom = source.column,
|
|
.columnTill = source.column + source.colspan,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] bool SameTableRangeBlock(
|
|
const PreparedEditTableCellRange &a,
|
|
const PreparedEditTableCellRange &b) {
|
|
return !a.empty()
|
|
&& !b.empty()
|
|
&& SamePreparedEditBlockPath(a.block, b.block);
|
|
}
|
|
|
|
[[nodiscard]] bool TableRangeContainsCell(
|
|
const PreparedEditTableCellRange &range,
|
|
const PreparedEditTableCellSource &source) {
|
|
const auto cell = TableRangeFromCell(source);
|
|
return SameTableRangeBlock(range, cell)
|
|
&& (range.rowFrom <= cell.rowFrom)
|
|
&& (range.rowTill >= cell.rowTill)
|
|
&& (range.columnFrom <= cell.columnFrom)
|
|
&& (range.columnTill >= cell.columnTill);
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditTableCellRange TableRangesUnion(
|
|
const PreparedEditTableCellRange &a,
|
|
const PreparedEditTableCellRange &b) {
|
|
if (!SameTableRangeBlock(a, b)) {
|
|
return {};
|
|
}
|
|
return {
|
|
.block = a.block,
|
|
.rowFrom = std::min(a.rowFrom, b.rowFrom),
|
|
.rowTill = std::max(a.rowTill, b.rowTill),
|
|
.columnFrom = std::min(a.columnFrom, b.columnFrom),
|
|
.columnTill = std::max(a.columnTill, b.columnTill),
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] std::optional<PreparedEditTableRowSource> TableRowFromOwner(
|
|
const StructuralOwner &owner) {
|
|
return owner.tableRow;
|
|
}
|
|
|
|
[[nodiscard]] std::optional<PreparedEditListItemSource> ListItemFromOwner(
|
|
const StructuralOwner &owner) {
|
|
return owner.listItem;
|
|
}
|
|
|
|
[[nodiscard]] bool IsBlockOwner(const StructuralOwner &owner) {
|
|
return (owner.kind == StructuralOwnerKind::Block);
|
|
}
|
|
|
|
[[nodiscard]] std::optional<PreparedEditBlockPath> BlockPathFromOwner(
|
|
const StructuralOwner &owner) {
|
|
if (owner.kind == StructuralOwnerKind::Block && owner.block) {
|
|
return owner.block->path;
|
|
} else if (owner.kind == StructuralOwnerKind::ListItem
|
|
&& owner.listItem) {
|
|
return owner.listItem->block;
|
|
} else if (owner.kind == StructuralOwnerKind::TableRow
|
|
&& owner.tableRow) {
|
|
return owner.tableRow->block;
|
|
} else if (owner.kind == StructuralOwnerKind::TableCell
|
|
&& owner.tableCell) {
|
|
return owner.tableCell->block;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
struct LiftedPreparedEditBlocks {
|
|
PreparedEditBlockContainerPath container;
|
|
int first = -1;
|
|
int second = -1;
|
|
};
|
|
|
|
[[nodiscard]] PreparedEditBlockContainerPath PreparedEditBlockContainerPrefix(
|
|
const PreparedEditBlockContainerPath &path,
|
|
int count) {
|
|
auto result = PreparedEditBlockContainerPath();
|
|
const auto till = std::clamp(count, 0, int(path.steps.size()));
|
|
result.steps.insert(
|
|
result.steps.end(),
|
|
path.steps.begin(),
|
|
path.steps.begin() + till);
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] int CommonPreparedEditBlockContainerSize(
|
|
const PreparedEditBlockContainerPath &a,
|
|
const PreparedEditBlockContainerPath &b) {
|
|
const auto common = std::min(a.steps.size(), b.steps.size());
|
|
for (auto i = size_t(); i != common; ++i) {
|
|
if (ComparePreparedEditBlockContainerSteps(
|
|
a.steps[i],
|
|
b.steps[i]) != 0) {
|
|
return int(i);
|
|
}
|
|
}
|
|
return int(common);
|
|
}
|
|
|
|
[[nodiscard]] int LiftedPreparedEditBlockIndex(
|
|
const PreparedEditBlockPath &path,
|
|
int commonContainerSize) {
|
|
if (commonContainerSize == int(path.container.steps.size())) {
|
|
return path.index;
|
|
} else if (commonContainerSize >= 0
|
|
&& commonContainerSize < int(path.container.steps.size())) {
|
|
return path.container.steps[commonContainerSize].blockIndex;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
[[nodiscard]] std::optional<LiftedPreparedEditBlocks>
|
|
LiftPreparedEditBlocksToCommonContainer(
|
|
const PreparedEditBlockPath &a,
|
|
const PreparedEditBlockPath &b) {
|
|
if (!ValidPreparedEditBlockPath(a) || !ValidPreparedEditBlockPath(b)) {
|
|
return std::nullopt;
|
|
}
|
|
const auto common = CommonPreparedEditBlockContainerSize(
|
|
a.container,
|
|
b.container);
|
|
auto result = LiftedPreparedEditBlocks{
|
|
.container = PreparedEditBlockContainerPrefix(a.container, common),
|
|
.first = LiftedPreparedEditBlockIndex(a, common),
|
|
.second = LiftedPreparedEditBlockIndex(b, common),
|
|
};
|
|
if (result.first < 0 || result.second < 0) {
|
|
return std::nullopt;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditSelection LiftedBlockSelection(
|
|
const PreparedEditBlockPath &anchor,
|
|
const PreparedEditBlockPath &focus) {
|
|
const auto lifted = LiftPreparedEditBlocksToCommonContainer(anchor, focus);
|
|
if (!lifted) {
|
|
return {};
|
|
}
|
|
return BlockSelectionFromIndexes(
|
|
lifted->container,
|
|
lifted->first,
|
|
lifted->second);
|
|
}
|
|
|
|
[[nodiscard]] auto ListItemSourcesFromBlockPath(
|
|
const PreparedEditBlockPath &path)
|
|
-> std::vector<PreparedEditListItemSource> {
|
|
auto result = std::vector<PreparedEditListItemSource>();
|
|
for (auto i = int(path.container.steps.size()); i != 0; --i) {
|
|
const auto stepIndex = i - 1;
|
|
const auto &step = path.container.steps[stepIndex];
|
|
if (step.kind != PreparedEditBlockContainerKind::ListItemChildren
|
|
|| step.blockIndex < 0
|
|
|| step.listItemIndex < 0) {
|
|
continue;
|
|
}
|
|
result.push_back({
|
|
.block = {
|
|
.container = PreparedEditBlockContainerPrefix(
|
|
path.container,
|
|
stepIndex),
|
|
.index = step.blockIndex,
|
|
},
|
|
.listItemIndex = step.listItemIndex,
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] auto ListItemSourcesFromOwner(
|
|
const StructuralOwner &owner,
|
|
const std::optional<PreparedEditBlockPath> &block)
|
|
-> std::vector<PreparedEditListItemSource> {
|
|
auto result = std::vector<PreparedEditListItemSource>();
|
|
if (const auto listItem = ListItemFromOwner(owner)) {
|
|
result.push_back(*listItem);
|
|
}
|
|
if (!block) {
|
|
return result;
|
|
}
|
|
for (const auto &source : ListItemSourcesFromBlockPath(*block)) {
|
|
if (std::find(result.begin(), result.end(), source) == result.end()) {
|
|
result.push_back(source);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] PreparedEditSelection ListItemSelectionFromSources(
|
|
const std::vector<PreparedEditListItemSource> &anchorSources,
|
|
const std::vector<PreparedEditListItemSource> &focusSources) {
|
|
for (const auto &anchorListItem : anchorSources) {
|
|
for (const auto &focusListItem : focusSources) {
|
|
if (!SamePreparedEditBlockPath(
|
|
anchorListItem.block,
|
|
focusListItem.block)) {
|
|
continue;
|
|
}
|
|
const auto range = NormalizeIntegerRange(
|
|
anchorListItem.listItemIndex,
|
|
focusListItem.listItemIndex);
|
|
if (!range.empty()) {
|
|
return {
|
|
.kind = PreparedEditSelectionKind::ListItems,
|
|
.listItems = {
|
|
.block = anchorListItem.block,
|
|
.from = range.from,
|
|
.till = range.till,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
[[nodiscard]] bool IsMultiListItemSelection(
|
|
const PreparedEditSelection &selection) {
|
|
return !selection.empty()
|
|
&& (selection.kind == PreparedEditSelectionKind::ListItems)
|
|
&& (selection.listItems.till > selection.listItems.from + 1);
|
|
}
|
|
|
|
[[nodiscard]] int FieldNaturalHeight(not_null<Ui::InputField*> field) {
|
|
const auto margins = field->fullTextMargins();
|
|
return std::max(
|
|
int(std::ceil(field->document()->size().height()))
|
|
+ margins.top()
|
|
+ margins.bottom(),
|
|
1);
|
|
}
|
|
|
|
[[nodiscard]] QPoint LocalPosition(QWheelEvent *e) {
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
return e->position().toPoint();
|
|
#else // Qt >= 6.0
|
|
return e->pos();
|
|
#endif // Qt >= 6.0
|
|
}
|
|
|
|
[[nodiscard]] QPoint GlobalPosition(QWheelEvent *e) {
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
return e->globalPosition().toPoint();
|
|
#else // Qt >= 6.0
|
|
return e->globalPos();
|
|
#endif // Qt >= 6.0
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Widget::Widget(
|
|
QWidget *parent,
|
|
WidgetServices services,
|
|
not_null<PeerData*> peer,
|
|
std::shared_ptr<State> state,
|
|
Fn<void(RichMessageLimitError)> showLimitToast)
|
|
: Ui::RpWidget(parent)
|
|
, _session(services.session)
|
|
, _show(std::move(services.show))
|
|
, _outer(services.outer)
|
|
, _customEmojiPaused(std::move(services.customEmojiPaused))
|
|
, _applyPreparedMedia(std::move(services.applyPreparedMedia))
|
|
, _peer(peer)
|
|
, _state(std::move(state))
|
|
, _showLimitToast(std::move(showLimitToast))
|
|
, _articleStyle(std::make_shared<style::Markdown>(
|
|
CreateEditorMarkdownStyle()))
|
|
, _article(std::make_shared<Markdown::MarkdownArticle>(*_articleStyle))
|
|
, _theme(CreateStandaloneChatTheme())
|
|
, _style(std::make_unique<Ui::ChatStyle>(style::main_palette::get())) {
|
|
_style->apply(_theme.get());
|
|
_highlightColors = HighlightColors(_style.get());
|
|
|
|
setMouseTracking(true);
|
|
setAttribute(Qt::WA_AcceptTouchEvents);
|
|
setFocusPolicy(Qt::StrongFocus);
|
|
setAttribute(Qt::WA_InputMethodEnabled);
|
|
|
|
std::move(services.imeCompositionStarts) | rpl::filter([=] {
|
|
return redirectImeToField();
|
|
}) | rpl::on_next([=] {
|
|
if (prepareFieldForInput()) {
|
|
_field->setFocusFast();
|
|
}
|
|
}, lifetime());
|
|
|
|
Spellchecker::HighlightReady(
|
|
) | rpl::on_next([=](Spellchecker::HighlightProcessId processId) {
|
|
if (_article && _article->highlightProcessDone(processId)) {
|
|
update();
|
|
}
|
|
}, _highlightReadyLifetime);
|
|
|
|
const auto weak = QPointer<Widget>(this);
|
|
_article->setTextRepaintCallbacks(
|
|
[=] {
|
|
if (weak) {
|
|
weak->update();
|
|
}
|
|
},
|
|
[=](QRect rect) {
|
|
if (!weak) {
|
|
return;
|
|
} else if (rect.isEmpty()) {
|
|
weak->update();
|
|
} else {
|
|
weak->update(rect.translated(weak->articleTopLeft()));
|
|
}
|
|
});
|
|
_article->setMediaBlockHost(this);
|
|
|
|
const auto &fieldStyle = inlineFieldStyleFor(
|
|
Markdown::MarkdownArticleTextLeafStyle());
|
|
_activeFieldStyleKey = fieldStyle.key;
|
|
_field = base::make_unique_q<Ui::InputField>(
|
|
this,
|
|
*fieldStyle.style,
|
|
Ui::InputField::Mode::MultiLine,
|
|
rpl::single(QString()));
|
|
setupInlineField();
|
|
refreshPreparedContent();
|
|
_history.push_back(captureHistoryEntry());
|
|
_historyIndex = 0;
|
|
}
|
|
|
|
Widget::~Widget() {
|
|
if (_article) {
|
|
_article->setTextRepaintCallbacks(nullptr, nullptr);
|
|
_article->setMediaBlockHost(nullptr);
|
|
}
|
|
}
|
|
|
|
void Widget::activateInitialNode() {
|
|
const auto ordinal = (_activeOrdinal >= 0)
|
|
? _activeOrdinal
|
|
: _state->activeTextOrdinal();
|
|
if (ordinal < 0) {
|
|
const auto first = _article->firstEditableSegmentIndex();
|
|
const auto fallback = editableOrdinalForSegment(first);
|
|
if (fallback < 0) {
|
|
return;
|
|
}
|
|
activateTextOrdinal(fallback, 0);
|
|
return;
|
|
}
|
|
activateTextOrdinal(ordinal, 0);
|
|
}
|
|
|
|
void Widget::activateSegment(int segmentIndex, int cursorOffset) {
|
|
const auto ordinal = editableOrdinalForSegment(segmentIndex);
|
|
if (ordinal < 0) {
|
|
return;
|
|
}
|
|
activateTextOrdinal(ordinal, cursorOffset);
|
|
}
|
|
|
|
bool Widget::prepareFieldForInput() {
|
|
if (hasStructuralSelection()) {
|
|
if (const auto target = removeCurrentStructuralSelection(true)) {
|
|
activateTextOrdinal(*target, 0);
|
|
} else {
|
|
activateInitialNode();
|
|
}
|
|
} else if (_field->isHidden()) {
|
|
activateInitialNode();
|
|
}
|
|
return !_field->isHidden();
|
|
}
|
|
|
|
bool Widget::replayKeyIntoField(QKeyEvent *e) {
|
|
if (!RedirectTextToField(e->text()) || !prepareFieldForInput()) {
|
|
return false;
|
|
}
|
|
_field->setFocusFast();
|
|
QCoreApplication::sendEvent(_field->rawTextEdit(), e);
|
|
return true;
|
|
}
|
|
|
|
bool Widget::replayImeIntoField(QInputMethodEvent *e) {
|
|
const auto cursor = _field->rawTextEdit()->textCursor();
|
|
if (!ImeEventProducesInput(*e, cursor) || !prepareFieldForInput()) {
|
|
return false;
|
|
}
|
|
_field->setFocusFast();
|
|
QCoreApplication::sendEvent(_field->rawTextEdit(), e);
|
|
return true;
|
|
}
|
|
|
|
ApplyResult Widget::commitInlineField() {
|
|
const auto result = applyFieldTextToState();
|
|
if (result != ApplyResult::Failed) {
|
|
return result;
|
|
}
|
|
revertInlineFieldToState();
|
|
showLastLimitToast();
|
|
return result;
|
|
}
|
|
|
|
ApplyResult Widget::commitInlineFieldForClose() {
|
|
const auto result = recordMutationTransaction([&] {
|
|
return applyFieldTextToState();
|
|
});
|
|
if (result == ApplyResult::Failed) {
|
|
showLastLimitToast();
|
|
} else if (result == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void Widget::hideInlineFieldAndRefresh() {
|
|
if (_field->isHidden()) {
|
|
return;
|
|
}
|
|
beginArticleRelayoutDeferral();
|
|
const auto relayoutGuard = gsl::finally([&] {
|
|
endArticleRelayoutDeferral();
|
|
});
|
|
const auto committed = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
return committed;
|
|
});
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
|
|
void Widget::acceptInlineField() {
|
|
hideInlineFieldAndRefresh();
|
|
}
|
|
|
|
void Widget::refreshPreparedContent() {
|
|
setDocument(_state->prepared());
|
|
relayoutCurrentContent();
|
|
update();
|
|
}
|
|
|
|
void Widget::refreshPreparedLeafAtActiveSource() {
|
|
if (const auto source = _state->activePreparedLeafSource()) {
|
|
refreshPreparedLeafAtSource(*source);
|
|
} else {
|
|
refreshPreparedContent();
|
|
}
|
|
}
|
|
|
|
void Widget::refreshPreparedLeafAtSource(
|
|
const Markdown::PreparedEditLeafSource &source) {
|
|
_article->updatePreparedLeaf(source, _state->prepared());
|
|
relayoutCurrentContent();
|
|
}
|
|
|
|
void Widget::applyExternalRichPageMutation(Fn<bool(RichPage&)> mutation) {
|
|
if (!mutation) {
|
|
return;
|
|
}
|
|
auto live = captureHistoryEntry();
|
|
for (auto &entry : _history) {
|
|
if (!mutation(entry.snapshot.richPage)) {
|
|
return;
|
|
}
|
|
}
|
|
if (!mutation(live.snapshot.richPage)) {
|
|
return;
|
|
}
|
|
const auto wasPreservingExternalFieldRestore
|
|
= PreservingExternalFieldRestore;
|
|
PreservingExternalFieldRestore = this;
|
|
const auto preserveExternalFieldRestore = gsl::finally([&] {
|
|
PreservingExternalFieldRestore = wasPreservingExternalFieldRestore;
|
|
});
|
|
restoreHistoryEntry(live);
|
|
_fieldUndoAvailable = !_field->isHidden()
|
|
? _field->isUndoAvailable()
|
|
: false;
|
|
_fieldRedoAvailable = !_field->isHidden()
|
|
? _field->isRedoAvailable()
|
|
: false;
|
|
}
|
|
|
|
void Widget::beginArticleRelayoutDeferral() {
|
|
++_articleRelayoutDeferralDepth;
|
|
}
|
|
|
|
void Widget::endArticleRelayoutDeferral() {
|
|
if (_articleRelayoutDeferralDepth <= 0) {
|
|
return;
|
|
}
|
|
--_articleRelayoutDeferralDepth;
|
|
if (_articleRelayoutDeferralDepth > 0) {
|
|
return;
|
|
}
|
|
flushArticleRelayoutDeferral();
|
|
}
|
|
|
|
bool Widget::articleRelayoutDeferralActive() const {
|
|
return (_articleRelayoutDeferralDepth > 0);
|
|
}
|
|
|
|
void Widget::requestDeferredArticleRelayout() {
|
|
_articleRelayoutDeferred = true;
|
|
}
|
|
|
|
void Widget::requestDeferredInlineFieldGeometry() {
|
|
_inlineFieldGeometryDeferred = true;
|
|
}
|
|
|
|
void Widget::requestDeferredInlineFieldHeightOverride() {
|
|
_inlineFieldHeightOverrideDeferred = true;
|
|
}
|
|
|
|
void Widget::clearArticleEditableHeightOverride() {
|
|
if (!_article) {
|
|
return;
|
|
} else if (articleRelayoutDeferralActive()) {
|
|
_articleEditableHeightOverrideClearDeferred = true;
|
|
requestDeferredArticleRelayout();
|
|
return;
|
|
}
|
|
_article->clearEditableHeightOverride();
|
|
}
|
|
|
|
void Widget::flushArticleRelayoutDeferral() {
|
|
if (articleRelayoutDeferralActive()) {
|
|
return;
|
|
}
|
|
const auto clearHeightOverride
|
|
= _articleEditableHeightOverrideClearDeferred;
|
|
const auto relayout = _articleRelayoutDeferred || clearHeightOverride;
|
|
const auto geometry = _inlineFieldGeometryDeferred;
|
|
const auto heightOverride = _inlineFieldHeightOverrideDeferred;
|
|
_articleEditableHeightOverrideClearDeferred = false;
|
|
_articleRelayoutDeferred = false;
|
|
_inlineFieldGeometryDeferred = false;
|
|
_inlineFieldHeightOverrideDeferred = false;
|
|
if (!relayout && !geometry && !heightOverride) {
|
|
return;
|
|
}
|
|
if (clearHeightOverride && _article) {
|
|
_article->clearEditableHeightOverride();
|
|
}
|
|
if (relayout) {
|
|
relayoutCurrentContent();
|
|
}
|
|
if (geometry) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
if (heightOverride) {
|
|
updateInlineFieldHeightOverride();
|
|
}
|
|
syncArticleVisibleTopBottom();
|
|
}
|
|
|
|
void Widget::beginInlineFieldRevealSuppression() {
|
|
++_inlineFieldRevealSuppressionDepth;
|
|
}
|
|
|
|
void Widget::endInlineFieldRevealSuppression() {
|
|
if (_inlineFieldRevealSuppressionDepth > 0) {
|
|
--_inlineFieldRevealSuppressionDepth;
|
|
}
|
|
}
|
|
|
|
bool Widget::inlineFieldRevealSuppressed() const {
|
|
return (_inlineFieldRevealSuppressionDepth > 0);
|
|
}
|
|
|
|
void Widget::resizeCurrentContentToWidth(int width) {
|
|
if (articleRelayoutDeferralActive()) {
|
|
requestDeferredArticleRelayout();
|
|
return;
|
|
}
|
|
if (width > 0) {
|
|
resizeToWidth(width);
|
|
} else {
|
|
update();
|
|
}
|
|
}
|
|
|
|
void Widget::relayoutCurrentContent() {
|
|
const auto width = std::max(
|
|
widthNoMargins(),
|
|
parentWidget() ? parentWidget()->width() : 0);
|
|
resizeCurrentContentToWidth(width);
|
|
}
|
|
|
|
void Widget::syncInlineFieldGeometry() {
|
|
syncInlineFieldGeometry(widthNoMargins());
|
|
}
|
|
|
|
void Widget::insertBlock(State::InsertAction action) {
|
|
recordMutationTransaction([&] {
|
|
const auto context = activeTextInsertContext();
|
|
const auto restoreField = context.has_value();
|
|
const auto restoreLeaf = restoreField
|
|
? _fieldLeaf
|
|
: std::optional<State::LeafPath>();
|
|
const auto restoreStyleKey = restoreField
|
|
? _activeFieldStyleKey
|
|
: std::optional<InlineFieldStyleKey>();
|
|
const auto restoreMode = _fieldMode;
|
|
const auto restoreSelection = restoreField
|
|
? captureHistoryViewState().leafSelection
|
|
: std::optional<HistoryLeafSelection>();
|
|
auto committed = ApplyResult::Unchanged;
|
|
if (!context && !_field->isHidden()) {
|
|
committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
}
|
|
const auto hadStructuralSelection = hasStructuralSelection();
|
|
if (hadStructuralSelection || restoreField) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession(restoreField);
|
|
}
|
|
auto restore = restoreField;
|
|
const auto restoreInlineField = gsl::finally([&] {
|
|
if (!restore) {
|
|
return;
|
|
}
|
|
if (restoreLeaf && restoreStyleKey) {
|
|
if (auto revived = reviveRetainedLeafField(
|
|
_historyIndex,
|
|
*restoreLeaf,
|
|
restoreMode,
|
|
*restoreStyleKey)) {
|
|
_field = std::move(revived);
|
|
_activeFieldStyleKey = restoreStyleKey;
|
|
_fieldMode = restoreMode;
|
|
_fieldLeaf = *restoreLeaf;
|
|
refreshInlineFieldPlaceholder();
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
}
|
|
if (!_fieldLeaf && restoreSelection) {
|
|
const auto ordinal = _state->textOrdinalForLeafPath(
|
|
restoreSelection->leaf);
|
|
if (ordinal >= 0) {
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
restoreSelection->anchorOffset,
|
|
restoreSelection->cursorOffset);
|
|
return;
|
|
}
|
|
}
|
|
_field->show();
|
|
syncInlineFieldGeometry();
|
|
updateInlineFieldHeightOverride();
|
|
syncArticleVisibleTopBottom();
|
|
revealActiveInlineField();
|
|
_field->raise();
|
|
_field->setFocusFast();
|
|
notifyToolbarStateChanged();
|
|
});
|
|
const auto applied = hadStructuralSelection
|
|
? _state->replaceStructuralSelectionWithBlock(
|
|
_structuralSelection,
|
|
action,
|
|
context)
|
|
: _state->insertBlockAfterActive(action, context);
|
|
if (!applied) {
|
|
showLastLimitToast();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
restore = false;
|
|
if (hadStructuralSelection) {
|
|
clearSelection();
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(_state->activeTextOrdinal(), 0);
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
}
|
|
|
|
void Widget::insertPreparedBlock(RichPage::Block block) {
|
|
auto blocks = std::vector<RichPage::Block>();
|
|
blocks.push_back(std::move(block));
|
|
insertPreparedBlocks(std::move(blocks));
|
|
}
|
|
|
|
void Widget::insertPreparedBlocks(std::vector<RichPage::Block> blocks) {
|
|
insertPreparedBlocks(std::move(blocks), activeTextInsertContext());
|
|
}
|
|
|
|
void Widget::pastePreparedBlock(
|
|
RichPage::Block block,
|
|
PreparedMediaPasteTarget target) {
|
|
auto blocks = std::vector<RichPage::Block>();
|
|
blocks.push_back(std::move(block));
|
|
pastePreparedBlocks(std::move(blocks), std::move(target));
|
|
}
|
|
|
|
void Widget::pastePreparedBlocks(
|
|
std::vector<RichPage::Block> blocks,
|
|
PreparedMediaPasteTarget target) {
|
|
auto activation = activatePreparedMediaPasteTarget(std::move(target));
|
|
if (activation.resolved) {
|
|
insertPreparedBlocks(
|
|
std::move(blocks),
|
|
std::move(activation.context),
|
|
false);
|
|
} else {
|
|
insertPreparedBlocks(
|
|
std::move(blocks),
|
|
activeTextInsertContext(),
|
|
false);
|
|
}
|
|
}
|
|
|
|
void Widget::insertPreparedBlocks(
|
|
std::vector<RichPage::Block> blocks,
|
|
std::optional<State::ActiveTextInsertContext> context,
|
|
bool useStructuralSelection) {
|
|
if (blocks.empty()) {
|
|
return;
|
|
}
|
|
recordMutationTransaction([&] {
|
|
const auto restoreField = context.has_value();
|
|
const auto restoreLeaf = restoreField
|
|
? _fieldLeaf
|
|
: std::optional<State::LeafPath>();
|
|
const auto restoreStyleKey = restoreField
|
|
? _activeFieldStyleKey
|
|
: std::optional<InlineFieldStyleKey>();
|
|
const auto restoreMode = _fieldMode;
|
|
const auto restoreSelection = restoreField
|
|
? captureHistoryViewState().leafSelection
|
|
: std::optional<HistoryLeafSelection>();
|
|
auto committed = ApplyResult::Unchanged;
|
|
if (!context && !_field->isHidden()) {
|
|
committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
}
|
|
const auto ignoredStructuralSelection = !useStructuralSelection
|
|
&& hasStructuralSelection();
|
|
const auto hadStructuralSelection = useStructuralSelection
|
|
&& hasStructuralSelection();
|
|
if (hadStructuralSelection || restoreField) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession(restoreField);
|
|
}
|
|
auto restore = restoreField;
|
|
const auto restoreInlineField = gsl::finally([&] {
|
|
if (!restore) {
|
|
return;
|
|
}
|
|
if (restoreLeaf && restoreStyleKey) {
|
|
if (auto revived = reviveRetainedLeafField(
|
|
_historyIndex,
|
|
*restoreLeaf,
|
|
restoreMode,
|
|
*restoreStyleKey)) {
|
|
_field = std::move(revived);
|
|
_activeFieldStyleKey = restoreStyleKey;
|
|
_fieldMode = restoreMode;
|
|
_fieldLeaf = *restoreLeaf;
|
|
refreshInlineFieldPlaceholder();
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
}
|
|
if (!_fieldLeaf && restoreSelection) {
|
|
const auto ordinal = _state->textOrdinalForLeafPath(
|
|
restoreSelection->leaf);
|
|
if (ordinal >= 0) {
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
restoreSelection->anchorOffset,
|
|
restoreSelection->cursorOffset);
|
|
return;
|
|
}
|
|
}
|
|
_field->show();
|
|
syncInlineFieldGeometry();
|
|
updateInlineFieldHeightOverride();
|
|
syncArticleVisibleTopBottom();
|
|
revealActiveInlineField();
|
|
_field->raise();
|
|
_field->setFocusFast();
|
|
notifyToolbarStateChanged();
|
|
});
|
|
const auto applied = hadStructuralSelection
|
|
? _state->replaceStructuralSelectionWithPreparedBlocks(
|
|
_structuralSelection,
|
|
std::move(blocks),
|
|
context)
|
|
: _state->insertPreparedBlocksAfterActive(
|
|
std::move(blocks),
|
|
context);
|
|
if (!applied) {
|
|
showLastLimitToast();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
restore = false;
|
|
if (hadStructuralSelection || ignoredStructuralSelection) {
|
|
clearSelection();
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(_state->activeTextOrdinal(), 0);
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
}
|
|
|
|
TextForMimeData Widget::currentSelectionTextForClipboard() const {
|
|
return _article
|
|
? _article->textForSelection(
|
|
_selection,
|
|
&_selectionEndpoints,
|
|
hasStructuralSelection() ? &_structuralSelection : nullptr)
|
|
: TextForMimeData();
|
|
}
|
|
|
|
void Widget::copyCurrentSelectionToClipboard() {
|
|
auto structured = std::optional<ClipboardData>();
|
|
if (hasStructuralSelection()) {
|
|
structured = _state->structuredClipboardDataForSelection(
|
|
_structuralSelection);
|
|
}
|
|
const auto text = currentSelectionTextForClipboard();
|
|
auto mimeData = structured
|
|
? MimeDataFromClipboardData(*structured)
|
|
: TextUtilities::MimeDataFromText(text);
|
|
if (!mimeData) {
|
|
return;
|
|
}
|
|
if (structured) {
|
|
if (const auto textMimeData = TextUtilities::MimeDataFromText(text)) {
|
|
for (const auto &format : textMimeData->formats()) {
|
|
mimeData->setData(format, textMimeData->data(format));
|
|
}
|
|
}
|
|
}
|
|
QApplication::clipboard()->setMimeData(mimeData.release());
|
|
}
|
|
|
|
void Widget::pasteStructuredClipboardData(const ClipboardData &data) {
|
|
const auto blocks = std::get_if<ClipboardBlockData>(&data);
|
|
const auto items = std::get_if<ClipboardListItemsData>(&data);
|
|
if (blocks) {
|
|
if (blocks->blocks.empty()) {
|
|
return;
|
|
}
|
|
} else if (!items || items->items.empty()) {
|
|
return;
|
|
}
|
|
recordMutationTransaction([&] {
|
|
const auto context = ClipboardPasteInsertContext(
|
|
activeTextInsertContext());
|
|
const auto restoreField = context.has_value();
|
|
const auto restoreLeaf = restoreField
|
|
? _fieldLeaf
|
|
: std::optional<State::LeafPath>();
|
|
const auto restoreStyleKey = restoreField
|
|
? _activeFieldStyleKey
|
|
: std::optional<InlineFieldStyleKey>();
|
|
const auto restoreMode = _fieldMode;
|
|
const auto restoreSelection = restoreField
|
|
? captureHistoryViewState().leafSelection
|
|
: std::optional<HistoryLeafSelection>();
|
|
auto committed = ApplyResult::Unchanged;
|
|
if (!context && !_field->isHidden()) {
|
|
committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
}
|
|
const auto hadStructuralSelection = hasStructuralSelection();
|
|
if (hadStructuralSelection || restoreField) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession(restoreField);
|
|
}
|
|
auto restore = restoreField;
|
|
const auto restoreInlineField = gsl::finally([&] {
|
|
if (!restore) {
|
|
return;
|
|
}
|
|
if (restoreLeaf && restoreStyleKey) {
|
|
if (auto revived = reviveRetainedLeafField(
|
|
_historyIndex,
|
|
*restoreLeaf,
|
|
restoreMode,
|
|
*restoreStyleKey)) {
|
|
_field = std::move(revived);
|
|
_activeFieldStyleKey = restoreStyleKey;
|
|
_fieldMode = restoreMode;
|
|
_fieldLeaf = *restoreLeaf;
|
|
refreshInlineFieldPlaceholder();
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
}
|
|
if (!_fieldLeaf && restoreSelection) {
|
|
const auto ordinal = _state->textOrdinalForLeafPath(
|
|
restoreSelection->leaf);
|
|
if (ordinal >= 0) {
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
restoreSelection->anchorOffset,
|
|
restoreSelection->cursorOffset);
|
|
return;
|
|
}
|
|
}
|
|
_field->show();
|
|
syncInlineFieldGeometry();
|
|
updateInlineFieldHeightOverride();
|
|
syncArticleVisibleTopBottom();
|
|
revealActiveInlineField();
|
|
_field->raise();
|
|
_field->setFocusFast();
|
|
notifyToolbarStateChanged();
|
|
});
|
|
const auto applied = hadStructuralSelection
|
|
? (blocks
|
|
? _state->replaceStructuralSelectionWithPreparedBlocks(
|
|
_structuralSelection,
|
|
blocks->blocks,
|
|
context)
|
|
: _state->replaceStructuralSelectionWithClipboardListItems(
|
|
_structuralSelection,
|
|
*items,
|
|
context))
|
|
: (blocks
|
|
? _state->insertPreparedBlocksAfterActive(
|
|
blocks->blocks,
|
|
context)
|
|
: _state->pasteClipboardListItemsAfterActive(
|
|
*items,
|
|
context));
|
|
if (!applied) {
|
|
showLastLimitToast();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
restore = false;
|
|
if (hadStructuralSelection) {
|
|
clearSelection();
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(_state->activeTextOrdinal(), 0);
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
}
|
|
|
|
bool Widget::handleClipboardKey(QKeyEvent *e) {
|
|
if (e == QKeySequence::Copy) {
|
|
if (_selection.empty() && !hasStructuralSelection()) {
|
|
return false;
|
|
}
|
|
copyCurrentSelectionToClipboard();
|
|
e->accept();
|
|
return true;
|
|
} else if (e == QKeySequence::Cut) {
|
|
if (!hasStructuralSelection()
|
|
|| !_state->canRemoveStructuralSelection(_structuralSelection)) {
|
|
return false;
|
|
}
|
|
copyCurrentSelectionToClipboard();
|
|
removeStructuralSelectionAndReposition(true);
|
|
e->accept();
|
|
return true;
|
|
} else if ((e == QKeySequence::Paste) && _field->isHidden()) {
|
|
const auto mimeData = QApplication::clipboard()->mimeData();
|
|
if (const auto data = ClipboardDataFromMimeData(mimeData)) {
|
|
pasteStructuredClipboardData(*data);
|
|
e->accept();
|
|
return true;
|
|
} else if (auto list = PreparedMediaFromClipboard(
|
|
not_null<const QMimeData*>(mimeData),
|
|
_session->premium())) {
|
|
if (_applyPreparedMedia) {
|
|
_applyPreparedMedia(
|
|
not_null<Widget*>(this),
|
|
std::move(*list),
|
|
preparedMediaPasteTarget());
|
|
e->accept();
|
|
return true;
|
|
}
|
|
} else if (prepareFieldForInput()) {
|
|
_field->setFocusFast();
|
|
QCoreApplication::sendEvent(_field->rawTextEdit(), e);
|
|
e->accept();
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Widget::handleFieldBlockInsertShortcut(QKeyEvent *e) {
|
|
if (_fieldMode != State::FieldMode::Rich || _field->isHidden()) {
|
|
return false;
|
|
}
|
|
const auto type = e->type();
|
|
if (type != QEvent::ShortcutOverride && type != QEvent::KeyPress) {
|
|
return false;
|
|
}
|
|
const auto blockquote = MatchesKeySequence(e, Ui::kBlockquoteSequence);
|
|
const auto monospace = MatchesKeySequence(e, Ui::kMonospaceSequence);
|
|
if (!blockquote && !monospace) {
|
|
return false;
|
|
}
|
|
if (type == QEvent::KeyPress) {
|
|
if (blockquote) {
|
|
insertBlockquote();
|
|
} else {
|
|
insertCodeBlock();
|
|
}
|
|
}
|
|
e->accept();
|
|
return true;
|
|
}
|
|
|
|
void Widget::truncateHistoryRedo() {
|
|
if ((_historyIndex < 0) || (_historyIndex >= int(_history.size()))) {
|
|
return;
|
|
}
|
|
const auto next = _history.begin() + _historyIndex + 1;
|
|
if (next != _history.end()) {
|
|
_history.erase(next, _history.end());
|
|
removeRetainedLeafFieldsAfter(_historyIndex);
|
|
notifyToolbarStateChanged();
|
|
}
|
|
}
|
|
|
|
bool Widget::canPerformFieldUndoRedo(bool redo) const {
|
|
if (_field->isHidden()) {
|
|
return false;
|
|
}
|
|
const auto document = _field->rawTextEdit()->document();
|
|
const auto steps = redo
|
|
? document->availableRedoSteps()
|
|
: document->availableUndoSteps();
|
|
const auto localRedoAvailable = (document->availableRedoSteps() > 0)
|
|
|| _fieldRedoAvailable
|
|
|| _field->isRedoAvailable();
|
|
if (!redo
|
|
&& localRedoAvailable
|
|
&& activeInlineFieldTextMatchesState()) {
|
|
return false;
|
|
}
|
|
const auto available = (steps > 0)
|
|
|| (redo
|
|
? (_fieldRedoAvailable || _field->isRedoAvailable())
|
|
: (_fieldUndoAvailable || _field->isUndoAvailable()));
|
|
if (!available) {
|
|
return false;
|
|
}
|
|
const auto &noopState = redo
|
|
? _fieldRedoNoopState
|
|
: _fieldUndoNoopState;
|
|
return !noopState || (_field->getTextWithTags() != *noopState);
|
|
}
|
|
|
|
bool Widget::activeInlineFieldTextMatchesState() const {
|
|
if (_field->isHidden() || !_fieldLeaf) {
|
|
return false;
|
|
}
|
|
const auto activeLeaf = _state->activeLeafPath();
|
|
if (!activeLeaf || (*activeLeaf != *_fieldLeaf)) {
|
|
return false;
|
|
}
|
|
const auto trimLeft = !_state->codeBlockLanguage(
|
|
_state->activeTextOrdinal()).has_value();
|
|
if (_state->activeFieldMode() == State::FieldMode::Raw) {
|
|
return _field->getTextWithTags() == TrimInlineFieldText(
|
|
{ _state->activeRawText(), {} },
|
|
trimLeft).text;
|
|
}
|
|
const auto activeText = ConvertRichTextToEditorTags(_state->activeText());
|
|
return _field->getTextWithTags() == TrimInlineFieldText(
|
|
activeText.text,
|
|
trimLeft).text;
|
|
}
|
|
|
|
bool Widget::canPerformHistoryUndoRedo(bool redo) const {
|
|
if ((_historyIndex < 0) || (_historyIndex >= int(_history.size()))) {
|
|
return false;
|
|
}
|
|
return redo
|
|
? (_historyIndex + 1 < int(_history.size()))
|
|
: (_historyIndex > 0);
|
|
}
|
|
|
|
bool Widget::canPerformUndoRedo(bool redo) const {
|
|
return canPerformFieldUndoRedo(redo) || canPerformHistoryUndoRedo(redo);
|
|
}
|
|
|
|
bool Widget::handleUndoRedoShortcut(QKeyEvent *e) {
|
|
auto redo = std::optional<bool>();
|
|
if (e == QKeySequence::Undo) {
|
|
redo = false;
|
|
} else if (e == QKeySequence::Redo) {
|
|
redo = true;
|
|
}
|
|
if (!redo) {
|
|
return false;
|
|
}
|
|
const auto redoValue = *redo;
|
|
if (canPerformFieldUndoRedo(redoValue)) {
|
|
if (performFieldUndoRedo(redoValue)) {
|
|
e->accept();
|
|
return true;
|
|
}
|
|
}
|
|
if (canPerformHistoryUndoRedo(redoValue)) {
|
|
crl::on_main(this, [=] {
|
|
performUndoRedo(redoValue, false);
|
|
});
|
|
}
|
|
e->accept();
|
|
return true;
|
|
}
|
|
|
|
bool Widget::handleSelectAllShortcut(QKeyEvent *e) {
|
|
if (e != QKeySequence::SelectAll) {
|
|
return false;
|
|
}
|
|
if (SingleRootPlainTextFieldSelectAllPassthrough(
|
|
_state->richPage(),
|
|
_state->activeLeafPath(),
|
|
_field->isHidden())) {
|
|
return false;
|
|
}
|
|
if (!_field->isHidden()) {
|
|
const auto committed = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed != ApplyResult::Failed) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
return committed;
|
|
});
|
|
if (committed == ApplyResult::Failed) {
|
|
e->accept();
|
|
return true;
|
|
}
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
const auto blockCount = int(_state->richPage().blocks.size());
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
_articleSelectionDrag = {};
|
|
setStructuralSelection(blockCount > 0
|
|
? BlockSelectionFromIndexes(
|
|
PreparedEditBlockContainerPath(),
|
|
0,
|
|
blockCount - 1)
|
|
: PreparedEditSelection());
|
|
setFocus();
|
|
update();
|
|
e->accept();
|
|
return true;
|
|
}
|
|
|
|
bool Widget::performFieldUndoRedo(bool redo) {
|
|
if (!canPerformFieldUndoRedo(redo)) {
|
|
return false;
|
|
}
|
|
const auto before = _field->getTextWithTags();
|
|
const auto wasPerformingUndoRedo = _performingUndoRedo;
|
|
_performingUndoRedo = true;
|
|
const auto guard = gsl::finally([&] {
|
|
_performingUndoRedo = wasPerformingUndoRedo;
|
|
});
|
|
if (redo) {
|
|
_field->redo();
|
|
} else {
|
|
_field->undo();
|
|
}
|
|
if (_field->isHidden()) {
|
|
return false;
|
|
}
|
|
const auto document = _field->rawTextEdit()->document();
|
|
_fieldUndoAvailable = (document->availableUndoSteps() > 0)
|
|
|| _field->isUndoAvailable();
|
|
_fieldRedoAvailable = (document->availableRedoSteps() > 0)
|
|
|| _field->isRedoAvailable();
|
|
const auto after = _field->getTextWithTags();
|
|
if (after != before) {
|
|
clearFieldUndoRedoNoopState();
|
|
notifyToolbarStateChanged();
|
|
return true;
|
|
}
|
|
if (redo) {
|
|
_fieldRedoNoopState = after;
|
|
} else {
|
|
_fieldUndoNoopState = after;
|
|
}
|
|
notifyToolbarStateChanged();
|
|
return false;
|
|
}
|
|
|
|
void Widget::performUndoRedo(bool redo, bool allowFieldLocal) {
|
|
if (allowFieldLocal && performFieldUndoRedo(redo)) {
|
|
return;
|
|
}
|
|
if (!canPerformHistoryUndoRedo(redo)) {
|
|
return;
|
|
}
|
|
const auto nextIndex = _historyIndex + (redo ? 1 : -1);
|
|
if ((nextIndex < 0) || (nextIndex >= int(_history.size()))) {
|
|
return;
|
|
}
|
|
const auto previousIndex = _historyIndex;
|
|
const auto wasPerformingUndoRedo = _performingUndoRedo;
|
|
_performingUndoRedo = true;
|
|
const auto guard = gsl::finally([&] {
|
|
_performingUndoRedo = wasPerformingUndoRedo;
|
|
});
|
|
const auto wasRetainingFieldHistoryIndexOverride
|
|
= _retainingFieldHistoryIndexOverride;
|
|
_retainingFieldHistoryIndexOverride = previousIndex;
|
|
const auto retainingFieldHistoryIndexOverride = gsl::finally([&] {
|
|
_retainingFieldHistoryIndexOverride
|
|
= wasRetainingFieldHistoryIndexOverride;
|
|
});
|
|
retainActiveLeafField();
|
|
_historyIndex = nextIndex;
|
|
const auto wasRestoringHistoryRedo = _restoringHistoryRedo;
|
|
_restoringHistoryRedo = redo;
|
|
const auto restoringHistoryRedo = gsl::finally([&] {
|
|
_restoringHistoryRedo = wasRestoringHistoryRedo;
|
|
});
|
|
restoreHistoryEntry(_history[_historyIndex]);
|
|
_fieldUndoAvailable = !_field->isHidden()
|
|
? _field->isUndoAvailable()
|
|
: false;
|
|
_fieldRedoAvailable = !_field->isHidden()
|
|
? _field->isRedoAvailable()
|
|
: false;
|
|
clearFieldUndoRedoNoopState();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
void Widget::notifyToolbarStateChanged() {
|
|
_toolbarStateChanges.fire_copy(toolbarStateValue());
|
|
}
|
|
|
|
bool Widget::inlineToolbarModeActive() const {
|
|
return !_field->isHidden()
|
|
&& (_state->activeFieldMode() == State::FieldMode::Rich);
|
|
}
|
|
|
|
Widget::ToolbarLinkMode Widget::toolbarLinkMode() const {
|
|
return inlineToolbarModeActive() && _field->hasCurrentMarkdownLink()
|
|
? ToolbarLinkMode::Edit
|
|
: ToolbarLinkMode::Create;
|
|
}
|
|
|
|
Widget::ToolbarActionState Widget::toolbarActionState(
|
|
ToolbarFormatAction action) const {
|
|
const auto inlineActive = inlineToolbarModeActive();
|
|
const auto activeDisplayMath = !inlineActive
|
|
&& [&] {
|
|
const auto leaf = _state->activeLeafPath();
|
|
return leaf && (leaf->kind == StateLeafKind::MathFormula);
|
|
}();
|
|
const auto broaderTextSelected = !inlineActive
|
|
&& broaderSelectionHasSelectedText();
|
|
const auto broaderMediaSelected = !inlineActive
|
|
&& (action == ToolbarFormatAction::Spoiler)
|
|
&& !broaderSelectionMediaBlocks().empty();
|
|
switch (action) {
|
|
case ToolbarFormatAction::Undo:
|
|
return {
|
|
.shown = true,
|
|
.enabled = canPerformFieldUndoRedo(false)
|
|
|| canPerformHistoryUndoRedo(false),
|
|
};
|
|
case ToolbarFormatAction::Redo: {
|
|
const auto enabled = canPerformFieldUndoRedo(true)
|
|
|| canPerformHistoryUndoRedo(true);
|
|
return {
|
|
.shown = enabled,
|
|
.enabled = enabled,
|
|
};
|
|
}
|
|
case ToolbarFormatAction::Link:
|
|
return {
|
|
.shown = true,
|
|
.enabled = inlineActive,
|
|
};
|
|
case ToolbarFormatAction::Count:
|
|
return {};
|
|
case ToolbarFormatAction::Bold:
|
|
case ToolbarFormatAction::Italic:
|
|
case ToolbarFormatAction::Underline:
|
|
case ToolbarFormatAction::StrikeOut:
|
|
case ToolbarFormatAction::PlainText:
|
|
return {
|
|
.shown = true,
|
|
.enabled = inlineActive || broaderTextSelected,
|
|
.active = inlineActive
|
|
&& (action != ToolbarFormatAction::PlainText)
|
|
&& ToolbarActionTag(action)
|
|
&& _field->isMarkdownTagActive(*ToolbarActionTag(action)),
|
|
};
|
|
case ToolbarFormatAction::Spoiler:
|
|
return {
|
|
.shown = true,
|
|
.enabled = inlineActive
|
|
|| broaderTextSelected
|
|
|| broaderMediaSelected,
|
|
.active = inlineActive
|
|
&& _field->isMarkdownTagActive(Ui::InputField::kTagSpoiler),
|
|
};
|
|
case ToolbarFormatAction::Subscript:
|
|
case ToolbarFormatAction::Superscript:
|
|
case ToolbarFormatAction::Marked:
|
|
return {
|
|
.shown = true,
|
|
.enabled = inlineActive,
|
|
.active = inlineActive
|
|
&& ToolbarActionTag(action)
|
|
&& _field->isMarkdownTagActive(*ToolbarActionTag(action)),
|
|
};
|
|
case ToolbarFormatAction::Math:
|
|
return {
|
|
.shown = true,
|
|
.enabled = inlineActive || activeDisplayMath,
|
|
.active = activeDisplayMath || (inlineActive
|
|
&& _field->isMarkdownTagActive(Ui::InputField::kTagIvMath)),
|
|
};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void Widget::clearFieldUndoRedoNoopState() {
|
|
_fieldUndoNoopState = std::nullopt;
|
|
_fieldRedoNoopState = std::nullopt;
|
|
}
|
|
|
|
bool Widget::escapeActiveBlockBodyFromToolbar() {
|
|
if (_field->isHidden()
|
|
|| _field->textCursor().hasSelection()
|
|
|| !_state->activeBlockBodyCanEscape()) {
|
|
return false;
|
|
}
|
|
auto handled = false;
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
} else if (const auto target = _state->escapeActiveBlockBody()) {
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(*target, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
} else if (_state->lastLimitError()) {
|
|
showLastLimitToast();
|
|
handled = true;
|
|
}
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
});
|
|
return handled;
|
|
}
|
|
|
|
Fn<void()> Widget::captureScrollTopRestorer() const {
|
|
for (auto parent = parentWidget(); parent; parent = parent->parentWidget()) {
|
|
if (const auto scroll = dynamic_cast<Ui::ScrollArea*>(parent)) {
|
|
const auto weak = QPointer<Ui::ScrollArea>(scroll);
|
|
const auto top = scroll->scrollTop();
|
|
return [=] {
|
|
if (weak) {
|
|
weak->scrollToY(top);
|
|
}
|
|
};
|
|
}
|
|
if (const auto scroll = dynamic_cast<Ui::ElasticScroll*>(parent)) {
|
|
const auto weak = QPointer<Ui::ElasticScroll>(scroll);
|
|
const auto top = scroll->scrollTop();
|
|
return [=] {
|
|
if (weak) {
|
|
weak->scrollToY(top);
|
|
}
|
|
};
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Widget::insertHeading1() {
|
|
insertBlock({
|
|
.type = State::InsertBlockType::Heading,
|
|
.headingLevel = 1,
|
|
});
|
|
}
|
|
|
|
void Widget::insertBlockquote() {
|
|
insertBlock({ .type = State::InsertBlockType::Blockquote });
|
|
}
|
|
|
|
void Widget::insertCodeBlock() {
|
|
insertBlock({ .type = State::InsertBlockType::Code });
|
|
}
|
|
|
|
void Widget::insertEmoji(EmojiPtr emoji) {
|
|
if (!emoji || !prepareFieldForInput()) {
|
|
return;
|
|
}
|
|
_field->setFocusFast();
|
|
Ui::InsertEmojiAtCursor(_field->textCursor(), emoji);
|
|
}
|
|
|
|
void Widget::insertCustomEmoji(not_null<DocumentData*> document) {
|
|
if (!prepareFieldForInput()) {
|
|
return;
|
|
}
|
|
_field->setFocusFast();
|
|
Data::InsertCustomEmoji(_field.get(), document);
|
|
}
|
|
|
|
Widget::ToolbarState Widget::toolbarStateValue() const {
|
|
auto result = ToolbarState();
|
|
result.linkMode = toolbarLinkMode();
|
|
for (auto i = 0; i != int(ToolbarFormatAction::Count); ++i) {
|
|
const auto action = ToolbarFormatAction(i);
|
|
result[action] = toolbarActionState(action);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
rpl::producer<Widget::ToolbarState> Widget::toolbarStateChanges() const {
|
|
return _toolbarStateChanges.events_starting_with(toolbarStateValue());
|
|
}
|
|
|
|
void Widget::performToolbarUndoRedo(bool redo) {
|
|
if (!canPerformFieldUndoRedo(redo) && !canPerformHistoryUndoRedo(redo)) {
|
|
return;
|
|
}
|
|
performUndoRedo(redo);
|
|
}
|
|
|
|
void Widget::applyToolbarFormatAction(ToolbarFormatAction action) {
|
|
switch (action) {
|
|
case ToolbarFormatAction::Undo:
|
|
performToolbarUndoRedo(false);
|
|
return;
|
|
case ToolbarFormatAction::Redo:
|
|
performToolbarUndoRedo(true);
|
|
return;
|
|
case ToolbarFormatAction::Link:
|
|
editLinkFromToolbar();
|
|
return;
|
|
case ToolbarFormatAction::Math:
|
|
editMathFromToolbar();
|
|
return;
|
|
case ToolbarFormatAction::Count:
|
|
return;
|
|
case ToolbarFormatAction::Bold:
|
|
case ToolbarFormatAction::Italic:
|
|
case ToolbarFormatAction::Underline:
|
|
case ToolbarFormatAction::StrikeOut:
|
|
case ToolbarFormatAction::Spoiler:
|
|
case ToolbarFormatAction::Subscript:
|
|
case ToolbarFormatAction::Superscript:
|
|
case ToolbarFormatAction::Marked:
|
|
case ToolbarFormatAction::PlainText:
|
|
break;
|
|
}
|
|
if (action == ToolbarFormatAction::PlainText) {
|
|
if (inlineToolbarModeActive() && escapeActiveBlockBodyFromToolbar()) {
|
|
return;
|
|
}
|
|
if (const auto fullHeadingSpan = visibleFullHeadingFieldTextSpan()) {
|
|
const auto full = ConvertEditorTagsToRichText(
|
|
_field->getTextWithAppliedMarkdown());
|
|
const auto cursor = _field->textCursor();
|
|
const auto length = int(full.text.size());
|
|
const auto restoreLeaf = fullHeadingSpan->leaf;
|
|
const auto restoreAnchorOffset = std::clamp(
|
|
richOffsetForFieldOffset(full, cursor.anchor()),
|
|
0,
|
|
length);
|
|
const auto restoreCursorOffset = std::clamp(
|
|
richOffsetForFieldOffset(full, cursor.position()),
|
|
0,
|
|
length);
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
const auto result = _state->applyFormattingToTextSpans(
|
|
{ *fullHeadingSpan },
|
|
TextFormattingAction::PlainText);
|
|
if (result == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
refreshPreparedContent();
|
|
const auto ordinal = _state->textOrdinalForLeafPath(restoreLeaf);
|
|
if (ordinal >= 0) {
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
restoreAnchorOffset,
|
|
restoreCursorOffset);
|
|
} else {
|
|
setFocus();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (result == ApplyResult::Changed)
|
|
|| (committed == ApplyResult::Changed),
|
|
};
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
if (inlineToolbarModeActive()) {
|
|
if (action == ToolbarFormatAction::PlainText) {
|
|
_field->clearCurrentMarkdown();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
}
|
|
if (const auto tag = ToolbarActionTag(action)) {
|
|
_field->toggleCurrentMarkdownTag(*tag);
|
|
notifyToolbarStateChanged();
|
|
}
|
|
return;
|
|
}
|
|
if (action == ToolbarFormatAction::Marked
|
|
|| action == ToolbarFormatAction::Subscript
|
|
|| action == ToolbarFormatAction::Superscript) {
|
|
return;
|
|
}
|
|
const auto textSpans = broaderSelectionTextSpans();
|
|
const auto mediaBlocks = (action == ToolbarFormatAction::Spoiler)
|
|
? broaderSelectionMediaBlocks()
|
|
: std::vector<State::BlockPath>();
|
|
const auto broaderAction = BroaderFormattingAction(action);
|
|
if ((!broaderAction || textSpans.empty())
|
|
&& mediaBlocks.empty()) {
|
|
return;
|
|
}
|
|
recordMutationTransaction([&] {
|
|
const auto hadVisibleField = !_field->isHidden();
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
if (hadVisibleField) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
auto changed = false;
|
|
if (action == ToolbarFormatAction::Spoiler) {
|
|
const auto &page = _state->richPage();
|
|
const auto allTextSpoilered = textSpans.empty()
|
|
|| ranges::all_of(textSpans, [&](const TextNodeSpan &span) {
|
|
const auto current = RichTextFromPath(page, span.leaf);
|
|
if (!current) {
|
|
return true;
|
|
}
|
|
auto before = TextWithEntities();
|
|
auto selected = TextWithEntities();
|
|
auto after = TextWithEntities();
|
|
if (!SplitTextSpan(
|
|
current->text,
|
|
span.from,
|
|
span.till,
|
|
&before,
|
|
&selected,
|
|
&after)) {
|
|
return true;
|
|
}
|
|
return HasFullTextTag(
|
|
ConvertRichTextToEditorTags(std::move(selected)).text,
|
|
Ui::InputField::kTagSpoiler);
|
|
});
|
|
const auto allMediaSpoilered = mediaBlocks.empty()
|
|
|| ranges::all_of(
|
|
mediaBlocks,
|
|
[&](const State::BlockPath &path) {
|
|
const auto block = BlockFromPath(page, path);
|
|
return block && MediaBlockHasSpoiler(*block);
|
|
});
|
|
const auto enableSpoiler = !(allTextSpoilered && allMediaSpoilered);
|
|
if (!textSpans.empty()) {
|
|
const auto result = _state->applyFormattingToTextSpans(
|
|
textSpans,
|
|
TextFormattingAction::Spoiler,
|
|
enableSpoiler);
|
|
if (result == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
changed |= (result == ApplyResult::Changed);
|
|
}
|
|
if (!mediaBlocks.empty()) {
|
|
changed |= _state->toggleSpoilerOnBlocks(
|
|
mediaBlocks,
|
|
enableSpoiler);
|
|
}
|
|
} else if (broaderAction) {
|
|
const auto result = _state->applyFormattingToTextSpans(
|
|
textSpans,
|
|
*broaderAction);
|
|
if (result == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
changed = (result == ApplyResult::Changed);
|
|
}
|
|
if (changed || hadVisibleField || (committed == ApplyResult::Changed)) {
|
|
refreshPreparedContent();
|
|
}
|
|
setFocus();
|
|
notifyToolbarStateChanged();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = changed
|
|
|| hadVisibleField
|
|
|| (committed == ApplyResult::Changed),
|
|
};
|
|
});
|
|
}
|
|
|
|
void Widget::editLinkFromToolbar() {
|
|
if (!inlineToolbarModeActive()) {
|
|
return;
|
|
}
|
|
_field->editCurrentMarkdownLink();
|
|
}
|
|
|
|
void Widget::editMathFromToolbar() {
|
|
if (!_field->isHidden()
|
|
&& _state->activeFieldMode() == State::FieldMode::Raw) {
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
if (const auto request = activeMathEditRequest()) {
|
|
showMathEditBox(*request);
|
|
}
|
|
}
|
|
|
|
void Widget::setInlineFieldExternalInteractionActive(bool active) {
|
|
_inlineFieldExternalInteractionActive = active;
|
|
}
|
|
|
|
int Widget::resizeGetHeight(int newWidth) {
|
|
if (!_article) {
|
|
return 1;
|
|
}
|
|
const auto width = std::max(newWidth, 1);
|
|
const auto padding = EditorBodyPadding();
|
|
if (articleRelayoutDeferralActive()) {
|
|
requestDeferredArticleRelayout();
|
|
if (!_field->isHidden()) {
|
|
requestDeferredInlineFieldGeometry();
|
|
requestDeferredInlineFieldHeightOverride();
|
|
}
|
|
const auto fieldBottom = !_field->isHidden()
|
|
? (_field->y() + _field->height())
|
|
: 0;
|
|
return std::max(
|
|
std::max(
|
|
_articleHeight + padding.top() + padding.bottom(),
|
|
fieldBottom),
|
|
st::ivEditorMinHeight);
|
|
}
|
|
_articleHeight = _article->resizeGetHeight(articleWidth(width));
|
|
syncArticleVisibleTopBottom();
|
|
ensurePendingActivation();
|
|
syncInlineFieldGeometry(width);
|
|
const auto fieldBottom = !_field->isHidden()
|
|
? (_field->y() + _field->height())
|
|
: 0;
|
|
return std::max(
|
|
std::max(
|
|
_articleHeight + padding.top() + padding.bottom(),
|
|
fieldBottom),
|
|
st::ivEditorMinHeight);
|
|
}
|
|
|
|
void Widget::visibleTopBottomUpdated(int visibleTop, int visibleBottom) {
|
|
_visibleRange = Ui::VisibleRange{
|
|
.top = visibleTop,
|
|
.bottom = visibleBottom,
|
|
};
|
|
syncArticleVisibleTopBottom();
|
|
}
|
|
|
|
bool Widget::eventFilter(QObject *object, QEvent *event) {
|
|
if (_field) {
|
|
const auto raw = _field->rawTextEdit();
|
|
if (object == raw.get() || object == raw->viewport()) {
|
|
const auto type = event->type();
|
|
if (type == QEvent::ShortcutOverride
|
|
&& handleFieldBlockInsertShortcut(
|
|
static_cast<QKeyEvent*>(event))) {
|
|
return true;
|
|
}
|
|
if (type == QEvent::Wheel) {
|
|
if (_article && _activeSegmentIndex >= 0) {
|
|
const auto wheel = static_cast<QWheelEvent*>(event);
|
|
auto articlePoint = std::optional<QPoint>();
|
|
if (const auto widget = qobject_cast<QWidget*>(object)) {
|
|
articlePoint = widget->mapTo(this, LocalPosition(wheel))
|
|
- articleTopLeft();
|
|
} else {
|
|
articlePoint = mapFromGlobal(GlobalPosition(wheel))
|
|
- articleTopLeft();
|
|
}
|
|
if (!articlePoint) {
|
|
const auto segmentRect = _article->segmentRect(
|
|
_activeSegmentIndex);
|
|
if (!segmentRect.isEmpty()) {
|
|
articlePoint = segmentRect.center();
|
|
}
|
|
}
|
|
if (articlePoint
|
|
&& handleHorizontalScrollWheel(wheel, *articlePoint)) {
|
|
return true;
|
|
}
|
|
}
|
|
} else if (type == QEvent::KeyPress) {
|
|
const auto keyEvent = static_cast<QKeyEvent*>(event);
|
|
if (handleFieldBlockInsertShortcut(keyEvent)
|
|
|| handleUndoRedoShortcut(keyEvent)
|
|
|| handleSelectAllShortcut(keyEvent)
|
|
|| handleTabNavigation(keyEvent)
|
|
|| handleStructuralSelectionKey(keyEvent)
|
|
|| handleFieldKey(keyEvent)) {
|
|
return true;
|
|
}
|
|
} else if ((type == QEvent::MouseButtonPress
|
|
|| type == QEvent::MouseMove
|
|
|| type == QEvent::MouseButtonRelease)
|
|
&& handleFieldMouseEvent(event)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return Ui::RpWidget::eventFilter(object, event);
|
|
}
|
|
|
|
bool Widget::eventHook(QEvent *e) {
|
|
if (e->type() == QEvent::TouchBegin
|
|
|| e->type() == QEvent::TouchUpdate
|
|
|| e->type() == QEvent::TouchEnd
|
|
|| e->type() == QEvent::TouchCancel) {
|
|
auto *ev = static_cast<QTouchEvent*>(e);
|
|
if (ev->device()->type() == base::TouchDevice::TouchScreen) {
|
|
const auto active = (_horizontalScrollDrag
|
|
== HorizontalScrollDrag::Touch);
|
|
touchEvent(ev);
|
|
if (active
|
|
|| (_horizontalScrollDrag == HorizontalScrollDrag::Touch)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return Ui::RpWidget::eventHook(e);
|
|
}
|
|
|
|
void Widget::contextMenuEvent(QContextMenuEvent *e) {
|
|
if (!_article) {
|
|
Ui::RpWidget::contextMenuEvent(e);
|
|
return;
|
|
}
|
|
const auto articlePoint = e->pos() - articleTopLeft();
|
|
const auto editHit = _article->editHitTest(articlePoint);
|
|
const auto owner = StructuralOwnerFromHit(editHit);
|
|
const auto cell = TableCellFromOwner(owner);
|
|
if (!cell) {
|
|
Ui::RpWidget::contextMenuEvent(e);
|
|
return;
|
|
}
|
|
const auto range = effectiveTableRangeForCell(*cell);
|
|
if (range.empty()) {
|
|
Ui::RpWidget::contextMenuEvent(e);
|
|
return;
|
|
}
|
|
showTableContextMenu(range, e->globalPos());
|
|
e->accept();
|
|
}
|
|
|
|
void Widget::focusInEvent(QFocusEvent *e) {
|
|
Ui::RpWidget::focusInEvent(e);
|
|
if (!_settingField && !_field->isHidden()) {
|
|
_field->setFocusFast();
|
|
}
|
|
}
|
|
|
|
bool Widget::focusNextPrevChild(bool next) {
|
|
if (hasFocus() && _field->isHidden() && moveTabBoundary(next)) {
|
|
return true;
|
|
}
|
|
return Ui::RpWidget::focusNextPrevChild(next);
|
|
}
|
|
|
|
void Widget::keyPressEvent(QKeyEvent *e) {
|
|
if (handleUndoRedoShortcut(e)) {
|
|
return;
|
|
} else if (handleSelectAllShortcut(e)) {
|
|
return;
|
|
} else if (handleClipboardKey(e)) {
|
|
return;
|
|
} else if (handleStructuralSelectionKey(e)) {
|
|
return;
|
|
} else if (_field->isHidden() && handleTabNavigation(e)) {
|
|
return;
|
|
} else if (redirectKeyToField(e) && replayKeyIntoField(e)) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
Ui::RpWidget::keyPressEvent(e);
|
|
}
|
|
|
|
bool Widget::handleHorizontalScrollWheel(
|
|
QWheelEvent *e,
|
|
QPoint articlePoint) {
|
|
const auto phase = e->phase();
|
|
if (phase == Qt::NoScrollPhase) {
|
|
_horizontalScrollLock = std::nullopt;
|
|
} else if (phase == Qt::ScrollBegin) {
|
|
_horizontalScrollLock = std::nullopt;
|
|
}
|
|
if (!_article) {
|
|
return false;
|
|
}
|
|
const auto delta = Ui::ScrollDeltaF(e);
|
|
const auto horizontal = (std::abs(delta.x()) > std::abs(delta.y()));
|
|
if (phase != Qt::NoScrollPhase
|
|
&& phase != Qt::ScrollBegin
|
|
&& !_horizontalScrollLock) {
|
|
_horizontalScrollLock = horizontal ? Qt::Horizontal : Qt::Vertical;
|
|
}
|
|
if (!_article->horizontalScrollHit(articlePoint).scrollable) {
|
|
return false;
|
|
}
|
|
if (horizontal) {
|
|
if (_horizontalScrollLock == Qt::Vertical) {
|
|
return false;
|
|
}
|
|
if (_article->consumeHorizontalScroll(
|
|
articlePoint,
|
|
int(std::round(delta.x())))) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
e->accept();
|
|
return true;
|
|
}
|
|
if (_horizontalScrollLock == Qt::Horizontal) {
|
|
e->accept();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::optional<PreparedEditTableCellSource> Widget::activeTableCellSourceAt(
|
|
QObject *object,
|
|
const QContextMenuEvent &e) const {
|
|
if (!_article || _activeSegmentIndex < 0) {
|
|
return std::nullopt;
|
|
}
|
|
const auto cellAt = [&](QPoint articlePoint) {
|
|
const auto owner = StructuralOwnerFromHit(
|
|
_article->editHitTest(articlePoint));
|
|
return TableCellFromOwner(owner);
|
|
};
|
|
if (const auto widget = qobject_cast<QWidget*>(object)) {
|
|
if (const auto cell = cellAt(
|
|
widget->mapTo(this, e.pos()) - articleTopLeft())) {
|
|
return cell;
|
|
}
|
|
}
|
|
const auto segmentRect = _article->segmentRect(_activeSegmentIndex);
|
|
return !segmentRect.isEmpty()
|
|
? cellAt(segmentRect.center())
|
|
: std::optional<PreparedEditTableCellSource>();
|
|
}
|
|
|
|
void Widget::addFieldBlockFormatActions(not_null<QMenu*> menu) {
|
|
const auto formattingText = tr::lng_menu_formatting(tr::now);
|
|
const auto baseText = [](const QString &text) {
|
|
const auto tab = text.indexOf(QChar('\t'));
|
|
return (tab >= 0) ? text.left(tab) : text;
|
|
};
|
|
auto submenu = (QMenu*)nullptr;
|
|
for (const auto action : menu->actions()) {
|
|
if (const auto candidate = action->menu()) {
|
|
if (baseText(action->text()) == formattingText) {
|
|
submenu = candidate;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!submenu) {
|
|
return;
|
|
}
|
|
const auto textWithShortcut = [](QString text, const QKeySequence &sequence) {
|
|
const auto shortcut = sequence.toString(QKeySequence::NativeText);
|
|
return shortcut.isEmpty()
|
|
? text
|
|
: text + QChar('\t') + shortcut;
|
|
};
|
|
const auto before = [&] {
|
|
for (const auto action : submenu->actions()) {
|
|
if (action->isSeparator()) {
|
|
return action;
|
|
}
|
|
}
|
|
return (QAction*)nullptr;
|
|
}();
|
|
const auto add = [&](QAction *action) {
|
|
if (before) {
|
|
submenu->insertAction(before, action);
|
|
} else {
|
|
submenu->addAction(action);
|
|
}
|
|
};
|
|
const auto blockquote = new QAction(
|
|
textWithShortcut(
|
|
tr::lng_menu_formatting_blockquote(tr::now),
|
|
Ui::kBlockquoteSequence),
|
|
submenu);
|
|
connect(blockquote, &QAction::triggered, this, [=] {
|
|
insertBlockquote();
|
|
});
|
|
add(blockquote);
|
|
|
|
const auto monospace = new QAction(
|
|
textWithShortcut(
|
|
tr::lng_menu_formatting_monospace(tr::now),
|
|
Ui::kMonospaceSequence),
|
|
submenu);
|
|
connect(monospace, &QAction::triggered, this, [=] {
|
|
insertCodeBlock();
|
|
});
|
|
add(monospace);
|
|
}
|
|
|
|
void Widget::handleFieldContextMenuRequest(
|
|
Ui::InputField::ContextMenuRequest request) {
|
|
addFieldBlockFormatActions(request.menu);
|
|
const auto cell = activeTableCellSourceAt(
|
|
_field->rawTextEdit().get(),
|
|
*request.event);
|
|
if (!cell) {
|
|
return;
|
|
}
|
|
const auto range = effectiveTableRangeForCell(*cell);
|
|
if (range.empty() || !_state->tableSelectionInfo(range).valid) {
|
|
return;
|
|
}
|
|
request.customizePopupMenu([=](not_null<Ui::PopupMenu*> popup) {
|
|
const auto popupMenu = popup->menu();
|
|
const auto action = new QAction(
|
|
tr::lng_article_table_change(tr::now),
|
|
popupMenu.get());
|
|
action->setMenu(new QMenu(popupMenu.get()));
|
|
popup->insertAction(
|
|
0,
|
|
base::make_unique_q<Ui::Menu::Action>(
|
|
popupMenu,
|
|
popupMenu->st(),
|
|
action,
|
|
nullptr,
|
|
nullptr));
|
|
const auto separator = new QAction(popupMenu.get());
|
|
separator->setSeparator(true);
|
|
popup->insertAction(
|
|
1,
|
|
base::make_unique_q<Ui::Menu::Separator>(
|
|
popupMenu,
|
|
popupMenu->st(),
|
|
popupMenu->st().separator,
|
|
separator));
|
|
const auto submenu = popup->ensureSubmenu(
|
|
action,
|
|
st::popupMenuWithIcons);
|
|
fillTableChangeMenu(submenu, range);
|
|
});
|
|
}
|
|
|
|
PreparedEditTableCellRange Widget::effectiveTableRangeForCell(
|
|
const PreparedEditTableCellSource &source) {
|
|
const auto single = TableRangeFromCell(source);
|
|
if (single.empty()) {
|
|
return {};
|
|
}
|
|
if (const auto selected = _state->tableContextRangeForSelection(
|
|
_structuralSelection,
|
|
source)) {
|
|
return *selected;
|
|
}
|
|
clearSelection();
|
|
return single;
|
|
}
|
|
|
|
void Widget::showTableContextMenu(
|
|
const PreparedEditTableCellRange &range,
|
|
QPoint globalPos) {
|
|
const auto menu = Ui::CreateChild<Ui::PopupMenu>(
|
|
this,
|
|
st::popupMenuWithIcons);
|
|
fillTableChangeMenu(menu, range);
|
|
if (menu->empty()) {
|
|
menu->deleteLater();
|
|
return;
|
|
}
|
|
menu->popup(globalPos);
|
|
}
|
|
|
|
void Widget::fillTableChangeMenu(
|
|
not_null<Ui::PopupMenu*> menu,
|
|
const PreparedEditTableCellRange &range) {
|
|
const auto info = _state->tableSelectionInfo(range);
|
|
if (!info.valid) {
|
|
return;
|
|
}
|
|
menu->addAction(
|
|
tr::lng_article_table_add_row(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->addTableRow(range, false);
|
|
});
|
|
},
|
|
&st::ivEditorTableAddRowAboveIcon);
|
|
menu->addAction(
|
|
tr::lng_article_table_add_row(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->addTableRow(range, true);
|
|
});
|
|
},
|
|
&st::ivEditorTableAddRowBelowIcon);
|
|
menu->addAction(
|
|
tr::lng_article_table_add_column(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->addTableColumn(range, false);
|
|
});
|
|
},
|
|
&st::ivEditorTableAddColumnLeftIcon);
|
|
menu->addAction(
|
|
tr::lng_article_table_add_column(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->addTableColumn(range, true);
|
|
});
|
|
},
|
|
&st::ivEditorTableAddColumnRightIcon);
|
|
menu->addSeparator();
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_header(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableHeader(range, !info.allHeader);
|
|
});
|
|
},
|
|
info.allHeader
|
|
? &st::ivEditorTableHeaderOffIcon
|
|
: &st::ivEditorTableHeaderIcon,
|
|
info.allHeader);
|
|
menu->addSeparator();
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_left(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableAlignment(
|
|
range,
|
|
RichPage::TableAlignment::Left);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignLeftIcon,
|
|
info.allAlignLeft);
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_center(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableAlignment(
|
|
range,
|
|
RichPage::TableAlignment::Center);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignCenterIcon,
|
|
info.allAlignCenter);
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_right(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableAlignment(
|
|
range,
|
|
RichPage::TableAlignment::Right);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignRightIcon,
|
|
info.allAlignRight);
|
|
menu->addSeparator();
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_top(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableVerticalAlignment(
|
|
range,
|
|
RichPage::TableVerticalAlignment::Top);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignTopIcon,
|
|
info.allAlignTop);
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_middle(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableVerticalAlignment(
|
|
range,
|
|
RichPage::TableVerticalAlignment::Middle);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignMiddleIcon,
|
|
info.allAlignMiddle);
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_align_bottom(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableVerticalAlignment(
|
|
range,
|
|
RichPage::TableVerticalAlignment::Bottom);
|
|
});
|
|
},
|
|
&st::ivEditorTableAlignBottomIcon,
|
|
info.allAlignBottom);
|
|
if (info.canSplitCell) {
|
|
menu->addSeparator();
|
|
menu->addAction(
|
|
tr::lng_article_table_split_cell(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->splitTableCell(range);
|
|
});
|
|
},
|
|
&st::ivEditorTableSplitIcon);
|
|
} else if (info.canUniteCells) {
|
|
menu->addSeparator();
|
|
menu->addAction(
|
|
tr::lng_article_table_unite_cells(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->uniteTableCells(range);
|
|
});
|
|
},
|
|
&st::ivEditorTableMergeIcon);
|
|
}
|
|
const auto hasDeleteAction = info.canDeleteTable
|
|
|| info.canDeleteRows
|
|
|| info.canDeleteColumns;
|
|
if (hasDeleteAction) {
|
|
menu->addSeparator();
|
|
if (info.canDeleteTable) {
|
|
menu->addAction(
|
|
tr::lng_article_table_delete_table(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->removeTable(range);
|
|
});
|
|
},
|
|
&st::menuIconTableSubmenuDelete);
|
|
} else {
|
|
if (info.canDeleteRows) {
|
|
menu->addAction(
|
|
(info.selectedRows == 1)
|
|
? tr::lng_article_table_delete_row(tr::now)
|
|
: tr::lng_article_table_delete_rows(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->removeTableRows(range);
|
|
});
|
|
},
|
|
&st::menuIconTableSubmenuDelete);
|
|
}
|
|
if (info.canDeleteColumns) {
|
|
menu->addAction(
|
|
(info.selectedColumns == 1)
|
|
? tr::lng_article_table_delete_column(tr::now)
|
|
: tr::lng_article_table_delete_columns(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->removeTableColumns(range);
|
|
});
|
|
},
|
|
&st::menuIconTableSubmenuDelete);
|
|
}
|
|
}
|
|
}
|
|
menu->addSeparator();
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_bordered(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableBordered(range, !info.bordered);
|
|
});
|
|
},
|
|
nullptr,
|
|
info.bordered);
|
|
Menu::AddCheckedAction(
|
|
menu,
|
|
tr::lng_article_table_striped(tr::now),
|
|
[=] {
|
|
applyTableChange([=] {
|
|
return _state->setTableStriped(range, !info.striped);
|
|
});
|
|
},
|
|
nullptr,
|
|
info.striped);
|
|
}
|
|
|
|
void Widget::applyTableChange(Fn<bool()> change) {
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
if (_article) {
|
|
_article->clearTextLeafHeightOverride();
|
|
}
|
|
clearSelection();
|
|
setFocus();
|
|
if (!change()) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
showLastLimitToast();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
refreshPreparedContent();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
}
|
|
|
|
void Widget::touchEvent(QTouchEvent *e) {
|
|
if (e->type() == QEvent::TouchCancel) {
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
if (_horizontalScrollDrag != HorizontalScrollDrag::Touch) {
|
|
return;
|
|
}
|
|
_horizontalScrollDrag = HorizontalScrollDrag::None;
|
|
if (_article) {
|
|
_article->endHorizontalScroll();
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (!_article || e->touchPoints().isEmpty()) {
|
|
return;
|
|
}
|
|
const auto articlePoint = mapFromGlobal(
|
|
e->touchPoints().cbegin()->screenPos().toPoint()) - articleTopLeft();
|
|
switch (e->type()) {
|
|
case QEvent::TouchBegin: {
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
const auto hit = _article->horizontalScrollHit(articlePoint);
|
|
if (hit.overScrollbar
|
|
&& _article->beginHorizontalScroll(articlePoint, false)) {
|
|
_horizontalScrollDrag = HorizontalScrollDrag::Touch;
|
|
syncInlineFieldGeometry();
|
|
e->accept();
|
|
} else if (hit.overViewport) {
|
|
_pendingTouchHorizontalScrollPoint = articlePoint;
|
|
}
|
|
} break;
|
|
case QEvent::TouchUpdate:
|
|
if (_horizontalScrollDrag == HorizontalScrollDrag::Touch) {
|
|
if (_article->updateHorizontalScroll(articlePoint)) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
e->accept();
|
|
} else if (_pendingTouchHorizontalScrollPoint) {
|
|
const auto delta = articlePoint - *_pendingTouchHorizontalScrollPoint;
|
|
if (delta.manhattanLength() < QApplication::startDragDistance()) {
|
|
break;
|
|
}
|
|
const auto horizontal = (std::abs(delta.x()) > std::abs(delta.y()));
|
|
if (!horizontal) {
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
break;
|
|
}
|
|
if (_article->beginHorizontalScroll(
|
|
*_pendingTouchHorizontalScrollPoint,
|
|
true)) {
|
|
_horizontalScrollDrag = HorizontalScrollDrag::Touch;
|
|
if (_article->updateHorizontalScroll(articlePoint)) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
e->accept();
|
|
}
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
}
|
|
break;
|
|
case QEvent::TouchEnd:
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
if (_horizontalScrollDrag == HorizontalScrollDrag::Touch) {
|
|
_horizontalScrollDrag = HorizontalScrollDrag::None;
|
|
_article->endHorizontalScroll();
|
|
e->accept();
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Widget::wheelEvent(QWheelEvent *e) {
|
|
if (handleHorizontalScrollWheel(
|
|
e,
|
|
LocalPosition(e) - articleTopLeft())) {
|
|
return;
|
|
}
|
|
e->ignore();
|
|
}
|
|
|
|
bool Widget::redirectKeyToField(QKeyEvent *e) const {
|
|
if (!hasFocus()) {
|
|
return false;
|
|
}
|
|
const auto modifiers = e->modifiers()
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
return (modifiers == Qt::NoModifier || modifiers == Qt::ShiftModifier)
|
|
&& (e->key() != Qt::Key_Shift)
|
|
&& RedirectTextToField(e->text());
|
|
}
|
|
|
|
void Widget::inputMethodEvent(QInputMethodEvent *e) {
|
|
if (!_field) {
|
|
Ui::RpWidget::inputMethodEvent(e);
|
|
return;
|
|
}
|
|
const auto cursor = _field->rawTextEdit()->textCursor();
|
|
if (!ImeEventProducesInput(*e, cursor) || !redirectImeToField()) {
|
|
Ui::RpWidget::inputMethodEvent(e);
|
|
return;
|
|
}
|
|
if (!replayImeIntoField(e)) {
|
|
Ui::RpWidget::inputMethodEvent(e);
|
|
return;
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
|
|
QVariant Widget::inputMethodQuery(Qt::InputMethodQuery query) const {
|
|
if (!_field) {
|
|
return Ui::RpWidget::inputMethodQuery(query);
|
|
}
|
|
return _field->rawTextEdit()->inputMethodQuery(query);
|
|
}
|
|
|
|
bool Widget::redirectImeToField() const {
|
|
return hasFocus()
|
|
&& (hasStructuralSelection() || _field->isHidden());
|
|
}
|
|
|
|
void Widget::mouseMoveEvent(QMouseEvent *e) {
|
|
const auto articlePoint = e->pos() - articleTopLeft();
|
|
if (_horizontalScrollDrag == HorizontalScrollDrag::Mouse) {
|
|
if (_article->updateHorizontalScroll(articlePoint)) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (!_articleSelectionDrag.active) {
|
|
auto cursor = style::cur_default;
|
|
const auto controlHit = _article->editControlHitTest(articlePoint);
|
|
if (controlHit.valid()) {
|
|
cursor = style::cur_pointer;
|
|
} else {
|
|
const auto hit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
if (hit.valid() && hit.codeHeaderCopy) {
|
|
cursor = style::cur_pointer;
|
|
} else if (hit.valid()
|
|
&& hit.direct
|
|
&& _article->segmentIsText(hit.segmentIndex)) {
|
|
cursor = style::cur_text;
|
|
}
|
|
}
|
|
setCursor(cursor);
|
|
Ui::RpWidget::mouseMoveEvent(e);
|
|
return;
|
|
}
|
|
const auto hit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
const auto editHit = _article->editHitTest(articlePoint);
|
|
const auto movedFarEnough = (e->globalPos()
|
|
- _articleSelectionDrag.globalPressPoint).manhattanLength()
|
|
>= QApplication::startDragDistance();
|
|
if (!_articleSelectionDrag.dragStarted) {
|
|
if (!movedFarEnough) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
_articleSelectionDrag.dragStarted = true;
|
|
}
|
|
updateArticleSelection(articlePoint, hit, editHit);
|
|
e->accept();
|
|
}
|
|
|
|
void Widget::mousePressEvent(QMouseEvent *e) {
|
|
if (e->button() != Qt::LeftButton) {
|
|
Ui::RpWidget::mousePressEvent(e);
|
|
return;
|
|
}
|
|
_trackingPointerPress = true;
|
|
_pressedControl = {};
|
|
_pressedControlPoint = std::nullopt;
|
|
auto articlePoint = e->pos() - articleTopLeft();
|
|
const auto horizontalScrollHit = _article->horizontalScrollHit(
|
|
articlePoint);
|
|
if (horizontalScrollHit.overScrollbar
|
|
&& _article->beginHorizontalScroll(articlePoint, false)) {
|
|
_horizontalScrollDrag = HorizontalScrollDrag::Mouse;
|
|
syncInlineFieldGeometry();
|
|
e->accept();
|
|
return;
|
|
}
|
|
const auto controlHit = _article->editControlHitTest(articlePoint);
|
|
if (controlHit.valid()) {
|
|
_pressedControl = controlHit;
|
|
_pressedControlPoint = articlePoint;
|
|
e->accept();
|
|
return;
|
|
}
|
|
auto hit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
const auto editHit = _article->editHitTest(articlePoint);
|
|
const auto startedBelow = (articlePoint.y() >= _articleHeight);
|
|
if (hit.codeHeaderCopy) {
|
|
startArticleSelection(articlePoint, e->globalPos(), hit, editHit);
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (hit.valid() && hit.direct && _article->segmentIsText(hit.segmentIndex)) {
|
|
startArticleSelection(articlePoint, e->globalPos(), hit, editHit);
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (startedBelow) {
|
|
if (editHit.valid()) {
|
|
startArticleSelection(
|
|
articlePoint,
|
|
e->globalPos(),
|
|
hit,
|
|
editHit,
|
|
false,
|
|
true);
|
|
} else {
|
|
clearSelection();
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (editHit.valid()) {
|
|
startArticleSelection(articlePoint, e->globalPos(), hit, editHit);
|
|
e->accept();
|
|
return;
|
|
}
|
|
clearSelection();
|
|
e->accept();
|
|
}
|
|
|
|
void Widget::mouseReleaseEvent(QMouseEvent *e) {
|
|
if (e->button() != Qt::LeftButton) {
|
|
Ui::RpWidget::mouseReleaseEvent(e);
|
|
return;
|
|
}
|
|
const auto guard = gsl::finally([&] {
|
|
_trackingPointerPress = false;
|
|
});
|
|
const auto finishDrag = gsl::finally([&] {
|
|
finishArticleSelection();
|
|
});
|
|
const auto articlePoint = e->pos() - articleTopLeft();
|
|
if (_horizontalScrollDrag == HorizontalScrollDrag::Mouse) {
|
|
const auto changed = _article->updateHorizontalScroll(articlePoint);
|
|
_article->endHorizontalScroll();
|
|
_horizontalScrollDrag = HorizontalScrollDrag::None;
|
|
if (changed) {
|
|
syncInlineFieldGeometry();
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
const auto controlHit = _article->editControlHitTest(articlePoint);
|
|
const auto applyControlToggle = [&](auto &&toggle, auto &&afterRefresh) {
|
|
const auto hadVisibleField = !_field->isHidden();
|
|
auto toggled = false;
|
|
const auto result = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
toggled = toggle();
|
|
if (toggled) {
|
|
refreshPreparedContent();
|
|
} else if (hadVisibleField) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
clearTextSelection();
|
|
clearStructuralSelection();
|
|
setFocus();
|
|
if (toggled) {
|
|
afterRefresh();
|
|
}
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed) || toggled,
|
|
};
|
|
});
|
|
return !result.failed && toggled;
|
|
};
|
|
if (_pressedControl.valid()) {
|
|
const auto pressedControl = _pressedControl;
|
|
const auto pressedControlPoint = _pressedControlPoint;
|
|
_pressedControl = {};
|
|
_pressedControlPoint = std::nullopt;
|
|
const auto matchedControl = pressedControlPoint
|
|
&& ((articlePoint - *pressedControlPoint).manhattanLength()
|
|
< QApplication::startDragDistance())
|
|
&& (controlHit == pressedControl);
|
|
if (matchedControl) {
|
|
switch (pressedControl.kind) {
|
|
case Markdown::MarkdownArticleEditControlHitKind::TaskMarker:
|
|
if (pressedControl.listItem) {
|
|
applyControlToggle([&] {
|
|
return _state->toggleTaskState(*pressedControl.listItem);
|
|
}, [&] {
|
|
_article->addTaskMarkerRipple(
|
|
*pressedControl.listItem,
|
|
articlePoint);
|
|
});
|
|
}
|
|
break;
|
|
case Markdown::MarkdownArticleEditControlHitKind::DetailsToggle:
|
|
if (pressedControl.block) {
|
|
applyControlToggle([&] {
|
|
return _state->toggleDetailsOpen(*pressedControl.block);
|
|
}, [] {
|
|
});
|
|
}
|
|
break;
|
|
case Markdown::MarkdownArticleEditControlHitKind::None:
|
|
break;
|
|
}
|
|
}
|
|
e->accept();
|
|
return;
|
|
}
|
|
const auto hit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
const auto editHit = _article->editHitTest(articlePoint);
|
|
const auto formulaOrdinalFromEditHit = [&] {
|
|
return editHit.leaf
|
|
&& (editHit.leaf->kind
|
|
== Markdown::PreparedEditLeafKind::MathFormula)
|
|
? _state->textOrdinalForLeaf(*editHit.leaf)
|
|
: -1;
|
|
};
|
|
const auto directEditableHit = [&] {
|
|
return (hit.valid()
|
|
&& hit.direct
|
|
&& _article->segmentIsEditable(hit.segmentIndex))
|
|
|| (formulaOrdinalFromEditHit() >= 0);
|
|
};
|
|
const auto commitVisibleInlineField = [&] {
|
|
if (_field->isHidden()) {
|
|
return false;
|
|
}
|
|
beginArticleRelayoutDeferral();
|
|
const auto relayoutGuard = gsl::finally([&] {
|
|
endArticleRelayoutDeferral();
|
|
});
|
|
const auto source = _state->activePreparedLeafSource();
|
|
const auto committed = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed != ApplyResult::Failed) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
return committed;
|
|
});
|
|
if (committed == ApplyResult::Failed) {
|
|
return false;
|
|
}
|
|
refreshAfterInlineFieldCommit(committed, source);
|
|
return true;
|
|
};
|
|
const auto commitAndActivateTextOrdinal = [&](
|
|
int ordinal,
|
|
int selectionFrom,
|
|
int selectionTo) {
|
|
const auto restoreScroll = captureScrollTopRestorer();
|
|
auto source = std::optional<Markdown::PreparedEditLeafSource>();
|
|
auto committed = ApplyResult::Unchanged;
|
|
beginArticleRelayoutDeferral();
|
|
if (!_field->isHidden()) {
|
|
source = _state->activePreparedLeafSource();
|
|
committed = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed != ApplyResult::Failed) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
return committed;
|
|
});
|
|
if (committed == ApplyResult::Failed) {
|
|
endArticleRelayoutDeferral();
|
|
return false;
|
|
}
|
|
}
|
|
finishArticleSelection();
|
|
beginInlineFieldRevealSuppression();
|
|
const auto revealGuard = gsl::finally([&] {
|
|
endInlineFieldRevealSuppression();
|
|
});
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
selectionFrom,
|
|
selectionTo,
|
|
ActivateReveal::Skip);
|
|
refreshAfterInlineFieldCommit(committed, std::move(source));
|
|
endArticleRelayoutDeferral();
|
|
if (restoreScroll) {
|
|
restoreScroll();
|
|
}
|
|
return true;
|
|
};
|
|
const auto focusOrActivateInitial = [&] {
|
|
if (_field->isHidden()) {
|
|
activateInitialNode();
|
|
} else {
|
|
_field->setFocusFast();
|
|
}
|
|
};
|
|
const auto editCodeBlockLanguage = [&] {
|
|
if (!hit.codeHeaderCopy) {
|
|
return false;
|
|
}
|
|
auto languageHit = hit;
|
|
if (!_field->isHidden()) {
|
|
if (!commitVisibleInlineField()) {
|
|
return true;
|
|
}
|
|
languageHit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
}
|
|
const auto ordinal = languageHit.codeHeaderCopy
|
|
? editableOrdinalForSegment(languageHit.segmentIndex)
|
|
: -1;
|
|
if (const auto now = _state->codeBlockLanguage(ordinal)) {
|
|
const auto weak = QPointer<Widget>(this);
|
|
DefaultEditLanguageCallback(_show)(
|
|
*now,
|
|
[=](QString language) {
|
|
if (!weak) {
|
|
return;
|
|
}
|
|
weak->recordMutationTransaction([&] {
|
|
const auto changed = weak->_state->setCodeBlockLanguage(
|
|
ordinal,
|
|
language);
|
|
if (changed) {
|
|
weak->refreshPreparedContent();
|
|
weak->update();
|
|
}
|
|
return changed;
|
|
});
|
|
});
|
|
}
|
|
return true;
|
|
};
|
|
if (_articleSelectionDrag.active) {
|
|
const auto fromField = _articleSelectionDrag.fromField;
|
|
const auto pendingCodeHeader = _articleSelectionDrag.codeHeader;
|
|
const auto startedBelow = _articleSelectionDrag.startedBelow;
|
|
const auto clickLike = !_articleSelectionDrag.dragStarted
|
|
&& ((e->globalPos()
|
|
- _articleSelectionDrag.globalPressPoint).manhattanLength()
|
|
< QApplication::startDragDistance());
|
|
const auto updateOnRelease
|
|
= !clickLike
|
|
&& ((_articleSelectionDrag.mode != DragSelectionMode::None)
|
|
|| (!pendingCodeHeader
|
|
&& (!startedBelow || articlePoint.y() < _articleHeight)));
|
|
if (updateOnRelease) {
|
|
updateArticleSelection(articlePoint, hit, editHit);
|
|
}
|
|
if (clickLike) {
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| hasStructuralSelection();
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
if (changed) {
|
|
update();
|
|
}
|
|
}
|
|
if (!clickLike && hasStructuralSelection()) {
|
|
commitVisibleInlineField();
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Text) {
|
|
const auto selection = _selection;
|
|
const auto sameSegmentSelection = !selection.empty()
|
|
&& (selection.from.segment == selection.to.segment)
|
|
&& _article->segmentIsText(selection.from.segment);
|
|
const auto selectionOrdinal = sameSegmentSelection
|
|
? editableOrdinalForSegment(selection.from.segment)
|
|
: -1;
|
|
if (!fromField && selectionOrdinal >= 0) {
|
|
const auto selectionFrom = selection.from.offset;
|
|
const auto selectionTo = selection.to.offset;
|
|
clearTextSelection();
|
|
commitAndActivateTextOrdinal(
|
|
selectionOrdinal,
|
|
selectionFrom,
|
|
selectionTo);
|
|
e->accept();
|
|
return;
|
|
} else if (fromField) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
}
|
|
if (pendingCodeHeader
|
|
&& _articleSelectionDrag.mode == DragSelectionMode::None
|
|
&& editCodeBlockLanguage()) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| hasStructuralSelection();
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
if (changed) {
|
|
update();
|
|
}
|
|
} else if (hit.codeHeaderCopy && editCodeBlockLanguage()) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
if (directEditableHit()) {
|
|
const auto formulaOrdinal = formulaOrdinalFromEditHit();
|
|
if (formulaOrdinal >= 0) {
|
|
const auto activeDisplayMath = !_field->isHidden()
|
|
&& (_state->activeTextOrdinal() == formulaOrdinal)
|
|
&& (_state->activeFieldMode() == State::FieldMode::Raw);
|
|
if (!activeDisplayMath) {
|
|
if (!_field->isHidden() && !commitVisibleInlineField()) {
|
|
e->accept();
|
|
return;
|
|
}
|
|
activateTextOrdinal(formulaOrdinal, 0);
|
|
}
|
|
editMathFromToolbar();
|
|
e->accept();
|
|
return;
|
|
}
|
|
const auto segmentHit = hit.valid()
|
|
&& hit.direct
|
|
&& _article->segmentIsEditable(hit.segmentIndex);
|
|
const auto targetOrdinal = segmentHit
|
|
? editableOrdinalForSegment(hit.segmentIndex)
|
|
: formulaOrdinal;
|
|
const auto offset = segmentHit
|
|
? _article->selectionOffsetFromHit(
|
|
hit,
|
|
TextSelectType::Letters)
|
|
: 0;
|
|
if (targetOrdinal >= 0
|
|
&& !_field->isHidden()
|
|
&& hit.segmentIndex == _activeSegmentIndex) {
|
|
auto cursor = _field->textCursor();
|
|
cursor.setPosition(std::clamp(
|
|
offset,
|
|
0,
|
|
int(_field->getLastText().size())));
|
|
_field->setTextCursor(cursor);
|
|
_field->setFocusFast();
|
|
} else if (targetOrdinal >= 0) {
|
|
commitAndActivateTextOrdinal(targetOrdinal, offset, offset);
|
|
}
|
|
} else if (articlePoint.y() >= _articleHeight) {
|
|
activateTrailingParagraph();
|
|
} else {
|
|
focusOrActivateInitial();
|
|
}
|
|
e->accept();
|
|
}
|
|
|
|
void Widget::paintEvent(QPaintEvent *e) {
|
|
if (!_article) {
|
|
return;
|
|
}
|
|
auto p = Painter(this);
|
|
p.setTextPalette(st::inTextPalette);
|
|
const auto topLeft = articleTopLeft();
|
|
p.translate(topLeft);
|
|
_article->paint(
|
|
p,
|
|
textPaintContext(e->rect().translated(-topLeft.x(), -topLeft.y())));
|
|
}
|
|
|
|
void Widget::resizeEvent(QResizeEvent *e) {
|
|
Ui::RpWidget::resizeEvent(e);
|
|
syncInlineFieldGeometry();
|
|
}
|
|
|
|
void Widget::requestRepaint(QRect articleRect) {
|
|
crl::on_main(this, [=] {
|
|
if (!_article) {
|
|
return;
|
|
} else if (articleRect.isEmpty()) {
|
|
update();
|
|
} else {
|
|
update(articleRect.translated(articleTopLeft()));
|
|
}
|
|
});
|
|
}
|
|
|
|
void Widget::requestRelayout(QRect articleRect) {
|
|
crl::on_main(this, [=] {
|
|
if (!_article) {
|
|
return;
|
|
}
|
|
relayoutCurrentContent();
|
|
if (articleRect.isEmpty()) {
|
|
update();
|
|
} else {
|
|
update(articleRect.translated(articleTopLeft()));
|
|
}
|
|
});
|
|
}
|
|
|
|
void Widget::setDocument(const Markdown::MarkdownArticleContent &prepared) {
|
|
_article->setContent(prepared);
|
|
}
|
|
|
|
Markdown::MarkdownArticleTextLeafStyle Widget::inlineFieldStyleForSegment(
|
|
int segmentIndex) const {
|
|
return _article
|
|
? _article->editableStyleForSegment(segmentIndex)
|
|
: Markdown::MarkdownArticleTextLeafStyle();
|
|
}
|
|
|
|
const Widget::CachedInlineFieldStyle &Widget::inlineFieldStyleFor(
|
|
const Markdown::MarkdownArticleTextLeafStyle &leafStyle) {
|
|
return inlineFieldStyleFor(normalizedInlineFieldStyle(leafStyle));
|
|
}
|
|
|
|
const Widget::CachedInlineFieldStyle &Widget::inlineFieldStyleFor(
|
|
const InlineFieldStyleData &data) {
|
|
auto key = inlineFieldStyleKey(data);
|
|
auto textFg = data.textFg;
|
|
auto ownedTextFg = std::shared_ptr<style::owned_color>();
|
|
auto ownedTextMarkBg = std::make_shared<style::owned_color>(
|
|
data.textMarkBg);
|
|
auto textMarkBg = ownedTextMarkBg->color();
|
|
if (_inlineFieldTextColorOverride
|
|
&& data.textFg.get() == _inlineFieldTextColorOverride->color().get()) {
|
|
ownedTextFg = std::make_shared<style::owned_color>(data.textFg->c);
|
|
textFg = ownedTextFg->color();
|
|
key.textFg = textFg;
|
|
}
|
|
for (const auto &cached : _fieldStyles) {
|
|
if (cached.key == key) {
|
|
cached.ownedTextMarkBg->update(data.textMarkBg);
|
|
return cached;
|
|
}
|
|
}
|
|
auto fieldStyle = std::make_shared<style::InputField>(
|
|
st::ivEditorInputField);
|
|
fieldStyle->style = *data.textStyle;
|
|
fieldStyle->style.font = data.italic
|
|
? data.textStyle->font->italic()
|
|
: data.textStyle->font;
|
|
fieldStyle->style.lineHeight = data.lineHeight;
|
|
fieldStyle->textFg = textFg;
|
|
fieldStyle->textMarkBg = textMarkBg;
|
|
fieldStyle->textAlign = data.align;
|
|
fieldStyle->placeholderFont = fieldStyle->style.font;
|
|
fieldStyle->placeholderAlign = data.align;
|
|
_fieldStyles.push_back({
|
|
.key = key,
|
|
.style = std::move(fieldStyle),
|
|
.ownedTextFg = std::move(ownedTextFg),
|
|
.ownedTextMarkBg = std::move(ownedTextMarkBg),
|
|
});
|
|
return _fieldStyles.back();
|
|
}
|
|
|
|
std::optional<QColor> Widget::activeQuoteCaptionColor() {
|
|
if (!_state->activeLeafUsesQuoteCaptionColor()) {
|
|
return std::nullopt;
|
|
}
|
|
return Markdown::NonPullquoteQuoteCaptionColor(
|
|
textPaintContext(QRect()),
|
|
*_articleStyle);
|
|
}
|
|
|
|
std::optional<QColor> Widget::activeQuotePlaceholderColor() {
|
|
if (!_state->activeLeafUsesQuotePlaceholderColor()) {
|
|
return std::nullopt;
|
|
}
|
|
return Markdown::NonPullquoteQuoteCaptionColor(
|
|
textPaintContext(QRect()),
|
|
*_articleStyle);
|
|
}
|
|
|
|
void Widget::refreshInlineFieldTextColorOverride() {
|
|
const auto color = activeQuoteCaptionColor();
|
|
if (!color) {
|
|
if (_inlineFieldTextColorOverride) {
|
|
_activeFieldStyleKey = std::nullopt;
|
|
_inlineFieldTextColorOverride.reset();
|
|
}
|
|
return;
|
|
}
|
|
if (_inlineFieldTextColorOverride) {
|
|
_inlineFieldTextColorOverride->update(*color);
|
|
} else {
|
|
_inlineFieldTextColorOverride.emplace(*color);
|
|
}
|
|
}
|
|
|
|
Widget::InlineFieldStyleData Widget::normalizedInlineFieldStyle(
|
|
const Markdown::MarkdownArticleTextLeafStyle &leafStyle) const {
|
|
const auto valid = leafStyle.valid();
|
|
const auto textStyle = valid
|
|
? leafStyle.textStyle
|
|
: &_articleStyle->body;
|
|
const auto lineHeight = (valid && leafStyle.lineHeight > 0)
|
|
? leafStyle.lineHeight
|
|
: std::max(textStyle->lineHeight, textStyle->font->height);
|
|
return {
|
|
.textStyle = textStyle,
|
|
.lineHeight = lineHeight,
|
|
.textFg = _inlineFieldTextColorOverride
|
|
? _inlineFieldTextColorOverride->color()
|
|
: (valid ? leafStyle.textColor : _articleStyle->textColor),
|
|
.textMarkBg = valid
|
|
? leafStyle.markBg
|
|
: _articleStyle->textPalette.markBg->c,
|
|
.align = valid ? leafStyle.align : style::al_left,
|
|
.italic = valid ? leafStyle.italic : false,
|
|
};
|
|
}
|
|
|
|
Widget::InlineFieldStyleKey Widget::inlineFieldStyleKey(
|
|
const InlineFieldStyleData &data) const {
|
|
const auto textStyle = data.textStyle
|
|
? data.textStyle
|
|
: &_articleStyle->body;
|
|
return {
|
|
.font = data.italic
|
|
? textStyle->font->italic()
|
|
: textStyle->font,
|
|
.lineHeight = data.lineHeight,
|
|
.textFg = data.textFg,
|
|
.align = data.align,
|
|
};
|
|
}
|
|
|
|
void Widget::ensureInlineFieldForSegment(int segmentIndex) {
|
|
_revivedRetainedField = false;
|
|
refreshInlineFieldTextColorOverride();
|
|
const auto data = normalizedInlineFieldStyle(
|
|
inlineFieldStyleForSegment(segmentIndex));
|
|
const auto key = inlineFieldStyleKey(data);
|
|
const auto mode = _state->activeFieldMode();
|
|
const auto leaf = _state->activeLeafPath();
|
|
const auto fieldLeafMismatch = leaf
|
|
&& _fieldLeaf
|
|
&& (*_fieldLeaf != *leaf);
|
|
if (_activeFieldStyleKey
|
|
&& leaf
|
|
&& _fieldLeaf
|
|
&& (*_fieldLeaf == *leaf)
|
|
&& *_activeFieldStyleKey == key
|
|
&& _fieldMode == mode) {
|
|
return;
|
|
}
|
|
if (leaf) {
|
|
if (auto revived = reviveRetainedLeafField(
|
|
_historyIndex,
|
|
*leaf,
|
|
mode,
|
|
key)) {
|
|
_field = std::move(revived);
|
|
_activeFieldStyleKey = key;
|
|
_fieldMode = mode;
|
|
_fieldLeaf = *leaf;
|
|
refreshInlineFieldPlaceholderColor();
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
_revivedRetainedField = true;
|
|
clearFieldUndoRedoNoopState();
|
|
return;
|
|
}
|
|
}
|
|
const auto needsRecreate = !_activeFieldStyleKey
|
|
|| (*_activeFieldStyleKey != key)
|
|
|| (_fieldMode != mode)
|
|
|| fieldLeafMismatch;
|
|
if (!needsRecreate) {
|
|
_activeFieldStyleKey = key;
|
|
_fieldMode = mode;
|
|
return;
|
|
}
|
|
const auto &cached = inlineFieldStyleFor(data);
|
|
_activeFieldStyleKey = cached.key;
|
|
_fieldMode = mode;
|
|
recreateInlineField(*cached.style);
|
|
}
|
|
|
|
void Widget::setupInlineField() {
|
|
if (_fieldMode == State::FieldMode::Rich) {
|
|
const auto allowPremiumEmoji = [peer = _peer](
|
|
not_null<DocumentData*> emoji) {
|
|
return Data::AllowEmojiWithoutPremium(peer, emoji);
|
|
};
|
|
_field->setInstantViewEditorTagsEnabled(true);
|
|
InitMessageFieldHandlers({
|
|
.session = _session,
|
|
.show = _show,
|
|
.field = _field.get(),
|
|
.customEmojiPaused = _customEmojiPaused,
|
|
.allowPremiumEmoji = allowPremiumEmoji,
|
|
.fieldStyle = &_field->st(),
|
|
.linkValidator = ValidateInstantViewEditorLink,
|
|
.allowMarkdownTags = {
|
|
Ui::InputField::kTagBold,
|
|
Ui::InputField::kTagItalic,
|
|
Ui::InputField::kTagUnderline,
|
|
Ui::InputField::kTagStrikeOut,
|
|
Ui::InputField::kTagSpoiler,
|
|
Ui::InputField::kTagIvMarked,
|
|
Ui::InputField::kTagIvSubscript,
|
|
Ui::InputField::kTagIvSuperscript,
|
|
Ui::InputField::kTagIvMath,
|
|
},
|
|
});
|
|
if (_show) {
|
|
_field->setEditLinkCallback(DefaultEditLinkCallback(
|
|
_show,
|
|
_field.get(),
|
|
nullptr,
|
|
ValidateInstantViewEditorLink));
|
|
}
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
_outer,
|
|
_field.get(),
|
|
_session,
|
|
{
|
|
.suggestCustomEmoji = true,
|
|
.allowCustomWithoutPremium = allowPremiumEmoji,
|
|
});
|
|
auto messageFieldMimeHook = WrappedMessageFieldMimeHook(
|
|
Ui::InputField::MimeDataHook(),
|
|
_field.get());
|
|
_field->setMimeDataHook([=,
|
|
messageFieldMimeHook = std::move(messageFieldMimeHook)](
|
|
not_null<const QMimeData*> data,
|
|
Ui::InputField::MimeAction action) {
|
|
return handleIvClipboardMime(data, action)
|
|
|| (messageFieldMimeHook
|
|
? messageFieldMimeHook(data, action)
|
|
: false);
|
|
});
|
|
} else {
|
|
_field->setInstantViewEditorTagsEnabled(false);
|
|
_field->setInstantReplacesEnabled(
|
|
rpl::single(false),
|
|
rpl::single(false));
|
|
_field->setMarkdownReplacesEnabled(
|
|
rpl::single(Ui::MarkdownEnabledState{
|
|
Ui::MarkdownDisabled()
|
|
}));
|
|
}
|
|
_field->setDocumentMargin(0.);
|
|
_field->setAdditionalMargins({});
|
|
_field->setSubmitSettings(Ui::InputField::SubmitSettings::None);
|
|
_field->setMaxHeight(std::numeric_limits<int>::max());
|
|
refreshInlineFieldPlaceholderColor();
|
|
const auto raw = _field->rawTextEdit();
|
|
raw->installEventFilter(this);
|
|
raw->viewport()->installEventFilter(this);
|
|
_field->addContextMenuHook([this](
|
|
Ui::InputField::ContextMenuRequest request) {
|
|
handleFieldContextMenuRequest(std::move(request));
|
|
});
|
|
|
|
const auto field = QPointer<Ui::InputField>(_field.get());
|
|
const auto revealActiveField = [=] {
|
|
if (!field || (_field.get() != field.data())) {
|
|
return;
|
|
}
|
|
revealActiveInlineField();
|
|
};
|
|
_field->heightChanges(
|
|
) | rpl::on_next([=] {
|
|
updateInlineFieldHeightOverride();
|
|
revealActiveField();
|
|
}, _field->lifetime());
|
|
_field->focusedChanges(
|
|
) | rpl::on_next([=](bool focused) {
|
|
if (!focused
|
|
&& !_settingField
|
|
&& !_trackingPointerPress
|
|
&& !_inlineFieldExternalInteractionActive) {
|
|
const auto committed = recordMutationTransaction([=] {
|
|
return commitInlineField();
|
|
});
|
|
if (committed == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
}
|
|
}, _field->lifetime());
|
|
QObject::connect(
|
|
raw->document(),
|
|
&QTextDocument::contentsChange,
|
|
_field.get(),
|
|
[this, field](int, int, int) {
|
|
if (!field || (_field.get() != field.data())) {
|
|
return;
|
|
}
|
|
const auto hadRedo = _fieldRedoAvailable;
|
|
const auto hadHistoryRedo
|
|
= (_historyIndex + 1 < int(_history.size()));
|
|
if (!_restoringHistory
|
|
&& !_performingUndoRedo
|
|
&& !_settingField
|
|
&& !_suppressHistoryRedoInvalidation
|
|
&& (hadRedo || hadHistoryRedo)) {
|
|
truncateHistoryRedo();
|
|
}
|
|
if (!_restoringHistory && !_performingUndoRedo && !_settingField) {
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
crl::on_main(this, [=] {
|
|
if (!field || (_field.get() != field.data())) {
|
|
return;
|
|
}
|
|
_fieldUndoAvailable = field->isUndoAvailable();
|
|
_fieldRedoAvailable = field->isRedoAvailable();
|
|
notifyToolbarStateChanged();
|
|
});
|
|
});
|
|
QObject::connect(
|
|
raw,
|
|
&QTextEdit::cursorPositionChanged,
|
|
_field.get(),
|
|
[this, revealActiveField] {
|
|
revealActiveField();
|
|
notifyToolbarStateChanged();
|
|
});
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
|
|
hideInlineField();
|
|
}
|
|
|
|
void Widget::recreateInlineField(const style::InputField &st) {
|
|
const auto text = _field->getTextWithTags();
|
|
const auto cursor = _field->textCursor();
|
|
const auto position = cursor.position();
|
|
const auto anchor = cursor.anchor();
|
|
const auto wasHidden = _field->isHidden();
|
|
const auto hadFocus = _field->hasFocus();
|
|
|
|
_settingField = true;
|
|
_field = base::make_unique_q<Ui::InputField>(
|
|
this,
|
|
st,
|
|
Ui::InputField::Mode::MultiLine,
|
|
rpl::single(QString()));
|
|
setupInlineField();
|
|
refreshInlineFieldPlaceholder();
|
|
const auto wasSuppressingHistoryRedoInvalidation
|
|
= _suppressHistoryRedoInvalidation;
|
|
_suppressHistoryRedoInvalidation = true;
|
|
const auto suppressRedoInvalidation = gsl::finally([&] {
|
|
_suppressHistoryRedoInvalidation
|
|
= wasSuppressingHistoryRedoInvalidation;
|
|
});
|
|
_field->setTextWithTags(text, Ui::InputField::HistoryAction::Clear);
|
|
auto restored = _field->textCursor();
|
|
const auto size = int(_field->getLastText().size());
|
|
const auto restoredAnchor = std::clamp(anchor, 0, size);
|
|
const auto restoredPosition = std::clamp(position, 0, size);
|
|
restored.setPosition(restoredAnchor);
|
|
if (restoredPosition != restoredAnchor) {
|
|
restored.setPosition(restoredPosition, QTextCursor::KeepAnchor);
|
|
}
|
|
_field->setTextCursor(restored);
|
|
if (!wasHidden) {
|
|
_field->show();
|
|
_field->raise();
|
|
if (hadFocus) {
|
|
_field->setFocusFast();
|
|
}
|
|
}
|
|
_fieldLeaf = std::nullopt;
|
|
_settingField = false;
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
|
|
void Widget::ensureInlineFieldCreated() {
|
|
if (_field) {
|
|
return;
|
|
}
|
|
const auto &fieldStyle = inlineFieldStyleFor(
|
|
Markdown::MarkdownArticleTextLeafStyle());
|
|
_activeFieldStyleKey = fieldStyle.key;
|
|
_fieldMode = State::FieldMode::Rich;
|
|
_field = base::make_unique_q<Ui::InputField>(
|
|
this,
|
|
*fieldStyle.style,
|
|
Ui::InputField::Mode::MultiLine,
|
|
rpl::single(QString()));
|
|
setupInlineField();
|
|
clearFieldUndoRedoNoopState();
|
|
}
|
|
|
|
void Widget::refreshInlineFieldPlaceholder() {
|
|
_field->setPlaceholder(rpl::single(_state->activePlaceholderText()));
|
|
refreshInlineFieldPlaceholderColor();
|
|
}
|
|
|
|
void Widget::refreshInlineFieldPlaceholderColor() {
|
|
auto color = activeQuotePlaceholderColor().value_or(
|
|
_articleStyle->supplementaryTextColor->c);
|
|
color.setAlphaF(color.alphaF() * 0.5);
|
|
if (_inlineFieldPlaceholderColorOverride) {
|
|
_inlineFieldPlaceholderColorOverride->update(color);
|
|
} else {
|
|
_inlineFieldPlaceholderColorOverride.emplace(color);
|
|
}
|
|
_field->setPlaceholderColorOverride(
|
|
_inlineFieldPlaceholderColorOverride->color());
|
|
}
|
|
|
|
void Widget::setInlineFieldFromActiveState(int selectionFrom, int selectionTo) {
|
|
ensureInlineFieldForSegment(_activeSegmentIndex);
|
|
const auto revivedRetainedField = _revivedRetainedField;
|
|
_revivedRetainedField = false;
|
|
refreshInlineFieldPlaceholder();
|
|
_settingField = true;
|
|
const auto activeLeaf = _state->activeLeafPath();
|
|
const auto preserveRetainedFieldSession = _restoringHistory
|
|
&& (PreservingExternalFieldRestore == this)
|
|
&& activeLeaf
|
|
&& _fieldLeaf
|
|
&& (*_fieldLeaf == *activeLeaf);
|
|
auto cursorSelectionFrom = selectionFrom;
|
|
auto cursorSelectionTo = selectionTo;
|
|
auto trimmedLeft = 0;
|
|
const auto trimLeft = !_state->codeBlockLanguage(
|
|
_state->activeTextOrdinal()).has_value();
|
|
const auto wasSuppressingHistoryRedoInvalidation
|
|
= _suppressHistoryRedoInvalidation;
|
|
_suppressHistoryRedoInvalidation = true;
|
|
const auto suppressRedoInvalidation = gsl::finally([&] {
|
|
_suppressHistoryRedoInvalidation
|
|
= wasSuppressingHistoryRedoInvalidation;
|
|
});
|
|
if (preserveRetainedFieldSession) {
|
|
_fieldLeaf = activeLeaf;
|
|
_settingField = false;
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
}
|
|
const auto preserveRestoredRetainedField = [&](const TextWithTags &text) {
|
|
const auto document = _field->rawTextEdit()->document();
|
|
const auto matchingHistoryDirection = _restoringHistoryRedo
|
|
&& (*_restoringHistoryRedo
|
|
? (document->availableRedoSteps() > 0
|
|
|| _field->isRedoAvailable())
|
|
: (document->availableUndoSteps() > 0
|
|
|| _field->isUndoAvailable()));
|
|
return revivedRetainedField
|
|
&& _restoringHistory
|
|
&& activeLeaf
|
|
&& _fieldLeaf
|
|
&& (*_fieldLeaf == *activeLeaf)
|
|
&& (matchingHistoryDirection || (_field->getTextWithTags() == text));
|
|
};
|
|
const auto finishWithRetainedField = [&] {
|
|
_fieldLeaf = activeLeaf;
|
|
_settingField = false;
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
};
|
|
const auto resetFieldHistory = !activeLeaf
|
|
|| !_fieldLeaf
|
|
|| (*_fieldLeaf != *activeLeaf);
|
|
if (_state->activeFieldMode() == State::FieldMode::Raw) {
|
|
const auto trimmed = TrimInlineFieldText(
|
|
{ _state->activeRawText(), {} },
|
|
trimLeft);
|
|
if (preserveRestoredRetainedField(trimmed.text)) {
|
|
clearArticleEditableHeightOverride();
|
|
finishWithRetainedField();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
}
|
|
if (resetFieldHistory || (_field->getTextWithTags() != trimmed.text)) {
|
|
_field->setTextWithTags(
|
|
trimmed.text,
|
|
Ui::InputField::HistoryAction::Clear);
|
|
}
|
|
trimmedLeft = trimmed.left;
|
|
clearArticleEditableHeightOverride();
|
|
} else {
|
|
const auto activeText = ConvertRichTextToEditorTags(
|
|
_state->activeText());
|
|
const auto trimmed = TrimInlineFieldText(activeText.text, trimLeft);
|
|
if (preserveRestoredRetainedField(trimmed.text)) {
|
|
finishWithRetainedField();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
}
|
|
if (resetFieldHistory || (_field->getTextWithTags() != trimmed.text)) {
|
|
_field->setTextWithTags(
|
|
trimmed.text,
|
|
Ui::InputField::HistoryAction::Clear);
|
|
}
|
|
cursorSelectionFrom = MapRichTextOffsetToEditorOffset(
|
|
activeText.replacements,
|
|
selectionFrom);
|
|
cursorSelectionTo = MapRichTextOffsetToEditorOffset(
|
|
activeText.replacements,
|
|
selectionTo);
|
|
trimmedLeft = trimmed.left;
|
|
}
|
|
cursorSelectionFrom -= trimmedLeft;
|
|
cursorSelectionTo -= trimmedLeft;
|
|
auto cursor = _field->textCursor();
|
|
const auto size = int(_field->getLastText().size());
|
|
const auto from = std::clamp(cursorSelectionFrom, 0, size);
|
|
const auto to = std::clamp(cursorSelectionTo, 0, size);
|
|
cursor.setPosition(from);
|
|
if (to != from) {
|
|
cursor.setPosition(to, QTextCursor::KeepAnchor);
|
|
}
|
|
_field->setTextCursor(cursor);
|
|
_fieldLeaf = _state->activeLeafPath();
|
|
_settingField = false;
|
|
_fieldUndoAvailable = _field->isUndoAvailable();
|
|
_fieldRedoAvailable = _field->isRedoAvailable();
|
|
clearFieldUndoRedoNoopState();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
void Widget::activateTextOrdinal(
|
|
int ordinal,
|
|
int cursorOffset,
|
|
ActivateReveal reveal) {
|
|
activateTextOrdinal(ordinal, cursorOffset, cursorOffset, reveal);
|
|
}
|
|
|
|
void Widget::activateTextOrdinal(
|
|
int ordinal,
|
|
int selectionFrom,
|
|
int selectionTo,
|
|
ActivateReveal reveal) {
|
|
const auto targetLeaf = [&]() -> std::optional<State::LeafPath> {
|
|
const auto &nodes = _state->textNodes();
|
|
return (ordinal >= 0 && ordinal < int(nodes.size()))
|
|
? std::make_optional(nodes[ordinal].leaf)
|
|
: std::nullopt;
|
|
}();
|
|
if (targetLeaf
|
|
&& _fieldLeaf
|
|
&& (*_fieldLeaf != *targetLeaf)) {
|
|
retainActiveLeafField();
|
|
}
|
|
if (!_state->setActiveTextByOrdinal(ordinal)) {
|
|
return;
|
|
}
|
|
_boundarySelectionOrigin = std::nullopt;
|
|
_activeOrdinal = ordinal;
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
|
|
const auto previousSegmentIndex = _activeSegmentIndex;
|
|
const auto segmentIndex = segmentIndexForEditableOrdinal(ordinal);
|
|
if (segmentIndex < 0) {
|
|
_activeSegmentIndex = -1;
|
|
_pendingOrdinal = ordinal;
|
|
_pendingCursorOffset = selectionTo;
|
|
hideInlineField();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
}
|
|
|
|
if (_article && previousSegmentIndex != segmentIndex) {
|
|
clearArticleEditableHeightOverride();
|
|
}
|
|
if (previousSegmentIndex != segmentIndex) {
|
|
clearDisplayMathEditSession();
|
|
}
|
|
_activeSegmentIndex = segmentIndex;
|
|
if (_article->segmentIsDisplayMath(_activeSegmentIndex)) {
|
|
clearDisplayMathEditSession();
|
|
clearArticleEditableHeightOverride();
|
|
hideInlineField();
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
update();
|
|
notifyToolbarStateChanged();
|
|
return;
|
|
} else {
|
|
clearDisplayMathEditSession();
|
|
}
|
|
const auto hadArticleSelection = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| hasStructuralSelection();
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
if (hadArticleSelection) {
|
|
update();
|
|
}
|
|
setInlineFieldFromActiveState(selectionFrom, selectionTo);
|
|
_field->show();
|
|
syncInlineFieldGeometry();
|
|
updateInlineFieldHeightOverride();
|
|
syncArticleVisibleTopBottom();
|
|
if (reveal == ActivateReveal::Reveal) {
|
|
revealActiveInlineField();
|
|
}
|
|
_field->raise();
|
|
_field->setFocusFast();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
QRect Widget::activeInlineFieldRevealRect() const {
|
|
const auto raw = _field->rawTextEdit();
|
|
const auto cursor = _field->textCursor();
|
|
auto positionCursor = cursor;
|
|
positionCursor.setPosition(cursor.position());
|
|
auto revealRect = raw->cursorRect(positionCursor);
|
|
if (cursor.hasSelection()) {
|
|
auto anchorCursor = cursor;
|
|
anchorCursor.setPosition(cursor.anchor());
|
|
revealRect = revealRect.united(raw->cursorRect(anchorCursor));
|
|
}
|
|
if (!revealRect.isValid() || revealRect.isEmpty()) {
|
|
return _field->rect();
|
|
}
|
|
revealRect.moveTopLeft(
|
|
raw->viewport()->mapTo(_field, revealRect.topLeft()));
|
|
return revealRect;
|
|
}
|
|
|
|
QRect Widget::mapFieldLocalRectToScrollContent(
|
|
QWidget *inner,
|
|
QRect rect) const {
|
|
rect.moveTopLeft(_field->mapTo(inner, rect.topLeft()));
|
|
return rect;
|
|
}
|
|
|
|
void Widget::revealActiveInlineField() {
|
|
if (inlineFieldRevealSuppressed()
|
|
|| _field->isHidden()
|
|
|| _activeSegmentIndex < 0) {
|
|
return;
|
|
}
|
|
if (_article->revealSegment(_activeSegmentIndex)) {
|
|
syncInlineFieldGeometry();
|
|
if (_field->isHidden()) {
|
|
return;
|
|
}
|
|
}
|
|
const auto scrollIn = [&](auto &&scroll) {
|
|
if (const auto inner = scroll->widget()) {
|
|
const auto localRect = mapFieldLocalRectToScrollContent(
|
|
inner,
|
|
activeInlineFieldRevealRect());
|
|
scroll->scrollToY(
|
|
localRect.y(),
|
|
localRect.y() + localRect.height());
|
|
}
|
|
};
|
|
for (auto parent = parentWidget(); parent; parent = parent->parentWidget()) {
|
|
if (const auto scroll = dynamic_cast<Ui::ScrollArea*>(parent)) {
|
|
scrollIn(scroll);
|
|
return;
|
|
}
|
|
if (const auto scroll = dynamic_cast<Ui::ElasticScroll*>(parent)) {
|
|
scrollIn(scroll);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Widget::activateTrailingParagraph() {
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
const auto ordinal = _state->ensureTrailingParagraphActive();
|
|
if (!ordinal) {
|
|
showLastLimitToast();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(*ordinal, _state->activeText().text.size());
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
}
|
|
|
|
void Widget::revertInlineFieldToState() {
|
|
if (_field->isHidden() || _activeSegmentIndex < 0) {
|
|
return;
|
|
}
|
|
const auto cursor = _field->textCursor();
|
|
setInlineFieldFromActiveState(cursor.anchor(), cursor.position());
|
|
syncInlineFieldGeometry();
|
|
updateInlineFieldHeightOverride();
|
|
}
|
|
|
|
std::optional<State::ActiveTextInsertContext>
|
|
Widget::activeTextInsertContext() const {
|
|
if (_settingField
|
|
|| _field->isHidden()
|
|
|| (_activeSegmentIndex < 0)
|
|
|| (_state->activeFieldMode() == State::FieldMode::Raw)) {
|
|
return std::nullopt;
|
|
}
|
|
auto full = ConvertEditorTagsToRichText(_field->getTextWithAppliedMarkdown());
|
|
const auto cursor = _field->textCursor();
|
|
auto from = richOffsetForFieldOffset(full, cursor.selectionStart());
|
|
auto till = richOffsetForFieldOffset(full, cursor.selectionEnd());
|
|
const auto textSize = int(full.text.size());
|
|
from = std::clamp(from, 0, textSize);
|
|
till = std::clamp(till, from, textSize);
|
|
auto before = (from > 0)
|
|
? Ui::Text::Mid(full, 0, from)
|
|
: TextWithEntities();
|
|
auto selected = (till > from)
|
|
? Ui::Text::Mid(full, from, till - from)
|
|
: TextWithEntities();
|
|
auto after = (till < textSize)
|
|
? Ui::Text::Mid(full, till)
|
|
: TextWithEntities();
|
|
return State::ActiveTextInsertContext{
|
|
.before = std::move(before),
|
|
.selected = std::move(selected),
|
|
.after = std::move(after),
|
|
};
|
|
}
|
|
|
|
PreparedMediaPasteTarget Widget::preparedMediaPasteTarget() const {
|
|
auto result = PreparedMediaPasteTarget();
|
|
const auto leaf = _state->activeLeafPath();
|
|
if (!leaf) {
|
|
return result;
|
|
}
|
|
result.leaf = leaf;
|
|
const auto ordinal = _state->textOrdinalForLeafPath(*leaf);
|
|
if (ordinal >= 0) {
|
|
result.anchor = _state->textNodes()[ordinal].insertionAnchor;
|
|
}
|
|
switch (leaf->kind) {
|
|
case StateLeafKind::BlockText:
|
|
case StateLeafKind::ListItemText:
|
|
result.context = ClipboardPasteInsertContext(
|
|
activeTextInsertContext());
|
|
break;
|
|
case StateLeafKind::BlockCaption:
|
|
case StateLeafKind::TableCellText:
|
|
case StateLeafKind::MathFormula:
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Widget::PreparedMediaPasteActivation
|
|
Widget::activatePreparedMediaPasteTarget(PreparedMediaPasteTarget target) {
|
|
if (!target.leaf) {
|
|
return {};
|
|
}
|
|
const auto ordinal = _state->textOrdinalForLeafPath(*target.leaf);
|
|
if (ordinal < 0) {
|
|
return {};
|
|
}
|
|
const auto &nodes = _state->textNodes();
|
|
if (ordinal >= int(nodes.size())) {
|
|
return {};
|
|
}
|
|
if (target.anchor
|
|
&& ((nodes[ordinal].insertionAnchor.blockIndex
|
|
!= target.anchor->blockIndex)
|
|
|| !(nodes[ordinal].insertionAnchor.container
|
|
== target.anchor->container))) {
|
|
return {};
|
|
}
|
|
activateTextOrdinal(ordinal, 0);
|
|
return {
|
|
.resolved = true,
|
|
.context = std::move(target.context),
|
|
};
|
|
}
|
|
|
|
std::optional<State::TextNodeSpan>
|
|
Widget::visibleFullHeadingFieldTextSpan() const {
|
|
if (_settingField
|
|
|| _field->isHidden()
|
|
|| (_activeSegmentIndex < 0)
|
|
|| (_state->activeFieldMode() == State::FieldMode::Raw)) {
|
|
return std::nullopt;
|
|
}
|
|
const auto leaf = _state->activeLeafPath();
|
|
if (!leaf || (leaf->kind != StateLeafKind::BlockText)) {
|
|
return std::nullopt;
|
|
}
|
|
const auto owner = BlockFromPath(_state->richPage(), leaf->block);
|
|
if (!owner || (owner->kind != RichPage::BlockKind::Heading)) {
|
|
return std::nullopt;
|
|
}
|
|
const auto full = ConvertEditorTagsToRichText(
|
|
_field->getTextWithAppliedMarkdown());
|
|
const auto cursor = _field->textCursor();
|
|
if (!cursor.hasSelection()) {
|
|
return std::nullopt;
|
|
}
|
|
const auto length = int(full.text.size());
|
|
auto from = richOffsetForFieldOffset(full, cursor.selectionStart());
|
|
auto till = richOffsetForFieldOffset(full, cursor.selectionEnd());
|
|
from = std::clamp(from, 0, length);
|
|
till = std::clamp(till, from, length);
|
|
return (from == 0) && (till == length)
|
|
? std::make_optional(TextNodeSpan{
|
|
.leaf = *leaf,
|
|
.from = 0,
|
|
.till = length,
|
|
})
|
|
: std::nullopt;
|
|
}
|
|
|
|
std::optional<Widget::MathEditRequest> Widget::activeMathEditRequest() const {
|
|
if (_settingField
|
|
|| (_activeSegmentIndex < 0)) {
|
|
return std::nullopt;
|
|
}
|
|
if (_state->activeFieldMode() == State::FieldMode::Raw) {
|
|
const auto leaf = _state->activeLeafPath();
|
|
if (!leaf || leaf->kind != StateLeafKind::MathFormula) {
|
|
return std::nullopt;
|
|
}
|
|
return MathEditRequest{
|
|
.source = _state->activeRawText(),
|
|
.displayMathOrdinal = _state->activeTextOrdinal(),
|
|
.editingExisting = true,
|
|
.allowSeparateLine = true,
|
|
.separateLine = true,
|
|
};
|
|
}
|
|
if (_field->isHidden()) {
|
|
return std::nullopt;
|
|
}
|
|
if (_state->activeFieldMode() != State::FieldMode::Rich) {
|
|
return std::nullopt;
|
|
}
|
|
const auto cursor = _field->textCursor();
|
|
const auto selection = Ui::InputFieldTextRange{
|
|
.from = cursor.selectionStart(),
|
|
.till = cursor.selectionEnd(),
|
|
};
|
|
auto request = MathEditRequest{
|
|
.range = selection,
|
|
.allowSeparateLine = _state->activeSurfaceAllowsSeparateLineFormula(),
|
|
};
|
|
if (!selection.empty()) {
|
|
request.source = _field->getTextWithTagsPart(
|
|
selection.from,
|
|
selection.till).text;
|
|
return request;
|
|
}
|
|
request.range = _field->selectionEditMarkdownTagRange(
|
|
selection,
|
|
Ui::InputField::kTagIvMath);
|
|
if (!request.range.empty()) {
|
|
request.source = _field->getTextWithTagsPart(
|
|
request.range.from,
|
|
request.range.till).text;
|
|
request.editingExisting = true;
|
|
}
|
|
return request;
|
|
}
|
|
|
|
bool Widget::handleIvClipboardMime(
|
|
not_null<const QMimeData*> data,
|
|
Ui::InputField::MimeAction action) {
|
|
const auto modifiers = QApplication::keyboardModifiers();
|
|
if ((modifiers & Qt::ControlModifier)
|
|
&& (modifiers & Qt::ShiftModifier)) {
|
|
return false;
|
|
}
|
|
const auto clipboardData = ClipboardDataFromMimeData(data.get());
|
|
if (clipboardData
|
|
&& ClipboardPasteInsertContext(activeTextInsertContext())) {
|
|
if (action == Ui::InputField::MimeAction::Check) {
|
|
return true;
|
|
}
|
|
crl::on_main(this, [=, clipboardData = *clipboardData] {
|
|
pasteStructuredClipboardData(clipboardData);
|
|
});
|
|
return true;
|
|
}
|
|
if (action == Ui::InputField::MimeAction::Check) {
|
|
return CanPrepareMediaFromClipboard(data);
|
|
} else if (auto list = PreparedMediaFromClipboard(
|
|
data,
|
|
_session->premium())) {
|
|
if (_applyPreparedMedia) {
|
|
auto target = preparedMediaPasteTarget();
|
|
crl::on_main(this, [=, list = std::move(*list)]() mutable {
|
|
_applyPreparedMedia(
|
|
not_null<Widget*>(this),
|
|
std::move(list),
|
|
std::move(target));
|
|
});
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int Widget::richOffsetForFieldOffset(
|
|
const TextWithEntities &text,
|
|
int offset) const {
|
|
const auto replacements = ConvertRichTextToEditorTags(text).replacements;
|
|
return std::clamp(
|
|
MapEditorOffsetToRichOffset(replacements, offset),
|
|
0,
|
|
int(text.text.size()));
|
|
}
|
|
|
|
ApplyResult Widget::applyFieldTextToState() {
|
|
if (_settingField || _field->isHidden()) {
|
|
return ApplyResult::Unchanged;
|
|
}
|
|
if (_state->activeFieldMode() == State::FieldMode::Raw) {
|
|
return _state->applyActiveRawText(_field->getLastText());
|
|
}
|
|
const auto text = _field->getTextWithAppliedMarkdown();
|
|
return _state->applyActiveText(ConvertEditorTagsToRichText(text));
|
|
}
|
|
|
|
ApplyResult Widget::applyMathEditResult(
|
|
const MathEditRequest &request,
|
|
MathEditResult result) {
|
|
const auto source = result.source.trimmed();
|
|
if (source.isEmpty()) {
|
|
return ApplyResult::Unchanged;
|
|
}
|
|
if (_settingField) {
|
|
return ApplyResult::Unchanged;
|
|
}
|
|
if (request.displayMathOrdinal >= 0) {
|
|
if (!_state->setActiveTextByOrdinal(request.displayMathOrdinal)) {
|
|
return ApplyResult::Failed;
|
|
}
|
|
_activeOrdinal = request.displayMathOrdinal;
|
|
_activeSegmentIndex = segmentIndexForEditableOrdinal(_activeOrdinal);
|
|
}
|
|
const auto displayMathEdit
|
|
= (_state->activeFieldMode() == State::FieldMode::Raw);
|
|
if (!displayMathEdit && _field->isHidden()) {
|
|
return ApplyResult::Unchanged;
|
|
}
|
|
if (displayMathEdit) {
|
|
auto displayMathResult = State::DisplayMathEditResult();
|
|
const auto committed = recordMutationTransaction([&] {
|
|
displayMathResult = _state->editActiveDisplayMath(
|
|
source,
|
|
result.separateLine);
|
|
return displayMathResult.result;
|
|
});
|
|
if (committed == ApplyResult::Failed) {
|
|
showLastLimitToast();
|
|
return committed;
|
|
}
|
|
if (committed != ApplyResult::Changed) {
|
|
return committed;
|
|
}
|
|
refreshPreparedContent();
|
|
if (displayMathResult.inlineLeaf) {
|
|
const auto ordinal = _state->textOrdinalForLeafPath(
|
|
*displayMathResult.inlineLeaf);
|
|
activateTextOrdinal(
|
|
(ordinal >= 0) ? ordinal : _state->activeTextOrdinal(),
|
|
displayMathResult.selectionFrom,
|
|
displayMathResult.selectionTo);
|
|
} else {
|
|
activateTextOrdinal(_state->activeTextOrdinal(), 0);
|
|
}
|
|
return committed;
|
|
}
|
|
if (_state->activeFieldMode() != State::FieldMode::Rich) {
|
|
return ApplyResult::Unchanged;
|
|
}
|
|
if (result.separateLine) {
|
|
auto cursor = _field->textCursor();
|
|
cursor.setPosition(request.range.from);
|
|
cursor.setPosition(request.range.till, QTextCursor::KeepAnchor);
|
|
_field->setTextCursor(cursor);
|
|
auto block = RichPage::Block();
|
|
block.kind = RichPage::BlockKind::Math;
|
|
block.formula = source;
|
|
insertPreparedBlock(std::move(block));
|
|
return ApplyResult::Changed;
|
|
}
|
|
const auto committed = recordMutationTransaction([&] {
|
|
_field->commitMarkdownTagEdit(
|
|
request.range,
|
|
Ui::InputField::kTagIvMath,
|
|
source);
|
|
const auto committed = commitInlineField();
|
|
if (committed != ApplyResult::Failed) {
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
}
|
|
return committed;
|
|
});
|
|
if (committed != ApplyResult::Failed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
return committed;
|
|
}
|
|
|
|
bool Widget::showLastLimitToast() {
|
|
if (_showLimitToast) {
|
|
if (const auto error = _state->lastLimitError()) {
|
|
_showLimitToast(*error);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Widget::showMathEditBox(MathEditRequest request) {
|
|
if (!_show) {
|
|
return;
|
|
}
|
|
const auto weak = QPointer<Widget>(this);
|
|
_show->showBox(Box(
|
|
EditMathBox,
|
|
request.source,
|
|
request.editingExisting,
|
|
request.allowSeparateLine
|
|
? std::make_optional(request.separateLine)
|
|
: std::nullopt,
|
|
[=](QString source, bool separateLine) {
|
|
if (!weak) {
|
|
return;
|
|
}
|
|
const auto result = weak->applyMathEditResult(request, {
|
|
.source = std::move(source),
|
|
.separateLine = separateLine,
|
|
});
|
|
if (result != ApplyResult::Changed) {
|
|
return;
|
|
}
|
|
weak->syncInlineFieldGeometry();
|
|
weak->updateInlineFieldHeightOverride();
|
|
weak->revealActiveInlineField();
|
|
weak->notifyToolbarStateChanged();
|
|
},
|
|
[=](bool active) {
|
|
if (weak) {
|
|
weak->setInlineFieldExternalInteractionActive(active);
|
|
weak->notifyToolbarStateChanged();
|
|
}
|
|
},
|
|
[=] {
|
|
if (weak && !weak->_field->isHidden()) {
|
|
weak->_field->setFocusFast();
|
|
weak->notifyToolbarStateChanged();
|
|
}
|
|
}));
|
|
}
|
|
|
|
void Widget::hideInlineField() {
|
|
if (_field->isHidden()) {
|
|
return;
|
|
}
|
|
const auto wasSettingField = _settingField;
|
|
_settingField = true;
|
|
const auto guard = gsl::finally([&] {
|
|
_settingField = wasSettingField;
|
|
});
|
|
_field->hide();
|
|
}
|
|
|
|
void Widget::activateTextOrdinalAtEnd(int ordinal) {
|
|
if (!_state->setActiveTextByOrdinal(ordinal)) {
|
|
return;
|
|
}
|
|
activateTextOrdinal(ordinal, _state->activeTextLength());
|
|
}
|
|
|
|
bool Widget::handleFieldKey(QKeyEvent *e) {
|
|
if (_field->isHidden()) {
|
|
return false;
|
|
}
|
|
const auto key = e->key();
|
|
if (key == Qt::Key_Escape) {
|
|
hideInlineFieldAndRefresh();
|
|
e->accept();
|
|
return true;
|
|
}
|
|
const auto modifiers = e->modifiers()
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
if (modifiers != Qt::NoModifier) {
|
|
return false;
|
|
}
|
|
const auto cursor = _field->textCursor();
|
|
if (cursor.hasSelection()) {
|
|
return false;
|
|
}
|
|
const auto atStart = cursor.atStart();
|
|
const auto atEnd = cursor.atEnd();
|
|
auto handled = false;
|
|
const auto refreshPreparedContentAndActivate = [&](
|
|
int ordinal,
|
|
int cursorOffset) {
|
|
beginInlineFieldRevealSuppression();
|
|
{
|
|
const auto revealGuard = gsl::finally([&] {
|
|
endInlineFieldRevealSuppression();
|
|
});
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(ordinal, cursorOffset, ActivateReveal::Skip);
|
|
}
|
|
revealActiveInlineField();
|
|
};
|
|
if (atStart
|
|
&& (key == Qt::Key_Left
|
|
|| key == Qt::Key_Up
|
|
|| key == Qt::Key_PageUp)) {
|
|
handled = moveBoundary(false, false);
|
|
} else if (atEnd && key == Qt::Key_Down) {
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
} else if (const auto target
|
|
= _state->removeTemporaryDownParagraphAndMove();
|
|
target.action != State::BoundaryTarget::Action::None) {
|
|
switch (target.action) {
|
|
case State::BoundaryTarget::Action::Text:
|
|
refreshPreparedContentAndActivate(target.textOrdinal, 0);
|
|
break;
|
|
case State::BoundaryTarget::Action::StructuralSelection:
|
|
beginInlineFieldRevealSuppression();
|
|
{
|
|
const auto revealGuard = gsl::finally([&] {
|
|
endInlineFieldRevealSuppression();
|
|
});
|
|
refreshPreparedContent();
|
|
}
|
|
_boundarySelectionOrigin = std::nullopt;
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
_articleSelectionDrag = {};
|
|
setStructuralSelection(target.structuralSelection);
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
update();
|
|
break;
|
|
case State::BoundaryTarget::Action::None:
|
|
case State::BoundaryTarget::Action::RemoveActiveOwner:
|
|
break;
|
|
}
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
} else if (const auto target = _state->moveActiveSpecialBlockDown()) {
|
|
refreshPreparedContentAndActivate(*target, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
}
|
|
auto mutated = false;
|
|
if (_state->lastLimitError()) {
|
|
handled = moveBoundaryAfterCommit(
|
|
committed,
|
|
true,
|
|
false,
|
|
&mutated);
|
|
if (!handled) {
|
|
handled = true;
|
|
}
|
|
} else {
|
|
handled = moveBoundaryAfterCommit(
|
|
committed,
|
|
true,
|
|
true,
|
|
&mutated);
|
|
}
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = mutated || (committed == ApplyResult::Changed),
|
|
};
|
|
});
|
|
} else if (atEnd
|
|
&& (key == Qt::Key_Right
|
|
|| key == Qt::Key_PageDown)) {
|
|
handled = moveBoundary(true, true);
|
|
} else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
} else if (const auto target = _state->handleActiveListEnter()) {
|
|
refreshPreparedContentAndActivate(*target, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
} else if (const auto target = _state->handleActiveHeadingEnter()) {
|
|
refreshPreparedContentAndActivate(*target, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
} else if (const auto target
|
|
= _state->submitActiveSingleLineField()) {
|
|
refreshPreparedContentAndActivate(*target, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
} else if (_state->lastLimitError()) {
|
|
showLastLimitToast();
|
|
handled = true;
|
|
}
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
});
|
|
} else if (atStart && key == Qt::Key_Backspace) {
|
|
handled = removeBoundaryOwner(false);
|
|
} else if (atEnd && key == Qt::Key_Delete) {
|
|
handled = removeBoundaryOwner(true);
|
|
}
|
|
if (handled) {
|
|
e->accept();
|
|
}
|
|
return handled;
|
|
}
|
|
|
|
bool Widget::handleTabNavigation(QKeyEvent *e) {
|
|
const auto key = e->key();
|
|
if (key != Qt::Key_Tab && key != Qt::Key_Backtab) {
|
|
return false;
|
|
}
|
|
const auto modifiers = e->modifiers()
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
if (modifiers != Qt::NoModifier && modifiers != Qt::ShiftModifier) {
|
|
return false;
|
|
}
|
|
const auto forward = (key != Qt::Key_Backtab)
|
|
&& (modifiers != Qt::ShiftModifier);
|
|
if (!moveTabBoundary(forward)) {
|
|
return false;
|
|
}
|
|
e->accept();
|
|
return true;
|
|
}
|
|
|
|
bool Widget::moveBoundary(bool forward, bool allowTrailing) {
|
|
const auto target = forward
|
|
? _state->nextEditableOrdinal()
|
|
: _state->previousEditableOrdinal();
|
|
const auto addTrailing = forward
|
|
&& allowTrailing
|
|
&& !target
|
|
&& !_state->isActiveTopLevelParagraph();
|
|
if (!target && !addTrailing) {
|
|
return false;
|
|
}
|
|
auto handled = false;
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
if (target) {
|
|
if (committed == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
if (forward) {
|
|
activateTextOrdinal(*target, 0);
|
|
} else {
|
|
activateTextOrdinalAtEnd(*target);
|
|
}
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
const auto ordinal = _state->ensureTrailingParagraphActive();
|
|
if (!ordinal) {
|
|
handled = forward
|
|
&& allowTrailing
|
|
&& _state->lastLimitError().has_value();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(*ordinal, 0);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
return handled;
|
|
}
|
|
|
|
bool Widget::moveBoundaryAfterCommit(
|
|
ApplyResult committed,
|
|
bool forward,
|
|
bool allowTrailing,
|
|
bool *mutated) {
|
|
if (mutated) {
|
|
*mutated = false;
|
|
}
|
|
const auto target = forward
|
|
? _state->nextEditableOrdinal()
|
|
: _state->previousEditableOrdinal();
|
|
if (target) {
|
|
if (committed == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
if (forward) {
|
|
activateTextOrdinal(*target, 0);
|
|
} else {
|
|
activateTextOrdinalAtEnd(*target);
|
|
}
|
|
return true;
|
|
}
|
|
if (forward && allowTrailing && !_state->isActiveTopLevelParagraph()) {
|
|
const auto ordinal = _state->ensureTrailingParagraphActive();
|
|
if (!ordinal) {
|
|
return _state->lastLimitError().has_value();
|
|
}
|
|
if (mutated) {
|
|
*mutated = true;
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinal(*ordinal, 0);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Widget::moveTabBoundary(bool forward) {
|
|
auto handled = false;
|
|
const auto target = forward
|
|
? _state->nextEditableOrdinal()
|
|
: _state->previousEditableOrdinal();
|
|
if (!target && (!forward || _state->isActiveTopLevelParagraph())) {
|
|
return false;
|
|
}
|
|
recordMutationTransaction([&] {
|
|
auto committed = ApplyResult::Unchanged;
|
|
if (!_field->isHidden()) {
|
|
committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
}
|
|
if (target) {
|
|
clearSelection();
|
|
if (committed == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
activateTextOrdinalAtEnd(*target);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
clearSelection();
|
|
const auto ordinal = _state->ensureTrailingParagraphActive();
|
|
if (!ordinal) {
|
|
handled = _state->lastLimitError().has_value();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
refreshPreparedContent();
|
|
activateTextOrdinalAtEnd(*ordinal);
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
return handled;
|
|
}
|
|
|
|
bool Widget::removeBoundaryOwner(bool forward) {
|
|
auto handled = false;
|
|
recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
const auto target = _state->activeBoundaryTarget(forward);
|
|
using BoundaryAction = State::BoundaryTarget::Action;
|
|
switch (target.action) {
|
|
case BoundaryAction::RemoveActiveOwner: {
|
|
_boundarySelectionOrigin = std::nullopt;
|
|
const auto adjacent = _state->removeActiveOwnerAndSelectAdjacent(
|
|
forward);
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
refreshPreparedContent();
|
|
if (adjacent) {
|
|
if (forward) {
|
|
activateTextOrdinal(*adjacent, 0);
|
|
} else {
|
|
activateTextOrdinalAtEnd(*adjacent);
|
|
}
|
|
} else {
|
|
activateInitialNode();
|
|
}
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
}
|
|
case BoundaryAction::Text:
|
|
_boundarySelectionOrigin = std::nullopt;
|
|
if (committed == ApplyResult::Changed) {
|
|
refreshAfterInlineFieldCommit(committed);
|
|
}
|
|
if (forward) {
|
|
activateTextOrdinal(target.textOrdinal, 0);
|
|
} else {
|
|
activateTextOrdinalAtEnd(target.textOrdinal);
|
|
}
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
case BoundaryAction::StructuralSelection:
|
|
setStructuralSelection(
|
|
target.structuralSelection,
|
|
BoundarySelectionOrigin{
|
|
.ordinal = _activeOrdinal,
|
|
.forward = forward,
|
|
});
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
_articleSelectionDrag = {};
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
refreshAfterInlineFieldCommit(committed);
|
|
update();
|
|
handled = true;
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = (committed == ApplyResult::Changed),
|
|
};
|
|
case BoundaryAction::None: {
|
|
auto mutated = false;
|
|
handled = moveBoundaryAfterCommit(
|
|
committed,
|
|
forward,
|
|
forward,
|
|
&mutated);
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = mutated || (committed == ApplyResult::Changed),
|
|
};
|
|
}
|
|
}
|
|
Unexpected("Boundary action.");
|
|
});
|
|
return handled;
|
|
}
|
|
|
|
void Widget::ensurePendingActivation() {
|
|
if (_pendingOrdinal < 0) {
|
|
_activeSegmentIndex = (_activeOrdinal >= 0)
|
|
? segmentIndexForEditableOrdinal(_activeOrdinal)
|
|
: _article->firstEditableSegmentIndex();
|
|
return;
|
|
}
|
|
const auto ordinal = _pendingOrdinal;
|
|
const auto cursorOffset = _pendingCursorOffset;
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
activateTextOrdinal(ordinal, cursorOffset);
|
|
}
|
|
|
|
void Widget::updateInlineFieldHeightOverride() {
|
|
if (_settingField
|
|
|| _field->isHidden()
|
|
|| _activeOrdinal < 0
|
|
|| !_article) {
|
|
return;
|
|
} else if (_syncingInlineFieldGeometry) {
|
|
_pendingHeightOverrideUpdate = true;
|
|
return;
|
|
} else if (articleRelayoutDeferralActive()) {
|
|
requestDeferredInlineFieldGeometry();
|
|
requestDeferredInlineFieldHeightOverride();
|
|
return;
|
|
}
|
|
if (_article->editableIndexForSegment(_activeSegmentIndex) < 0) {
|
|
clearArticleEditableHeightOverride();
|
|
return;
|
|
}
|
|
const auto segmentRect = fieldOuterRectForSegment(_activeSegmentIndex);
|
|
auto height = segmentRect.isEmpty()
|
|
? _field->height()
|
|
: std::max(_field->geometry().bottom() + 1 - segmentRect.y(), 1);
|
|
if (_activeSegmentIsDisplayMath) {
|
|
const auto blockRect = _article->displayMathBlockRect(
|
|
_activeSegmentIndex).translated(articleTopLeft());
|
|
if (!blockRect.isEmpty()) {
|
|
height = std::max(
|
|
_field->geometry().bottom() + 1 - blockRect.y(),
|
|
1);
|
|
}
|
|
height = std::max(height, _activeDisplayMathBaselineHeight);
|
|
}
|
|
_article->setEditableHeightOverrideForSegment(_activeSegmentIndex, height);
|
|
relayoutCurrentContent();
|
|
update();
|
|
}
|
|
|
|
void Widget::clearDisplayMathEditSession() {
|
|
_activeSegmentIsDisplayMath = false;
|
|
_activeDisplayMathBaselineHeight = 0;
|
|
}
|
|
|
|
void Widget::clearInlineFieldEditSession(
|
|
bool keepRetainedFieldOnCurrentHistoryEntry) {
|
|
clearDisplayMathEditSession();
|
|
if (_article) {
|
|
clearArticleEditableHeightOverride();
|
|
}
|
|
if (!_field->isHidden()
|
|
|| !_fieldLeaf) {
|
|
return;
|
|
}
|
|
const auto activeLeaf = _state->activeLeafPath();
|
|
if (!activeLeaf || (*activeLeaf != *_fieldLeaf)) {
|
|
const auto &fieldStyle = inlineFieldStyleFor(
|
|
Markdown::MarkdownArticleTextLeafStyle());
|
|
_activeFieldStyleKey = fieldStyle.key;
|
|
_fieldMode = State::FieldMode::Rich;
|
|
recreateInlineField(*fieldStyle.style);
|
|
return;
|
|
}
|
|
retainActiveLeafField(keepRetainedFieldOnCurrentHistoryEntry);
|
|
const auto &fieldStyle = inlineFieldStyleFor(
|
|
Markdown::MarkdownArticleTextLeafStyle());
|
|
_activeFieldStyleKey = fieldStyle.key;
|
|
_fieldMode = State::FieldMode::Rich;
|
|
recreateInlineField(*fieldStyle.style);
|
|
}
|
|
|
|
Widget::HistoryViewState Widget::captureHistoryViewState() const {
|
|
auto result = HistoryViewState();
|
|
if (!_field->isHidden()) {
|
|
const auto leaf = _state->activeLeafPath();
|
|
if (!leaf) {
|
|
return result;
|
|
}
|
|
const auto cursor = _field->textCursor();
|
|
const auto trimLeft = !_state->codeBlockLanguage(
|
|
_state->activeTextOrdinal()).has_value();
|
|
auto anchorOffset = 0;
|
|
auto cursorOffset = 0;
|
|
if (_state->activeFieldMode() == State::FieldMode::Raw) {
|
|
const auto trimmed = TrimInlineFieldText(
|
|
{ _state->activeRawText(), {} },
|
|
trimLeft);
|
|
const auto size = int(_state->activeRawText().size());
|
|
anchorOffset = std::clamp(cursor.anchor() + trimmed.left, 0, size);
|
|
cursorOffset = std::clamp(
|
|
cursor.position() + trimmed.left,
|
|
0,
|
|
size);
|
|
} else {
|
|
const auto activeText = ConvertRichTextToEditorTags(
|
|
_state->activeText());
|
|
const auto trimmed = TrimInlineFieldText(activeText.text, trimLeft);
|
|
const auto size = int(_state->activeText().text.size());
|
|
anchorOffset = std::clamp(
|
|
MapEditorOffsetToRichOffset(
|
|
activeText.replacements,
|
|
cursor.anchor() + trimmed.left),
|
|
0,
|
|
size);
|
|
cursorOffset = std::clamp(
|
|
MapEditorOffsetToRichOffset(
|
|
activeText.replacements,
|
|
cursor.position() + trimmed.left),
|
|
0,
|
|
size);
|
|
}
|
|
result.leafSelection = HistoryLeafSelection{
|
|
.leaf = *leaf,
|
|
.anchorOffset = anchorOffset,
|
|
.cursorOffset = cursorOffset,
|
|
};
|
|
} else if (hasStructuralSelection()) {
|
|
result.structuralSelection = _structuralSelection;
|
|
result.boundarySelectionOrigin = _boundarySelectionOrigin;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Widget::HistoryEntry Widget::captureHistoryEntry() const {
|
|
return {
|
|
.snapshot = _state->snapshot(),
|
|
.viewState = captureHistoryViewState(),
|
|
};
|
|
}
|
|
|
|
void Widget::restoreHistoryEntry(const HistoryEntry &entry) {
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
if (_article && (_horizontalScrollDrag != HorizontalScrollDrag::None)) {
|
|
_article->endHorizontalScroll();
|
|
}
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
_articleSelectionDrag = {};
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
_trackingPointerPress = false;
|
|
_horizontalScrollLock = std::nullopt;
|
|
_pressedControl = {};
|
|
_pressedControlPoint = std::nullopt;
|
|
_horizontalScrollDrag = HorizontalScrollDrag::None;
|
|
_pendingTouchHorizontalScrollPoint = std::nullopt;
|
|
|
|
const auto wasRestoring = _restoringHistory;
|
|
_restoringHistory = true;
|
|
const auto guard = gsl::finally([&] {
|
|
_restoringHistory = wasRestoring;
|
|
});
|
|
|
|
_state->restoreSnapshot(entry.snapshot);
|
|
refreshPreparedContent();
|
|
|
|
if (const auto &selection = entry.viewState.structuralSelection) {
|
|
_activeOrdinal = _state->activeTextOrdinal();
|
|
_activeSegmentIndex = -1;
|
|
_fieldLeaf = std::nullopt;
|
|
clearDisplayMathEditSession();
|
|
setStructuralSelection(
|
|
*selection,
|
|
entry.viewState.boundarySelectionOrigin);
|
|
hideInlineField();
|
|
update();
|
|
return;
|
|
}
|
|
if (const auto &leafSelection = entry.viewState.leafSelection) {
|
|
const auto ordinal = _state->textOrdinalForLeafPath(leafSelection->leaf);
|
|
if (ordinal >= 0) {
|
|
activateTextOrdinal(
|
|
ordinal,
|
|
leafSelection->anchorOffset,
|
|
leafSelection->cursorOffset);
|
|
return;
|
|
}
|
|
}
|
|
_activeOrdinal = _state->activeTextOrdinal();
|
|
_activeSegmentIndex = -1;
|
|
_fieldLeaf = std::nullopt;
|
|
clearDisplayMathEditSession();
|
|
hideInlineField();
|
|
update();
|
|
}
|
|
|
|
bool Widget::mutationTransactionChanged(bool changed) {
|
|
return changed;
|
|
}
|
|
|
|
bool Widget::mutationTransactionChanged(ApplyResult result) {
|
|
return (result == ApplyResult::Changed);
|
|
}
|
|
|
|
bool Widget::mutationTransactionChanged(
|
|
const MutationTransactionResult &result) {
|
|
return result.changed;
|
|
}
|
|
|
|
void Widget::finishMutationTransaction(
|
|
const HistoryEntry &before,
|
|
bool changed,
|
|
int beforeHistoryIndex,
|
|
uint64 beforeRetainToken) {
|
|
if (!changed) {
|
|
return;
|
|
}
|
|
const auto after = captureHistoryEntry();
|
|
if (SnapshotEquals(before.snapshot, after.snapshot)
|
|
&& (before.viewState == after.viewState)) {
|
|
return;
|
|
}
|
|
truncateHistoryRedo();
|
|
_history.push_back(after);
|
|
_historyIndex = int(_history.size()) - 1;
|
|
moveRetainedLeafFields(
|
|
beforeHistoryIndex,
|
|
_historyIndex,
|
|
beforeRetainToken);
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
void Widget::retainActiveLeafField(
|
|
bool keepRetainedFieldOnCurrentHistoryEntry) {
|
|
if (!_field) {
|
|
ensureInlineFieldCreated();
|
|
return;
|
|
} else if (!_fieldLeaf
|
|
|| !_activeFieldStyleKey) {
|
|
return;
|
|
}
|
|
const auto leaf = *_fieldLeaf;
|
|
if (_state->textOrdinalForLeafPath(leaf) < 0) {
|
|
return;
|
|
}
|
|
const auto wasSettingField = _settingField;
|
|
_settingField = true;
|
|
_field->hide();
|
|
_settingField = wasSettingField;
|
|
const auto historyIndex = _retainingFieldHistoryIndexOverride.value_or(
|
|
_historyIndex);
|
|
const auto &fieldStyle = inlineFieldStyleFor(
|
|
Markdown::MarkdownArticleTextLeafStyle());
|
|
auto replacement = base::make_unique_q<Ui::InputField>(
|
|
this,
|
|
*fieldStyle.style,
|
|
Ui::InputField::Mode::MultiLine,
|
|
rpl::single(QString()));
|
|
auto retained = RetainedLeafField{
|
|
.historyIndex = historyIndex,
|
|
.retainToken = keepRetainedFieldOnCurrentHistoryEntry
|
|
? _retainedLeafFieldToken
|
|
: ++_retainedLeafFieldToken,
|
|
.leaf = leaf,
|
|
.mode = _fieldMode,
|
|
.styleKey = _activeFieldStyleKey,
|
|
};
|
|
retained.field = std::move(_field);
|
|
_field = std::move(replacement);
|
|
_activeFieldStyleKey = std::nullopt;
|
|
_fieldMode = State::FieldMode::Rich;
|
|
_fieldLeaf = std::nullopt;
|
|
setupInlineField();
|
|
clearFieldUndoRedoNoopState();
|
|
for (auto i = _retainedLeafFields.begin(); i != _retainedLeafFields.end();) {
|
|
if ((i->historyIndex == retained.historyIndex)
|
|
&& (i->leaf == retained.leaf)
|
|
&& (i->mode == retained.mode)
|
|
&& (i->styleKey == retained.styleKey)) {
|
|
i = _retainedLeafFields.erase(i);
|
|
} else {
|
|
++i;
|
|
}
|
|
}
|
|
_retainedLeafFields.push_back(std::move(retained));
|
|
pruneRetainedLeafFields();
|
|
}
|
|
|
|
base::unique_qptr<Ui::InputField> Widget::reviveRetainedLeafField(
|
|
int historyIndex,
|
|
const State::LeafPath &leaf,
|
|
State::FieldMode mode,
|
|
const InlineFieldStyleKey &styleKey) {
|
|
for (auto i = int(_retainedLeafFields.size()) - 1; i >= 0; --i) {
|
|
if ((_retainedLeafFields[i].historyIndex == historyIndex)
|
|
&& (_retainedLeafFields[i].leaf == leaf)
|
|
&& (_retainedLeafFields[i].mode == mode)
|
|
&& _retainedLeafFields[i].styleKey
|
|
&& (*_retainedLeafFields[i].styleKey == styleKey)) {
|
|
auto result = std::move(_retainedLeafFields[i].field);
|
|
_retainedLeafFields.erase(_retainedLeafFields.begin() + i);
|
|
return result;
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void Widget::pruneRetainedLeafFields() {
|
|
for (auto i = _retainedLeafFields.begin(); i != _retainedLeafFields.end();) {
|
|
if (!i->field) {
|
|
i = _retainedLeafFields.erase(i);
|
|
} else {
|
|
++i;
|
|
}
|
|
}
|
|
while (int(_retainedLeafFields.size()) > kRetainedLeafFieldLimit) {
|
|
_retainedLeafFields.erase(_retainedLeafFields.begin());
|
|
}
|
|
}
|
|
|
|
void Widget::removeRetainedLeafFieldsAfter(int historyIndex) {
|
|
for (auto i = _retainedLeafFields.begin(); i != _retainedLeafFields.end();) {
|
|
if (i->historyIndex > historyIndex) {
|
|
i = _retainedLeafFields.erase(i);
|
|
} else {
|
|
++i;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Widget::moveRetainedLeafFields(
|
|
int fromHistoryIndex,
|
|
int toHistoryIndex,
|
|
uint64 afterRetainToken) {
|
|
if (fromHistoryIndex == toHistoryIndex) {
|
|
return;
|
|
}
|
|
for (auto &retained : _retainedLeafFields) {
|
|
if ((retained.historyIndex == fromHistoryIndex)
|
|
&& (retained.retainToken > afterRetainToken)) {
|
|
retained.historyIndex = toHistoryIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Widget::refreshAfterInlineFieldCommit(ApplyResult committed) {
|
|
refreshAfterInlineFieldCommit(
|
|
committed,
|
|
_state->activePreparedLeafSource());
|
|
}
|
|
|
|
void Widget::refreshAfterInlineFieldCommit(
|
|
ApplyResult committed,
|
|
std::optional<Markdown::PreparedEditLeafSource> source) {
|
|
switch ((committed == ApplyResult::Changed)
|
|
? _state->lastPreparedMutationKind()
|
|
: PreparedMutationKind::None) {
|
|
case PreparedMutationKind::LeafOnly:
|
|
if (source) {
|
|
refreshPreparedLeafAtSource(*source);
|
|
} else {
|
|
refreshPreparedContent();
|
|
}
|
|
break;
|
|
case PreparedMutationKind::FullRebuild:
|
|
refreshPreparedContent();
|
|
break;
|
|
case PreparedMutationKind::None:
|
|
relayoutCurrentContent();
|
|
break;
|
|
}
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
void Widget::ensureArticleLayoutForInlineField(int width) {
|
|
if (!_article || width <= 0) {
|
|
return;
|
|
} else if (articleRelayoutDeferralActive()) {
|
|
requestDeferredArticleRelayout();
|
|
requestDeferredInlineFieldGeometry();
|
|
return;
|
|
}
|
|
_articleHeight = _article->resizeGetHeight(articleWidth(width));
|
|
}
|
|
|
|
void Widget::syncArticleVisibleTopBottom() {
|
|
if (!_article) {
|
|
return;
|
|
}
|
|
const auto articleTop = articleTopLeft().y();
|
|
_article->setVisibleTopBottom(
|
|
_visibleRange.top - articleTop,
|
|
_visibleRange.bottom - articleTop);
|
|
}
|
|
|
|
void Widget::syncInlineFieldGeometry(int width) {
|
|
if (_field->isHidden() || width <= 0) {
|
|
return;
|
|
} else if (articleRelayoutDeferralActive()) {
|
|
requestDeferredInlineFieldGeometry();
|
|
return;
|
|
}
|
|
ensureArticleLayoutForInlineField(width);
|
|
if (_activeSegmentIndex >= 0) {
|
|
ensureInlineFieldForSegment(_activeSegmentIndex);
|
|
}
|
|
const auto segmentRect = fieldOuterRectForSegment(_activeSegmentIndex);
|
|
if (segmentRect.isEmpty()) {
|
|
_pendingOrdinal = _activeOrdinal;
|
|
_pendingCursorOffset = _field->textCursor().position();
|
|
hideInlineField();
|
|
clearArticleEditableHeightOverride();
|
|
return;
|
|
}
|
|
const auto margins = _field->fullTextMargins();
|
|
const auto left = segmentRect.x() - margins.left();
|
|
const auto top = segmentRect.y() - margins.top();
|
|
const auto fieldWidth = std::max(
|
|
segmentRect.width() + margins.left() + margins.right(),
|
|
1);
|
|
_syncingInlineFieldGeometry = true;
|
|
_field->resizeToWidth(fieldWidth);
|
|
const auto fieldHeight = FieldNaturalHeight(_field.get());
|
|
_field->setGeometryToLeft(left, top, fieldWidth, fieldHeight, width);
|
|
_field->raise();
|
|
_syncingInlineFieldGeometry = false;
|
|
if (_pendingHeightOverrideUpdate) {
|
|
_pendingHeightOverrideUpdate = false;
|
|
updateInlineFieldHeightOverride();
|
|
}
|
|
}
|
|
|
|
void Widget::setStructuralSelection(
|
|
Markdown::PreparedEditSelection selection,
|
|
std::optional<BoundarySelectionOrigin> origin) {
|
|
_structuralSelection = std::move(selection);
|
|
_boundarySelectionOrigin = std::move(origin);
|
|
notifyToolbarStateChanged();
|
|
}
|
|
|
|
bool Widget::broaderSelectionHasSelectedText() const {
|
|
const auto &nodes = _state->textNodes();
|
|
const auto &page = _state->richPage();
|
|
const auto hasTextSelection = !_selection.empty()
|
|
&& _selectionEndpoints.from.valid()
|
|
&& _selectionEndpoints.to.valid();
|
|
const auto normalizedSelection = hasTextSelection
|
|
? NormalizeSelection(_selection)
|
|
: Markdown::MarkdownArticleSelection();
|
|
for (auto ordinal = 0, count = int(nodes.size()); ordinal != count;
|
|
++ordinal) {
|
|
const auto &descriptor = nodes[ordinal];
|
|
if (descriptor.mode != State::FieldMode::Rich) {
|
|
continue;
|
|
}
|
|
const auto current = RichTextFromPath(page, descriptor.leaf);
|
|
if (!current || current->text.text.isEmpty()) {
|
|
continue;
|
|
}
|
|
const auto segmentIndex = segmentIndexForEditableOrdinal(ordinal);
|
|
if (segmentIndex < 0
|
|
|| (editableOrdinalForSegment(segmentIndex) != ordinal)) {
|
|
continue;
|
|
}
|
|
const auto length = int(current->text.text.size());
|
|
if (!_structuralSelection.empty()
|
|
&& LeafSelectedStructurally(
|
|
page,
|
|
descriptor.leaf,
|
|
_structuralSelection)
|
|
&& (length > 0)) {
|
|
return true;
|
|
}
|
|
if (!hasTextSelection
|
|
|| (normalizedSelection.from.segment > segmentIndex)
|
|
|| (normalizedSelection.to.segment < segmentIndex)) {
|
|
continue;
|
|
}
|
|
auto from = 0;
|
|
auto till = length;
|
|
if (normalizedSelection.from.segment == segmentIndex) {
|
|
from = normalizedSelection.from.offset;
|
|
}
|
|
if (normalizedSelection.to.segment == segmentIndex) {
|
|
till = normalizedSelection.to.offset;
|
|
}
|
|
from = std::clamp(from, 0, length);
|
|
till = std::clamp(till, from, length);
|
|
if (from < till) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::vector<State::TextNodeSpan> Widget::broaderSelectionTextSpans() const {
|
|
auto result = std::vector<State::TextNodeSpan>();
|
|
const auto &nodes = _state->textNodes();
|
|
const auto &page = _state->richPage();
|
|
const auto hasTextSelection = !_selection.empty()
|
|
&& _selectionEndpoints.from.valid()
|
|
&& _selectionEndpoints.to.valid();
|
|
const auto normalizedSelection = hasTextSelection
|
|
? NormalizeSelection(_selection)
|
|
: Markdown::MarkdownArticleSelection();
|
|
result.reserve(nodes.size());
|
|
for (auto ordinal = 0, count = int(nodes.size()); ordinal != count;
|
|
++ordinal) {
|
|
const auto &descriptor = nodes[ordinal];
|
|
if (descriptor.mode != State::FieldMode::Rich) {
|
|
continue;
|
|
}
|
|
const auto current = RichTextFromPath(page, descriptor.leaf);
|
|
if (!current || current->text.text.isEmpty()) {
|
|
continue;
|
|
}
|
|
const auto segmentIndex = segmentIndexForEditableOrdinal(ordinal);
|
|
if (segmentIndex < 0
|
|
|| (editableOrdinalForSegment(segmentIndex) != ordinal)) {
|
|
continue;
|
|
}
|
|
const auto length = int(current->text.text.size());
|
|
if (!_structuralSelection.empty()
|
|
&& LeafSelectedStructurally(
|
|
page,
|
|
descriptor.leaf,
|
|
_structuralSelection)) {
|
|
result.push_back({
|
|
.leaf = descriptor.leaf,
|
|
.from = 0,
|
|
.till = length,
|
|
});
|
|
continue;
|
|
}
|
|
if (!hasTextSelection
|
|
|| (normalizedSelection.from.segment > segmentIndex)
|
|
|| (normalizedSelection.to.segment < segmentIndex)) {
|
|
continue;
|
|
}
|
|
auto from = 0;
|
|
auto till = length;
|
|
if (normalizedSelection.from.segment == segmentIndex) {
|
|
from = normalizedSelection.from.offset;
|
|
}
|
|
if (normalizedSelection.to.segment == segmentIndex) {
|
|
till = normalizedSelection.to.offset;
|
|
}
|
|
from = std::clamp(from, 0, length);
|
|
till = std::clamp(till, from, length);
|
|
if (from < till) {
|
|
result.push_back({
|
|
.leaf = descriptor.leaf,
|
|
.from = from,
|
|
.till = till,
|
|
});
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::vector<State::BlockPath> Widget::broaderSelectionMediaBlocks() const {
|
|
auto result = std::vector<State::BlockPath>();
|
|
if (_structuralSelection.empty()) {
|
|
return result;
|
|
}
|
|
const auto &page = _state->richPage();
|
|
EnumerateBlockPaths(
|
|
page,
|
|
StateBlockContainerPath(),
|
|
[&](const StateBlockPath &path, const RichPage::Block &block) {
|
|
if (BlockSelectedStructurally(path, _structuralSelection)
|
|
&& MediaBlockSupportsSpoiler(block)) {
|
|
result.push_back(path);
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
void Widget::clearSelection() {
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| hasStructuralSelection()
|
|
|| _articleSelectionDrag.active;
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
_articleSelectionDrag = {};
|
|
if (changed) {
|
|
update();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
}
|
|
|
|
void Widget::clearTextSelection() {
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| (_articleSelectionDrag.active
|
|
&& _articleSelectionDrag.mode == DragSelectionMode::Text);
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Text) {
|
|
finishArticleSelection();
|
|
} else {
|
|
_articleSelectionDrag.textSegment = -1;
|
|
_articleSelectionDrag.textOffset = 0;
|
|
}
|
|
if (changed) {
|
|
update();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
}
|
|
|
|
void Widget::clearStructuralSelection() {
|
|
const auto changed = hasStructuralSelection()
|
|
|| (_articleSelectionDrag.active
|
|
&& _articleSelectionDrag.mode == DragSelectionMode::Structural);
|
|
setStructuralSelection({});
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Structural) {
|
|
finishArticleSelection();
|
|
}
|
|
if (changed) {
|
|
update();
|
|
notifyToolbarStateChanged();
|
|
}
|
|
}
|
|
|
|
bool Widget::hasStructuralSelection() const {
|
|
return !_structuralSelection.empty();
|
|
}
|
|
|
|
void Widget::startArticleSelection(
|
|
QPoint pressPoint,
|
|
QPoint globalPressPoint,
|
|
const Markdown::MarkdownArticleHitTestResult &hit,
|
|
const PreparedEditHit &editHit,
|
|
bool fromField,
|
|
bool startedBelow) {
|
|
const auto isTextHit = hit.valid()
|
|
&& !hit.codeHeaderCopy
|
|
&& hit.direct
|
|
&& _article->segmentIsText(hit.segmentIndex);
|
|
if (isTextHit) {
|
|
clearStructuralSelection();
|
|
} else {
|
|
clearTextSelection();
|
|
clearStructuralSelection();
|
|
}
|
|
_articleSelectionDrag = {
|
|
.active = true,
|
|
.fromField = fromField,
|
|
.startedBelow = startedBelow,
|
|
.codeHeader = hit.codeHeaderCopy,
|
|
.pressPoint = pressPoint,
|
|
.globalPressPoint = globalPressPoint,
|
|
.anchorHit = editHit,
|
|
.textSegment = -1,
|
|
.textOffset = 0,
|
|
.mode = DragSelectionMode::None,
|
|
};
|
|
if (!isTextHit) {
|
|
const auto mathFormulaHit = editHit.leaf
|
|
&& (editHit.leaf->kind == PreparedEditLeafKind::MathFormula);
|
|
if (editHit.valid()
|
|
&& !mathFormulaHit
|
|
&& !hit.codeHeaderCopy
|
|
&& !startedBelow) {
|
|
_articleSelectionDrag.mode = DragSelectionMode::Structural;
|
|
}
|
|
return;
|
|
}
|
|
const auto offset = _article->selectionOffsetFromHit(
|
|
hit,
|
|
TextSelectType::Letters);
|
|
_articleSelectionDrag.textSegment = hit.segmentIndex;
|
|
_articleSelectionDrag.textOffset = offset;
|
|
_articleSelectionDrag.mode = DragSelectionMode::Text;
|
|
_selection = {
|
|
{ hit.segmentIndex, offset },
|
|
{ hit.segmentIndex, offset },
|
|
};
|
|
_selectionEndpoints = {
|
|
.from = MakeSelectionEndpoint(hit),
|
|
.to = MakeSelectionEndpoint(hit),
|
|
};
|
|
update();
|
|
}
|
|
|
|
void Widget::updateArticleSelection(
|
|
QPoint articlePoint,
|
|
const Markdown::MarkdownArticleHitTestResult &hit,
|
|
const PreparedEditHit &editHit) {
|
|
if (!_articleSelectionDrag.active) {
|
|
return;
|
|
}
|
|
const auto dragSegment = _articleSelectionDrag.textSegment;
|
|
const auto originalMathFormulaHit = [&] {
|
|
return _articleSelectionDrag.anchorHit.leaf
|
|
&& (_articleSelectionDrag.anchorHit.leaf->kind
|
|
== Markdown::PreparedEditLeafKind::MathFormula)
|
|
&& editHit.leaf
|
|
&& (*editHit.leaf == *_articleSelectionDrag.anchorHit.leaf);
|
|
};
|
|
const auto directOriginalTextHit = [&] {
|
|
return (dragSegment >= 0)
|
|
&& hit.valid()
|
|
&& hit.direct
|
|
&& (hit.segmentIndex == dragSegment)
|
|
&& _article->segmentIsText(hit.segmentIndex);
|
|
};
|
|
const auto directOriginalEditableHit = [&] {
|
|
return ((dragSegment >= 0)
|
|
&& hit.valid()
|
|
&& hit.direct
|
|
&& (hit.segmentIndex == dragSegment)
|
|
&& _article->segmentIsEditable(hit.segmentIndex))
|
|
|| originalMathFormulaHit();
|
|
};
|
|
const auto updateTextSelection = [&](bool forceUpdate) {
|
|
const auto offset = _article->selectionOffsetFromHit(
|
|
hit,
|
|
TextSelectType::Letters);
|
|
const auto adjusted = _article->adjustSelection(
|
|
dragSegment,
|
|
TextSelection(
|
|
uint16(std::clamp(
|
|
std::min(_articleSelectionDrag.textOffset, offset),
|
|
0,
|
|
0xFFFF)),
|
|
uint16(std::clamp(
|
|
std::max(_articleSelectionDrag.textOffset, offset),
|
|
0,
|
|
0xFFFF))),
|
|
TextSelectType::Letters);
|
|
const auto selection = NormalizeSelection({
|
|
{ dragSegment, adjusted.from },
|
|
{ dragSegment, adjusted.to },
|
|
});
|
|
const auto endpoints = Markdown::MarkdownArticleSelectionEndpoints{
|
|
.from = _selectionEndpoints.from.valid()
|
|
? _selectionEndpoints.from
|
|
: Markdown::MarkdownArticleSelectionEndpoint{
|
|
dragSegment,
|
|
false },
|
|
.to = MakeSelectionEndpoint(hit),
|
|
};
|
|
const auto endpointsChanged
|
|
= (_selectionEndpoints.from.segment != endpoints.from.segment)
|
|
|| (_selectionEndpoints.from.direct != endpoints.from.direct)
|
|
|| (_selectionEndpoints.to.segment != endpoints.to.segment)
|
|
|| (_selectionEndpoints.to.direct != endpoints.to.direct);
|
|
if (_selection != selection || endpointsChanged || forceUpdate) {
|
|
_selection = selection;
|
|
_selectionEndpoints = endpoints;
|
|
update();
|
|
} else {
|
|
_selectionEndpoints = endpoints;
|
|
}
|
|
};
|
|
const auto clearFieldSelection = [&] {
|
|
if (!_articleSelectionDrag.fromField) {
|
|
return;
|
|
}
|
|
auto cursor = _field->textCursor();
|
|
if (!cursor.hasSelection()) {
|
|
return;
|
|
}
|
|
cursor.clearSelection();
|
|
_field->setTextCursor(cursor);
|
|
};
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Structural) {
|
|
if (directOriginalTextHit()) {
|
|
const auto forceUpdate = !_structuralSelection.empty();
|
|
setStructuralSelection({});
|
|
_articleSelectionDrag.mode = DragSelectionMode::Text;
|
|
updateTextSelection(forceUpdate);
|
|
return;
|
|
}
|
|
if (directOriginalEditableHit()) {
|
|
const auto changed = !_structuralSelection.empty();
|
|
setStructuralSelection({});
|
|
_articleSelectionDrag.mode = DragSelectionMode::None;
|
|
if (changed) {
|
|
update();
|
|
}
|
|
return;
|
|
}
|
|
const auto selection = structuralSelectionFromHits(
|
|
_articleSelectionDrag.anchorHit,
|
|
editHit);
|
|
if (_structuralSelection != selection) {
|
|
setStructuralSelection(selection);
|
|
update();
|
|
}
|
|
return;
|
|
} else if (_articleSelectionDrag.mode == DragSelectionMode::None) {
|
|
if (directOriginalEditableHit()) {
|
|
return;
|
|
}
|
|
if (!editHit.valid()
|
|
|| (_articleSelectionDrag.startedBelow
|
|
&& articlePoint.y() >= _articleHeight)) {
|
|
return;
|
|
}
|
|
_articleSelectionDrag.mode = DragSelectionMode::Structural;
|
|
const auto selection = structuralSelectionFromHits(
|
|
_articleSelectionDrag.anchorHit,
|
|
editHit);
|
|
if (_structuralSelection != selection) {
|
|
setStructuralSelection(selection);
|
|
update();
|
|
}
|
|
return;
|
|
}
|
|
if (_articleSelectionDrag.mode != DragSelectionMode::Text) {
|
|
return;
|
|
}
|
|
if (directOriginalTextHit()) {
|
|
updateTextSelection(false);
|
|
return;
|
|
}
|
|
const auto selection = structuralSelectionFromHits(
|
|
_articleSelectionDrag.anchorHit,
|
|
editHit);
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| (_structuralSelection != selection);
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection(selection);
|
|
_articleSelectionDrag.mode = DragSelectionMode::Structural;
|
|
clearFieldSelection();
|
|
if (changed) {
|
|
update();
|
|
}
|
|
}
|
|
|
|
void Widget::finishArticleSelection() {
|
|
_articleSelectionDrag = {};
|
|
}
|
|
|
|
bool Widget::handleStructuralSelectionKey(QKeyEvent *e) {
|
|
if (!hasStructuralSelection()) {
|
|
return false;
|
|
}
|
|
const auto key = e->key();
|
|
if (key == Qt::Key_Escape) {
|
|
clearStructuralSelection();
|
|
e->accept();
|
|
return true;
|
|
}
|
|
const auto modifiers = e->modifiers()
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
if (modifiers != Qt::NoModifier) {
|
|
return false;
|
|
}
|
|
const auto forward = (key == Qt::Key_Delete);
|
|
if (!forward && key != Qt::Key_Backspace) {
|
|
return false;
|
|
}
|
|
if (_state->canRemoveStructuralSelection(_structuralSelection)) {
|
|
removeStructuralSelectionAndReposition(forward);
|
|
}
|
|
e->accept();
|
|
return true;
|
|
}
|
|
|
|
void Widget::removeStructuralSelectionAndReposition(bool forward) {
|
|
const auto origin = [&]() -> std::optional<BoundarySelectionOrigin> {
|
|
if (_boundarySelectionOrigin
|
|
&& _boundarySelectionOrigin->forward == forward) {
|
|
return _boundarySelectionOrigin;
|
|
}
|
|
return std::nullopt;
|
|
}();
|
|
const auto target = removeCurrentStructuralSelection(forward);
|
|
if (hasStructuralSelection()) {
|
|
return;
|
|
}
|
|
auto activatedOrigin = false;
|
|
if (origin && _state->setActiveTextByOrdinal(origin->ordinal)) {
|
|
const auto cursor = origin->forward ? _state->activeTextLength() : 0;
|
|
activateTextOrdinal(origin->ordinal, cursor);
|
|
activatedOrigin = true;
|
|
}
|
|
if (!activatedOrigin) {
|
|
if (target) {
|
|
if (forward) {
|
|
activateTextOrdinal(*target, 0);
|
|
} else {
|
|
activateTextOrdinalAtEnd(*target);
|
|
}
|
|
} else {
|
|
activateInitialNode();
|
|
}
|
|
}
|
|
}
|
|
|
|
std::optional<int> Widget::removeCurrentStructuralSelection(bool forward) {
|
|
if (!hasStructuralSelection()) {
|
|
return std::nullopt;
|
|
}
|
|
const auto selection = _structuralSelection;
|
|
auto target = std::optional<int>();
|
|
const auto result = recordMutationTransaction([&] {
|
|
const auto committed = commitInlineField();
|
|
if (committed == ApplyResult::Failed) {
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.failed = true,
|
|
};
|
|
}
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
target = _state->removeStructuralSelection(selection, forward);
|
|
clearSelection();
|
|
refreshPreparedContent();
|
|
return MutationTransactionResult{
|
|
.committed = committed,
|
|
.changed = true,
|
|
};
|
|
});
|
|
if (result.failed) {
|
|
return std::nullopt;
|
|
}
|
|
return target;
|
|
}
|
|
|
|
bool Widget::handleFieldMouseEvent(QEvent *event) {
|
|
if (!_article || _field->isHidden() || _activeSegmentIndex < 0) {
|
|
return false;
|
|
}
|
|
const auto type = event->type();
|
|
const auto mouse = static_cast<QMouseEvent*>(event);
|
|
if (type == QEvent::MouseButtonPress) {
|
|
if (mouse->button() != Qt::LeftButton) {
|
|
return false;
|
|
}
|
|
const auto segmentRect = _article->segmentRect(_activeSegmentIndex);
|
|
if (segmentRect.isEmpty()) {
|
|
return false;
|
|
}
|
|
auto anchorHit = _article->editHitTest(segmentRect.center());
|
|
if (!anchorHit.valid()) {
|
|
anchorHit = _article->editHitTest(segmentRect.topLeft());
|
|
}
|
|
if (!anchorHit.valid()) {
|
|
return false;
|
|
}
|
|
clearTextSelection();
|
|
clearStructuralSelection();
|
|
const auto globalPoint = mouse->globalPos();
|
|
const auto articlePoint = mapFromGlobal(globalPoint)
|
|
- articleTopLeft();
|
|
const auto cursor = _field->textCursor();
|
|
_trackingPointerPress = true;
|
|
_articleSelectionDrag = {
|
|
.active = true,
|
|
.fromField = true,
|
|
.startedBelow = false,
|
|
.codeHeader = false,
|
|
.pressPoint = articlePoint,
|
|
.globalPressPoint = globalPoint,
|
|
.anchorHit = anchorHit,
|
|
.textSegment = _activeSegmentIndex,
|
|
.textOffset = std::clamp(
|
|
cursor.position(),
|
|
0,
|
|
int(_field->getLastText().size())),
|
|
.mode = DragSelectionMode::Text,
|
|
};
|
|
return false;
|
|
} else if (!_articleSelectionDrag.active
|
|
|| !_articleSelectionDrag.fromField) {
|
|
return false;
|
|
} else if (type == QEvent::MouseButtonRelease
|
|
&& mouse->button() != Qt::LeftButton) {
|
|
return false;
|
|
} else if (type == QEvent::MouseMove
|
|
&& !(mouse->buttons() & Qt::LeftButton)) {
|
|
finishArticleSelection();
|
|
_trackingPointerPress = false;
|
|
return false;
|
|
}
|
|
|
|
const auto globalPoint = mouse->globalPos();
|
|
const auto articlePoint = mapFromGlobal(globalPoint) - articleTopLeft();
|
|
const auto movedFarEnough = (globalPoint
|
|
- _articleSelectionDrag.globalPressPoint).manhattanLength()
|
|
>= QApplication::startDragDistance();
|
|
if (type == QEvent::MouseMove && !_articleSelectionDrag.dragStarted) {
|
|
if (!movedFarEnough) {
|
|
return false;
|
|
}
|
|
_articleSelectionDrag.dragStarted = true;
|
|
}
|
|
const auto clickLike = (type == QEvent::MouseButtonRelease)
|
|
&& !_articleSelectionDrag.dragStarted
|
|
&& !movedFarEnough;
|
|
const auto hit = _article->hitTest(
|
|
articlePoint,
|
|
Ui::Text::StateRequest::Flag::LookupSymbol);
|
|
const auto editHit = _article->editHitTest(articlePoint);
|
|
const auto insideActiveField = _field->rect().contains(
|
|
_field->mapFromGlobal(globalPoint));
|
|
const auto originalSegmentHit = hit.valid()
|
|
&& hit.direct
|
|
&& (hit.segmentIndex == _articleSelectionDrag.textSegment)
|
|
&& _article->segmentIsEditable(hit.segmentIndex);
|
|
const auto originalMathFormulaHit = _articleSelectionDrag.anchorHit.leaf
|
|
&& (_articleSelectionDrag.anchorHit.leaf->kind
|
|
== Markdown::PreparedEditLeafKind::MathFormula)
|
|
&& editHit.leaf
|
|
&& (*editHit.leaf == *_articleSelectionDrag.anchorHit.leaf);
|
|
const auto clearArticleSelection = [&] {
|
|
const auto changed = !_selection.empty()
|
|
|| _selectionEndpoints.from.valid()
|
|
|| _selectionEndpoints.to.valid()
|
|
|| hasStructuralSelection();
|
|
_selection = {};
|
|
_selectionEndpoints = {};
|
|
setStructuralSelection({});
|
|
if (changed) {
|
|
update();
|
|
}
|
|
};
|
|
if (insideActiveField || originalSegmentHit || originalMathFormulaHit) {
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Structural) {
|
|
clearArticleSelection();
|
|
_articleSelectionDrag.mode = DragSelectionMode::Text;
|
|
}
|
|
if (type == QEvent::MouseButtonRelease) {
|
|
finishArticleSelection();
|
|
_trackingPointerPress = false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (clickLike) {
|
|
finishArticleSelection();
|
|
_trackingPointerPress = false;
|
|
return false;
|
|
}
|
|
updateArticleSelection(articlePoint, hit, editHit);
|
|
if (type == QEvent::MouseButtonRelease) {
|
|
if (hasStructuralSelection()) {
|
|
const auto committed = recordMutationTransaction([&] {
|
|
return commitInlineField();
|
|
});
|
|
if (committed == ApplyResult::Failed) {
|
|
mouse->accept();
|
|
return true;
|
|
}
|
|
_pendingOrdinal = -1;
|
|
_pendingCursorOffset = 0;
|
|
hideInlineField();
|
|
clearInlineFieldEditSession();
|
|
refreshAfterInlineFieldCommit(committed);
|
|
finishArticleSelection();
|
|
_trackingPointerPress = false;
|
|
mouse->accept();
|
|
return true;
|
|
}
|
|
finishArticleSelection();
|
|
_trackingPointerPress = false;
|
|
return false;
|
|
}
|
|
if (_articleSelectionDrag.mode == DragSelectionMode::Structural) {
|
|
mouse->accept();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
PreparedEditSelection Widget::structuralSelectionFromHits(
|
|
const PreparedEditHit &anchor,
|
|
const PreparedEditHit &focus) const {
|
|
const auto anchorOwner = StructuralOwnerFromHit(anchor);
|
|
const auto focusOwner = StructuralOwnerFromHit(focus);
|
|
if (!anchorOwner.valid() || !focusOwner.valid()) {
|
|
return {};
|
|
}
|
|
const auto anchorCell = TableCellFromOwner(anchorOwner);
|
|
const auto focusCell = TableCellFromOwner(focusOwner);
|
|
if (anchorCell && focusCell) {
|
|
const auto range = TableRangesUnion(
|
|
TableRangeFromCell(*anchorCell),
|
|
TableRangeFromCell(*focusCell));
|
|
if (!range.empty()
|
|
&& TableRangeContainsCell(range, *anchorCell)
|
|
&& TableRangeContainsCell(range, *focusCell)) {
|
|
return {
|
|
.kind = PreparedEditSelectionKind::TableCells,
|
|
.tableCells = range,
|
|
};
|
|
}
|
|
}
|
|
const auto anchorRow = TableRowFromOwner(anchorOwner);
|
|
const auto focusRow = TableRowFromOwner(focusOwner);
|
|
if (anchorRow
|
|
&& focusRow
|
|
&& SamePreparedEditBlockPath(anchorRow->block, focusRow->block)) {
|
|
const auto range = NormalizeIntegerRange(
|
|
anchorRow->tableRowIndex,
|
|
focusRow->tableRowIndex);
|
|
if (!range.empty()) {
|
|
return {
|
|
.kind = PreparedEditSelectionKind::TableRows,
|
|
.tableRows = {
|
|
.block = anchorRow->block,
|
|
.from = range.from,
|
|
.till = range.till,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
const auto anchorListItem = ListItemFromOwner(anchorOwner);
|
|
const auto focusListItem = ListItemFromOwner(focusOwner);
|
|
if (anchorListItem
|
|
&& focusListItem
|
|
&& SamePreparedEditBlockPath(
|
|
anchorListItem->block,
|
|
focusListItem->block)) {
|
|
const auto range = NormalizeIntegerRange(
|
|
anchorListItem->listItemIndex,
|
|
focusListItem->listItemIndex);
|
|
if (!range.empty()) {
|
|
return {
|
|
.kind = PreparedEditSelectionKind::ListItems,
|
|
.listItems = {
|
|
.block = anchorListItem->block,
|
|
.from = range.from,
|
|
.till = range.till,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
const auto anchorBlock = BlockPathFromOwner(anchorOwner);
|
|
const auto focusBlock = BlockPathFromOwner(focusOwner);
|
|
if (!anchorBlock || !focusBlock) {
|
|
return {};
|
|
}
|
|
if (ComparePreparedEditBlockContainerPaths(
|
|
anchorBlock->container,
|
|
focusBlock->container) == 0) {
|
|
const auto blockSelection = BlockSelectionFromIndexes(
|
|
anchorBlock->container,
|
|
anchorBlock->index,
|
|
focusBlock->index);
|
|
if (!blockSelection.empty()) {
|
|
return blockSelection;
|
|
}
|
|
}
|
|
const auto listItemsFromChildren = ListItemSelectionFromSources(
|
|
ListItemSourcesFromOwner(anchorOwner, anchorBlock),
|
|
ListItemSourcesFromOwner(focusOwner, focusBlock));
|
|
const auto liftedBlockSelection = LiftedBlockSelection(
|
|
*anchorBlock,
|
|
*focusBlock);
|
|
if (IsBlockOwner(anchorOwner)
|
|
&& IsBlockOwner(focusOwner)
|
|
&& !liftedBlockSelection.empty()
|
|
&& !IsMultiListItemSelection(listItemsFromChildren)) {
|
|
return liftedBlockSelection;
|
|
}
|
|
if (!listItemsFromChildren.empty()) {
|
|
return listItemsFromChildren;
|
|
}
|
|
if (!liftedBlockSelection.empty()) {
|
|
return liftedBlockSelection;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
int Widget::editableOrdinalForSegment(int segmentIndex) const {
|
|
return _article->editableIndexForSegment(segmentIndex);
|
|
}
|
|
|
|
int Widget::segmentIndexForEditableOrdinal(int ordinal) const {
|
|
return _article->segmentIndexForEditableIndex(ordinal);
|
|
}
|
|
|
|
QPoint Widget::articleTopLeft() const {
|
|
const auto padding = EditorBodyPadding();
|
|
const auto outerWidth = std::max(widthNoMargins(), 1);
|
|
const auto available = std::max(
|
|
outerWidth - padding.left() - padding.right(),
|
|
1);
|
|
const auto bodyWidth = articleWidth(outerWidth);
|
|
return {
|
|
padding.left() + std::max((available - bodyWidth) / 2, 0),
|
|
padding.top()
|
|
};
|
|
}
|
|
|
|
int Widget::articleWidth(int outerWidth) const {
|
|
const auto padding = EditorBodyPadding();
|
|
const auto available = std::max(
|
|
outerWidth - padding.left() - padding.right(),
|
|
1);
|
|
return _article ? std::min(available, _article->maxWidth()) : available;
|
|
}
|
|
|
|
QRect Widget::outerEditableSegmentRect(int segmentIndex) const {
|
|
const auto rect = _article->logicalSegmentRect(segmentIndex);
|
|
return rect.isEmpty() ? rect : rect.translated(articleTopLeft());
|
|
}
|
|
|
|
QRect Widget::fieldOuterRectForSegment(int segmentIndex) const {
|
|
if (!_article || segmentIndex < 0) {
|
|
return QRect();
|
|
}
|
|
if (!_activeSegmentIsDisplayMath) {
|
|
return outerEditableSegmentRect(segmentIndex);
|
|
}
|
|
const auto rect = _article->displayMathEditRect(segmentIndex);
|
|
return rect.isEmpty() ? rect : rect.translated(articleTopLeft());
|
|
}
|
|
|
|
Markdown::MarkdownArticlePaintContext Widget::textPaintContext(QRect clip) {
|
|
const auto logicalRect = QRect(QPoint(), QSize(
|
|
articleWidth(std::max(widthNoMargins(), 1)),
|
|
std::max(_articleHeight, 1)));
|
|
auto context = Markdown::MarkdownArticlePaintContext(
|
|
_theme->preparePaintContext(
|
|
_style.get(),
|
|
logicalRect,
|
|
logicalRect,
|
|
clip,
|
|
window() ? !window()->isActiveWindow() : false));
|
|
const auto messageStyle = context.messageStyle();
|
|
context.caches = {
|
|
.pre = messageStyle->preCache.get(),
|
|
.blockquote = context.quoteCache({}, 0),
|
|
.colors = _highlightColors,
|
|
.st = &messageStyle->richPageStyle,
|
|
.repaint = [=] {
|
|
crl::on_main(this, [=] {
|
|
update();
|
|
});
|
|
},
|
|
.repaintRect = [=](QRect rect) {
|
|
crl::on_main(this, [=] {
|
|
if (rect.isEmpty()) {
|
|
update();
|
|
} else {
|
|
update(rect.translated(articleTopLeft()));
|
|
}
|
|
});
|
|
},
|
|
};
|
|
const auto hiddenSegmentIndex = _field->isHidden()
|
|
? -1
|
|
: _activeSegmentIndex;
|
|
context.hiddenTextSegmentIndex = hiddenSegmentIndex;
|
|
context.hiddenSegmentIndex = hiddenSegmentIndex;
|
|
context.selectionState.selection = _selection;
|
|
context.selectionState.endpoints = &_selectionEndpoints;
|
|
if (!_structuralSelection.empty()) {
|
|
context.selectionState.structuralSelection = &_structuralSelection;
|
|
}
|
|
return context;
|
|
}
|
|
|
|
} // namespace Iv::Editor
|