index.html 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Animal.js Documentation</title>
  7. <link rel="stylesheet" href="../xjs.css">
  8. <style>
  9. :root {
  10. --sidebar-bg: #111;
  11. --main-bg: #111;
  12. --border-color: #222;
  13. --text-color: #888;
  14. --text-hover: #ccc;
  15. --active-color: #ff4b4b; /* Red/Orange for active item */
  16. --header-color: #666;
  17. --middle-col-bg: #161616;
  18. --tree-line-color: #333;
  19. --accent-color: #ff9f43;
  20. }
  21. body {
  22. margin: 0;
  23. padding: 0;
  24. height: 100vh;
  25. display: flex;
  26. background: var(--main-bg);
  27. color: var(--text-color);
  28. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  29. overflow: hidden;
  30. font-size: 14px;
  31. }
  32. /* 1. LEFT SIDEBAR - MAIN NAVIGATION */
  33. .sidebar-left {
  34. width: 260px;
  35. background: var(--sidebar-bg);
  36. display: flex;
  37. flex-direction: column;
  38. overflow-y: auto;
  39. flex-shrink: 0;
  40. padding-bottom: 40px;
  41. }
  42. .logo-area {
  43. padding: 24px 20px;
  44. font-size: 18px;
  45. font-weight: bold;
  46. color: #fff;
  47. display: flex;
  48. align-items: center;
  49. gap: 10px;
  50. margin-bottom: 10px;
  51. }
  52. .logo-circle {
  53. width: 10px;
  54. height: 10px;
  55. background: #fff;
  56. border-radius: 50%;
  57. }
  58. /* Tree Structure */
  59. .nav-tree {
  60. padding: 0 20px;
  61. }
  62. .tree-group {
  63. margin-bottom: 5px;
  64. }
  65. .tree-header {
  66. padding: 8px 0;
  67. color: var(--header-color);
  68. font-weight: 600;
  69. cursor: pointer;
  70. transition: color 0.2s;
  71. display: flex;
  72. align-items: center;
  73. justify-content: space-between;
  74. }
  75. .tree-header:hover {
  76. color: var(--text-hover);
  77. }
  78. .tree-header.active-header {
  79. color: #e69f3c; /* Highlight parent if needed, usually just children */
  80. }
  81. .tree-children {
  82. padding-left: 14px; /* Indent */
  83. border-left: 1px solid var(--tree-line-color);
  84. margin-left: 2px;
  85. display: none; /* Collapsed by default */
  86. flex-direction: column;
  87. }
  88. .tree-children.expanded {
  89. display: flex;
  90. }
  91. .nav-item {
  92. display: block;
  93. padding: 6px 0 6px 15px;
  94. color: var(--text-color);
  95. text-decoration: none;
  96. cursor: pointer;
  97. transition: all 0.2s;
  98. position: relative;
  99. }
  100. .nav-item:hover {
  101. color: var(--text-hover);
  102. }
  103. /* Active Item Style (Reference: Red text, maybe red border left overlaying the gray line?) */
  104. .nav-item.active {
  105. color: var(--active-color);
  106. }
  107. /* Optional: Active marker on the left line */
  108. .nav-item.active::before {
  109. content: '';
  110. position: absolute;
  111. left: -1px; /* Overlap the border */
  112. top: 0;
  113. bottom: 0;
  114. width: 2px;
  115. background: var(--active-color);
  116. }
  117. /* Flat menu section title */
  118. .nav-section-title {
  119. margin-top: 18px;
  120. padding: 10px 0 6px 0;
  121. color: #7a7a7a;
  122. font-size: 11px;
  123. font-weight: 800;
  124. letter-spacing: 1px;
  125. text-transform: uppercase;
  126. opacity: 0.9;
  127. }
  128. /* Search Box in sidebar */
  129. .sidebar-search {
  130. margin: 0 20px 20px 20px;
  131. background: #1a1a1a;
  132. border: 1px solid #333;
  133. border-radius: 4px;
  134. padding: 8px 12px;
  135. color: #fff;
  136. font-size: 13px;
  137. cursor: pointer;
  138. }
  139. .sidebar-search:hover {
  140. border-color: #555;
  141. }
  142. /* 2. MIDDLE SIDEBAR */
  143. .sidebar-middle {
  144. width: 320px;
  145. background: var(--middle-col-bg);
  146. display: flex;
  147. flex-direction: column;
  148. overflow: hidden;
  149. flex-shrink: 0;
  150. }
  151. #middle-frame {
  152. width: 100%;
  153. height: 100%;
  154. border: none;
  155. background: transparent;
  156. }
  157. /* 3. RIGHT CONTENT */
  158. .content-right {
  159. flex: 1;
  160. display: flex;
  161. flex-direction: column;
  162. background: #000;
  163. overflow: hidden;
  164. }
  165. #content-frame {
  166. flex: 1;
  167. border: none;
  168. width: 100%;
  169. height: 100%;
  170. }
  171. /* Connection line between middle and right columns */
  172. .column-connection-line {
  173. position: fixed;
  174. inset: 0;
  175. width: 100vw;
  176. height: 100vh;
  177. pointer-events: none;
  178. z-index: 20;
  179. opacity: 0.85;
  180. }
  181. .column-connection-path {
  182. stroke: var(--accent-color);
  183. stroke-width: 2;
  184. stroke-dasharray: 6 6;
  185. stroke-linecap: round;
  186. fill: none;
  187. opacity: 0.35;
  188. animation: dashFlow 1.1s linear infinite;
  189. filter: drop-shadow(0 0 2px rgba(255, 159, 67, 0.25));
  190. }
  191. @keyframes dashFlow {
  192. to { stroke-dashoffset: -12; }
  193. }
  194. </style>
  195. </head>
  196. <body>
  197. <!-- 1. Left Sidebar -->
  198. <div class="sidebar-left">
  199. <div class="logo-area">
  200. <div class="logo-circle"></div>
  201. Animal.js
  202. </div>
  203. <div class="sidebar-search" onclick="selectCategory('search.html', 'test_on_update.html', null)">
  204. Search
  205. </div>
  206. <div class="nav-tree" aria-label="主菜单(扁平化)">
  207. <div class="nav-section-title">Animal.js 动画</div>
  208. <div class="nav-item active" data-list="targets/list.html" data-default="targets/overview.html" onclick="selectCategory('targets/list.html', 'targets/overview.html', this)">目标 Targets</div>
  209. <div class="nav-item" data-list="animatable_properties/list.html" data-default="animatable_properties/test_transforms.html" onclick="selectCategory('animatable_properties/list.html', 'animatable_properties/test_transforms.html', this)">可动画属性 Properties</div>
  210. <div class="nav-item" data-list="tween_value_types/list.html" data-default="tween_value_types/test_numerical.html" onclick="selectCategory('tween_value_types/list.html', 'tween_value_types/test_numerical.html', this)">补间值类型 Tween 值</div>
  211. <div class="nav-item" data-list="tween_parameters/list.html" data-default="tween_parameters/overview.html" onclick="selectCategory('tween_parameters/list.html', 'tween_parameters/overview.html', this)">补间参数 Tween 参数</div>
  212. <div class="nav-item" data-list="svg/list.html" data-default="svg/overview.html" onclick="selectCategory('svg/list.html', 'svg/overview.html', this)">SVG 动画</div>
  213. <div class="nav-item" data-list="list_callbacks.html" data-default="test_on_update.html" onclick="selectCategory('list_callbacks.html', 'test_on_update.html', this)">回调 Callbacks</div>
  214. <div class="nav-item" data-list="examples/list.html" data-default="examples/controls.html" onclick="selectCategory('examples/list.html', 'examples/controls.html', this)">示例 Examples</div>
  215. <div class="nav-section-title">Layer 弹窗</div>
  216. <div class="tree-group" data-tree="layer">
  217. <div class="tree-header" onclick="toggleTree(this)">Layer 子菜单</div>
  218. <div class="tree-children expanded">
  219. <div class="nav-item" data-list="layer/list.html" data-default="layer/overview.html" onclick="selectCategory('layer/list.html', 'layer/overview.html', this)">Layer API / Tests</div>
  220. <div class="nav-item" data-list="layer/custom_list.html" data-default="layer/test_custom_animation.html" onclick="selectCategory('layer/custom_list.html', 'layer/test_custom_animation.html', this)">自定义动画</div>
  221. <div class="nav-item" data-list="examples/list.html" data-default="examples/layer.html" onclick="selectCategory('examples/list.html', 'examples/layer.html', this)">交互示例(Layer + 动画)</div>
  222. </div>
  223. </div>
  224. <div class="nav-section-title">开发者</div>
  225. <div class="nav-item" data-list="search.html" data-default="module.html" onclick="selectCategory('search.html', 'module.html', this)">模块开发与测试指南</div>
  226. </div>
  227. </div>
  228. <!-- 2. Middle Sidebar -->
  229. <div class="sidebar-middle">
  230. <iframe id="middle-frame" src="targets/list.html"></iframe>
  231. </div>
  232. <!-- 3. Right Content -->
  233. <div class="content-right">
  234. <iframe id="content-frame" src="targets/test_css_selector.html"></iframe>
  235. </div>
  236. <!-- SVG overlay for the middle→right connection line -->
  237. <svg id="column-connection-line" class="column-connection-line" aria-hidden="true">
  238. <path id="column-connection-path" class="column-connection-path" d=""></path>
  239. </svg>
  240. <script>
  241. // Bump this when you want to force-refresh iframe pages
  242. const DOC_CACHE_VERSION = '2';
  243. // =========================================================
  244. // URL state (hash routing)
  245. // - When navigating: write selected content page into URL: #p=<encoded>
  246. // - On refresh / back-forward: restore the same content page + sync left/middle
  247. // =========================================================
  248. let _docApplyingRoute = false;
  249. function ensureHtmlPath(u) {
  250. const n = normalizeDocUrl(u);
  251. if (!n) return null;
  252. return n.endsWith('.html') ? n : (n + '.html');
  253. }
  254. // New readable token:
  255. // - omit ".html"
  256. // - "/" becomes "_" (separator)
  257. // - existing "_" becomes "__" (escape), so it's reversible
  258. function routeTokenFromContentUrl(contentUrl) {
  259. let u = normalizeDocUrl(contentUrl);
  260. if (!u) return '';
  261. if (u.endsWith('.html')) u = u.slice(0, -5);
  262. // escape "_" first, then turn "/" into "_"
  263. return u.replace(/_/g, '__').replace(/\//g, '_');
  264. }
  265. function contentUrlFromRouteToken(token) {
  266. let t = String(token || '');
  267. if (!t) return null;
  268. // In case someone pasted an encoded token, be tolerant.
  269. try { t = decodeURIComponent(t); } catch {}
  270. // If it already looks like a path (legacy), keep it.
  271. if (t.includes('/')) return ensureHtmlPath(t);
  272. // Decode: "_" => "/", "__" => "_"
  273. let out = '';
  274. for (let i = 0; i < t.length; i++) {
  275. const ch = t[i];
  276. if (ch === '_') {
  277. if (t[i + 1] === '_') { out += '_'; i++; }
  278. else out += '/';
  279. } else {
  280. out += ch;
  281. }
  282. }
  283. return ensureHtmlPath(out);
  284. }
  285. function getRouteContentUrl() {
  286. const h = String(location.hash || '');
  287. if (!h) return null;
  288. const hash = h.startsWith('#') ? h.slice(1) : h;
  289. // Legacy-friendly:
  290. // - "#p=<encoded path>"
  291. // - "#/path/to/page.html"
  292. // New:
  293. // - "#layer_overview" / "#layer_test__confirm__flow"
  294. if (hash.startsWith('/')) {
  295. const p = hash.slice(1);
  296. return p ? ensureHtmlPath(p) : null;
  297. }
  298. if (hash.startsWith('p=')) {
  299. const params = new URLSearchParams(hash);
  300. const p = params.get('p');
  301. return p ? ensureHtmlPath(p) : null;
  302. }
  303. return contentUrlFromRouteToken(hash);
  304. }
  305. function setRouteContentUrl(contentUrl, { replace = false } = {}) {
  306. const token = routeTokenFromContentUrl(contentUrl);
  307. if (!token) return;
  308. const nextHash = '#' + token;
  309. if (location.hash === nextHash) return;
  310. if (replace) history.replaceState(null, '', nextHash);
  311. else location.hash = nextHash;
  312. }
  313. function baseDocPath(url) {
  314. return normalizeDocUrl(url);
  315. }
  316. function guessListUrlByContent(contentUrl) {
  317. const u = normalizeDocUrl(contentUrl);
  318. if (!u) return null;
  319. if (u === 'module.html' || u === 'search.html') return 'search.html';
  320. if (u === 'test_on_update.html') return 'list_callbacks.html';
  321. if (u.startsWith('targets/')) return 'targets/list.html';
  322. if (u.startsWith('animatable_properties/')) return 'animatable_properties/list.html';
  323. if (u.startsWith('tween_value_types/')) return 'tween_value_types/list.html';
  324. if (u.startsWith('tween_parameters/')) return 'tween_parameters/list.html';
  325. if (u.startsWith('svg/')) return 'svg/list.html';
  326. if (u === 'layer/test_custom_animation.html') return 'layer/custom_list.html';
  327. if (u === 'layer/test_custom_animation_banner.html') return 'layer/custom_list.html';
  328. if (u === 'layer/test_custom_animation_ballsorting.html') return 'layer/custom_list.html';
  329. if (u.startsWith('layer/')) return 'layer/list.html';
  330. if (u.startsWith('examples/')) return 'examples/list.html';
  331. return null;
  332. }
  333. function syncLeftActiveByListUrl(listUrl, contentUrl) {
  334. const u = normalizeDocUrl(listUrl);
  335. if (!u) return;
  336. const items = Array.from(document.querySelectorAll('.nav-item'));
  337. items.forEach(i => i.classList.remove('active'));
  338. // Prefer exact (list + default) match, then fall back to first list match
  339. const normContent = normalizeDocUrl(contentUrl);
  340. const exact = items.find(i =>
  341. normalizeDocUrl(i.getAttribute('data-list')) === u &&
  342. normalizeDocUrl(i.getAttribute('data-default')) === normContent
  343. );
  344. const any = items.find(i => normalizeDocUrl(i.getAttribute('data-list')) === u);
  345. (exact || any)?.classList.add('active');
  346. syncNavTreeState();
  347. }
  348. function ensureCategoryForContent(contentUrl) {
  349. const listUrl = guessListUrlByContent(contentUrl);
  350. if (!listUrl) return;
  351. // Left highlight
  352. syncLeftActiveByListUrl(listUrl, contentUrl);
  353. // Middle list
  354. const middleFrame = document.getElementById('middle-frame');
  355. if (middleFrame) {
  356. const current = baseDocPath(middleFrame.getAttribute('src') || '');
  357. if (current !== normalizeDocUrl(listUrl)) {
  358. middleFrame.src = withDocVersion(listUrl);
  359. }
  360. }
  361. }
  362. function selectCategory(listUrl, defaultContentUrl, el) {
  363. // 1. Highlight Nav Item
  364. document.querySelectorAll('.nav-item').forEach(item => item.classList.remove('active'));
  365. if (el) el.classList.add('active');
  366. syncNavTreeState();
  367. // 2. Load Middle Column (List)
  368. const middleFrame = document.getElementById('middle-frame');
  369. const listUrlV = withDocVersion(listUrl);
  370. if (middleFrame.getAttribute('src') !== listUrlV) {
  371. middleFrame.src = listUrlV;
  372. }
  373. // 3. Load Right Column (Default Content)
  374. loadContent(defaultContentUrl, { updateRoute: true });
  375. }
  376. function toggleTree(headerEl) {
  377. if (!headerEl) return;
  378. const next = headerEl.nextElementSibling;
  379. if (!next || !next.classList.contains('tree-children')) return;
  380. next.classList.toggle('expanded');
  381. const isExpanded = next.classList.contains('expanded');
  382. headerEl.classList.toggle('active-header', isExpanded);
  383. }
  384. function syncNavTreeState() {
  385. try {
  386. const active = document.querySelector('.nav-item.active');
  387. const children = active?.closest('.tree-children');
  388. const header = children ? children.previousElementSibling : null;
  389. document.querySelectorAll('.tree-children').forEach(c => c.classList.remove('expanded'));
  390. document.querySelectorAll('.tree-header').forEach(h => h.classList.remove('active-header'));
  391. if (children) {
  392. children.classList.add('expanded');
  393. if (header && header.classList.contains('tree-header')) {
  394. header.classList.add('active-header');
  395. }
  396. }
  397. } catch {}
  398. }
  399. function withDocVersion(url) {
  400. const u = String(url || '');
  401. if (!u) return u;
  402. // keep explicit query params if present
  403. if (u.includes('?')) return u;
  404. return u + '?v=' + encodeURIComponent(DOC_CACHE_VERSION);
  405. }
  406. function loadContent(url, opts = {}) {
  407. const { updateRoute = true, replaceRoute = false } = opts || {};
  408. const contentFrame = document.getElementById('content-frame');
  409. const urlV = withDocVersion(url);
  410. if (contentFrame.getAttribute('src') !== urlV) {
  411. contentFrame.src = urlV;
  412. }
  413. // Keep middle list selection in sync when possible
  414. try {
  415. setMiddleActive(url);
  416. } catch {}
  417. // Keep left + middle category consistent even if navigation came from middle list / prev-next
  418. try {
  419. ensureCategoryForContent(url);
  420. } catch {}
  421. // Persist current selection in URL (so refresh stays on same page)
  422. if (updateRoute) {
  423. try { setRouteContentUrl(url, { replace: !!replaceRoute }); } catch {}
  424. }
  425. // Update connection line after navigation
  426. scheduleConnectionLineUpdate();
  427. }
  428. // Called by content iframe pages (and also internally after navigation)
  429. function setMiddleActive(contentUrl) {
  430. const middleFrame = document.getElementById('middle-frame');
  431. if (!middleFrame) return;
  432. const win = middleFrame.contentWindow;
  433. if (win && typeof win.setActiveByContentUrl === 'function') {
  434. win.setActiveByContentUrl(contentUrl);
  435. }
  436. // Update connection line when active item changes
  437. scheduleConnectionLineUpdate();
  438. }
  439. // =========================================================
  440. // Global Prev/Next(组内顺序 + 跨组跳转,由路由表统一驱动)
  441. // =========================================================
  442. const DOC_PAGES = [
  443. // Targets
  444. { group: 'Targets', title: 'Targets 概览', url: 'targets/overview.html' },
  445. { group: 'Targets', title: 'CSS Selector', url: 'targets/test_css_selector.html' },
  446. { group: 'Targets', title: 'DOM Elements', url: 'targets/test_dom_elements.html' },
  447. { group: 'Targets', title: 'JavaScript Objects', url: 'targets/test_js_objects.html' },
  448. { group: 'Targets', title: 'Array of targets', url: 'targets/test_array_targets.html' },
  449. // Animatable properties
  450. { group: 'Properties', title: 'Transforms', url: 'animatable_properties/test_transforms.html' },
  451. { group: 'Properties', title: 'CSS Properties', url: 'animatable_properties/test_css_props.html' },
  452. { group: 'Properties', title: 'CSS Variables', url: 'animatable_properties/test_css_vars.html' },
  453. { group: 'Properties', title: 'JS Properties', url: 'animatable_properties/test_js_props.html' },
  454. // Tween value types
  455. { group: 'Tween 值', title: 'Numerical', url: 'tween_value_types/test_numerical.html' },
  456. { group: 'Tween 值', title: 'Unit', url: 'tween_value_types/test_unit.html' },
  457. { group: 'Tween 值', title: 'Relative', url: 'tween_value_types/test_relative.html' },
  458. { group: 'Tween 值', title: 'Colors', url: 'tween_value_types/test_colors.html' },
  459. // Tween parameters
  460. { group: 'Tween 参数', title: 'Tween parameters 概览', url: 'tween_parameters/overview.html' },
  461. { group: 'Tween 参数', title: 'duration / delay', url: 'tween_parameters/test_duration_delay.html' },
  462. // SVG
  463. { group: 'SVG', title: 'SVG 概览', url: 'svg/overview.html' },
  464. { group: 'SVG', title: 'draw() 路径描边', url: 'svg/test_draw_path.html' },
  465. { group: 'SVG', title: 'Motion Path', url: 'svg/test_motion_path.html' },
  466. { group: 'SVG', title: 'SVG 属性动画', url: 'svg/test_svg_attributes.html' },
  467. { group: 'SVG', title: 'SVG transform', url: 'svg/test_svg_transforms.html' },
  468. { group: 'SVG', title: 'Morph(points)', url: 'svg/test_morph_points.html' },
  469. { group: 'SVG', title: 'Morph(path d)', url: 'svg/test_morph_path.html' },
  470. // Callbacks
  471. { group: 'Callbacks', title: 'onUpdate', url: 'test_on_update.html' },
  472. // Examples
  473. { group: 'Examples', title: 'Controls', url: 'examples/controls.html' },
  474. { group: 'Examples', title: 'Layer', url: 'examples/layer.html' },
  475. // Layer
  476. { group: 'Layer', title: 'Layer 概览', url: 'layer/overview.html' },
  477. { group: 'Layer', title: '自定义动画', url: 'layer/test_custom_animation.html' },
  478. { group: 'Layer', title: '自定义动画(Banner)', url: 'layer/test_custom_animation_banner.html' },
  479. { group: 'Layer', title: '自定义动画(BallSorting)', url: 'layer/test_custom_animation_ballsorting.html' },
  480. { group: 'Layer', title: 'SVG Icon animation', url: 'layer/test_icons_svg_animation.html' },
  481. { group: 'Layer', title: 'Confirm flow', url: 'layer/test_confirm_flow.html' },
  482. { group: 'Layer', title: 'Selection.layer + dataset', url: 'layer/test_selection_layer_dataset.html' },
  483. { group: 'Layer', title: 'Static API / Builder API', url: 'layer/test_static_fire.html' },
  484. { group: 'Layer', title: 'DOM content + Step flow', url: 'layer/test_dom_steps.html' },
  485. // Developer
  486. { group: 'Developer', title: '模块开发与测试指南', url: 'module.html' }
  487. ];
  488. function normalizeDocUrl(url) {
  489. const raw = String(url || '');
  490. const noHash = raw.split('#')[0];
  491. const noQuery = noHash.split('?')[0];
  492. return String(noQuery || '')
  493. .replace(/^[#/]+/, '')
  494. .replace(/^\.\//, '')
  495. .replace(/^\/+/, '');
  496. }
  497. function docFindIndex(currentUrl) {
  498. const u = normalizeDocUrl(currentUrl);
  499. if (!u) return -1;
  500. return DOC_PAGES.findIndex(p => u === p.url || u.endsWith(p.url));
  501. }
  502. function docGetPrevNext(currentUrl) {
  503. const idx = docFindIndex(currentUrl);
  504. if (idx < 0) return { idx: -1, prev: null, next: null, current: null };
  505. const prev = (idx > 0) ? DOC_PAGES[idx - 1] : null;
  506. const next = (idx < DOC_PAGES.length - 1) ? DOC_PAGES[idx + 1] : null;
  507. return { idx, prev, next, current: DOC_PAGES[idx] };
  508. }
  509. function docNavigatePrev(currentUrl) {
  510. const { prev } = docGetPrevNext(currentUrl);
  511. if (prev) loadContent(prev.url);
  512. }
  513. function docNavigateNext(currentUrl) {
  514. const { next } = docGetPrevNext(currentUrl);
  515. if (next) loadContent(next.url);
  516. }
  517. // Expose to iframes (same origin)
  518. window.docGetPrevNext = docGetPrevNext;
  519. window.docNavigatePrev = docNavigatePrev;
  520. window.docNavigateNext = docNavigateNext;
  521. // =============================
  522. // Middle ↔ Right connection line
  523. // =============================
  524. const connectionSvg = document.getElementById('column-connection-line');
  525. const connectionPath = document.getElementById('column-connection-path');
  526. let _connRaf = 0;
  527. function scheduleConnectionLineUpdate() {
  528. if (_connRaf) return;
  529. _connRaf = requestAnimationFrame(() => {
  530. _connRaf = 0;
  531. updateConnectionLine();
  532. });
  533. }
  534. function getActiveElementInFrame(frameEl) {
  535. try {
  536. const doc = frameEl?.contentDocument;
  537. if (!doc) return null;
  538. // Middle list pages use .card.active / .header-card.active; fall back to .active
  539. return (
  540. doc.querySelector('.card.active') ||
  541. doc.querySelector('.header-card.active') ||
  542. doc.querySelector('.nav-item.active') ||
  543. doc.querySelector('.active')
  544. );
  545. } catch {
  546. return null;
  547. }
  548. }
  549. function getAnchorElementInContentFrame(frameEl) {
  550. try {
  551. const doc = frameEl?.contentDocument;
  552. if (!doc) return null;
  553. // Prefer a *visible* code area anchor (in-viewport), then fall back
  554. const candidates = [
  555. '.box-container .box-header',
  556. '.code-view.active, .html-view.active, .css-view.active',
  557. '.box-container',
  558. 'h1',
  559. '.container',
  560. 'body'
  561. ];
  562. for (const sel of candidates) {
  563. const el = doc.querySelector(sel);
  564. if (!el) continue;
  565. // If it's visible in the iframe viewport, use it; else keep looking
  566. const r = el.getBoundingClientRect();
  567. const vh = frameEl?.clientHeight || 0;
  568. if (!vh) return el;
  569. const visibleTop = Math.max(0, r.top);
  570. const visibleBottom = Math.min(vh, r.bottom);
  571. if (visibleBottom - visibleTop > 8) return el;
  572. }
  573. return doc.body;
  574. } catch {
  575. return null;
  576. }
  577. }
  578. function clamp(n, min, max) {
  579. return Math.min(Math.max(n, min), max);
  580. }
  581. function getVisibleCenterInFrame(frameEl, elRect) {
  582. const w = frameEl?.clientWidth || 0;
  583. const h = frameEl?.clientHeight || 0;
  584. if (!w || !h) return null;
  585. const visibleLeft = Math.max(0, elRect.left);
  586. const visibleRight = Math.min(w, elRect.right);
  587. const visibleTop = Math.max(0, elRect.top);
  588. const visibleBottom = Math.min(h, elRect.bottom);
  589. if (visibleRight - visibleLeft <= 1 || visibleBottom - visibleTop <= 1) {
  590. return null;
  591. }
  592. return {
  593. x: (visibleLeft + visibleRight) / 2,
  594. y: (visibleTop + visibleBottom) / 2
  595. };
  596. }
  597. function updateConnectionLine() {
  598. if (!connectionSvg || !connectionPath) return;
  599. const middleFrame = document.getElementById('middle-frame');
  600. const contentFrame = document.getElementById('content-frame');
  601. if (!middleFrame || !contentFrame) return;
  602. const midRect = middleFrame.getBoundingClientRect();
  603. const rightRect = contentFrame.getBoundingClientRect();
  604. const active = getActiveElementInFrame(middleFrame);
  605. const anchor = getAnchorElementInContentFrame(contentFrame);
  606. if (!active || !anchor) {
  607. connectionPath.setAttribute('d', '');
  608. connectionSvg.style.opacity = '0';
  609. return;
  610. }
  611. const activeRect = active.getBoundingClientRect();
  612. const anchorRect = anchor.getBoundingClientRect();
  613. // Use the *visible* part of the element within its iframe viewport
  614. const activeCenter = getVisibleCenterInFrame(middleFrame, activeRect);
  615. const anchorCenter = getVisibleCenterInFrame(contentFrame, anchorRect);
  616. if (!activeCenter || !anchorCenter) {
  617. // If either side isn't visible in its iframe, hide the line (prevents weird drifting)
  618. connectionPath.setAttribute('d', '');
  619. connectionSvg.style.opacity = '0';
  620. return;
  621. }
  622. // Convert iframe-viewport coordinates to parent viewport coordinates
  623. const startX = midRect.left + clamp(activeRect.right, 0, middleFrame.clientWidth);
  624. const startY = midRect.top + activeCenter.y;
  625. // Stop just before the content block (avoid entering the dark code area)
  626. const endX = rightRect.left + clamp(anchorRect.left - 2, 6, contentFrame.clientWidth - 12);
  627. const endY = rightRect.top + anchorCenter.y;
  628. // Draw an L-shaped polyline.
  629. // Keep the vertical segment aligned with the column divider (middle ↔ right).
  630. const dividerX = rightRect.left + 0.5; // half-pixel for crisper stroke alignment
  631. const x1 = Math.max(startX + 12, dividerX);
  632. const d = [
  633. `M ${startX} ${startY}`,
  634. `L ${x1} ${startY}`,
  635. `L ${x1} ${endY}`,
  636. `L ${endX} ${endY}`
  637. ].join(' ');
  638. connectionPath.setAttribute('d', d);
  639. connectionSvg.style.opacity = '0.85';
  640. }
  641. function attachFrameScrollListeners(frameEl) {
  642. if (!frameEl) return;
  643. const onScroll = () => {
  644. markFrameScrolling(frameEl);
  645. scheduleConnectionLineUpdate();
  646. };
  647. try {
  648. // Window scroll inside iframe
  649. frameEl.contentWindow?.addEventListener('scroll', onScroll, { passive: true });
  650. // Some pages scroll on document/body; capture to be safe
  651. frameEl.contentDocument?.addEventListener('scroll', onScroll, true);
  652. frameEl.contentDocument?.documentElement?.addEventListener?.('scroll', onScroll, { passive: true });
  653. frameEl.contentDocument?.body?.addEventListener?.('scroll', onScroll, { passive: true });
  654. } catch {}
  655. }
  656. const _scrollHideTimers = new WeakMap();
  657. function markFrameScrolling(frameEl) {
  658. try {
  659. const doc = frameEl?.contentDocument;
  660. if (!doc) return;
  661. doc.documentElement?.classList.add('scrolling');
  662. doc.body?.classList.add('scrolling');
  663. const prev = _scrollHideTimers.get(frameEl);
  664. if (prev) clearTimeout(prev);
  665. _scrollHideTimers.set(frameEl, setTimeout(() => {
  666. try {
  667. doc.documentElement?.classList.remove('scrolling');
  668. doc.body?.classList.remove('scrolling');
  669. } catch {}
  670. }, 500));
  671. } catch {}
  672. }
  673. // Always-on lightweight loop to keep the line aligned even
  674. // if iframe scroll events were missed due to timing.
  675. let _connLoopStarted = false;
  676. function startConnectionLineLoop() {
  677. if (_connLoopStarted) return;
  678. _connLoopStarted = true;
  679. const loop = () => {
  680. // Only bother when we have a path (avoids work when hidden)
  681. if (connectionPath?.getAttribute('d')) {
  682. scheduleConnectionLineUpdate();
  683. }
  684. requestAnimationFrame(loop);
  685. };
  686. requestAnimationFrame(loop);
  687. }
  688. // Make initial active state robust against iframe load ordering
  689. window.addEventListener('load', () => {
  690. // Restore route (if present) before doing any extra syncing
  691. try {
  692. const routeUrl = getRouteContentUrl();
  693. if (routeUrl) {
  694. _docApplyingRoute = true;
  695. ensureCategoryForContent(routeUrl);
  696. loadContent(routeUrl, { updateRoute: false });
  697. } else {
  698. // No route: persist current default content into URL (so first refresh is stable)
  699. const current = baseDocPath(document.getElementById('content-frame')?.getAttribute('src') || '');
  700. if (current) setRouteContentUrl(current, { replace: true });
  701. }
  702. } catch {}
  703. _docApplyingRoute = false;
  704. const contentFrame = document.getElementById('content-frame');
  705. const src = contentFrame?.getAttribute('src');
  706. if (src) setMiddleActive(src);
  707. const middleFrame = document.getElementById('middle-frame');
  708. if (middleFrame) {
  709. middleFrame.addEventListener('load', () => {
  710. const current = document.getElementById('content-frame')?.getAttribute('src');
  711. if (current) setMiddleActive(current);
  712. attachFrameScrollListeners(middleFrame);
  713. scheduleConnectionLineUpdate();
  714. });
  715. }
  716. if (contentFrame) {
  717. contentFrame.addEventListener('load', () => {
  718. const current = contentFrame.getAttribute('src');
  719. if (current) setMiddleActive(current);
  720. attachFrameScrollListeners(contentFrame);
  721. scheduleConnectionLineUpdate();
  722. });
  723. }
  724. // If the iframes were already loaded before we attached 'load' listeners,
  725. // still attach scroll listeners immediately.
  726. attachFrameScrollListeners(middleFrame);
  727. attachFrameScrollListeners(contentFrame);
  728. window.addEventListener('resize', scheduleConnectionLineUpdate, { passive: true });
  729. scheduleConnectionLineUpdate();
  730. startConnectionLineLoop();
  731. });
  732. // Back/forward + manual hash edits: restore selection
  733. window.addEventListener('hashchange', () => {
  734. if (_docApplyingRoute) return;
  735. const routeUrl = getRouteContentUrl();
  736. if (!routeUrl) return;
  737. _docApplyingRoute = true;
  738. try {
  739. ensureCategoryForContent(routeUrl);
  740. loadContent(routeUrl, { updateRoute: false });
  741. } finally {
  742. _docApplyingRoute = false;
  743. }
  744. });
  745. </script>
  746. </body>
  747. </html>