text-view.ts 8.04 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import { PhaseView } from "../src/view";
6 7
import { anyToString, ViewElements, isIterable } from "../src/util";
import { MySelection } from "../src/selection";
8
import { SourceResolver } from "./source-resolver";
9
import { SelectionBroker } from "./selection-broker";
10
import { NodeSelectionHandler, BlockSelectionHandler } from "./selection-handler";
11

12
export abstract class TextView extends PhaseView {
13 14 15 16 17 18 19 20 21 22
  selectionHandler: NodeSelectionHandler;
  blockSelectionHandler: BlockSelectionHandler;
  selection: MySelection;
  blockSelection: MySelection;
  textListNode: HTMLUListElement;
  nodeIdToHtmlElementsMap: Map<string, Array<HTMLElement>>;
  blockIdToHtmlElementsMap: Map<string, Array<HTMLElement>>;
  blockIdtoNodeIds: Map<string, Array<string>>;
  nodeIdToBlockId: Array<string>;
  patterns: any;
23
  sourceResolver: SourceResolver;
24
  broker: SelectionBroker;
25

26
  constructor(id, broker) {
27
    super(id);
28
    const view = this;
29
    view.textListNode = view.divNode.getElementsByTagName('ul')[0];
30
    view.patterns = null;
31 32 33 34
    view.nodeIdToHtmlElementsMap = new Map();
    view.blockIdToHtmlElementsMap = new Map();
    view.blockIdtoNodeIds = new Map();
    view.nodeIdToBlockId = [];
35 36
    view.selection = new MySelection(anyToString);
    view.blockSelection = new MySelection(anyToString);
37
    view.broker = broker;
38
    view.sourceResolver = broker.sourceResolver;
39 40 41 42 43
    const selectionHandler = {
      clear: function () {
        view.selection.clear();
        view.updateSelection();
        broker.broadcastClear(selectionHandler);
44
      },
45 46 47 48
      select: function (nodeIds, selected) {
        view.selection.select(nodeIds, selected);
        view.updateSelection();
        broker.broadcastNodeSelect(selectionHandler, view.selection.selectedKeys(), selected);
49
      },
50 51 52 53
      brokeredNodeSelect: function (nodeIds, selected) {
        const firstSelect = view.blockSelection.isEmpty();
        view.selection.select(nodeIds, selected);
        view.updateSelection(firstSelect);
54
      },
55
      brokeredClear: function () {
56
        view.selection.clear();
57
        view.updateSelection();
58 59
      }
    };
60 61
    this.selectionHandler = selectionHandler;
    broker.addNodeHandler(selectionHandler);
62
    view.divNode.addEventListener('click', e => {
63 64 65
      if (!e.shiftKey) {
        view.selectionHandler.clear();
      }
66 67
      e.stopPropagation();
    });
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    const blockSelectionHandler = {
      clear: function () {
        view.blockSelection.clear();
        view.updateSelection();
        broker.broadcastClear(blockSelectionHandler);
      },
      select: function (blockIds, selected) {
        view.blockSelection.select(blockIds, selected);
        view.updateSelection();
        broker.broadcastBlockSelect(blockSelectionHandler, blockIds, selected);
      },
      brokeredBlockSelect: function (blockIds, selected) {
        const firstSelect = view.blockSelection.isEmpty();
        view.blockSelection.select(blockIds, selected);
        view.updateSelection(firstSelect);
      },
      brokeredClear: function () {
        view.blockSelection.clear();
        view.updateSelection();
      }
    };
    this.blockSelectionHandler = blockSelectionHandler;
    broker.addBlockHandler(blockSelectionHandler);
  }

93
  addHtmlElementForNodeId(anyNodeId: any, htmlElement: HTMLElement) {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    const nodeId = anyToString(anyNodeId);
    if (!this.nodeIdToHtmlElementsMap.has(nodeId)) {
      this.nodeIdToHtmlElementsMap.set(nodeId, []);
    }
    this.nodeIdToHtmlElementsMap.get(nodeId).push(htmlElement);
  }

  addHtmlElementForBlockId(anyBlockId, htmlElement) {
    const blockId = anyToString(anyBlockId);
    if (!this.blockIdToHtmlElementsMap.has(blockId)) {
      this.blockIdToHtmlElementsMap.set(blockId, []);
    }
    this.blockIdToHtmlElementsMap.get(blockId).push(htmlElement);
  }

  addNodeIdToBlockId(anyNodeId, anyBlockId) {
    const blockId = anyToString(anyBlockId);
    if (!this.blockIdtoNodeIds.has(blockId)) {
      this.blockIdtoNodeIds.set(blockId, []);
    }
    this.blockIdtoNodeIds.get(blockId).push(anyToString(anyNodeId));
    this.nodeIdToBlockId[anyNodeId] = blockId;
  }

  blockIdsForNodeIds(nodeIds) {
    const blockIds = [];
    for (const nodeId of nodeIds) {
      const blockId = this.nodeIdToBlockId[nodeId];
      if (blockId == undefined) continue;
      blockIds.push(blockId);
    }
    return blockIds;
  }

128
  updateSelection(scrollIntoView: boolean = false) {
129
    if (this.divNode.parentNode == null) return;
130
    const mkVisible = new ViewElements(this.divNode.parentNode as HTMLElement);
131 132 133 134 135 136 137 138
    const view = this;
    for (const [blockId, elements] of this.blockIdToHtmlElementsMap.entries()) {
      const isSelected = view.blockSelection.isSelected(blockId);
      for (const element of elements) {
        mkVisible.consider(element, isSelected);
        element.classList.toggle("selected", isSelected);
      }
    }
139
    const elementsToSelect = view.divNode.querySelectorAll(`[data-pc-offset]`);
140 141 142
    for (const el of elementsToSelect) {
      el.classList.toggle("selected", false);
    }
143 144 145 146 147 148 149 150
    for (const key of this.nodeIdToHtmlElementsMap.keys()) {
      for (const element of this.nodeIdToHtmlElementsMap.get(key)) {
        element.classList.toggle("selected", false);
      }
    }
    for (const nodeId of view.selection.selectedKeys()) {
      const elements = this.nodeIdToHtmlElementsMap.get(nodeId);
      if (!elements) continue;
151
      for (const element of elements) {
152 153
        mkVisible.consider(element, true);
        element.classList.toggle("selected", true);
154 155 156
      }
    }
    mkVisible.apply(scrollIntoView);
157 158 159
  }

  setPatterns(patterns) {
160
    this.patterns = patterns;
161 162 163
  }

  clearText() {
164 165
    while (this.textListNode.firstChild) {
      this.textListNode.removeChild(this.textListNode.firstChild);
166 167 168
    }
  }

169
  createFragment(text, style) {
170
    const fragment = document.createElement("SPAN");
171

172 173
    if (typeof style.associateData == 'function') {
      style.associateData(text, fragment);
174 175 176 177 178 179
    } else {
      if (style.css != undefined) {
        const css = isIterable(style.css) ? style.css : [style.css];
        for (const cls of css) {
          fragment.classList.add(cls);
        }
180
      }
181
      fragment.innerText = text;
182
    }
183

184
    return fragment;
185 186 187
  }

  processLine(line) {
188 189
    const view = this;
    const result = [];
190 191
    let patternSet = 0;
    while (true) {
192 193 194
      const beforeLine = line;
      for (const pattern of view.patterns[patternSet]) {
        const matches = line.match(pattern[0]);
195 196
        if (matches != null) {
          if (matches[0] != '') {
197 198
            const style = pattern[1] != null ? pattern[1] : {};
            const text = matches[0];
199
            if (text != '') {
200
              const fragment = view.createFragment(matches[0], style);
201 202 203 204 205 206 207 208 209 210
              result.push(fragment);
            }
            line = line.substr(matches[0].length);
          }
          let nextPatternSet = patternSet;
          if (pattern.length > 2) {
            nextPatternSet = pattern[2];
          }
          if (line == "") {
            if (nextPatternSet != -1) {
211
              throw ("illegal parsing state in text-view in patternSet" + patternSet);
212 213 214 215 216 217 218 219
            }
            return result;
          }
          patternSet = nextPatternSet;
          break;
        }
      }
      if (beforeLine == line) {
220
        throw ("input not consumed in text-view in patternSet" + patternSet);
221 222 223 224 225
      }
    }
  }

  processText(text) {
226 227
    const view = this;
    const textLines = text.split(/[\n]/);
228
    let lineNo = 0;
229 230
    for (const line of textLines) {
      const li = document.createElement("LI");
231
      li.className = "nolinenums";
232
      li.dataset.lineNo = "" + lineNo++;
233 234
      const fragments = view.processLine(line);
      for (const fragment of fragments) {
235
        li.appendChild(fragment);
236 237 238 239 240 241
      }
      view.textListNode.appendChild(li);
    }
  }

  initializeContent(data, rememberedSelection) {
242 243 244
    this.clearText();
    this.processText(data);
    this.show();
245 246
  }

247
  public onresize(): void {}
248 249 250 251 252

  isScrollable() {
    return true;
  }
}