mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 22:09:52 +08:00
VWebView: fix img src after copied
Add config web/fix_img_src_when_copied to control the behavior.
This commit is contained in:
parent
63825a27d0
commit
db73ab6dfd
@ -24,6 +24,7 @@ VTipsDialog::VTipsDialog(const QString &p_tipFile,
|
|||||||
void VTipsDialog::setupUI(const QString &p_actionText)
|
void VTipsDialog::setupUI(const QString &p_actionText)
|
||||||
{
|
{
|
||||||
m_viewer = VUtils::getWebEngineView();
|
m_viewer = VUtils::getWebEngineView();
|
||||||
|
m_viewer->setContextMenuPolicy(Qt::NoContextMenu);
|
||||||
|
|
||||||
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
||||||
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||||
|
@ -36,6 +36,7 @@ void VUpdater::setupUI()
|
|||||||
m_proBar->setTextVisible(false);
|
m_proBar->setTextVisible(false);
|
||||||
|
|
||||||
m_descriptionWV = VUtils::getWebEngineView();
|
m_descriptionWV = VUtils::getWebEngineView();
|
||||||
|
m_descriptionWV->setContextMenuPolicy(Qt::NoContextMenu);
|
||||||
m_descriptionWV->setHtml(VUtils::generateSimpleHtmlTemplate(VNote::s_sloganTemplate));
|
m_descriptionWV->setHtml(VUtils::generateSimpleHtmlTemplate(VNote::s_sloganTemplate));
|
||||||
|
|
||||||
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
||||||
|
@ -190,6 +190,9 @@ custom_colors=White:#EEEEEE,Green:#CCE8CF,Wheat:#DFC7B2,LightGrey:#D3D3D3
|
|||||||
; Location and configuration for Mathjax
|
; Location and configuration for Mathjax
|
||||||
mathjax_javascript=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML
|
mathjax_javascript=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML
|
||||||
|
|
||||||
|
; Fix local relative image source when copied
|
||||||
|
fix_img_src_when_copied=true
|
||||||
|
|
||||||
[shortcuts]
|
[shortcuts]
|
||||||
; Define shortcuts here, with each item in the form "operation=keysequence".
|
; Define shortcuts here, with each item in the form "operation=keysequence".
|
||||||
; Leave keysequence empty to disable the shortcut of an operation.
|
; Leave keysequence empty to disable the shortcut of an operation.
|
||||||
|
@ -284,6 +284,9 @@ void VConfigManager::initialize()
|
|||||||
|
|
||||||
m_closeBeforeExternalEditor = getConfigFromSettings("global",
|
m_closeBeforeExternalEditor = getConfigFromSettings("global",
|
||||||
"close_before_external_editor").toBool();
|
"close_before_external_editor").toBool();
|
||||||
|
|
||||||
|
m_fixImageSrcInWebWhenCopied = getConfigFromSettings("web",
|
||||||
|
"fix_img_src_when_copied").toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VConfigManager::initSettings()
|
void VConfigManager::initSettings()
|
||||||
|
@ -426,6 +426,8 @@ public:
|
|||||||
|
|
||||||
bool getCloseBeforeExternalEditor() const;
|
bool getCloseBeforeExternalEditor() const;
|
||||||
|
|
||||||
|
bool getFixImageSrcInWebWhenCopied() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Look up a config from user and default settings.
|
// Look up a config from user and default settings.
|
||||||
QVariant getConfigFromSettings(const QString §ion, const QString &key) const;
|
QVariant getConfigFromSettings(const QString §ion, const QString &key) const;
|
||||||
@ -816,6 +818,9 @@ private:
|
|||||||
// Whether user has reset the configurations.
|
// Whether user has reset the configurations.
|
||||||
bool m_hasReset;
|
bool m_hasReset;
|
||||||
|
|
||||||
|
// Whether fix the local relative image src in read mode when copied.
|
||||||
|
bool m_fixImageSrcInWebWhenCopied;
|
||||||
|
|
||||||
// The name of the config file in each directory, obsolete.
|
// The name of the config file in each directory, obsolete.
|
||||||
// Use c_dirConfigFile instead.
|
// Use c_dirConfigFile instead.
|
||||||
static const QString c_obsoleteDirConfigFile;
|
static const QString c_obsoleteDirConfigFile;
|
||||||
@ -1983,4 +1988,9 @@ inline bool VConfigManager::getCloseBeforeExternalEditor() const
|
|||||||
return m_closeBeforeExternalEditor;
|
return m_closeBeforeExternalEditor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool VConfigManager::getFixImageSrcInWebWhenCopied() const
|
||||||
|
{
|
||||||
|
return m_fixImageSrcInWebWhenCopied;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // VCONFIGMANAGER_H
|
#endif // VCONFIGMANAGER_H
|
||||||
|
@ -11,9 +11,13 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
|
#include <QFileInfo>
|
||||||
#include "vfile.h"
|
#include "vfile.h"
|
||||||
#include "utils/vclipboardutils.h"
|
#include "utils/vclipboardutils.h"
|
||||||
#include "utils/viconutils.h"
|
#include "utils/viconutils.h"
|
||||||
|
#include "vconfigmanager.h"
|
||||||
|
|
||||||
|
extern VConfigManager *g_config;
|
||||||
|
|
||||||
// We set the property of the clipboard to mark that the URL copied in the
|
// We set the property of the clipboard to mark that the URL copied in the
|
||||||
// clipboard has been altered.
|
// clipboard has been altered.
|
||||||
@ -23,7 +27,8 @@ VWebView::VWebView(VFile *p_file, QWidget *p_parent)
|
|||||||
: QWebEngineView(p_parent),
|
: QWebEngineView(p_parent),
|
||||||
m_file(p_file),
|
m_file(p_file),
|
||||||
m_copyImageUrlActionHooked(false),
|
m_copyImageUrlActionHooked(false),
|
||||||
m_needRemoveBackground(false)
|
m_needRemoveBackground(false),
|
||||||
|
m_fixImgSrc(g_config->getFixImageSrcInWebWhenCopied())
|
||||||
{
|
{
|
||||||
setAcceptDrops(false);
|
setAcceptDrops(false);
|
||||||
|
|
||||||
@ -246,6 +251,56 @@ void VWebView::handleClipboardChanged(QClipboard::Mode p_mode)
|
|||||||
alterHtmlMimeData(clipboard, mimeData, removeBackground);
|
alterHtmlMimeData(clipboard, mimeData, removeBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VWebView::fixImgSrc(QString &p_html)
|
||||||
|
{
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
QUrl::ComponentFormattingOption strOpt = QUrl::EncodeSpaces;
|
||||||
|
#else
|
||||||
|
QUrl::ComponentFormattingOption strOpt = QUrl::FullyEncoded;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
QRegExp reg("(<img src=\")([^\"]+)\"");
|
||||||
|
QUrl baseUrl(url());
|
||||||
|
|
||||||
|
int pos = 0;
|
||||||
|
while (pos < p_html.size()) {
|
||||||
|
int idx = p_html.indexOf(reg, pos);
|
||||||
|
if (idx == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString urlStr = reg.cap(2);
|
||||||
|
QUrl imgUrl(urlStr);
|
||||||
|
|
||||||
|
QString fixedStr;
|
||||||
|
if (imgUrl.isRelative()) {
|
||||||
|
fixedStr = baseUrl.resolved(imgUrl).toString(strOpt);
|
||||||
|
} else if (imgUrl.isLocalFile()) {
|
||||||
|
fixedStr = imgUrl.toString(strOpt);
|
||||||
|
} else if (imgUrl.scheme() != "https" && imgUrl.scheme() != "http") {
|
||||||
|
QString tmp = imgUrl.toString();
|
||||||
|
if (QFileInfo::exists(tmp)) {
|
||||||
|
fixedStr = QUrl::fromLocalFile(tmp).toString(strOpt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pos = idx + reg.matchedLength();
|
||||||
|
if (!fixedStr.isEmpty() && urlStr != fixedStr) {
|
||||||
|
qDebug() << "fix img url" << urlStr << fixedStr;
|
||||||
|
// Insert one more space to avoid fix the url twice.
|
||||||
|
pos = pos + fixedStr.size() + 1 - urlStr.size();
|
||||||
|
p_html.replace(idx,
|
||||||
|
reg.matchedLength(),
|
||||||
|
QString("<img src=\"%1\"").arg(fixedStr));
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
|
void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
|
||||||
const QMimeData *p_mimeData,
|
const QMimeData *p_mimeData,
|
||||||
bool p_removeBackground)
|
bool p_removeBackground)
|
||||||
@ -257,15 +312,22 @@ void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
|
|||||||
bool altered = false;
|
bool altered = false;
|
||||||
QString html = p_mimeData->html();
|
QString html = p_mimeData->html();
|
||||||
|
|
||||||
|
// Add surrounded tags.
|
||||||
if (!html.startsWith("<html>")) {
|
if (!html.startsWith("<html>")) {
|
||||||
altered = true;
|
altered = true;
|
||||||
html = QString("<html><body>%1</body></html>").arg(html);
|
html = QString("<html><body>%1</body></html>").arg(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove background color.
|
||||||
if (p_removeBackground && removeBackgroundColor(html)) {
|
if (p_removeBackground && removeBackgroundColor(html)) {
|
||||||
altered = true;
|
altered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fix local relative images.
|
||||||
|
if (m_fixImgSrc && fixImgSrc(html)) {
|
||||||
|
altered = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (!altered) {
|
if (!altered) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -275,5 +337,5 @@ void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
|
|||||||
data->setHtml(html);
|
data->setHtml(html);
|
||||||
|
|
||||||
VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
|
VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
|
||||||
qDebug() << "altered clipboard's Html";
|
qDebug() << "altered clipboard's Html" << html;
|
||||||
}
|
}
|
||||||
|
@ -40,12 +40,16 @@ private:
|
|||||||
const QMimeData *p_mimeData,
|
const QMimeData *p_mimeData,
|
||||||
bool p_removeBackground);
|
bool p_removeBackground);
|
||||||
|
|
||||||
|
bool fixImgSrc(QString &p_html);
|
||||||
|
|
||||||
VFile *m_file;
|
VFile *m_file;
|
||||||
|
|
||||||
// Whether this view has hooked the Copy Image Url action.
|
// Whether this view has hooked the Copy Image Url action.
|
||||||
bool m_copyImageUrlActionHooked;
|
bool m_copyImageUrlActionHooked;
|
||||||
|
|
||||||
bool m_needRemoveBackground;
|
bool m_needRemoveBackground;
|
||||||
|
|
||||||
|
bool m_fixImgSrc;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VWEBVIEW_H
|
#endif // VWEBVIEW_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user