|
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom'; |
| 3 | +import BaseManager, { Options, Props } from '../common/BaseManager'; |
| 4 | +import HighlightContainer from './HighlightContainer'; |
| 5 | + |
| 6 | +export default class HighlightManager implements BaseManager { |
| 7 | + location: number; |
| 8 | + |
| 9 | + reactEl: HTMLElement; |
| 10 | + |
| 11 | + constructor({ location = 1, referenceEl }: Options) { |
| 12 | + this.location = location; |
| 13 | + this.reactEl = this.insert(referenceEl); |
| 14 | + } |
| 15 | + |
| 16 | + destroy(): void { |
| 17 | + ReactDOM.unmountComponentAtNode(this.reactEl); |
| 18 | + |
| 19 | + this.reactEl.remove(); |
| 20 | + } |
| 21 | + |
| 22 | + exists(parentEl: HTMLElement): boolean { |
| 23 | + return parentEl.contains(this.reactEl); |
| 24 | + } |
| 25 | + |
| 26 | + insert(referenceEl: HTMLElement): HTMLElement { |
| 27 | + // Find the nearest applicable reference and document elements |
| 28 | + const documentEl = referenceEl.ownerDocument || document; |
| 29 | + const parentEl = referenceEl.parentNode || documentEl; |
| 30 | + |
| 31 | + // Construct a layer element where we can inject a root React component |
| 32 | + const rootLayerEl = documentEl.createElement('div'); |
| 33 | + rootLayerEl.classList.add('ba-Layer'); |
| 34 | + rootLayerEl.classList.add('ba-Layer--highlight'); |
| 35 | + rootLayerEl.dataset.testid = 'ba-Layer--highlight'; |
| 36 | + rootLayerEl.setAttribute('data-resin-feature', 'annotations'); |
| 37 | + |
| 38 | + // Insert the new layer element immediately after the reference element |
| 39 | + return parentEl.insertBefore(rootLayerEl, referenceEl.nextSibling); |
| 40 | + } |
| 41 | + |
| 42 | + render(props: Props): void { |
| 43 | + ReactDOM.render(<HighlightContainer {...props} />, this.reactEl); |
| 44 | + } |
| 45 | + |
| 46 | + style(styles: Partial<CSSStyleDeclaration>): CSSStyleDeclaration { |
| 47 | + return Object.assign(this.reactEl.style, styles); |
| 48 | + } |
| 49 | +} |
0 commit comments