8000 Lazy loading files support by rtfpessoa · Pull Request #410 · rtfpessoa/diff2html · GitHub
[go: up one dir, main page]

Skip to content

Lazy loading files support #410

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
wip: add highlight to lazy loading
  • Loading branch information
rtfpessoa committed Oct 15, 2021
commit dc3c4be2b2352e471ab9048252d1656cce26874a
90 changes: 53 additions & 37 deletions src/ui/js/diff2html-ui-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export class Diff2HtmlUI {

constructor(target: HTMLElement, diffInput?: string | DiffFile[], config: Diff2HtmlUIConfig = {}, hljs?: HLJSApi) {
this.config = { ...defaultDiff2HtmlUIConfig, ...config };

if (config.lazy && (config.fileListStartVisible ?? true)) {
this.config.fileListStartVisible = true;
}

this.diffFiles = typeof diffInput === 'string' ? parse(diffInput, this.config) : diffInput ?? [];
this.diffHtml = diffInput !== undefined ? html(this.diffFiles, this.config) : target.innerHTML;
this.targetElement = target;
Expand Down Expand Up @@ -85,13 +90,18 @@ export class Diff2HtmlUI {
const fileListItems: NodeListOf<HTMLElement> = this.targetElement.querySelectorAll('.d2h-file-name');
fileListItems.forEach((i, idx) =>
i.addEventListener('click', () => {
console.log('HERE');
const fileId = i.getAttribute('href');
if (fileId && this.targetElement.querySelector(fileId)) {
return;
}

const tmpDiv = document.createElement('div');
tmpDiv.innerHTML = htmlFile(this.diffFiles[idx], this.config);
const fileElem = tmpDiv.querySelector('.d2h-file-wrapper');

if (tmpDiv.firstChild) {
this.targetElement.querySelector('.d2h-wrapper')?.appendChild(tmpDiv.firstChild);
if (fileElem) {
this.targetElement.querySelector('.d2h-wrapper')?.appendChild(fileElem);
this.highlightFile(fileElem);
}
}),
);
Expand Down Expand Up @@ -159,43 +169,49 @@ export class Diff2HtmlUI {

// Collect all the diff files and execute the highlight on their lines
const files = this.targetElement.querySelectorAll('.d2h-file-wrapper');
files.forEach(file => {
files.forEach(this.highlightFile);
}

highlightFile(file: Element): void {
if (this.hljs === null) {
throw new Error('Missing a `highlight.js` implementation. Please provide one when instantiating Diff2HtmlUI.');
}

// HACK: help Typescript know that `this.hljs` is defined since we already checked it
if (this.hljs === null) return;
const language = file.getAttribute('data-lang');
const hljsLanguage = language ? getLanguage(language) : 'plaintext';

// Collect all the code lines and execute the highlight on them
const codeLines = file.querySelectorAll('.d2h-code-line-ctn');
codeLines.forEach(line => {
// HACK: help Typescript know that `this.hljs` is defined since we already checked it
if (this.hljs === null) return;
const language = file.getAttribute('data-lang');
const hljsLanguage = language ? getLanguage(language) : 'plaintext';

// Collect all the code lines and execute the highlight on them
const codeLines = file.querySelectorAll('.d2h-code-line-ctn');
codeLines.forEach(line => {
// HACK: help Typescript know that `this.hljs` is defined since we already checked it
if (this.hljs === null) return;

const text = line.textContent;
const lineParent = line.parentNode;

if (text === null || lineParent === null || !this.isElement(lineParent)) return;

const result: HighlightResult = closeTags(
this.hljs.highlight(text, {
language: hljsLanguage,
ignoreIllegals: true,
}),
);

const originalStream = nodeStream(line);
if (originalStream.length) {
const resultNode = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
resultNode.innerHTML = result.value;
result.value = mergeStreams(originalStream, nodeStream(resultNode), text);
}

line.classList.add('hljs');
if (result.language) {
line.classList.add(result.language);
}
line.innerHTML = result.value;
});
const text = line.textContent;
const lineParent = line.parentNode;

if (text === null || lineParent === null || !this.isElement(lineParent)) return;

const result: HighlightResult = closeTags(
this.hljs.highlight(text, {
language: hljsLanguage,
ignoreIllegals: true,
}),
);

const originalStream = nodeStream(line);
if (originalStream.length) {
const resultNode = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
resultNode.innerHTML = result.value;
result.value = mergeStreams(originalStream, nodeStream(resultNode), text);
}

line.classList.add('hljs');
if (result.language) {
line.classList.add(result.language);
}
line.innerHTML = result.value;
});
}

Expand Down
0