Skip to content

Conversation

@01zulfi
Copy link
Contributor

@01zulfi 01zulfi commented Dec 11, 2024

  • also fix spacing issues in language selector list

Closes #7032 #6932

Comment on lines 53 to 68
const cachedLanguage =
config.get<(typeof Languages)[number]>("codeBlockLanguage");

useEffect(() => {
if (languageDefinition) {
config.set("codeBlockLanguage", languageDefinition);
return;
}

if (!languageDefinition && cachedLanguage && cachedLanguage.filename) {
updateAttributes(
{ language: cachedLanguage.filename },
{ addToHistory: false, preventUpdate: false }
);
}
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't do this here. This will run for all codeblocks even though we just want to set the language once when a new codeblock is added. It'll also cause a flash where a codeblock is plaintext and then switches to the last used language.

Instead let's do this in code-block.ts in the parseHTML function of the language attribute. Not sure if it'll work or not though so give it a try.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, useEffect is called for all codeblocks when the page loads. Also, I wasn't able to notice a flash when codeblock is plaintext and then switches to the local storage language. But, there is a minor bug in this PR: user won't ever be able to go back to plaintext codeblock until they delete the local storage entry. I think we should add plaintext as a language in languages.json. What do you think?

Instead let's do this in code-block.ts in the parseHTML function of the language attribute. Not sure if it'll work or not though so give it a try.

I tried in parseHTML of the language attribute. The issue here is the parseHTML doesn't get called when a new code block is added, so we can't save the code block language if user pasted a code block from VSCode for example. I'll continue trying with parseHTML.

Copy link
Contributor Author

@01zulfi 01zulfi Dec 16, 2024

Choose a reason for hiding this comment

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

Thanks for the comment, I realized we don't need the useEffect at all. Please review the latest changes.

(plus I've added Plaintext in languages, let me know if we should keep this or else I can revert)

@01zulfi 01zulfi force-pushed the chore/code-block-changes branch 2 times, most recently from 935bfab to 064a6b3 Compare December 16, 2024 07:43
@01zulfi 01zulfi requested a review from thecodrr December 16, 2024 07:45
Copy link
Contributor

@thecodrr thecodrr left a comment

Choose a reason for hiding this comment

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

I don't think this is working. It changes the language label in the code block status bar but no highlighting actually happens.

@01zulfi 01zulfi force-pushed the chore/code-block-changes branch from 064a6b3 to bcb19ac Compare December 23, 2024 14:30
Comment on lines 541 to 547
const cachedLanguage =
config.get<(typeof Languages)[number]>("codeBlockLanguage");
if (!languageDefinition && cachedLanguage && cachedLanguage.filename) {
this.editor.commands.updateAttributes(this.type, {
language: cachedLanguage.filename
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks again for the thorough review!

I've added the language update from local storage logic here and it works. Please let me know if this is okay. Other place this exact logic works is in the useEffect inside the code block component.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just checked again and we can reuse defaultLanguage in the Highlighter to support this. It can be a function since the value can change.

However, the default language doesn't get stored in the code block attributes which you'll need to add support for in appendTransaction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, please review

@01zulfi 01zulfi requested a review from thecodrr December 23, 2024 14:32
@01zulfi 01zulfi force-pushed the chore/code-block-changes branch 2 times, most recently from dadcee1 to 016f094 Compare December 31, 2024 10:02
attrs: node.attrs,
defaultLanguage: defaultLanguage()
});
attributes.language = node.attrs.language ?? defaultLanguage();
Copy link
Contributor

Choose a reason for hiding this comment

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

This works but I think it'll cause issues with old code blocks (added before this change) as they'll get their language changed to the cached language as soon as the user makes a selection or a change in the document.

We should find a way to only run this once when the code block is added in the document. I think we can compare oldState and newState to find that out.

@01zulfi 01zulfi force-pushed the chore/code-block-changes branch from 016f094 to 73d61c8 Compare January 7, 2025 10:10
@01zulfi 01zulfi force-pushed the chore/code-block-changes branch from 0fe67f7 to 74ad99e Compare November 3, 2025 06:57
@thecodrr thecodrr added this to the v3.3.8-beta.2 milestone Jan 16, 2026
@01zulfi 01zulfi changed the base branch from master to beta January 17, 2026 05:35
* also add plaintext language option
* also fix spacing issues in language selector list

Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
@01zulfi 01zulfi force-pushed the chore/code-block-changes branch from 74ad99e to ed933c0 Compare January 17, 2026 06:04
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.

Set default programming language for Code Block

2 participants