Skip to content

Commit

Permalink
load more complete blocks if loadLimit is not reached
Browse files Browse the repository at this point in the history
  • Loading branch information
campersau committed Feb 29, 2020
1 parent 6f979da commit 9c8bf6e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions components/textdiff/textdiff.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<!-- ko if: isParsed -->
<div data-bind="template: {nodes: ko.utils.parseHtmlFragment(htmlSrc)}, css: {'word-wrap': wordWrap.value}"></div>
<!-- /ko -->
<div class="btn-load-more" data-bind="visible: loadMoreCount() > 0">
<span class="btn btn-warning" data-bind="click: loadMore, text: 'Load ' + loadMoreCount() + ' more lines'"></span>
<div class="btn-load-more" data-bind="visible: hasMore">
<span class="btn btn-warning" data-bind="click: loadMore, text: 'Load more'"></span>
</div>
</div>
<!-- /ko -->
20 changes: 7 additions & 13 deletions components/textdiff/textdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TextDiffViewModel {
this.repoPath = args.repoPath;
this.server = args.server;
this.sha1 = args.sha1;
this.loadMoreCount = ko.observable(0);
this.hasMore = ko.observable(false);
this.diffJson = null;
this.loadCount = loadLimit;
this.textDiffType = args.textDiffType;
Expand Down Expand Up @@ -134,26 +134,20 @@ class TextDiffViewModel {
}

let lineCount = 0;
let loadCount = 0;
this.diffJson[0].blocks = this.diffJson[0].allBlocks.reduce((blocks, block) => {
const length = (block.allLines || block.lines).length;
const length = block.lines.length;
const remaining = this.loadCount - lineCount;
if (remaining > 0) {
if (remaining < length) {
if (!block.allLines) {
block.allLines = block.lines;
}
block.lines = block.allLines.slice(0, remaining);
} else if (block.allLines) {
block.lines = block.allLines;
block.allLines = undefined;
}
loadCount += length;
blocks.push(block);
}
lineCount += length;
return blocks;
}, []);

this.loadMoreCount(Math.min(loadLimit, Math.max(0, lineCount - this.loadCount)));
this.loadCount = loadCount;
this.hasMore(lineCount > loadCount);

let html = diff2html.html(this.diffJson, {
outputFormat: this.textDiffType.value() === sideBySideDiff ? 'side-by-side' : 'line-by-line',
Expand Down Expand Up @@ -186,7 +180,7 @@ class TextDiffViewModel {
}

loadMore() {
this.loadCount += this.loadMoreCount();
this.loadCount += loadLimit;
this.render();
}

Expand Down

0 comments on commit 9c8bf6e

Please sign in to comment.