Skip to content

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

:sparkles: Improve mobile backlink and attribute bottom sheets https://github.com/siyuan-note/siyuan/issues/19250

Daniel0d2ab51c1ef0
diff --git a/app/scripts/mobileBacklinks.test.js b/app/scripts/mobileBacklinks.test.js
index 1fcacb4d6c..24f308dcc3 100644
--- a/app/scripts/mobileBacklinks.test.js
+++ b/app/scripts/mobileBacklinks.test.js
@@ -9,7 +9,7 @@ const sources = () => {
const modules = {};
for (const name of ["layout/dock/BacklinkContent", "layout/dock/backlinkRefresh",
"layout/dock/backlinkReadingAnchor", "layout/dock/backlinkSourceFilter", "mobile/util/secondaryEditors",
- "mobile/util/backlinkPanels", "mobile/util/openBacklinks", "protyle/util/transactionQueue",
+ "mobile/util/backlinkPanels", "mobile/util/openBacklinks", "mobile/util/bindBottomSheetDrag", "mobile/util/bindBottomSheetDialog", "protyle/util/transactionQueue",
"util/escape", "dialog/index"]) {
modules[name] = ts.transpileModule(preprocess(
readFileSync(path.join(__dirname, "../src", name + ".ts"), "utf8"),
@@ -199,6 +199,48 @@ const runCases = async (sources) => {
for (const request of requests.splice(0)) {request.reply(list);}
await tick();
assert.equal(window.siyuan.dialogs.length, 0);
+ const dragElement = document.createElement("div");
+ dragElement.style.height = "400px";
+ dragElement.innerHTML = '<div class="heading">Backlinks</div><div contenteditable="true">editable</div><div class="scroll" style="height:40px;overflow:auto"><div style="height:200px">content</div></div>';
+ document.body.appendChild(dragElement);
+ let closeCount = 0;
+ const disposeDrag = load("mobile/util/bindBottomSheetDrag").bindBottomSheetDrag(dragElement,
+ document.createElement("div"), async () => {closeCount++;});
+ const touch = (target, type, y, x = 10) => {
+ const event = new Event(type, {bubbles: true, cancelable: true});
+ Object.defineProperties(event, {
+ touches: {value: type === "touchend" ? [] : [{clientX: x, clientY: y}]},
+ changedTouches: {value: [{clientX: x, clientY: y}]},
+ });
+ target.dispatchEvent(event);
+ return event;
+ };
+ const heading = dragElement.firstElementChild;
+ touch(heading, "touchstart", 0);
+ assert.equal(touch(heading, "touchmove", 150).defaultPrevented, true);
+ assert.equal(dragElement.style.transform, "translateY(150px)");
+ touch(heading, "touchend", 150);
+ await tick();
+ assert.equal(closeCount, 1);
+ assert.equal(heading.dispatchEvent(new MouseEvent("click", {bubbles: true, cancelable: true})), false);
+ touch(heading, "touchstart", 0);
+ touch(heading, "touchmove", 10);
+ touch(heading, "touchend", 10);
+ assert.equal(dragElement.style.transform, "");
+ assert.equal(closeCount, 1, "short drag must rebound");
+ for (const target of [dragElement.querySelector('[contenteditable="true"]'), dragElement.querySelector(".scroll > div")]) {
+ dragElement.querySelector(".scroll").scrollTop = 30;
+ touch(target, "touchstart", 0);
+ assert.equal(touch(target, "touchmove", 150).defaultPrevented, false);
+ touch(target, "touchend", 150);
+ assert.equal(closeCount, 1, "editing and scrolled content must retain native gestures");
+ }
+ touch(heading, "touchstart", 0);
+ touch(heading, "touchmove", 80);
+ touch(heading, "touchcancel", 80);
+ assert.equal(dragElement.style.transform, "");
+ assert.equal(closeCount, 1);
+ disposeDrag();
return "Mobile backlink lifecycle cases passed";
};
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index 7da98d6119..76350be107 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -1549,7 +1549,7 @@ body.mobile-topbar--merged {
padding: var(--mobile-top-safe-area) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
-.mobile-backlinks-dialog {
+.mobile-bottom-sheet-dialog {
.b3-dialog {
align-items: flex-end;
box-sizing: border-box;
@@ -1557,22 +1557,63 @@ body.mobile-topbar--merged {
height: 100%;
}
- .mobile-backlinks-sheet {
- max-height: 100%;
+ .mobile-bottom-sheet {
+ max-height: min(60vh, 100%);
border-radius: 16px 16px 0 0;
- padding-top: 32px;
+ padding-top: 4px;
box-sizing: border-box;
transform: translateY(100%);
transition: transform 150ms ease;
}
- &.b3-dialog--open .mobile-backlinks-sheet {
+ &.b3-dialog--open .mobile-bottom-sheet {
transform: translateY(0);
}
+ .b3-dialog__body {
+ min-height: 0;
+ overflow: hidden;
+ }
+}
+
+.mobile-backlinks-dialog {
.mobile-backlinks-content {
- height: 100%;
+ flex: 0 1 auto;
+ min-height: 0;
+
+ > .backlinkList__header {
+ padding: 4px 12px 8px;
+ gap: 4px;
+
+ > .block__logo {
+ flex: 0 0 100%;
+ box-sizing: border-box;
+ min-height: 28px;
+ margin: 0;
+ padding-right: 32px;
+ }
+
+ > .fn__space {
+ display: none;
+ }
+ }
+
+ > .backlinkList {
+ flex: 0 1 auto;
+ padding-bottom: 8px;
+ }
+ }
+
+}
+
+.mobile-attributes-sheet .b3-dialog__body > .fn__flex-column {
+ flex: 1;
+ min-height: 0;
+
+ > .fn__flex-1 {
min-height: 0;
+ overflow: auto;
+ overscroll-behavior: contain;
}
}
@@ -1590,6 +1631,12 @@ body.mobile-topbar--merged {
min-height: 0;
overflow: auto;
overscroll-behavior: contain;
+
+ > .b3-list > .backlinkList__item > .b3-list-item {
+ margin-left: 0;
+ margin-right: 0;
+ border-radius: 0;
+ }
}
.protyle-breadcrumb__bar {
@@ -1597,8 +1644,15 @@ body.mobile-topbar--merged {
}
.block__icon {
- min-width: 28px;
- min-height: 28px;
+ opacity: 1;
+ box-sizing: border-box;
+ min-width: 32px;
+ min-height: 32px;
+ justify-content: center;
+
+ &[disabled] {
+ opacity: .38;
+ }
}
&--only-backlinks {
diff --git a/app/src/menus/commonMenuItem.ts b/app/src/menus/commonMenuItem.ts
index 5e10783093..e981c3b418 100644
--- a/app/src/menus/commonMenuItem.ts
+++ b/app/src/menus/commonMenuItem.ts
@@ -42,6 +42,10 @@ import {
} from "../editor/assetOpen";
import {resolvePdfAssetLink} from "../editor/pdfAssetLink";
import {getHostCapabilities} from "../util/hostCapabilities";
+/// #if MOBILE
+import {bindBottomSheetDialog} from "../mobile/util/bindBottomSheetDialog";
+import {activeBlur} from "../mobile/util/keyboardToolbar";
+/// #endif
const bindAttrInput = (inputElement: HTMLInputElement, id: string) => {
inputElement.addEventListener("change", () => {
@@ -221,8 +225,8 @@ export const openFileAttr = (attrs: Record<string, string>, focusName = "bookmar
});
const dialog = new Dialog({
width: isMobile() ? "100vw" : "50vw",
- containerClassName: "b3-dialog__container--theme",
- height: isMobile() ? "100vh" : "80vh",
+ containerClassName: "b3-dialog__container--theme" + (isMobile() ? " mobile-attributes-sheet" : ""),
+ height: isMobile() ? "60vh" : "80vh",
content: `<div class="fn__flex-column">
<div class="layout-tab-bar fn__flex" style="${isMobile() ? "padding-right: 38px;" : ""}flex-shrink:0;border-radius: var(--b3-border-radius-b) var(--b3-border-radius-b) 0 0">
<div class="item item--full item--focus" data-type="attr">
@@ -280,7 +284,11 @@ export const openFileAttr = (attrs: Record<string, string>, focusName = "bookmar
</div>
</div>`,
destroyCallback() {
+ /// #if MOBILE
+ disposeSheet();
+ /// #else
focusByRange(range);
+ /// #endif
if (protyle) {
hideElements(["select"], protyle);
} else {
@@ -288,6 +296,16 @@ export const openFileAttr = (attrs: Record<string, string>, focusName = "bookmar
}
}
});
+ /// #if MOBILE
+ const destroyDialog = dialog.destroy.bind(dialog);
+ dialog.destroy = (options?: IObject) => {
+ if (dialog.element.contains(document.activeElement)) {
+ activeBlur(true);
+ }
+ destroyDialog(options);
+ };
+ const disposeSheet = bindBottomSheetDialog(dialog, async () => dialog.destroy());
+ /// #endif
dialog.element.setAttribute("data-key", Constants.DIALOG_ATTR);
(dialog.element.querySelector('.b3-text-field[data-name="bookmark"]') as HTMLInputElement).value = attrs.bookmark || "";
(dialog.element.querySelector('.b3-text-field[data-name="name"]') as HTMLInputElement).value = attrs.name || "";
diff --git a/app/src/mobile/util/bindBottomSheetDialog.ts b/app/src/mobile/util/bindBottomSheetDialog.ts
new file mode 100644
index 0000000000..f678960d2f
--- /dev/null
+++ b/app/src/mobile/util/bindBottomSheetDialog.ts
@@ -0,0 +1,25 @@
+import type {Dialog} from "../../dialog";
+import {bindBottomSheetDrag} from "./bindBottomSheetDrag";
+
+export const bindBottomSheetDialog = (dialog: Dialog, close: () => Promise<void>) => {
+ dialog.element.classList.add("mobile-bottom-sheet-dialog");
+ const sheet = dialog.element.querySelector<HTMLElement>(".b3-dialog__container");
+ sheet.classList.add("mobile-bottom-sheet");
+ const resize = () => {
+ const viewport = window.visualViewport;
+ const container = dialog.element.querySelector<HTMLElement>(".b3-dialog");
+ if (viewport) {
+ container.style.top = `${viewport.offsetTop}px`;
+ container.style.height = `${viewport.height}px`;
+ }
+ };
+ window.visualViewport?.addEventListener("resize", resize);
+ window.visualViewport?.addEventListener("scroll", resize);
+ resize();
+ const disposeDrag = bindBottomSheetDrag(sheet, dialog.element.querySelector(".b3-dialog__scrim"), close);
+ return () => {
+ disposeDrag();
+ window.visualViewport?.removeEventListener("resize", resize);
+ window.visualViewport?.removeEventListener("scroll", resize);
+ };
+};
diff --git a/app/src/mobile/util/bindBottomSheetDrag.ts b/app/src/mobile/util/bindBottomSheetDrag.ts
new file mode 100644
index 0000000000..249d4d9f84
--- /dev/null
+++ b/app/src/mobile/util/bindBottomSheetDrag.ts
@@ -0,0 +1,101 @@
+// 下拉手势沿用移动端菜单的距离和速度阈值,编辑区域保留原生选区及滚动操作。
+export const bindBottomSheetDrag = (element: HTMLElement, scrim: HTMLElement, close: () => Promise<void>) => {
+ let start: {x: number, y: number, time: number};
+ let dragging = false;
+ let closing = false;
+ let suppressClickUntil = 0;
+ const reset = () => {
+ element.style.transition = "";
+ element.style.transform = "";
+ scrim.style.opacity = "";
+ start = undefined;
+ dragging = false;
+ };
+ const onStart = (event: TouchEvent) => {
+ if (closing) {
+ return;
+ }
+ reset();
+ const target = event.target as HTMLElement;
+ if (event.touches.length !== 1 || target.closest('input, textarea, select, [contenteditable="true"], .protyle, .block__icon, .b3-dialog__close')) {
+ return;
+ }
+ let parent = target;
+ while (parent && parent !== element) {
+ if (parent.scrollHeight > parent.clientHeight + 1 && parent.scrollTop > 0 &&
+ ["auto", "scroll", "overlay"].includes(getComputedStyle(parent).overflowY)) {
+ return;
+ }
+ parent = parent.parentElement;
+ }
+ const touch = event.touches[0];
+ start = {x: touch.clientX, y: touch.clientY, time: performance.now()};
+ };
+ const onMove = (event: TouchEvent) => {
+ if (!start || closing) {
+ return;
+ }
+ if (event.touches.length !== 1) {
+ reset();
+ return;
+ }
+ const touch = event.touches[0];
+ const offset = touch.clientY - start.y;
+ if (!dragging && (offset <= 0 || Math.abs(touch.clientX - start.x) > offset)) {
+ start = undefined;
+ return;
+ }
+ dragging = true;
+ element.style.transition = "none";
+ element.style.transform = `translateY(${Math.max(0, offset)}px)`;
+ scrim.style.opacity = Math.max(0, 1 - Math.max(0, offset) / Math.max(element.clientHeight, 1)).toString();
+ if (event.cancelable) {
+ event.preventDefault();
+ }
+ };
+ const onEnd = (event: TouchEvent) => {
+ if (!dragging || !start || !event.changedTouches.length) {
+ reset();
+ return;
+ }
+ const offset = Math.max(0, event.changedTouches[0].clientY - start.y);
+ const duration = Math.max(performance.now() - start.time, 1);
+ const shouldClose = offset > Math.min(120, element.clientHeight * .25) || (offset > 20 && offset / duration > .6);
+ suppressClickUntil = performance.now() + 300;
+ element.style.transition = "";
+ start = undefined;
+ dragging = false;
+ if (shouldClose) {
+ closing = true;
+ void close().catch(error => console.error(error)).finally(() => {
+ closing = false;
+ reset();
+ });
+ } else {
+ reset();
+ }
+ };
+ const onCancel = () => {
+ if (!closing) {
+ reset();
+ }
+ };
+ const onClick = (event: MouseEvent) => {
+ if (performance.now() < suppressClickUntil) {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ }
+ };
+ element.addEventListener("touchstart", onStart, {passive: true});
+ element.addEventListener("touchmove", onMove, {passive: false});
+ element.addEventListener("touchend", onEnd);
+ element.addEventListener("touchcancel", onCancel);
+ element.addEventListener("click", onClick, true);
+ return () => {
+ element.removeEventListener("touchstart", onStart);
+ element.removeEventListener("touchmove", onMove);
+ element.removeEventListener("touchend", onEnd);
+ element.removeEventListener("touchcancel", onCancel);
+ element.removeEventListener("click", onClick, true);
+ };
+};
diff --git a/app/src/mobile/util/openBacklinks.ts b/app/src/mobile/util/openBacklinks.ts
index 91c6ca472a..1ecd16e3af 100644
--- a/app/src/mobile/util/openBacklinks.ts
+++ b/app/src/mobile/util/openBacklinks.ts
@@ -5,6 +5,7 @@ import {registerMobileBacklinkPanel} from "./backlinkPanels";
import {clearActiveMobileSecondaryEditor, flushMobileSecondaryEditor} from "./secondaryEditors";
import {showMessage} from "../../dialog/message";
import {escapeHtml} from "../../util/escape";
+import {bindBottomSheetDialog} from "./bindBottomSheetDialog";
class MobileBacklinkDialog extends Dialog {
public beforeClose: () => Promise<void>;
@@ -47,32 +48,20 @@ export const openMobileBacklinks = async (protyle: IProtyle, blockId: string) =>
const dialog = new MobileBacklinkDialog({
content: '<div class="mobile-backlinks-content fn__flex-column"></div>',
width: "100vw",
- height: "60vh",
+ height: "auto",
containerClassName: "mobile-backlinks-sheet",
destroyCallback: () => {
+ disposeSheet();
unregisterPanel?.();
panel?.destroy();
if (currentDialog === dialog) {
currentDialog = undefined;
clearActiveMobileSecondaryEditor();
}
- window.visualViewport?.removeEventListener("resize", resize);
- window.visualViewport?.removeEventListener("scroll", resize);
}
});
currentDialog = dialog;
dialog.element.classList.add("mobile-backlinks-dialog");
- const resize = () => {
- const viewport = window.visualViewport;
- const container = dialog.element.querySelector<HTMLElement>(".b3-dialog");
- if (viewport) {
- container.style.top = `${viewport.offsetTop}px`;
- container.style.height = `${viewport.height}px`;
- }
- };
- window.visualViewport?.addEventListener("resize", resize);
- window.visualViewport?.addEventListener("scroll", resize);
- resize();
const panel = new BacklinkContent({
app: protyle.app,
element: dialog.element.querySelector(".mobile-backlinks-content"),
@@ -102,4 +91,5 @@ export const openMobileBacklinks = async (protyle: IProtyle, blockId: string) =>
}
};
const unregisterPanel = registerMobileBacklinkPanel(panel, () => dialog.close());
+ const disposeSheet = bindBottomSheetDialog(dialog, () => dialog.close());
};
diff --git a/app/src/util/Tree.ts b/app/src/util/Tree.ts
index abc419a91b..1b9fbd1b2a 100644
--- a/app/src/util/Tree.ts
+++ b/app/src/util/Tree.ts
@@ -115,7 +115,7 @@ export class Tree {
} else {
style = `padding-left: ${(item.depth * 18) || 4}px;margin-right: 2px`;
}
- const showArrow = hasChild || (item.type === "backlink" && !isM);
+ const showArrow = hasChild || item.type === "backlink";
// data-id 需要添加 item.id,否则大纲更新时 name 不一致导致 https://github.com/siyuan-note/siyuan/issues/11843
html += `<li class="b3-list-item${isM ? "" : " b3-list-item--hide-action"}"
${item.id ? 'data-node-id="' + item.id + '"' : ""}