fix paste image issue

- Will detect whether there is only <img> in html;
- Detect local file when handling text;
This commit is contained in:
Le Tan 2018-05-27 12:12:24 +08:00
parent 2ff9c607dd
commit d3a5642d06
3 changed files with 19 additions and 3 deletions

View File

@ -1580,3 +1580,9 @@ QString VUtils::promptForFileName(const QString &p_title,
return name;
}
bool VUtils::onlyHasImgInHtml(const QString &p_html)
{
// Tricky.
return !p_html.contains("<p ");
}

View File

@ -339,6 +339,9 @@ public:
const QString &p_dir,
QWidget *p_parent = nullptr);
// Whether @p_html has only <img> content.
static bool onlyHasImgInHtml(const QString &p_html);
// Regular expression for image link.
// ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100)
// Captured texts (need to be trimmed):

View File

@ -766,7 +766,8 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
if (p_source->hasHtml()) {
// Handle <img>.
QRegExp reg("<img ([^>]*)src=\"([^\"]+)\"([^>]*)>");
if (reg.indexIn(p_source->html()) != -1) {
QString html(p_source->html());
if (reg.indexIn(html) != -1 && VUtils::onlyHasImgInHtml(html)) {
if (p_source->hasImage()) {
// Both image data and URL are embedded.
VSelectDialog dialog(tr("Insert From Clipboard"), this);
@ -858,9 +859,15 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
int selection = dialog.getSelection();
if (selection == 0) {
// Insert as image.
QUrl url(text);
QUrl url;
if (QFileInfo::exists(text)) {
url = QUrl::fromLocalFile(text);
} else {
url = QUrl(text);
}
if (url.isValid()) {
m_editOps->insertImageFromURL(QUrl(text));
m_editOps->insertImageFromURL(url);
}
return;