Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the wrong text is inserted after uploading an image #580

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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'

Check failure on line 13 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (14)

Missing trailing comma

Check failure on line 13 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (16)

Missing trailing comma

Check failure on line 13 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (18)

Missing trailing comma
}, {
action: 'drag-drop',
force: true

Check failure on line 16 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (14)

Missing trailing comma

Check failure on line 16 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (16)

Missing trailing comma

Check failure on line 16 in cypress/e2e/4-image-upload/upload.cy.js

View workflow job for this annotation

GitHub Actions / test (18)

Missing trailing comma
});
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
ppodds marked this conversation as resolved.
Show resolved Hide resolved
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
Loading