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 double MarkdownBlock EasyMDE initialisation #142

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

zerolab
Copy link
Member

@zerolab zerolab commented Mar 9, 2024

Fixes #141

@lb- suggested a conditional

if (!element.matches('[data-controller~="easymde"') {
   // Wagtail 6.0, using data attribute (Stimulus), does not require Telepath to instantiate MD Editor
    easymdeAttach(id);
}

in the wagtailmarkdown/js/markdown-textarea-adapter.js Telepath adapter, but that is really not needed with the new controller

@zerolab
Copy link
Member Author

zerolab commented Mar 9, 2024

@lb- would appreciate your input on this

@zerolab zerolab mentioned this pull request Mar 9, 2024

connect() {
if (this.autodownloadValue != null) {
if (this.hasAutodownloadValue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr - EasyMDE will try to detect if FA is present if initialized with a null value. Given static values = { autodownload: Boolean}; a missing value would be false, so we were initialising EasyMDE with "don't download FA"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Just reading this now.

The hasSomeValue just checks if the value is declared in the DOM so I'm not sure this is what you want.

<textarea data-controller="easymde" ... (No html data attribute for the value )
// this.hasAutodownloadValue - will be false 
<textarea data-controller="easymde" data-easymde-autodownload="false" ...
// this.hasAutodownloadValue - will be true
<textarea data-controller="easymde"  data-easymde-autodownload="true" ...
// this.hasAutodownloadValue - will be true 

I think the issue was the confusion of checking against null with a != as this will try to coerce the Boolean into a comparable value. As a general guide it's good to use strict inequality and try to align checks with the type set of the value.

Or keep the code simpler with returns or a non-comparison approach.

easymdeAttach(this.element.id, this.autodownloadValue);

this.autodownloadValue will always return a Boolean, Stimulus does this for us, so why do any branch logic at all. The default will be false if not set in the html.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for additional context, LB.

this.hasAutodownloadValue is what I want because we only set the data attribute the setting is present, hence I only want to pass it down when the attribute is set.

https://github.com/Ionaru/easy-markdown-editor?tab=readme-ov-file#configuration

autoDownloadFontAwesome: If set to true, force downloads Font Awesome (used for icons). If set to false, prevents downloading. Defaults to undefined, which will intelligently check whether Font Awesome has already been included, then download accordingly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. No problems.

Copy link

@softquantum softquantum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested locally and it fixed the issue in #141

@zerolab zerolab merged commit c5d1a43 into main Mar 9, 2024
8 checks passed
@zerolab zerolab deleted the fix/double-sf-block-init branch March 9, 2024 15:18
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.

double markdown block
3 participants