Skip to content

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

:lipstick: Improve mobile emoji panel with a bottom sheet https://github.com/siyuan-note/siyuan/issues/19619

Daniel516a2977a82b
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index 2b3ea5e620..b425b03ab9 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -37,6 +37,9 @@ import {
type TRandomEmojiScope,
} from "./panel";
import {getFileTreeIconHTML, updateFileTreeItemIcon} from "./fileTreeIcon";
+import {bindBottomSheetDialog} from "../mobile/util/bindBottomSheetDialog";
+import {activeBlur} from "../mobile/util/keyboardToolbar";
+import {bindEmojiSheetSelection} from "../mobile/util/bindEmojiSheetSelection";
export {unicode2Emoji};
@@ -845,6 +848,7 @@ export const openEmojiPanel = (
custom?: boolean,
ownerElement?: HTMLElement,
targetID?: string,
+ insertRange?: Range,
}) => {
if (type !== "av") {
window.siyuan.menus.menu.remove();
@@ -892,13 +896,17 @@ export const openEmojiPanel = (
}
};
+ let disposeSheet: (() => void) | undefined;
+ let disposeSelection: (() => void) | undefined;
const dialog = new Dialog({
- disableAnimation: true,
- transparent: true,
+ disableAnimation: !isMobile(),
+ transparent: !isMobile(),
hideCloseIcon: true,
- width: isMobile() ? "80vw" : "368px",
- height: "50vh",
+ width: isMobile() ? "100vw" : "368px",
+ height: isMobile() ? "min(40vh, 360px)" : "50vh",
destroyCallback: () => {
+ disposeSheet?.();
+ disposeSelection?.();
clearPastedCustomIcon();
emojiPanelState.controller?.destroy();
},
@@ -1060,8 +1068,23 @@ export const openEmojiPanel = (
}
dialog.element.querySelector(".b3-dialog__container").setAttribute("data-menu", "true");
const dialogElement = dialog.element.querySelector(".b3-dialog") as HTMLElement;
- dialogElement.style.justifyContent = "inherit";
- dialogElement.style.alignItems = "inherit";
+ if (isMobile()) {
+ const destroyDialog = dialog.destroy.bind(dialog);
+ dialog.destroy = (destroyOptions?: IObject) => {
+ if (dialog.element.contains(document.activeElement)) {
+ activeBlur(true);
+ }
+ destroyDialog(destroyOptions);
+ };
+ disposeSheet = bindBottomSheetDialog(dialog, async () => dialog.destroy());
+ if (type === "insert" && options?.insertRange) {
+ disposeSelection = bindEmojiSheetSelection(
+ dialog.element.querySelector(".b3-dialog__container"), options.insertRange.cloneRange());
+ }
+ } else {
+ dialogElement.style.justifyContent = "inherit";
+ dialogElement.style.alignItems = "inherit";
+ }
let currentTab = window.siyuan.storage[Constants.LOCAL_EMOJIS].currentTab;
const currentTabElement = dialog.element.querySelector(`[data-type="tab-${currentTab}"]`);
if (!currentTabElement || currentTabElement.classList.contains("fn__none")) {
@@ -1071,7 +1094,9 @@ export const openEmojiPanel = (
const currentBodyTab = customEmojiPage ? "emoji" : currentTab;
dialog.element.querySelector(`.emojis__tabheader [data-type="tab-${currentTab}"]`).classList.add("block__icon--active");
dialog.element.querySelector(`.emojis__tabbody [data-type="tab-${currentBodyTab}"]`).classList.remove("fn__none");
- setPosition(dialog.element.querySelector(".b3-dialog__container"), position.x, position.y, position.h, position.w);
+ if (!isMobile()) {
+ setPosition(dialog.element.querySelector(".b3-dialog__container"), position.x, position.y, position.h, position.w);
+ }
const networkIconInputElement = dialog.element.querySelector('[data-type="network-icon-url"]') as HTMLTextAreaElement;
const customIconFileElement = dialog.element.querySelector('[data-type="custom-icon-file"]') as HTMLInputElement;
const customIconNameElement = dialog.element.querySelector('[data-type="custom-icon-name"]') as HTMLInputElement;
diff --git a/app/src/mobile/util/bindEmojiSheetSelection.ts b/app/src/mobile/util/bindEmojiSheetSelection.ts
new file mode 100644
index 0000000000..ddad76f72a
--- /dev/null
+++ b/app/src/mobile/util/bindEmojiSheetSelection.ts
@@ -0,0 +1,62 @@
+import {getSelectionPosition} from "../../protyle/util/selection";
+import {getVisibleViewportBounds} from "./visibleViewport";
+
+export const bindEmojiSheetSelection = (sheet: HTMLElement, range: Range) => {
+ const anchor = range.startContainer.nodeType === Node.ELEMENT_NODE ?
+ range.startContainer as HTMLElement : range.startContainer.parentElement;
+ const content = anchor?.closest<HTMLElement>(".protyle-content");
+ if (!content) {
+ return () => {};
+ }
+ // 在编辑区域外补足滚动空间,使文档末尾的输入位置也能移到面板上方。
+ const spacer = document.createElement("div");
+ spacer.setAttribute("aria-hidden", "true");
+ spacer.style.flexShrink = "0";
+ spacer.style.pointerEvents = "none";
+ content.append(spacer);
+ let frame = 0;
+ const update = () => {
+ frame = 0;
+ if (!sheet.isConnected || !content.contains(range.startContainer)) {
+ return;
+ }
+ const viewport = getVisibleViewportBounds();
+ const contentRect = content.getBoundingClientRect();
+ // 使用面板的布局高度,避免入场动画和下拉手势影响避让位置。
+ const dialogRect = sheet.parentElement.getBoundingClientRect();
+ const dialogStyle = getComputedStyle(sheet.parentElement);
+ const sheetTop = dialogRect.bottom - parseFloat(dialogStyle.paddingBottom || "0") - sheet.offsetHeight;
+ const bottom = Math.min(contentRect.bottom, viewport.bottom, sheetTop);
+ const top = Math.max(contentRect.top, viewport.top);
+ if (bottom <= top) {
+ return;
+ }
+ const lineHeight = parseFloat(getComputedStyle(anchor).lineHeight) ||
+ window.siyuan.config.editor.fontSize * 1.625;
+ spacer.style.height = `${Math.max(0, contentRect.bottom - bottom) + lineHeight}px`;
+ const cursorTop = getSelectionPosition(content, range.cloneRange()).top;
+ const visibleCursorTop = Math.max(top, bottom - lineHeight * 2);
+ if (cursorTop > visibleCursorTop) {
+ content.scrollTop += cursorTop - visibleCursorTop;
+ } else if (cursorTop < top) {
+ content.scrollTop += cursorTop - top;
+ }
+ };
+ const schedule = () => {
+ cancelAnimationFrame(frame);
+ frame = requestAnimationFrame(update);
+ };
+ const observer = new ResizeObserver(schedule);
+ observer.observe(sheet);
+ observer.observe(content);
+ window.visualViewport?.addEventListener("resize", schedule);
+ window.visualViewport?.addEventListener("scroll", schedule);
+ schedule();
+ return () => {
+ cancelAnimationFrame(frame);
+ observer.disconnect();
+ window.visualViewport?.removeEventListener("resize", schedule);
+ window.visualViewport?.removeEventListener("scroll", schedule);
+ spacer.remove();
+ };
+};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 9d39feaac4..45fd9d4fb3 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -743,7 +743,7 @@ ${genHintItemHTML(item)}
focusByRange(protyle.toolbar.range);
insertHTML(protyle.lute.SpinBlockDOM(genEmojiInsertHTML(unicode)), protyle, false, true,
false, undefined, undoContext);
- }, undefined, {targetID});
+ }, undefined, {targetID, insertRange: range});
}
private destroyEmojiPanel() {