Skip to content

Commit

Permalink
fix: bypass the interaction from monaco instance when using multiple …
Browse files Browse the repository at this point in the history
…components in the same page
  • Loading branch information
legends-killer committed Jan 15, 2024
1 parent cf46d39 commit 75f10da
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yaje",
"version": "1.0.1",
"version": "1.0.3",
"author": "legends-killer <yyy@legends-killer.cq.cn>",
"description": "yet another json editor",
"keywords": [
Expand Down
13 changes: 8 additions & 5 deletions src/JsonEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: legends-killer
* @Date: 2023-12-27 18:47:08
* @LastEditors: legends-killer
* @LastEditTime: 2023-12-28 23:43:39
* @LastEditTime: 2024-01-15 21:19:23
* @Description:
*/
import { Editor, EditorProps, useMonaco } from '@monaco-editor/react'
Expand All @@ -11,6 +11,7 @@ import { refItemProvider } from './provider/refItemProvider'
import { ISuggestionItem, JsonSchemaProcessor } from './helper/jsonSchemaProcessor'
import { languages as Languages, IDisposable } from 'monaco-editor'
import { IProviderParam } from './provider/types'
import { filterRepeateSuggestions } from '../utils'

export interface IJsonEditor extends Partial<EditorProps> {
/**
Expand Down Expand Up @@ -72,17 +73,19 @@ export const JsonEditor = forwardRef((props: IJsonEditor, ref: any) => {

// init prompt
useEffect(() => {
if (promptJsonSchema) {
if (promptJsonSchema && !(window as any).__YAJE_INIT__) {
const jsonSchemaProcessor = new JsonSchemaProcessor(promptJsonSchema)
const suggestions = jsonSchemaProcessor.getSuggestions()
setFinalKeySuggestions(suggestions.keySuggestions.concat(keySuggesions))
setFinalValueSuggestions(suggestions.valueSuggestions.concat(valueSuggestions))
setFinalKeySuggestions(filterRepeateSuggestions(suggestions.keySuggestions.concat(keySuggesions)))
setFinalValueSuggestions(filterRepeateSuggestions(suggestions.valueSuggestions.concat(valueSuggestions)))
}
// use this to bypass multiple monaco instance interation
// FIXME this is a hack, related issue: https://github.com/microsoft/monaco-editor/issues/3378
(window as any).__YAJE_INIT__ = true
}, [keySuggesions, promptJsonSchema, valueSuggestions])

// init monaco
useEffect(() => {
console.log(finalKeySuggestions, finalValueSuggestions)
if (monaco) {
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
Expand Down
4 changes: 1 addition & 3 deletions src/JsonEditor/provider/refItemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: legends-killer
* @Date: 2023-12-27 19:43:15
* @LastEditors: legends-killer
* @LastEditTime: 2023-12-28 17:00:14
* @LastEditTime: 2024-01-15 21:19:34
* @Description:
*/
import { languages as Languages } from 'monaco-editor'
Expand All @@ -19,14 +19,12 @@ export const refItemProvider = (param: IProviderParam): Languages.CompletionItem
const currentLine = model.getLineContent(position.lineNumber)
const currentEditorValue = model.getValue()
const currentCursorType = getCurrentCursorType(currentEditorValue, position)
console.log('currentCursorType', currentCursorType)
const jsonSuggestions = currentCursorType === JSON_FIELD_TYPE.KEY ? jsonKeySuggestions : jsonValueSuggestions

const suggestions = jsonSuggestions.map((jsonSuggestion) => {
monaco.editor.registerCommand(
`accept.completeItem.suggestion-${jsonSuggestion.title}`,
(_accessor: any, ...args: any[]) => {
console.log('accept.completeItem.suggestion', args)
if (onSuggestionItemSelect) onSuggestionItemSelect(jsonSuggestion)
}
)
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* @Author: legends-killer
* @Date: 2024-01-15 21:02:24
* @LastEditors: legends-killer
* @LastEditTime: 2024-01-15 21:02:56
* @Description:
*/
import { ISuggestionItem } from "./JsonEditor/helper/jsonSchemaProcessor";
export const filterRepeateSuggestions = (suggestions: ISuggestionItem[]) => {
const suggestionsMap = new Map<string, ISuggestionItem>()
suggestions.forEach((suggestion) => {
suggestionsMap.set(suggestion.title, suggestion)
})
return Array.from(suggestionsMap.values())
}

0 comments on commit 75f10da

Please sign in to comment.