index.html 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 === 'layer/test_custom_animation_background.html') return 'layer/custom_list.html';
  330. if (u === 'layer/test_custom_animation_container_steps.html') return 'layer/custom_list.html';
  331. if (u.startsWith('layer/')) return 'layer/list.html';
  332. if (u.startsWith('examples/')) return 'examples/list.html';
  333. return null;
  334. }
  335. function syncLeftActiveByListUrl(listUrl, contentUrl) {
  336. const u = normalizeDocUrl(listUrl);
  337. if (!u) return;
  338. const items = Array.from(document.querySelectorAll('.nav-item'));
  339. items.forEach(i => i.classList.remove('active'));
  340. // Prefer exact (list + default) match, then fall back to first list match
  341. const normContent = normalizeDocUrl(contentUrl);
  342. const exact = items.find(i =>
  343. normalizeDocUrl(i.getAttribute('data-list')) === u &&
  344. normalizeDocUrl(i.getAttribute('data-default')) === normContent
  345. );
  346. const any = items.find(i => normalizeDocUrl(i.getAttribute('data-list')) === u);
  347. (exact || any)?.classList.add('active');
  348. syncNavTreeState();
  349. }
  350. function ensureCategoryForContent(contentUrl) {
  351. const listUrl = guessListUrlByContent(contentUrl);
  352. if (!listUrl) return;
  353. // Left highlight
  354. syncLeftActiveByListUrl(listUrl, contentUrl);
  355. // Middle list
  356. const middleFrame = document.getElementById('middle-frame');
  357. if (middleFrame) {
  358. const current = baseDocPath(middleFrame.getAttribute('src') || '');
  359. if (current !== normalizeDocUrl(listUrl)) {
  360. middleFrame.src = withDocVersion(listUrl);
  361. }
  362. }
  363. }
  364. function selectCategory(listUrl, defaultContentUrl, el) {
  365. // 1. Highlight Nav Item
  366. document.querySelectorAll('.nav-item').forEach(item => item.classList.remove('active'));
  367. if (el) el.classList.add('active');
  368. syncNavTreeState();
  369. // 2. Load Middle Column (List)
  370. const middleFrame = document.getElementById('middle-frame');
  371. const listUrlV = withDocVersion(listUrl);
  372. if (middleFrame.getAttribute('src') !== listUrlV) {
  373. middleFrame.src = listUrlV;
  374. }
  375. // 3. Load Right Column (Default Content)
  376. loadContent(defaultContentUrl, { updateRoute: true });
  377. }
  378. function toggleTree(headerEl) {
  379. if (!headerEl) return;
  380. const next = headerEl.nextElementSibling;
  381. if (!next || !next.classList.contains('tree-children')) return;
  382. next.classList.toggle('expanded');
  383. const isExpanded = next.classList.contains('expanded');
  384. headerEl.classList.toggle('active-header', isExpanded);
  385. }
  386. function syncNavTreeState() {
  387. try {
  388. const active = document.querySelector('.nav-item.active');
  389. const children = active?.closest('.tree-children');
  390. const header = children ? children.previousElementSibling : null;
  391. document.querySelectorAll('.tree-children').forEach(c => c.classList.remove('expanded'));
  392. document.querySelectorAll('.tree-header').forEach(h => h.classList.remove('active-header'));
  393. if (children) {
  394. children.classList.add('expanded');
  395. if (header && header.classList.contains('tree-header')) {
  396. header.classList.add('active-header');
  397. }
  398. }
  399. } catch {}
  400. }
  401. function withDocVersion(url) {
  402. const u = String(url || '');
  403. if (!u) return u;
  404. // keep explicit query params if present
  405. if (u.includes('?')) return u;
  406. return u + '?v=' + encodeURIComponent(DOC_CACHE_VERSION);
  407. }
  408. function loadContent(url, opts = {}) {
  409. const { updateRoute = true, replaceRoute = false } = opts || {};
  410. const contentFrame = document.getElementById('content-frame');
  411. const urlV = withDocVersion(url);
  412. if (contentFrame.getAttribute('src') !== urlV) {
  413. contentFrame.src = urlV;
  414. }
  415. // Keep middle list selection in sync when possible
  416. try {
  417. setMiddleActive(url);
  418. } catch {}
  419. // Keep left + middle category consistent even if navigation came from middle list / prev-next
  420. try {
  421. ensureCategoryForContent(url);
  422. } catch {}
  423. // Persist current selection in URL (so refresh stays on same page)
  424. if (updateRoute) {
  425. try { setRouteContentUrl(url, { replace: !!replaceRoute }); } catch {}
  426. }
  427. // Update connection line after navigation
  428. scheduleConnectionLineUpdate();
  429. }
  430. // Called by content iframe pages (and also internally after navigation)
  431. function setMiddleActive(contentUrl) {
  432. const middleFrame = document.getElementById('middle-frame');
  433. if (!middleFrame) return;
  434. const win = middleFrame.contentWindow;
  435. if (win && typeof win.setActiveByContentUrl === 'function') {
  436. win.setActiveByContentUrl(contentUrl);
  437. }
  438. // Update connection line when active item changes
  439. scheduleConnectionLineUpdate();
  440. }
  441. // =========================================================
  442. // Global Prev/Next(组内顺序 + 跨组跳转,由路由表统一驱动)
  443. // =========================================================
  444. const DOC_PAGES = [
  445. // Targets
  446. { group: 'Targets', title: 'Targets 概览', url: 'targets/overview.html' },
  447. { group: 'Targets', title: 'CSS Selector', url: 'targets/test_css_selector.html' },
  448. { group: 'Targets', title: 'DOM Elements', url: 'targets/test_dom_elements.html' },
  449. { group: 'Targets', title: 'JavaScript Objects', url: 'targets/test_js_objects.html' },
  450. { group: 'Targets', title: 'Array of targets', url: 'targets/test_array_targets.html' },
  451. // Animatable properties
  452. { group: 'Properties', title: 'Transforms', url: 'animatable_properties/test_transforms.html' },
  453. { group: 'Properties', title: 'CSS Properties', url: 'animatable_properties/test_css_props.html' },
  454. { group: 'Properties', title: 'CSS Variables', url: 'animatable_properties/test_css_vars.html' },
  455. { group: 'Properties', title: 'JS Properties', url: 'animatable_properties/test_js_props.html' },
  456. // Tween value types
  457. { group: 'Tween 值', title: 'Numerical', url: 'tween_value_types/test_numerical.html' },
  458. { group: 'Tween 值', title: 'Unit', url: 'tween_value_types/test_unit.html' },
  459. { group: 'Tween 值', title: 'Relative', url: 'tween_value_types/test_relative.html' },
  460. { group: 'Tween 值', title: 'Colors', url: 'tween_value_types/test_colors.html' },
  461. // Tween parameters
  462. { group: 'Tween 参数', title: 'Tween parameters 概览', url: 'tween_parameters/overview.html' },
  463. { group: 'Tween 参数', title: 'duration / delay', url: 'tween_parameters/test_duration_delay.html' },
  464. // SVG
  465. { group: 'SVG', title: 'SVG 概览', url: 'svg/overview.html' },
  466. { group: 'SVG', title: 'draw() 路径描边', url: 'svg/test_draw_path.html' },
  467. { group: 'SVG', title: 'Motion Path', url: 'svg/test_motion_path.html' },
  468. { group: 'SVG', title: 'SVG 属性动画', url: 'svg/test_svg_attributes.html' },
  469. { group: 'SVG', title: 'SVG transform', url: 'svg/test_svg_transforms.html' },
  470. { group: 'SVG', title: 'Morph(points)', url: 'svg/test_morph_points.html' },
  471. { group: 'SVG', title: 'Morph(path d)', url: 'svg/test_morph_path.html' },
  472. // Callbacks
  473. { group: 'Callbacks', title: 'onUpdate', url: 'test_on_update.html' },
  474. // Examples
  475. { group: 'Examples', title: 'Controls', url: 'examples/controls.html' },
  476. { group: 'Examples', title: 'Layer', url: 'examples/layer.html' },
  477. // Layer
  478. { group: 'Layer', title: 'Layer 概览', url: 'layer/overview.html' },
  479. { group: 'Layer', title: '自定义动画', url: 'layer/test_custom_animation.html' },
  480. { group: 'Layer', title: '自定义动画(Banner)', url: 'layer/test_custom_animation_banner.html' },
  481. { group: 'Layer', title: '自定义动画(BallSorting)', url: 'layer/test_custom_animation_ballsorting.html' },
  482. { group: 'Layer', title: '背景动画', url: 'layer/test_custom_animation_background.html' },
  483. { group: 'Layer', title: '容器定义混合类型动画', url: 'layer/test_custom_animation_container_steps.html' },
  484. { group: 'Layer', title: 'SVG Icon animation', url: 'layer/test_icons_svg_animation.html' },
  485. { group: 'Layer', title: 'Confirm flow', url: 'layer/test_confirm_flow.html' },
  486. { group: 'Layer', title: 'Selection.layer + dataset', url: 'layer/test_selection_layer_dataset.html' },
  487. { group: 'Layer', title: 'Static API / Builder API', url: 'layer/test_static_fire.html' },
  488. { group: 'Layer', title: 'DOM content + Step flow', url: 'layer/test_dom_steps.html' },
  489. // Developer
  490. { group: 'Developer', title: '模块开发与测试指南', url: 'module.html' }
  491. ];
  492. function normalizeDocUrl(url) {
  493. const raw = String(url || '');
  494. const noHash = raw.split('#')[0];
  495. const noQuery = noHash.split('?')[0];
  496. return String(noQuery || '')
  497. .replace(/^[#/]+/, '')
  498. .replace(/^\.\//, '')
  499. .replace(/^\/+/, '');
  500. }
  501. function docFindIndex(currentUrl) {
  502. const u = normalizeDocUrl(currentUrl);
  503. if (!u) return -1;
  504. return DOC_PAGES.findIndex(p => u === p.url || u.endsWith(p.url));
  505. }
  506. function docGetPrevNext(currentUrl) {
  507. const idx = docFindIndex(currentUrl);
  508. if (idx < 0) return { idx: -1, prev: null, next: null, current: null };
  509. const prev = (idx > 0) ? DOC_PAGES[idx - 1] : null;
  510. const next = (idx < DOC_PAGES.length - 1) ? DOC_PAGES[idx + 1] : null;
  511. return { idx, prev, next, current: DOC_PAGES[idx] };
  512. }
  513. function docNavigatePrev(currentUrl) {
  514. const { prev } = docGetPrevNext(currentUrl);
  515. if (prev) loadContent(prev.url);
  516. }
  517. function docNavigateNext(currentUrl) {
  518. const { next } = docGetPrevNext(currentUrl);
  519. if (next) loadContent(next.url);
  520. }
  521. // Expose to iframes (same origin)
  522. window.docGetPrevNext = docGetPrevNext;
  523. window.docNavigatePrev = docNavigatePrev;
  524. window.docNavigateNext = docNavigateNext;
  525. // =============================
  526. // Middle ↔ Right connection line
  527. // =============================
  528. const connectionSvg = document.getElementById('column-connection-line');
  529. const connectionPath = document.getElementById('column-connection-path');
  530. let _connRaf = 0;
  531. function scheduleConnectionLineUpdate() {
  532. if (_connRaf) return;
  533. _connRaf = requestAnimationFrame(() => {
  534. _connRaf = 0;
  535. updateConnectionLine();
  536. });
  537. }
  538. function getActiveElementInFrame(frameEl) {
  539. try {
  540. const doc = frameEl?.contentDocument;
  541. if (!doc) return null;
  542. // Middle list pages use .card.active / .header-card.active; fall back to .active
  543. return (
  544. doc.querySelector('.card.active') ||
  545. doc.querySelector('.header-card.active') ||
  546. doc.querySelector('.nav-item.active') ||
  547. doc.querySelector('.active')
  548. );
  549. } catch {
  550. return null;
  551. }
  552. }
  553. function getAnchorElementInContentFrame(frameEl) {
  554. try {
  555. const doc = frameEl?.contentDocument;
  556. if (!doc) return null;
  557. // Prefer a *visible* code area anchor (in-viewport), then fall back
  558. const candidates = [
  559. '.box-container .box-header',
  560. '.code-view.active, .html-view.active, .css-view.active',
  561. '.box-container',
  562. 'h1',
  563. '.container',
  564. 'body'
  565. ];
  566. for (const sel of candidates) {
  567. const el = doc.querySelector(sel);
  568. if (!el) continue;
  569. // If it's visible in the iframe viewport, use it; else keep looking
  570. const r = el.getBoundingClientRect();
  571. const vh = frameEl?.clientHeight || 0;
  572. if (!vh) return el;
  573. const visibleTop = Math.max(0, r.top);
  574. const visibleBottom = Math.min(vh, r.bottom);
  575. if (visibleBottom - visibleTop > 8) return el;
  576. }
  577. return doc.body;
  578. } catch {
  579. return null;
  580. }
  581. }
  582. function clamp(n, min, max) {
  583. return Math.min(Math.max(n, min), max);
  584. }
  585. function getVisibleCenterInFrame(frameEl, elRect) {
  586. const w = frameEl?.clientWidth || 0;
  587. const h = frameEl?.clientHeight || 0;
  588. if (!w || !h) return null;
  589. const visibleLeft = Math.max(0, elRect.left);
  590. const visibleRight = Math.min(w, elRect.right);
  591. const visibleTop = Math.max(0, elRect.top);
  592. const visibleBottom = Math.min(h, elRect.bottom);
  593. if (visibleRight - visibleLeft <= 1 || visibleBottom - visibleTop <= 1) {
  594. return null;
  595. }
  596. return {
  597. x: (visibleLeft + visibleRight) / 2,
  598. y: (visibleTop + visibleBottom) / 2
  599. };
  600. }
  601. function updateConnectionLine() {
  602. if (!connectionSvg || !connectionPath) return;
  603. const middleFrame = document.getElementById('middle-frame');
  604. const contentFrame = document.getElementById('content-frame');
  605. if (!middleFrame || !contentFrame) return;
  606. const midRect = middleFrame.getBoundingClientRect();
  607. const rightRect = contentFrame.getBoundingClientRect();
  608. const active = getActiveElementInFrame(middleFrame);
  609. const anchor = getAnchorElementInContentFrame(contentFrame);
  610. if (!active || !anchor) {
  611. connectionPath.setAttribute('d', '');
  612. connectionSvg.style.opacity = '0';
  613. return;
  614. }
  615. const activeRect = active.getBoundingClientRect();
  616. const anchorRect = anchor.getBoundingClientRect();
  617. // Use the *visible* part of the element within its iframe viewport
  618. const activeCenter = getVisibleCenterInFrame(middleFrame, activeRect);
  619. const anchorCenter = getVisibleCenterInFrame(contentFrame, anchorRect);
  620. if (!activeCenter || !anchorCenter) {
  621. // If either side isn't visible in its iframe, hide the line (prevents weird drifting)
  622. connectionPath.setAttribute('d', '');
  623. connectionSvg.style.opacity = '0';
  624. return;
  625. }
  626. // Convert iframe-viewport coordinates to parent viewport coordinates
  627. const startX = midRect.left + clamp(activeRect.right, 0, middleFrame.clientWidth);
  628. const startY = midRect.top + activeCenter.y;
  629. // Stop just before the content block (avoid entering the dark code area)
  630. const endX = rightRect.left + clamp(anchorRect.left - 2, 6, contentFrame.clientWidth - 12);
  631. const endY = rightRect.top + anchorCenter.y;
  632. // Draw an L-shaped polyline.
  633. // Keep the vertical segment aligned with the column divider (middle ↔ right).
  634. const dividerX = rightRect.left + 0.5; // half-pixel for crisper stroke alignment
  635. const x1 = Math.max(startX + 12, dividerX);
  636. const d = [
  637. `M ${startX} ${startY}`,
  638. `L ${x1} ${startY}`,
  639. `L ${x1} ${endY}`,
  640. `L ${endX} ${endY}`
  641. ].join(' ');
  642. connectionPath.setAttribute('d', d);
  643. connectionSvg.style.opacity = '0.85';
  644. }
  645. function attachFrameScrollListeners(frameEl) {
  646. if (!frameEl) return;
  647. const onScroll = () => {
  648. markFrameScrolling(frameEl);
  649. scheduleConnectionLineUpdate();
  650. };
  651. try {
  652. // Window scroll inside iframe
  653. frameEl.contentWindow?.addEventListener('scroll', onScroll, { passive: true });
  654. // Some pages scroll on document/body; capture to be safe
  655. frameEl.contentDocument?.addEventListener('scroll', onScroll, true);
  656. frameEl.contentDocument?.documentElement?.addEventListener?.('scroll', onScroll, { passive: true });
  657. frameEl.contentDocument?.body?.addEventListener?.('scroll', onScroll, { passive: true });
  658. } catch {}
  659. }
  660. const _scrollHideTimers = new WeakMap();
  661. function markFrameScrolling(frameEl) {
  662. try {
  663. const doc = frameEl?.contentDocument;
  664. if (!doc) return;
  665. doc.documentElement?.classList.add('scrolling');
  666. doc.body?.classList.add('scrolling');
  667. const prev = _scrollHideTimers.get(frameEl);
  668. if (prev) clearTimeout(prev);
  669. _scrollHideTimers.set(frameEl, setTimeout(() => {
  670. try {
  671. doc.documentElement?.classList.remove('scrolling');
  672. doc.body?.classList.remove('scrolling');
  673. } catch {}
  674. }, 500));
  675. } catch {}
  676. }
  677. // Always-on lightweight loop to keep the line aligned even
  678. // if iframe scroll events were missed due to timing.
  679. let _connLoopStarted = false;
  680. function startConnectionLineLoop() {
  681. if (_connLoopStarted) return;
  682. _connLoopStarted = true;
  683. const loop = () => {
  684. // Only bother when we have a path (avoids work when hidden)
  685. if (connectionPath?.getAttribute('d')) {
  686. scheduleConnectionLineUpdate();
  687. }
  688. requestAnimationFrame(loop);
  689. };
  690. requestAnimationFrame(loop);
  691. }
  692. // Make initial active state robust against iframe load ordering
  693. window.addEventListener('load', () => {
  694. // Restore route (if present) before doing any extra syncing
  695. try {
  696. const routeUrl = getRouteContentUrl();
  697. if (routeUrl) {
  698. _docApplyingRoute = true;
  699. ensureCategoryForContent(routeUrl);
  700. loadContent(routeUrl, { updateRoute: false });
  701. } else {
  702. // No route: persist current default content into URL (so first refresh is stable)
  703. const current = baseDocPath(document.getElementById('content-frame')?.getAttribute('src') || '');
  704. if (current) setRouteContentUrl(current, { replace: true });
  705. }
  706. } catch {}
  707. _docApplyingRoute = false;
  708. const contentFrame = document.getElementById('content-frame');
  709. const src = contentFrame?.getAttribute('src');
  710. if (src) setMiddleActive(src);
  711. const middleFrame = document.getElementById('middle-frame');
  712. if (middleFrame) {
  713. middleFrame.addEventListener('load', () => {
  714. const current = document.getElementById('content-frame')?.getAttribute('src');
  715. if (current) setMiddleActive(current);
  716. attachFrameScrollListeners(middleFrame);
  717. scheduleConnectionLineUpdate();
  718. });
  719. }
  720. if (contentFrame) {
  721. contentFrame.addEventListener('load', () => {
  722. const current = contentFrame.getAttribute('src');
  723. if (current) setMiddleActive(current);
  724. attachFrameScrollListeners(contentFrame);
  725. scheduleConnectionLineUpdate();
  726. });
  727. }
  728. // If the iframes were already loaded before we attached 'load' listeners,
  729. // still attach scroll listeners immediately.
  730. attachFrameScrollListeners(middleFrame);
  731. attachFrameScrollListeners(contentFrame);
  732. window.addEventListener('resize', scheduleConnectionLineUpdate, { passive: true });
  733. scheduleConnectionLineUpdate();
  734. startConnectionLineLoop();
  735. });
  736. // Back/forward + manual hash edits: restore selection
  737. window.addEventListener('hashchange', () => {
  738. if (_docApplyingRoute) return;
  739. const routeUrl = getRouteContentUrl();
  740. if (!routeUrl) return;
  741. _docApplyingRoute = true;
  742. try {
  743. ensureCategoryForContent(routeUrl);
  744. loadContent(routeUrl, { updateRoute: false });
  745. } finally {
  746. _docApplyingRoute = false;
  747. }
  748. });
  749. </script>
  750. </body>
  751. </html>