Skip to content
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

console-ui 新增 toml 语言支持,修复之前 properties 主题未生效的问题 #10896

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public enum ConfigType {
*/
YAML("yaml"),

/**
* config type is "toml".
*/
TOML("toml"),

/**
* not a real type.
*/
Expand Down
6 changes: 6 additions & 0 deletions console-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions console-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"dependencies": {
"@alifd/next": "^1.19.4",
"@alifd/theme-design-pro": "0.x",
"@iarna/toml": "^3.0.0",
"axios": "^0.21.1",
"js-yaml": "^4.1.0",
"moment": "^2.23.0",
Expand Down
2 changes: 1 addition & 1 deletion console-ui/src/components/MonacoEditor/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const MONACO_OPTIONS: Object = {
roundedSelection: false,
readOnly: false,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
wordWrapColumn: 120,
folding: true,
showFoldingControls: 'always',
Expand Down
71 changes: 67 additions & 4 deletions console-ui/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ window.require.config({
window.require(['vs/editor/editor.main'], () => {
// Register a new language
window.monaco.languages.register({ id: 'properties' });
window.monaco.languages.register({ id: 'toml' });

// Register a tokens provider for the language
window.monaco.languages.setMonarchTokensProvider('properties', {
Expand All @@ -77,15 +78,77 @@ window.require(['vs/editor/editor.main'], () => {
],
},
});
window.monaco.languages.setMonarchTokensProvider('toml', {
// The main tokenizer for our languages
tokenizer: {
root: [
{ include: '@comment' },
{ include: '@datetime' },
{ include: '@boolean' },
{ include: '@number' },
{ include: '@string' },
{ include: '@table' },
[/"""/, { token: 'string.block', bracket: '@open', next: '@string' }],
[/'''/, { token: 'stringLiteral.block', bracket: '@open', next: '@string' }],
[
/\s*((?:(?:(?:[A-Za-z0-9_+-]+)|(?:"[^"]+")|(?:'[^']+'))\s*\.?\s*)+)\s*(=)/,
['variable.name', 'eq'],
],
],
comment: [[/\s*((#).*)$/, 'comment']],
datetime: [
[/\d{2}:\d{2}:\d{2}(?:\.\d+)?/, 'localTime'],
[/\d{4}\-\d{2}\-\d{2}/, 'localDate'],
[/\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?/, 'localDateTime'],
[
/(?<!\w)(\d{4}\-\d{2}\-\d{2}[T| ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[\+\-]\d{2}:\d{2}))(?!\w)/,
'offsetDateTime',
],
],
boolean: [[/(?<!\w)(true|false)(?!\w)/, 'boolean']],
number: [
[/(?<!\w)((?:[\+\-]?(0|([1-9](([0-9]|_[0-9])+)?))))(?!\w)/, 'number'],
[
/(?<!\w)([\+\-]?(0|([1-9](([0-9]|_[0-9])+)?))(?:(?:\.([0-9]+))?[eE][\+\-]?[1-9]_?[0-9]*|(?:\.[0-9_]*)))(?!\w)/,
'number.float',
],
[/(?<!\w)((?:0x(([0-9a-fA-F](([0-9a-fA-F]|_[0-9a-fA-F])+)?))))(?!\w)/, 'number.hex'],
[/(?<!\w)(0o[0-7](_?[0-7])*)(?!\w)/, 'number.octal'],
[/(?<!\w)(0b[01](_?[01])*)(?!\w)/, 'number.binary'],
[/(?<!\w)([\+\-]?inf)(?!\w)/, 'number.inf'],
[/(?<!\w)([\+\-]?nan)(?!\w)/, 'number.nan'],
],
string: [
[/\\([btnfr"\\\n/ ]|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, 'string.escape'],
[/\\[^btnfr/"\\\n]/, 'string.escape.invalid'],
[/".+?"/, 'string'],
[/"""/, { token: 'string.block', bracket: '@close', next: '@pop' }],
[/'.+?'/, 'stringLiteral'],
[/'''/, { token: 'stringLiteral.block', bracket: '@close', next: '@pop' }],
],
table: [
[/^\s*(\[)([^\[\]]*)(\])/, 'table'],
[/^\s*(\[\[)([^\[\]]*)(\]\])/, 'table.array'],
[/(?<!\w)(\{)((.)+)(\})(?!\w)/, 'table.inline'],
],
},
});

// Define a new theme that constains only rules that match this language
window.monaco.editor.defineTheme('properties', {
base: 'vs',
inherit: false,
window.monaco.editor.defineTheme('vs-dark-enhanced', {
base: 'vs-dark',
inherit: true,
rules: [
{ token: 'key', foreground: '009968' },
{ token: 'value', foreground: '009968' },
{ token: 'comment', foreground: '666666' },
{ token: 'table', foreground: 'eee8aa' },
{ token: 'table.array', foreground: 'eee8aa' },
{ token: 'table.inline', foreground: 'eee8aa' },
{ token: 'string.block', foreground: 'ce9178' },
{ token: 'stringLiteral', foreground: 'ce9178' },
{ token: 'stringLiteral.block', foreground: 'ce9178' },
{ token: 'boolean', foreground: '3dc9b0' },
// { token: 'eq', foreground: '000000' },
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class ConfigDetail extends React.Component {
roundedSelection: false,
readOnly: true,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
wordWrapColumn: 120,
folding: false,
showFoldingControls: 'always',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ConfigEditor extends React.Component {
roundedSelection: false,
readOnly: false,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
wordWrapColumn: 120,
folding: false,
showFoldingControls: 'always',
Expand All @@ -140,7 +140,7 @@ class ConfigEditor extends React.Component {
roundedSelection: false,
readOnly: false,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
wordWrapColumn: 120,
folding: false,
showFoldingControls: 'always',
Expand Down Expand Up @@ -580,6 +580,7 @@ class ConfigEditor extends React.Component {
{ value: 'yaml', label: 'YAML' },
{ value: 'html', label: 'HTML' },
{ value: 'properties', label: 'Properties' },
{ value: 'toml', label: 'TOML' },
];
const activeKey = this.state.activeKey.split('-')[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const LANGUAGE_LIST = [
{ value: 'yaml', label: 'YAML' },
{ value: 'html', label: 'HTML' },
{ value: 'properties', label: 'Properties' },
{ value: 'toml', label: 'TOML' },
];

const TAB_LIST = ['production', 'beta'];
Expand Down Expand Up @@ -137,7 +138,7 @@ class ConfigEditor extends React.Component {
roundedSelection: false,
readOnly: false,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
folding: true,
showFoldingControls: 'always',
cursorStyle: 'line',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class NewConfig extends React.Component {
roundedSelection: false,
readOnly: false,
lineNumbersMinChars: true,
theme: 'vs-dark',
theme: 'vs-dark-enhanced',
folding: true,
showFoldingControls: 'always',
cursorStyle: 'line',
Expand Down Expand Up @@ -458,6 +458,10 @@ class NewConfig extends React.Component {
value: 'properties',
label: 'Properties',
},
{
value: 'toml',
label: 'TOML',
},
];
const { editorClass } = this.state;

Expand Down
15 changes: 15 additions & 0 deletions console-ui/src/utils/validateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as yaml from 'js-yaml';
import * as toml from '@iarna/toml';

/**
* 校验一个配置项
Expand Down Expand Up @@ -238,6 +239,19 @@ export default {
return hasProperty;
},

/**
* 检测toml是否合法
*/
validateToml(str) {
try {
// 如果不加这里的 replace 的话在 toml 的注释换行可能会出现以下错误:
// TomlError: Control characters (codes < 0x1f and 0x7f) are not allowed in comments
return toml.parse(str.replace(/\r\n/g, '\n'));
} catch (e) {
return false;
}
},

/**
* 根据类型验证类型
*/
Expand All @@ -249,6 +263,7 @@ export default {
html: this.validateXml,
properties: this.validateProperties,
yaml: this.validateYaml,
toml: this.validateToml,
};

if (!validateObj[type]) {
Expand Down
2 changes: 1 addition & 1 deletion console/src/main/resources/static/css/main.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions console/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<link rel="stylesheet" type="text/css" href="console-ui/public/css/icon.css">
<link rel="stylesheet" type="text/css" href="console-ui/public/css/font-awesome.css">
<!-- 第三方css结束 -->
<link href="./css/main.css?16f22c0c56795bc26e9a" rel="stylesheet"></head>
<link href="./css/main.css?a14e556e16f769484355" rel="stylesheet"></head>

<body>
<div id="root" style="overflow:hidden"></div>
Expand All @@ -56,6 +56,6 @@
<script src="console-ui/public/js/merge.js"></script>
<script src="console-ui/public/js/loader.js"></script>
<!-- 第三方js结束 -->
<script type="text/javascript" src="./js/main.js?16f22c0c56795bc26e9a"></script></body>
<script type="text/javascript" src="./js/main.js?a14e556e16f769484355"></script></body>

</html>
Loading
Loading