-
Notifications
You must be signed in to change notification settings - Fork 16
new-log-viewer: Add custom Monaco editor themes and a Monaco language for logs. #69
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
Changes from all commits
3b9295e
f5228ae
1d38980
4d46387
712476f
3fa73aa
4dc8fb4
e378adc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js"; | ||
|
||
import {TOKEN_NAME} from "./typings"; | ||
|
||
|
||
const LOG_LANGUAGE_NAME = "logLanguage"; | ||
|
||
|
||
/** | ||
* Registers a custom log language in the Monaco editor. | ||
*/ | ||
const setupCustomLogLanguage = () => { | ||
monaco.languages.register({ | ||
id: LOG_LANGUAGE_NAME, | ||
}); | ||
monaco.languages.setMonarchTokensProvider(LOG_LANGUAGE_NAME, { | ||
tokenizer: { | ||
root: [ | ||
[ | ||
"INFO", | ||
TOKEN_NAME.CUSTOM_INFO, | ||
], | ||
[ | ||
"WARN", | ||
TOKEN_NAME.CUSTOM_WARN, | ||
], | ||
[ | ||
"ERROR", | ||
TOKEN_NAME.CUSTOM_ERROR, | ||
], | ||
[ | ||
"FATAL", | ||
TOKEN_NAME.CUSTOM_FATAL, | ||
], | ||
[ | ||
/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3})Z/, | ||
TOKEN_NAME.CUSTOM_DATE, | ||
], | ||
[ | ||
/^[\t ]*at.*$/, | ||
TOKEN_NAME.CUSTOM_EXCEPTION, | ||
], | ||
[ | ||
/(\d+(?:\.\d+)?([eE])([+-])[0-9](\.[0-9])?|\d+(?:\.\d+)?)/, | ||
TOKEN_NAME.CUSTOM_NUMBER, | ||
], | ||
], | ||
}, | ||
}); | ||
}; | ||
|
||
export { | ||
LOG_LANGUAGE_NAME, | ||
setupCustomLogLanguage, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js"; | ||
|
||
import {THEME_NAME} from "../../../typings/config"; | ||
import {TOKEN_NAME} from "./typings"; | ||
|
||
|
||
/** | ||
* Sets up custom themes for the Monaco editor. | ||
*/ | ||
const setupThemes = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit: Grammatically, this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about On the other hand, for consistency we should rename the other functions as well. Any objection if we do the renaming in the same PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mm, that might touch a lot of places. We could leave it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
monaco.editor.defineTheme(THEME_NAME.DARK, { | ||
base: "vs-dark", | ||
inherit: true, | ||
rules: [ | ||
{token: TOKEN_NAME.CUSTOM_INFO, foreground: "#098658"}, | ||
{token: TOKEN_NAME.CUSTOM_WARN, foreground: "#ce9178"}, | ||
{token: TOKEN_NAME.CUSTOM_ERROR, foreground: "#ce9178", fontStyle: "bold"}, | ||
{token: TOKEN_NAME.CUSTOM_FATAL, foreground: "#ce9178", fontStyle: "bold"}, | ||
{token: TOKEN_NAME.CUSTOM_DATE, foreground: "#529955"}, | ||
{token: TOKEN_NAME.CUSTOM_EXCEPTION, foreground: "#ce723b", fontStyle: "italic"}, | ||
{token: TOKEN_NAME.CUSTOM_NUMBER, foreground: "#3f9ccb"}, | ||
{token: TOKEN_NAME.COMMENT, foreground: "#008000"}, | ||
], | ||
colors: { | ||
"editor.lineHighlightBackground": "#3c3c3c", | ||
}, | ||
}); | ||
monaco.editor.defineTheme(THEME_NAME.LIGHT, { | ||
base: "vs", | ||
inherit: true, | ||
rules: [ | ||
{token: TOKEN_NAME.CUSTOM_INFO, foreground: "#098658"}, | ||
{token: TOKEN_NAME.CUSTOM_WARN, foreground: "#b81560"}, | ||
{token: TOKEN_NAME.CUSTOM_ERROR, foreground: "#ac1515", fontStyle: "bold"}, | ||
{token: TOKEN_NAME.CUSTOM_FATAL, foreground: "#ac1515", fontStyle: "bold"}, | ||
{token: TOKEN_NAME.CUSTOM_DATE, foreground: "#008000"}, | ||
{token: TOKEN_NAME.CUSTOM_EXCEPTION, foreground: "#ce723b", fontStyle: "italic"}, | ||
{token: TOKEN_NAME.CUSTOM_NUMBER, foreground: "#3f9ccb"}, | ||
], | ||
colors: {}, | ||
}); | ||
}; | ||
|
||
|
||
export {setupThemes}; | ||
Henry8192 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,18 @@ import { | |
|
||
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js"; | ||
|
||
import {useColorScheme} from "@mui/joy"; | ||
|
||
import {StateContext} from "../../contexts/StateContextProvider"; | ||
import { | ||
updateWindowUrlHashParams, | ||
UrlContext, | ||
} from "../../contexts/UrlContextProvider"; | ||
import {Nullable} from "../../typings/common"; | ||
import {CONFIG_KEY} from "../../typings/config"; | ||
import { | ||
CONFIG_KEY, | ||
THEME_NAME, | ||
} from "../../typings/config"; | ||
import {BeginLineNumToLogEventNumMap} from "../../typings/worker"; | ||
import { | ||
ACTION_NAME, | ||
|
@@ -36,12 +41,28 @@ import {goToPositionAndCenter} from "./MonacoInstance/utils"; | |
import "./index.css"; | ||
|
||
|
||
/** | ||
* Resets the cached page size in case it causes a client OOM. If it doesn't, the saved value | ||
* will be restored when {@link restoreCachedPageSize} is called. | ||
*/ | ||
const resetCachedPageSize = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was pulled out from Editor to avoid max lines lint violation. |
||
const error = setConfig( | ||
{key: CONFIG_KEY.PAGE_SIZE, value: CONFIG_DEFAULT[CONFIG_KEY.PAGE_SIZE]} | ||
); | ||
|
||
if (null !== error) { | ||
console.error(`Unexpected error returned by setConfig(): ${error}`); | ||
} | ||
}; | ||
|
||
/** | ||
* Renders a read-only editor for viewing logs. | ||
* | ||
* @return | ||
*/ | ||
const Editor = () => { | ||
const {mode, systemMode} = useColorScheme(); | ||
|
||
const {beginLineNumToLogEventNum, logData, numEvents} = useContext(StateContext); | ||
const {logEventNum} = useContext(UrlContext); | ||
|
||
|
@@ -100,20 +121,6 @@ const Editor = () => { | |
}); | ||
}, []); | ||
|
||
/** | ||
* Resets the cached page size in case it causes a client OOM. If it doesn't, the saved value | ||
* will be restored when {@link restoreCachedPageSize} is called. | ||
*/ | ||
const resetCachedPageSize = useCallback(() => { | ||
const error = setConfig( | ||
{key: CONFIG_KEY.PAGE_SIZE, value: CONFIG_DEFAULT[CONFIG_KEY.PAGE_SIZE]} | ||
); | ||
|
||
if (null !== error) { | ||
console.error(`Unexpected error returned by setConfig(): ${error}`); | ||
} | ||
}, []); | ||
|
||
/** | ||
* Restores the cached page size that was unset in {@link resetCachedPageSize}; | ||
*/ | ||
|
@@ -192,6 +199,9 @@ const Editor = () => { | |
beforeTextUpdate={resetCachedPageSize} | ||
lineNum={lineNum} | ||
text={logData} | ||
themeName={(("system" === mode) ? | ||
systemMode : | ||
mode) ?? THEME_NAME.DARK} | ||
onCursorExplicitPosChange={handleCursorExplicitPosChange} | ||
onCustomAction={handleEditorCustomAction} | ||
onMount={handleMount} | ||
|
Uh oh!
There was an error while loading. Please reload this page.