-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (46 loc) · 1.49 KB
/
script.js
File metadata and controls
52 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* eslint-disable no-undef */
function isHTML(str) {
if (typeof str !== 'string') return false;
var doc = new DOMParser().parseFromString(str, "text/html");
return Array.from(doc.body.childNodes).some(node => node.nodeType === 1);
}
const diffAOA = JSON.parse(document.querySelector("#spread-diff-patch-data").dataset.rawDiffaoa);
const app = document.querySelector('#spread-diff-patch');
const gridElement = document.createElement('div');
gridElement.id = "diff-grid";
gridElement.style.height = '100vh';
gridElement.style.width = '100%';
gridElement.className = 'ag-theme-alpine'; // or other theme
app.append(gridElement);
// Generate column definitions
const columnDefs = diffAOA[0] ? diffAOA[0].map((_, index) => ({
headerName: String.fromCharCode(65 + index), // A, B, C...
field: String(index),
cellRenderer: params => {
if (isHTML(params.value)) {
const e = document.createElement("div")
e.innerHTML = params.value
return e
}
return params.value
}
})) : [];
// Convert array of arrays to array of objects
const rowData = diffAOA.map(row => {
const rowObject = {};
row.forEach((cell, index) => {
rowObject[String(index)] = cell;
});
return rowObject;
});
const gridOptions = {
columnDefs: columnDefs,
rowData: rowData,
defaultColDef: {
editable: false,
sortable: true,
filter: true,
resizable: true
}
};
agGrid.createGrid(gridElement, gridOptions);