Skip to content

Commit

Permalink
🎨 File history support pagination navigation (#12563)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime authored Sep 23, 2024
1 parent f8abfd2 commit 8c8c7c9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/appearance/langs/en_US.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "Jump to the specified page: 1 ~ ${x}",
"pageCountAndSnapshotCount": "Total ${x} pages, ${y} snapshots",
"pageCountAndHistoryCount": "Total ${x} pages, ${y} histories",
"visitCommunityShare": "Visit community share",
"clearContextSucc": "The context has been cleared",
"emptyMobilePlaceholder": "Record something",
Expand Down
1 change: 1 addition & 0 deletions app/appearance/langs/es_ES.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "Saltar a la página especificada: 1 ~ ${x}",
"pageCountAndSnapshotCount": "Total ${x} páginas, ${y} instantáneas",
"pageCountAndHistoryCount": "Total ${x} páginas,${y} historias",
"visitCommunityShare": "Visitar la comunidad compartida",
"clearContextSucc": "Se ha borrado el contexto",
"emptyMobilePlaceholder": "Grabar algo",
Expand Down
1 change: 1 addition & 0 deletions app/appearance/langs/fr_FR.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "Aller à la page spécifiée : 1 ~ ${x}",
"pageCountAndSnapshotCount": "Total de ${x} pages, ${y} instantanés",
"pageCountAndHistoryCount": "Total de ${x} pages,${y} histoires",
"visitCommunityShare": "Visiter le partage communautaire",
"clearContextSucc": "Le contexte a été effacé",
"emptyMobilePlaceholder": "Enregistrer quelque chose",
Expand Down
1 change: 1 addition & 0 deletions app/appearance/langs/ja_JP.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "指定されたページにジャンプ:1 ~ ${x}",
"pageCountAndSnapshotCount": "合計 ${x} ページ、${y} スナップショット",
"pageCountAndHistoryCount": "合計 ${x} ページ、${y} 履歴",
"visitCommunityShare": "コミュニティシェアを訪問",
"clearContextSucc": "コンテキストがクリアされました",
"emptyMobilePlaceholder": "何かを記録する",
Expand Down
1 change: 1 addition & 0 deletions app/appearance/langs/zh_CHT.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "跳轉到指定頁:1 ~ ${x}",
"pageCountAndSnapshotCount": "共 ${x} 頁,${y} 個快照",
"pageCountAndHistoryCount": "共 ${x} 頁,${y} 個歷史紀錄",
"visitCommunityShare": "訪問社區分享",
"clearContextSucc": "上下文已清空",
"emptyMobilePlaceholder": "記錄點什麼",
Expand Down
1 change: 1 addition & 0 deletions app/appearance/langs/zh_CN.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"jumpToPage": "跳转到指定页:1 ~ ${x}",
"pageCountAndSnapshotCount": "共 ${x} 页,${y} 个快照",
"pageCountAndHistoryCount": "共 ${x} 页,${y} 个历史版本",
"visitCommunityShare": "访问社区分享",
"clearContextSucc": "上下文已清空",
"emptyMobilePlaceholder": "记录点什么",
Expand Down
106 changes: 68 additions & 38 deletions app/src/history/history.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {Dialog} from "../dialog";
import {confirmDialog} from "../dialog/confirmDialog";
import {Constants} from "../constants";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {renderAssetsPreview} from "../asset/renderAssets";
import {Protyle} from "../protyle";
import {disabledProtyle, onGet} from "../protyle/util/onGet";
import { Dialog } from "../dialog";
import { confirmDialog } from "../dialog/confirmDialog";
import { Constants } from "../constants";
import { hasClosestByClassName } from "../protyle/util/hasClosest";
import { renderAssetsPreview } from "../asset/renderAssets";
import { Protyle } from "../protyle";
import { disabledProtyle, onGet } from "../protyle/util/onGet";
import * as dayjs from "dayjs";
import {fetchPost} from "../util/fetch";
import {escapeAttr, escapeHtml} from "../util/escape";
import {isMobile} from "../util/functions";
import {showDiff} from "./diff";
import {setStorageVal} from "../protyle/util/compatibility";
import {openModel} from "../mobile/menu/model";
import {closeModel} from "../mobile/util/closePanel";
import {App} from "../index";
import { fetchPost } from "../util/fetch";
import { escapeAttr, escapeHtml } from "../util/escape";
import { isMobile } from "../util/functions";
import { showDiff } from "./diff";
import { setStorageVal } from "../protyle/util/compatibility";
import { openModel } from "../mobile/menu/model";
import { closeModel } from "../mobile/util/closePanel";
import { App } from "../index";

let historyEditor: Protyle;

Expand All @@ -26,6 +26,9 @@ const renderDoc = (element: HTMLElement, currentPage: number) => {
} else {
previousElement.setAttribute("disabled", "disabled");
}
const pageBtn = element.querySelector('button[data-type="jumpHistoryPage"]');
pageBtn.textContent = `${currentPage}`;

const inputElement = element.querySelector(".b3-text-field") as HTMLInputElement;
const opElement = element.querySelector('.b3-select[data-type="opselect"]') as HTMLSelectElement;
const typeElement = element.querySelector('.b3-select[data-type="typeselect"]') as HTMLSelectElement;
Expand Down Expand Up @@ -80,7 +83,10 @@ const renderDoc = (element: HTMLElement, currentPage: number) => {
} else {
nextElement.setAttribute("disabled", "disabled");
}
nextElement.nextElementSibling.nextElementSibling.textContent = `${currentPage}/${response.data.pageCount || 1}`;
pageBtn.setAttribute("data-totalpage", (response.data.pageCount || 1).toString());
// nextElement.nextElementSibling.nextElementSibling.textContent = `${currentPage}/${response.data.pageCount || 1}`;
const titleElement = nextElement.nextElementSibling.nextElementSibling;
titleElement.textContent = `${window.siyuan.languages.pageCountAndHistoryCount.replace("${x}", response.data.pageCount).replace("${y}", response.data.totalCount || 1)}`;
if (response.data.histories.length === 0) {
listElement.innerHTML = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
return;
Expand Down Expand Up @@ -241,7 +247,7 @@ ${actionHTML}
const renderRepo = (element: Element, currentPage: number) => {
const selectValue = (element.querySelector(".b3-select") as HTMLSelectElement).value;
element.lastElementChild.innerHTML = '<li style="position: relative;height: 100%;"><div class="fn__loading"><img width="64px" src="/stage/loading-pure.svg"></div></li>';
const pageBtn = element.querySelector('button[data-type="jumpPage"]');
const pageBtn = element.querySelector('button[data-type="jumpRepoPage"]');
pageBtn.textContent = `${currentPage}`;

const previousElement = element.querySelector('[data-type="previous"]');
Expand All @@ -268,7 +274,7 @@ const renderRepo = (element: Element, currentPage: number) => {
previousElement.setAttribute("disabled", "disabled");
}
nextElement.setAttribute("disabled", "disabled");
fetchPost(`/api/repo/${selectValue}`, {page: currentPage}, (response) => {
fetchPost(`/api/repo/${selectValue}`, { page: currentPage }, (response) => {
if (currentPage < response.data.pageCount) {
nextElement.removeAttribute("disabled");
} else {
Expand Down Expand Up @@ -348,10 +354,10 @@ export const openHistory = (app: App) => {
<div style="overflow:auto;">
<div class="block__icons">
<span data-type="docprevious" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href='#iconLeft'></use></svg></span>
<span class="fn__space"></span>
<button class="b3-button b3-button--text" data-type="jumpHistoryPage" data-totalpage="1">1</button>
<span data-type="docnext" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href='#iconRight'></use></svg></span>
<span class="fn__space"></span>
<span>1/1</span>
<span class="ft__on-surface fn__flex-shrink ft__selectnone">${window.siyuan.languages.pageCountAndHistoryCount}</span>
<span class="fn__space"></span>
<div class="fn__flex-1"></div>
<div style="position: relative">
Expand Down Expand Up @@ -399,7 +405,7 @@ export const openHistory = (app: App) => {
<div style="overflow: auto"">
<div class="block__icons">
<span data-type="previous" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href='#iconLeft'></use></svg></span>
<button class="b3-button b3-button--text" data-type="jumpPage">1</button>
<button class="b3-button b3-button--text" data-type="jumpRepoPage">1</button>
<span data-type="next" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href='#iconRight'></use></svg></span>
<span class="fn__space"></span>
<span class="ft__on-surface fn__flex-shrink ft__selectnone">${window.siyuan.languages.pageCountAndSnapshotCount}</span>
Expand Down Expand Up @@ -645,7 +651,7 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
target.parentElement.querySelector(`.b3-list-item[data-id="${idJSON.splice(0, 1)[0].id}"]`)?.classList.remove("b3-list-item--focus");
}
}
idJSON.push({id, time: target.querySelector('[data-type="hCreated"]').textContent});
idJSON.push({ id, time: target.querySelector('[data-type="hCreated"]').textContent });
}

if (idJSON.length === 2) {
Expand Down Expand Up @@ -717,7 +723,7 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
genRepoDialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
fetchPost("/api/repo/createSnapshot", {memo: textareaElement.value}, () => {
fetchPost("/api/repo/createSnapshot", { memo: textareaElement.value }, () => {
renderRepo(repoElement, 1);
});
genRepoDialog.destroy();
Expand All @@ -728,7 +734,7 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
} else if (type === "removeRepoTagSnapshot" || type === "removeCloudRepoTagSnapshot") {
const tag = target.parentElement.getAttribute("data-tag");
confirmDialog(window.siyuan.languages.deleteOpConfirm, `${window.siyuan.languages.confirmDelete} <i>${tag}</i>?`, () => {
fetchPost("/api/repo/" + type, {tag}, () => {
fetchPost("/api/repo/" + type, { tag }, () => {
renderRepo(repoElement, 1);
});
});
Expand Down Expand Up @@ -803,24 +809,48 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
event.stopPropagation();
event.preventDefault();
break;
} else if (type === "jumpPage") {
} else if (type === "jumpRepoPage") {
const currentPage = parseInt(repoElement.getAttribute("data-page"));
const totalPage = parseInt(repoElement.getAttribute("total-page") || "1");

confirmDialog(
window.siyuan.languages.jumpToPage.replace("${x}", totalPage),
// eslint-disable-next-line quotes
`<input style="width: 100%;" class="b3-text-field fn__flex-center" type="number" min="1" max="${totalPage}" value="${currentPage}">`,
(dialog: Dialog) => {
const inputElement = dialog.element.querySelector(".b3-text-field") as HTMLInputElement;
if (inputElement.value === "") {
return;
if (totalPage > 1) {
confirmDialog(
window.siyuan.languages.jumpToPage.replace("${x}", totalPage),
// eslint-disable-next-line quotes
`<input style="width: 100%;" class="b3-text-field fn__flex-center" type="number" min="1" max="${totalPage}" value="${currentPage}">`,
(dialog: Dialog) => {
const inputElement = dialog.element.querySelector(".b3-text-field") as HTMLInputElement;
if (inputElement.value === "") {
return;
}
let page = parseInt(inputElement.value);
page = Math.max(1, Math.min(page, totalPage));
renderRepo(repoElement, page);
}
let page = parseInt(inputElement.value);
page = Math.max(1, Math.min(page, totalPage));
renderRepo(repoElement, page);
}
);
);
}
} else if (type === "jumpHistoryPage") {
const currentPage = parseInt(target.textContent.trim());
const totalPage = parseInt(target.getAttribute("data-totalpage") || "1");

if (totalPage > 1) {
confirmDialog(
window.siyuan.languages.jumpToPage.replace("${x}", totalPage),
// eslint-disable-next-line quotes
`<input style="width: 100%;" class="b3-text-field fn__flex-center" type="number" min="1" max="${totalPage}" value="${currentPage}">`,
(dialog: Dialog) => {
const inputElement = dialog.element.querySelector(".b3-text-field") as HTMLInputElement;
if (inputElement.value === "") {
return;
}
let page = parseInt(inputElement.value);
page = Math.max(1, Math.min(page, totalPage));
renderDoc(firstPanelElement, page);
}
);
}


} else if ((type === "docprevious" || type === "docnext") && target.getAttribute("disabled") !== "disabled") {
const currentPage = parseInt(firstPanelElement.getAttribute("data-page"));
renderDoc(firstPanelElement, type === "docprevious" ? currentPage - 1 : currentPage + 1);
Expand Down

0 comments on commit 8c8c7c9

Please sign in to comment.