简体   繁体   English

摩纳哥编辑器:只显示文件的一部分

[英]Monaco Editor: only show part of document

Is there a way to only show part of a document, or in monacos case of a model, while still getting intellisense for the whole document?有没有办法只显示文档的一部分,或者在 model 的摩纳哥案例中,同时仍然获得整个文档的智能感知?

I only want a user to edit a part of a document, but the user should be able to get the right contextual intellisense.我只希望用户编辑文档的一部分,但用户应该能够获得正确的上下文智能感知。

It would be best for my usecase to hide the uneditable sections, but deactivating them would also be ok.我的用例最好隐藏不可编辑的部分,但停用它们也可以。

In case this is not possible, is there any embedded editor that can do this, or can this be achived by modifying the language server?如果这是不可能的,是否有任何嵌入式编辑器可以做到这一点,或者这可以通过修改语言服务器来实现吗?

Monaco editor loads every line as a container under a section with the class name "view-lines". Monaco 编辑器将每一行作为容器加载到 class 名称为“view-lines”的部分下。 Once the editor content has loaded, set "display: none" to the corresponding container for each line that you want to hide.加载编辑器内容后,将“display: none”设置为要隐藏的每一行的相应容器。

Implementation: https://jsfiddle.net/renatodc/s6fxedo2/实施: https://jsfiddle.net/renatodc/s6fxedo2/

let value = `function capitalizeFirstLetter(string) {
\treturn string.charAt(0).toUpperCase() + string.slice(1);
}

$(function() {
\tlet word = "script";
\tlet result = capitalizeFirstLetter(word);
\tconsole.log(result);
});
`
let linesToDisable = [1,2,3];
let editor = monaco.editor.create(document.getElementById('container'), {
    value,
    language: 'javascript',
    theme: 'vs-dark',
    scrollbar: {
      vertical: "hidden",
      handleMouseWheel: false
    },
    scrollBeyondLastLine: false
});

// onLoad event for Monaco Editor: https://github.com/Microsoft/monaco-editor/issues/115
let didScrollChangeDisposable = editor.onDidScrollChange(function() {
    didScrollChangeDisposable.dispose();
    setTimeout(function() {
      $(".monaco-editor .view-lines > div").each(function(i) {
        if(linesToDisable.includes(i+1)) {
          $(this).css("display","none");
          $(this).css("pointer-events","none");
        }
      });
    },1000);
 }); 

Scrolling from Monaco will render the lines again and break the implementation.从 Monaco 滚动将再次呈现这些行并中断实现。 To prevent this, disable the scrolling feature in Monaco, set a fixed height for the editor container, and use the browser or a parent container to scroll instead.为防止这种情况,请在 Monaco 中禁用滚动功能,为编辑器容器设置固定高度,然后使用浏览器或父容器进行滚动。

If you use the arrow keys 'up' or 'down' to navigate to the hidden content, the cursor will still work, and typing will break the implementation.如果您使用箭头键“向上”或“向下”导航到隐藏内容,cursor 仍然可以工作,并且键入会破坏执行。 You might be able to use the editor's onKeyDown event to prevent this.您也许可以使用编辑器的 onKeyDown 事件来防止这种情况。

If you're looking for a break-proof implementation, I would suggest loading Monaco editor only with the portion of the document that you wish to edit.如果您正在寻找一个防破解的实现,我建议您只加载您希望编辑的文档部分的 Monaco 编辑器。 Then extend the completion provider (Intellisense) as shown in this example: https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example然后扩展完成提供程序(Intellisense),如下例所示: https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example

monaco.languages.registerCompletionItemProvider('javascript', {
    provideCompletionItems: function(model, position) {
        return {
            suggestions: [
                {
                    label: "capitalizeFirstLetter",
                    kind: monaco.languages.CompletionItemKind.Method,
                    documentation: "Capitalize the first letter of a word",
                    insertText: "capitalizeFirstLetter("
                }
            ]
        };
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: `$(function() {
\tlet word = "script";
\tlet result = capitalizeFirstLetter(word);
\tconsole.log(result);
});
  `,
    language: "javascript"
});

Use an AST parser like Esprima to get the identifiers from your source document, and plug these into the suggestions array.使用像 Esprima 这样的 AST 解析器从源文档中获取标识符,并将它们插入到建议数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM