wrap_annotation = (fn) =>
(config, vis) =>
(...args) => {
let ext_fn = wrap(fn, "slide");
let configs = Array.isArray(config) ? config : [config];
let get_candidates = (cfg, e) => {
let candidates = [];
if (cfg.query) {
candidates.push(e.querySelector(cfg.query));
} else if (cfg.queryAll) {
candidates = candidates.concat(
Array.from(e.querySelectorAll(cfg.queryAll))
);
} else {
candidates.push(e);
}
return candidates;
};
let apply_annotation = (e) => {
let exclusions = configs
.filter((cfg) => cfg.exclude)
.map((cfg) => {
if (!e) return null;
return get_candidates(cfg, e);
})
.flat()
.filter((e) => !!e)
.map((e) => {
return Array.from(e.querySelectorAll("*"));
})
.flat();
let annotations = configs
.filter((cfg) => !cfg.exclude)
.map((cfg) => {
if (!e) return null;
let candidates = get_candidates(cfg, e);
return candidates
.filter((c) => c && !exclusions.includes(c))
.map((c) => roughNotation.annotate(c, cfg));
});
if (annotations.length > 0) {
const ag = roughNotation.annotationGroup(
annotations.flat().filter((a) => a)
);
ag.show();
}
return e;
};
if (args[0].raw) {
if (vis) return vis().then(() => apply_annotation(ext_fn(...args)));
else return apply_annotation(ext_fn(...args));
} else {
let f = ext_fn(args[0]);
return (...rest) => {
if (vis) return vis().then(() => apply_annotation(f(...rest)));
else return apply_annotation(f(...rest));
};
}
}