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

Pass the MDE context to the custom image function #225

Conversation

JoshuaLicense
Copy link

@JoshuaLicense JoshuaLicense commented Aug 8, 2020

Removes the need to bind the context manually to the imageUploadFunction when wanting to access the class methods (such as this.updateStatusBar).

Removes the need to bind the context manually when wanting to access the class methods.
@Ionaru
Copy link
Owner

Ionaru commented Aug 10, 2020

Is this not an option?

    var editor = new EasyMDE({
        imageUploadFunction: function(files) {
            editor.updateStatusBar('upload-image', 'Uploading...');
        },
    });

@JoshuaLicense
Copy link
Author

JoshuaLicense commented Aug 11, 2020

There are many options to workaround it like the above; I thought it'd be a little more user-friendly if the context was already bind however. Are there any downsides to calling the function using apply?

An example I can think of it being helpful is if the imageUploadFunction was declared before the EasyMDE class was initialised.

const imageUploadFunction = function(file, onSuccess, onError) {
    // ...
    this.updateStatusBar('upload-image', '...');
    // ...
};

const textareas = document.querySelectorAll('textarea');

textareas.forEach(textarea => new EasyMDE({ imageUploadFunction }));

In my case, I've abstracted some pre-configured default options for EasyMDE that are firstly imported from a different file to the initialisation call.

defaults.js

const options = {
    //...
    imageUploadFunction = function(file, onSuccess, onError) {}
    //..
};
export default options;

markdown.js

import _defaults from "defaults";

window.EasyMDE = options => {
    // ...
    // Merge default-options with passed options.
    // ...

    return new EasyMDE(options);
};

@Ionaru Ionaru merged commit b9b19bb into Ionaru:master Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants