Skip to content
New issue

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

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

Already on GitHub? # to your account

自定义monaco开发时,功能不生效 #11122

Open
chaserstrong opened this issue Oct 28, 2024 · 1 comment
Open

自定义monaco开发时,功能不生效 #11122

chaserstrong opened this issue Oct 28, 2024 · 1 comment

Comments

@chaserstrong
Copy link

chaserstrong commented Oct 28, 2024

描述问题:

请用一段清晰简洁的文字描述问题是什么...
在写amis插件,想使用编辑器提供的扩展monaco的方法,实现输入$后,自动出现下拉补全的功能,但是实际功能不生效。

代码如下:

 {render('editorSupportars', {
          "type": "editor",
          "name": "editor",
          "label": "编辑器",
          "language": "json",
          "editorDidMount": (editor, monaco) => {
            const provider = monaco.languages.registerCompletionItemProvider('json', {
              provideCompletionItems: (model, position) => {
                // 获取当前行数
                const line = position.lineNumber
                // 获取当前列数
                const column = position.column
                // 获取当前输入行的所有内容
                const content = model.getLineContent(line)
                // 通过下标来获取当前光标后一个内容,即为刚输入的内容
                const sym = content[column - 2]
    

                let suggestions: any[] = []
                if (sym === '$') {
                  console.log('sym', sym)
                  suggestions = constValues.map((e) => ({
                    label: e,
                    kind: monaco.languages.CompletionItemKind.Keyword,
                    insertText: '{' + e + '}',
                    detail: '常量配置',
                  }))
                  console.log('suggestions', suggestions)
                }

                return {
                  suggestions: suggestions
                };
              },
              triggerCharacters: ['$']
            });

             // 修改编辑器配置,添加更多选项
            editor.updateOptions({
              quickSuggestions: {
                  other: true,
                  comments: true,
                  strings: true
              },
              suggestOnTriggerCharacters: true,
              acceptSuggestionOnCommitCharacter: true,
              wordBasedSuggestions: true,
              parameterHints: {
                  enabled: true
              }
            });

            // 手动添加触发建议的快捷键
            editor.addCommand(monaco.KeyCode.Space | monaco.KeyMod.CtrlCmd, () => {
                editor.trigger('keyboard', 'editor.action.triggerSuggest', {});
            });

            return provider;
          },
          triggerCharacters: ['$', 'a']
        }
        )}

补充:同样的代码,把language改成sql就能生效,改成json,就无效了。

@chaserstrong
Copy link
Author

@qkiroc 大佬知道么,比较急

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant