zoneWidget.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import * as dom from '../../../base/browser/dom.js';
  6. import { Sash } from '../../../base/browser/ui/sash/sash.js';
  7. import { Color, RGBA } from '../../../base/common/color.js';
  8. import { IdGenerator } from '../../../base/common/idGenerator.js';
  9. import { DisposableStore } from '../../../base/common/lifecycle.js';
  10. import * as objects from '../../../base/common/objects.js';
  11. import './zoneWidget.css';
  12. import { Range } from '../../common/core/range.js';
  13. import { ModelDecorationOptions } from '../../common/model/textModel.js';
  14. const defaultColor = new Color(new RGBA(0, 122, 204));
  15. const defaultOptions = {
  16. showArrow: true,
  17. showFrame: true,
  18. className: '',
  19. frameColor: defaultColor,
  20. arrowColor: defaultColor,
  21. keepEditorSelection: false
  22. };
  23. const WIDGET_ID = 'vs.editor.contrib.zoneWidget';
  24. export class ViewZoneDelegate {
  25. constructor(domNode, afterLineNumber, afterColumn, heightInLines, onDomNodeTop, onComputedHeight) {
  26. this.id = ''; // A valid zone id should be greater than 0
  27. this.domNode = domNode;
  28. this.afterLineNumber = afterLineNumber;
  29. this.afterColumn = afterColumn;
  30. this.heightInLines = heightInLines;
  31. this._onDomNodeTop = onDomNodeTop;
  32. this._onComputedHeight = onComputedHeight;
  33. }
  34. onDomNodeTop(top) {
  35. this._onDomNodeTop(top);
  36. }
  37. onComputedHeight(height) {
  38. this._onComputedHeight(height);
  39. }
  40. }
  41. export class OverlayWidgetDelegate {
  42. constructor(id, domNode) {
  43. this._id = id;
  44. this._domNode = domNode;
  45. }
  46. getId() {
  47. return this._id;
  48. }
  49. getDomNode() {
  50. return this._domNode;
  51. }
  52. getPosition() {
  53. return null;
  54. }
  55. }
  56. class Arrow {
  57. constructor(_editor) {
  58. this._editor = _editor;
  59. this._ruleName = Arrow._IdGenerator.nextId();
  60. this._decorations = [];
  61. this._color = null;
  62. this._height = -1;
  63. //
  64. }
  65. dispose() {
  66. this.hide();
  67. dom.removeCSSRulesContainingSelector(this._ruleName);
  68. }
  69. set color(value) {
  70. if (this._color !== value) {
  71. this._color = value;
  72. this._updateStyle();
  73. }
  74. }
  75. set height(value) {
  76. if (this._height !== value) {
  77. this._height = value;
  78. this._updateStyle();
  79. }
  80. }
  81. _updateStyle() {
  82. dom.removeCSSRulesContainingSelector(this._ruleName);
  83. dom.createCSSRule(`.monaco-editor ${this._ruleName}`, `border-style: solid; border-color: transparent; border-bottom-color: ${this._color}; border-width: ${this._height}px; bottom: -${this._height}px; margin-left: -${this._height}px; `);
  84. }
  85. show(where) {
  86. if (where.column === 1) {
  87. // the arrow isn't pretty at column 1 and we need to push it out a little
  88. where = { lineNumber: where.lineNumber, column: 2 };
  89. }
  90. this._decorations = this._editor.deltaDecorations(this._decorations, [{ range: Range.fromPositions(where), options: { description: 'zone-widget-arrow', className: this._ruleName, stickiness: 1 /* NeverGrowsWhenTypingAtEdges */ } }]);
  91. }
  92. hide() {
  93. this._editor.deltaDecorations(this._decorations, []);
  94. }
  95. }
  96. Arrow._IdGenerator = new IdGenerator('.arrow-decoration-');
  97. export class ZoneWidget {
  98. constructor(editor, options = {}) {
  99. this._arrow = null;
  100. this._overlayWidget = null;
  101. this._resizeSash = null;
  102. this._positionMarkerId = [];
  103. this._viewZone = null;
  104. this._disposables = new DisposableStore();
  105. this.container = null;
  106. this._isShowing = false;
  107. this.editor = editor;
  108. this.options = objects.deepClone(options);
  109. objects.mixin(this.options, defaultOptions, false);
  110. this.domNode = document.createElement('div');
  111. if (!this.options.isAccessible) {
  112. this.domNode.setAttribute('aria-hidden', 'true');
  113. this.domNode.setAttribute('role', 'presentation');
  114. }
  115. this._disposables.add(this.editor.onDidLayoutChange((info) => {
  116. const width = this._getWidth(info);
  117. this.domNode.style.width = width + 'px';
  118. this.domNode.style.left = this._getLeft(info) + 'px';
  119. this._onWidth(width);
  120. }));
  121. }
  122. dispose() {
  123. if (this._overlayWidget) {
  124. this.editor.removeOverlayWidget(this._overlayWidget);
  125. this._overlayWidget = null;
  126. }
  127. if (this._viewZone) {
  128. this.editor.changeViewZones(accessor => {
  129. if (this._viewZone) {
  130. accessor.removeZone(this._viewZone.id);
  131. }
  132. this._viewZone = null;
  133. });
  134. }
  135. this.editor.deltaDecorations(this._positionMarkerId, []);
  136. this._positionMarkerId = [];
  137. this._disposables.dispose();
  138. }
  139. create() {
  140. this.domNode.classList.add('zone-widget');
  141. if (this.options.className) {
  142. this.domNode.classList.add(this.options.className);
  143. }
  144. this.container = document.createElement('div');
  145. this.container.classList.add('zone-widget-container');
  146. this.domNode.appendChild(this.container);
  147. if (this.options.showArrow) {
  148. this._arrow = new Arrow(this.editor);
  149. this._disposables.add(this._arrow);
  150. }
  151. this._fillContainer(this.container);
  152. this._initSash();
  153. this._applyStyles();
  154. }
  155. style(styles) {
  156. if (styles.frameColor) {
  157. this.options.frameColor = styles.frameColor;
  158. }
  159. if (styles.arrowColor) {
  160. this.options.arrowColor = styles.arrowColor;
  161. }
  162. this._applyStyles();
  163. }
  164. _applyStyles() {
  165. if (this.container && this.options.frameColor) {
  166. let frameColor = this.options.frameColor.toString();
  167. this.container.style.borderTopColor = frameColor;
  168. this.container.style.borderBottomColor = frameColor;
  169. }
  170. if (this._arrow && this.options.arrowColor) {
  171. let arrowColor = this.options.arrowColor.toString();
  172. this._arrow.color = arrowColor;
  173. }
  174. }
  175. _getWidth(info) {
  176. return info.width - info.minimap.minimapWidth - info.verticalScrollbarWidth;
  177. }
  178. _getLeft(info) {
  179. // If minimap is to the left, we move beyond it
  180. if (info.minimap.minimapWidth > 0 && info.minimap.minimapLeft === 0) {
  181. return info.minimap.minimapWidth;
  182. }
  183. return 0;
  184. }
  185. _onViewZoneTop(top) {
  186. this.domNode.style.top = top + 'px';
  187. }
  188. _onViewZoneHeight(height) {
  189. this.domNode.style.height = `${height}px`;
  190. if (this.container) {
  191. let containerHeight = height - this._decoratingElementsHeight();
  192. this.container.style.height = `${containerHeight}px`;
  193. const layoutInfo = this.editor.getLayoutInfo();
  194. this._doLayout(containerHeight, this._getWidth(layoutInfo));
  195. }
  196. if (this._resizeSash) {
  197. this._resizeSash.layout();
  198. }
  199. }
  200. get position() {
  201. const [id] = this._positionMarkerId;
  202. if (!id) {
  203. return undefined;
  204. }
  205. const model = this.editor.getModel();
  206. if (!model) {
  207. return undefined;
  208. }
  209. const range = model.getDecorationRange(id);
  210. if (!range) {
  211. return undefined;
  212. }
  213. return range.getStartPosition();
  214. }
  215. show(rangeOrPos, heightInLines) {
  216. const range = Range.isIRange(rangeOrPos) ? Range.lift(rangeOrPos) : Range.fromPositions(rangeOrPos);
  217. this._isShowing = true;
  218. this._showImpl(range, heightInLines);
  219. this._isShowing = false;
  220. this._positionMarkerId = this.editor.deltaDecorations(this._positionMarkerId, [{ range, options: ModelDecorationOptions.EMPTY }]);
  221. }
  222. hide() {
  223. if (this._viewZone) {
  224. this.editor.changeViewZones(accessor => {
  225. if (this._viewZone) {
  226. accessor.removeZone(this._viewZone.id);
  227. }
  228. });
  229. this._viewZone = null;
  230. }
  231. if (this._overlayWidget) {
  232. this.editor.removeOverlayWidget(this._overlayWidget);
  233. this._overlayWidget = null;
  234. }
  235. if (this._arrow) {
  236. this._arrow.hide();
  237. }
  238. }
  239. _decoratingElementsHeight() {
  240. let lineHeight = this.editor.getOption(58 /* lineHeight */);
  241. let result = 0;
  242. if (this.options.showArrow) {
  243. let arrowHeight = Math.round(lineHeight / 3);
  244. result += 2 * arrowHeight;
  245. }
  246. if (this.options.showFrame) {
  247. let frameThickness = Math.round(lineHeight / 9);
  248. result += 2 * frameThickness;
  249. }
  250. return result;
  251. }
  252. _showImpl(where, heightInLines) {
  253. const position = where.getStartPosition();
  254. const layoutInfo = this.editor.getLayoutInfo();
  255. const width = this._getWidth(layoutInfo);
  256. this.domNode.style.width = `${width}px`;
  257. this.domNode.style.left = this._getLeft(layoutInfo) + 'px';
  258. // Render the widget as zone (rendering) and widget (lifecycle)
  259. const viewZoneDomNode = document.createElement('div');
  260. viewZoneDomNode.style.overflow = 'hidden';
  261. const lineHeight = this.editor.getOption(58 /* lineHeight */);
  262. // adjust heightInLines to viewport
  263. const maxHeightInLines = Math.max(12, (this.editor.getLayoutInfo().height / lineHeight) * 0.8);
  264. heightInLines = Math.min(heightInLines, maxHeightInLines);
  265. let arrowHeight = 0;
  266. let frameThickness = 0;
  267. // Render the arrow one 1/3 of an editor line height
  268. if (this._arrow && this.options.showArrow) {
  269. arrowHeight = Math.round(lineHeight / 3);
  270. this._arrow.height = arrowHeight;
  271. this._arrow.show(position);
  272. }
  273. // Render the frame as 1/9 of an editor line height
  274. if (this.options.showFrame) {
  275. frameThickness = Math.round(lineHeight / 9);
  276. }
  277. // insert zone widget
  278. this.editor.changeViewZones((accessor) => {
  279. if (this._viewZone) {
  280. accessor.removeZone(this._viewZone.id);
  281. }
  282. if (this._overlayWidget) {
  283. this.editor.removeOverlayWidget(this._overlayWidget);
  284. this._overlayWidget = null;
  285. }
  286. this.domNode.style.top = '-1000px';
  287. this._viewZone = new ViewZoneDelegate(viewZoneDomNode, position.lineNumber, position.column, heightInLines, (top) => this._onViewZoneTop(top), (height) => this._onViewZoneHeight(height));
  288. this._viewZone.id = accessor.addZone(this._viewZone);
  289. this._overlayWidget = new OverlayWidgetDelegate(WIDGET_ID + this._viewZone.id, this.domNode);
  290. this.editor.addOverlayWidget(this._overlayWidget);
  291. });
  292. if (this.container && this.options.showFrame) {
  293. const width = this.options.frameWidth ? this.options.frameWidth : frameThickness;
  294. this.container.style.borderTopWidth = width + 'px';
  295. this.container.style.borderBottomWidth = width + 'px';
  296. }
  297. let containerHeight = heightInLines * lineHeight - this._decoratingElementsHeight();
  298. if (this.container) {
  299. this.container.style.top = arrowHeight + 'px';
  300. this.container.style.height = containerHeight + 'px';
  301. this.container.style.overflow = 'hidden';
  302. }
  303. this._doLayout(containerHeight, width);
  304. if (!this.options.keepEditorSelection) {
  305. this.editor.setSelection(where);
  306. }
  307. const model = this.editor.getModel();
  308. if (model) {
  309. const revealLine = where.endLineNumber + 1;
  310. if (revealLine <= model.getLineCount()) {
  311. // reveal line below the zone widget
  312. this.revealLine(revealLine, false);
  313. }
  314. else {
  315. // reveal last line atop
  316. this.revealLine(model.getLineCount(), true);
  317. }
  318. }
  319. }
  320. revealLine(lineNumber, isLastLine) {
  321. if (isLastLine) {
  322. this.editor.revealLineInCenter(lineNumber, 0 /* Smooth */);
  323. }
  324. else {
  325. this.editor.revealLine(lineNumber, 0 /* Smooth */);
  326. }
  327. }
  328. setCssClass(className, classToReplace) {
  329. if (!this.container) {
  330. return;
  331. }
  332. if (classToReplace) {
  333. this.container.classList.remove(classToReplace);
  334. }
  335. this.container.classList.add(className);
  336. }
  337. _onWidth(widthInPixel) {
  338. // implement in subclass
  339. }
  340. _doLayout(heightInPixel, widthInPixel) {
  341. // implement in subclass
  342. }
  343. _relayout(newHeightInLines) {
  344. if (this._viewZone && this._viewZone.heightInLines !== newHeightInLines) {
  345. this.editor.changeViewZones(accessor => {
  346. if (this._viewZone) {
  347. this._viewZone.heightInLines = newHeightInLines;
  348. accessor.layoutZone(this._viewZone.id);
  349. }
  350. });
  351. }
  352. }
  353. // --- sash
  354. _initSash() {
  355. if (this._resizeSash) {
  356. return;
  357. }
  358. this._resizeSash = this._disposables.add(new Sash(this.domNode, this, { orientation: 1 /* HORIZONTAL */ }));
  359. if (!this.options.isResizeable) {
  360. this._resizeSash.state = 0 /* Disabled */;
  361. }
  362. let data;
  363. this._disposables.add(this._resizeSash.onDidStart((e) => {
  364. if (this._viewZone) {
  365. data = {
  366. startY: e.startY,
  367. heightInLines: this._viewZone.heightInLines,
  368. };
  369. }
  370. }));
  371. this._disposables.add(this._resizeSash.onDidEnd(() => {
  372. data = undefined;
  373. }));
  374. this._disposables.add(this._resizeSash.onDidChange((evt) => {
  375. if (data) {
  376. let lineDelta = (evt.currentY - data.startY) / this.editor.getOption(58 /* lineHeight */);
  377. let roundedLineDelta = lineDelta < 0 ? Math.ceil(lineDelta) : Math.floor(lineDelta);
  378. let newHeightInLines = data.heightInLines + roundedLineDelta;
  379. if (newHeightInLines > 5 && newHeightInLines < 35) {
  380. this._relayout(newHeightInLines);
  381. }
  382. }
  383. }));
  384. }
  385. getHorizontalSashLeft() {
  386. return 0;
  387. }
  388. getHorizontalSashTop() {
  389. return (this.domNode.style.height === null ? 0 : parseInt(this.domNode.style.height)) - (this._decoratingElementsHeight() / 2);
  390. }
  391. getHorizontalSashWidth() {
  392. const layoutInfo = this.editor.getLayoutInfo();
  393. return layoutInfo.width - layoutInfo.minimap.minimapWidth;
  394. }
  395. }