Skip to content

Adding CodeMirror editor for code highlighting and editing #2279

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

Merged
merged 3 commits into from
Mar 17, 2025

Conversation

johnsimons
Copy link
Member

@johnsimons johnsimons commented Mar 16, 2025

This PR adds CodeMirror as the code editor and syntax highlighter.

I changed the stack trace to use the editor. At the moment, the syntax highlighter for the stack trace uses the "PowerShell" language, which highlights it slightly but not super nicely. We can write a custom language for it later.
image

Also added syntax highlighting to the message body in both view and editing mode.
image

For editing mode, we can add lint so that it helps the user avoid syntax mistakes. See https://codemirror.net/examples/lint/
Mar-17-2025 11-05-29

The editor has:

  • code folding
  • copy to clipboard
  • line numbers

@johnsimons johnsimons self-assigned this Mar 17, 2025
Comment on lines -107 to -130
function isContentTypeSupported(contentType: string | undefined) {
if (contentType === undefined) return false;

if (contentType.startsWith("text/")) return true;

const charsetUtf8 = "; charset=utf-8";

if (contentType.endsWith(charsetUtf8)) {
contentType = contentType.substring(0, contentType.length - charsetUtf8.length);
}

if (contentType === "application/json") return true;

if (contentType.startsWith("application/")) {
// Some examples:
// application/atom+xml
// application/ld+json
// application/vnd.masstransit+json
if (contentType.endsWith("+json") || contentType.endsWith("+xml")) return true;
}

return false;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Refactor so it can be reused

@@ -0,0 +1 @@
export type CodeLanguage = "json" | "xml" | "shell" | "powershell" | "csharp";
Copy link
Member Author

Choose a reason for hiding this comment

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

At the moment these are the only languages supported, we can add more later if needed

}

// taken from https://github.com/krtnio/angular-pretty-xml/blob/master/src/angular-pretty-xml.js
function formatXml(xml: string) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I found a lib that does the formatting for xml.
I tested a message with xml serialization, and everything works correctly.

Comment on lines +8 to +32
function getBoundingClientRect(): DOMRect {
const rec = {
x: 0,
y: 0,
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
};
return { ...rec, toJSON: () => rec };
}

class FakeDOMRectList extends Array<DOMRect> implements DOMRectList {
item(index: number): DOMRect | null {
return this[index];
}
}

document.elementFromPoint = (): null => null;
HTMLElement.prototype.getBoundingClientRect = getBoundingClientRect;
HTMLElement.prototype.getClientRects = (): DOMRectList => new FakeDOMRectList();
Range.prototype.getBoundingClientRect = getBoundingClientRect;
Range.prototype.getClientRects = (): DOMRectList => new FakeDOMRectList();
Copy link
Member Author

Choose a reason for hiding this comment

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

Ran into jsdom/jsdom#3729 (comment) so I used that fix mentioned there

@@ -0,0 +1 @@
export type CodeLanguage = "json" | "xml" | "shell" | "powershell" | "csharp";
Copy link
Contributor

Choose a reason for hiding this comment

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

enum?

Copy link
Member Author

Choose a reason for hiding this comment

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

works the same


<template>
<div class="wrapper" :aria-label="ariaLabel">
<div v-if="props.showCopyToClipboard" class="toolbar">
Copy link
Contributor

Choose a reason for hiding this comment

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

comment here to explain that the copy on CodeMirror doesn't seem to work so we rolled our own

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, out of the box, there is no copy button.
The last time I looked at a plugin option, I can't remember which one.
So, we don't need a comment here to spell that out.

@johnsimons johnsimons added the Type: Improvement Type: Improvement label Mar 17, 2025
@johnsimons johnsimons enabled auto-merge March 17, 2025 05:47
@johnsimons johnsimons merged commit 027fae2 into master Mar 17, 2025
5 checks passed
@johnsimons johnsimons deleted the john/codemirror branch March 17, 2025 05:50
@johnsimons johnsimons added this to the vNext milestone Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Improvement Type: Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants