Skip to content

Commit

Permalink
#10 fetch data from es
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankll committed Dec 29, 2023
1 parent cf5796f commit 71b8d91
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const BrowserWindowOptions: BrowserWindowConstructorOptions = {
webPreferences: {
preload: path.resolve(__dirname, 'preload.js'),
devTools: isDev,
webSecurity: false,
},
};

Expand Down
6 changes: 6 additions & 0 deletions src/lang/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const enUS = {
cancel: 'Cancel',
removeSuccess: 'Connection removed successfully',
},
editor: {
establishedRequired: 'Select a DB instance before execute actions',
},
};

const zhCN = {
Expand Down Expand Up @@ -89,6 +92,9 @@ const zhCN = {
cancel: '取消',
removeSuccess: '连接删除成功',
},
editor: {
establishedRequired: '请选择执行操作的数据库实例',
},
};

const langType = localStorage.lang || 'auto';
Expand Down
29 changes: 29 additions & 0 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,34 @@ export const useConnectionStore = defineStore('connectionStore', {
establishConnection(connection: Connection) {
this.established = connection;
},
async searchQDSL(index: string | undefined, qdsl: string) {
const url = index
? `${this.established?.host}:${this.established?.port}/${index}/_search`
: `${this.established?.host}:${this.established?.port}/_search?`;

// eslint-disable-next-line no-console
console.log(`searchQDSL_URL ${url}`);
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: qdsl,
});
const data = await response.json();
if (!response.ok) throw new CustomError(response.status, data);
return data;
} catch (err) {
// eslint-disable-next-line no-console
console.log(`searchQDSL error ${JSON.stringify({ err })}`);

if (err instanceof CustomError) {
throw new CustomError(err.status, err.details);
}
if (err instanceof Error) {
throw new CustomError(500, err.message);
}
throw new CustomError(500, `unknown error, trace: ${JSON.stringify(err)}`);
}
},
},
});
62 changes: 45 additions & 17 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
<script setup lang="ts">
import * as monaco from 'monaco-editor';
import { storeToRefs } from 'pinia';
import { searchTokensProvider } from '../../common';
import { useAppStore, useSourceFileStore } from '../../store';
import { CustomError, searchTokensProvider } from '../../common';
import { useAppStore, useSourceFileStore, useConnectionStore } from '../../store';
import { useLang } from '../../lang';
const appStore = useAppStore();
const message = useMessage();
const lang = useLang();
const sourceFileStore = useSourceFileStore();
const { readSourceFromFile } = sourceFileStore;
const { defaultFile } = storeToRefs(sourceFileStore);
readSourceFromFile();
const connectionStore = useConnectionStore();
const { searchQDSL } = connectionStore;
const { established } = storeToRefs(connectionStore);
/**
* refer https://github.com/wobsoriano/codeplayground
* https://github.com/wobsoriano/codeplayground/blob/master/src/components/MonacoEditor.vue
Expand Down Expand Up @@ -54,6 +61,7 @@ type Decoration = {
range: monaco.Range;
options: { isWholeLine: boolean; linesDecorationsClassName: string };
};
let executeDecorations: Array<Decoration> = [];
const getActionMarksDecorations = (editor: monaco.Editor): Array<Decoration> => {

Check failure on line 67 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'"/Users/runner/work/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?

Check failure on line 67 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'"/home/runner/work/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?

Check failure on line 67 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'"D:/a/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?
Expand Down Expand Up @@ -98,20 +106,40 @@ const getAction = (editor: monaco.Editor, startLine: number) => {
return { payload, action };
};
const executeQueryAction = (
const executeQueryAction = async (
editor: monaco.Editor,

Check failure on line 111 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'"/Users/runner/work/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?

Check failure on line 111 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'"/home/runner/work/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?

Check failure on line 111 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'"D:/a/dockit/dockit/node_modules/monaco-editor/esm/vs/editor/editor.api"' has no exported member named 'Editor'. Did you mean 'editor'?
position: { column: number; lineNumber: number },
) => {
const { action, payload } = getAction(editor, position.lineNumber);
try {
// eslint-disable-next-line no-console
console.log(
`executeQueryAction ${JSON.stringify({
payload,
action,
})}`,
);
if (!established.value) {
message.error(lang.t('editor.establishedRequired'), {
closable: true,
keepAliveOnHover: true,
duration: 3000,
});
return;
}
// eslint-disable-next-line no-console
console.log(
`executeQueryAction ${JSON.stringify({
executeDecorations,
payload,
action,
})}`,
);
const data = await searchQDSL(undefined, payload);
// eslint-disable-next-line no-console
console.log(`data ${JSON.stringify({ data })}`);
} catch (err) {
const { status, details } = err as CustomError;
message.error(`status: ${status}, details: ${details}`, {
closable: true,
keepAliveOnHover: true,
duration: 36000000,
});
}
};
onMounted(() => {
Expand All @@ -123,14 +151,14 @@ onMounted(() => {
});
editorView.value = editor;
// Register language injection rule
editor.onKeyUp(e => refreshActionMarks(editor));
editor.onMouseDown(e => {
editor.onKeyUp(event => refreshActionMarks(editor));
editor.onMouseDown(({ event, target }) => {
if (
e.event.leftButton &&
e.target.type === 4 &&
Object.values(e.target!.element!.classList).includes(executionGutterClass)
event.leftButton &&
target.type === 4 &&
Object.values(target!.element!.classList).includes(executionGutterClass)
) {
executeQueryAction(editor, e.target.position);
executeQueryAction(editor, target.position);
}
});
});
Expand Down

0 comments on commit 71b8d91

Please sign in to comment.