Skip to content

Commit

Permalink
feat: support ob editor
Browse files Browse the repository at this point in the history
  • Loading branch information
HSunboy committed Apr 12, 2024
1 parent 5830093 commit caa79d1
Show file tree
Hide file tree
Showing 10 changed files with 2,114 additions and 94 deletions.
79 changes: 79 additions & 0 deletions web/components/chat/OBEditor/monaco-editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
import Editor, { OnChange, loader } from '@monaco-editor/react';
import classNames from 'classnames';
import { useContext, useMemo } from 'react';
import { formatSql } from '@/utils';
import { getModelService } from './service';
import { useLatest } from 'ahooks';
import { ChatContext } from '@/app/chat-context';
import { github, githubDark } from './theme';

loader.config({ monaco });

export interface ISession {
getTableList: (schemaName?: string) => Promise<string[]>;
getTableColumns: (tableName: string) => Promise<{ columnName: string; columnType: string }[]>;
getSchemaList: () => Promise<string[]>;
}

interface MonacoEditorProps {
className?: string;
value: string;
language: string;
onChange?: OnChange;
thoughts?: string;
session?: ISession;

}

let plugin = null;
monaco.editor.defineTheme('github', github as any);
monaco.editor.defineTheme('githubDark', githubDark as any);

export default function MonacoEditor({ className, value, language = 'mysql', onChange, thoughts, session }: MonacoEditorProps) {
// merge value and thoughts
const editorValue = useMemo(() => {
if (language !== 'mysql') {
return value;
}
if (thoughts && thoughts.length > 0) {
return formatSql(`-- ${thoughts} \n${value}`);
}
return formatSql(value);
}, [value, thoughts]);

const sessionRef = useLatest(session);

const context = useContext(ChatContext);

async function pluginRegister(editor: monaco.editor.IStandaloneCodeEditor) {
console.log('register plugin');
const module = await import('./ob-plugin');
const plugin = module.register();
plugin.setModelOptions(
editor.getModel()?.id || '',
getModelService({
modelId: editor.getModel()?.id || '',
delimiter: ';',
}, () => sessionRef.current || null)
)
}


return (
<Editor
className={classNames(className)}
onMount={pluginRegister}
value={editorValue}
defaultLanguage={language}
onChange={onChange}
theme={context?.mode !== "dark" ? "github" : "githubDark"}
options={{
minimap: {
enabled: false,
},
wordWrap: 'on',
}}
/>
);
}
13 changes: 13 additions & 0 deletions web/components/chat/OBEditor/ob-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import Plugin from '@oceanbase-odc/monaco-plugin-ob';

let plugin: Plugin;

export function register(): Plugin {
if (plugin) {
return plugin;
}
plugin = new Plugin();
plugin.setup();
return plugin;
}
22 changes: 22 additions & 0 deletions web/components/chat/OBEditor/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { IModelOptions } from '@oceanbase-odc/monaco-plugin-ob/dist/type';
import { ISession } from './monaco-editor';


export function getModelService(
{ modelId, delimiter }: { modelId: string; delimiter: string },
session?: () => ISession | null
): IModelOptions {
return {
delimiter,
async getTableList(schemaName?: string) {
console.log('getTableList')
return session?.()?.getTableList(schemaName) || []
},
async getTableColumns(tableName: string, dbName?: string) {
return session?.()?.getTableColumns(tableName) || []
},
async getSchemaList() {
return session?.()?.getSchemaList() || []
},
};
}
Loading

0 comments on commit caa79d1

Please sign in to comment.