synchro du labo (0bf0da65) : app.blade.php, README.md

This commit is contained in:
cloudion
2026-11-03 22:38:04 +00:00
parent 3955664c6d
commit b1ecab18fe
2 changed files with 869 additions and 2 deletions

View File

@@ -1,3 +1,21 @@
# ploxion-glyphes-editor # 🔺 Glyphes
PLOXION · Composeur de glyphes & xerkions : pose/enlève des koins, change le type (koin/kion/wormion/portion), emboîte, et compose un xerkion — code (notation ⟷ spec ⟷ svg) synchronisé. · fournit: glyph-codec, compose-xerkion, glyphes Composeur de glyphes & xerkions : pose/enlève des koins, change le type (koin/kion/wormion/portion), emboîte, et compose un xerkion — code (notation ⟷ spec ⟷ svg) synchronisé.
| | |
| --- | --- |
| id | `glyphes-editor` |
| fournit | `glyph-codec` `compose-xerkion` `glyphes` |
| bions qu'il nomme | `jOSApps` `jOSBoxion` `jOSGlyphCodec` `jOSKion` |
| fichiers | `app.blade.php` |
| dans le labo | `resources/views/ploxions/glyphes-editor/` (dépôt `my_website2`) |
## Contribuer
- **Dans le xer** : https://labo.j0bot.ch/px/atelier?ploxion=glyphes-editor — prends-le, modifie-le, vois-le tourner, « ⧗ envoyer le tsoin ».
- **Dans VS Code** : `xb prendre glyphes-editor` (ou l'extension xerboxion › ⇩ Prendre), puis ⧗ Envoyer.
- Le chemin complet : https://labo.j0bot.ch/px/contribuer
Ce dépôt est un **miroir** du labo (synchronisé par `bin/ploxions-forge.mjs`, depuis le commit `0bf0da65`) : une modif se propose par un tsoin ou une branche du labo, pas directement ici.
© 2026 chill-tekk · ne pas nuire

849
app.blade.php Normal file
View File

@@ -0,0 +1,849 @@
{{-- PL0XI0N glyphes-editor — ATELIER LIBRE + collapse-LECTURE (non destructif).
DEUX COUCHES (feedback RS-7 : « pas assez de liberté créative ») :
(1) COUCHE CRÉATIVE = son art. Chaque forme = {g,x,y,s,col,rot,style,sw,z}. Couleurs néon,
rotation, taille individuelle, contour/plein, calques, liens entre formes, multi-sélection,
dupliquer, copier/coller, galerie de compos nommées. TOUJOURS préservée.
(2) COUCHE STRUCTURE = {g,x,y,s} que le codec window.jOSGlyphCodec.collapse LIT -> un kion.
Le collapse IGNORE couleur/rotation/style/liens (déco) et ne TOUCHE JAMAIS le canvas :
il affiche juste la lecture dans « ton code » + un aperçu du kion en overlay.
On garde le vrac coloré ; collapse = la loupe, pas l'écrasement.
ES5 strict, IIFE, pointer events, window.jOSApps. Aucun double-accolade ouvrant, aucun sigil
directive en tête de token, aucun tag-script littéral (pièges Blade évités). --}}
<style>
.pge{display:flex;flex-direction:column;height:100%;min-height:0;background:var(--xr-bg,#070b14);color:var(--xr-fg,#dfe9ff);
font-family:var(--xr-font-ui,ui-sans-serif,system-ui,sans-serif);font-size:.85rem;box-sizing:border-box}
.pge *{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
.pge__intro{flex:0 0 auto;display:flex;align-items:center;gap:8px;padding:7px 11px;border-bottom:1px solid var(--xr-line,#1a2b48);
font-size:.78rem;color:var(--xr-muted,#8aa0c0);line-height:1.35}
.pge__intro b{color:var(--xr-accent,#7df0ff);font-weight:800}
.pge__intro .sp{margin-left:auto}
.pge__bar{flex:0 0 auto;display:flex;align-items:center;gap:6px;flex-wrap:wrap;padding:7px 10px;border-bottom:1px solid var(--xr-line,#1a2b48)}
.pge__bar .grp{display:flex;align-items:center;gap:4px}
.pge__bar .sep{width:1px;align-self:stretch;background:var(--xr-line,#22324f);margin:2px 3px}
.pge__bar .sp{margin-left:auto}
.pge button{font:inherit;cursor:pointer;border-radius:9px;border:1px solid var(--xr-line,#22324f);background:rgba(255,255,255,.04);
color:var(--xr-fg,#dfe9ff);padding:6px 9px;line-height:1}
.pge button:hover{border-color:var(--xr-accent,#7df0ff)}
.pge button.on{background:var(--xr-accent,#7df0ff);color:#04121a;border-color:var(--xr-accent,#7df0ff);font-weight:800}
.pge button:disabled{opacity:.35;cursor:default}
.pge .glyph{min-width:38px;font-size:1rem;padding:6px 8px}
.pge .tbtn{font-size:.78rem}
.pge .primary{background:var(--xr-accent,#7df0ff);color:#04121a;font-weight:800;border-color:var(--xr-accent,#7df0ff)}
.pge .danger{border-color:rgba(255,90,90,.4);color:#ff9a9a}
.pge__lc{display:inline-flex;align-items:center;justify-content:center;width:26px;height:26px;border-radius:8px;overflow:hidden;
border:1px solid var(--xr-line,#22324f);cursor:pointer;padding:0}
.pge__lc input{width:40px;height:40px;border:0;padding:0;background:none;cursor:pointer;transform:translate(-6px,-6px)}
.pge__size{display:flex;align-items:center;gap:6px;font-size:.72rem;color:var(--xr-muted,#8aa0c0)}
.pge__size input[type=range]{width:88px}
.pge__main{flex:1;min-height:0;display:flex;gap:10px;padding:10px;overflow:hidden}
@media (max-width:760px){ .pge__main{flex-direction:column;overflow:auto} }
.pge__canvas{position:relative;flex:1 1 auto;min-width:0;min-height:220px;border:1px solid var(--xr-line,#1a2b48);border-radius:14px;
overflow:hidden;background:
radial-gradient(120% 120% at 50% 8%,#0c1526,#05080f),
radial-gradient(rgba(125,240,255,.10) 1px,transparent 1px);
background-size:cover,20px 20px}
.pge__canvas svg.stage{position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:none;cursor:crosshair}
.pge__hint{position:absolute;left:10px;bottom:8px;font-size:.66rem;color:var(--xr-muted,#7f93c8);pointer-events:none;
background:rgba(5,8,15,.55);padding:3px 7px;border-radius:7px;max-width:70%}
.pge__badge{position:absolute;right:10px;top:8px;font-size:.68rem;pointer-events:none;background:rgba(5,8,15,.6);
border:1px solid var(--xr-line,#22324f);border-radius:999px;padding:3px 9px;color:var(--xr-accent,#7df0ff)}
.pge__side{flex:0 0 300px;max-width:340px;display:flex;flex-direction:column;gap:12px;min-height:0;
border:1px solid var(--xr-line,#1a2b48);border-radius:14px;padding:12px;background:rgba(255,255,255,.02);overflow:auto}
@media (max-width:760px){ .pge__side{flex:0 0 auto;max-width:none} }
.pge__side h4{margin:0 0 6px;font-size:.68rem;text-transform:uppercase;letter-spacing:.09em;color:var(--xr-muted,#7f93c8)}
.pge__insp{display:none;border:1px solid var(--xr-line,#22324f);border-radius:12px;padding:10px;background:rgba(125,240,255,.03)}
.pge__pal{display:flex;flex-wrap:wrap;gap:5px;margin-bottom:9px}
.pge__pal .sw{width:22px;height:22px;min-width:0;padding:0;border-radius:6px;border:2px solid transparent}
.pge__pal .sw.on{border-color:#fff;box-shadow:0 0 0 1px #04121a}
.pge__pal .sw--cust{position:relative;overflow:hidden;border:1px dashed var(--xr-line,#4a5f86)}
.pge__pal .sw--cust input{position:absolute;inset:-6px;width:40px;height:40px;border:0;padding:0;cursor:pointer;opacity:0}
.pge__row{display:flex;align-items:center;gap:8px;margin-bottom:7px;font-size:.74rem;color:var(--xr-muted,#9db2d4)}
.pge__row>span:first-child{flex:0 0 54px}
.pge__row input[type=range]{flex:1 1 auto;min-width:0}
.pge__row .v{flex:0 0 34px;text-align:right;color:var(--xr-accent,#7df0ff);font-variant-numeric:tabular-nums}
.pge__row .seg{display:flex;gap:4px;flex:1 1 auto}
.pge__row .seg button{flex:1 1 auto;padding:5px 6px;font-size:.72rem}
.pge__acts{display:flex;flex-wrap:wrap;gap:5px;margin-top:6px}
.pge__acts button{flex:1 1 auto}
.pge__codebox{border:1px solid var(--xr-line,#22324f);border-radius:12px;padding:10px;background:rgba(255,255,255,.02)}
.pge__code{font-size:1rem;line-height:1.4;font-weight:700;color:var(--xr-fg,#eaf2ff);margin-bottom:4px}
.pge__passes{font-size:.72rem;color:var(--xr-accent,#7df0ff);margin-bottom:10px}
.pge__mini{display:flex;justify-content:center;padding:8px;border:1px solid var(--xr-line,#22324f);border-radius:11px;
background:rgba(255,255,255,.02);margin-bottom:10px;min-height:90px;align-items:center}
.pge__mini svg{width:130px;height:130px;max-width:100%}
.pge__notation{width:100%;font-family:ui-monospace,'Share Tech Mono',monospace;font-size:.78rem;background:var(--xr-bg,#0a1120);
color:var(--xr-accent,#7df0ff);border:1px solid var(--xr-line,#22324f);border-radius:8px;padding:7px 9px;word-break:break-all;
line-height:1.4;min-height:38px;margin-bottom:8px}
.pge__note{font-size:.7rem;color:var(--xr-muted,#7f93c8);line-height:1.45;margin-top:8px}
.pge__empty{color:var(--xr-muted,#7f93c8);font-size:.8rem;line-height:1.5}
.pge__ov,.pge__help{position:absolute;inset:0;z-index:6;display:none;flex-direction:column;gap:10px;padding:16px 18px;overflow:auto;
background:rgba(4,7,14,.95);font-size:.82rem;line-height:1.5}
.pge__ov.show,.pge__help.show{display:flex}
.pge__ovhead{display:flex;align-items:center;gap:10px}
.pge__ovhead b{color:var(--xr-accent,#7df0ff);font-size:1rem}
.pge__ovhead .sp{margin-left:auto}
.pge__ovbody{display:flex;flex-direction:column;align-items:center;gap:10px}
.pge__ovbody svg{max-width:100%;height:auto}
.pge__ovmeta{text-align:center;color:var(--xr-fg,#dfe9ff)}
.pge__ovmeta .mono{font-family:ui-monospace,monospace;color:var(--xr-accent,#7df0ff)}
.pge__ovmeta .dim{color:var(--xr-muted,#7f93c8);font-size:.74rem}
.pge__gnew{display:flex;gap:6px;flex-wrap:wrap}
.pge__gnew input{flex:1 1 140px;min-width:0;background:var(--xr-bg,#0a1120);color:var(--xr-fg,#dfe9ff);
border:1px solid var(--xr-line,#22324f);border-radius:8px;padding:7px 9px;font:inherit}
.pge__glist{display:flex;flex-direction:column;gap:6px}
.pge__gitem{display:flex;align-items:center;gap:8px;border:1px solid var(--xr-line,#22324f);border-radius:9px;padding:7px 9px;
background:rgba(255,255,255,.02)}
.pge__gitem .nm{font-weight:700;color:var(--xr-fg,#eaf2ff);word-break:break-word}
.pge__gitem .sp{margin-left:auto}
.pge__help h3{margin:0;color:var(--xr-accent,#7df0ff);font-size:1rem}
.pge__help .forms{display:flex;flex-direction:column;gap:5px}
.pge__help .forms div{display:flex;gap:9px;align-items:baseline}
.pge__help .forms .s{width:34px;font-size:1.1rem;color:var(--xr-accent,#7df0ff);text-align:center}
.pge__tmotifs{display:flex;flex-direction:column;gap:6px;width:100%}
.pge__mbtn{width:100%;text-align:left;padding:9px 11px;font-size:.82rem}
.pge__mbtn.on{background:var(--xr-accent,#7df0ff);color:#04121a;border-color:var(--xr-accent,#7df0ff);font-weight:800}
.pge__tstep{display:flex;align-items:center;gap:10px;justify-content:center;margin-top:6px;font-size:.8rem;color:var(--xr-muted,#9db2d4)}
.pge__tstep b{min-width:34px;text-align:center;color:var(--xr-accent,#7df0ff);font-size:1.05rem}
.pge__tstep.dim{color:var(--xr-muted,#7f93c8);font-style:italic}
.pge__tstep button{padding:5px 12px;font-size:1rem}
.pge .close{padding:5px 10px}
</style>
<script>
(function () {
'use strict';
var ID = 'glyphes-editor';
// palette de glyphes : chaque outil pose une forme {g,x,y,s,...}
var TOOLS = [
{ g: 'koin', s: '△', l: 'koin — la brique' },
{ g: 'kion', s: '▢', l: 'kion — le ploxion' },
{ g: 'wormion', s: '○', l: 'wormion — le portail' },
{ g: 'portion', s: '▣', l: 'portion — le lieu' },
{ g: 'losange', s: '◇', l: 'losange — le lien' },
{ g: 'xer', s: '⬢', l: 'xer — octogone' }
];
// couleurs néon xerboxion
var PALETTE = ['#7df0ff', '#5affc4', '#ffd23f', '#ff8a5a', '#ff5a7a', '#c07dff', '#7d9bff', '#cfe0f5'];
var COL = '#7df0ff', COL2 = '#cfe0f5', GOLD = '#ffd23f', SW = 2.2;
function clone(a) { return JSON.parse(JSON.stringify(a)); }
function fmt(v) { return (Math.round(v * 100) / 100).toString(); }
function clamp(v, a, b) { return v < a ? a : v > b ? b : v; }
function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]; }); }
function slug(s) { return String(s).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40) || 'compo'; }
function glyphLabel(g) { return g === 'koin' ? 'koin △' : g === 'kion' ? 'kion ▢' : g === 'wormion' ? 'wormion ○' : g === 'portion' ? 'portion ▣' : g === 'losange' ? 'losange ◇' : g === 'xer' ? 'xer ⬢' : g; }
// ---------- primitives svg (couche créative : couleur/style/trait respectés) ----------
function paint(col, style, sw) {
if (style === 'fill') return 'fill="' + col + '" fill-opacity="0.85" stroke="' + col + '" stroke-width="1.2" stroke-linejoin="round"';
return 'fill="none" stroke="' + col + '" stroke-width="' + fmt(sw) + '" stroke-linejoin="round" stroke-linecap="round"';
}
function polyA(pts, a) { var s = pts.map(function (p) { return fmt(p[0]) + ',' + fmt(p[1]); }).join(' '); return '<polygon points="' + s + '" ' + a + '/>'; }
function rectA(x, y, w, a) { return '<rect x="' + fmt(x) + '" y="' + fmt(y) + '" width="' + fmt(w) + '" height="' + fmt(w) + '" ' + a + '/>'; }
function circA(cx, cy, r, a) { return '<circle cx="' + fmt(cx) + '" cy="' + fmt(cy) + '" r="' + fmt(r) + '" ' + a + '/>'; }
function ringDash(x, y, w) { return '<rect x="' + fmt(x) + '" y="' + fmt(y) + '" width="' + fmt(w) + '" height="' + fmt(w) + '" fill="none" stroke="' + GOLD + '" stroke-width="1.6" stroke-dasharray="4 3" rx="3"/>'; }
function oct(cx, cy, r) { var p = [], a; for (a = 0; a < 8; a++) { var g = (-90 + a * 45) * Math.PI / 180; p.push([cx + r * Math.cos(g), cy + r * Math.sin(g)]); } return p; }
function glyphSvg(sh) {
var x = sh.x, y = sh.y, s = sh.s, h = s / 2, g = sh.g;
var col = sh.col || COL, style = sh.style === 'fill' ? 'fill' : 'stroke', sw = sh.sw || SW;
var a = paint(col, style, sw), body = '';
if (sh.poly && sh.poly.length >= 3) { var pp = [], pi; for (pi = 0; pi < sh.poly.length; pi++) pp.push([x + sh.poly[pi][0], y + sh.poly[pi][1]]); body = polyA(pp, a); }
else if (g === 'koin') body = polyA([[x, y - h], [x - h * 0.92, y + h * 0.72], [x + h * 0.92, y + h * 0.72]], a);
else if (g === 'wormion') body = circA(x, y, h, a);
else if (g === 'portion') body = rectA(x - h, y - h, s, a) + rectA(x - h * 0.5, y - h * 0.5, s * 0.5, paint(col, 'stroke', sw));
else if (g === 'losange') body = polyA([[x, y - h], [x + h, y], [x, y + h], [x - h, y]], a);
else if (g === 'xer') body = polyA(oct(x, y, h), a) + polyA(oct(x, y, h * 0.55), paint(col, 'stroke', sw));
else body = rectA(x - h, y - h, s, a); // kion
var rot = sh.rot || 0;
return '<g transform="rotate(' + fmt(rot) + ' ' + fmt(x) + ' ' + fmt(y) + ')">' + body + '</g>';
}
function ringSvg(sh) { var h = sh.s / 2; return ringDash(sh.x - h * 1.16, sh.y - h * 1.16, sh.s * 1.32); }
function mount(container, ctx) {
var C = window.jOSGlyphCodec;
if (!C || !C.collapse) {
container.innerHTML = '<div style="padding:26px;color:#ff9a9a;font-family:system-ui;line-height:1.5">Le codec des glyphes (jOSGlyphCodec.collapse) n\'est pas charge.<br><span style="opacity:.7">La couche créative marche quand même, mais la lecture kion a besoin du codec.</span></div>';
return { unmount: function () { container.innerHTML = ''; } };
}
var NS = 'glyphes', KEY = 'puzzle', LS = 'xb:glyphes:puzzle';
var store = (window.jOSb && window.jOSb.store) ? window.jOSb.store(LS) : { get: function (d) { return d; }, set: function () {} };
function toast(msg, bad) { if (window.jOSb && window.jOSb.toast && root) window.jOSb.toast(root, msg, { bad: !!bad }); else if (window.jOSBoxion && window.jOSBoxion.toast) window.jOSBoxion.toast(msg); }
function copy(v) { if (window.jOSb && window.jOSb.copy) window.jOSb.copy(v); else if (navigator.clipboard) try { navigator.clipboard.writeText(v); } catch (e) {} }
// ---------- état ----------
var nextId = 0, nextZ = 0;
function newId() { nextId += 1; return nextId; }
function newZ() { nextZ += 1; return nextZ; }
function normShape(sh) {
sh = sh || {};
// poly = sommets RELATIFS au centre (x,y), pour les sous-formes de tiling (pavage exact).
var poly = null;
if (sh.poly && sh.poly.length) { poly = []; var pi; for (pi = 0; pi < sh.poly.length; pi++) { var pp = sh.poly[pi]; if (pp && pp.length >= 2) poly.push([+pp[0] || 0, +pp[1] || 0]); } if (poly.length < 3) poly = null; }
return {
id: (sh.id != null) ? sh.id : newId(),
g: sh.g || 'koin',
x: +sh.x || 0, y: +sh.y || 0,
s: Math.abs(+sh.s) || 60,
col: sh.col || COL,
rot: (+sh.rot) || 0,
style: (sh.style === 'fill') ? 'fill' : 'stroke',
sw: (+sh.sw) || SW,
poly: poly,
z: (sh.z != null) ? sh.z : newZ()
};
}
function defaultShapes() {
return [ normShape({ g: 'kion', x: 0, y: 2, s: 96 }), normShape({ g: 'koin', x: -24, y: 12, s: 26 }), normShape({ g: 'koin', x: 0, y: 12, s: 26 }), normShape({ g: 'koin', x: 24, y: 12, s: 26 }) ];
}
var shapes = [], connections = [], selIds = [], clipboard = [];
var tool = 'koin', size = 60, paintCol = COL, linkCol = GOLD;
var view = { tx: 0, ty: 0, scale: 1 };
var lastKion = null;
var undoStack = [], persistT = 0;
var marquee = null, linkPreview = null, gesture = null;
function byId(id) { var i; for (i = 0; i < shapes.length; i++) if (shapes[i].id === id) return shapes[i]; return null; }
function isSel(id) { var i; for (i = 0; i < selIds.length; i++) if (selIds[i] === id) return true; return false; }
function selShapes() { var o = [], i, s; for (i = 0; i < selIds.length; i++) { s = byId(selIds[i]); if (s) o.push(s); } return o; }
function maxZ() { var m = 0, i; for (i = 0; i < shapes.length; i++) if (shapes[i].z > m) m = shapes[i].z; return m; }
function minZ() { var m = 0, i; for (i = 0; i < shapes.length; i++) if (shapes[i].z < m) m = shapes[i].z; return m; }
function reindex() { var mi = 0, mz = 0, i; for (i = 0; i < shapes.length; i++) { if (shapes[i].id > mi) mi = shapes[i].id; if (shapes[i].z > mz) mz = shapes[i].z; } if (mi > nextId) nextId = mi; if (mz > nextZ) nextZ = mz; }
function ingest(d) {
var r = [], i;
if (d && d.shapes && d.shapes.length) { for (i = 0; i < d.shapes.length; i++) r.push(normShape(d.shapes[i])); shapes = r; }
else shapes = defaultShapes();
connections = [];
if (d && d.connections && d.connections.length) { for (i = 0; i < d.connections.length; i++) { var c = d.connections[i]; if (c && c.a != null && c.b != null) connections.push({ a: c.a, b: c.b, col: c.col || GOLD }); } }
reindex(); selIds = [];
}
container.innerHTML =
'<div class="pge">' +
'<div class="pge__intro">' +
'<span>Crée <b>librement</b> — couleurs, rotations, liens, calques. Le <b>⧗ collapse</b> te <b>lit</b> en kion stable, sans jamais écraser ton art.</span>' +
'<span class="sp"></span>' +
'<button data-a="help">? aide</button>' +
'</div>' +
'<div class="pge__bar">' +
'<div class="grp" data-tools></div>' +
'<div class="sep"></div>' +
'<button class="tbtn" data-tool="select" title="sélection / cadre de sélection">⬚ sélec</button>' +
'<button class="tbtn" data-tool="link" title="relier deux formes">⟶ lien</button>' +
'<label class="pge__lc" title="couleur du lien"><input type="color" data-linkcol value="' + GOLD + '"></label>' +
'<div class="sep"></div>' +
'<div class="pge__size"><span>taille</span><input type="range" data-size min="20" max="200" step="2" value="' + size + '"><span data-sizev>' + size + '</span></div>' +
'<div class="sep"></div>' +
'<button data-a="undo" title="Annuler">↶</button>' +
'<button data-a="dup" title="Dupliquer la sélection (Ctrl+D)">⧉</button>' +
'<button data-a="tile" title="Tiler : mettre des formes DANS la forme (pavage fractal)">⊞ tiler</button>' +
'<button data-a="del" title="Supprimer la sélection (Suppr)">⌫</button>' +
'<button data-a="clear" class="danger" title="Tout vider">🗑</button>' +
'<div class="sep"></div>' +
'<button data-a="collapse" class="primary" title="Lire en kion stable (ne modifie pas le canvas)">⧗ collapse</button>' +
'<button data-a="viewkion" title="Aperçu du kion">👁</button>' +
'<span class="sp"></span>' +
'<button data-a="galerie" title="Mes compos">📁</button>' +
'<button data-a="save" title="Enregistrer la compo en cours">💾</button>' +
'</div>' +
'<div class="pge__main">' +
'<div class="pge__canvas" data-canvas>' +
'<svg class="stage" data-stage xmlns="http://www.w3.org/2000/svg"><g data-layer></g></svg>' +
'<div class="pge__badge" data-badge></div>' +
'<div class="pge__hint">tap=pose · glisse=déplace · fond=vue · molette=zoom · ⬚ cadre · ⟶ lien · ⊞ tiler=formes dans la forme</div>' +
'<div class="pge__help" data-helppanel></div>' +
'<div class="pge__ov" data-kionview></div>' +
'<div class="pge__ov" data-galerie></div>' +
'<div class="pge__ov" data-tiler></div>' +
'</div>' +
'<div class="pge__side">' +
'<div class="pge__insp" data-inspector></div>' +
'<div class="pge__codebox">' +
'<h4>ton code — lecture (⧗ collapse)</h4>' +
'<div class="pge__code" data-readable></div>' +
'<div class="pge__passes" data-passes></div>' +
'<div class="pge__mini" data-mini></div>' +
'<div class="pge__notation" data-notation></div>' +
'<div class="pge__acts">' +
'<button data-a="recollapse" class="primary">⧗ collapse</button>' +
'<button data-a="viewkion">👁 voir le kion</button>' +
'<button data-a="copynote">⎘ notation</button>' +
'</div>' +
'<div class="pge__note">Le collapse <b>lit</b> ta compo (formes + positions) — il ignore couleurs, rotations et liens, et ne touche jamais ton canvas.</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var root = container.querySelector('.pge');
var toolsEl = root.querySelector('[data-tools]');
var canvasEl = root.querySelector('[data-canvas]');
var stage = root.querySelector('[data-stage]');
var layer = root.querySelector('[data-layer]');
var badgeEl = root.querySelector('[data-badge]');
var helpPanel = root.querySelector('[data-helppanel]');
var kionEl = root.querySelector('[data-kionview]');
var galEl = root.querySelector('[data-galerie]');
var tileEl = root.querySelector('[data-tiler]');
var inspEl = root.querySelector('[data-inspector]');
var codeBox = root.querySelector('.pge__codebox');
var readableEl = root.querySelector('[data-readable]');
var passesEl = root.querySelector('[data-passes]');
var miniEl = root.querySelector('[data-mini]');
var notationEl = root.querySelector('[data-notation]');
var sizeInput = root.querySelector('[data-size]');
var sizeVal = root.querySelector('[data-sizev]');
var linkColInput = root.querySelector('[data-linkcol]');
// ---------- barre de glyphes ----------
(function () {
var h = '', i;
for (i = 0; i < TOOLS.length; i++) h += '<button class="glyph' + (TOOLS[i].g === tool ? ' on' : '') + '" data-tool="' + TOOLS[i].g + '" title="' + TOOLS[i].l + '">' + TOOLS[i].s + '</button>';
toolsEl.innerHTML = h;
})();
function setTool(t) {
tool = t;
var btns = root.querySelectorAll('[data-tool]'), i;
for (i = 0; i < btns.length; i++) { if (btns[i].getAttribute('data-tool') === t) btns[i].classList.add('on'); else btns[i].classList.remove('on'); }
stage.style.cursor = (t === 'link') ? 'cell' : (t === 'select') ? 'default' : 'crosshair';
}
// ---------- persistance ----------
function snapshot() { return { shapes: clone(shapes), connections: clone(connections), kion: lastKion ? lastKion.kion : null, notation: lastKion ? lastKion.notation : '', savedAt: Date.now() }; }
function persist() { var snap = snapshot(); try { store.set(snap); } catch (e) {} if (window.jOSBoxion && window.jOSBoxion.saveSoon) try { window.jOSBoxion.saveSoon(NS, KEY, snap, '⧗ compo en cours'); } catch (e) {} }
function persistSoon() {
// toute edition rend l'ancienne lecture perimee : on ferme l'apercu stale tout de suite,
// et on recalcule le panneau en debounce (LECTURE non-destructive, pas de morph du canvas).
if (kionEl && kionEl.className.indexOf('show') >= 0) closeOv(kionEl);
if (persistT) clearTimeout(persistT);
persistT = setTimeout(function () { persistT = 0; persist(); computeCollapse(); }, 500);
}
function pushUndo() { undoStack.push({ s: clone(shapes), c: clone(connections) }); if (undoStack.length > 100) undoStack.shift(); }
// ---------- vue / transform ----------
function applyTransform() { layer.setAttribute('transform', 'translate(' + fmt(view.tx) + ',' + fmt(view.ty) + ') scale(' + fmt(view.scale) + ')'); }
function centerView() { var w = canvasEl.clientWidth || 300, hh = canvasEl.clientHeight || 300; view.tx = w / 2; view.ty = hh / 2; applyTransform(); }
function worldFromEvent(cx, cy) { var r = stage.getBoundingClientRect(); return { x: (cx - r.left - view.tx) / view.scale, y: (cy - r.top - view.ty) / view.scale }; }
// ---------- rendu (toujours le vrac coloré, jamais écrasé) ----------
function connSvg(c) {
var a = byId(c.a), b = byId(c.b); if (!a || !b) return '';
var col = c.col || GOLD;
return '<line x1="' + fmt(a.x) + '" y1="' + fmt(a.y) + '" x2="' + fmt(b.x) + '" y2="' + fmt(b.y) + '" stroke="' + col + '" stroke-width="1.8" opacity="0.85"/>' +
'<circle cx="' + fmt(b.x) + '" cy="' + fmt(b.y) + '" r="3.2" fill="' + col + '"/>';
}
function marqueeSvg() {
var x = Math.min(marquee.x0, marquee.x1), y = Math.min(marquee.y0, marquee.y1), w = Math.abs(marquee.x1 - marquee.x0), h = Math.abs(marquee.y1 - marquee.y0);
return '<rect x="' + fmt(x) + '" y="' + fmt(y) + '" width="' + fmt(w) + '" height="' + fmt(h) + '" fill="rgba(125,240,255,.08)" stroke="' + COL + '" stroke-width="1" stroke-dasharray="4 3"/>';
}
function drawAll() {
var parts = [], i;
for (i = 0; i < connections.length; i++) parts.push(connSvg(connections[i]));
if (linkPreview) { var lf = byId(linkPreview.id); if (lf) parts.push('<line x1="' + fmt(lf.x) + '" y1="' + fmt(lf.y) + '" x2="' + fmt(linkPreview.x) + '" y2="' + fmt(linkPreview.y) + '" stroke="' + linkCol + '" stroke-width="1.6" stroke-dasharray="5 4" opacity="0.8"/>'); }
var ordered = shapes.slice().sort(function (p, q) { return (p.z || 0) - (q.z || 0); });
for (i = 0; i < ordered.length; i++) parts.push(glyphSvg(ordered[i]));
for (i = 0; i < shapes.length; i++) if (isSel(shapes[i].id)) parts.push(ringSvg(shapes[i]));
if (marquee) parts.push(marqueeSvg());
layer.innerHTML = parts.join('');
updateBadge();
}
function updateBadge() { var n = shapes.length, t = n + ' glyphe' + (n > 1 ? 's' : ''); if (selIds.length) t += ' · ' + selIds.length + ' sélec'; badgeEl.textContent = t; }
// ---------- inspecteur (couche créative) ----------
function applySel(fn) { var i, s; for (i = 0; i < selIds.length; i++) { s = byId(selIds[i]); if (s) fn(s); } }
function renderInspector() {
if (!selIds.length) { inspEl.style.display = 'none'; inspEl.innerHTML = ''; return; }
inspEl.style.display = 'block';
var one = selIds.length === 1 ? byId(selIds[0]) : null, ref = one || byId(selIds[0]);
if (!ref) { inspEl.style.display = 'none'; return; }
var h = '<h4>inspecteur — ' + (one ? glyphLabel(one.g) : selIds.length + ' formes') + '</h4>';
h += '<div class="pge__pal">';
var i; for (i = 0; i < PALETTE.length; i++) h += '<button class="sw' + (ref.col === PALETTE[i] ? ' on' : '') + '" data-a="col" data-col="' + PALETTE[i] + '" style="background:' + PALETTE[i] + '" title="' + PALETTE[i] + '"></button>';
h += '<label class="sw sw--cust" title="couleur libre" style="background:' + esc(ref.col) + '"><input type="color" data-a="colcustom" value="' + esc(ref.col) + '"></label>';
h += '</div>';
h += '<div class="pge__row"><span>rotation</span><input type="range" data-a="rot" min="0" max="360" step="5" value="' + (ref.rot || 0) + '"><span class="v">' + (ref.rot || 0) + '°</span></div>';
h += '<div class="pge__row"><span>taille</span><input type="range" data-a="ssize" min="20" max="220" step="2" value="' + ref.s + '"><span class="v">' + Math.round(ref.s) + '</span></div>';
h += '<div class="pge__row"><span>style</span><span class="seg"><button data-a="style" data-v="stroke" class="' + (ref.style !== 'fill' ? 'on' : '') + '">contour</button><button data-a="style" data-v="fill" class="' + (ref.style === 'fill' ? 'on' : '') + '">plein</button></span></div>';
h += '<div class="pge__row"><span>trait</span><input type="range" data-a="sw" min="1" max="7" step="0.5" value="' + (ref.sw || SW) + '"><span class="v">' + fmt(ref.sw || SW) + '</span></div>';
h += '<div class="pge__row"><span>calque</span><span class="seg"><button data-a="front">▲ avant</button><button data-a="back">▼ arrière</button></span></div>';
h += '<div class="pge__acts"><button data-a="tile" class="primary">⊞ tiler (formes dans la forme)</button></div>';
h += '<div class="pge__acts"><button data-a="dup">⧉ dupliquer</button><button data-a="del" class="danger">⌫ supprimer</button></div>';
inspEl.innerHTML = h;
}
function setV(el, txt) { var p = el.parentNode; if (p) { var vv = p.querySelector('.v'); if (vv) vv.textContent = txt; } }
inspEl.addEventListener('input', function (e) {
var el = e.target, a = el.getAttribute ? el.getAttribute('data-a') : null; if (!a) return;
var v = el.value;
if (a === 'rot') { applySel(function (s) { s.rot = +v; }); setV(el, (+v) + '°'); }
else if (a === 'ssize') { applySel(function (s) { var nv = +v; if (s.poly && s.s) { var f = nv / s.s, k; for (k = 0; k < s.poly.length; k++) { s.poly[k][0] *= f; s.poly[k][1] *= f; } } s.s = nv; }); setV(el, Math.round(+v)); }
else if (a === 'sw') { applySel(function (s) { s.sw = +v; }); setV(el, fmt(+v)); }
else if (a === 'colcustom') { paintCol = v; applySel(function (s) { s.col = v; }); }
else return;
drawAll(); persistSoon();
});
inspEl.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-a]') : null; if (!b) return;
var a = b.getAttribute('data-a');
if (a === 'col') { var c = b.getAttribute('data-col'); paintCol = c; pushUndo(); applySel(function (s) { s.col = c; }); renderInspector(); drawAll(); persistSoon(); }
else if (a === 'style') { var vv = b.getAttribute('data-v'); pushUndo(); applySel(function (s) { s.style = vv; }); renderInspector(); drawAll(); persistSoon(); }
else if (a === 'front') { pushUndo(); var mz = maxZ(); applySel(function (s) { mz += 1; s.z = mz; }); drawAll(); persistSoon(); }
else if (a === 'back') { pushUndo(); var nz = minZ(); applySel(function (s) { nz -= 1; s.z = nz; }); drawAll(); persistSoon(); }
else if (a === 'dup') duplicateSelection();
else if (a === 'del') deleteSelection();
else if (a === 'tile') openTiler();
});
// ---------- édition ----------
function placeShape(w) {
if (tool === 'select' || tool === 'link') return;
pushUndo();
var sh = normShape({ g: tool, x: Math.round(w.x * 10) / 10, y: Math.round(w.y * 10) / 10, s: size, col: paintCol });
shapes.push(sh); selIds = [sh.id];
renderInspector(); drawAll(); persistSoon();
}
function duplicateSelection() {
if (!selIds.length) { toast('rien à dupliquer'); return; }
pushUndo();
var src = selShapes(), copies = [], map = {}, i, j;
for (i = 0; i < src.length; i++) { var n = clone(src[i]); n.id = newId(); n.x += 14; n.y += 14; n.z = newZ(); copies.push(n); map[src[i].id] = n.id; }
var nc = []; for (j = 0; j < connections.length; j++) { var c = connections[j]; if (map[c.a] != null && map[c.b] != null) nc.push({ a: map[c.a], b: map[c.b], col: c.col }); }
for (i = 0; i < copies.length; i++) shapes.push(copies[i]);
for (j = 0; j < nc.length; j++) connections.push(nc[j]);
selIds = []; for (i = 0; i < copies.length; i++) selIds.push(copies[i].id);
renderInspector(); drawAll(); persistSoon(); toast('dupliqué');
}
function pasteClipboard() {
if (!clipboard.length) return;
pushUndo();
var copies = [], i;
for (i = 0; i < clipboard.length; i++) { var n = clone(clipboard[i]); n.id = newId(); n.x += 18; n.y += 18; n.z = newZ(); copies.push(n); shapes.push(n); }
selIds = []; for (i = 0; i < copies.length; i++) selIds.push(copies[i].id);
renderInspector(); drawAll(); persistSoon(); toast('collé');
}
function deleteSelection() {
if (!selIds.length) return;
pushUndo();
var keep = [], kc = [], i;
for (i = 0; i < shapes.length; i++) if (!isSel(shapes[i].id)) keep.push(shapes[i]);
for (i = 0; i < connections.length; i++) { var c = connections[i]; if (!isSel(c.a) && !isSel(c.b)) kc.push(c); }
shapes = keep; connections = kc; selIds = [];
renderInspector(); drawAll(); persistSoon();
}
function addConnection(a, b) {
var i; for (i = 0; i < connections.length; i++) { var c = connections[i]; if ((c.a === a && c.b === b) || (c.a === b && c.b === a)) { toast('lien déjà là'); return; } }
pushUndo(); connections.push({ a: a, b: b, col: linkCol }); drawAll(); persistSoon(); toast('lien tracé');
}
// ---------- hit / sélection ----------
function hitTest(w) {
var bestId = -1, bestS = 1e9, i;
for (i = 0; i < shapes.length; i++) { var sh = shapes[i], h = sh.s / 2; if (Math.abs(w.x - sh.x) <= h && Math.abs(w.y - sh.y) <= h && sh.s < bestS) { bestId = sh.id; bestS = sh.s; } }
return bestId;
}
function toggleSel(id) { var i; for (i = 0; i < selIds.length; i++) if (selIds[i] === id) { selIds.splice(i, 1); return; } selIds.push(id); }
function selOrig() { var o = [], i, s; for (i = 0; i < selIds.length; i++) { s = byId(selIds[i]); if (s) o.push({ id: s.id, x: s.x, y: s.y }); } return o; }
function finalizeMarquee() {
if (!marquee) return;
var x0 = Math.min(marquee.x0, marquee.x1), x1 = Math.max(marquee.x0, marquee.x1), y0 = Math.min(marquee.y0, marquee.y1), y1 = Math.max(marquee.y0, marquee.y1);
var picked = [], i, s; for (i = 0; i < shapes.length; i++) { s = shapes[i]; if (s.x >= x0 && s.x <= x1 && s.y >= y0 && s.y <= y1) picked.push(s.id); }
if (marquee.add) { for (i = 0; i < picked.length; i++) if (!isSel(picked[i])) selIds.push(picked[i]); }
else selIds = picked;
}
// ---------- gestes pointer ----------
var ptrs = {}, MOVE_THR = 4;
function ptrArr() { var a = [], k; for (k in ptrs) { if (ptrs.hasOwnProperty(k)) a.push(ptrs[k]); } return a; }
function dist(a, b) { var dx = a.x - b.x, dy = a.y - b.y; return Math.sqrt(dx * dx + dy * dy); }
function mid(a, b) { return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }; }
function onDown(e) {
try { stage.setPointerCapture(e.pointerId); } catch (ex) {}
ptrs[e.pointerId] = { x: e.clientX, y: e.clientY };
if (ptrArr().length >= 2) { startPinch(); return; }
var w = worldFromEvent(e.clientX, e.clientY), hitId = hitTest(w);
if (tool === 'link') {
if (hitId >= 0) { gesture = { type: 'link', id: hitId }; linkPreview = { id: hitId, x: w.x, y: w.y }; drawAll(); }
else gesture = { type: 'pan', cx: e.clientX, cy: e.clientY, tx: view.tx, ty: view.ty, moved: false };
return;
}
if (hitId >= 0) {
if (e.shiftKey) { toggleSel(hitId); renderInspector(); drawAll(); gesture = { type: 'tap' }; }
else { if (!isSel(hitId)) selIds = [hitId]; gesture = { type: 'move', sx: w.x, sy: w.y, moved: false, orig: selOrig() }; renderInspector(); drawAll(); }
return;
}
if (e.shiftKey || tool === 'select') { gesture = { type: 'marquee' }; marquee = { x0: w.x, y0: w.y, x1: w.x, y1: w.y, add: !!e.shiftKey }; }
else gesture = { type: 'pan', cx: e.clientX, cy: e.clientY, tx: view.tx, ty: view.ty, moved: false, placeW: w };
}
function onMove(e) {
if (!ptrs[e.pointerId]) return;
ptrs[e.pointerId] = { x: e.clientX, y: e.clientY };
if (gesture && gesture.type === 'pinch') { updatePinch(); return; }
if (!gesture) return;
if (gesture.type === 'move') {
var w = worldFromEvent(e.clientX, e.clientY);
if (!gesture.moved && dist(w, { x: gesture.sx, y: gesture.sy }) * view.scale > MOVE_THR) { gesture.moved = true; pushUndo(); }
if (gesture.moved) { var dx = w.x - gesture.sx, dy = w.y - gesture.sy, i, o, s; for (i = 0; i < gesture.orig.length; i++) { o = gesture.orig[i]; s = byId(o.id); if (s) { s.x = o.x + dx; s.y = o.y + dy; } } drawAll(); }
} else if (gesture.type === 'marquee') { var w2 = worldFromEvent(e.clientX, e.clientY); marquee.x1 = w2.x; marquee.y1 = w2.y; drawAll(); }
else if (gesture.type === 'link') { var w3 = worldFromEvent(e.clientX, e.clientY); linkPreview.x = w3.x; linkPreview.y = w3.y; drawAll(); }
else if (gesture.type === 'pan') { var pdx = e.clientX - gesture.cx, pdy = e.clientY - gesture.cy; if (Math.abs(pdx) + Math.abs(pdy) > MOVE_THR) gesture.moved = true; view.tx = gesture.tx + pdx; view.ty = gesture.ty + pdy; applyTransform(); }
}
function onUp(e) {
var wasPinch = gesture && gesture.type === 'pinch';
delete ptrs[e.pointerId];
try { stage.releasePointerCapture(e.pointerId); } catch (ex) {}
if (wasPinch) { if (ptrArr().length < 2) gesture = null; return; }
if (!gesture) return;
var t = gesture.type;
if (t === 'move') { if (gesture.moved) persistSoon(); }
else if (t === 'marquee') { finalizeMarquee(); marquee = null; drawAll(); renderInspector(); }
else if (t === 'link') { var w = worldFromEvent(e.clientX, e.clientY), over = hitTest(w); if (over >= 0 && over !== gesture.id) addConnection(gesture.id, over); linkPreview = null; drawAll(); }
else if (t === 'pan') { if (!gesture.moved) { if (tool === 'select') { selIds = []; renderInspector(); drawAll(); } else placeShape(gesture.placeW); } }
gesture = null;
}
function startPinch() {
var a = ptrArr(); if (a.length < 2) return;
var r = stage.getBoundingClientRect(), m = mid(a[0], a[1]);
gesture = { type: 'pinch', d0: dist(a[0], a[1]) || 1, s0: view.scale, wx: (m.x - r.left - view.tx) / view.scale, wy: (m.y - r.top - view.ty) / view.scale };
}
function updatePinch() {
var a = ptrArr(); if (a.length < 2 || !gesture) return;
var r = stage.getBoundingClientRect(), m = mid(a[0], a[1]);
var ns = clamp(gesture.s0 * dist(a[0], a[1]) / gesture.d0, 0.2, 6);
view.scale = ns; view.tx = (m.x - r.left) - gesture.wx * ns; view.ty = (m.y - r.top) - gesture.wy * ns; applyTransform();
}
function onWheel(e) {
e.preventDefault();
var r = stage.getBoundingClientRect(), px = e.clientX - r.left, py = e.clientY - r.top;
var wx = (px - view.tx) / view.scale, wy = (py - view.ty) / view.scale;
var ns = clamp(view.scale * Math.exp(-e.deltaY * 0.0015), 0.2, 6);
view.scale = ns; view.tx = px - wx * ns; view.ty = py - wy * ns; applyTransform();
}
stage.addEventListener('pointerdown', onDown);
stage.addEventListener('pointermove', onMove);
stage.addEventListener('pointerup', onUp);
stage.addEventListener('pointercancel', onUp);
stage.addEventListener('wheel', onWheel, { passive: false });
// ---------- clavier ----------
function onKey(e) {
if (!container.isConnected) return;
var ae = document.activeElement, tn = ae && ae.tagName;
if (tn === 'INPUT' || tn === 'TEXTAREA' || tn === 'SELECT') return;
var mod = e.ctrlKey || e.metaKey;
if (mod && (e.key === 'd' || e.key === 'D')) { e.preventDefault(); duplicateSelection(); }
else if (mod && (e.key === 'c' || e.key === 'C')) { clipboard = clone(selShapes()); if (clipboard.length) toast(clipboard.length + ' copié' + (clipboard.length > 1 ? 's' : '')); }
else if (mod && (e.key === 'v' || e.key === 'V')) { e.preventDefault(); pasteClipboard(); }
else if (e.key === 'Delete' || e.key === 'Backspace') { if (selIds.length) { e.preventDefault(); deleteSelection(); } }
else if (e.key === 'Escape') { selIds = []; renderInspector(); drawAll(); }
}
document.addEventListener('keydown', onKey);
// ---------- taille par défaut + couleur de lien ----------
sizeInput.addEventListener('input', function () { size = sizeInput.value | 0; sizeVal.textContent = size; });
linkColInput.addEventListener('input', function () { linkCol = linkColInput.value; });
// ---------- collapse : LECTURE non destructive ----------
function structOf() { var o = [], i, s; for (i = 0; i < shapes.length; i++) { s = shapes[i]; o.push({ g: s.g, x: s.x, y: s.y, s: s.s }); } return o; }
function updatePanel() {
if (!lastKion) {
readableEl.innerHTML = '<span class="pge__empty">Crée librement, puis ⧗ collapse : la lecture de ta compo (kion stable) apparaît ici — ton canvas ne bouge pas.</span>';
passesEl.textContent = ''; notationEl.textContent = ''; miniEl.innerHTML = '';
return;
}
readableEl.textContent = lastKion.readable;
passesEl.textContent = 'état stable atteint en ' + lastKion.passes + ' passe' + (lastKion.passes > 1 ? 's' : '');
notationEl.textContent = lastKion.notation;
try { miniEl.innerHTML = C.encode(lastKion.kion, { size: 130 }); } catch (e) { miniEl.innerHTML = ''; }
}
function computeCollapse() {
var st = structOf();
if (!st.length) { lastKion = null; updatePanel(); return; }
try { lastKion = C.collapse(st); } catch (e) { lastKion = null; }
updatePanel();
}
function doCollapse() {
if (!shapes.length) { toast('Rien à lire — pose des glyphes'); return; }
computeCollapse();
if (lastKion) toast('lecture : ' + lastKion.notation);
}
// ---------- overlays ----------
function closeOv(el) { el.className = 'pge__ov'; }
function openKion() {
if (!lastKion) computeCollapse();
if (!lastKion) { toast('Rien à afficher — pose des glyphes'); return; }
var sz = Math.max(180, Math.min(canvasEl.clientWidth, canvasEl.clientHeight) * 0.62), svg = '';
try { svg = C.encode(lastKion.kion, { size: sz }); } catch (e) { svg = ''; }
kionEl.innerHTML = '<div class="pge__ovhead"><b>aperçu du kion</b><span class="sp"></span><button class="close" data-a="closeov">fermer</button></div>' +
'<div class="pge__ovbody">' + svg + '<div class="pge__ovmeta">' + esc(lastKion.readable) + '<br><span class="mono">' + esc(lastKion.notation) + '</span><br><span class="dim">aperçu seulement — ton canvas reste intact</span></div></div>';
kionEl.className = 'pge__ov show';
}
kionEl.addEventListener('click', function (e) { var b = e.target.closest ? e.target.closest('[data-a]') : null; if (b && b.getAttribute('data-a') === 'closeov') closeOv(kionEl); else if (e.target === kionEl) closeOv(kionEl); });
// ---------- galerie de compos ----------
function openGalerie() { galEl.className = 'pge__ov show'; renderGalerie(); }
function renderGalerie() {
galEl.innerHTML = '<div class="pge__ovhead"><b>mes compos</b><span class="sp"></span><button class="close" data-a="closeov">fermer</button></div>' +
'<div class="pge__gnew"><input type="text" data-gname placeholder="nom de la compo" maxlength="40"><button data-a="gsave" class="primary">💾 sauver</button><button data-a="gnew">+ nouvelle</button></div>' +
'<div class="pge__glist" data-glist><div class="pge__empty">chargement…</div></div>';
var glist = galEl.querySelector('[data-glist]');
if (window.jOSBoxion && window.jOSBoxion.list) {
window.jOSBoxion.list(NS).then(function (items) { fillGList(glist, items || []); }).catch(function () { glist.innerHTML = '<div class="pge__empty">liste indisponible.</div>'; });
} else glist.innerHTML = '<div class="pge__empty">connecte-toi pour sauver/charger des compos (le travail en cours reste en local).</div>';
}
function fillGList(glist, items) {
var comps = [], i; for (i = 0; i < items.length; i++) if (String(items[i].key).indexOf('comp:') === 0) comps.push(items[i]);
if (!comps.length) { glist.innerHTML = '<div class="pge__empty">aucune compo enregistrée. Nomme ta création et 💾 sauve-la.</div>'; return; }
var h = '', j; for (j = 0; j < comps.length; j++) { var it = comps[j]; h += '<div class="pge__gitem"><span class="nm">' + esc(it.name || it.key.slice(5)) + '</span><span class="sp"></span><button data-a="gopen" data-key="' + esc(it.key) + '">ouvrir</button><button data-a="gdel" data-key="' + esc(it.key) + '" class="danger">✕</button></div>'; }
glist.innerHTML = h;
}
function saveComp() {
var inp = galEl.querySelector('[data-gname]'), nm = inp ? inp.value.replace(/^\s+|\s+$/g, '') : '';
if (!nm) { toast('donne un nom à ta compo'); if (inp) inp.focus(); return; }
if (!(window.jOSBoxion && window.jOSBoxion.save)) { toast('boxion absent — connecte-toi', true); return; }
computeCollapse();
var key = 'comp:' + slug(nm), data = { name: nm, shapes: clone(shapes), connections: clone(connections), notation: lastKion ? lastKion.notation : '', savedAt: Date.now() };
window.jOSBoxion.save(NS, key, data, '◇ ' + nm).then(function (r) { toast((r && r.offline) ? 'hors-ligne — gardé en local' : 'compo « ' + nm + ' » sauvée', r && r.offline); renderGalerie(); });
}
function openComp(key) {
if (!(window.jOSBoxion && window.jOSBoxion.load)) return;
window.jOSBoxion.load(NS, key).then(function (it) {
if (it && it.data && it.data.shapes) { ingest(it.data); renderInspector(); drawAll(); computeCollapse(); closeOv(galEl); toast('compo ouverte'); }
else toast('compo introuvable', true);
});
}
function delComp(key) {
if (!(window.jOSBoxion && window.jOSBoxion.del)) return;
window.jOSBoxion.del(NS, key).then(function () { renderGalerie(); toast('compo supprimée'); });
}
galEl.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-a]') : null;
if (!b) { if (e.target === galEl) closeOv(galEl); return; }
var a = b.getAttribute('data-a');
if (a === 'closeov') closeOv(galEl);
else if (a === 'gnew') { pushUndo(); shapes = []; connections = []; selIds = []; lastKion = null; renderInspector(); updatePanel(); drawAll(); persistSoon(); closeOv(galEl); toast('nouvelle compo — canvas vierge'); }
else if (a === 'gsave') saveComp();
else if (a === 'gopen') openComp(b.getAttribute('data-key'));
else if (a === 'gdel') delComp(b.getAttribute('data-key'));
});
// ---------- TILING : subdiviser une forme en une TESSELLATION de sous-formes (pavage fractal) ----------
// koin -> C.packTri(triangle, N) = N sous-triangles qui pavent le triangle à 100% (exact)
// kion -> grille k×k / C.pack(N) koins / d carrés emboîtés
// wormion -> d ronds concentriques · portion -> d carrés emboîtés
// losange -> 4 sous-losanges (pavage L1 exact) OU 2 koins · xer -> 8 koins (éventail)
// Les sous-formes sont de VRAIES shapes (poly relatif pour les triangles) -> re-sélectionnables,
// colorables et RE-TILABLES (fractale). C.pack/C.packTri retombent sur le fpack du codec si jOSKion absent.
var tileState = { motif: null, n: 4 };
function centroidOf(pts) { var sx = 0, sy = 0, i; for (i = 0; i < pts.length; i++) { sx += pts[i][0]; sy += pts[i][1]; } return [sx / pts.length, sy / pts.length]; }
function bboxOf(pts) { var mnx = 1e9, mny = 1e9, mxx = -1e9, mxy = -1e9, i, p; for (i = 0; i < pts.length; i++) { p = pts[i]; if (p[0] < mnx) mnx = p[0]; if (p[0] > mxx) mxx = p[0]; if (p[1] < mny) mny = p[1]; if (p[1] > mxy) mxy = p[1]; } return { w: mxx - mnx, h: mxy - mny }; }
function hx2(v) { v = v < 0 ? 0 : v > 255 ? 255 : Math.round(v); var t = v.toString(16); return t.length < 2 ? '0' + t : t; }
function varyCol(col) { var s = String(col || ''); if (s.charAt(0) === '#') s = s.slice(1); if (!/^[0-9a-fA-F]{6}$/.test(s)) return col || COL; var r = parseInt(s.slice(0, 2), 16), g = parseInt(s.slice(2, 4), 16), b = parseInt(s.slice(4, 6), 16); function j(x) { return x + Math.round(Math.random() * 30 - 15); } return '#' + hx2(j(r)) + hx2(j(g)) + hx2(j(b)); }
// triangle absolu d'un koin (respecte le poly si c'est déjà une sous-forme -> récursion exacte)
function triOf(sh) {
if (sh.poly && sh.poly.length === 3) return [[sh.x + sh.poly[0][0], sh.y + sh.poly[0][1]], [sh.x + sh.poly[1][0], sh.y + sh.poly[1][1]], [sh.x + sh.poly[2][0], sh.y + sh.poly[2][1]]];
var h = sh.s / 2; return [[sh.x, sh.y - h], [sh.x - h * 0.92, sh.y + h * 0.72], [sh.x + h * 0.92, sh.y + h * 0.72]];
}
// carré absolu d'un kion : {x0,y0,s}
function sqOf(sh) { if (sh.poly && sh.poly.length) { var bb = bboxOf(sh.poly), c = centroidOf(sh.poly); return { x0: sh.x + c[0] - bb.w / 2, y0: sh.y + c[1] - bb.h / 2, s: Math.max(bb.w, bb.h) }; } var h = sh.s / 2; return { x0: sh.x - h, y0: sh.y - h, s: sh.s }; }
// spec de sous-forme polygonale (koin) à partir de sommets ABSOLUS -> poly relatif au centroïde
function polyShapeFromAbs(absPts, g, col) { var c = centroidOf(absPts), bb = bboxOf(absPts), rel = [], i; for (i = 0; i < absPts.length; i++) rel.push([absPts[i][0] - c[0], absPts[i][1] - c[1]]); return { g: g, x: c[0], y: c[1], s: Math.max(bb.w, bb.h) || 10, col: col, poly: rel }; }
function motifsFor(g) {
if (g === 'koin') return [{ k: 'ktri', label: '△ koins qui pavent (N)' }];
if (g === 'kion') return [{ k: 'kgrid', label: '▦ grille k×k de kions' }, { k: 'kpack', label: '△ koins qui pavent le carré (N)' }, { k: 'kconc', label: '▣ carrés emboîtés (profondeur)' }];
if (g === 'wormion') return [{ k: 'wrings', label: '◎ ronds concentriques (profondeur)' }];
if (g === 'portion') return [{ k: 'pconc', label: '▣ carrés emboîtés (profondeur)' }];
if (g === 'losange') return [{ k: 'los4', label: '◇ 4 sous-losanges (pavage)' }, { k: 'los2', label: '△ 2 koins' }];
if (g === 'xer') return [{ k: 'xfan', label: '△ 8 koins (éventail)' }];
return [];
}
function motifOk(motif, g) {
if (motif === 'ktri') return g === 'koin';
if (motif === 'kpack' || motif === 'kgrid' || motif === 'kconc') return g === 'kion';
if (motif === 'pconc') return g === 'portion';
if (motif === 'wrings') return g === 'wormion';
if (motif === 'los4' || motif === 'los2') return g === 'losange';
if (motif === 'xfan') return g === 'xer';
return false;
}
function ctrlOf(motif) { if (motif === 'kgrid') return 'k'; if (motif === 'kconc' || motif === 'pconc' || motif === 'wrings') return 'd'; if (motif === 'los4' || motif === 'los2' || motif === 'xfan') return null; return 'n'; }
function ctrlRange(c) { if (c === 'k') return [2, 10]; if (c === 'd') return [2, 10]; return [2, 64]; }
// génère les specs de sous-formes (sans id/z) pour un motif sur UNE forme
function genTiles(motif, sh, n) {
var out = [], i, j;
if (motif === 'ktri') { var subs = C.packTri(triOf(sh), n); for (i = 0; i < subs.length; i++) out.push(polyShapeFromAbs(subs[i], 'koin', varyCol(sh.col))); }
else if (motif === 'kpack') { var q = sqOf(sh), u = C.pack(n); for (i = 0; i < u.length; i++) { var abs = []; for (j = 0; j < u[i].length; j++) abs.push([q.x0 + u[i][j][0] * q.s, q.y0 + u[i][j][1] * q.s]); out.push(polyShapeFromAbs(abs, 'koin', varyCol(sh.col))); } }
else if (motif === 'kgrid') { var qg = sqOf(sh), k = Math.max(2, Math.round(n)), cell = qg.s / k, a, b; for (a = 0; a < k; a++) for (b = 0; b < k; b++) out.push({ g: 'kion', x: qg.x0 + (a + 0.5) * cell, y: qg.y0 + (b + 0.5) * cell, s: cell, col: varyCol(sh.col) }); }
else if (motif === 'kconc' || motif === 'pconc') { var gg = (motif === 'pconc') ? 'portion' : 'kion', d = Math.max(2, Math.round(n)); for (i = 1; i <= d; i++) { var f = 1 - i / (d + 1); out.push({ g: gg, x: sh.x, y: sh.y, s: sh.s * f, col: varyCol(sh.col) }); } }
else if (motif === 'wrings') { var dd = Math.max(2, Math.round(n)); for (i = 1; i <= dd; i++) { var ff = 1 - i / (dd + 1); out.push({ g: 'wormion', x: sh.x, y: sh.y, s: sh.s * ff, col: varyCol(sh.col) }); } }
else if (motif === 'los4') { var half = sh.s / 2, off = half / 2, cs = [[sh.x + off, sh.y], [sh.x - off, sh.y], [sh.x, sh.y + off], [sh.x, sh.y - off]]; for (i = 0; i < 4; i++) out.push({ g: 'losange', x: cs[i][0], y: cs[i][1], s: half, col: varyCol(sh.col) }); }
else if (motif === 'los2') { var h2 = sh.s / 2; out.push(polyShapeFromAbs([[sh.x, sh.y - h2], [sh.x + h2, sh.y], [sh.x - h2, sh.y]], 'koin', varyCol(sh.col))); out.push(polyShapeFromAbs([[sh.x + h2, sh.y], [sh.x, sh.y + h2], [sh.x - h2, sh.y]], 'koin', varyCol(sh.col))); }
else if (motif === 'xfan') { var h3 = sh.s / 2, ocv = oct(sh.x, sh.y, h3), ce = [sh.x, sh.y], m; for (i = 0; i < 8; i++) { m = (i + 1) % 8; out.push(polyShapeFromAbs([ce.slice(), ocv[i], ocv[m]], 'koin', varyCol(sh.col))); } }
return out;
}
function applyTile() {
var sel = selShapes(); if (!sel.length) { toast('sélectionne une forme'); return; }
var motif = tileState.motif, n = tileState.n, specs = [], i, j, base = maxZ();
for (i = 0; i < sel.length; i++) { if (!motifOk(motif, sel[i].g)) continue; var gs = genTiles(motif, sel[i], n); for (j = 0; j < gs.length; j++) specs.push(gs[j]); }
if (!specs.length) { toast('ce motif ne va pas à cette forme', true); return; }
pushUndo();
var made = []; for (i = 0; i < specs.length; i++) { base += 1; specs[i].z = base; var sh = normShape(specs[i]); shapes.push(sh); made.push(sh.id); }
selIds = made;
closeOv(tileEl); renderInspector(); drawAll(); computeCollapse(); persistSoon();
toast(specs.length + ' sous-forme' + (specs.length > 1 ? 's' : '') + ' — pavage');
}
function openTiler() {
var sel = selShapes(); if (!sel.length) { toast('sélectionne une forme à tiler'); return; }
var g = sel[0].g, list = motifsFor(g);
if (!list.length) { toast('cette forme n\'a pas de tiling', true); return; }
var ok = false, i; for (i = 0; i < list.length; i++) if (list[i].k === tileState.motif) ok = true;
if (!ok) tileState.motif = list[0].k;
tileEl.className = 'pge__ov show'; renderTiler(g, list);
}
function renderTiler(g, list) {
var c = ctrlOf(tileState.motif), rng = ctrlRange(c); if (c) tileState.n = clamp(tileState.n, rng[0], rng[1]);
var h = '<div class="pge__ovhead"><b>⊞ tiler — ' + esc(glyphLabel(g)) + '</b><span class="sp"></span><button class="close" data-a="closeov">fermer</button></div>';
h += '<div class="pge__ovbody" style="align-items:stretch;max-width:420px;margin:0 auto;width:100%">';
h += '<div class="pge__tmotifs">'; var i;
for (i = 0; i < list.length; i++) h += '<button class="pge__mbtn' + (list[i].k === tileState.motif ? ' on' : '') + '" data-mot="' + list[i].k + '">' + esc(list[i].label) + '</button>';
h += '</div>';
if (c) { var lab = c === 'k' ? 'grille k' : (c === 'd' ? 'profondeur' : 'N sous-formes'); h += '<div class="pge__tstep"><span>' + lab + '</span><button data-step="-1">−</button><b data-nval>' + tileState.n + '</b><button data-step="1">+</button></div>'; }
else h += '<div class="pge__tstep dim">motif à nombre fixe</div>';
h += '<button class="primary" data-a="applytile" style="margin-top:8px">⊞ appliquer le pavage</button>';
h += '<div class="pge__note">Ça pose des formes DANS ta forme (pavage). Chaque sous-forme est sélectionnable, colorable et RE-TILABLE (fractale). La forme parente reste dessous, intacte.</div>';
h += '</div>';
tileEl.innerHTML = h;
}
tileEl.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-a],[data-mot],[data-step]') : null;
if (!b) { if (e.target === tileEl) closeOv(tileEl); return; }
if (b.getAttribute('data-mot')) { tileState.motif = b.getAttribute('data-mot'); var sel = selShapes(), g = sel.length ? sel[0].g : 'koin'; renderTiler(g, motifsFor(g)); return; }
if (b.getAttribute('data-step')) { var cc = ctrlOf(tileState.motif); if (cc) { var rg = ctrlRange(cc); tileState.n = clamp(tileState.n + (b.getAttribute('data-step') === '1' ? 1 : -1), rg[0], rg[1]); var nv = tileEl.querySelector('[data-nval]'); if (nv) nv.textContent = tileState.n; } return; }
var a = b.getAttribute('data-a');
if (a === 'closeov') closeOv(tileEl);
else if (a === 'applytile') applyTile();
});
// ---------- barres d'actions ----------
root.querySelector('.pge__bar').addEventListener('click', function (e) {
var tb = e.target.closest ? e.target.closest('[data-tool]') : null;
if (tb) { setTool(tb.getAttribute('data-tool')); return; }
var b = e.target.closest ? e.target.closest('[data-a]') : null; if (!b) return;
var a = b.getAttribute('data-a');
if (a === 'undo') { if (!undoStack.length) return toast('Rien à annuler'); var st = undoStack.pop(); shapes = st.s; connections = st.c; selIds = []; renderInspector(); drawAll(); computeCollapse(); persistSoon(); }
else if (a === 'dup') duplicateSelection();
else if (a === 'tile') openTiler();
else if (a === 'del') deleteSelection();
else if (a === 'clear') { if (!shapes.length && !connections.length) return; pushUndo(); shapes = []; connections = []; selIds = []; lastKion = null; renderInspector(); updatePanel(); drawAll(); persistSoon(); }
else if (a === 'collapse') doCollapse();
else if (a === 'viewkion') openKion();
else if (a === 'galerie') openGalerie();
else if (a === 'save') doSave();
});
codeBox.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-a]') : null; if (!b) return;
var a = b.getAttribute('data-a');
if (a === 'recollapse') doCollapse();
else if (a === 'viewkion') openKion();
else if (a === 'copynote') { if (lastKion) { copy(lastKion.notation); toast('notation copiée'); } else toast('fais ⧗ collapse d\'abord'); }
});
root.querySelector('.pge__intro').addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-a]') : null; if (!b) return;
helpPanel.className = 'pge__help show';
helpPanel.innerHTML = helpHtml();
helpPanel.querySelector('.close').onclick = function () { helpPanel.className = 'pge__help'; };
});
function helpHtml() {
return '<h3>⧗ Crée librement — le collapse LIT, il n\'écrase pas</h3>' +
'<div>Il y a <b>deux couches</b>. <b>Ta couche créative</b> : ton art, totalement libre — chaque forme a sa <b>couleur</b>, sa <b>rotation</b>, sa <b>taille</b>, son <b>style</b> (contour/plein), son <b>calque</b> ; tu relies les formes par des <b>liens</b> ; tu multi-sélectionnes, dupliques, colles. <b>La couche structure</b> : juste les formes et leurs positions, que l\'algo <b>lit</b>.</div>' +
'<div>Le <b>⧗ collapse</b> est une <b>loupe</b> : il réduit ta compo à un <b>kion stable</b> (forme normale — le re-lire ne change plus rien) et l\'affiche dans « ton code ». Il <b>ignore</b> couleurs, rotations et liens (c\'est de la déco) et <b>ne touche jamais</b> ton canvas. « 👁 voir le kion » ouvre un aperçu propre, ton art reste intact.</div>' +
'<div class="forms">' +
'<div><span class="s">△</span><span><b>koin</b> — le triangle, la brique.</span></div>' +
'<div><span class="s">▢</span><span><b>kion</b> — carré de koins = un ploxion.</span></div>' +
'<div><span class="s">○</span><span><b>wormion</b> — ronds emboîtés = un portail.</span></div>' +
'<div><span class="s">▣</span><span><b>portion</b> — carrés emboîtés = un lieu.</span></div>' +
'<div><span class="s">◇</span><span><b>losange</b> — le lien.</span></div>' +
'<div><span class="s">⬢</span><span><b>xer</b> — l\'octogone, le hub.</span></div>' +
'</div>' +
'<div><b>⊞ Tiling (formes dans les formes) :</b> sélectionne une forme et fais <b>⊞ tiler</b> — ça la <b>pave</b> de sous-formes qui remplissent l\'espace : un <b>koin</b> se remplit de koins (triangles dans le triangle, exact), un <b>kion</b> devient une grille k×k, un pavage de koins, ou des carrés emboîtés ; un <b>wormion</b> = ronds concentriques, une <b>portion</b> = carrés emboîtés, un <b>losange</b> = 4 sous-losanges ou 2 koins, un <b>xer</b> = 8 koins en éventail. Chaque sous-forme est une vraie forme : re-sélectionne-la et <b>⊞</b> encore pour densifier (fractale). C\'est la densité fractale — mets des formes dans des formes, ça pave.</div>' +
'<div><b>Gestes :</b> choisis un glyphe puis tape pour le poser · glisse une forme pour la déplacer · <b>clic</b> = sélection, <b>Maj+clic</b> ou outil <b>⬚</b> + cadre = multi-sélection · outil <b>⟶</b> = glisse de A vers B pour tracer un lien · <b>⧉</b>/Ctrl+D dupliquer · <b>⊞</b> tiler · Ctrl+C/V copier-coller · Suppr supprimer · glisse le fond = vue · molette/pinch = zoom · <b>📁</b> tes compos nommées.</div>' +
'<button class="close">fermer</button>';
}
// ---------- enregistrer (doc de travail) ----------
function doSave() {
computeCollapse();
var data = snapshot();
if (window.jOSBoxion && window.jOSBoxion.save) {
window.jOSBoxion.save(NS, KEY, data, '⧗ ' + (lastKion ? lastKion.notation : 'compo glyphes')).then(function (r) { toast((r && r.offline) ? 'hors-ligne — gardé en local' : 'compo enregistrée', r && r.offline); });
} else toast('boxion absent — gardé en local', true);
persist();
}
// ---------- resize ----------
function onResize() { drawAll(); }
window.addEventListener('resize', onResize);
// ---------- boot ----------
ingest(store.get(null));
setTool('koin');
centerView();
renderInspector();
drawAll();
computeCollapse();
if (window.jOSBoxion && window.jOSBoxion.load) {
window.jOSBoxion.load(NS, KEY).then(function (it) {
if (it && it.data && it.data.shapes && it.data.shapes.length) { ingest(it.data); renderInspector(); drawAll(); computeCollapse(); }
});
}
return {
unmount: function () {
if (persistT) clearTimeout(persistT);
document.removeEventListener('keydown', onKey);
window.removeEventListener('resize', onResize);
container.innerHTML = '';
}
};
}
// ===== XUI (contrat v0) : describe() PUR, marche NON MONTÉ — lit la compo persistée
// (localStorage xb:glyphes:puzzle, même clé que jOSb.store du mount). Ne lève jamais. =====
function describe(){
try {
var snap = null;
try { snap = JSON.parse(localStorage.getItem('xb:glyphes:puzzle') || 'null'); } catch (e0) {}
var shapes = (snap && snap.shapes instanceof Array) ? snap.shapes : [];
var conns = (snap && snap.connections instanceof Array) ? snap.connections : [];
var byG = {};
shapes.forEach(function(s){ var g = (s && s.g) || '?'; byG[g] = (byG[g] || 0) + 1; });
var kinds = Object.keys(byG).map(function(g){ return { label: g, value: byG[g] }; });
var items = [
{ p:1, kind:'stat', label:'🔺 compo en cours', value: shapes.length ? (shapes.length + ' formes · ' + conns.length + ' liens') : 'toile vide' },
{ p:2, kind:'stat', label:'notation', value: (snap && snap.notation) ? String(snap.notation).slice(0, 40) : '—' }
];
if (kinds.length) items.push({ p:3, kind:'list', label:'formes posées', items: kinds.slice(0, 6) });
if (snap && snap.savedAt) items.push({ p:4, kind:'text', label:'sauvé ' + new Date(snap.savedAt).toLocaleString() });
if (!shapes.length) items.push({ p:4, kind:'text', label:'pose des koins △, kions ▢, wormions ○ — puis ⊞ tiler' });
return { v:1, id:ID, title:'Glyphes', icon:'🔺', items:items };
} catch (e) {
return { v:1, id:ID, title:'Glyphes', icon:'🔺', items:[{ p:1, kind:'text', label:'Glyphes' }] };
}
}
window.jOSApps = window.jOSApps || {};
window.jOSApps[ID] = { manifest: { id: ID, name: 'Glyphes', icon: '🔺' }, mount: mount, describe: describe };
})();
</script>