Overwrite methods in Discourse.Utilities

AI 生成的摘要

leo.proctor sought assistance on overriding Discourse.Utilities.getUploadMarkdown within a plugin but encountered issues with the module structure and read-only properties. simon initially shared a legacy plugin example, but leo.proctor reported that Discourse.Utilities was now undefined due to the shift to JavaScript modules. simon confirmed that the approach needed to change, and leo.proctor noted that direct overwriting failed because the method was read-only. simon suggested reopening the composer-editor component to override the _getUploadMarkdown function or _bindUploadTarget as a workaround, providing a code snippet demonstrating how to conditionally modify upload markdown logic via ComposerEditor.reopen. Ultimately, leo.proctor confirmed success by overriding _bindUploadTarget and implementing a custom getCustomUploadMarkdown function, which resolved the issue.

I’m struggling with EmberJS for a while now. I need to override Discourse.Utilities.getUploadMarkdown in a plugin, but i’m not getting anywhere. How do you override export methods in Discourse.Utilities. any help plz?

I did it in this plugin a while ago. GitHub - scossar/m4a-audio-uploads: adds m4a to the Discourse audio file list · GitHub

Hi Simon, thanks for your reply. This is exactly what I want. But upon following your javascript code, I get error that Discourse.Utilities is undefined.
Uncaught TypeError: Cannot set property 'getUploadMarkdown' of undefined
and it stills calls the old getUploadMarkdown method.

Yes, it looks like Discourse.Utilities has been converted to javascript modules since I wrote that plugin, so it will have to be done differently.

Still no luck. I’m unable to find a workaround to overwrite getUploadMarkdown. It won’t let me overwrite it. getUploadMarkdown is read-only. Any help?

I spent some time trying to figure this out and didn’t find an obvious way to do it. What makes sense to me is reopening the composer-editor component and making the changes there. It would be possible to override the _bindUploadTarget function that calls getUploadMarkdown, but that seems like a bad idea.

If getUploadMarkdown was called inside of a function in composer-editor.js.es6, it would be possible to do something like this in a site customization - for testing this I removed wav files from the audio file list:


// composer-editor.js.es6
...
_getUploadMarkdown(upload) {
    return getUploadMarkdown(upload);
},
...

<script>
    var ComposerEditor = require('discourse/components/composer-editor').default;
    var uploadLocation = require('discourse/lib/utilities').uploadLocation;
    
    ComposerEditor.reopen({
        _getUploadMarkdown: function(upload) {
            if (!Discourse.SiteSettings.prevent_anons_from_downloading_files && (/\.(wav)$/i).test(upload.original_filename)) {
                
                return uploadLocation(upload.url);
            }
            
            return this._super(upload);
        }
    }); 
</script>

Maybe there’s a way to do something similar by doing something directly with the getUploadMarkdown import.

Thanks for your efforts Simon. Yeah, I ended up overriding _bindUploadTarget and defined getCustomUploadMarkdown. And it works fine for now.

ComposerEditor.reopen({
_bindUploadTarget() {
…
const markdown = getCustomUploadMarkdown(upload);
…
}
)};