unscopeOverlays function
document as its notation wrote it, before scopeOverlays: a page's
overlays made its own named as written again (and what names them), a
component's events for them its actions again, and the calls' reactions
to those events gone. document itself when it has none of them.
Implementation
Json unscopeOverlays(Json document) {
final copy = jsonDecode(jsonEncode(document)) as Json;
var changed = false;
final roots = readStrings(copy['roots']);
final nodes = readMap(copy['nodes']) ?? const {};
if (roots.isNotEmpty) {
final map = {
for (final id in roots.skip(1))
if (writtenOverlayId(id, roots.first, readMap(nodes[id])) case final String written) id: written,
};
if (map.isNotEmpty) {
changed = true;
_renameCanvases(copy, map);
_renameReferences(copy, map, (_, _) {});
}
}
void walk(Object? v) {
if (v is List) {
for (var i = 0; i < v.length; i++) {
final a = v[i];
if (a is Map && a['type'] == ComponentEventAction.typeName && isOverlayEvent(readString(a['event'])) && a['value'] is Map) {
v[i] = {...(a['value'] as Map).cast<String, Object?>(), for (final k in _common) if (a.containsKey(k)) k: a[k]};
changed = true;
} else {
walk(a);
}
}
} else if (v is Map) {
final reactions = v['actions'];
if (reactions is List && reactions.any((r) => r is Map && isOverlayEvent(readString(readMap(r['trigger'])?['event'])))) {
reactions.removeWhere((r) => r is Map && isOverlayEvent(readString(readMap(r['trigger'])?['event'])));
if (reactions.isEmpty) v.remove('actions');
changed = true;
}
v.values.toList().forEach(walk);
}
}
walk(copy['nodes']);
return changed ? copy : document;
}