Vanessa 2022-11-14 17:29:41 +08:00
parent e8f1808cb0
commit 452b79c601
8 changed files with 121 additions and 51 deletions

View File

@ -5,13 +5,54 @@
.protyle-scroll { .protyle-scroll {
position: absolute; position: absolute;
right: -80px; right: 0;
height: 100%;
top: 0;
width: 24px;
svg {
height: 16px;
width: 16px;
}
&__down,
&__up {
transition: var(--b3-transition);
position: absolute;
opacity: 0;
cursor: pointer;
color: var(--b3-border-color);
&:hover {
color: var(--b3-theme-on-surface);
}
}
&__up {
top: calc(50% - 114px);
}
&__down {
bottom: calc(50% - 135px);
}
&:hover {
.protyle-scroll__down,
.protyle-scroll__up {
opacity: 1;
}
}
&__bar {
position: absolute;
right: -89px;
top: 50%; top: 50%;
transform: rotate(90deg); transform: rotate(90deg);
z-index: 1;
.b3-slider { .b3-slider {
width: 200px; width: 200px;
} }
}
} }
.protyle-attr { .protyle-attr {
@ -110,6 +151,7 @@
flex-shrink: 0; flex-shrink: 0;
box-sizing: border-box; box-sizing: border-box;
min-height: 30px; min-height: 30px;
z-index: 1;
&__space { &__space {
flex: 1; flex: 1;

View File

@ -59,6 +59,7 @@ export const openMobileFileById = (id: string, action = [Constants.CB_GET_HL]) =
blockId: id, blockId: id,
action, action,
render: { render: {
scroll: true,
background: true, background: true,
gutter: true, gutter: true,
}, },

View File

@ -72,7 +72,7 @@ export class Protyle {
this.protyle.undo = new Undo(); this.protyle.undo = new Undo();
this.protyle.wysiwyg = new WYSIWYG(this.protyle); this.protyle.wysiwyg = new WYSIWYG(this.protyle);
this.protyle.toolbar = new Toolbar(this.protyle); this.protyle.toolbar = new Toolbar(this.protyle);
this.protyle.scroll = new Scroll(this.protyle); this.protyle.scroll = new Scroll(this.protyle); // 不能使用 render.scroll 来判读是否初始化,除非重构后面用到的相关变量
if (this.protyle.options.render.gutter) { if (this.protyle.options.render.gutter) {
this.protyle.gutter = new Gutter(this.protyle); this.protyle.gutter = new Gutter(this.protyle);
} }

View File

@ -1,25 +1,37 @@
import {Constants} from "../../constants"; import {Constants} from "../../constants";
import {onGet} from "../util/onGet"; import {onGet} from "../util/onGet";
import {fetchPost} from "../../util/fetch"; import {fetchPost} from "../../util/fetch";
import {updateHotkeyTip} from "../util/compatibility";
import {hasClosestByClassName} from "../util/hasClosest";
import {goEnd, goHome} from "../wysiwyg/commonHotkey";
export class Scroll { export class Scroll {
public element: HTMLElement; public element: HTMLElement;
private parentElement: HTMLElement;
private inputElement: HTMLInputElement; private inputElement: HTMLInputElement;
public lastScrollTop: number; public lastScrollTop: number;
public keepLazyLoad: boolean; public keepLazyLoad: boolean;
constructor(protyle: IProtyle) { constructor(protyle: IProtyle) {
const divElement = document.createElement("div"); this.parentElement = document.createElement("div");
divElement.innerHTML = "<input class='b3-slider' type='range' max='1' min='1' step='1' value='1' />"; this.parentElement.classList.add("protyle-scroll");
divElement.className = "fn__none protyle-scroll b3-tooltips b3-tooltips__s"; this.parentElement.innerHTML = `<div class="b3-tooltips b3-tooltips__nw protyle-scroll__up" aria-label="${updateHotkeyTip("⌘Home")}">
divElement.setAttribute("aria-label", "Blocks 1/1"); <svg><use xlink:href="#iconUp"></use></svg>
this.element = divElement; </div>
<div class="fn__none protyle-scroll__bar b3-tooltips b3-tooltips__s" aria-label="Blocks 1/1">
<input class="b3-slider" type="range" max="1" min="1" step="1" value="1" />
</div>
<div class="b3-tooltips b3-tooltips__sw protyle-scroll__down" aria-label="${updateHotkeyTip("End")}">
<svg><use xlink:href="#iconDown"></use></svg>
</div>`
this.element = this.parentElement.querySelector(".protyle-scroll__bar");
this.keepLazyLoad = false; this.keepLazyLoad = false;
if (!protyle.options.render.scroll) { if (!protyle.options.render.scroll) {
this.element.classList.add("fn__none"); this.parentElement.classList.add("fn__none");
} }
this.lastScrollTop = 0; this.lastScrollTop = 0;
this.inputElement = divElement.firstElementChild as HTMLInputElement; this.inputElement = this.element.firstElementChild as HTMLInputElement;
this.inputElement.addEventListener("input", () => { this.inputElement.addEventListener("input", () => {
this.element.setAttribute("aria-label", `Blocks ${this.inputElement.value}/${protyle.block.blockCount}`); this.element.setAttribute("aria-label", `Blocks ${this.inputElement.value}/${protyle.block.blockCount}`);
}); });
@ -31,13 +43,20 @@ export class Scroll {
this.setIndex(protyle); this.setIndex(protyle);
}); });
/// #endif /// #endif
this.inputElement.addEventListener("click", () => { this.parentElement.addEventListener("click", (event) => {
const target = event.target as HTMLElement
if (hasClosestByClassName(target, "protyle-scroll__up")) {
goHome(protyle)
} else if (hasClosestByClassName(target, "protyle-scroll__down")) {
goEnd(protyle)
} else if (target.classList.contains("b3-slider")) {
this.setIndex(protyle); this.setIndex(protyle);
}
}); });
} }
private setIndex(protyle: IProtyle) { private setIndex(protyle: IProtyle) {
if (protyle.wysiwyg.element.getAttribute("data-top") || !protyle.model) { if (protyle.wysiwyg.element.getAttribute("data-top")) {
return; return;
} }
protyle.wysiwyg.element.setAttribute("data-top", protyle.wysiwyg.element.scrollTop.toString()); protyle.wysiwyg.element.setAttribute("data-top", protyle.wysiwyg.element.scrollTop.toString());

View File

@ -28,8 +28,8 @@ export const initUI = (protyle: IProtyle) => {
if (protyle.upload) { if (protyle.upload) {
protyle.element.appendChild(protyle.upload.element); protyle.element.appendChild(protyle.upload.element);
} }
if (protyle.scroll) { if (protyle.options.render) {
protyle.element.appendChild(protyle.scroll.element); protyle.element.appendChild(protyle.scroll.element.parentElement);
} }
if (protyle.gutter) { if (protyle.gutter) {
protyle.element.appendChild(protyle.gutter.element); protyle.element.appendChild(protyle.gutter.element);

View File

@ -19,6 +19,8 @@ import {hideElements} from "../ui/hideElements";
import {countBlockWord} from "../../layout/status"; import {countBlockWord} from "../../layout/status";
import {scrollCenter} from "../../util/highlightById"; import {scrollCenter} from "../../util/highlightById";
import {transaction} from "./transaction"; import {transaction} from "./transaction";
import {onGet} from "../util/onGet";
import {Constants} from "../../constants";
export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => { export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => {
const target = event.target as HTMLElement; const target = event.target as HTMLElement;
@ -215,3 +217,38 @@ export const duplicateBlock = (nodeElements: Element[], protyle: IProtyle) => {
focusBlock(focusElement); focusBlock(focusElement);
scrollCenter(protyle); scrollCenter(protyle);
}; };
export const goHome = (protyle:IProtyle) => {
if (protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-index") === "0" ||
protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") === "true" ||
protyle.options.backlinkData) {
focusBlock(protyle.wysiwyg.element.firstElementChild);
protyle.contentElement.scrollTop = 0;
protyle.scroll.lastScrollTop = 1;
} else {
fetchPost("/api/filetree/getDoc", {
id: protyle.block.rootID,
mode: 0,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_FOCUS]);
});
}
}
export const goEnd = (protyle:IProtyle) => {
if (!protyle.scroll.element.classList.contains("fn__none") &&
protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "true") {
fetchPost("/api/filetree/getDoc", {
id: protyle.block.rootID,
mode: 4,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_FOCUS]);
});
} else {
protyle.contentElement.scrollTop = protyle.contentElement.scrollHeight;
protyle.scroll.lastScrollTop = protyle.contentElement.scrollTop;
focusBlock(protyle.wysiwyg.element.lastElementChild, undefined, false);
}
}

View File

@ -42,14 +42,13 @@ import {isLocalPath} from "../../util/pathName";
/// #if !MOBILE /// #if !MOBILE
import {openBy, openFileById} from "../../editor/util"; import {openBy, openFileById} from "../../editor/util";
/// #endif /// #endif
import {commonHotkey, downSelect, duplicateBlock, getStartEndElement, upSelect} from "./commonHotkey"; import {commonHotkey, downSelect, duplicateBlock, getStartEndElement, goEnd, goHome, upSelect} from "./commonHotkey";
import {linkMenu, refMenu, setFold, zoomOut} from "../../menus/protyle"; import {linkMenu, refMenu, setFold, zoomOut} from "../../menus/protyle";
import {removeEmbed} from "./removeEmbed"; import {removeEmbed} from "./removeEmbed";
import {openAttr} from "../../menus/commonMenuItem"; import {openAttr} from "../../menus/commonMenuItem";
import {Constants} from "../../constants"; import {Constants} from "../../constants";
import {bindMenuKeydown} from "../../menus/Menu"; import {bindMenuKeydown} from "../../menus/Menu";
import {fetchPost} from "../../util/fetch"; import {fetchPost} from "../../util/fetch";
import {onGet} from "../util/onGet";
import {scrollCenter} from "../../util/highlightById"; import {scrollCenter} from "../../util/highlightById";
import {BlockPanel} from "../../block/Panel"; import {BlockPanel} from "../../block/Panel";
import * as dayjs from "dayjs"; import * as dayjs from "dayjs";
@ -452,41 +451,14 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
} }
// ctrl+home 光标移动到顶 // ctrl+home 光标移动到顶
if (!event.altKey && !event.shiftKey && isCtrl(event) && event.key === "Home") { if (!event.altKey && !event.shiftKey && isCtrl(event) && event.key === "Home") {
if (protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-index") === "0" || goHome(protyle);
protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") === "true" ||
protyle.options.backlinkData) {
focusBlock(protyle.wysiwyg.element.firstElementChild);
protyle.contentElement.scrollTop = 0;
protyle.scroll.lastScrollTop = 1;
} else {
fetchPost("/api/filetree/getDoc", {
id: protyle.block.rootID,
mode: 0,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_FOCUS]);
});
}
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
return; return;
} }
// ctrl+end 光标移动到尾 // ctrl+end 光标移动到尾
if (!event.altKey && !event.shiftKey && isCtrl(event) && event.key === "End") { if (!event.altKey && !event.shiftKey && isCtrl(event) && event.key === "End") {
if (!protyle.scroll.element.classList.contains("fn__none") && goEnd(protyle);
protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "true") {
fetchPost("/api/filetree/getDoc", {
id: protyle.block.rootID,
mode: 4,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_FOCUS]);
});
} else {
protyle.contentElement.scrollTop = protyle.contentElement.scrollHeight;
protyle.scroll.lastScrollTop = protyle.contentElement.scrollTop;
focusBlock(protyle.wysiwyg.element.lastElementChild, undefined, false);
}
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
return; return;

View File

@ -311,7 +311,6 @@ export const openHistory = () => {
background: false, background: false,
title: false, title: false,
gutter: false, gutter: false,
scroll: false,
breadcrumb: false, breadcrumb: false,
breadcrumbDocName: false, breadcrumbDocName: false,
breadcrumbContext: false, breadcrumbContext: false,