bannerController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __param = (this && this.__param) || function (paramIndex, decorator) {
  8. return function (target, key) { decorator(target, key, paramIndex); }
  9. };
  10. /*---------------------------------------------------------------------------------------------
  11. * Copyright (c) Microsoft Corporation. All rights reserved.
  12. * Licensed under the MIT License. See License.txt in the project root for license information.
  13. *--------------------------------------------------------------------------------------------*/
  14. import './bannerController.css';
  15. import { $, append, clearNode } from '../../../base/browser/dom.js';
  16. import { ActionBar } from '../../../base/browser/ui/actionbar/actionbar.js';
  17. import { Action } from '../../../base/common/actions.js';
  18. import { Disposable } from '../../../base/common/lifecycle.js';
  19. import { MarkdownRenderer } from '../../browser/core/markdownRenderer.js';
  20. import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
  21. import { Link } from '../../../platform/opener/browser/link.js';
  22. import { widgetClose } from '../../../platform/theme/common/iconRegistry.js';
  23. import { ThemeIcon } from '../../../platform/theme/common/themeService.js';
  24. const BANNER_ELEMENT_HEIGHT = 26;
  25. let BannerController = class BannerController extends Disposable {
  26. constructor(_editor, instantiationService) {
  27. super();
  28. this._editor = _editor;
  29. this.instantiationService = instantiationService;
  30. this.banner = this._register(this.instantiationService.createInstance(Banner));
  31. }
  32. hide() {
  33. this._editor.setBanner(null, 0);
  34. this.banner.clear();
  35. }
  36. show(item) {
  37. this.banner.show(Object.assign(Object.assign({}, item), { onClose: () => {
  38. this.hide();
  39. if (item.onClose) {
  40. item.onClose();
  41. }
  42. } }));
  43. this._editor.setBanner(this.banner.element, BANNER_ELEMENT_HEIGHT);
  44. }
  45. };
  46. BannerController = __decorate([
  47. __param(1, IInstantiationService)
  48. ], BannerController);
  49. export { BannerController };
  50. // TODO@hediet: Investigate if this can be reused by the workspace banner (bannerPart.ts).
  51. let Banner = class Banner extends Disposable {
  52. constructor(instantiationService) {
  53. super();
  54. this.instantiationService = instantiationService;
  55. this.markdownRenderer = this.instantiationService.createInstance(MarkdownRenderer, {});
  56. this.element = $('div.editor-banner');
  57. this.element.tabIndex = 0;
  58. }
  59. getAriaLabel(item) {
  60. if (item.ariaLabel) {
  61. return item.ariaLabel;
  62. }
  63. if (typeof item.message === 'string') {
  64. return item.message;
  65. }
  66. return undefined;
  67. }
  68. getBannerMessage(message) {
  69. if (typeof message === 'string') {
  70. const element = $('span');
  71. element.innerText = message;
  72. return element;
  73. }
  74. return this.markdownRenderer.render(message).element;
  75. }
  76. clear() {
  77. clearNode(this.element);
  78. }
  79. show(item) {
  80. // Clear previous item
  81. clearNode(this.element);
  82. // Banner aria label
  83. const ariaLabel = this.getAriaLabel(item);
  84. if (ariaLabel) {
  85. this.element.setAttribute('aria-label', ariaLabel);
  86. }
  87. // Icon
  88. const iconContainer = append(this.element, $('div.icon-container'));
  89. iconContainer.setAttribute('aria-hidden', 'true');
  90. if (item.icon) {
  91. iconContainer.appendChild($(`div${ThemeIcon.asCSSSelector(item.icon)}`));
  92. }
  93. // Message
  94. const messageContainer = append(this.element, $('div.message-container'));
  95. messageContainer.setAttribute('aria-hidden', 'true');
  96. messageContainer.appendChild(this.getBannerMessage(item.message));
  97. // Message Actions
  98. this.messageActionsContainer = append(this.element, $('div.message-actions-container'));
  99. if (item.actions) {
  100. for (const action of item.actions) {
  101. this._register(this.instantiationService.createInstance(Link, this.messageActionsContainer, Object.assign(Object.assign({}, action), { tabIndex: -1 }), {}));
  102. }
  103. }
  104. // Action
  105. const actionBarContainer = append(this.element, $('div.action-container'));
  106. this.actionBar = this._register(new ActionBar(actionBarContainer));
  107. this.actionBar.push(this._register(new Action('banner.close', 'Close Banner', ThemeIcon.asClassName(widgetClose), true, () => {
  108. if (typeof item.onClose === 'function') {
  109. item.onClose();
  110. }
  111. })), { icon: true, label: false });
  112. this.actionBar.setFocusable(false);
  113. }
  114. };
  115. Banner = __decorate([
  116. __param(0, IInstantiationService)
  117. ], Banner);