Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the content of the component if the code property is changed #166

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/pb-code-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class PbCodeHighlight extends themableMixin(LitElement) {
* The code to be highlighted as a string. If not set,
* this will be populated from either a template child element
* or the element's text content.
* The value of the property can be changed programmatically from JavaScript.
*/
code: {
type: String
Expand Down Expand Up @@ -118,6 +119,14 @@ export class PbCodeHighlight extends themableMixin(LitElement) {
updated(changedProperties) {
super.updated(changedProperties);
if (changedProperties.has('code')) {
//if the code property is changed, the content of the <code> element is replaced
const pre = this.shadowRoot.getElementById("pb-code-highlight");
if(pre != null) {
const code = document.createElement("code");
code.textContent = this.code; //textContent property keeps new lines in the code
pre.replaceChildren(code);

}
this.highlight();
}
}
Expand All @@ -130,7 +139,7 @@ export class PbCodeHighlight extends themableMixin(LitElement) {
if (this.code) {
return html`
${this._themeStyles}
<pre class="${this.lineNumbers ? 'line-numbers' : ''} language-${this.language}"><code>${this.code}</code></pre>
<pre id="pb-code-highlight" class="${this.lineNumbers ? 'line-numbers' : ''} language-${this.language}"><code>${this.code}</code></pre>
`;
}
return html`<pre class="line-numbers"><code><code></pre>`;
Expand Down
Loading