Files
nav-carte/components/GrapheConnaissances.vue

485 lines
19 KiB
Vue

<!--
GrapheConnaissances — B19-M2/M4. Un graphe biparti générique : des nœuds principaux
(par défaut les concepts) et des nœuds secondaires (les sources : textes, organisations,
outils), reliés par des liens typés. Écrit pour /connaissances, et pour être repris tel
quel par la bibliothèque du corpus (B9 réécrit) : il ne connaît ni la vue fiches, ni la
couche, seulement `noeuds`, `liens`, `selection`, `survol`.
La manière de GrapheMots (B6) : SVG rendu au serveur, taille mesurée, nœuds secondaires
hors tabulation avec <title>, estompage. Rien de sa couronne de sept mots.
Les gestes repris de l'explorateur Holosapiens (CONSTAT du 12/10, §4), en mieux :
- clic = recadrage en 600 ms au plus, puis immobile ; aucune dérive perpétuelle ;
- sœurs estompées, chemin allumé (liens de la sélection), un seul accent safran ;
- nœuds principaux = <button>, au clavier ; `prefers-reduced-motion` = état final, sans transition ;
- caméra bornée à la zone libre : le panneau de fiche est une colonne à part (VueFiches),
la barre de pas et la poignée ont leur marge ; aucun nœud dessous.
Graphe local (M4) : une sélection restreint le graphe à son voisinage à un ou deux pas
(bascule « 1 pas / 2 pas / tout »), comme le graphe local d'Obsidian ; sans sélection,
le graphe entier.
Layout : force simple à graine fixe, calculé depuis nœuds et liens (aucune position en
données), mis en cache par signature du graphe (une fois par processus serveur), puis
projeté sur la taille mesurée. Liens : trait plein = énoncé par la source, tirets = posé
par la curation. La géométrie (disposer, cache, voisinage, boîte) vit dans
`utils/connaissances/graphe.ts`, testée (B19b M4).
-->
<template>
<div ref="racine" class="gc" :class="{ 'gc--fige': fige }">
<svg class="gc__svg" :viewBox="`0 0 ${taille.w} ${taille.h}`" aria-hidden="true" focusable="false">
<g class="gc__liens">
<line
v-for="l in liensVus"
:key="l.cle"
:x1="l.x1" :y1="l.y1" :x2="l.x2" :y2="l.y2"
class="gc__lien"
:class="[`gc__lien--${l.provenance}`, { 'gc__lien--vif': l.vif, 'gc__lien--eteint': l.eteint }]"
/>
</g>
<g
v-for="s in secondairesVus"
:key="s.id"
class="gc__source"
:class="{ 'gc__source--sel': s.sel, 'gc__source--survol': s.survol, 'gc__source--eteint': s.eteint === 'fort', 'gc__source--loin': s.eteint === 'leger' }"
:transform="`translate(${s.x} ${s.y})`"
@click="emit('select', s.id)"
@mouseenter="emit('survol', s.id)"
@mouseleave="emit('survol', null)"
>
<title>{{ s.libelle }}</title>
<rect x="-6" y="-6" width="12" height="12" rx="2" class="gc__carre" />
<text v-if="s.etiquette" x="10" y="4" class="gc__source-texte">{{ s.etiquette }}</text>
</g>
</svg>
<div class="gc__noeuds" role="group" :aria-label="titre">
<button
v-for="p in principauxVus"
:key="p.id"
type="button"
class="gc__noeud"
:class="{ 'gc__noeud--sel': p.sel, 'gc__noeud--survol': p.survol, 'gc__noeud--eteint': p.eteint === 'fort', 'gc__noeud--loin': p.eteint === 'leger' }"
:style="{ left: `${p.x}px`, top: `${p.y}px`, '--gc-r': `${p.r}px` }"
:data-id="p.id"
:aria-label="p.aria"
:aria-pressed="p.sel ? 'true' : 'false'"
@click="emit('select', p.id)"
@mouseenter="emit('survol', p.id)"
@mouseleave="emit('survol', null)"
@focus="emit('survol', p.id)"
@blur="emit('survol', null)"
>
<span class="gc__rond" aria-hidden="true" />
<span class="gc__libelle" aria-hidden="true">{{ p.libelle }}</span>
</button>
</div>
<div v-if="selVue" class="gc__barre" role="group" aria-label="Voisinage affiché">
<button
v-for="o in OPTIONS_PAS"
:key="o.v"
type="button"
class="gc__pas"
:aria-pressed="pas === o.v ? 'true' : 'false'"
@click="pas = o.v"
>{{ o.libelle }}</button>
</div>
<!-- Légende visible (retour de Jules du 27/10, n° 6 : « pourquoi des carrés, pourquoi des ronds ») -->
<ul class="gc__legende" aria-label="Légende du graphe">
<li><span class="gc__legende-rond" aria-hidden="true" />Concept</li>
<li><span class="gc__legende-carre" aria-hidden="true" />Source (texte, organisation, outil)</li>
<li><span class="gc__legende-trait" aria-hidden="true" />Énoncé par la source</li>
<li><span class="gc__legende-trait gc__legende-trait--curation" aria-hidden="true" />Posé par AEP</li>
</ul>
</div>
</template>
<script setup lang="ts">
import { boite, disposerEnCache, voisinage, type LienGraphe, type NoeudGraphe, type Pas, type Pt } from '~/utils/connaissances/graphe'
const props = withDefaults(defineProps<{
noeuds: NoeudGraphe[]
liens: LienGraphe[]
selection: string | null
survol: string | null
/** Kinds rendus en nœuds principaux (ronds, boutons) ; les autres sont secondaires (carrés). */
kindsPrincipaux?: string[]
/** Pas du graphe local à l'ouverture d'une sélection. */
pasDefaut?: Pas
/** Nom accessible du groupe de nœuds. */
titre?: string
/** Libellé de kind pour les noms accessibles (« concept », « texte »…). */
libelleKind?: (kind: string) => string
/** Nœuds du résultat courant (filtres de la page) ; les autres s'estompent sans disparaître. null = tous. */
retenus?: Set<string> | null
}>(), {
kindsPrincipaux: () => ['concept'],
pasDefaut: 1,
titre: 'Graphe',
libelleKind: (k: string) => k,
retenus: null,
})
const emit = defineEmits<{
select: [id: string]
survol: [id: string | null]
}>()
const OPTIONS_PAS: { v: Pas; libelle: string }[] = [
{ v: 1, libelle: '1 pas' },
{ v: 2, libelle: '2 pas' },
{ v: 'tout', libelle: 'tout' },
]
const DUREE = 600 // recadrage : ≤ 700 ms, puis immobile
const MARGE = { haut: 52, bas: 88, gauche: 96, droite: 96 } // bas : la légende (une ligne) et le libellé du dernier nœud, qui pend sous son rond
const MARGE_TEL = { haut: 52, bas: 116, gauche: 64, droite: 64 } // < 768 : la légende tient sur deux lignes en bas ; les libellés sont tenus dans le cadre par la borne de `ecran`
// ── Taille mesurée (le rendu serveur part d'une taille plausible, comme GrapheMots) ──
const racine = ref<HTMLElement | null>(null)
const taille = reactive({ w: 720, h: 560 })
const fige = ref(false) // prefers-reduced-motion : état final, sans transition
let redim = false
onMounted(() => {
const mq = window.matchMedia('(prefers-reduced-motion: reduce)')
fige.value = mq.matches
const majMq = () => { fige.value = mq.matches }
mq.addEventListener('change', majMq)
const el = racine.value
if (el && typeof ResizeObserver !== 'undefined') {
const maj = (w: number, h: number, initiale = false) => {
if (w > 40 && h > 40 && (Math.round(w) !== taille.w || Math.round(h) !== taille.h)) {
redim = initiale // la première mesure saute ; un redimensionnement (fiche ouverte, fenêtre) se recadre en douceur
taille.w = Math.round(w)
taille.h = Math.round(h)
}
}
maj(el.clientWidth, el.clientHeight, true)
const ro = new ResizeObserver((e) => { const r = e[0]?.contentRect; if (r) maj(r.width, r.height) })
ro.observe(el)
onBeforeUnmount(() => ro.disconnect())
}
onBeforeUnmount(() => { mq.removeEventListener('change', majMq); if (raf) cancelAnimationFrame(raf) })
})
// ── Layout (repère abstrait, en cache) ──
const principaux = computed(() => new Set(props.kindsPrincipaux))
const monde = computed(() => disposerEnCache(props.noeuds, props.liens, principaux.value))
const ids = computed(() => new Set(props.noeuds.map((x) => x.id)))
// ── Sélection et voisinage ──
const selVue = computed(() => (props.selection && ids.value.has(props.selection) ? props.selection : null))
const pas = ref<Pas>(props.pasDefaut)
watch(selVue, (s, avant) => { if (s && !avant) pas.value = props.pasDefaut })
const visibles = computed<Set<string> | null>(() => (selVue.value ? voisinage(props.liens, selVue.value, pas.value) : null))
const voisinsSel = computed(() => (selVue.value ? voisinage(props.liens, selVue.value, 1)! : null))
const voisinsSurvol = computed(() => (props.survol && ids.value.has(props.survol) ? voisinage(props.liens, props.survol, 1)! : null))
// ── Caméra : boîte des nœuds visibles → zone libre ──
interface Camera { cx: number; cy: number; s: number }
const zone = computed(() => {
const m = taille.w < 768 ? MARGE_TEL : MARGE
return { x: m.gauche, y: m.haut, w: Math.max(40, taille.w - m.gauche - m.droite), h: Math.max(40, taille.h - m.haut - m.bas) }
})
/** Le dessin est-il en paysage ? Calculé une fois par layout (le monde est en cache), pas à chaque redimensionnement. */
const paysage = computed(() => {
const b = boite(monde.value.values())
return b.x1 - b.x0 >= b.y1 - b.y0
})
/** Portrait d'écran et dessin paysage (ou l'inverse) : on tourne le dessin d'un quart. Projection, rien en base. */
const tourne = computed(() => paysage.value !== zone.value.w >= zone.value.h)
const pt = (id: string): Pt | null => {
const p = monde.value.get(id)
if (!p) return null
return tourne.value ? { x: p.y, y: -p.x } : p
}
const cible = computed<Camera>(() => {
const vis = visibles.value
const points = props.noeuds
.filter((x) => !vis || vis.has(x.id))
.map((x) => pt(x.id))
.filter((p): p is Pt => !!p)
const { x0, x1, y0, y1 } = boite(points)
if (!Number.isFinite(x0)) return { cx: 0, cy: 0, s: 1 }
const z = zone.value
const plein = Math.min(z.w, z.h) // échelle du graphe entier (étendue 1)
const s = Math.min(z.w / Math.max(x1 - x0, 1e-3), z.h / Math.max(y1 - y0, 1e-3), plein * 2.4)
return { cx: (x0 + x1) / 2, cy: (y0 + y1) / 2, s }
})
const camera = reactive<Camera>({ ...cible.value })
let raf = 0
watch(cible, (c) => {
if (raf) { cancelAnimationFrame(raf); raf = 0 }
if (!import.meta.client || fige.value || redim) {
redim = false
Object.assign(camera, c)
return
}
const de = { ...camera }
const t0 = performance.now()
const pasAnim = (t: number) => {
const u = Math.min(1, (t - t0) / DUREE)
const e = 1 - Math.pow(1 - u, 3)
camera.cx = de.cx + (c.cx - de.cx) * e
camera.cy = de.cy + (c.cy - de.cy) * e
camera.s = de.s + (c.s - de.s) * e
raf = u < 1 ? requestAnimationFrame(pasAnim) : 0
}
raf = requestAnimationFrame(pasAnim)
})
/** Bord à garder libre autour du centre d'un nœud : la demi-largeur maximale d'un libellé (9,5 rem) pour un principal. */
const BORD_PRINCIPAL = 80 // 64 sous 768 px, où le libellé passe à 8 rem
const BORD_SECONDAIRE = 12
const kindDe = computed(() => new Map(props.noeuds.map((x) => [x.id, x.kind])))
const ecran = (id: string): Pt | null => {
const p = pt(id)
if (!p) return null
const z = zone.value
const x = (p.x - camera.cx) * camera.s + z.x + z.w / 2
// Garde-fou : aucun libellé ne sort du cadre, même en pleine animation ou sur une colonne étroite.
const b = principaux.value.has(kindDe.value.get(id) ?? '') ? (taille.w < 768 ? 64 : BORD_PRINCIPAL) : BORD_SECONDAIRE
const xBorne = taille.w > 2 * b ? Math.min(taille.w - b, Math.max(b, x)) : x
return {
x: Math.round(xBorne * 10) / 10,
y: Math.round(((p.y - camera.cy) * camera.s + z.y + z.h / 2) * 10) / 10,
}
}
// ── Ce qui se dessine ──
const estPrincipal = (kind: string) => principaux.value.has(kind)
const vu = (id: string) => !visibles.value || visibles.value.has(id)
/**
* Estompé : hors du voisinage immédiat de la sélection. En « tout », les sœurs s'éteignent
* franchement ; en graphe local à deux pas, le second cercle reste lisible, un ton en dessous.
*/
const eteint = (id: string): 'fort' | 'leger' | null => {
if (!voisinsSel.value) return props.retenus && !props.retenus.has(id) ? 'fort' : null
if (voisinsSel.value.has(id) || voisinsSurvol.value?.has(id)) return null
return pas.value === 'tout' ? 'fort' : 'leger'
}
const degre = computed(() => {
const d = new Map<string, number>()
for (const l of props.liens) for (const id of [l.de, l.vers]) d.set(id, (d.get(id) ?? 0) + 1)
return d
})
const tronquer = (t: string, max = 30) => (t.length > max ? `${t.slice(0, max - 1).trimEnd()}…` : t)
const principauxVus = computed(() => props.noeuds
.filter((x) => estPrincipal(x.kind) && vu(x.id))
.map((x) => {
const p = ecran(x.id)!
const n = degre.value.get(x.id) ?? 0
return {
id: x.id,
libelle: x.libelle,
x: p.x,
y: p.y,
r: 7 + Math.min(x.poids ?? n, 6),
sel: x.id === selVue.value,
survol: x.id === props.survol,
eteint: eteint(x.id),
aria: `${x.libelle}, ${props.libelleKind(x.kind)}, ${n} ${n > 1 ? 'liens' : 'lien'}`,
}
}))
const secondairesVus = computed(() => props.noeuds
.filter((x) => !estPrincipal(x.kind) && vu(x.id))
.map((x) => {
const p = ecran(x.id)!
const enVue = !!visibles.value || !!voisinsSel.value?.has(x.id) || !!voisinsSurvol.value?.has(x.id) || x.id === props.survol
return {
id: x.id,
libelle: x.libelle,
etiquette: enVue ? tronquer(x.libelle) : '',
x: p.x,
y: p.y,
sel: x.id === selVue.value,
survol: x.id === props.survol,
eteint: eteint(x.id),
}
}))
const liensVus = computed(() => props.liens
.filter((l) => vu(l.de) && vu(l.vers))
.map((l, i) => {
const a = ecran(l.de)
const b = ecran(l.vers)
if (!a || !b) return null
const sel = selVue.value
const sv = props.survol
const vif = (!!sel && (l.de === sel || l.vers === sel)) || (!!sv && (l.de === sv || l.vers === sv))
return {
cle: `${l.de}>${l.vers}>${l.type}>${i}`,
x1: a.x, y1: a.y, x2: b.x, y2: b.y,
provenance: l.provenance === 'curation' ? 'curation' : 'source',
vif,
eteint: !!sel && !vif && pas.value === 'tout',
}
})
.filter((l): l is NonNullable<typeof l> => !!l))
</script>
<style scoped>
.gc {
position: relative;
flex: 1;
min-height: 0;
min-width: 0;
overflow: hidden;
background: var(--bg-alt);
color: var(--text);
}
.gc__svg {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
font-family: inherit;
}
/* Liens : encre fine ; tirets = posé par la curation */
.gc__lien {
stroke: color-mix(in srgb, var(--text) 38%, transparent);
stroke-width: 1.1;
transition: stroke var(--dur-fast) var(--ease-out), stroke-width var(--dur-fast) var(--ease-out), opacity var(--dur-fast) var(--ease-out);
}
.gc__lien--curation { stroke-dasharray: 4 3; }
.gc__lien--vif { stroke: var(--text); stroke-width: 1.8; }
.gc__lien--eteint { opacity: 0.25; }
/* Sources : carrés gris, hors tabulation (au clavier, on y va par la liste des fiches) */
.gc__source { cursor: pointer; transition: opacity var(--dur-fast) var(--ease-out); }
.gc__carre {
fill: color-mix(in srgb, var(--text) 52%, var(--bg-alt));
stroke: var(--bg-alt);
stroke-width: 1.5;
}
.gc__source--survol .gc__carre { fill: var(--text); }
.gc__source--sel .gc__carre { fill: var(--accent); stroke: var(--text); }
.gc__source--eteint { opacity: 0.28; }
.gc__source--loin { opacity: 0.6; }
.gc__source-texte {
fill: color-mix(in srgb, var(--text) 80%, transparent);
font-size: var(--fs-xs);
font-weight: var(--fw-medium);
paint-order: stroke;
stroke: var(--bg-alt);
stroke-width: 3px;
stroke-linejoin: round;
pointer-events: none;
}
/* Concepts : boutons HTML posés sur le dessin */
.gc__noeuds { position: absolute; inset: 0; pointer-events: none; }
.gc__noeud {
position: absolute;
transform: translate(-50%, calc(-1 * var(--gc-r)));
display: flex;
flex-direction: column;
align-items: center;
gap: 3px;
max-width: 9.5rem;
padding: 0 2px 2px;
border: none;
border-radius: var(--radius-sm);
background: none;
color: var(--text);
font: inherit;
cursor: pointer;
pointer-events: auto;
transition: opacity var(--dur-fast) var(--ease-out);
}
.gc__rond {
width: calc(2 * var(--gc-r));
height: calc(2 * var(--gc-r));
border-radius: 50%;
background: var(--surface);
border: 2px solid var(--text);
transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out);
}
.gc__libelle {
font-size: var(--fs-xs);
font-weight: var(--fw-semibold);
line-height: 1.2;
text-align: center;
overflow-wrap: anywhere;
padding: 0 3px;
border-radius: var(--radius-sm);
background: color-mix(in srgb, var(--bg-alt) 82%, transparent);
}
.gc__noeud--survol .gc__rond { border-width: 3px; }
.gc__noeud--sel .gc__rond { background: var(--accent); border-color: var(--text); }
.gc__noeud--sel .gc__libelle { background: var(--accent); color: var(--on-accent); }
.gc__noeud--eteint { opacity: 0.32; }
.gc__noeud--loin { opacity: 0.62; }
/* L'opacité éteindrait l'anneau de focus (DESIGN-SYSTEM §Texte atténué) : un nœud focalisé revient à l'opacité pleine. */
.gc__noeud--eteint:focus-visible, .gc__noeud--loin:focus-visible { opacity: 1; }
.gc__noeud:focus { outline: none; }
.gc__noeud:focus-visible .gc__rond { outline: 3px solid var(--focus-ring); outline-offset: 2px; }
.gc__noeud:focus-visible .gc__libelle { outline: 2px solid var(--focus-ring); }
/* Barre du graphe local (en haut à gauche, hors de la poignée de la vue fiches) */
.gc__barre {
position: absolute;
top: var(--space-xs);
left: 32px;
display: inline-flex;
padding: 2px;
border-radius: var(--radius-md);
background: var(--surface);
box-shadow: var(--shadow-sm);
}
.gc__pas {
min-height: 44px;
min-width: 44px;
padding: 0 var(--space-sm);
border: none;
border-radius: calc(var(--radius-md) - 2px);
background: none;
color: var(--text);
font: inherit;
font-size: var(--fs-sm);
font-weight: var(--fw-medium);
cursor: pointer;
}
.gc__pas[aria-pressed='true'] { background: var(--text); color: var(--surface); }
.gc__pas:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
/* Légende : une ligne en bas du graphe (deux au téléphone), hors de la poignée et de la barre des pas */
.gc__legende {
position: absolute;
left: var(--space-lg);
bottom: var(--space-xs);
max-width: calc(100% - var(--space-lg) - var(--space-sm));
margin: 0;
padding: var(--space-2xs) var(--space-xs);
list-style: none;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--space-2xs) var(--space-sm);
border-radius: var(--radius-sm);
background: color-mix(in srgb, var(--bg-alt) 88%, transparent);
font-size: var(--fs-xs);
line-height: 1.3;
color: color-mix(in srgb, var(--text) 78%, transparent);
pointer-events: none;
}
.gc__legende li { display: inline-flex; align-items: center; gap: var(--space-2xs); }
.gc__legende-rond { width: 10px; height: 10px; border-radius: var(--radius-full); border: 2px solid var(--text); background: var(--surface); }
.gc__legende-carre { width: 9px; height: 9px; border-radius: 2px; background: color-mix(in srgb, var(--text) 52%, var(--bg-alt)); }
.gc__legende-trait { width: 18px; border-top: 1.5px solid currentColor; }
.gc__legende-trait--curation { border-top-style: dashed; }
@media (max-width: 767.98px) {
.gc__noeud { max-width: 8rem; }
}
/* Mouvement réduit : état final, sans transition (la caméra saute aussi, côté script) */
.gc--fige .gc__lien, .gc--fige .gc__source, .gc--fige .gc__noeud, .gc--fige .gc__rond { transition: none; }
@media (prefers-reduced-motion: reduce) {
.gc__lien, .gc__source, .gc__noeud, .gc__rond { transition: none; }
}
</style>