mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-07-01 23:24:28 -06:00
fix cursor preceise positioning when content with images are loaded into editor, show preview only for images on separate lines (not inlines)
This commit is contained in:
parent
23c17b88b4
commit
dba3201c79
@ -374,7 +374,7 @@
|
||||
display: block;
|
||||
}
|
||||
|
||||
span[data-img-src]::before{
|
||||
span[data-img-src]::after{
|
||||
content: '';
|
||||
background-image: var(--bg-image);
|
||||
display: block;
|
||||
|
@ -2084,11 +2084,6 @@ EasyMDE.prototype.render = function (el) {
|
||||
});
|
||||
}
|
||||
|
||||
function handleImages() {
|
||||
if (!options.previewImagesInEditor) {
|
||||
return;
|
||||
}
|
||||
|
||||
function calcHeight(naturalWidth, naturalHeight) {
|
||||
var height;
|
||||
var viewportWidth = window.getComputedStyle(document.querySelector('.CodeMirror-sizer')).width.replace('px', '');
|
||||
@ -2099,26 +2094,51 @@ EasyMDE.prototype.render = function (el) {
|
||||
}
|
||||
return height;
|
||||
}
|
||||
easyMDEContainer.querySelectorAll('.cm-formatting-image').forEach(function(e) {
|
||||
|
||||
var _vm = this;
|
||||
|
||||
|
||||
function assignImageBlockAttributes(parentEl, img) {
|
||||
parentEl.setAttribute('data-img-src', img.url);
|
||||
parentEl.setAttribute('style', '--bg-image:url('+img.url+');--width:'+img.naturalWidth+'px;--height:'+calcHeight(img.naturalWidth, img.naturalHeight));
|
||||
_vm.codemirror.setSize();
|
||||
}
|
||||
|
||||
function handleImages() {
|
||||
if (options.previewImagesInEditor === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
easyMDEContainer.querySelectorAll('.cm-image-marker').forEach(function(e) {
|
||||
var parentEl = e.parentElement;
|
||||
if (!parentEl.innerText.match(/^!\[.*?\]\(.*\)/g)) {
|
||||
// if img pasted on the same line with other text, don't preview, preview only images on separate line
|
||||
return;
|
||||
}
|
||||
if (!parentEl.hasAttribute('data-img-src')) {
|
||||
var srcAttr = parentEl.innerText.match('\\((.*)\\)'); // might require better parsing according to markdown spec
|
||||
if (!window.EMDEimagesCache) {
|
||||
window.EMDEimagesCache = {};
|
||||
}
|
||||
|
||||
if (srcAttr && srcAttr.length >= 2) {
|
||||
var keySrc = srcAttr[1];
|
||||
|
||||
if (! window.EMDEimagesCache[keySrc]) {
|
||||
var img = document.createElement('img');
|
||||
img.onload = function() {
|
||||
parentEl.setAttribute('data-img-src', srcAttr[1]);
|
||||
parentEl.setAttribute('data-img-width', img.naturalWidth);
|
||||
parentEl.setAttribute('data-img-height', img.naturalHeight);
|
||||
parentEl.setAttribute('style', '--bg-image:url('+srcAttr[1]+');--width:'+img.naturalWidth+'px;--height:'+calcHeight(img.naturalWidth, img.naturalHeight));
|
||||
window.EMDEimagesCache[keySrc] = {
|
||||
naturalWidth: img.naturalWidth,
|
||||
naturalHeight: img.naturalHeight,
|
||||
url: keySrc,
|
||||
};
|
||||
img.src = srcAttr[1];
|
||||
}
|
||||
assignImageBlockAttributes(parentEl, window.EMDEimagesCache[keySrc]);
|
||||
};
|
||||
img.src = keySrc;
|
||||
} else {
|
||||
// handle resizes case
|
||||
var src = parentEl.getAttribute('data-img-src');
|
||||
var naturalWidth = +parentEl.getAttribute('data-img-width');
|
||||
var naturalHeight = +parentEl.getAttribute('data-img-height');
|
||||
parentEl.setAttribute('style', '--bg-image:url('+src+');--width:'+naturalWidth+'px;--height:'+calcHeight(naturalWidth, naturalHeight));
|
||||
assignImageBlockAttributes(parentEl, window.EMDEimagesCache[keySrc]);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -2126,12 +2146,6 @@ EasyMDE.prototype.render = function (el) {
|
||||
handleImages();
|
||||
});
|
||||
|
||||
this.onWindowResize = function() {
|
||||
handleImages();
|
||||
};
|
||||
|
||||
window.addEventListener('resize', this.onWindowResize, true);
|
||||
|
||||
this.gui.sideBySide = this.createSideBySide();
|
||||
this._rendered = this.element;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user