|
| 1 | +import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; |
| 2 | +import { registerSingleton, InstantiationType } from 'vs/platform/instantiation/common/extensions'; |
| 3 | +import { Disposable, IDisposable } from 'vs/base/common/lifecycle'; |
| 4 | +import { CommandsRegistry } from 'vs/platform/commands/common/commands'; |
| 5 | + |
| 6 | +export const IShadowOverlayService = createDecorator<IShadowOverlayService>('shadowOverlayService'); |
| 7 | + |
| 8 | +export interface IShadowOverlayService extends IDisposable { |
| 9 | + readonly _serviceBrand: undefined; |
| 10 | + |
| 11 | + highlight(elements: string[]): void; |
| 12 | + restoreStyles(elements: string[]): void; |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +export class ShadowOverlayService extends Disposable implements IShadowOverlayService { |
| 17 | + declare readonly _serviceBrand: undefined; |
| 18 | + private highlightedElements: Map<string, { |
| 19 | + position: string; |
| 20 | + zIndex: string; |
| 21 | + isolation: string; |
| 22 | + pointerEvents: string; |
| 23 | + backgroundColor: string; |
| 24 | + boxShadow: string; |
| 25 | + }> = new Map(); |
| 26 | + |
| 27 | + constructor( |
| 28 | + ) { |
| 29 | + super(); |
| 30 | + this.registerCommands(); |
| 31 | + } |
| 32 | + |
| 33 | + private registerCommands(): void { |
| 34 | + CommandsRegistry.registerCommand('pearai.highlightElements', (accessor, ...args) => { |
| 35 | + |
| 36 | + const selectors = args[0] as string[]; // array of CSS selectors |
| 37 | + this.highlight(selectors); |
| 38 | + }); |
| 39 | + |
| 40 | + CommandsRegistry.registerCommand('pearai.removeHighlight', (accessor, ...args) => { |
| 41 | + const selectors = args[0] as string[]; // array of CSS selectors |
| 42 | + // Convert selectors to elements |
| 43 | + this.restoreStyles(selectors); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + restoreStyles(selectors: string[]): void { |
| 48 | + selectors.forEach(selector => { |
| 49 | + const originalStyles = this.highlightedElements.get(selector); |
| 50 | + |
| 51 | + const element = document.querySelector(selector) as HTMLElement |
| 52 | + |
| 53 | + if (originalStyles) { |
| 54 | + element.style.position = originalStyles.position; |
| 55 | + element.style.zIndex = originalStyles.zIndex; |
| 56 | + element.style.isolation = originalStyles.isolation; |
| 57 | + element.style.pointerEvents = originalStyles.pointerEvents; |
| 58 | + element.style.backgroundColor = originalStyles.backgroundColor; |
| 59 | + element.style.boxShadow = originalStyles.boxShadow; |
| 60 | + |
| 61 | + // Remove this element from the tracked elements |
| 62 | + this.highlightedElements.delete(selector); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + highlight(selectors: string[]): void { |
| 68 | + selectors.forEach(selector => { |
| 69 | + if (selector) { |
| 70 | + const element = document.querySelector(selector) as HTMLElement |
| 71 | + |
| 72 | + // save original styles |
| 73 | + if (!this.highlightedElements.has(selector)) { |
| 74 | + this.highlightedElements.set(selector, { |
| 75 | + position: element.style.position, |
| 76 | + zIndex: element.style.zIndex, |
| 77 | + isolation: element.style.isolation, |
| 78 | + pointerEvents: element.style.pointerEvents, |
| 79 | + backgroundColor: element.style.backgroundColor, |
| 80 | + boxShadow: element.style.boxShadow, |
| 81 | + }); |
| 82 | + } |
| 83 | + element.style.position = 'absolute'; |
| 84 | + element.style.zIndex = '3000'; |
| 85 | + element.style.isolation = 'isolate'; |
| 86 | + element.style.pointerEvents = 'auto'; |
| 87 | + element.style.backgroundColor = 'transparent'; |
| 88 | + element.style.boxShadow = '0 0 0 9999px rgba(0, 0, 0, 0.6)'; |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +registerSingleton(IShadowOverlayService, ShadowOverlayService, InstantiationType.Eager,); |
0 commit comments