-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.js
147 lines (135 loc) · 4.59 KB
/
demo.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {createView} from "./gridchen/matrixview.js"
import {createTransactionManager, registerUndo} from "./gridchen/utils.js";
export function createInteractiveDemoGrid(schema, orgData) {
const container = document.body.appendChild(document.createElement('div'));
schema.pathPrefix = '';
function html(html) {
container.innerHTML = html;
}
html`
<label class="demo">
Edit JSON Schema
<textarea class="demo schema"></textarea>
</label>
<label class="demo">
Edit JavaScript Matrix
<textarea class="demo data"></textarea>
</label>
<label class="demo grid">
Grid
<grid-chen></grid-chen>
</label>
<label class="demo patch">
JSON Transaction Patch
<textarea class="demo" readonly></textarea>
</label>
<label class="demo tsv">
TabularSeparatedValues (as of clipboard)
<textarea class="demo" readonly></textarea>
</label>`;
const schemaElement = container.querySelector('.schema');
const dataElement = container.querySelector('.data');
const patchElement = document.querySelector('.patch textarea');
const tsvElement = document.querySelector('.tsv textarea');
const gridElement = container.querySelector('grid-chen');
const tm = createTransactionManager();
registerUndo(document.body, tm);
let view;
function resetGrid() {
function newView() {
try {
schema = JSON.parse(schemaElement.value);
} catch (e) {
e.message = 'Error in JSON Schema: ' + e.message;
return e;
}
let data;
if (dataElement.value.trim() === '') {
dataElement.value = 'null';
}
try {
data = REPR.parse(dataElement.value);
} catch (e) {
e.message = 'Error in JavaScript Data: ' + e.message;
return e;
}
return createView(schema, data);
}
patchElement.value = '';
view = newView();
gridElement.resetFromView(view, tm);
tsvElement.value = gridElement._toTSV();
}
/**
* @type {GridChenNS.Transaction} trans
*/
tm.addEventListener('change', function (evt) {
dataElement.value = REPR.stringify(view.getModel(), null, 2);
patchElement.value = REPR.stringify(evt.transaction.operations, null, 2);
tsvElement.value = gridElement._toTSV();
});
schemaElement.value = JSON.stringify(schema, null, 4);
dataElement.oninput = schemaElement.oninput = resetGrid;
dataElement.value = REPR.stringify(orgData, null, 2);
resetGrid();
}
/**
* Same as the global JSON object, but representation is JavaScript, not JSON.
*/
export const REPR = {
/**
* Return a JavaScript object from its representation.
* @param {string} s
* @returns {*}
*/
parse(s) {
s = s.trim();
if (s === '') {
throw new SyntaxError('Unexpected end of REPR input');
}
return eval('(' + s + ')')
},
/**
* Return a string containing a printable representation of an object.
* @param {*} v
* @param {null} replacer
* @param {number=} depth
* @param {number=} level
* @returns {string}
*/
stringify(v, replacer, depth, level) {
level = level || 0;
depth = depth || 0;
const nl0 = '\n' + Array.from({length: level * depth}, () => ' ').join('');
const nl1 = nl0 + Array.from({length: depth}, () => ' ').join('');
const out = [];
if (v == null) {
out.push('null');
} else if (v.constructor === String) {
out.push('"');
out.push(v.replace('\n', '\\n'));
out.push('"');
} else if (v.constructor === Date) {
out.push('new Date("' + v.toISOString().replace(':00.000Z', 'Z') + '")');
} else if (Array.isArray(v)) {
out.push('[' + nl1);
const a = [];
for (const value of v) {
a.push(REPR.stringify(value, replacer, depth, level + 1));
}
out.push(a.join(',' + nl1));
out.push(nl0 + ']');
} else if (typeof v === 'object') {
out.push('{' + nl1);
const a = [];
for (const [key, value] of Object.entries(v)) {
a.push('"' + key + '": ' + REPR.stringify(value, replacer, depth, level + 1));
}
out.push(a.join(',' + nl1));
out.push(nl0 + '}');
} else {
out.push(v);
}
return out.join('')
}
};