Monaco-editor: Add margin above first line?

Created on 28 Feb 2019  ยท  4Comments  ยท  Source: microsoft/monaco-editor


monaco-editor version: 0.15.6

Note: I realize this is a question that could have been asked at StackOverflow but I'm fairly certain this is just not possible presently.

I'd like the ability to specify the height of a top-margin that is only visible when scrolled to the top of the file. After scrolling down, the margin will disappear. Here's a visual demonstration of what I need to accomplish:

Starts with top-margin/gutter:
โ”โ”โ”โ”ณโ”โ”โ”โ”“
โ”ƒ  โ”ƒ   โ”ƒ
โ”ƒ 1โ”ƒ A โ”ƒ
โ”ƒ 2โ”ƒ B โ”ƒ
โ”—โ”โ”โ”ปโ”โ”โ”โ”›

After scrolling, top-margin/gutter is gone:
โ”โ”โ”โ”ณโ”โ”โ”โ”“
โ”ƒ 2โ”ƒ B โ”ƒ
โ”ƒ 3โ”ƒ C โ”ƒ
โ”ƒ 4โ”ƒ D โ”ƒ
โ”—โ”โ”โ”ปโ”โ”โ”โ”›

I suspect this sort of change will require code changes because, as far as I can tell, the line numbers and text lines are scrolled separately via code as opposed to CSS.

Most helpful comment

Took some time to find an example with view zones. Leaving this here for others:

const editor = monaco.editor.create(document.getElementById("container"))
editor.changeViewZones((changeAccessor) => {
  const domNode = document.createElement("div");
  const viewZoneId = changeAccessor.addZone({
    afterLineNumber: 0,
    heightInLines: 3,
    domNode: domNode
  });
});

Couldn't find a way to set the height of the view zone as a percentage of the editor height tho'.

All 4 comments

You can use view zones and add a view zone with afterLineNumber: 0.

Took some time to find an example with view zones. Leaving this here for others:

const editor = monaco.editor.create(document.getElementById("container"))
editor.changeViewZones((changeAccessor) => {
  const domNode = document.createElement("div");
  const viewZoneId = changeAccessor.addZone({
    afterLineNumber: 0,
    heightInLines: 3,
    domNode: domNode
  });
});

Couldn't find a way to set the height of the view zone as a percentage of the editor height tho'.

Thanks. Unfortunately, zones are destroyed when setting the model. I ended up adding the following code to the one location where I set the model:

    this.editor.setModel(model);
    if (this.marginTopPx > 0) {
      this.editor.changeViewZones(accessor => {
        accessor.addZone({
          afterLineNumber: 0,
          heightInPx: this.marginTopPx,
          domNode: document.createElement('SPAN'),
        });
      });
    }

Closing the issue as I don't think there is a real issue. It's unfortunate that the zone does not stick around but this can be worked around without too much trouble.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inf9144 picture inf9144  ยท  3Comments

poloten4uk picture poloten4uk  ยท  3Comments

SoftTimur picture SoftTimur  ยท  3Comments

galyech picture galyech  ยท  3Comments

andreymarchenko picture andreymarchenko  ยท  3Comments