-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonView.js
More file actions
278 lines (241 loc) · 8.98 KB
/
Copy pathjsonView.js
File metadata and controls
278 lines (241 loc) · 8.98 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ========== FILE: jsonView.js (WAVE2 node-features lane) ==========
//
// A READ-ONLY "View as JSON" panel: shows the current flow's canonical
// .flow.json and a line diff against the last-saved bytes.
//
// Editing the JSON in place is DEFERRED — round-trip safety (marker encoding in
// request bodies, extract-path normalization, additive schema evolution) has
// not been proven, and a broken round-trip could silently corrupt a shared
// .flow.json. Until then this panel is strictly read-only: no textarea, no
// contenteditable.
//
// The serialization goes through flowCore.flowModelToJson — the SAME function
// the save path uses — so "View as JSON" always shows exactly what would be
// written to disk, and the diff is a true against-last-saved diff.
//
// Colors/spacing come from the OKLCH semantic tokens in styles.css (see the
// `/* === WAVE2 LANE node-features === */` block at the end of that file).
import { flowModelToJson } from './flowCore.js';
// --- Pure helpers (unit-tested; no DOM) ------------------------------------
/**
* Serialize a flow model to pretty-printed, canonical JSON text — identical to
* what the save path would persist. Null/empty model yields ''.
* @param {object|null} model
* @returns {string}
*/
export function flowToPrettyJson(model) {
if (!model) return '';
try {
return JSON.stringify(flowModelToJson(model), null, 2);
} catch (err) {
// eslint-disable-next-line no-console
console.error('[jsonView] failed to serialize flow model:', err);
return '';
}
}
/**
* Compute a line-level diff between two blocks of text using a classic LCS.
* Returns an ordered list of `{ type, text }` where type is one of
* 'context' (unchanged), 'add' (present only in `next`), 'remove' (present
* only in `prev`). A changed line surfaces as a remove followed by an add.
*
* @param {string} prevText last-saved text ('' when never saved)
* @param {string} nextText current text
* @returns {{type:'context'|'add'|'remove', text:string}[]}
*/
export function computeLineDiff(prevText, nextText) {
const a = String(prevText ?? '').split('\n');
const b = String(nextText ?? '').split('\n');
// Treat a truly empty baseline as zero lines (everything is an add),
// rather than a single empty line.
const prevLines = prevText === '' ? [] : a;
const nextLines = nextText === '' ? [] : b;
const n = prevLines.length;
const m = nextLines.length;
// LCS length table.
const lcs = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
for (let i = n - 1; i >= 0; i--) {
for (let j = m - 1; j >= 0; j--) {
if (prevLines[i] === nextLines[j]) {
lcs[i][j] = lcs[i + 1][j + 1] + 1;
} else {
lcs[i][j] = Math.max(lcs[i + 1][j], lcs[i][j + 1]);
}
}
}
const out = [];
let i = 0;
let j = 0;
while (i < n && j < m) {
if (prevLines[i] === nextLines[j]) {
out.push({ type: 'context', text: prevLines[i] });
i++;
j++;
} else if (lcs[i + 1][j] >= lcs[i][j + 1]) {
out.push({ type: 'remove', text: prevLines[i] });
i++;
} else {
out.push({ type: 'add', text: nextLines[j] });
j++;
}
}
while (i < n) {
out.push({ type: 'remove', text: prevLines[i] });
i++;
}
while (j < m) {
out.push({ type: 'add', text: nextLines[j] });
j++;
}
return out;
}
// --- DOM panel controller --------------------------------------------------
/**
* Create a read-only JSON view panel mounted into `mount`.
* Returns `{ update, hasChanges, destroy }`.
*
* update({ model, savedJson }):
* - model: the current flow model (or null to clear).
* - savedJson: the last-saved pretty JSON text, or null/undefined if the
* flow has never been saved (then the whole doc reads as added).
*
* @param {{mount:HTMLElement}} options
*/
export function createJsonView(options = {}) {
const mount = options.mount;
if (!mount) throw new Error('createJsonView requires a mount element');
let panelEl = null;
let bodyEl = null;
let toggleEl = null;
let statusEl = null;
let currentText = '';
let currentSaved = null;
let currentDiff = [];
let changed = false;
let mode = 'diff'; // 'diff' | 'raw'
let hasModel = false;
let toggleHandler = null;
function ensureDom() {
if (panelEl) return;
panelEl = document.createElement('div');
panelEl.className = 'jsonview-panel';
const header = document.createElement('div');
header.className = 'jsonview-header';
const title = document.createElement('h4');
title.className = 'jsonview-title';
title.textContent = 'Flow JSON';
header.appendChild(title);
statusEl = document.createElement('span');
statusEl.className = 'jsonview-status';
header.appendChild(statusEl);
toggleEl = document.createElement('button');
toggleEl.type = 'button';
toggleEl.className = 'jsonview-mode-toggle btn btn-sm btn-secondary';
toggleEl.textContent = 'Show Full JSON';
toggleHandler = () => {
mode = mode === 'diff' ? 'raw' : 'diff';
toggleEl.textContent = mode === 'diff' ? 'Show Full JSON' : 'Show Diff';
render();
};
toggleEl.addEventListener('click', toggleHandler);
header.appendChild(toggleEl);
bodyEl = document.createElement('div');
bodyEl.className = 'jsonview-body';
panelEl.appendChild(header);
panelEl.appendChild(bodyEl);
mount.appendChild(panelEl);
}
function render() {
ensureDom();
bodyEl.innerHTML = '';
if (!hasModel) {
statusEl.textContent = '';
statusEl.className = 'jsonview-status';
toggleEl.style.display = 'none';
const empty = document.createElement('div');
empty.className = 'jsonview-empty';
empty.textContent = 'No flow loaded.';
bodyEl.appendChild(empty);
return;
}
toggleEl.style.display = '';
if (changed) {
statusEl.textContent = currentSaved === null
? 'Unsaved (new flow)'
: 'Unsaved changes';
statusEl.className = 'jsonview-status changed';
} else {
statusEl.textContent = 'Saved';
statusEl.className = 'jsonview-status saved';
}
if (mode === 'raw') {
renderRaw();
} else {
renderDiff();
}
}
function renderRaw() {
const pre = document.createElement('pre');
pre.className = 'jsonview-raw';
const code = document.createElement('code');
code.textContent = currentText;
pre.appendChild(code);
bodyEl.appendChild(pre);
}
function renderDiff() {
const pre = document.createElement('pre');
pre.className = 'jsonview-diff';
for (const line of currentDiff) {
const row = document.createElement('div');
row.className = `jsonview-line ${line.type}`;
const gutter = document.createElement('span');
gutter.className = 'jsonview-gutter';
gutter.textContent = line.type === 'add' ? '+' : line.type === 'remove' ? '-' : ' ';
const text = document.createElement('span');
text.className = 'jsonview-text';
// Preserve empty lines' height.
text.textContent = line.text.length ? line.text : '';
row.appendChild(gutter);
row.appendChild(text);
pre.appendChild(row);
}
bodyEl.appendChild(pre);
}
return {
update({ model, savedJson } = {}) {
hasModel = !!model;
currentText = model ? flowToPrettyJson(model) : '';
currentSaved = savedJson == null ? null : String(savedJson);
if (!hasModel) {
changed = false;
currentDiff = [];
render();
return;
}
const baseline = currentSaved === null ? '' : currentSaved;
currentDiff = computeLineDiff(baseline, currentText);
if (currentSaved === null) {
// Never saved: the whole document is new/changed.
changed = currentText.length > 0;
} else {
changed = currentDiff.some((d) => d.type !== 'context');
}
render();
},
hasChanges() {
return changed;
},
destroy() {
if (toggleEl && toggleHandler) {
toggleEl.removeEventListener('click', toggleHandler);
}
if (panelEl && panelEl.parentNode) {
panelEl.parentNode.removeChild(panelEl);
}
panelEl = null;
bodyEl = null;
toggleEl = null;
statusEl = null;
},
};
}