From 5e9d12d1d9793afe0099fc8b68eeae1b8bbe8098 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 13 Sep 2017 11:12:30 +0200 Subject: Extract html or plaintext depending on setting --- framework/qml/TextEditor.qml | 2 +- framework/src/domain/documenthandler.cpp | 26 ++++++++++++++++++++++++++ framework/src/domain/documenthandler.h | 6 ++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/framework/qml/TextEditor.qml b/framework/qml/TextEditor.qml index 047c9b77..8a298989 100644 --- a/framework/qml/TextEditor.qml +++ b/framework/qml/TextEditor.qml @@ -46,7 +46,7 @@ FocusScope { selectionStart: edit.selectionStart selectionEnd: edit.selectionEnd //textColor: colorDialog.color - onTextChanged: root.text = text + onTextChanged: root.htmlEnabled ? root.text = htmlText : root.text = plainText } Kube.ScrollHelper { diff --git a/framework/src/domain/documenthandler.cpp b/framework/src/domain/documenthandler.cpp index 0f949da8..3589fc9f 100644 --- a/framework/src/domain/documenthandler.cpp +++ b/framework/src/domain/documenthandler.cpp @@ -79,10 +79,36 @@ void DocumentHandler::setDocument(QQuickTextDocument *document) { if (document != m_document) { m_document = document; + connect(m_document->textDocument(), &QTextDocument::contentsChanged, this, [this] () { + emit textChanged(); + }); emit documentChanged(); } } +static QString stripInvisibleChars(const QString &s) +{ + auto text = s; + text.replace("\u2063", ""); + return text; +} + +QString DocumentHandler::plainText() const +{ + if (m_document) { + return stripInvisibleChars(m_document->textDocument()->toPlainText()); + } + return {}; +} + +QString DocumentHandler::htmlText() const +{ + if (m_document) { + return stripInvisibleChars(m_document->textDocument()->toHtml()); + } + return {}; +} + int DocumentHandler::cursorPosition() const { return m_cursorPosition; diff --git a/framework/src/domain/documenthandler.h b/framework/src/domain/documenthandler.h index 24b3b35f..0c1bf971 100644 --- a/framework/src/domain/documenthandler.h +++ b/framework/src/domain/documenthandler.h @@ -80,12 +80,18 @@ class DocumentHandler : public QObject Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) + Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged) + Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged) + public: explicit DocumentHandler(QObject *parent = nullptr); QQuickTextDocument *document() const; void setDocument(QQuickTextDocument *document); + QString plainText() const; + QString htmlText() const; + int cursorPosition() const; void setCursorPosition(int position); -- cgit v1.2.3