2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-07-21 00:44:28 -06:00

Merge pull request #253 from devforth/master

Bugfixes for preview images in editor
This commit is contained in:
Jeroen Akkerman 2020-11-11 22:57:08 +01:00 committed by GitHub
commit 6209e53c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 35 deletions

View File

@ -136,7 +136,7 @@ easyMDE.value('New input for **EasyMDE**');
- **hideIcons**: An array of icon names to hide. Can be used to hide specific icons shown by default without completely customizing the toolbar. - **hideIcons**: An array of icon names to hide. Can be used to hide specific icons shown by default without completely customizing the toolbar.
- **indentWithTabs**: If set to `false`, indent using spaces instead of tabs. Defaults to `true`. - **indentWithTabs**: If set to `false`, indent using spaces instead of tabs. Defaults to `true`.
- **initialValue**: If set, will customize the initial value of the editor. - **initialValue**: If set, will customize the initial value of the editor.
- **previewImagesInEditor**: - EasyMDE will show preview of images, `true` by default, use `false` to disable. - **previewImagesInEditor**: - EasyMDE will show preview of images, `false` by default, preview for images will appear only for images on separate lines.
- **insertTexts**: Customize how certain buttons that insert text behave. Takes an array with two elements. The first element will be the text inserted before the cursor or highlight, and the second element will be inserted after. For example, this is the default link value: `["[", "](http://)"]`. - **insertTexts**: Customize how certain buttons that insert text behave. Takes an array with two elements. The first element will be the text inserted before the cursor or highlight, and the second element will be inserted after. For example, this is the default link value: `["[", "](http://)"]`.
- horizontalRule - horizontalRule
- image - image

View File

@ -374,7 +374,7 @@
display: block; display: block;
} }
span[data-img-src]::before{ span[data-img-src]::after{
content: ''; content: '';
background-image: var(--bg-image); background-image: var(--bg-image);
display: block; display: block;

View File

@ -2086,11 +2086,6 @@ EasyMDE.prototype.render = function (el) {
}); });
} }
function handleImages() {
if (!options.previewImagesInEditor) {
return;
}
function calcHeight(naturalWidth, naturalHeight) { function calcHeight(naturalWidth, naturalHeight) {
var height; var height;
var viewportWidth = window.getComputedStyle(document.querySelector('.CodeMirror-sizer')).width.replace('px', ''); var viewportWidth = window.getComputedStyle(document.querySelector('.CodeMirror-sizer')).width.replace('px', '');
@ -2101,26 +2096,51 @@ EasyMDE.prototype.render = function (el) {
} }
return height; 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) {
return;
}
easyMDEContainer.querySelectorAll('.cm-image-marker').forEach(function(e) {
var parentEl = e.parentElement; 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')) { if (!parentEl.hasAttribute('data-img-src')) {
var srcAttr = parentEl.innerText.match('\\((.*)\\)'); // might require better parsing according to markdown spec var srcAttr = parentEl.innerText.match('\\((.*)\\)'); // might require better parsing according to markdown spec
if (!window.EMDEimagesCache) {
window.EMDEimagesCache = {};
}
if (srcAttr && srcAttr.length >= 2) { if (srcAttr && srcAttr.length >= 2) {
var keySrc = srcAttr[1];
if (! window.EMDEimagesCache[keySrc]) {
var img = document.createElement('img'); var img = document.createElement('img');
img.onload = function() { img.onload = function() {
parentEl.setAttribute('data-img-src', srcAttr[1]); window.EMDEimagesCache[keySrc] = {
parentEl.setAttribute('data-img-width', img.naturalWidth); naturalWidth: img.naturalWidth,
parentEl.setAttribute('data-img-height', img.naturalHeight); naturalHeight: img.naturalHeight,
parentEl.setAttribute('style', '--bg-image:url('+srcAttr[1]+');--width:'+img.naturalWidth+'px;--height:'+calcHeight(img.naturalWidth, img.naturalHeight)); url: keySrc,
}; };
img.src = srcAttr[1]; assignImageBlockAttributes(parentEl, window.EMDEimagesCache[keySrc]);
} };
img.src = keySrc;
} else { } else {
// handle resizes case assignImageBlockAttributes(parentEl, window.EMDEimagesCache[keySrc]);
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));
} }
}); });
} }
@ -2128,12 +2148,6 @@ EasyMDE.prototype.render = function (el) {
handleImages(); handleImages();
}); });
this.onWindowResize = function() {
handleImages();
};
window.addEventListener('resize', this.onWindowResize, true);
this.gui.sideBySide = this.createSideBySide(); this.gui.sideBySide = this.createSideBySide();
this._rendered = this.element; this._rendered = this.element;