Skip to content

feat: add JavaScript and CSS support to script and style blocks in HT… - #5994

Merged
knolleary merged 4 commits into
chore/update-monaco-0.57.0from
feat/monaco-embedded-languages
Oct 14, 2026
Merged

knolleary merged 4 commits into
chore/update-monaco-0.57.0from
feat/monaco-embedded-languages

Conversation

@Steve-Mcl

@Steve-Mcl Steve-Mcl commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

The main reason for this work is to work towards supporting vue and vuetify in dashboard-2:

As a happy side effect, this enables embedded <script> and <style> blocks in all monaco editors in Node-RED set to html mode - providing error checking and code completions for users writing HTML.

Note

This is a feature but it builds on monaco 0.57.0 (which is a bug fix PR pointed at main). If you would prefer this to be based off dev, I will repoint it once monaco 0 57 is merged and dev has been updated.

Proposed changes

Code in <script> and <style> blocks only gets syntax highlighting in the Monaco editor today. That covers the template node and any other node that edits HTML. This PR adds proper language support for that code:

  • JavaScript in <script>: completion, hover info, parameter hints, go to definition and error checking
  • CSS / SCSS / Less in <style>: completion, hover, colour previews and error checking

It works out of the box for html and handlebars editors. No settings, no extra download, no new workers.

How it works

A new code-editors/monaco-embedded.js keeps a hidden model for each document holding only the embedded code, with everything else blanked out (newlines kept), so positions line up with the real document. Monaco's existing TypeScript and CSS workers answer for those hidden models, and the results and markers are mapped back onto the editor. This is the same "embedded document" approach VS Code uses for its HTML support. It only uses public Monaco APIs.

A few details worth knowing:

  • {{...}} and {{{...}}} are treated as placeholders inside the blocks, so a mustache template like const data = {{{payload.json}}} doesn't show false errors.
  • Embedded errors use their own marker owners, so they show in the editor but don't change what getAnnotations() reports to nodes.
  • The hidden models are disposed with their editor, so nothing is left behind for the editor:close cleanup.

API for other HTML-like languages

Nodes and plugins that register their own HTML-like language can opt in:

  • RED.editor.codeEditor.monaco.registerEmbeddedLanguages(languageId, options) turns this on for another language. The options can adjust how the JavaScript is read, e.g. wrap it and add type declarations so this is typed for that node's runtime. I'll use this in FlowFuse Dashboard's ui-template to type this for its Vue components (this.send(), this.msg, data/methods...).
  • RED.editor.codeEditor.monaco.registerHTMLLanguage(languageId, options) registers Monaco's HTML language service for another language and makes sure it gets the html worker.

Worker fix

While testing I found that Node-RED's MonacoEnvironment.getWorkerUrl is never used. Monaco loads asynchronously and the bundle already defines its own getWorkerUrl, so window.MonacoEnvironment.getWorkerUrl || function ... always kept the bundle's version. That version returns .../dist/undefined for labels it doesn't know, such as a language registered with registerHTMLLanguageService(). Monaco then falls back to running the worker on the main thread, and its requests never get an answer. Node-RED now wraps whatever function is already there and points HTML-like languages at the html worker.

Related

Testing

  • New unit tests in test/unit/@node-red/editor-client/ui/editors/code-editors/monaco-embedded_spec.js for finding script/style blocks, building the hidden document, mustache placeholders and offset mapping. 19 editor-client tests pass, and npm run lint is clean.
  • Checked in the browser with Playwright:
    • template node in html format with mustache syntax: DOM completion, no false errors from {{...}};
    • errors drawn on the right line and column;
    • CSS completion, hover and colour swatches;
    • no console errors;
    • no models or type libraries left behind after closing the editors.

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

@Steve-Mcl

Copy link
Copy Markdown
Contributor Author

demo of what this brings.

to be clear, without this, the content of <script> and <style> blocks are completely inert (no code completions, error checking, helper hints etc)

template-node-script-style-demo.mp4

…ML editors

The template node and any other HTML editor only highlight code in <script> and
<style> blocks. This adds completion, hover, signature help, go to definition and
error checking for that code, using Monaco's own TypeScript and CSS workers on a
hidden copy of each block, the same approach VS Code's HTML support uses.

- New monaco-embedded.js, registered for html and handlebars out of the box
- {{...}} is treated as a placeholder, so mustache templates don't show false errors
- CSS checking works today; CSS completion, hover and colours switch on when
  Monaco provides monaco.css.getWorker() (nr-monaco-build)
- Public API so other HTML-like languages can opt in and adjust the JavaScript,
  e.g. type `this` for their own runtime: RED.editor.codeEditor.monaco
  .registerEmbeddedLanguages() and .registerHTMLLanguage()
- getWorkerUrl now wraps the function the Monaco bundle already defines (Node-RED's
  own was never used) and points HTML-like languages at the html worker
A registerEmbeddedLanguages() wrap() hook can wrap a script in a helper call, e.g.
Dashboard's __uiTemplate({ ... }). With the cursor inside its arguments, parameter
hints showed the helper and its full internal type. Completion and hover already
left out names matching hiddenNames; parameter hints now do too.
@Steve-Mcl
Steve-Mcl force-pushed the feat/monaco-embedded-languages branch from 555858e to 138f74a Compare October 13, 2026 10:07
@Steve-Mcl Steve-Mcl mentioned this pull request Oct 13, 2026
2 of 6 tasks
@Steve-Mcl
Steve-Mcl added this pull request to stack #6001 October 13, 2026 10:21
@knolleary
knolleary merged commit 53e6683 into main Oct 14, 2026
3 checks passed
@knolleary
knolleary deleted the feat/monaco-embedded-languages branch October 14, 2026 09:05
Steve-Mcl added a commit to FlowFuse/node-red-dashboard that referenced this pull request Oct 14, 2026
Builds on node-red/node-red#5994, which adds JavaScript and CSS support to <script>
and <style> blocks in Node-RED's HTML editors and lets other HTML-like languages opt
in. This opts the ui-template's vue-sfc language in and adds what is specific to
Dashboard: the type of `this`.

- Component scripts (a single `export default { ... }`) are wrapped in the hidden
  copy so `this` in data, computed, methods, watch, mounted and unmounted knows the
  component's members and what Dashboard adds (send, submit, msg, value, id, props,
  $socket, $store, $router, $vuetify, ...)
- Other scripts, which Dashboard runs inside mounted, get the same `this`
- Only loaded when Node-RED provides registerEmbeddedLanguages, so older Node-RED
  is unaffected
- The plugin uses Node-RED's registerHTMLLanguage when present, which also maps
  the worker, instead of its own registration and worker shim
Steve-Mcl added a commit to FlowFuse/node-red-dashboard that referenced this pull request Oct 15, 2026
Builds on node-red/node-red#5994, which adds JavaScript and CSS support to <script>
and <style> blocks in Node-RED's HTML editors and lets other HTML-like languages opt
in. This opts the ui-template's vue-sfc language in and adds what is specific to
Dashboard: the type of `this`.

- Component scripts (a single `export default { ... }`) are wrapped in the hidden
  copy so `this` in data, computed, methods, watch, mounted and unmounted knows the
  component's members and what Dashboard adds (send, submit, msg, value, id, props,
  $socket, $store, $router, $vuetify, ...)
- Other scripts, which Dashboard runs inside mounted, get the same `this`
- Only loaded when Node-RED provides registerEmbeddedLanguages, so older Node-RED
  is unaffected
- The plugin uses Node-RED's registerHTMLLanguage when present, which also maps
  the worker, instead of its own registration and worker shim
Steve-Mcl added a commit to FlowFuse/node-red-dashboard that referenced this pull request Oct 16, 2026
Builds on node-red/node-red#5994, which adds JavaScript and CSS support to <script>
and <style> blocks in Node-RED's HTML editors and lets other HTML-like languages opt
in. This opts the ui-template's vue-sfc language in and adds what is specific to
Dashboard: the type of `this`.

- Component scripts (a single `export default { ... }`) are wrapped in the hidden
  copy so `this` in data, computed, methods, watch, mounted and unmounted knows the
  component's members and what Dashboard adds (send, submit, msg, value, id, props,
  $socket, $store, $router, $vuetify, ...)
- Other scripts, which Dashboard runs inside mounted, get the same `this`
- Only loaded when Node-RED provides registerEmbeddedLanguages, so older Node-RED
  is unaffected
- The plugin uses Node-RED's registerHTMLLanguage when present, which also maps
  the worker, instead of its own registration and worker shim
Steve-Mcl added a commit to FlowFuse/node-red-dashboard that referenced this pull request Oct 17, 2026
Builds on node-red/node-red#5994, which adds JavaScript and CSS support to <script>
and <style> blocks in Node-RED's HTML editors and lets other HTML-like languages opt
in. This opts the ui-template's vue-sfc language in and adds what is specific to
Dashboard: the type of `this`.

- Component scripts (a single `export default { ... }`) are wrapped in the hidden
  copy so `this` in data, computed, methods, watch, mounted and unmounted knows the
  component's members and what Dashboard adds (send, submit, msg, value, id, props,
  $socket, $store, $router, $vuetify, ...)
- Other scripts, which Dashboard runs inside mounted, get the same `this`
- Only loaded when Node-RED provides registerEmbeddedLanguages, so older Node-RED
  is unaffected
- The plugin uses Node-RED's registerHTMLLanguage when present, which also maps
  the worker, instead of its own registration and worker shim
n-lark pushed a commit to FlowFuse/node-red-dashboard that referenced this pull request Oct 21, 2026
Builds on node-red/node-red#5994, which adds JavaScript and CSS support to <script>
and <style> blocks in Node-RED's HTML editors and lets other HTML-like languages opt
in. This opts the ui-template's vue-sfc language in and adds what is specific to
Dashboard: the type of `this`.

- Component scripts (a single `export default { ... }`) are wrapped in the hidden
  copy so `this` in data, computed, methods, watch, mounted and unmounted knows the
  component's members and what Dashboard adds (send, submit, msg, value, id, props,
  $socket, $store, $router, $vuetify, ...)
- Other scripts, which Dashboard runs inside mounted, get the same `this`
- Only loaded when Node-RED provides registerEmbeddedLanguages, so older Node-RED
  is unaffected
- The plugin uses Node-RED's registerHTMLLanguage when present, which also maps
  the worker, instead of its own registration and worker shim
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