BigW Consortium Gitlab

controller.js 1.1 KB
Newer Older
Phil Hughes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
export default class DecorationsController {
  constructor(editor) {
    this.editor = editor;
    this.decorations = new Map();
    this.editorDecorations = new Map();
  }

  getAllDecorationsForModel(model) {
    if (!this.decorations.has(model.url)) return [];

    const modelDecorations = this.decorations.get(model.url);
    const decorations = [];

    modelDecorations.forEach(val => decorations.push(...val));

    return decorations;
  }

  addDecorations(model, decorationsKey, decorations) {
    const decorationMap = this.decorations.get(model.url) || new Map();

    decorationMap.set(decorationsKey, decorations);

    this.decorations.set(model.url, decorationMap);

    this.decorate(model);
  }

  decorate(model) {
    if (!this.editor.instance) return;

    const decorations = this.getAllDecorationsForModel(model);
    const oldDecorations = this.editorDecorations.get(model.url) || [];

    this.editorDecorations.set(
      model.url,
      this.editor.instance.deltaDecorations(oldDecorations, decorations),
    );
  }

  dispose() {
    this.decorations.clear();
    this.editorDecorations.clear();
  }
}