Drei Bugs gefixt die Responsive-Layout kaputt machten: 1. Script-Tag war NACH </body> platziert (HTML-invalid) Fix: Script-Tag ist jetzt VOR </body> im vault.html-Patch 2. CSS-Spezifität zu niedrig (nur .vault-app statt .vault-app.hermes-rc) Fix: 2-Klassen-Selektoren .vault-app.hermes-rc.X schlagen obv's Single-Class-Selektoren 3. Mobile-Overflow nicht kontrolliert Fix: overflow-x: hidden auf .vault-app + .vault-content, word-wrap + overflow-wrap für lange URLs/Codes Außerdem: - Init mit 50ms-Delay nach DOMContentLoaded (gibt obv Zeit für Setup) - Hamburger-Toggle mit e.stopPropagation() - Tablet-Graph als Bottom-Panel (220px) mit border-top statt border-left - Mobile-Graph komplett hidden (war: nur klein) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
399 lines
14 KiB
JavaScript
399 lines
14 KiB
JavaScript
/* HermesCustom — Lukas' additions to obsidian-web-viewer
|
|
*
|
|
* Loaded by vault.html (one-line patch) AFTER the main script. Initializes
|
|
* automatically via HermesCustom.init() at the bottom of this file.
|
|
*
|
|
* Features:
|
|
* 1. Current-page highlighting in 3D Graph
|
|
* 2. Click-to-navigate on 3D Graph nodes (Three.js raycasting)
|
|
* 3. Responsive layout (graph collapses below 1024px, tree below 768px)
|
|
* 4. URL-based navigation (?file=<path>)
|
|
*
|
|
* Design notes: see docs/ARCHITECTURE.md
|
|
*
|
|
* IMPORTANT: This script runs AFTER obv's main script (which sets up the
|
|
* layout). So our responsiveLayout() needs to apply classes/styles AFTER
|
|
* obv's setup, and our CSS must win the specificity battle against obv's
|
|
* inline styles + media queries.
|
|
*/
|
|
(function() {
|
|
'use strict';
|
|
|
|
window.HermesCustom = window.HermesCustom || {};
|
|
|
|
const HermesCustom = window.HermesCustom;
|
|
|
|
// ============================================================
|
|
// Configuration
|
|
// ============================================================
|
|
|
|
const BREAKPOINTS = {
|
|
mobile: 768, // <768px
|
|
tablet: 1024, // 768-1023px
|
|
};
|
|
|
|
// ============================================================
|
|
// 1. CSS injection — high specificity to win against obv
|
|
// ============================================================
|
|
|
|
/**
|
|
* Inject responsive CSS into <head> as a stylesheet (not <style>), so
|
|
* the cascade is well-defined. We use:
|
|
* - High specificity selectors (extra class chains)
|
|
* - !important on critical layout properties
|
|
* - CSS variables that we toggle via JS
|
|
*
|
|
* Strategy: we add a class `hermes-rc` (hermes responsive container) to
|
|
* the .vault-app element. All our CSS rules start with `.vault-app.hermes-rc`
|
|
* to beat obv's `.vault-app` selectors.
|
|
*/
|
|
HermesCustom.injectResponsiveCSS = function() {
|
|
if (document.getElementById('hermes-custom-css')) return;
|
|
|
|
// Wait until <head> exists
|
|
const head = document.head || document.getElementsByTagName('head')[0];
|
|
if (!head) {
|
|
setTimeout(HermesCustom.injectResponsiveCSS, 10);
|
|
return;
|
|
}
|
|
|
|
const style = document.createElement('style');
|
|
style.id = 'hermes-custom-css';
|
|
style.textContent = `
|
|
/* === Hermes Custom: always-applied base (no media query) === */
|
|
.vault-app.hermes-rc {
|
|
box-sizing: border-box !important;
|
|
width: 100% !important;
|
|
max-width: 100vw !important;
|
|
overflow-x: hidden !important; /* prevent horizontal scroll on mobile */
|
|
}
|
|
.vault-app.hermes-rc .vault-content {
|
|
overflow-x: hidden !important; /* content scrolls vertically only */
|
|
overflow-y: auto !important;
|
|
max-width: 100% !important;
|
|
word-wrap: break-word !important; /* long URLs/codes don't overflow */
|
|
overflow-wrap: break-word !important;
|
|
}
|
|
.vault-app.hermes-rc .vault-sidebar {
|
|
overflow-x: hidden !important;
|
|
overflow-y: auto !important;
|
|
max-width: 280px !important;
|
|
}
|
|
.vault-app.hermes-rc .vault-graph {
|
|
overflow: hidden !important;
|
|
position: relative !important;
|
|
}
|
|
|
|
/* === Mobile (<768px) === */
|
|
.vault-app.hermes-rc.hermes-mobile {
|
|
grid-template-columns: 1fr !important;
|
|
grid-template-rows: 48px 1fr !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-mobile .vault-sidebar {
|
|
position: fixed !important;
|
|
top: 48px !important;
|
|
left: 0 !important;
|
|
bottom: 0 !important;
|
|
width: 280px !important;
|
|
max-width: 85vw !important;
|
|
transform: translateX(-100%) !important;
|
|
transition: transform 0.2s ease !important;
|
|
z-index: 1000 !important;
|
|
background: var(--bg-secondary) !important;
|
|
border-right: 1px solid var(--border) !important;
|
|
display: block !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-mobile .vault-sidebar.hermes-sidebar-open {
|
|
transform: translateX(0) !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-mobile .vault-graph {
|
|
display: none !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-mobile .vault-content {
|
|
grid-column: 1 !important;
|
|
padding: 16px 20px !important;
|
|
max-width: 100% !important;
|
|
}
|
|
.hermes-mobile-toggle {
|
|
display: none;
|
|
background: var(--bg-secondary);
|
|
border: 1px solid var(--border);
|
|
color: var(--text-secondary);
|
|
padding: 6px 12px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
margin-right: 8px;
|
|
}
|
|
.vault-app.hermes-rc.hermes-mobile .hermes-mobile-toggle { display: inline-block !important; }
|
|
|
|
/* === Tablet (768px-1023px) === */
|
|
.vault-app.hermes-rc.hermes-tablet {
|
|
grid-template-columns: 200px 1fr !important;
|
|
grid-template-rows: 48px 1fr 220px !important;
|
|
height: 100vh !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-tablet .vault-sidebar {
|
|
width: 200px !important;
|
|
max-width: 200px !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-tablet .vault-graph {
|
|
grid-column: 1 / -1 !important;
|
|
grid-row: 3 !important;
|
|
height: 220px !important;
|
|
border-left: none !important;
|
|
border-top: 1px solid var(--border) !important;
|
|
}
|
|
.vault-app.hermes-rc.hermes-tablet .vault-content {
|
|
grid-column: 2 !important;
|
|
grid-row: 2 !important;
|
|
padding: 20px 28px !important;
|
|
}
|
|
|
|
/* === Desktop (≥1024px): leave obv's default layout alone === */
|
|
.vault-app.hermes-rc.hermes-desktop {
|
|
/* inherit obv's grid */
|
|
}
|
|
`;
|
|
head.appendChild(style);
|
|
console.log('[HermesCustom] CSS injected');
|
|
};
|
|
|
|
// ============================================================
|
|
// 2. Layout application — called on init and on resize
|
|
// ============================================================
|
|
|
|
HermesCustom.applyLayout = function() {
|
|
const app = document.querySelector('.vault-app');
|
|
if (!app) {
|
|
// obv not loaded yet, retry
|
|
setTimeout(HermesCustom.applyLayout, 100);
|
|
return;
|
|
}
|
|
|
|
// Add our marker class (specificity booster)
|
|
if (!app.classList.contains('hermes-rc')) {
|
|
app.classList.add('hermes-rc');
|
|
}
|
|
|
|
const width = window.innerWidth;
|
|
// Remove all mode classes
|
|
app.classList.remove('hermes-mobile', 'hermes-tablet', 'hermes-desktop');
|
|
// Sidebar state reset
|
|
const sidebar = document.querySelector('.vault-sidebar');
|
|
if (sidebar) sidebar.classList.remove('hermes-sidebar-open');
|
|
|
|
if (width < BREAKPOINTS.mobile) {
|
|
app.classList.add('hermes-mobile');
|
|
} else if (width < BREAKPOINTS.tablet) {
|
|
app.classList.add('hermes-tablet');
|
|
} else {
|
|
app.classList.add('hermes-desktop');
|
|
}
|
|
|
|
// Add hamburger toggle in mobile mode
|
|
if (width < BREAKPOINTS.mobile) {
|
|
HermesCustom.ensureHamburgerButton();
|
|
}
|
|
};
|
|
|
|
HermesCustom.ensureHamburgerButton = function() {
|
|
if (document.querySelector('.hermes-mobile-toggle')) return;
|
|
const header = document.querySelector('.vault-header');
|
|
if (!header) return;
|
|
const btn = document.createElement('button');
|
|
btn.className = 'hermes-mobile-toggle';
|
|
btn.textContent = '☰';
|
|
btn.setAttribute('aria-label', 'Toggle file tree');
|
|
btn.onclick = (e) => {
|
|
e.stopPropagation();
|
|
const sidebar = document.querySelector('.vault-sidebar');
|
|
if (sidebar) sidebar.classList.toggle('hermes-sidebar-open');
|
|
};
|
|
header.insertBefore(btn, header.firstChild);
|
|
};
|
|
|
|
// Throttled resize handler
|
|
HermesCustom.setupResizeHandler = function() {
|
|
let resizeTimer;
|
|
let lastWidth = window.innerWidth;
|
|
window.addEventListener('resize', () => {
|
|
clearTimeout(resizeTimer);
|
|
resizeTimer = setTimeout(() => {
|
|
if (window.innerWidth !== lastWidth) {
|
|
lastWidth = window.innerWidth;
|
|
HermesCustom.applyLayout();
|
|
}
|
|
}, 150);
|
|
});
|
|
};
|
|
|
|
// ============================================================
|
|
// 3. Current-page highlighting
|
|
// ============================================================
|
|
|
|
HermesCustom.getCurrentFile = function() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const fileParam = params.get('file');
|
|
if (fileParam) return fileParam;
|
|
if (window.location.hash) {
|
|
const hashMatch = window.location.hash.match(/file=([^&]+)/);
|
|
if (hashMatch) return decodeURIComponent(hashMatch[1]);
|
|
}
|
|
return HermesCustom._lastLoadedFile || null;
|
|
};
|
|
|
|
HermesCustom.getConnectedFiles = function(currentFile) {
|
|
if (!currentFile) return [];
|
|
const graphData = HermesCustom._graphData;
|
|
if (!graphData) return [];
|
|
const connections = graphData.edges
|
|
.filter(e => e.source === currentFile)
|
|
.map(e => e.target);
|
|
return connections;
|
|
};
|
|
|
|
HermesCustom.highlightCurrentNode = function() {
|
|
const currentFile = HermesCustom.getCurrentFile();
|
|
if (!currentFile) return;
|
|
if (typeof window.graphNodeObjects === 'undefined' || !window.graphNodeObjects) {
|
|
setTimeout(HermesCustom.highlightCurrentNode, 500);
|
|
return;
|
|
}
|
|
|
|
const connected = new Set(HermesCustom.getConnectedFiles(currentFile));
|
|
connected.add(currentFile);
|
|
|
|
window.graphNodeObjects.forEach((mesh, nodeId) => {
|
|
const isCurrent = (nodeId === currentFile);
|
|
const isConnected = connected.has(nodeId) && !isCurrent;
|
|
|
|
if (isCurrent) {
|
|
mesh.material.color.setHex(0xFFD700);
|
|
mesh.material.emissive.setHex(0xFFD700);
|
|
mesh.material.emissiveIntensity = 0.6;
|
|
mesh.scale.set(1.5, 1.5, 1.5);
|
|
} else if (isConnected) {
|
|
mesh.material.color.setHex(0x89b4fa);
|
|
mesh.material.emissive.setHex(0x89b4fa);
|
|
mesh.material.emissiveIntensity = 0.3;
|
|
mesh.scale.set(1.1, 1.1, 1.1);
|
|
} else {
|
|
mesh.material.color.setHex(0x313244);
|
|
mesh.material.emissiveIntensity = 0;
|
|
mesh.material.opacity = 0.2;
|
|
mesh.material.transparent = true;
|
|
}
|
|
});
|
|
};
|
|
|
|
// ============================================================
|
|
// 4. Click-to-navigate on 3D Graph
|
|
// ============================================================
|
|
|
|
HermesCustom.setupGraphClickHandler = function() {
|
|
if (!window.graphRenderer || !window.graphScene || !window.graphCamera) {
|
|
setTimeout(HermesCustom.setupGraphClickHandler, 500);
|
|
return;
|
|
}
|
|
|
|
const raycaster = new THREE.Raycaster();
|
|
const mouse = new THREE.Vector2();
|
|
|
|
window.graphRenderer.domElement.addEventListener('click', (event) => {
|
|
const rect = window.graphRenderer.domElement.getBoundingClientRect();
|
|
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
|
|
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
|
|
|
|
raycaster.setFromCamera(mouse, window.graphCamera);
|
|
const intersects = raycaster.intersectObjects(
|
|
Object.values(window.graphNodeObjects || {})
|
|
);
|
|
|
|
if (intersects.length > 0) {
|
|
const nodeId = intersects[0].object.userData.nodeId;
|
|
if (nodeId && typeof window.loadFile === 'function') {
|
|
window.loadFile(nodeId);
|
|
window.history.pushState({}, '', `?file=${encodeURIComponent(nodeId)}`);
|
|
}
|
|
}
|
|
});
|
|
console.log('[HermesCustom] Graph click handler attached');
|
|
};
|
|
|
|
// ============================================================
|
|
// 5. URL-based navigation
|
|
// ============================================================
|
|
|
|
HermesCustom.updateURL = function(path) {
|
|
const newURL = `?file=${encodeURIComponent(path)}`;
|
|
window.history.pushState({}, '', newURL);
|
|
};
|
|
|
|
// ============================================================
|
|
// 6. loadFile hook — track current file
|
|
// ============================================================
|
|
|
|
HermesCustom.hookLoadFile = function() {
|
|
if (typeof window.loadFile !== 'function') {
|
|
setTimeout(HermesCustom.hookLoadFile, 200);
|
|
return;
|
|
}
|
|
if (window.loadFile._hermesPatched) return;
|
|
|
|
const originalLoadFile = window.loadFile;
|
|
window.loadFile = function(path) {
|
|
HermesCustom._lastLoadedFile = path;
|
|
HermesCustom.updateURL(path);
|
|
const result = originalLoadFile.apply(this, arguments);
|
|
setTimeout(HermesCustom.highlightCurrentNode, 300);
|
|
return result;
|
|
};
|
|
window.loadFile._hermesPatched = true;
|
|
console.log('[HermesCustom] loadFile() hooked');
|
|
};
|
|
|
|
// ============================================================
|
|
// 7. Graph data fetch (for highlight logic)
|
|
// ============================================================
|
|
|
|
HermesCustom.loadGraphData = function() {
|
|
fetch('/api/vault/graph')
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
HermesCustom._graphData = data;
|
|
console.log(`[HermesCustom] Loaded graph: ${data.nodes.length} nodes, ${data.edges.length} edges`);
|
|
HermesCustom.highlightCurrentNode();
|
|
})
|
|
.catch(err => console.warn('[HermesCustom] Graph fetch failed:', err));
|
|
};
|
|
|
|
// ============================================================
|
|
// Init
|
|
// ============================================================
|
|
|
|
HermesCustom.init = function() {
|
|
console.log('[HermesCustom] init v2 (responsive-fix)');
|
|
HermesCustom.injectResponsiveCSS();
|
|
HermesCustom.hookLoadFile();
|
|
HermesCustom.applyLayout();
|
|
HermesCustom.setupResizeHandler();
|
|
HermesCustom.loadGraphData();
|
|
|
|
// Graph click handler needs Three.js to be ready
|
|
setTimeout(() => {
|
|
HermesCustom.setupGraphClickHandler();
|
|
}, 1500);
|
|
};
|
|
|
|
// Auto-init
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', HermesCustom.init);
|
|
} else {
|
|
// DOM already ready — but obv's main script may not have run yet.
|
|
// Defer slightly to let obv set up the layout first.
|
|
setTimeout(HermesCustom.init, 50);
|
|
}
|
|
|
|
})();
|