Skip to content

Commit

Permalink
feat: show error msg when json parse failed #24
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Mar 22, 2024
1 parent 6610a57 commit 46fdf90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/lang/enUS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const enUS = {
},
editor: {
establishedRequired: 'Select a DB instance before execute actions',
invalidJson: 'Invalid JSON format',
},
history: {
empty: 'No history yet',
Expand Down
1 change: 1 addition & 0 deletions src/lang/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const zhCN = {
},
editor: {
establishedRequired: '请选择执行操作的数据库实例',
invalidJson: '无效的 JSON 格式',
},
history: {
empty: '无历史记录',
Expand Down
48 changes: 33 additions & 15 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,33 @@ const setupQueryEditor = (code: string) => {
language: 'search',
});
autoIndentCmdId = queryEditor.addCommand(0, (ctx, args) => {
if (args) {
const { startLineNumber, endLineNumber } = args as {
startLineNumber: number;
endLineNumber: number;
};
autoIndentCmdId = queryEditor.addCommand(
0,
(
ctx,
args:
| {
startLineNumber: number;
endLineNumber: number;
}
| undefined,
) => {
if (!args) {
return;
}
const model = queryEditor?.getModel();
if (!model) {
return;
}
if (model) {
const content = model.getValueInRange({
startLineNumber,
startColumn: 1,
endLineNumber: endLineNumber,
endColumn: model.getLineLength(endLineNumber) + 1,
});
const { startLineNumber, endLineNumber } = args;
const content = model.getValueInRange({
startLineNumber,
startColumn: 1,
endLineNumber: endLineNumber,
endColumn: model.getLineLength(endLineNumber) + 1,
});
try {
const formatted = JSON.stringify(JSON.parse(content), null, 2);
model.pushEditOperations(
[],
Expand All @@ -220,9 +232,15 @@ const setupQueryEditor = (code: string) => {
],
inverseEditOperations => [],
);
} catch (err) {
message.error(lang.t('editor.invalidJson'), {
closable: true,
keepAliveOnHover: true,
});
return;
}
}
});
},
);
queryEditor.onMouseDown(({ event, target }) => {
if (
Expand Down

0 comments on commit 46fdf90

Please sign in to comment.