link.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. 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;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. import { $, append, EventHelper } from '../../../base/browser/dom.js';
  15. import { DomEmitter } from '../../../base/browser/event.js';
  16. import { StandardKeyboardEvent } from '../../../base/browser/keyboardEvent.js';
  17. import { EventType as TouchEventType, Gesture } from '../../../base/browser/touch.js';
  18. import { Event } from '../../../base/common/event.js';
  19. import { Disposable } from '../../../base/common/lifecycle.js';
  20. import { IOpenerService } from '../common/opener.js';
  21. import { textLinkActiveForeground, textLinkForeground } from '../../theme/common/colorRegistry.js';
  22. import { registerThemingParticipant } from '../../theme/common/themeService.js';
  23. let Link = class Link extends Disposable {
  24. constructor(container, _link, options = {}, openerService) {
  25. var _a;
  26. super();
  27. this._link = _link;
  28. this._enabled = true;
  29. this.el = append(container, $('a.monaco-link', {
  30. tabIndex: (_a = _link.tabIndex) !== null && _a !== void 0 ? _a : 0,
  31. href: _link.href,
  32. title: _link.title
  33. }, _link.label));
  34. this.el.setAttribute('role', 'button');
  35. const onClickEmitter = this._register(new DomEmitter(this.el, 'click'));
  36. const onKeyPress = this._register(new DomEmitter(this.el, 'keypress'));
  37. const onEnterPress = Event.chain(onKeyPress.event)
  38. .map(e => new StandardKeyboardEvent(e))
  39. .filter(e => e.keyCode === 3 /* Enter */)
  40. .event;
  41. const onTap = this._register(new DomEmitter(this.el, TouchEventType.Tap)).event;
  42. this._register(Gesture.addTarget(this.el));
  43. const onOpen = Event.any(onClickEmitter.event, onEnterPress, onTap);
  44. this._register(onOpen(e => {
  45. if (!this.enabled) {
  46. return;
  47. }
  48. EventHelper.stop(e, true);
  49. if (options === null || options === void 0 ? void 0 : options.opener) {
  50. options.opener(this._link.href);
  51. }
  52. else {
  53. openerService.open(this._link.href, { allowCommands: true });
  54. }
  55. }));
  56. this.enabled = true;
  57. }
  58. get enabled() {
  59. return this._enabled;
  60. }
  61. set enabled(enabled) {
  62. if (enabled) {
  63. this.el.setAttribute('aria-disabled', 'false');
  64. this.el.tabIndex = 0;
  65. this.el.style.pointerEvents = 'auto';
  66. this.el.style.opacity = '1';
  67. this.el.style.cursor = 'pointer';
  68. this._enabled = false;
  69. }
  70. else {
  71. this.el.setAttribute('aria-disabled', 'true');
  72. this.el.tabIndex = -1;
  73. this.el.style.pointerEvents = 'none';
  74. this.el.style.opacity = '0.4';
  75. this.el.style.cursor = 'default';
  76. this._enabled = true;
  77. }
  78. this._enabled = enabled;
  79. }
  80. };
  81. Link = __decorate([
  82. __param(3, IOpenerService)
  83. ], Link);
  84. export { Link };
  85. registerThemingParticipant((theme, collector) => {
  86. const textLinkForegroundColor = theme.getColor(textLinkForeground);
  87. if (textLinkForegroundColor) {
  88. collector.addRule(`.monaco-link { color: ${textLinkForegroundColor}; }`);
  89. }
  90. const textLinkActiveForegroundColor = theme.getColor(textLinkActiveForeground);
  91. if (textLinkActiveForegroundColor) {
  92. collector.addRule(`.monaco-link:hover { color: ${textLinkActiveForegroundColor}; }`);
  93. }
  94. });