Maik Skoddow
Tera Patron
Tera Patron

MaikSkoddow_0-1688299256048.png

 

With the Utah release ServiceNow replaced the TinyMCE-based editor with the Monaco editor for all script fields, but unfortunately there is not much documentation about that new editor and how to use it in ServiceNow.

 

Therefore, I decided to write that article, which might be extended whenever I figure out new interesting facts and tips.

 

 

Why has ServiceNow replaced the code editor?

 

Monaco is a popular open-source code editor developed by Microsoft and probably the most widely used code editor in the world, among other things through its use in Visual Studio Code The Monaco editor is renowned for its rapid performance, robust IntelliSense code completion, and compatibility with a diverse range of programming languages and file formats. It has several features that make it well-suited for coding and debugging, including syntax highlighting, code folding, automatic indentation, and code refactoring. Thus, the replacement with the previously used code editor based on TinyMCE was just a logical and well overdue consequence.

 

 

Command Palette 

 

The countless commands can be selected most easily via the "Command Palette" menu after pressing the key "F1". The selection offers a full text search among all commands, so that you can also find commands of which you only know parts of the name.

 

MaikSkoddow_0-1688284878671.png

 

 

 

Dark Mode

 

If you are using the Next Experience UI (Polaris) in your ServiceNow instance and the configured theme has the option of the "Dark Mode" then also the Monaco editor is displayed that way, just as you are used to it from Visual Studio Code.

 

MaikSkoddow_3-1688286184148.png

 

But in case you cannot use the dark mode in your ServiceNow instance, there are two options:

 

With the command "Toggle High Contrast Theme" you can enable a kind of dark mode with a black background:

 

MaikSkoddow_5-1688286668330.png

 

But this option has to be called up every time, which is not a satisfactory solution in the end. Fortunately the Monaco editor API provides a method to set a different theme at runtime, however ServiceNow does not offer a corresponding option here, so that the dark mode can only be permanently activated by using an injected JavaScript via the TamperMonkey browser extension:

 

MaikSkoddow_4-1688286559916.png

 

Now the script editor comes with the well-known dark mode used in Visual Studio Code:

 

MaikSkoddow_6-1688287834286.png

 

Updates

  • @CezaryBasta explained it in his blog how to achieve the same with an approach built in ServiceNow.
  • @Oliver S_1 has contributed the setting for the browser extension SN Utils which does the same without the need to leverage Tampermonkey (see below chapter "Minimap" to learn where to place that configuration): "theme":"vs-dark"

 

 

 

Context Menu

 

Actually, the Monaco Editor also provides a context menu via right-clicking anywhere into the pane, but for some reason it does not work in ServiceNow. If you still want to use the context menu, you need the browser extension SN Utils, which fixes this bug. You also have the possibility to switch the context menu on and off with the shortcut SHIFT+ALT+M.

 

MaikSkoddow_1-1688285184361.png

 

 

 

Open referenced Script Includes

 

If a record exists in the system for a referenced Script Include (API), its name is displayed in bold italics. In the past, you could click on the name by holding down the CTRL key to open the referenced Script Include in a new tab. Unfortunately, this does not work any longer now. Instead, ServiceNow seems to have developed an extension to the Monaco editor, because if you right-click on the name of the Script Include, an individual context menu appears, which allows you to open the referenced Script Include.

 

MaikSkoddow_2-1688286003061.png

 

 

 

Code Formatting

 

One of the most used options in the code editor was the automatic code formatting via CTRL+A and SHIFT+TAB. In the Monaco Code editor, this combination doesn't work any longer. And also try to prevent using the respective button in the icon bar, because this option does not do what it is supposed to do at all:

  • It formats the code anyhow, but not in the expected way.
  • Existing tabs are replaced by spaces, which later - when you continue writing - becomes a mix of tabs and spaces, which is absolutely unacceptable.

 

MaikSkoddow_7-1688290214789.png

 

 

With the Monaco editor, you now have to use its shortcuts or options:

 

MaikSkoddow_8-1688291232411.png

 

Formatting the current line is now a bit tricky, because you first have to press CTRL+K and then right after it CTRL+F. This is quite inconvenient, just to format a single line!

 

 

 

Minimap

 

The minimap is the area on the right side of the editor, which offers the entire code as a visual representation, but in a much smaller form. I'm not a friend of that feature, as it takes up valuable space and offers me no added value for the small script fields in ServiceNow. Unfortunately, I have not yet found a switch that prevents the minimap from being displayed. 

But again the great browser extension SN Utils comes to the rescue here, because in one of the last versions Arnood Kooi added a possibility in the settings to define options, which will then be used to configure the Monaco editor when the page is loaded.

 

MaikSkoddow_0-1688298673519.png

 

 

 

ESLint

 

All configured ESLint rules work as expected, but it seems that ServiceNow has updated with the Utah release also the ESLint version. As a result, more rules as before are accepted and applied accordingly. 

If you want to learn more about ESLint, I recommend my article Reduce smelling code and detect JavaScript issues with ESLint.

 

Comments
Uncle Rob
Kilo Patron

Phenomenal work as always, Maik.

Maik Skoddow
Tera Patron
Tera Patron

Thank you @Uncle Rob !

Ravi Chandra_K
Kilo Patron
Kilo Patron

This is very helpful Maik, Thank you!

 

Kind Regards,

Ravi Chandra.

Michael Bankwit
Tera Contributor

Is there a way to get ride of the bug that you cannot enter brackets [], {} on Safari on macOS? 

Update: Issue is fixed for me with Vancouver-Release.

CezaryBasta
Tera Guru

@Maik Skoddow wrote:

But this option has to be called up every time, which is not a satisfactory solution in the end. Fortunately the Monaco editor API provides a method to set a different theme at runtime, however ServiceNow does not offer a corresponding option here, so that the dark mode can only be permanently activated by using an injected JavaScript via the TamperMonkey browser extension:


There is a solution to this with some downsides and configuration required. I explained it in my blog.

It also includes short demo at the bottom of the page 🙂

 

Maik Skoddow
Tera Patron
Tera Patron

Hi @CezaryBasta 

thanks for pointing me to your blog post. It is now mentioned in the article.

christianp
Giga Explorer

Thank you for the update Maik!..

James Fricker
Tera Guru

the "Toggle High Contrast Theme" does not work for me. The only thing that changed was the colour of the Command Palette and the line numbers on the left.

James Fricker
Tera Guru

There is this issue
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1319600
If you have the SN Utils browser extension you can fix this issue. Just update the Monaco editor settings with the following {"showDeprecated": false}

smicloud
Giga Guru

It is possible to format the script using c t r l+alt+f  (hm, not adding space in c t r l transpose the text to **beep**). Additional short cuts can be found here (not sure if ServiceNow is disabling any, did not test many of those):  https://en.docs.monaca.io/products_guide/monaca_ide/editor 

 

Would be sweet to save using ctrl+s as for widgets. Also to let the script editor be in full size mode after save. 

Oliver S_1
Tera Contributor

"Toggle High Contrast Theme"
Based on the great work of @Arnoud Kooi & @Maik Skoddow  & @CezaryBasta 

I was able to figure out that adding this option

 

"theme" : "vs-dark"

 

to the SN Utils settings does the trick as well without the need of TamperMonkey.

Screenshot 2024-02-05 at 14.53.37.png

Oliver S_1
Tera Contributor

@James Fricker looks like the string value isn’t closed in your screen shot („).

Therefore it seems to be an invalid JSON.

James Fricker
Tera Guru

working now, many thanks

frank121
Kilo Sage

I think with Vancouvor Patch 7 the format butt was fixed along with auto completion.

sasfancy
Tera Explorer

Excellent Maik as always. Great deep dive for the Monaco editor.

James Fricker
Tera Guru

Anyone know how to make the Monaco editor use spaces instead of tabs when formatting? I prefer spaces.

James Fricker
Tera Guru
Version history
Last update:
‎02-16-2024 10:00 PM
Updated by:
Contributors