-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Replace SyntaxHighlighter with Monaco Editor. #9728
Conversation
https://raw.githack.com/nojaf/swagger-ui/large-bodies/dist/index.html#/pet/findPetsByStatus the |
@nojaf I think the issue here might be just an old bundle, I added my changes to my branch and the height is fine: |
I changed the theme to be dark and disabled the minimap to get as close as possible as it was: simple changes: |
Hi @heldersepu, thank you for the feedback! Any thoughts on what to do with the |
@nojaf rebuild the bundles when you can I still can see the old version: would be nice to integrate the I'm not in control of the workflow some one from the team should do that |
@glowcloud & @char0n 👀 |
Hi @heldersepu, apologies for the delay. I took some time off.
Happy to do this, but could use a pointer. Do we care about the existing values? |
I don't think the existing theme values are compatible with the Monaco editor |
So, some e2e tests are failing like: swagger-ui/test/e2e-cypress/e2e/bugs/4943.cy.js Lines 10 to 19 in 3e81a4f
The problem presumably is in the assertion and might not be an actual problem. To run this: Could I please get some pointers on how to address this? |
They are not, I've updated the setting to work with the Monaco themes. @glowcloud & @char0n what are your thoughts on this PR, please? |
Nice job @nojaf ! |
Hey everyone 👋, With the recent changes in #9783, where the highlighting component got extracted, I've been inspired to pivot my approach a bit. I'm now considering focusing my efforts on developing a plugin, rather than continuing to push for my original PR. It's been a few weeks (three, to be exact), and I'm still not sure if there's any appetite for merging my PR. For those who might come across this thread in the future and are looking to integrate Monaco into SwaggerUI, I've had some success doing so with a plugin approach. Here’s a quick snippet to show you how I managed to hook in Monaco: import loader from "https://esm.sh/@monaco-editor/loader@1.4.0"
const nojafPlugin = {
components: {
SyntaxHighlighter: (props) => {
const React = props.React;
const myRef = React.createRef();
React.useEffect(() => {
let editor = null;
const cancelable = loader.init();
cancelable.then(monaco => {
if (myRef.current) {
editor = monaco.editor.create(myRef.current, {
value: props.children,
language: props.language
})
}
}).catch(
(error) =>
error?.type !== 'cancelation' && console.error('Monaco initialization: error:', error),
);
return () => {
if (cancelable && !myRef.current) {
cancelable.cancel();
}
if (editor) {
console.log(editor);
editor.dispose();
}
}
}, [myRef, props.children]);
// The container needs a height.
const maxHeight = 450;
const newLineCount = !props.children ? 0 : (props.children.match(/\n/g)).length
const proposedHeight = (newLineCount * 14)
const minHeight = Math.max(100, Math.min(proposedHeight, maxHeight));
return React.createElement("div", { ref: myRef, style: { minHeight: `${minHeight}px` } }, null);
}
}
} And you can integrate it like so: window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
...,
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
nojafPlugin
],
})
window.ui = ui
ui.initOAuth({...})
} A few key takeaways from my experience:
I hope this helps someone down the line! |
@nojaf don't feel discouraged this PR is great, the syntax highlighter was in need of a major refactor... |
When you can address the: |
Thanks for all the work on this, but we'll not be incorporating this major architectural change into core SwaggerUI. Due to #9783, this can now be approached using the standard SwaggerUI plugin system. More information in #3832 (comment) I've also created a wiki page with your MonacoEditor plugin - https://github.com/swagger-api/swagger-ui/wiki/Plugins |
Hi @char0n, thanks for the update and for putting my experiment into the wiki! |
Description
This replaces
react-syntax-highlighter
with@monaco-editor/react
.Motivation and Context
Fixes #3832.
The key motivation for us is to see large response bodies all the time.
How Has This Been Tested?
I've been playing with this manually on my local machine.
The endpoint I'm testing is retrieving 2.8 MB of JSON and it gets instantly rendered in my browser.
Screenshots (if appropriate):
Checklist
My PR contains...
src/
is unmodified: changes to documentation, CI, metadata, etc.)package.json
)My changes...
I'm not quite sure yet what to do with the
syntaxHighlight.theme
setting. These themes were tied toSyntaxHighlighter
and the Monaco editor has different themes out of the box.Please let know how we can deal with this.
Documentation
I'm not sure here: please tell me.
Automated tests
As I'm new to this, I'm not quite sure what needs to be done here. Again, let me know and I'll look into it.