diff --git a/assets/gitbook-fix.css b/assets/gitbook-fix.css index 29935b5..4559aba 100644 --- a/assets/gitbook-fix.css +++ b/assets/gitbook-fix.css @@ -102,3 +102,11 @@ #book-search-input { background-color: var(--color-nav-background-base); } + +#page-container { + transition-duration: 400ms; +} + +#page-container.loading { + opacity: 0.2; +} diff --git a/assets/gitbook-fix.js b/assets/gitbook-fix.js index b4fee80..41874cc 100644 --- a/assets/gitbook-fix.js +++ b/assets/gitbook-fix.js @@ -3,9 +3,14 @@ const WITH_SUMMARY_CLASS = "with-summary" const DROPDOWN_OPEN_CLASS = "open"; let bookRoot; +let pageContainer; +let inBookNavContainer; let fontSettingDiv; +let bookCurrentId; +let pageCurrentId; + function summaryOnOrOff () { if (bookRoot.classList.contains(WITH_SUMMARY_CLASS)) { @@ -19,6 +24,8 @@ function summaryOnOrOff () { window.onload = function () { bookRoot = document.getElementsByClassName("book")[0]; + pageContainer = document.getElementById("page-container"); + inBookNavContainer = document.getElementById("in-book-nav-container"); fontSettingDiv = document.getElementsByClassName("font-settings")[0].getElementsByClassName("dropdown-menu")[0]; @@ -28,16 +35,17 @@ window.onload = function () { }; -for (let node of document.getElementsByClassName("fold")) { - node.childNodes[0].addEventListener("click", function () { - if (node.classList.contains("on")) { - node.classList.remove("on"); - } else node.classList.add("on"); - }); -} +function bindFolderClickEvent () { + for (let node of document.getElementsByClassName("fold")) { + node.childNodes[0].addEventListener("click", function () { + if (node.classList.contains("on")) { + node.classList.remove("on"); + } else node.classList.add("on"); + }); + } +} bindFolderClickEvent(); for (const node of document.getElementsByClassName("summary-container")) { - node.nextElementSibling.innerHTML = node.nextElementSibling.innerHTML + ""; node.nextElementSibling.getElementsByClassName("summary-container-icon")[0].addEventListener("click", function () { if (node.classList.contains("on")) { @@ -46,7 +54,6 @@ for (const node of document.getElementsByClassName("summary-container")) { node.classList.add("on") } }) - } function openOrCloseFontSettings () { @@ -114,3 +121,70 @@ function setCookie(name, value) { const expires = "expires=" + d.toGMTString() document.cookie = name + "=" + value + "; " + expires; } + +function updatePage (bookId, pageId = "") { + + const isNavRefresh = bookId !== bookCurrentId; + const request = new XMLHttpRequest(); + const url = ( + "/" + bookId + "/" + pageId + ); + const urlParam = ( + "?raw=true" + + ((isNavRefresh)?("&nav=true"):("")) + ) + request.open("GET", url + urlParam, true); + console.log(url + urlParam); + request.onreadystatechange = function () { + if (request.readyState === 4 && request.status === 200) { + + // data + const data = request.responseText.split("\n", 2); + const nav = isNavRefresh?data[0]:""; + const content = request.responseText.substr(nav.length); + console.log(nav); + console.log(content); + // content + pageContainer.innerHTML = content; + if (!isNavRefresh) document.getElementById("page/"+pageCurrentId).classList.remove("active"); + if (!isNavRefresh) pageCurrentId = pageId; + if (!isNavRefresh) document.getElementById("page/"+pageId).classList.add("active"); + // nav + if (isNavRefresh) { + inBookNavContainer.innerHTML = nav; + if (bookCurrentId !== "%root") + document.getElementById("book/"+bookCurrentId).classList.remove("active"); + bookCurrentId = bookId; + pageCurrentId = inBookNavContainer.getElementsByClassName("active")[0].getAttribute("page-id"); + document.getElementById("book/"+bookId).classList.add("active"); + bindFolderClickEvent(); + bindPageLinkClickEvent(); + } + // history + window.history.pushState(document.documentElement.innerHTML, document.title, url); + pageContainer.classList.remove("loading"); + + } + } + request.send(); + pageContainer.classList.add("loading"); + +} + +function bindBookLinkClickEvent () { + for (let node of document.getElementsByClassName("link-book")) { + node.children[0].removeAttribute("href"); + node.childNodes[0].addEventListener("click", function () { + updatePage(node.getAttribute("book-id")); + }, true); + } +} bindBookLinkClickEvent(); + +function bindPageLinkClickEvent () { + for (let node of document.getElementsByClassName("link-page")) { + node.children[0].removeAttribute("href"); + node.childNodes[0].addEventListener("click", function () { + updatePage(bookCurrentId, node.getAttribute("page-id")); + }, true); + } +} bindPageLinkClickEvent(); diff --git a/index.php b/index.php index 84d0f0b..28286a5 100644 --- a/index.php +++ b/index.php @@ -17,6 +17,10 @@ try { SiteMeta::load(); + // 检查是否为 ajax 请求 + $rawContent = $_GET['raw']=="true"; + $rawWithNav = $_GET['nav']=="true"; + // 格式化所给链接,并将链接转化为路径字符串数组 $req = $_GET['p']; if (strlen($req) > 0 && $req[strlen($req) - 1] === '/') @@ -66,6 +70,12 @@ try { } + if ($rawContent && $rawWithNav) { + echo PageMeta::$book->getSummaryHtml() . "\n"; + } + + if (!$rawContent) : + require "./template/header.php"; ?> @@ -123,11 +133,12 @@ try {
" . $e->getMessage() . "
"; diff --git a/src/Element/Book.php b/src/Element/Book.php index 8601f76..65c5f25 100644 --- a/src/Element/Book.php +++ b/src/Element/Book.php @@ -54,7 +54,7 @@ class Book { } public function getHtml (): string { - return "