Skip to content

Commit

Permalink
add filer register
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankll committed Dec 17, 2023
1 parent 611d074 commit c14a7a9
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 209 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"vite:start": "vite",
"vite:build": "vue-tsc --noEmit && vite build",
"start": "tsc && cross-env APP_ENV=dev concurrently vite \" electron .\" \"tsc -w\"",
"start": "tsc && cross-env APP_ENV=dev DEBUG=dockit concurrently vite \" electron .\" \"tsc -w\"",
"build": "npm run vite:build && tsc",
"package": "npm run vite:build && tsc && electron-builder",
"test": "jest --runInBand --coverage --coverageReporters json-summary text html lcov",
Expand Down
180 changes: 180 additions & 0 deletions src/common/searchTokensProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
export const searchTokensProvider = {
// Set defaultToken to invalid to see what you do not tokenize yet
defaultToken: 'invalid',
tokenPostfix: '.search',

// keywords of elasticsearch
keywords: [
'GET',
'POST',
'PUT',
'DELETE',
'HEAD',
'OPTIONS',
'PATCH',
'TRACE',
'index',
'indices',
'type',
'types',
'from',
'size',
'explain',
'analyze',
'default_operator',
'df',
'analyzer',
'lenient',
'lowercase_expanded_terms',
'analyze_wildcard',
'all_shards',
'allow_no_indices',
'expand_wildcards',
'preference',
'routing',
'ignore_unavailable',
'allow_no_indices',
'ignore_throttled',
'search_type',
'batched_reduce_size',
'ccs_minimize_roundtrips',
'max_concurrent_shard_requests',
'pre_filter_shard_size',
'rest_total_hits_as_int',
'scroll',
'search_type',
'typed_keys',
'wait_for_active_shards',
'wait_for_completion',
'requests_per_second',
'slices',
'timeout',
'terminate_after',
'stats',
'version',
'version_type',
'if_seq_no',
'if_primary_term',
'refresh',
'routing',
'parent',
'preference',
'realtime',
'refresh',
'retry_on_conflict',
'timeout',
'version',
'version_type',
'if_seq_no',
'if_primary_term',
'pipeline',
'wait_for_active_shards',
'wait_for_completion',
'requests_per_second',
'slices',
'timeout',
'terminate_after',
'stats',
'version',
'version_type',
'if_seq_no',
'if_primary_term',
'refresh',
'routing',
'parent',
'preference',
'realtime',
'refresh',
'retry_on_conflict',
'timeout',
'version',
'version_type',
'if_seq_no',
'if_primary_term',
'pipeline',
'wait_for_active_shards',
'wait_for_completion',
'requests_per_second',
'slices',
'timeout',
'terminate_after',
'stats',
'version',
'version_type',
'_search',
],

typeKeywords: ['any', 'boolean', 'number', 'object', 'string', 'undefined'],

// we include these common regular expressions
symbols: /[=><!~?:&|+\-*\\^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
digits: /\d+(_+\d+)*/,
octaldigits: /[0-7]+(_+[0-7]+)*/,
binarydigits: /[0-1]+(_+[0-1]+)*/,
hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,

// The main tokenizer for our languages
tokenizer: {
root: [
[/^(GET|DELETE|POST|PUT)\s\w+/, 'action-execute-decoration'],
[/[{}]/, 'delimiter.bracket'],
{ include: 'common' },
],

common: [
// identifiers and keywords
[
/[a-z_$][\w$]*/,
{
cases: {
'@typeKeywords': 'keyword',
'@keywords': 'keyword',
'@default': 'identifier',
},
},
],

// whitespace
{ include: '@whitespace' },
// json block
{ include: '@json' },
],

json: [
// JSON strings
[/"(?:\\.|[^\\"])*"/, 'string'],

// JSON numbers
[/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, 'number'],

// JSON booleans
[/\b(?:true|false)\b/, 'keyword'],

// JSON null
[/\bnull\b/, 'keyword'],

// JSON property names
[/"(?:\\.|[^\\"])*"(?=\s*:)/, 'key'],

// JSON punctuation
[/[{}[\],:]/, 'delimiter'],

// JSON whitespace
{ include: '@whitespace' },
],

whitespace: [
[/[ \t\r\n]+/, ''],
[/\/\*\*(?!\/)/, 'comment.doc'],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
],

comment: [
[/[^/*]+/, 'comment'],
[/\*\//, 'comment', '@pop'],
[/[/*]/, 'comment'],
],
},
};
3 changes: 3 additions & 0 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { debug } from '../common/debug';
import { githubLink } from '../config';
import Store from 'electron-store';
import { registerStoreApiListener } from './storeApi';
import { registerSourceFileApiListener } from './sourceFIleApi';

const isDev = process.env.APP_ENV === 'dev';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -100,7 +101,9 @@ const createWindow = async () => {
ipcMain.on('open-github', () => {
shell.openExternal(githubLink);
});

registerStoreApiListener(ipcMain);
registerSourceFileApiListener(ipcMain);

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
Expand Down
10 changes: 10 additions & 0 deletions src/electron/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ const menuTemplate = [
},
],
},
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
selector: 'save:',
},
],
},
{
label: 'Edit',
submenu: [
Expand Down
6 changes: 6 additions & 0 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ contextBridge.exposeInMainWorld('storeAPI', {
set: async (key: string, value: unknown) =>
ipcRenderer.invoke('storeAPI', { method: 'SET', key, value }),
});

contextBridge.exposeInMainWorld('sourceFileAPI', {
saveFile: async (content: string) =>
ipcRenderer.invoke('sourceFileAPI', { method: 'SAVE_FILE', content }),
readFile: async () => ipcRenderer.invoke('sourceFileAPI', { method: 'READ_FILE' }),
});
57 changes: 57 additions & 0 deletions src/electron/sourceFIleApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Electron from 'electron';
import { readFile, writeFile, mkdir } from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
import { debug } from '../common/debug';

export enum sourceFileApiMethods {
SAVE_FILE = 'SAVE_FILE',
READ_FILE = 'READ_FILE',
}

export type SourceFileApiInput = {
method: sourceFileApiMethods;
fileName: string;
content: string;
};

const saveFile = async (filePath: string, content: string) => {
const resolvedPath = path.resolve(__dirname, filePath);
try {
await writeFile(resolvedPath, content, 'utf-8');
debug('save file success');
} catch (err) {
debug(`saveFile error: ${err}`);
throw err;
}
};

const readFromFile = async (filePath: string) => {
const resolvedPath = path.resolve(__dirname, filePath);
debug(`resolvedPath: ${resolvedPath}`);
if (!existsSync(resolvedPath)) {
debug('File does not exist. Creating a new file...');
await mkdir(path.dirname(resolvedPath), { recursive: true });
await writeFile(resolvedPath, '', { encoding: 'utf-8', flag: 'w+' });
return '';
}

try {
return await readFile(resolvedPath, 'utf-8');
} catch (err) {
debug(`readFromFile error: ${err}`);
throw err;
}
};

const sourceFileApi: { [key: string]: (filePath: string, content: string) => unknown } = {
save_file: (filePath: string, content: string) => saveFile(filePath, content),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
read_file: (filePath: string, _: string) => readFromFile(filePath),
};

export const registerSourceFileApiListener = (ipcMain: Electron.IpcMain) => {
ipcMain.handle('sourceFileAPI', (_, { content, method }: SourceFileApiInput) =>
sourceFileApi[method.toLowerCase()]('./data/default.search', content),
);
};
2 changes: 2 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export default store;

export * from './appStore';
export * from './userStore';
export * from './connectionStore';
export * from './sourceFileStore';
20 changes: 20 additions & 0 deletions src/store/sourceFileStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineStore } from 'pinia';

const { sourceFileAPI } = window;
export const useSourceFileStore = defineStore('sourceFileStore', {
state(): { defaultFile: string } {
return {
defaultFile: '',
};
},
getters: {},
actions: {
async readSourceFromFile() {
this.defaultFile = await sourceFileAPI.readFile();
},
async saveSourceToFile(content: string) {
this.defaultFile = content;
await sourceFileAPI.saveFile(content);
},
},
});
2 changes: 1 addition & 1 deletion src/views/connect/components/connect-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<script setup lang="ts">
import { MoreOutlined } from '@vicons/antd';
import { storeToRefs } from 'pinia';
import { Connection, useConnectionStore } from '../../../store/connectionStore';
import { useLang } from '../../../lang';
import { Connection, useConnectionStore } from '../../../store';
const emits = defineEmits(['edit-connect']);
Expand Down
Loading

0 comments on commit c14a7a9

Please sign in to comment.