简体   繁体   English

当模型更改时,摩纳哥 deltaDecorations 以 7 角消失

[英]Monaco deltaDecorations disappear in angular 7 when model changes

There is a great example of how to use the ngx monaco editor with deltaDecorations (ranges):有一个很好的例子说明如何使用带有deltaDecorations (ranges) 的ngx monaco 编辑器

app.component.html应用程序组件.html

<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>

app.component.ts app.component.ts

editorOptions = {
    theme: 'vs',
    language: 'javascript',
    glyphMargin: true
  };
code = [
    '"use strict";',
    'function Person(age) {',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '}',
    'Person.prototype.getAge = function () {',
    '   return this.age;',
    '};'
  ].join('\n');

onInit(editor: any) {
  const t = editor.deltaDecorations([], [
    {
      range: new monaco.Range(3, 1, 3, 1),
      options: {
        isWholeLine: true,
        className: 'myContentClass',
        glyphMarginClassName: 'myGlyphMarginClass'
      }
    }
  ]);
  console.log(t);
}

On ngOnInit everything works as expected.在 ngOnInit 上,一切都按预期工作。 But when I change the ngModel, the highlighting of the code disappears:但是当我更改 ngModel 时,代码的突出显示消失了:

onFileClicked() {
    this.code = "this is some other code'\n'
    that needs to be'\n'
    highlighted just like'\n'
    the first one."
}

When I change the ngModel once again now, the code of the old model gets highlighted for a very brief time, but disappears together with the old model as soon as the new one is initiated.当我现在再次更改 ngModel 时,旧模型的代码会在很短的时间内突出显示,但一旦启动新模型,它就会与旧模型一起消失。 The code of the new model should be highlighted as well, but is not.新模型的代码也应该突出显示,但不是。

What am I missing here?我在这里缺少什么?

Found a solution that works, but I don't really know why.找到了一个有效的解决方案,但我真的不知道为什么。

I have a function that initialises a new editor:我有一个初始化新编辑器的函数:

onFileClicked(file) {

    // Angular @Input setter detects only object updates,
    // so property updates are not handled =
    // we need to update whole object when language changes
    this.editorOptions = {
        theme: "vs",
        language: this.langDetect.getLanguage(file),
        readOnly: true,
        automaticLayout: true
    };

    this.code = file.data;
    this.ranges = file.ranges;
}

It appears as if one has to set the model manually again after init of the editor:似乎必须在编辑器初始化后再次手动设置模型:

onEditorInit(editor: any) {
    const model = monaco.editor.getModels()[0];
    monaco.editor.setModelLanguage(model, this.editorOptions.language);
    model.setValue(this.code);

    if (this.coverage) {
        let ranges = this.buildEditorRanges(this.coverage);
        editor.deltaDecorations([], ranges);
    }
}

buildEditorRanges(ranges) {
    let editorRanges = [];
    let options = {
        isWholeLine: true,
        className: "code-highlight",
        glyphMarginClassName: "code-highlight"
    };
    ranges.forEach(function(range) {
        editorRanges.push({
            range: new monaco.Range(
                range.startLine,
                range.startColumn,
                range.endLine,
                range.endColumn
            ),
            options: options
        });
    });
    return editorRanges;
}

Simply setting the model before initialising the editor in the function that triggers when a file has been clicked does NOT work:一个文件已被点击时的功能触发初始化编辑器前简单地设置模式工作:

onFileClicked(file) {

    this.code = file.data;
    // Angular @Input setter detects only object updates,
    // so property updates are not handled =
    // we need to update whole object when language changes
    this.editorOptions = {
        theme: "vs",
        language: this.langDetect.getLanguage(file),
        readOnly: true,
        automaticLayout: true
    };


    this.ranges = file.ranges;
}

I encountered a similar problem: When I change theme/language dynamically using Object.assign() like this Github issue , decorations disappear.我遇到了类似的问题:当我使用Object.assign()动态更改theme/language这个 Github issue ,装饰消失了。

I found a solution: Use monaco.editor.setModelLanguage() and monaco.editor.setTheme()我找到了一个解决方案:使用monaco.editor.setModelLanguage()monaco.editor.setTheme()

Not sure about changing the code content though虽然不确定更改代码内容

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

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