Skip to content

Commit 469b00a

Browse files
javier-godoymlopezFC
authored andcommitted
fix: fix Vaadin 21+ compatibility issues
Change the way in which the code highlighter is initialized. Close #11
1 parent dc6defc commit 469b00a

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

src/main/resources/META-INF/resources/frontend/code-viewer.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@ import {
33
customElement,
44
html,
55
LitElement,
6-
property,
76
unsafeCSS,
87
} from "lit-element";
9-
import { unsafeHTML } from "lit-html/directives/unsafe-html";
8+
109
//@ts-ignore
1110
import * as Prism from "./prism.js";
1211
import prismCss from "./prism.css";
1312

1413
@customElement("code-viewer")
1514
export class CodeViewer extends LitElement {
1615

17-
@property({ type: String })
18-
contents = "";
19-
20-
@property({ type: String })
21-
language = "none";
22-
2316
createRenderRoot() {
2417
return this;
2518
}
@@ -40,34 +33,30 @@ export class CodeViewer extends LitElement {
4033
}
4134
</style>
4235
43-
${/*Don't reuse these elements. This is needed because Prism
44-
removes the markers lit-html uses to track slots */
45-
unsafeHTML(
46-
`<pre><code class="language-${this.language}">${this.escapeHtml(
47-
this.contents
48-
)}
49-
</code></pre>`
50-
)}
36+
<pre><code id="code"></code></pre>
5137
`;
5238
}
5339

5440
async fetchContents(sourceUrl: string, language: string) {
5541
var self=this;
5642
var xhr = new XMLHttpRequest();
43+
5744
xhr.onreadystatechange = async function() {
5845
if (this.readyState == 4 && this.status == 200) {
59-
self.contents = this.responseText;
60-
self.language = language;
6146
// Wait for LitElement to finish updating the DOM before higlighting
6247
await self.updateComplete;
48+
var code = self.querySelector("code") as HTMLElement;
6349

50+
code.setAttribute("class", "language-" + language);
51+
code.innerHTML = self.escapeHtml(this.responseText);
52+
6453
//@ts-ignore
6554
Prism.highlightAllUnder(self);
6655
}};
6756
xhr.open('GET', sourceUrl, true);
6857
xhr.send();
6958
}
70-
59+
7160
escapeHtml(unsafe: string) {
7261
return unsafe
7362
.replace(/&/g, "&amp;")

0 commit comments

Comments
 (0)