refactor(connaissances): dette de la page, domaine sorti dans utils/connaissances (B19b M4)
- graphe.ts : disposer, disposerEnCache, voisinage (mêmes signatures) et boite, sortis de GrapheConnaissances.vue ; la boîte du monde se calcule une fois par layout. - lire.ts : lecture (index du jeu mémorisé), sourcesDe, porteursDe, relationsDe, relationsParLibelle, dettesDe, vacancesDe, LIBELLES_RELATION, estRendu, sortis de la page. - vocabulaire.ts : natures, préfixes, échelles, couches, états, seuil de copie et libellés, écrits une fois et comparés au schéma par le test ; valider.ts les importe et les réexporte. - Types : ObjetConnaissance / JeuConnaissances (valider.ts) seuls ; ObjetCouche / JeuCouche retirés de l'adaptateur ; la page lit un computed typé objetOuvert. - toMiniConnaissance ne reconstruit plus la carte des objets à chaque appel ; le degré des nœuds n'est plus calculé deux fois (le graphe le calcule seul). - Tokens existants à la place des valeurs en dur (--fs-*, --dur-fast, --radius-*), .cn-sr remplacé par sr-only (Tailwind, servi sur la page). - test-connaissances : 92 → 130 cas. HTML de la page identique avant / après hors classes sr-only et CSS (positions du graphe comprises). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PWGVn1RZVsSd6YYS1o7CHk
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
be8206d25b
commit
448752f994
@@ -20,7 +20,8 @@
|
||||
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.
|
||||
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 }">
|
||||
@@ -89,145 +90,9 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
/** Un nœud : `kind` dit sa forme (principal si dans `kindsPrincipaux`, sinon secondaire). */
|
||||
export interface NoeudGraphe { id: string; libelle: string; kind: string; poids?: number }
|
||||
export interface LienGraphe { de: string; vers: string; type: string; provenance: string }
|
||||
export type Pas = 1 | 2 | 'tout'
|
||||
|
||||
type Pt = { x: number; y: number }
|
||||
|
||||
/** PRNG à graine fixe (mulberry32) : même graphe, même dessin, au serveur comme au navigateur. */
|
||||
function alea(graine: number) {
|
||||
let a = graine >>> 0
|
||||
return () => {
|
||||
a = (a + 0x6d2b79f5) >>> 0
|
||||
let t = a
|
||||
t = Math.imul(t ^ (t >>> 15), t | 1)
|
||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Force simple (Fruchterman-Reingold), graine fixe, nombre de pas fixe : positions dans un
|
||||
* repère sans unité, centré, d'étendue 1. Fonction pure de (nœuds, liens, principaux).
|
||||
*/
|
||||
export function disposer(noeuds: NoeudGraphe[], liens: LienGraphe[], principaux: Set<string>): Map<string, Pt> {
|
||||
const n = noeuds.length
|
||||
const pos = new Map<string, Pt>()
|
||||
if (!n) return pos
|
||||
const rnd = alea(0x9e3779b9)
|
||||
const index = new Map(noeuds.map((x, i) => [x.id, i]))
|
||||
const aretes = liens
|
||||
.map((l) => [index.get(l.de), index.get(l.vers)] as const)
|
||||
.filter((a): a is readonly [number, number] => a[0] != null && a[1] != null && a[0] !== a[1])
|
||||
const voisins: number[][] = noeuds.map(() => [])
|
||||
for (const [a, b] of aretes) { voisins[a].push(b); voisins[b].push(a) }
|
||||
|
||||
// Départ : les principaux en cercle, chaque secondaire au barycentre de ses voisins principaux.
|
||||
const prin = noeuds.map((x, i) => (principaux.has(x.kind) ? i : -1)).filter((i) => i >= 0)
|
||||
const X = new Float64Array(n)
|
||||
const Y = new Float64Array(n)
|
||||
prin.forEach((i, k) => {
|
||||
const t = (2 * Math.PI * k) / Math.max(prin.length, 1)
|
||||
X[i] = 0.4 * Math.cos(t)
|
||||
Y[i] = 0.4 * Math.sin(t)
|
||||
})
|
||||
noeuds.forEach((x, i) => {
|
||||
if (principaux.has(x.kind)) return
|
||||
const v = voisins[i].filter((j) => principaux.has(noeuds[j].kind))
|
||||
if (v.length) {
|
||||
X[i] = (v.reduce((s, j) => s + X[j], 0) / v.length) * 1.35 + (rnd() - 0.5) * 0.08
|
||||
Y[i] = (v.reduce((s, j) => s + Y[j], 0) / v.length) * 1.35 + (rnd() - 0.5) * 0.08
|
||||
} else {
|
||||
X[i] = (rnd() - 0.5) * 0.9
|
||||
Y[i] = (rnd() - 0.5) * 0.9
|
||||
}
|
||||
})
|
||||
|
||||
const k = 0.9 * Math.sqrt(1 / n)
|
||||
const ITER = 350
|
||||
const dx = new Float64Array(n)
|
||||
const dy = new Float64Array(n)
|
||||
for (let it = 0; it < ITER; it++) {
|
||||
const temp = 0.08 * (1 - it / ITER) + 0.002
|
||||
dx.fill(0)
|
||||
dy.fill(0)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
let ex = X[i] - X[j]
|
||||
let ey = Y[i] - Y[j]
|
||||
let d = Math.hypot(ex, ey)
|
||||
if (d < 1e-4) { ex = (rnd() - 0.5) * 1e-3; ey = (rnd() - 0.5) * 1e-3; d = Math.hypot(ex, ey) }
|
||||
const f = (k * k) / d
|
||||
dx[i] += (ex / d) * f; dy[i] += (ey / d) * f
|
||||
dx[j] -= (ex / d) * f; dy[j] -= (ey / d) * f
|
||||
}
|
||||
}
|
||||
for (const [a, b] of aretes) {
|
||||
const ex = X[a] - X[b]
|
||||
const ey = Y[a] - Y[b]
|
||||
const d = Math.max(Math.hypot(ex, ey), 1e-4)
|
||||
const f = (d * d) / k
|
||||
dx[a] -= (ex / d) * f; dy[a] -= (ey / d) * f
|
||||
dx[b] += (ex / d) * f; dy[b] += (ey / d) * f
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
dx[i] -= X[i] * 0.9 * k * 4
|
||||
dy[i] -= Y[i] * 0.9 * k * 4
|
||||
const d = Math.hypot(dx[i], dy[i])
|
||||
if (d > 0) {
|
||||
const m = Math.min(d, temp)
|
||||
X[i] += (dx[i] / d) * m
|
||||
Y[i] += (dy[i] / d) * m
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Centré, étendue 1 (le plus grand côté de la boîte englobante)
|
||||
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity
|
||||
for (let i = 0; i < n; i++) { x0 = Math.min(x0, X[i]); x1 = Math.max(x1, X[i]); y0 = Math.min(y0, Y[i]); y1 = Math.max(y1, Y[i]) }
|
||||
const e = Math.max(x1 - x0, y1 - y0) || 1
|
||||
noeuds.forEach((x, i) => pos.set(x.id, { x: (X[i] - (x0 + x1) / 2) / e, y: (Y[i] - (y0 + y1) / 2) / e }))
|
||||
return pos
|
||||
}
|
||||
|
||||
/** Cache par signature du graphe : au serveur, une fois par processus, pas une fois par requête. */
|
||||
const CACHE = new Map<string, Map<string, Pt>>()
|
||||
export function disposerEnCache(noeuds: NoeudGraphe[], liens: LienGraphe[], principaux: Set<string>): Map<string, Pt> {
|
||||
const sig = JSON.stringify([noeuds.map((x) => [x.id, x.kind]), liens.map((l) => [l.de, l.vers]), [...principaux]])
|
||||
let p = CACHE.get(sig)
|
||||
if (!p) {
|
||||
p = disposer(noeuds, liens, principaux)
|
||||
if (CACHE.size > 24) CACHE.delete(CACHE.keys().next().value as string)
|
||||
CACHE.set(sig, p)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
/** Identifiants à `pas` liens au plus de `id` (le nœud compris) ; `tout` = tous. */
|
||||
export function voisinage(liens: LienGraphe[], id: string, pas: Pas): Set<string> | null {
|
||||
if (pas === 'tout') return null
|
||||
const adj = new Map<string, string[]>()
|
||||
for (const l of liens) {
|
||||
if (!adj.has(l.de)) adj.set(l.de, [])
|
||||
if (!adj.has(l.vers)) adj.set(l.vers, [])
|
||||
adj.get(l.de)!.push(l.vers)
|
||||
adj.get(l.vers)!.push(l.de)
|
||||
}
|
||||
const vus = new Set([id])
|
||||
let front = [id]
|
||||
for (let p = 0; p < pas; p++) {
|
||||
const suite: string[] = []
|
||||
for (const x of front) for (const y of adj.get(x) ?? []) if (!vus.has(y)) { vus.add(y); suite.push(y) }
|
||||
front = suite
|
||||
}
|
||||
return vus
|
||||
}
|
||||
</script>
|
||||
|
||||
<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[]
|
||||
@@ -311,13 +176,13 @@ 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) }
|
||||
})
|
||||
/** Portrait d'écran et dessin paysage (ou l'inverse) : on tourne le dessin d'un quart. Projection, rien en base. */
|
||||
const tourne = computed(() => {
|
||||
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity
|
||||
for (const p of monde.value.values()) { x0 = Math.min(x0, p.x); x1 = Math.max(x1, p.x); y0 = Math.min(y0, p.y); y1 = Math.max(y1, p.y) }
|
||||
const paysage = x1 - x0 >= y1 - y0
|
||||
return paysage !== zone.value.w >= zone.value.h
|
||||
/** 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
|
||||
@@ -326,13 +191,11 @@ const pt = (id: string): Pt | null => {
|
||||
|
||||
const cible = computed<Camera>(() => {
|
||||
const vis = visibles.value
|
||||
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity
|
||||
for (const x of props.noeuds) {
|
||||
if (vis && !vis.has(x.id)) continue
|
||||
const p = pt(x.id)
|
||||
if (!p) continue
|
||||
x0 = Math.min(x0, p.x); x1 = Math.max(x1, p.x); y0 = Math.min(y0, p.y); y1 = Math.max(y1, p.y)
|
||||
}
|
||||
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)
|
||||
@@ -477,14 +340,14 @@ const liensVus = computed(() => props.liens
|
||||
.gc__lien {
|
||||
stroke: color-mix(in srgb, var(--text) 38%, transparent);
|
||||
stroke-width: 1.1;
|
||||
transition: stroke 160ms ease-out, stroke-width 160ms ease-out, opacity 160ms ease-out;
|
||||
transition: stroke var(--dur-fast) ease-out, stroke-width var(--dur-fast) ease-out, opacity var(--dur-fast) 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 160ms ease-out; }
|
||||
.gc__source { cursor: pointer; transition: opacity var(--dur-fast) ease-out; }
|
||||
.gc__carre {
|
||||
fill: color-mix(in srgb, var(--text) 52%, var(--bg-alt));
|
||||
stroke: var(--bg-alt);
|
||||
@@ -496,8 +359,8 @@ const liensVus = computed(() => props.liens
|
||||
.gc__source--loin { opacity: 0.6; }
|
||||
.gc__source-texte {
|
||||
fill: color-mix(in srgb, var(--text) 80%, transparent);
|
||||
font-size: 11px;
|
||||
font-weight: 500;
|
||||
font-size: var(--fs-xs);
|
||||
font-weight: var(--fw-medium);
|
||||
paint-order: stroke;
|
||||
stroke: var(--bg-alt);
|
||||
stroke-width: 3px;
|
||||
@@ -523,7 +386,7 @@ const liensVus = computed(() => props.liens
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
pointer-events: auto;
|
||||
transition: opacity 160ms ease-out;
|
||||
transition: opacity var(--dur-fast) ease-out;
|
||||
}
|
||||
.gc__rond {
|
||||
width: calc(2 * var(--gc-r));
|
||||
@@ -531,16 +394,16 @@ const liensVus = computed(() => props.liens
|
||||
border-radius: 50%;
|
||||
background: var(--surface);
|
||||
border: 2px solid var(--text);
|
||||
transition: background 160ms ease-out, border-color 160ms ease-out;
|
||||
transition: background var(--dur-fast) ease-out, border-color var(--dur-fast) ease-out;
|
||||
}
|
||||
.gc__libelle {
|
||||
font-size: 12.5px;
|
||||
font-weight: 600;
|
||||
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: 4px;
|
||||
border-radius: var(--radius-sm);
|
||||
background: color-mix(in srgb, var(--bg-alt) 82%, transparent);
|
||||
}
|
||||
.gc__noeud--survol .gc__rond { border-width: 3px; }
|
||||
@@ -598,7 +461,6 @@ const liensVus = computed(() => props.liens
|
||||
@media (max-width: 639.98px) { .gc__legende { display: none; } }
|
||||
@media (max-width: 767.98px) {
|
||||
.gc__noeud { max-width: 8rem; }
|
||||
.gc__libelle { font-size: 12px; }
|
||||
}
|
||||
|
||||
/* Mouvement réduit : état final, sans transition (la caméra saute aussi, côté script) */
|
||||
|
||||
Reference in New Issue
Block a user