Skip to content

Commit cb56c13

Browse files
authored
Upgraded performance using AG grid
Upgraded performance using AG grid
2 parents 0d08c88 + c76b610 commit cb56c13

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spread-diff-patch",
3-
"version": "1.2.2",
3+
"version": "1.2.5",
44
"description": "Diff & patch SpreadSheet files",
55
"main": "./lib/index.js",
66
"module": "./lib/index.mjs",
@@ -14,14 +14,14 @@
1414
},
1515
"repository": {
1616
"type": "git",
17-
"url": "git+https://github.com/dev-DTECH/spread-diff-patch.git"
17+
"url": "git+https://github.com/dtech-labs/spread-diff-patch.git"
1818
},
1919
"author": "DTech",
2020
"license": "MIT",
2121
"bugs": {
22-
"url": "https://github.com/dev-DTECH/spread-diff-patch/issues"
22+
"url": "https://github.com/dtech-labs/spread-diff-patch/issues"
2323
},
24-
"homepage": "https://github.com/dev-DTECH/spread-diff-patch#readme",
24+
"homepage": "https://github.com/dtech-labs/spread-diff-patch#readme",
2525
"keywords": [
2626
"csv",
2727
"html",

src/formatter/html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class HTML extends Formatter<string> {
4141
}
4242
</style>
4343
<div id="spread-diff-patch-data" data-raw-diffAOA='${escapeHTML(JSON.stringify(patchedAOA))}'></div>
44-
<script src="https://unpkg.com/canvas-datagrid"></script>
44+
<script src="https://cdn.jsdelivr.net/npm/ag-grid-community@34.1.2/dist/ag-grid-community.min.js"></script>
4545
<script>
4646
${script}
4747
</script>

src/formatter/script.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
/* eslint-disable no-undef */
22
function isHTML(str) {
3+
if (typeof str !== 'string') return false;
34
var doc = new DOMParser().parseFromString(str, "text/html");
45
return Array.from(doc.body.childNodes).some(node => node.nodeType === 1);
56
}
67

7-
const diffAOA = JSON.parse(document.querySelector("#spread-diff-patch-data").dataset.rawDiffaoa)
8+
const diffAOA = JSON.parse(document.querySelector("#spread-diff-patch-data").dataset.rawDiffaoa);
89

910
const app = document.querySelector('#spread-diff-patch');
1011
const gridElement = document.createElement('div');
11-
gridElement.id = "diff-grid"
12-
const grid = canvasDatagrid({
13-
parentNode: gridElement,
14-
editable: false,
15-
});
16-
12+
gridElement.id = "diff-grid";
13+
gridElement.style.height = '100vh';
14+
gridElement.style.width = '100%';
15+
gridElement.className = 'ag-theme-alpine'; // or other theme
1716
app.append(gridElement);
1817

19-
grid.style.height = '100%';
20-
grid.style.width = '100%';
21-
grid.data = diffAOA
18+
// Generate column definitions
19+
const columnDefs = diffAOA[0] ? diffAOA[0].map((_, index) => ({
20+
headerName: String.fromCharCode(65 + index), // A, B, C...
21+
field: String(index),
22+
cellRenderer: params => {
23+
if (isHTML(params.value)) {
24+
const e = document.createElement("div")
25+
e.innerHTML = params.value
26+
return e
27+
}
28+
return params.value
29+
}
30+
})) : [];
31+
32+
// Convert array of arrays to array of objects
33+
const rowData = diffAOA.map(row => {
34+
const rowObject = {};
35+
row.forEach((cell, index) => {
36+
rowObject[String(index)] = cell;
37+
});
38+
return rowObject;
39+
});
40+
41+
const gridOptions = {
42+
columnDefs: columnDefs,
43+
rowData: rowData,
44+
defaultColDef: {
45+
editable: false,
46+
sortable: true,
47+
filter: true,
48+
resizable: true
49+
}
50+
};
2251

23-
grid.addEventListener('afterrendercell', function (e) {
24-
if (isHTML(e.cell.value))
25-
e.cell.innerHTML = e.cell.value
26-
});
52+
agGrid.createGrid(gridElement, gridOptions);

0 commit comments

Comments
 (0)