Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cypress/e2e/4-image-upload/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Default</title>
<link rel="stylesheet" href="../../../dist/easymde.min.css">
<script src="../../../dist/easymde.min.js"></script>
</head>

<body>
<textarea id="textarea"></textarea>
<script>
const easyMDE = new EasyMDE({
uploadImage: true,
showIcons: ["upload-image"],
imageUploadFunction: (file, onSuccess) => {
onSuccess('https://test.com/test.jpg')
}
});
</script>
</body>

</html>
20 changes: 20 additions & 0 deletions cypress/e2e/4-image-upload/upload.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference types="cypress" />

describe('Upload', () => {
beforeEach(() => {
cy.visit(__dirname + '/index.html');
});

it('upload an image should insert a mock image url', () => {
cy.get('.EasyMDEContainer button.upload-image').click();
cy.get('.EasyMDEContainer input[type=file]').selectFile({
contents: Cypress.Buffer.from('', 'utf-8'),
fileName: 'test.jpg',
mimeType: 'image/jpeg'
}, {
action: 'drag-drop',
force: true
});
cy.get('.EasyMDEContainer .CodeMirror').contains('![test.jpg](https://test.com/test.jpg)');
});
});
14 changes: 3 additions & 11 deletions src/js/easymde.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,18 +878,10 @@ function afterImageUploaded(editor, url) {
var cm = editor.codemirror;
var stat = getState(cm);
var options = editor.options;
// TODO: Get the image name from the original file name
var imageName = url.substr(url.lastIndexOf('/') + 1);
var ext = imageName.substring(imageName.lastIndexOf('.') + 1).replace(/\?.*$/, '').toLowerCase();

// Check if media is an image
if (['png', 'jpg', 'jpeg', 'gif', 'svg', 'apng', 'avif', 'webp'].includes(ext)) {
_replaceSelection(cm, stat.image, options.insertTexts.uploadedImage, url);
} else {
var text_link = options.insertTexts.link;
text_link[0] = '[' + imageName;
_replaceSelection(cm, stat.link, text_link, url);
}

var text_link = [`${options.insertTexts.image[0]}${imageName}`, options.insertTexts.image[1]];
_replaceSelection(cm, stat.image, text_link, url);
// show uploaded image filename for 1000ms
editor.updateStatusBar('upload-image', editor.options.imageTexts.sbOnUploaded.replace('#image_name#', imageName));
setTimeout(function () {
Expand Down