Skip to content

Commit

Permalink
fix: registerCompletionItemProvider in useEffect trigger (#162)
Browse files Browse the repository at this point in the history
* Update registerCompletionItemProvider trigger timing
* Code Format
  • Loading branch information
PisecesPeng authored Aug 16, 2022
1 parent c83f9b2 commit 3348fdb
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,22 @@ function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstan
}, [options.theme])

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)
};
}
});
}
if (editor.current && autoComplete) {
if (editor?.current?.getModel() && editor?.current?.getPosition()) {
const CPDisposable: monaco.IDisposable
= monaco.languages.registerCompletionItemProvider(language, {
provideCompletionItems: (model, position) => {
return {
suggestions: autoComplete(model, position)
};
}
});
}

setVal(value);
editor.current.setValue(value);
}
}, [value]);
return () => {
CPDisposable.dispose();
}
}, [language, autoComplete]);

useEffect(() => {
if (editor.current) {
Expand All @@ -147,7 +146,7 @@ function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstan
useEffect(() => {
if (editor.current) {
const optionsRaw = editor.current.getRawOptions();
;(Object.keys(optionsRaw) as (keyof editor.IEditorOptions)[]).forEach((keyname) => {
(Object.keys(optionsRaw) as (keyof editor.IEditorOptions)[]).forEach((keyname) => {
const propsOpt = options[keyname];
if (optionsRaw[keyname] !== propsOpt && propsOpt !== undefined) {
editor.current!.updateOptions({ [keyname]: propsOpt });
Expand All @@ -159,4 +158,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);

0 comments on commit 3348fdb

Please sign in to comment.