Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support draggable mobile menu sheets https://github.com/siyuan-note/siyuan/issues/14648

Danielc77ca0fae568
diff --git a/app/src/assets/scss/component/_menu.scss b/app/src/assets/scss/component/_menu.scss
index a6a8f954c9..5ed15ee6f9 100644
--- a/app/src/assets/scss/component/_menu.scss
+++ b/app/src/assets/scss/component/_menu.scss
@@ -66,13 +66,10 @@
padding: 0;
border-radius: 0;
width: 100%;
+ background-color: var(--b3-theme-surface);
overflow-x: hidden;
transition: transform 0.15s cubic-bezier(0, 0, 0.2, 1) 0ms;
display: block;
-
- & > .b3-menu__item:first-child {
- border-top: .5px solid var(--b3-theme-background-light);
- }
}
&__items {
@@ -150,6 +147,107 @@
}
}
}
+
+ & > .b3-menu__items:has(> .b3-menu__item),
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) {
+ box-sizing: border-box;
+ padding: 8px 16px max(16px, env(safe-area-inset-bottom));
+ }
+
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item,
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item {
+ border: 0;
+ margin: 0;
+ }
+
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:not(:last-child):not(:has(+ .b3-menu__separator)),
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:not(:last-child):not(:has(+ .b3-menu__separator)) {
+ border-bottom: .5px solid var(--b3-theme-background-light);
+ }
+
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:first-child,
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__separator + .b3-menu__item,
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:first-child,
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__separator + .b3-menu__item {
+ border-top-left-radius: var(--b3-border-radius-b);
+ border-top-right-radius: var(--b3-border-radius-b);
+ }
+
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:last-child,
+ & > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:has(+ .b3-menu__separator),
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:last-child,
+ .b3-menu__submenu > .b3-menu__items:has(> .b3-menu__item) > .b3-menu__item:has(+ .b3-menu__separator) {
+ border-bottom: 0;
+ border-bottom-left-radius: var(--b3-border-radius-b);
+ border-bottom-right-radius: var(--b3-border-radius-b);
+ }
+ }
+
+ &--sheet {
+ top: auto;
+ bottom: 0;
+ left: 8px;
+ right: 8px;
+ width: auto;
+ height: auto;
+ max-height: 90vh;
+ max-height: 90dvh;
+ border-radius: var(--b3-border-radius-b) var(--b3-border-radius-b) 0 0;
+ box-shadow: var(--b3-dialog-shadow);
+ overflow: hidden;
+ transform: translateY(100%);
+ transition: transform .15s cubic-bezier(0, 0, .2, 1), height .15s cubic-bezier(0, 0, .2, 1);
+ will-change: transform;
+
+ .b3-menu {
+ &__title {
+ box-sizing: border-box;
+ height: 56px;
+ padding-top: 8px;
+ position: relative;
+ touch-action: none;
+
+ &::before {
+ position: absolute;
+ top: 5px;
+ left: calc(50% - 16px);
+ width: 32px;
+ height: 4px;
+ border-radius: 2px;
+ background-color: var(--b3-theme-on-surface-light);
+ content: "";
+ opacity: .45;
+ }
+ }
+
+ &__items {
+ min-height: 0;
+ overscroll-behavior: contain;
+ }
+
+ &__submenu {
+ top: 56px;
+ left: 100%;
+ }
+ }
+
+ & .b3-menu__submenu .b3-menu__submenu {
+ top: 0;
+ }
+ }
+
+ &__scrim {
+ position: fixed;
+ inset: 0;
+ background-color: #000;
+ opacity: 0;
+ pointer-events: none;
+ transition: opacity 150ms linear;
+
+ &--open {
+ opacity: .4;
+ pointer-events: auto;
+ }
}
&--fullscreen &__groups,
diff --git a/app/src/assets/template/mobile/index.tpl b/app/src/assets/template/mobile/index.tpl
index ce3bd55618..9985ae37d3 100644
--- a/app/src/assets/template/mobile/index.tpl
+++ b/app/src/assets/template/mobile/index.tpl
@@ -64,6 +64,7 @@
</div>
<div id="modelMain" class="fn__flex-1"></div>
</div>
+<div id="commonMenuScrim" class="b3-menu__scrim fn__none" aria-hidden="true"></div>
<div id="commonMenu" class="b3-menu fn__none">
<div class="b3-menu__title fn__none">
<svg class="b3-menu__icon"><use xlink:href="#iconLeft"></use></svg>
diff --git a/app/src/menus/Menu.ts b/app/src/menus/Menu.ts
index 8c96f08c35..485ecf4a17 100644
--- a/app/src/menus/Menu.ts
+++ b/app/src/menus/Menu.ts
@@ -11,6 +11,8 @@ import {applyMenuEntryVisibility} from "../config/entryVisibility/runtime";
/// #endif
const CUSTOM_EVENT_LOAD_SUBMENU = "load-submenu";
+let fullscreenCloseTimeout: number;
+let fullscreenScrimHideTimeout: number;
export class Menu {
public element: HTMLElement;
@@ -18,6 +20,12 @@ export class Menu {
public removeCB: () => void;
private wheelEvent: string;
private position: IPosition;
+ private sheetTouchStartX: number | undefined;
+ private sheetTouchStartY: number | undefined;
+ private sheetTouchStartTime: number | undefined;
+ private sheetCanDrag = false;
+ private sheetDragging = false;
+ private suppressSheetClick = false;
constructor(element?: HTMLElement) {
this.wheelEvent = "onwheel" in document.createElement("div") ? "wheel" : "mousewheel";
@@ -25,7 +33,24 @@ export class Menu {
this.element = element || document.getElementById("commonMenu");
this.element.querySelector(".b3-menu__title .b3-menu__label").innerHTML = window.siyuan.languages.back;
+ if (isMobile()) {
+ this.element.addEventListener("touchstart", this.handleSheetTouchStart, {passive: true});
+ this.element.addEventListener("touchmove", this.handleSheetTouchMove, {passive: false});
+ this.element.addEventListener("touchend", this.handleSheetTouchEnd);
+ this.element.addEventListener("touchcancel", this.handleSheetTouchCancel);
+ if (this.element.id === "commonMenu") {
+ document.getElementById("commonMenuScrim")?.addEventListener("click", (event) => {
+ event.stopPropagation();
+ this.closeSheet();
+ });
+ }
+ }
this.element.addEventListener(isMobile() ? "click" : "mouseover", (event) => {
+ if (isMobile() && this.suppressSheetClick && typeof event.detail !== "string") {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ return;
+ }
const target = event.target as Element;
if (isMobile()) {
const titleElement = hasClosestByClassName(target, "b3-menu__title");
@@ -33,11 +58,11 @@ export class Menu {
const lastShowElements = this.element.querySelectorAll(".b3-menu__item--show");
if (lastShowElements.length > 0) {
lastShowElements[lastShowElements.length - 1].classList.remove("b3-menu__item--show");
+ if (this.element.classList.contains("b3-menu--sheet")) {
+ this.setSheetHeight(this.element.dataset.position === "bottom" ? "bottom" : "all");
+ }
} else {
- this.element.style.transform = "";
- setTimeout(() => {
- this.remove();
- }, Constants.TIMEOUT_DBLCLICK);
+ this.closeSheet();
}
return;
}
@@ -69,12 +94,196 @@ export class Menu {
return;
}
itemElement.classList.add("b3-menu__item--show");
- if (!isSubMenuShown && !this.element.classList.contains("b3-menu--fullscreen")) {
- this.showSubMenu(subMenuElement);
+ if (!isSubMenuShown) {
+ if (this.element.classList.contains("b3-menu--sheet")) {
+ this.setSheetHeight(this.element.dataset.position === "bottom" ? "bottom" : "all");
+ } else if (!this.element.classList.contains("b3-menu--fullscreen")) {
+ this.showSubMenu(subMenuElement);
+ }
}
});
}
+ private getFullscreenScrim() {
+ if (this.element.id !== "commonMenu") {
+ return;
+ }
+ return document.getElementById("commonMenuScrim");
+ }
+
+ private showFullscreenScrim() {
+ const scrimElement = this.getFullscreenScrim();
+ if (!scrimElement) {
+ return;
+ }
+ clearTimeout(fullscreenScrimHideTimeout);
+ scrimElement.style.opacity = "";
+ scrimElement.style.zIndex = (++window.siyuan.zIndex).toString();
+ scrimElement.classList.remove("fn__none");
+ requestAnimationFrame(() => {
+ if (this.element.classList.contains("b3-menu--sheet") &&
+ !this.element.classList.contains("fn__none")) {
+ scrimElement.classList.add("b3-menu__scrim--open");
+ }
+ });
+ }
+
+ private hideFullscreenScrim() {
+ const scrimElement = this.getFullscreenScrim();
+ if (!scrimElement) {
+ return;
+ }
+ scrimElement.style.opacity = "";
+ scrimElement.classList.remove("b3-menu__scrim--open");
+ clearTimeout(fullscreenScrimHideTimeout);
+ fullscreenScrimHideTimeout = window.setTimeout(() => {
+ if (!scrimElement.classList.contains("b3-menu__scrim--open")) {
+ scrimElement.classList.add("fn__none");
+ scrimElement.style.zIndex = "";
+ }
+ }, Constants.TIMEOUT_DBLCLICK);
+ }
+
+ private canDragSheet(target: HTMLElement) {
+ if (target.closest("input, textarea, select, [contenteditable=\"true\"]")) {
+ return false;
+ }
+ if (target.closest(".b3-menu__title")) {
+ return true;
+ }
+ if (!target.closest(".b3-menu__items")) {
+ return false;
+ }
+ let element: HTMLElement = target;
+ while (element && element !== this.element) {
+ const style = getComputedStyle(element);
+ if (element.scrollHeight > element.clientHeight + 1 &&
+ ["auto", "scroll", "overlay"].includes(style.overflowY) && element.scrollTop > 0) {
+ return false;
+ }
+ element = element.parentElement;
+ }
+ return true;
+ }
+
+ private handleSheetTouchStart = (event: TouchEvent) => {
+ if (!this.element.classList.contains("b3-menu--sheet") || event.touches.length !== 1) {
+ return;
+ }
+ const touch = event.touches[0];
+ this.sheetTouchStartX = touch.clientX;
+ this.sheetTouchStartY = touch.clientY;
+ this.sheetTouchStartTime = performance.now();
+ this.sheetCanDrag = this.canDragSheet(event.target as HTMLElement);
+ this.sheetDragging = false;
+ };
+
+ private handleSheetTouchMove = (event: TouchEvent) => {
+ if (!this.sheetCanDrag || typeof this.sheetTouchStartX !== "number" ||
+ typeof this.sheetTouchStartY !== "number" || event.touches.length !== 1) {
+ return;
+ }
+ const touch = event.touches[0];
+ const xDiff = touch.clientX - this.sheetTouchStartX;
+ const yDiff = touch.clientY - this.sheetTouchStartY;
+ if (!this.sheetDragging && (yDiff <= 0 || Math.abs(xDiff) > Math.abs(yDiff))) {
+ return;
+ }
+ const offset = Math.max(0, yDiff);
+ this.sheetDragging = true;
+ this.element.style.transition = "none";
+ this.element.style.transform = `translateY(${offset}px)`;
+ const scrimElement = this.getFullscreenScrim();
+ if (scrimElement) {
+ scrimElement.style.opacity = Math.max(0, .4 * (1 - offset / this.element.clientHeight)).toString();
+ }
+ if (event.cancelable) {
+ event.preventDefault();
+ }
+ };
+
+ private finishSheetTouch() {
+ this.sheetTouchStartX = undefined;
+ this.sheetTouchStartY = undefined;
+ this.sheetTouchStartTime = undefined;
+ this.sheetCanDrag = false;
+ this.sheetDragging = false;
+ }
+
+ private handleSheetTouchEnd = (event: TouchEvent) => {
+ if (!this.sheetDragging || typeof this.sheetTouchStartY !== "number" ||
+ typeof this.sheetTouchStartTime !== "number") {
+ this.finishSheetTouch();
+ return;
+ }
+ const touch = event.changedTouches[0];
+ const offset = Math.max(0, touch.clientY - this.sheetTouchStartY);
+ const duration = Math.max(performance.now() - this.sheetTouchStartTime, 1);
+ const velocity = offset / duration;
+ const shouldClose = offset > Math.min(120, this.element.clientHeight * .25) ||
+ (offset > 20 && velocity > .6);
+ this.element.style.transition = "";
+ void this.element.offsetHeight;
+ if (shouldClose) {
+ this.closeSheet();
+ } else {
+ this.element.style.transform = "translateY(0px)";
+ const scrimElement = this.getFullscreenScrim();
+ if (scrimElement) {
+ scrimElement.style.opacity = "";
+ }
+ }
+ this.suppressSheetClick = true;
+ window.setTimeout(() => {
+ this.suppressSheetClick = false;
+ }, 300);
+ this.finishSheetTouch();
+ };
+
+ private handleSheetTouchCancel = () => {
+ if (this.sheetDragging) {
+ this.element.style.transition = "";
+ void this.element.offsetHeight;
+ this.element.style.transform = "translateY(0px)";
+ const scrimElement = this.getFullscreenScrim();
+ if (scrimElement) {
+ scrimElement.style.opacity = "";
+ }
+ }
+ this.finishSheetTouch();
+ };
+
+ private closeSheet() {
+ if (!this.element.classList.contains("b3-menu--sheet")) {
+ this.element.style.transform = "";
+ window.setTimeout(() => this.remove(), Constants.TIMEOUT_DBLCLICK);
+ return;
+ }
+ clearTimeout(fullscreenCloseTimeout);
+ this.element.style.transition = "";
+ void this.element.offsetHeight;
+ this.element.style.transform = "translateY(100%)";
+ this.hideFullscreenScrim();
+ fullscreenCloseTimeout = window.setTimeout(() => this.removeImmediately(), Constants.TIMEOUT_DBLCLICK);
+ }
+
+ private setSheetHeight(position: "bottom" | "all") {
+ if (position === "bottom") {
+ this.element.style.height = "50vh";
+ return;
+ }
+ let itemsElement = this.element.lastElementChild;
+ const shownItems = this.element.querySelectorAll(".b3-menu__item--show");
+ if (shownItems.length > 0) {
+ itemsElement = shownItems[shownItems.length - 1]
+ .querySelector(":scope > .b3-menu__submenu > .b3-menu__items") || itemsElement;
+ }
+ const maxHeight = window.innerHeight * .9;
+ const titleHeight = this.element.firstElementChild.getBoundingClientRect().height;
+ const contentHeight = itemsElement.scrollHeight;
+ this.element.style.height = Math.min(maxHeight, Math.max(160, titleHeight + contentHeight)) + "px";
+ }
+
public showSubMenu(subMenuElement: HTMLElement) {
const itemsMenuElement = subMenuElement.lastElementChild as HTMLElement;
if (itemsMenuElement) {
@@ -82,6 +291,10 @@ export class Menu {
} else {
return;
}
+ if (this.element.classList.contains("b3-menu--sheet")) {
+ this.setSheetHeight(this.element.dataset.position === "bottom" ? "bottom" : "all");
+ return;
+ }
const itemRect = subMenuElement.parentElement.getBoundingClientRect();
const subMenuRect = subMenuElement.getBoundingClientRect();
if (subMenuElement.dataset.anchor === "action" && !this.element.classList.contains("b3-menu--fullscreen")) {
@@ -172,9 +385,19 @@ export class Menu {
subElement.classList.remove("b3-menu__item--show");
subElement.classList.add("b3-menu__item--current");
subElement.querySelector(".b3-menu__item--current")?.classList.remove("b3-menu__item--current");
+ if (this.element.classList.contains("b3-menu--sheet")) {
+ this.setSheetHeight(this.element.dataset.position === "bottom" ? "bottom" : "all");
+ }
return;
}
}
+ this.removeImmediately();
+ }
+
+ private removeImmediately() {
+ clearTimeout(fullscreenCloseTimeout);
+ this.hideFullscreenScrim();
+ this.finishSheetTouch();
if (this.removeCB) {
const removeCB = this.removeCB;
this.removeCB = undefined;
@@ -185,10 +408,11 @@ export class Menu {
this.element.lastElementChild.innerHTML = "";
this.element.lastElementChild.removeAttribute("style"); // 输入框 focus 后 boxShadow 显示不全
this.element.classList.add("fn__none");
- this.element.classList.remove("b3-menu--list", "b3-menu--fullscreen");
+ this.element.classList.remove("b3-menu--list", "b3-menu--fullscreen", "b3-menu--sheet");
this.element.removeAttribute("style"); // zIndex
this.element.removeAttribute("data-name"); // 标识再次点击不消失
this.element.removeAttribute("data-from"); // 标识菜单入口
+ this.element.removeAttribute("data-position");
this.data = undefined; // 移除数据
}
@@ -240,18 +464,37 @@ export class Menu {
if (this.element.lastElementChild.innerHTML === "") {
return;
}
- this.element.classList.add("b3-menu--fullscreen");
+ if (!isMobile()) {
+ this.element.classList.add("b3-menu--fullscreen");
+ this.element.style.zIndex = (++window.siyuan.zIndex).toString();
+ this.element.firstElementChild.classList.remove("fn__none");
+ this.element.classList.remove("fn__none");
+ window.addEventListener("touchmove", this.preventDefault, {passive: false});
+ setTimeout(() => {
+ if (position === "bottom") {
+ this.element.style.transform = "translateY(-50vh)";
+ this.element.style.height = "50vh";
+ } else {
+ this.element.style.transform = "translateY(-100%)";
+ }
+ });
+ this.element.lastElementChild.scrollTop = 0;
+ return;
+ }
+ clearTimeout(fullscreenCloseTimeout);
+ this.element.classList.add("b3-menu--fullscreen", "b3-menu--sheet");
+ this.element.dataset.position = position;
+ this.element.style.transform = "translateY(100%)";
+ this.showFullscreenScrim();
this.element.style.zIndex = (++window.siyuan.zIndex).toString();
this.element.firstElementChild.classList.remove("fn__none");
this.element.classList.remove("fn__none");
window.addEventListener("touchmove", this.preventDefault, {passive: false});
-
- setTimeout(() => {
- if (position === "bottom") {
- this.element.style.transform = "translateY(-50vh)";
- this.element.style.height = "50vh";
- } else {
- this.element.style.transform = "translateY(-100%)";
+ this.setSheetHeight(position);
+ void this.element.offsetHeight;
+ requestAnimationFrame(() => {
+ if (this.element.classList.contains("b3-menu--sheet")) {
+ this.element.style.transform = "translateY(0px)";
}
});
this.element.lastElementChild.scrollTop = 0;