Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : studio/web/src/sdk-adapter.ts9 * Module : studio10 * Repository : https://git.spboucher.ai/ka-maps-engine11 * Created : 2026-12-0112 * Last Modified : 2026-12-0113 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Adaptateur « SDK Web » de l'aperçu. Charge @ka/maps publié dans /sdk/<version>/ (même origine,43// session requise) et l'habille en PreviewAdapter : carte interactive, thème remplacé à chaud par44// `setThemeDocument`. Si le module ne se charge pas ou si ni WebGPU ni WebGL 2 ne sont disponibles,45// l'appelant garde le rendu serveur.46import { api } from './api.ts';47import type { Camera, PreviewAdapter, PreviewState } from './Preview.tsx';4849const ll = (c: any): [number, number] => (Array.isArray(c) ? [c[0], c[1]] : [c.lng ?? c.lon, c.lat]);5051export async function loadSdkAdapter(version: string): Promise<PreviewAdapter> {52 const base = `/sdk/${version}/`;53 const mod: any = await import(/* @vite-ignore */ `${base}index.js`);54 const KaMap = mod.KaMap;55 if (!KaMap?.create) throw new Error('export KaMap absent');56 if (mod.isSupported && !mod.isSupported()) throw new Error('ni WebGPU ni WebGL 2 dans ce navigateur');57 return {58 kind: 'sdk',59 label: `SDK Web ${mod.VERSION ?? version} (carte interactive rendue dans le navigateur)`,60 interactive: true,61 mount(el, mode, onStatus, onCamera) {62 const host = document.createElement('div');63 host.style.cssText = 'position:absolute;inset:0';64 el.appendChild(host);65 let map: any = null;66 let dead = false;67 let latest: PreviewState | null = null;68 let applied: { doc: any; app: string | null; brand: any } | null = null;69 let silent = false;70 let seq = 0;71 const pitchFor = (c: Camera) => (mode === '3d' ? Math.max(45, c.pitch) : 0);72 const documentFor = async (s: PreviewState) => (s.app ? (await api.post('/api/effective', { doc: s.doc, app: s.app, brand: s.brand })).doc : s.doc);73 const apply = async () => {74 const s = latest;75 if (!s || dead) return;76 const mine = ++seq;77 try {78 if (!map) {79 onStatus({ busy: true });80 const doc = await documentFor(s);81 if (dead || mine !== seq) return;82 const m = await KaMap.create(host, { themeDocument: doc, center: s.camera.center, zoom: s.camera.zoom, bearing: s.camera.bearing, pitch: pitchFor(s.camera), assetsUrl: base, cache: false });83 if (dead) return void m.destroy();84 map = m;85 applied = { doc: s.doc, app: s.app, brand: s.brand };86 map.setMode(mode);87 map.on('moveend', () => {88 if (silent) return;89 const c = map.getCamera();90 onCamera({ center: ll(c.center), zoom: c.zoom, bearing: c.bearing, pitch: mode === '3d' ? c.pitch : (latest?.camera.pitch ?? 55) });91 });92 map.on('error', (e: any) => onStatus({ busy: false, error: String(e?.message ?? e?.error ?? 'erreur du moteur') }));93 onStatus({ busy: false });94 if (latest !== s) void apply();95 return;96 }97 if (!applied || applied.doc !== s.doc || applied.app !== s.app || applied.brand !== s.brand) {98 onStatus({ busy: true });99 const doc = await documentFor(s);100 if (dead || mine !== seq) return;101 map.setThemeDocument(doc);102 applied = { doc: s.doc, app: s.app, brand: s.brand };103 }104 const c = map.getCamera();105 const cur = ll(c.center);106 if (Math.abs(c.zoom - s.camera.zoom) > 0.01 || Math.abs(cur[0] - s.camera.center[0]) > 1e-6 || Math.abs(cur[1] - s.camera.center[1]) > 1e-6 || Math.abs(c.bearing - s.camera.bearing) > 0.5 || (mode === '3d' && Math.abs(c.pitch - pitchFor(s.camera)) > 0.5)) {107 silent = true;108 map.jumpTo({ center: s.camera.center, zoom: s.camera.zoom, bearing: s.camera.bearing, pitch: pitchFor(s.camera) });109 setTimeout(() => (silent = false), 60);110 }111 onStatus({ busy: false });112 } catch (e: any) {113 const first = e?.detail?.errors?.[0];114 onStatus({ busy: false, error: first ? `${first.path ? first.path + ' — ' : ''}${first.message}` : String(e?.message ?? e) });115 }116 };117 return {118 update(s) {119 latest = s;120 void apply();121 },122 destroy() {123 dead = true;124 map?.destroy();125 host.remove();126 },127 };128 },129 };130}131