Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user provided auto-complete #47

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useImperativeHandle, useEffect, useRef, useState } from 'react';
import * as monaco from 'monaco-editor';
import { editor } from 'monaco-editor';
import {editor, languages} from 'monaco-editor';

function noop() {}

Expand Down Expand Up @@ -29,6 +29,10 @@ export interface MonacoEditorProps extends Omit<React.HTMLAttributes<HTMLDivElem
* To not create automatically a model, use `model: null`.
*/
language?: monaco.editor.IStandaloneEditorConstructionOptions['language'];
/**
* User provided extension function provider for auto-complete.
*/
autoComplete?: (model: monaco.editor.ITextModel, position: monaco.Position) => languages.CompletionItem[];
/**
* Initial theme to be used for rendering.
* The current out-of-the-box available themes are: 'vs' (default), 'vs-dark', 'hc-black'.
Expand Down Expand Up @@ -57,7 +61,7 @@ export interface RefEditorInstance {
}

function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstance) => void) | React.RefObject<RefEditorInstance> | null | undefined) {
const { width = '100%', height = '100%', value = '', theme = '', language = 'javascript', options = {}, editorDidMount = noop, onChange = noop, defaultValue = '', ...other } = props;
const { width = '100%', height = '100%', value = '', theme = '', language = 'javascript', autoComplete, options = {}, editorDidMount = noop, onChange = noop, defaultValue = '', ...other } = props;
options.language = language || options.language;
options.theme = theme || options.theme;
const [val, setVal] = useState(defaultValue);
Expand Down Expand Up @@ -86,6 +90,18 @@ function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstan

useEffect(() => {
if (value !== val && editor.current) {
if (autoComplete) {
if (editor?.current?.getModel() && editor?.current?.getPosition()) {
monaco.languages.registerCompletionItemProvider(language, {
provideCompletionItems: (model, position) => {
return {
suggestions: autoComplete(model, position)
};
}
});
}
}

setVal(value);
editor.current.setValue(value);
}
Expand Down Expand Up @@ -115,4 +131,4 @@ function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstan
return <div {...other} ref={container} style={{ ...other.style, width, height }} />;
}

export default React.forwardRef<RefEditorInstance, MonacoEditorProps>(MonacoEditor);
export default React.forwardRef<RefEditorInstance, MonacoEditorProps>(MonacoEditor);