首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >摩纳哥deltaDecorations消失在角7当模型改变

摩纳哥deltaDecorations消失在角7当模型改变
EN

Stack Overflow用户
提问于 2019-12-10 21:21:30
回答 2查看 585关注 0票数 1

有一个关于如何与伟大的例子 ( ngx摩纳哥编辑 )一起使用deltaDecorations (Ranges)的deltaDecorations

app.component.html

代码语言:javascript
复制
<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>

app.component.ts

代码语言:javascript
复制
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);
}

在ngOnInit上,一切都如预期的那样工作。但是,当我更改ngModel时,代码的突出显示就消失了:

代码语言:javascript
复制
onFileClicked() {
    this.code = "this is some other code'\n'
    that needs to be'\n'
    highlighted just like'\n'
    the first one."
}

当我现在再次更改ngModel时,旧模型的代码会被高亮显示很短的时间,但是一旦新模型启动,它就会与旧模型一起消失。新模型的代码也应该突出显示,但不是。

我在这里错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-21 23:57:11

找到了一个可行的解决方案,但我不知道为什么。

我有一个函数初始化一个新编辑器:

代码语言:javascript
复制
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;
}

在编辑器的init之后,似乎必须再次手动设置模型:

代码语言:javascript
复制
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;
}

只需在初始化在单击文件时触发的函数中的编辑器之前设置模型,就不起作用了,而不是

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2020-07-23 20:36:39

我遇到了一个类似的问题:当我使用像theme/language这样的Object.assign()动态地改变这个吉特布问题时,装饰就消失了。

我找到了一个解决方案:使用monaco.editor.setModelLanguage()monaco.editor.setTheme()

但是不确定是否要更改代码内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59275532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档