baidu / amis

前端低代码框架,通过 JSON 配置就能生成各种页面。
https://baidu.github.io/amis/
Apache License 2.0
17.41k stars 2.52k forks source link

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

Open chaserstrong opened 3 weeks ago

chaserstrong commented 3 weeks ago

描述问题:

请用一段清晰简洁的文字描述问题是什么... 在写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 commented 3 weeks ago

@qkiroc 大佬知道么,比较急