|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { action } from '@ember/object'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import { argOrDefaultDecorator as argOrDefault } from '../../helpers/arg-or-default'; |
| 5 | +import { PaddingSize, FontSize } from '../eui-code-block'; |
| 6 | +//@ts-ignore |
| 7 | +import hljs from 'highlight.js'; |
| 8 | +import { keys } from '../../utils/keys'; |
| 9 | +import { scheduleOnce } from '@ember/runloop'; |
| 10 | + |
| 11 | +type EuiCodeImplArgs = { |
| 12 | + fontSize?: FontSize; |
| 13 | + |
| 14 | + /** |
| 15 | + * Displays the passed code in an inline format. Also removes any margins set. |
| 16 | + */ |
| 17 | + inline?: boolean; |
| 18 | + |
| 19 | + /** |
| 20 | + * Displays an icon button to copy the code snippet to the clipboard. |
| 21 | + */ |
| 22 | + isCopyable?: boolean; |
| 23 | + |
| 24 | + /** |
| 25 | + * Sets the syntax highlighting for a specific language |
| 26 | + */ |
| 27 | + language?: string; |
| 28 | + overflowHeight?: number; |
| 29 | + paddingSize?: PaddingSize; |
| 30 | + transparentBackground?: boolean; |
| 31 | + /** |
| 32 | + * Specify how `white-space` inside the element is handled. |
| 33 | + * `pre` respects line breaks/white space but doesn't force them to wrap the line |
| 34 | + * `pre-wrap` respects line breaks/white space but does force them to wrap the line when necessary. |
| 35 | + */ |
| 36 | + whiteSpace?: 'pre' | 'pre-wrap'; |
| 37 | +}; |
| 38 | + |
| 39 | +export default class EuiAccordionAccordionComponent extends Component<EuiCodeImplArgs> { |
| 40 | + // Defaults |
| 41 | + @argOrDefault(false) declare transparentBackground: boolean; |
| 42 | + @argOrDefault('l') declare paddingSize: string; |
| 43 | + @argOrDefault('s') declare fontSize: string; |
| 44 | + @argOrDefault(false) declare isCopyable: boolean; |
| 45 | + @argOrDefault('pre-wrap') declare whiteSpace: string; |
| 46 | + |
| 47 | + @tracked isFullScreen: boolean = false; |
| 48 | + @tracked isPortalTargetReady: boolean = false; |
| 49 | + @tracked codeTarget: HTMLDivElement | null = null; |
| 50 | + @tracked code: HTMLElement | null = null; |
| 51 | + @tracked codeFullScreen: HTMLElement | null = null; |
| 52 | + |
| 53 | + observer: MutationObserver | null = null; |
| 54 | + |
| 55 | + constructor(owner: unknown, args: EuiCodeImplArgs) { |
| 56 | + super(owner, args); |
| 57 | + this.codeTarget = document.createElement('div'); |
| 58 | + this.isPortalTargetReady = true; |
| 59 | + this.setupObserver(); |
| 60 | + } |
| 61 | + |
| 62 | + setupObserver() { |
| 63 | + this.observer?.disconnect(); |
| 64 | + this.observer = new MutationObserver((mutationsList) => { |
| 65 | + if (mutationsList.length) this.update(); |
| 66 | + }); |
| 67 | + if (this.codeTarget) { |
| 68 | + this.update(); |
| 69 | + this.observer.observe(this.codeTarget, { |
| 70 | + characterData: true, |
| 71 | + subtree: true, |
| 72 | + childList: true |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + willDestroy(): void { |
| 78 | + this.observer?.disconnect(); |
| 79 | + } |
| 80 | + |
| 81 | + @action |
| 82 | + update() { |
| 83 | + const render = () => { |
| 84 | + const language = this.args.language; |
| 85 | + const html = ( |
| 86 | + this.isPortalTargetReady && this.codeTarget?.innerHTML |
| 87 | + ? this.codeTarget.innerHTML |
| 88 | + : '' |
| 89 | + ).trim(); |
| 90 | + |
| 91 | + const code = this.code; |
| 92 | + |
| 93 | + if (code) { |
| 94 | + code.innerHTML = html; |
| 95 | + } |
| 96 | + |
| 97 | + if (language) { |
| 98 | + if (code) { |
| 99 | + hljs.highlightBlock(code); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (this.codeFullScreen) { |
| 104 | + this.codeFullScreen.innerHTML = html; |
| 105 | + if (language) { |
| 106 | + hljs.highlightBlock(this.codeFullScreen); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + scheduleOnce('afterRender', this, render); |
| 111 | + } |
| 112 | + |
| 113 | + @action |
| 114 | + onKeyDown(event: KeyboardEvent): void { |
| 115 | + if (event.key === keys.ESCAPE) { |
| 116 | + event.preventDefault(); |
| 117 | + event.stopPropagation(); |
| 118 | + this.isFullScreen = false; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments