Add automatic markdown formatting

This commit is contained in:
James Graham
2025-11-06 20:46:51 +00:00
parent 11bf741554
commit 4db1e1c437
16 changed files with 801 additions and 313 deletions

View File

@@ -84,7 +84,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.bold
onClicked: {
root.focusedDocumentHandler.bold = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Bold);
root.clicked()
}
@@ -105,7 +105,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.italic
onClicked: {
root.focusedDocumentHandler.italic = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Italic);
root.clicked()
}
@@ -126,7 +126,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.underline
onClicked: {
root.focusedDocumentHandler.underline = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Underline);
root.clicked();
}
@@ -142,7 +142,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.strikethrough
onClicked: {
root.focusedDocumentHandler.strikethrough = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Strikethrough);
root.clicked()
}
@@ -174,7 +174,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.bold
onTriggered: {
root.focusedDocumentHandler.bold = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Bold);
root.clicked();
}
}
@@ -184,7 +184,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.italic
onTriggered: {
root.focusedDocumentHandler.italic = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Italic);
root.clicked();
}
}
@@ -194,7 +194,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.underline
onTriggered: {
root.focusedDocumentHandler.underline = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Underline);
root.clicked();
}
}
@@ -204,7 +204,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.strikethrough
onTriggered: {
root.focusedDocumentHandler.strikethrough = checked;
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.Strikethrough);
root.clicked();
}
}
@@ -229,7 +229,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.currentListStyle === 1
onClicked: {
root.focusedDocumentHandler.setListStyle(root.focusedDocumentHandler.currentListStyle === 1 ? 0 : 1);
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.UnorderedList);
root.clicked();
}
@@ -245,7 +245,7 @@ QQC2.ToolBar {
checkable: true
checked: root.focusedDocumentHandler.currentListStyle === 4
onClicked: {
root.focusedDocumentHandler.setListStyle(root.focusedDocumentHandler.currentListStyle === 4 ? 0 : 4);
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.OrderedList);
root.clicked();
}
@@ -303,7 +303,7 @@ QQC2.ToolBar {
icon.name: "format-list-unordered"
text: i18nc("@action:button", "Unordered List")
onTriggered: {
root.focusedDocumentHandler.setListStyle(root.focusedDocumentHandler.currentListStyle === 1 ? 0 : 1);
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.UnorderedList);
root.clicked();
}
}
@@ -311,7 +311,7 @@ QQC2.ToolBar {
icon.name: "format-list-ordered"
text: i18nc("@action:button", "Ordered List")
onTriggered: {
root.focusedDocumentHandler.setListStyle(root.focusedDocumentHandler.currentListStyle === 4 ? 0 : 4);
root.focusedDocumentHandler.setFormat(LibNeoChat.RichFormat.OrderedList);
root.clicked();
}
}

View File

@@ -36,18 +36,18 @@ QQC2.Popup {
leftPadding: lineRow.visible ? lineRow.width + lineRow.anchors.leftMargin + Kirigami.Units.smallSpacing : Kirigami.Units.largeSpacing
verticalAlignment: Text.AlignVCenter
enabled: root.chatContentModel.focusType !== LibNeoChat.MessageComponentType.Code || styleDelegate.index === LibNeoChat.TextStyle.Paragraph || styleDelegate.index === LibNeoChat.TextStyle.Quote
enabled: root.chatContentModel.focusType !== LibNeoChat.MessageComponentType.Code || styleDelegate.index === LibNeoChat.RichFormat.Paragraph || styleDelegate.index === LibNeoChat.RichFormat.Quote
readOnly: true
selectByMouse: false
onPressed: (event) => {
if (styleDelegate.index === LibNeoChat.TextStyle.Paragraph ||
styleDelegate.index === LibNeoChat.TextStyle.Code ||
styleDelegate.index === LibNeoChat.TextStyle.Quote
if (styleDelegate.index === LibNeoChat.RichFormat.Paragraph ||
styleDelegate.index === LibNeoChat.RichFormat.Code ||
styleDelegate.index === LibNeoChat.RichFormat.Quote
) {
root.chatContentModel.insertStyleAtCursor(styleDelegate.index);
} else {
root.focusedDocumentHandler.style = styleDelegate.index;
root.focusedDocumentHandler.setFormat(styleDelegate.index);
}
root.close();
}
@@ -61,7 +61,7 @@ QQC2.Popup {
leftMargin: Kirigami.Units.smallSpacing
}
visible: styleDelegate.index === LibNeoChat.TextStyle.Code
visible: styleDelegate.index === LibNeoChat.RichFormat.Code
QQC2.Label {
horizontalAlignment: Text.AlignRight
@@ -82,7 +82,7 @@ QQC2.Popup {
background: Rectangle {
color: Kirigami.Theme.backgroundColor
Kirigami.Theme.colorSet: styleDelegate.index === LibNeoChat.TextStyle.Quote ? Kirigami.Theme.Window : Kirigami.Theme.View
Kirigami.Theme.colorSet: styleDelegate.index === LibNeoChat.RichFormat.Quote ? Kirigami.Theme.Window : Kirigami.Theme.View
Kirigami.Theme.inherit: false
radius: Kirigami.Units.cornerRadius
border {

View File

@@ -7,7 +7,7 @@
#include <QTextCursor>
#include <QTextDocument>
#include "enums/textstyle.h"
#include "enums/richformat.h"
StyleDelegateHelper::StyleDelegateHelper(QObject *parent)
: QObject(parent)
@@ -59,8 +59,8 @@ void StyleDelegateHelper::formatDocument()
cursor.beginEditBlock();
cursor.select(QTextCursor::Document);
cursor.removeSelectedText();
const auto style = static_cast<TextStyle::Style>(m_textItem->property("index").toInt());
const auto string = TextStyle::styleString(style);
const auto style = static_cast<RichFormat::Format>(m_textItem->property("index").toInt());
const auto string = RichFormat::styleString(style);
const int headingLevel = style <= 6 ? style : 0;
// Apparently, 5 is maximum for FontSizeAdjustment; otherwise level=1 and
@@ -74,9 +74,9 @@ void StyleDelegateHelper::formatDocument()
QTextCharFormat chrfmt;
chrfmt.setFontWeight(headingLevel > 0 ? QFont::Bold : QFont::Normal);
chrfmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment / 2);
if (style == TextStyle::Code) {
if (style == RichFormat::Code) {
chrfmt.setFontFamilies({u"monospace"_s});
} else if (style == TextStyle::Quote) {
} else if (style == RichFormat::Quote) {
chrfmt.setFontItalic(true);
}