Impossível definir o padrão para o modo markdown

Hi, I find that even when I have set “default_composition_mode” to Markdown mode, users on my forum are getting served the Rich text editor on starting a new topic. The forum version is “Discourse v3.5.0.beta9-dev — Commits · discourse/discourse · GitHub — Ember v5.12.0”.

Each time I apply the change, I am prompted “Would you like to apply this change historically? This will change preferences for 15 existing users.” and I click Yes. Even then, users being served the rich editor. Is this a known bug?

Edit - note: I have this issue with users who never used the toggle option to swap their editor mode between rich/markdown. They just logged in today after I had already set the default composition mode on the weekend right after the upgrade. I had also hidden the toggle option via theme CSS.

1 curtida

Following up on this, we have a plugin that uses “wrap_open” BBCode tokens, which are not supported by the rich text editor yet. So we need to ensure we stay on the Markdown mode for now.

I used Claude Sonnet to generate the following code to ensure that. I would be happy to receive any feedback on how I could improve it (especially if it has any bugs :sweat_smile:). Also sharing this in case it helps other forum admins. The code goes in Theme settings > JS (/admin/customize/themes/2/common/js/edit):

import {
  apiInitializer
} from "discourse/lib/api";

export default apiInitializer((api) => {
  // Force switch the user's editor to markdown mode
  function switchToMarkdownMode() {
    const currentUser = api.getCurrentUser();

    // User should be signed in if composer was opened
    if (!currentUser) {
      console.error('No user found');
      return;
    }

    // Got this option from here
    // https://github.com/discourse/discourse/blob/f0fc5646dc9bd29b0e814faea490e34800e9b322/app/assets/javascripts/discourse/app/models/user.js#L262C1-L266C4
    const currentMode = currentUser.get('user_option.composition_mode');

    if (currentMode !== 0) {
      // Only switch if not already in markdown mode
      // Example usage: https://github.com/discourse/discourse/blob/87476ce2c18fb8f856dda7ff03804ed5fbb0ff38/app/assets/javascripts/discourse/app/services/user-tips.js#L127
      currentUser.set('user_option.composition_mode', 0);

      // Save the user preference to the server
      currentUser.save(['composition_mode']).then(() => {
        console.log('Successfully switched to markdown mode');
      }).catch((error) => {
        console.error('Failed to update composition mode:', error);
      });

      // Immediately toggle the current composer UI
      setTimeout(() => {
        const toggleButton = document.querySelector('.composer-toggle-switch[data-rich-editor]');
        if (toggleButton) {
          const isRichTextActive = toggleButton.getAttribute('aria-checked') === 'true';
          if (isRichTextActive) {
            toggleButton.click();
            console.log('Toggled current composer to markdown mode');
          }
        }
      }, 100); // Small delay to ensure composer is fully rendered
    } else {
      console.log('Already in markdown mode, no change needed');
    }
  }

  api.onAppEvent('composer:opened', () => {
    switchToMarkdownMode();
  });
});

This is generally a bad practice, relying on timing for stuff like this is fragile.

Let me split this off, we certainly want to allow admins to set defaults to markdown mode.

What is a minimal repro here?

2 curtidas

I will try reproduce this + work on a fix tomorrow, but I am not sure why this would happen since the admin default sets all user option values (if you use the existing user option in the prompt), and this is all we use to control the toggle switch.

2 curtidas

Thanks for the quick responses! :slightly_smiling_face: I’m not sure how I can create a minimal repro for this. I’m happy to share any additional info from my forum if you can share the steps for it.

2 curtidas

I tried to reproduce this today with no luck. I do have one question though – are users refreshing the browser after you change this? Because if not, we still have their old user preference data in the browser in ember, so the new preference will not be used yet.

For this to work as expected I did this:

  • Checked on user A that editor was toggled to rich mode
  • As admin, I changed the default to markdown mode and applied for all users
  • As user A, reloaded the page and reopened the editor, and saw that it was in markdown mode

If I didn’t reload on step 3 there, I would still see rich mode.

1 curtida

Thanks for the follow-up, Martin. I had already set the mode to Markdown on Sunday. I had then logged in with a test account on Monday, in a separate browser profile, which received the Rich editor instead.

Also, if that helps, each time I set “default_composition_mode” to “Markdown”, I get the warning: “Would you like to apply this change historically? This will change preferences for 61 existing users.” I understand I should get this the first time, but it seems like a bug that it prompts me each time for some number of users.

For further testing, I also just logged into another test account that hadn’t logged in for 10 months. It was also set to Rich Text mode by default.

1 curtida

This is very odd indeed…how many users are on your site in total? I wonder if it’s hanging on the step where it’s supposed to update all users from the site setting change, because I noticed there is nothing in the UI to indicate that work is still happening.

Depends on how many users have changed it with the toggle in the interim. It seems like it works properly for me in local testing.

1 curtida

As per our /about page, we have 20,000 members.

I will clarify that the steps are as follows:

  1. Visit /admin/site_settings/category/all_results?filter=default_composition_mode
  2. Set default_composition_mode to Markdown mode. Even when it’s already in Markdown mode, you can open the dropdown and select the Markdown option.
  3. Get the warning for XX number of users. Accept it.
  4. Wait ten minutes, and then reload the page.
  5. Set default_composition_mode to Markdown mode (same as step 2).
  6. Again, get the warning for the same number of users.

As you mentioned, I also don’t know how long it takes (over 10 minutes?) to update the historical user preferences. Also, I don’t think all those users would have updated their preferences back in just that time. I could be missing something here.

1 curtida

Okay yeah definitely doesn’t sound like that many people would be toggling in that time :thinking: Are you able to do a screen recording to show what’s happening?

I’d also advise you take a look at the Network tab in Chrome Developer tools. It should show 2 requests like this after you save it (first one is the one to show the prompt of XX users, second one is to actually save the setting and do the backfill):

image

The second one needs to have a 200 status, if it doesn’t and you navigate away then it will not have backfilled for existing users.

Thanks for the additional info. The two requests are shown in the image below. Both were pretty quick.

A video recording is at this Drive link. I set the setting, accepted the warning dialog, waited a little, reloaded the page, and then set it again. Then I got the warning dialog again.

1 curtida

Okay the plot thickens…this part where you choose Markdown when you already have Markdown selected and it offers to save:

Should not happen…there is a bug here where it’s considering the value for the list item (0) different from the setting value (’0’). So I think the Save here is doing nothing. I can fix that bug, but can you do this:

  • Change the setting to “Rich text”
  • Save it, but choose “No, only apply changes going forward”
  • Change back to “Markdown”
  • Save it, and choose “Yes” to apply it retroactively

And see if that fixes the issues you are having.

1 curtida

Thanks for letting me know. I logged in with an old test account, and it seems to have worked correctly: it got the Markdown mode on the first run :slightly_smiling_face:

Is there a way to double-check that all the users are on Markdown mode, for example, by using the Data Explorer?

2 curtidas

Good to hear that solved the problem!

Yes definitely, you could do a query like:

SELECT composition_mode, COUNT(composition_mode) FROM user_options GROUP BY composition_mode;

Values are here:

2 curtidas

Thanks, all our users are on the Markdown mode now :partying_face:

2 curtidas