import { demoFunc } from './dees-dataview-codebox.demo.js'; import { DeesElement, html, customElement, type TemplateResult, property, state, cssManager, } from '@design.estate/dees-element'; import hlight from 'highlight.js'; import * as smartstring from '@push.rocks/smartstring'; import * as domtools from '@design.estate/dees-domtools'; import { DeesContextmenu } from './dees-contextmenu.js'; declare global { interface HTMLElementTagNameMap { 'dees-dataview-codebox': DeesDataviewCodebox; } } @customElement('dees-dataview-codebox') export class DeesDataviewCodebox extends DeesElement { public static demo = demoFunc; @property() public progLang: string = 'typescript'; @property({ type: String, reflect: true, }) public codeToDisplay: string = ''; constructor() { super(); } render(): TemplateResult { return html` ${domtools.elementBasic.styles}
index.ts
${(() => { let lineCounter = 0; return this.codeToDisplay.split('\n').map((lineArg) => { lineCounter++; return html`
${lineCounter}
`; }); })()}
Spaces: 2
${this.progLang}
`; } @state() private codeToDisplayStore = ''; public async updated(_changedProperties) { super.updated(_changedProperties); console.log('highlighting now'); console.log(this.childNodes); const slottedCodeNodes: Text[] = []; this.childNodes.forEach((childNode) => { if (childNode.nodeName === '#text') { slottedCodeNodes.push(childNode as Text); } }); if (this.codeToDisplay && this.codeToDisplay !== this.codeToDisplayStore) { this.codeToDisplayStore = smartstring.indent.normalize(this.codeToDisplay).trimStart(); } if (slottedCodeNodes[0] && slottedCodeNodes[0].wholeText && !this.codeToDisplay) { this.codeToDisplayStore = smartstring.indent .normalize(slottedCodeNodes[0].wholeText) .trimStart(); this.codeToDisplay = this.codeToDisplayStore; } await domtools.plugins.smartdelay.delayFor(0); const localCodeNode = this.shadowRoot.querySelector('code'); const html = hlight.highlight(this.codeToDisplayStore, { language: this.progLang, ignoreIllegals: true, }); localCodeNode.innerHTML = html.value; } }