-
Notifications
You must be signed in to change notification settings - Fork 26
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
Conversation
Removed previous code highlighter
8291c8a
to
e2365cc
Compare
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; | ||
} | ||
|
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
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(); |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum?
There was a problem hiding this comment.
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"> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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.

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

For editing mode, we can add lint so that it helps the user avoid syntax mistakes. See https://codemirror.net/examples/lint/

The editor has: