mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 22:09:52 +08:00
refine image insert selection logics
This commit is contained in:
parent
ccd3c55cad
commit
69c14394ac
@ -9,8 +9,10 @@ VInsertLinkDialog::VInsertLinkDialog(const QString &p_title,
|
|||||||
const QString &p_info,
|
const QString &p_info,
|
||||||
const QString &p_linkText,
|
const QString &p_linkText,
|
||||||
const QString &p_linkUrl,
|
const QString &p_linkUrl,
|
||||||
|
bool p_linkTextEmptyAllowed,
|
||||||
QWidget *p_parent)
|
QWidget *p_parent)
|
||||||
: QDialog(p_parent)
|
: QDialog(p_parent),
|
||||||
|
m_linkTextEmptyAllowed(p_linkTextEmptyAllowed)
|
||||||
{
|
{
|
||||||
setupUI(p_title, p_text, p_info, p_linkText, p_linkUrl);
|
setupUI(p_title, p_text, p_info, p_linkText, p_linkUrl);
|
||||||
|
|
||||||
@ -80,7 +82,8 @@ void VInsertLinkDialog::setupUI(const QString &p_title,
|
|||||||
void VInsertLinkDialog::handleInputChanged()
|
void VInsertLinkDialog::handleInputChanged()
|
||||||
{
|
{
|
||||||
bool textOk = true;
|
bool textOk = true;
|
||||||
if (m_linkTextEdit->getEvaluatedText().isEmpty()) {
|
if (m_linkTextEdit->getEvaluatedText().isEmpty()
|
||||||
|
&& !m_linkTextEmptyAllowed) {
|
||||||
textOk = false;
|
textOk = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ public:
|
|||||||
const QString &p_info,
|
const QString &p_info,
|
||||||
const QString &p_linkText,
|
const QString &p_linkText,
|
||||||
const QString &p_linkUrl,
|
const QString &p_linkUrl,
|
||||||
|
bool p_linkTextEmptyAllowed,
|
||||||
QWidget *p_parent = nullptr);
|
QWidget *p_parent = nullptr);
|
||||||
|
|
||||||
QString getLinkText() const;
|
QString getLinkText() const;
|
||||||
@ -45,6 +46,8 @@ private:
|
|||||||
VLineEdit *m_linkUrlEdit;
|
VLineEdit *m_linkUrlEdit;
|
||||||
|
|
||||||
QDialogButtonBox *m_btnBox;
|
QDialogButtonBox *m_btnBox;
|
||||||
|
|
||||||
|
bool m_linkTextEmptyAllowed;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VINSERTLINKDIALOG_H
|
#endif // VINSERTLINKDIALOG_H
|
||||||
|
@ -394,6 +394,7 @@ void VEditor::insertLink()
|
|||||||
"",
|
"",
|
||||||
linkText,
|
linkText,
|
||||||
linkUrl,
|
linkUrl,
|
||||||
|
false,
|
||||||
m_editor);
|
m_editor);
|
||||||
if (dialog.exec() == QDialog::Accepted) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
linkText = dialog.getLinkText();
|
linkText = dialog.getLinkText();
|
||||||
|
@ -1050,3 +1050,16 @@ bool VMdEditOperations::insertLink(const QString &p_linkText,
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VMdEditOperations::insertImageLink(const QString &p_linkText,
|
||||||
|
const QString &p_linkUrl)
|
||||||
|
{
|
||||||
|
QString link = QString("").arg(p_linkText).arg(p_linkUrl);
|
||||||
|
QTextCursor cursor = m_editor->textCursorW();
|
||||||
|
cursor.insertText(link);
|
||||||
|
m_editor->setTextCursorW(cursor);
|
||||||
|
|
||||||
|
setVimMode(VimMode::Insert);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -32,6 +32,8 @@ public:
|
|||||||
// If it is Vim Normal mode, change to Insert mode first.
|
// If it is Vim Normal mode, change to Insert mode first.
|
||||||
void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
|
void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
bool insertImageLink(const QString &p_linkText, const QString &p_linkUrl);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Insert image from @oriImagePath as @path.
|
// Insert image from @oriImagePath as @path.
|
||||||
// @folderInLink: the folder part in the image link.
|
// @folderInLink: the folder part in the image link.
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include "utils/viconutils.h"
|
#include "utils/viconutils.h"
|
||||||
#include "dialog/vcopytextashtmldialog.h"
|
#include "dialog/vcopytextashtmldialog.h"
|
||||||
#include "utils/vwebutils.h"
|
#include "utils/vwebutils.h"
|
||||||
|
#include "dialog/vinsertlinkdialog.h"
|
||||||
|
|
||||||
extern VWebUtils *g_webUtils;
|
extern VWebUtils *g_webUtils;
|
||||||
|
|
||||||
@ -728,28 +729,57 @@ bool VMdEditor::canInsertFromMimeData(const QMimeData *p_source) const
|
|||||||
|
|
||||||
void VMdEditor::insertFromMimeData(const QMimeData *p_source)
|
void VMdEditor::insertFromMimeData(const QMimeData *p_source)
|
||||||
{
|
{
|
||||||
VSelectDialog dialog(tr("Insert From Clipboard"), this);
|
|
||||||
dialog.addSelection(tr("Insert As Image"), 0);
|
|
||||||
dialog.addSelection(tr("Insert As Text"), 1);
|
|
||||||
|
|
||||||
if (p_source->hasHtml()) {
|
if (p_source->hasHtml()) {
|
||||||
// Handle <img>.
|
// Handle <img>.
|
||||||
QRegExp reg("<img ([^>]*)src=\"([^\"]+)\"([^>]*)>");
|
QRegExp reg("<img ([^>]*)src=\"([^\"]+)\"([^>]*)>");
|
||||||
if (reg.indexIn(p_source->html()) != -1) {
|
if (reg.indexIn(p_source->html()) != -1) {
|
||||||
|
if (p_source->hasImage()) {
|
||||||
|
// Both image data and URL are embedded.
|
||||||
|
VSelectDialog dialog(tr("Insert From Clipboard"), this);
|
||||||
|
dialog.addSelection(tr("Insert From URL"), 0);
|
||||||
|
dialog.addSelection(tr("Insert From Image Data"), 1);
|
||||||
|
dialog.addSelection(tr("Insert As Image Link"), 2);
|
||||||
|
|
||||||
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
|
int selection = dialog.getSelection();
|
||||||
|
if (selection == 1) {
|
||||||
|
// Insert from image data.
|
||||||
|
m_editOps->insertImageFromMimeData(p_source);
|
||||||
|
return;
|
||||||
|
} else if (selection == 2) {
|
||||||
|
// Insert as link.
|
||||||
|
insertImageLink("", reg.cap(2));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_editOps->insertImageFromURL(QUrl(reg.cap(2)));
|
m_editOps->insertImageFromURL(QUrl(reg.cap(2)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VSelectDialog dialog(tr("Insert From Clipboard"), this);
|
||||||
|
dialog.addSelection(tr("Insert As Image"), 0);
|
||||||
|
dialog.addSelection(tr("Insert As Text"), 1);
|
||||||
|
dialog.addSelection(tr("Insert As Image Link"), 2);
|
||||||
|
|
||||||
if (p_source->hasImage()) {
|
if (p_source->hasImage()) {
|
||||||
// Image data in the clipboard
|
// Image data in the clipboard
|
||||||
if (p_source->hasText()) {
|
if (p_source->hasText()) {
|
||||||
if (dialog.exec() == QDialog::Accepted) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
if (dialog.getSelection() == 1) {
|
int selection = dialog.getSelection();
|
||||||
|
if (selection == 1) {
|
||||||
// Insert as text.
|
// Insert as text.
|
||||||
Q_ASSERT(p_source->hasText() && p_source->hasImage());
|
Q_ASSERT(p_source->hasText() && p_source->hasImage());
|
||||||
VTextEdit::insertFromMimeData(p_source);
|
VTextEdit::insertFromMimeData(p_source);
|
||||||
return;
|
return;
|
||||||
|
} else if (selection == 2) {
|
||||||
|
// Insert as link.
|
||||||
|
insertImageLink("", p_source->text());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
@ -765,10 +795,15 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
|
|||||||
if (urls.size() == 1 && VUtils::isImageURL(urls[0])) {
|
if (urls.size() == 1 && VUtils::isImageURL(urls[0])) {
|
||||||
if (dialog.exec() == QDialog::Accepted) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
// FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
|
// FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
|
||||||
if (dialog.getSelection() == 0) {
|
int selection = dialog.getSelection();
|
||||||
|
if (selection == 0) {
|
||||||
// Insert as image.
|
// Insert as image.
|
||||||
m_editOps->insertImageFromURL(urls[0]);
|
m_editOps->insertImageFromURL(urls[0]);
|
||||||
return;
|
return;
|
||||||
|
} else if (selection == 2) {
|
||||||
|
// Insert as link.
|
||||||
|
insertImageLink("", urls[0].toString(QUrl::FullyEncoded));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QMimeData newSource;
|
QMimeData newSource;
|
||||||
@ -786,12 +821,18 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
|
|||||||
if (VUtils::isImageURLText(text)) {
|
if (VUtils::isImageURLText(text)) {
|
||||||
// The text is a URL to an image.
|
// The text is a URL to an image.
|
||||||
if (dialog.exec() == QDialog::Accepted) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
if (dialog.getSelection() == 0) {
|
int selection = dialog.getSelection();
|
||||||
|
if (selection == 0) {
|
||||||
// Insert as image.
|
// Insert as image.
|
||||||
QUrl url(text);
|
QUrl url(text);
|
||||||
if (url.isValid()) {
|
if (url.isValid()) {
|
||||||
m_editOps->insertImageFromURL(QUrl(text));
|
m_editOps->insertImageFromURL(QUrl(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} else if (selection == 2) {
|
||||||
|
// Insert as link.
|
||||||
|
insertImageLink("", text);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1175,3 +1216,20 @@ void VMdEditor::initCopyAsMenu(QAction *p_before, QMenu *p_menu)
|
|||||||
p_menu->insertSeparator(menuAct);
|
p_menu->insertSeparator(menuAct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMdEditor::insertImageLink(const QString &p_text, const QString &p_url)
|
||||||
|
{
|
||||||
|
VInsertLinkDialog dialog(tr("Insert Image Link"),
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
p_text,
|
||||||
|
p_url,
|
||||||
|
true,
|
||||||
|
this);
|
||||||
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
|
QString linkText = dialog.getLinkText();
|
||||||
|
QString linkUrl = dialog.getLinkUrl();
|
||||||
|
static_cast<VMdEditOperations *>(m_editOps)->insertImageLink(linkText, linkUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -234,6 +234,8 @@ private:
|
|||||||
|
|
||||||
void initCopyAsMenu(QAction *p_before, QMenu *p_menu);
|
void initCopyAsMenu(QAction *p_before, QMenu *p_menu);
|
||||||
|
|
||||||
|
void insertImageLink(const QString &p_text, const QString &p_url);
|
||||||
|
|
||||||
HGMarkdownHighlighter *m_mdHighlighter;
|
HGMarkdownHighlighter *m_mdHighlighter;
|
||||||
|
|
||||||
VCodeBlockHighlightHelper *m_cbHighlighter;
|
VCodeBlockHighlightHelper *m_cbHighlighter;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user