Skip to content

Commit

Permalink
feat: implement shortcuts of open documentation for current request #36
Browse files Browse the repository at this point in the history
… (#39)

#36

Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll authored Mar 31, 2024
1 parent fb7e824 commit 470981d
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 8 deletions.
37 changes: 37 additions & 0 deletions docs/dockit-actions.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Index page
this docs folder contains the documentation for the project.

135 changes: 133 additions & 2 deletions src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const executeActions = {
decorationClassName: 'action-execute-decoration',
};

export type SearchToken = {
export type SearchAction = {
qdsl: string;
actionPosition: Range;
qdslPosition: Range;
Expand Down Expand Up @@ -247,7 +247,7 @@ export const buildSearchToken = (lines: Array<{ lineNumber: number; lineContent:
endColumn: lines[endLineNumber].lineContent.length,
}
: null,
} as SearchToken;
} as SearchAction;
});
};

Expand Down Expand Up @@ -343,3 +343,134 @@ POST _bulk
{"name": "Document 1"}
{"delete": {"_index": "dockit_sample_index", "_id": "2"}}
`;
export enum ActionType {
POST_INDEX = 'POST_INDEX',
POST_SEARCH = 'POST_SEARCH',
POST_COUNT = 'POST_COUNT',
GET_SEARCH = 'GET_SEARCH',
POST_UPDATE = 'POST_UPDATE',
DELETE_DOC = 'DELETE_DOC',
PUT_INDEX = 'PUT_INDEX',
DELETE_INDEX = 'DELETE_INDEX',
POST_BULK = 'POST_BULK',
PUT_PUT_INDEX = 'PUT_PUT_INDEX',
PUT_MAPPING = 'PUT_MAPPING',
GET_MAPPING = 'GET_MAPPING',
POST_ALIAS = 'POST_ALIAS',
GET_HEALTH = 'GET_HEALTH',
GET_STATE = 'GET_STATE',
GET_INFO = 'GET_INFO',
HEAD_INDEX = 'HEAD_INDEX',
PUT_AUTO_FOLLOW = 'PUT_AUTO_FOLLOW',
PUT_CCR_FOLLOW = 'PUT_CCR_FOLLOW',
PUT_SLM_POLICY = 'PUT_SLM_POLICY',
PUT_SECURITY_ROLE_MAPPING = 'PUT_SECURITY_ROLE_MAPPING',
PUT_ROLLUP_JOB = 'PUT_ROLLUP_JOB',
PUT_SECURITY_API_KEY = 'PUT_SECURITY_API_KEY',
PUT_INGEST_PIPELINE = 'PUT_INGEST_PIPELINE',
PUT_TRANSFORM = 'PUT_TRANSFORM',
POST_ML_INFER = 'POST_ML_INFER',
POST_MULTI_SEARCH = 'POST_MULTI_SEARCH',
POST_OPEN_INDEX = 'POST_OPEN_INDEX',
PUT_COMPONENT_TEMPLATE = 'PUT_COMPONENT_TEMPLATE',
PUT_ENRICH_POLICY = 'PUT_ENRICH_POLICY',
PUT_TEMPLATE = 'PUT_TEMPLATE',
}

const actionRegexMap: { [key in ActionType]: RegExp } = {
POST_INDEX: /POST \/_doc\/\d+/,
POST_SEARCH: /POST \/_search/,
POST_COUNT: /POST \/_count/,
GET_SEARCH: /GET \/_doc\/\d+/,
POST_UPDATE: /POST \/_update\/\d+/,
DELETE_DOC: /DELETE \/_doc\/\d+/,
PUT_INDEX: /PUT /,
DELETE_INDEX: /DELETE /,
POST_BULK: /POST \/_bulk/,
PUT_PUT_INDEX: /PUT /,
PUT_MAPPING: /PUT \/_mapping/,
GET_MAPPING: /GET \/_mapping/,
POST_ALIAS: /POST \/_aliases/,
GET_HEALTH: /GET \/_cluster\/health/,
GET_STATE: /GET \/_cluster\/state/,
GET_INFO: /GET \/_nodes\/info/,
HEAD_INDEX: /HEAD /,
PUT_AUTO_FOLLOW: /PUT \/_ccr\/auto_follow\/\w+/,
PUT_CCR_FOLLOW: /PUT \/_ccr\/follow/,
PUT_SLM_POLICY: /PUT \/_slm\/policy\/\w+/,
PUT_SECURITY_ROLE_MAPPING: /PUT \/_security\/role_mapping\/\w+/,
PUT_ROLLUP_JOB: /PUT \/_rollup\/job\/\w+/,
PUT_SECURITY_API_KEY: /PUT \/_security\/api_key/,
PUT_INGEST_PIPELINE: /PUT \/_ingest\/pipeline\/\w+/,
PUT_TRANSFORM: /PUT \/_transform\/\w+/,
POST_ML_INFER: /POST \/_ml\/infer\/\w+/,
POST_MULTI_SEARCH: /POST \/_msearch/,
POST_OPEN_INDEX: /POST \/_open/,
PUT_COMPONENT_TEMPLATE: /PUT \/_component_template\/\w+/,
PUT_ENRICH_POLICY: /PUT \/_enrich\/policy\/\w+/,
PUT_TEMPLATE: /PUT \/_template\/\w+/,
};
export enum EngineType {
ELASTICSEARCH = 'ELASTICSEARCH',
OPENSEARCH = 'OPENSEARCH',
}

export const getActionApiDoc = (engine: EngineType, version: string, action: SearchAction) => {
const { APIS } = getDocLinks(engine, version);
const matchedAction = Object.entries(actionRegexMap).find(([, regex]: [ActionType, RegExp]) =>
`${action.method} /${action.path}`.match(regex),
);

return matchedAction ? APIS[matchedAction[0] as ActionType] : undefined;
};

const getDocLinks = (engine: EngineType, version: string) => {
const DOCS_LINK = `https://www.elastic.co/guide/en/elasticsearch/reference/${version}`;
const linksMap: {
[key in EngineType]: {
APIS: {
[key in ActionType]: string;
};
};
} = {
[EngineType.ELASTICSEARCH]: {
APIS: {
POST_INDEX: `${DOCS_LINK}/indices-create-index.html`,
POST_SEARCH: `${DOCS_LINK}/search-search.html`,
POST_COUNT: `${DOCS_LINK}/search-count.html`,
GET_SEARCH: `${DOCS_LINK}/docs-get.html`,
POST_UPDATE: `${DOCS_LINK}/docs-update.html`,
DELETE_DOC: `${DOCS_LINK}/docs-delete.html`,
PUT_INDEX: `${DOCS_LINK}/indices-create-index.html`,
DELETE_INDEX: `${DOCS_LINK}/indices-delete-index.html`,
POST_BULK: `${DOCS_LINK}/docs-bulk.html`,
PUT_PUT_INDEX: `${DOCS_LINK}/indices-create-index.html`,
PUT_MAPPING: `${DOCS_LINK}/indices-put-mapping.html`,
GET_MAPPING: `${DOCS_LINK}/indices-get-mapping.html`,
POST_ALIAS: `${DOCS_LINK}/indices-aliases.html`,
GET_HEALTH: `${DOCS_LINK}/cluster-health.html`,
GET_STATE: `${DOCS_LINK}/indices-stats.html`,
GET_INFO: '',
HEAD_INDEX: `${DOCS_LINK}/indices-exists.html`,
PUT_AUTO_FOLLOW: `${DOCS_LINK}/ccr-put-auto-follow-pattern.html`,
PUT_CCR_FOLLOW: `${DOCS_LINK}/ccr-put-follow.html`,
PUT_SLM_POLICY: `${DOCS_LINK}/slm-api-put-policy.html`,
PUT_SECURITY_ROLE_MAPPING: `${DOCS_LINK}/security-api-put-role-mapping.html`,
PUT_ROLLUP_JOB: `${DOCS_LINK}/rollup-put-job.html#rollup-put-job-api-request-body`,
PUT_SECURITY_API_KEY: `${DOCS_LINK}/security-api-create-api-key.html`,
PUT_INGEST_PIPELINE: `${DOCS_LINK}/put-pipeline-api.html`,
PUT_TRANSFORM: `${DOCS_LINK}/put-transform.html#put-transform-request-body`,
POST_ML_INFER: `${DOCS_LINK}/infer-trained-model.html`,
POST_MULTI_SEARCH: `${DOCS_LINK}/search-multi-search.html`,
POST_OPEN_INDEX: `${DOCS_LINK}/indices-open-close.html`,
PUT_COMPONENT_TEMPLATE: `${DOCS_LINK}/indices-component-template.html`,
PUT_ENRICH_POLICY: `${DOCS_LINK}/put-enrich-policy-api.html`,
PUT_TEMPLATE: `${DOCS_LINK}/indices-templates-v1.html`,
},
},
// @TODO docs link for OpenSearch
[EngineType.OPENSEARCH]: { APIS: {} } as { APIS: { [key in ActionType]: string } },
};

return linksMap[engine];
};
3 changes: 3 additions & 0 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ app.on('activate', async () => {
ipcMain.on('open-github', () => {
shell.openExternal(githubLink);
});
ipcMain.on('open-link', (_event, link: string) => {
shell.openExternal(link);
});

registerStoreApiListener(ipcMain);
registerSourceFileApiListener(ipcMain);
Expand Down
2 changes: 2 additions & 0 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ contextBridge.exposeInMainWorld('browserWindow', {

contextBridge.exposeInMainWorld('electronAPI', {
openGitHub: () => ipcRenderer.send('open-github'),
openLink: (link: string) => ipcRenderer.send('open-link', link),
});

contextBridge.exposeInMainWorld('storeAPI', {
get: async (key: string, defaultValue: unknown) =>
ipcRenderer.invoke('storeAPI', { method: 'GET', key, value: defaultValue }),
Expand Down
26 changes: 20 additions & 6 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
CustomError,
Decoration,
defaultCodeSnippet,
SearchToken,
EngineType,
getActionApiDoc,
SearchAction,
searchTokensProvider,
} from '../../common';
import { useAppStore, useConnectionStore, useSourceFileStore } from '../../store';
Expand Down Expand Up @@ -94,9 +96,9 @@ let autoIndentCmdId: string | null = null;
const queryEditorRef = ref();
const displayEditorRef = ref();
let searchTokens: SearchToken[] = [];
let searchTokens: SearchAction[] = [];
const getActionMarksDecorations = (searchTokens: SearchToken[]): Array<Decoration> => {
const getActionMarksDecorations = (searchTokens: SearchAction[]): Array<Decoration> => {
return searchTokens
.map(({ actionPosition }) => ({
id: actionPosition.startLineNumber,
Expand All @@ -107,15 +109,15 @@ const getActionMarksDecorations = (searchTokens: SearchToken[]): Array<Decoratio
.sort((a, b) => (a as Decoration).id - (b as Decoration).id) as Array<Decoration>;
};
const refreshActionMarks = (editor: Editor, searchTokens: SearchToken[]) => {
const refreshActionMarks = (editor: Editor, searchTokens: SearchAction[]) => {
const freshedDecorations = getActionMarksDecorations(searchTokens);
// @See https://github.com/Microsoft/monaco-editor/issues/913#issuecomment-396537569
executeDecorations = editor.deltaDecorations(
executeDecorations as Array<string>,
freshedDecorations,
) as unknown as Decoration[];
};
const buildCodeLens = (searchTokens: SearchToken[]) =>
const buildCodeLens = (searchTokens: SearchAction[]) =>
searchTokens
.filter(({ qdslPosition }) => qdslPosition)
.map(({ actionPosition, qdslPosition }, index) => ({
Expand Down Expand Up @@ -243,7 +245,7 @@ const autoIndentAction = (
}
};
const getPointerAction = (editor: Editor, tokens: Array<SearchToken>) => {
const getPointerAction = (editor: Editor, tokens: Array<SearchAction>) => {
if (!editor) {
return;
}
Expand Down Expand Up @@ -341,6 +343,18 @@ const setupQueryEditor = (code: string) => {
queryEditor.trigger('keyboard', 'editor.foldAll', {});
queryEditor.trigger('keyboard', 'editor.unfoldRecursively', {});
});
// Open the documentation for the current action
queryEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Slash, () => {
const docLink = getActionApiDoc(
EngineType.ELASTICSEARCH,
'current',
getPointerAction(queryEditor, searchTokens),
);
if (docLink) {
window.electronAPI.openLink(docLink);
}
});
};
const setupJsonEditor = () => {
displayEditor = monaco.editor.create(displayEditorRef.value, {
Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare module '*.css';

export interface IElectronAPI {
openGitHub: () => void;
openLink: (link: string) => void;
}
export interface IStoreAPI {
get: <T>(key: string, defaultValue: T) => Promise<T>;
Expand Down

0 comments on commit 470981d

Please sign in to comment.