Skip to content

Commit

Permalink
Improve textviewer rendering speed
Browse files Browse the repository at this point in the history
  • Loading branch information
david-yz-liu committed Sep 4, 2024
1 parent f05a82c commit 129f460
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### ✨ New features and improvements

- Improve textviewer rendering speed (#7211)

### 🐛 Bug fixes

### 🔧 Internal changes
Expand Down
75 changes: 25 additions & 50 deletions app/assets/javascripts/Components/Result/text_viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,65 +65,40 @@ export class TextViewer extends React.PureComponent {

run_syntax_highlighting = () => {
Prism.highlightElement(this.raw_content.current, false);

let nodeLines = [];
let currLine = document.createElement("span");
currLine.classList.add("source-line");
let currChildren = [];
for (let node of this.raw_content.current.childNodes) {
if (node.nodeType === Node.TEXT_NODE) {
// SourceCodeLine.glow assumes text nodes are wrapped in <span> elements
let textContainer = document.createElement("span");
let textNode = null;
if (node.textContent.includes("\n")) {
const splits = node.textContent.split("\n");
for (let i = 0; i < splits.length - 1; i++) {
textNode = document.createTextNode(splits[i] + "\n");
textContainer.appendChild(textNode);
currLine.appendChild(textContainer);
nodeLines.push(currLine);
currLine = document.createElement("span");
currLine.classList.add("source-line");
textContainer = document.createElement("span");
}
textNode = document.createTextNode(splits[splits.length - 1]);
} else {
textNode = node.cloneNode(true);
}
textContainer.appendChild(textNode);
currLine.appendChild(textContainer);
} else {
if (node.textContent.includes("\n")) {
const splits = node.textContent.split("\n");
let textContainer = document.createElement("span");
textContainer.className = node.className;
let textNode = null;
for (let i = 0; i < splits.length - 1; i++) {
textNode = document.createTextNode(splits[i] + "\n");
textContainer.appendChild(textNode);
currLine.appendChild(textContainer);
nodeLines.push(currLine);
currLine = document.createElement("span");
currLine.classList.add("source-line");
textContainer = document.createElement("span");
textContainer.className = node.className;
}
textNode = document.createTextNode(splits[splits.length - 1]);
textContainer.appendChild(textNode);
currLine.appendChild(textContainer);
} else {
currLine.appendChild(node.cloneNode(true));
}
const splits = node.textContent.split("\n");

// Note: SourceCodeLine.glow assumes text nodes are wrapped in <span> elements
let textContainer = document.createElement("span");
let className = node.nodeType === Node.TEXT_NODE ? "" : node.className;
textContainer.className = className;

for (let i = 0; i < splits.length - 1; i++) {
textContainer.textContent = splits[i] + "\n";
currLine.append(...currChildren, textContainer);
nodeLines.push(currLine);
currLine = document.createElement("span");
currLine.classList.add("source-line");
currChildren = [];
textContainer = document.createElement("span");
textContainer.className = className;
}

textContainer.textContent = splits[splits.length - 1];
currLine.append(...currChildren, textContainer);
}
if (currLine.textContent.length > 0) {
nodeLines.push(currLine);
}
nodeLines.push(this.raw_content.current.lastChild.cloneNode(true));
while (this.raw_content.current.firstChild) {
this.raw_content.current.removeChild(this.raw_content.current.lastChild);
}
for (let n of nodeLines) {
this.raw_content.current.appendChild(n);
}
this.raw_content.current.replaceChildren(
...nodeLines,
this.raw_content.current.lastChild.cloneNode(true)
);
};

change_font_size = delta => {
Expand Down

0 comments on commit 129f460

Please sign in to comment.