Skip to content

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

:zap: Optimize emoji picker scrolling and layout https://github.com/siyuan-note/siyuan/issues/13563

Daniele3e5111a6c48
diff --git a/app/appearance/icons/index.html b/app/appearance/icons/index.html
index a73ffc2fab..1e4376ad6a 100644
--- a/app/appearance/icons/index.html
+++ b/app/appearance/icons/index.html
@@ -1090,6 +1090,12 @@
</svg>
iconRefresh
</div>
+ <div>
+ <svg>
+ <use xlink:href="#iconDices"></use>
+ </svg>
+ iconDices
+ </div>
<div>
<svg>
<use xlink:href="#iconUnlock"></use>
diff --git a/app/appearance/icons/litheness/icon.js b/app/appearance/icons/litheness/icon.js
index bfbb66e5e9..aeccbe9118 100644
--- a/app/appearance/icons/litheness/icon.js
+++ b/app/appearance/icons/litheness/icon.js
@@ -546,6 +546,9 @@ document.body.insertAdjacentHTML('afterbegin', `<svg id="iconsLitheness" style="
<symbol id="iconRefresh" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">
<path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/>
</symbol>
+ <symbol id="iconDices" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">
+ <rect width="12" height="12" x="2" y="10" rx="2" ry="2"/><path d="m17.92 14 3.5-3.5a2.24 2.24 0 0 0 0-3l-5-4.92a2.24 2.24 0 0 0-3 0L10 6"/><path d="M6 18h.01"/><path d="M10 14h.01"/><path d="M15 6h.01"/><path d="M18 9h.01"/>
+ </symbol>
<symbol id="iconUnlock" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">
<rect width="18" height="11" x="3" y="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 9.9-1"/>
</symbol>
diff --git a/app/src/assets/scss/business/_emojis.scss b/app/src/assets/scss/business/_emojis.scss
index caa82dc2a4..1a8f9f396d 100644
--- a/app/src/assets/scss/business/_emojis.scss
+++ b/app/src/assets/scss/business/_emojis.scss
@@ -1,5 +1,7 @@
.emojis {
+ --emojis-background: var(--b3-theme-surface);
+
word-break: break-all;
white-space: normal;
display: flex;
@@ -46,6 +48,7 @@
}
&__item {
+ box-sizing: border-box;
font-size: 19px;
font-family: var(--b3-font-family-emoji);
font-variant-emoji: emoji;
@@ -79,7 +82,7 @@
z-index: 1;
top: 0;
color: var(--b3-theme-on-surface);
- background-color: var(--b3-theme-background);
+ background-color: var(--emojis-background);
padding: 8px 4px 4px 4px;
}
@@ -90,7 +93,8 @@
&__panel {
flex: 1;
overflow: auto;
- padding: 0 8px;
+ margin-right: 4px;
+ padding: 0 4px 0 8px;
}
&__link {
@@ -227,18 +231,28 @@
height: 1px;
}
+ &__types {
+ display: flex;
+ flex-shrink: 0;
+ gap: 2px;
+ padding: 4px 8px 0;
+ }
+
&__type {
cursor: pointer;
- flex: 1;
+ flex: 1 1 0;
+ min-width: 0;
display: flex;
align-items: center;
justify-content: center;
+ box-sizing: border-box;
height: 28px;
- line-height: 28px;
+ line-height: 1;
transition: var(--b3-list-hover);
font-size: 16px;
background-color: transparent;
border: 0;
+ border-radius: var(--b3-border-radius);
padding: 0;
font-family: var(--b3-font-family-emoji);
font-variant-emoji: emoji;
@@ -254,3 +268,7 @@
}
}
}
+
+.protyle-hint .emojis {
+ --emojis-background: var(--b3-menu-background);
+}
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index 58af2ba3e1..2df53f6535 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -102,10 +102,11 @@ const emojiCategoryIcons: Record<string, string> = {
flags: "1f6a9",
};
-const genEmojiSection = (title: string, content: string, groupName?: string) => {
+const genEmojiSection = (title: string, content: string, groupName?: string, categoryID?: string) => {
const groupAttribute = typeof groupName === "string" ? ` data-group="${escapeAttr(escapeHtml(groupName))}"` : "";
+ const categoryAttribute = categoryID ? ` data-category="${escapeAttr(categoryID)}"` : "";
const titleHTML = title ? `<div class="emojis__title">${escapeHtml(title)}</div>` : "";
- return `<div class="emojis__section"${groupAttribute}>${titleHTML}<div class="emojis__content">${content}</div></div>`;
+ return `<div class="emojis__section"${groupAttribute}${categoryAttribute}>${titleHTML}<div class="emojis__content">${content}</div></div>`;
};
const getRecentEmojiButtons = (key: string, options: TEmojiPanelOptions) => {
@@ -191,9 +192,21 @@ export class EmojiPanelController {
private customOffset = 0;
private customObserver?: IntersectionObserver;
private imageObserver?: IntersectionObserver;
+ private resizeObserver: ResizeObserver;
private searchMode = false;
+ private scrollFrame = 0;
+ private categoryOffsets: {id: string, top: number, element: HTMLElement}[] = [];
constructor(private panelElement: HTMLElement, private typeElement: HTMLElement, private options: TEmojiPanelOptions) {
+ this.panelElement.addEventListener("scroll", this.handleScroll, {passive: true});
+ this.resizeObserver = new ResizeObserver(() => {
+ if (!this.searchMode && this.categoryID !== "custom") {
+ this.updateBuiltInPlaceholders();
+ this.updateCategoryOffsets();
+ this.updateVirtualCategories();
+ }
+ });
+ this.resizeObserver.observe(this.panelElement);
}
public setOptions(options: TEmojiPanelOptions) {
@@ -212,6 +225,19 @@ export class EmojiPanelController {
this.renderInitial();
return;
}
+
+ if (category?.id !== "custom") {
+ const targetElement = this.getCategoryElement(categoryID);
+ if (!this.searchMode && targetElement) {
+ this.categoryID = categoryID;
+ this.updateCurrentType();
+ this.scrollToCategory(targetElement);
+ return;
+ }
+ this.renderCommonPage(categoryID);
+ return;
+ }
+
this.disconnectObservers();
this.panelElement.innerHTML = "";
this.panelElement.scrollTop = 0;
@@ -220,21 +246,12 @@ export class EmojiPanelController {
this.typeElement.classList.remove("fn__none");
this.updateCurrentType();
- if (categoryID === "recent") {
- const html = getRecentEmojiButtons("", this.options);
- this.panelElement.innerHTML = html ? genEmojiSection(window.siyuan.languages.recentEmoji, html) :
- `<div class="emojis__section"><div class="emojis__title">${window.siyuan.languages.emptyContent}</div></div>`;
- } else if (category.id === "custom") {
- if (category.items.length === 0) {
- this.panelElement.innerHTML = `<div class="emojis__section"><div class="emojis__title">${window.siyuan.languages.setEmojiTip}</div></div>`;
- } else {
- this.customGroups = groupCustomEmojiItems(category.items);
- this.customOffset = 0;
- this.loadMoreCustomEmojis();
- }
+ if (category.items.length === 0) {
+ this.panelElement.innerHTML = `<div class="emojis__section"><div class="emojis__title">${window.siyuan.languages.setEmojiTip}</div></div>`;
} else {
- const html = category.items.map((item) => genEmojiButton(item.unicode, getEmojiDesc(item), true)).join("");
- this.panelElement.innerHTML = genEmojiSection(getEmojiTitle(window.siyuan.emojis.indexOf(category)), html);
+ this.customGroups = groupCustomEmojiItems(category.items);
+ this.customOffset = 0;
+ this.loadMoreCustomEmojis();
}
this.observeImages(this.panelElement);
this.selectFirst();
@@ -312,8 +329,154 @@ export class EmojiPanelController {
public destroy() {
this.disconnectObservers();
+ this.resizeObserver.disconnect();
+ this.panelElement.removeEventListener("scroll", this.handleScroll);
+ if (this.scrollFrame) {
+ cancelAnimationFrame(this.scrollFrame);
+ }
+ }
+
+ private renderCommonPage(categoryID: string) {
+ this.disconnectObservers();
+ const sections: string[] = [];
+ const recentHTML = getRecentEmojiButtons("", this.options);
+ if (recentHTML) {
+ sections.push(genEmojiSection(window.siyuan.languages.recentEmoji, recentHTML, undefined, "recent"));
+ }
+ window.siyuan.emojis.forEach((item, index) => {
+ if (item.id === "custom") {
+ return;
+ }
+ sections.push(genEmojiSection(getEmojiTitle(index), "", undefined, item.id));
+ });
+ this.panelElement.innerHTML = sections.join("") ||
+ `<div class="emojis__section"><div class="emojis__title">${window.siyuan.languages.emptyContent}</div></div>`;
+ this.panelElement.scrollTop = 0;
+ this.updateBuiltInPlaceholders();
+ this.updateCategoryOffsets();
+ this.categoryID = this.getCategoryElement(categoryID)?.dataset.category ||
+ this.panelElement.querySelector<HTMLElement>(".emojis__section[data-category]")?.dataset.category || "recent";
+ this.searchMode = false;
+ this.typeElement.classList.remove("fn__none");
+ this.updateCurrentType();
+ this.observeImages(this.panelElement);
+ const targetElement = this.getCategoryElement(this.categoryID);
+ if (targetElement) {
+ this.loadBuiltInCategory(targetElement);
+ this.scrollToCategory(targetElement);
+ this.updateVirtualCategories();
+ } else {
+ this.selectFirst();
+ }
+ }
+
+ private getCategoryElement(categoryID: string) {
+ return Array.from(this.panelElement.querySelectorAll<HTMLElement>(".emojis__section[data-category]"))
+ .find((item) => item.dataset.category === categoryID);
+ }
+
+ private scrollToCategory(targetElement: HTMLElement) {
+ this.loadBuiltInCategory(targetElement);
+ this.panelElement.querySelector(".emojis__item--current")?.classList.remove("emojis__item--current");
+ targetElement.querySelector(".emojis__item")?.classList.add("emojis__item--current");
+ const categoryOffset = this.categoryOffsets.find((item) => item.id === targetElement.dataset.category);
+ if (categoryOffset) {
+ this.panelElement.scrollTop = categoryOffset.top;
+ }
+ }
+
+ private updateCategoryOffsets() {
+ const panelTop = this.panelElement.getBoundingClientRect().top;
+ this.categoryOffsets = Array.from(this.panelElement.querySelectorAll<HTMLElement>(".emojis__section[data-category]"))
+ .map((item) => ({
+ id: item.dataset.category || "",
+ top: item.getBoundingClientRect().top - panelTop + this.panelElement.scrollTop,
+ element: item,
+ }));
}
+ private updateBuiltInPlaceholders() {
+ this.panelElement.querySelectorAll<HTMLElement>(".emojis__section[data-category]").forEach((sectionElement) => {
+ const category = window.siyuan.emojis.find((item) => item.id === sectionElement.dataset.category && item.id !== "custom");
+ const contentElement = sectionElement.querySelector<HTMLElement>(".emojis__content");
+ if (!category || !contentElement || contentElement.clientWidth === 0) {
+ return;
+ }
+ const columnCount = Math.max(1, Math.floor(contentElement.clientWidth / 34));
+ contentElement.style.height = `${Math.ceil(category.items.length / columnCount) * 34}px`;
+ });
+ }
+
+ private loadBuiltInCategory(sectionElement: HTMLElement) {
+ const category = window.siyuan.emojis.find((item) => item.id === sectionElement.dataset.category && item.id !== "custom");
+ const contentElement = sectionElement.querySelector<HTMLElement>(".emojis__content");
+ if (!category || !contentElement || contentElement.childElementCount > 0) {
+ return;
+ }
+ contentElement.innerHTML = category.items
+ .map((item) => genEmojiButton(item.unicode, getEmojiDesc(item), true)).join("");
+ this.observeImages(contentElement);
+ }
+
+ private unloadBuiltInCategory(sectionElement: HTMLElement) {
+ const category = window.siyuan.emojis.find((item) => item.id === sectionElement.dataset.category && item.id !== "custom");
+ const contentElement = sectionElement.querySelector<HTMLElement>(".emojis__content");
+ if (!category || !contentElement || contentElement.childElementCount === 0) {
+ return;
+ }
+ contentElement.querySelectorAll("img[data-src]").forEach((item) => this.imageObserver?.unobserve(item));
+ contentElement.innerHTML = "";
+ }
+
+ private updateVirtualCategories() {
+ const viewportHeight = this.panelElement.clientHeight;
+ if (viewportHeight === 0 || this.categoryOffsets.length === 0) {
+ return;
+ }
+ const renderStart = Math.max(0, this.panelElement.scrollTop - viewportHeight);
+ const renderEnd = this.panelElement.scrollTop + viewportHeight * 2;
+ this.categoryOffsets.forEach((item, index) => {
+ const category = window.siyuan.emojis.find((emoji) => emoji.id === item.id && emoji.id !== "custom");
+ if (!category) {
+ return;
+ }
+ const sectionEnd = this.categoryOffsets[index + 1]?.top ?? this.panelElement.scrollHeight;
+ if (item.top <= renderEnd && sectionEnd >= renderStart) {
+ this.loadBuiltInCategory(item.element);
+ } else {
+ this.unloadBuiltInCategory(item.element);
+ }
+ });
+ }
+
+ private handleScroll = () => {
+ if (this.searchMode || this.categoryID === "custom" || this.scrollFrame) {
+ return;
+ }
+ this.scrollFrame = requestAnimationFrame(() => {
+ this.scrollFrame = 0;
+ if (this.categoryOffsets.length === 0) {
+ return;
+ }
+ let currentCategory = this.categoryOffsets[0];
+ this.categoryOffsets.some((item) => {
+ if (item.top > this.panelElement.scrollTop + 1) {
+ return true;
+ }
+ currentCategory = item;
+ return false;
+ });
+ if (this.panelElement.scrollTop + this.panelElement.clientHeight >= this.panelElement.scrollHeight - 1) {
+ currentCategory = this.categoryOffsets[this.categoryOffsets.length - 1];
+ }
+ if (currentCategory.id !== this.categoryID) {
+ this.categoryID = currentCategory.id;
+ this.updateCurrentType();
+ }
+ this.updateVirtualCategories();
+ });
+ };
+
private selectFirst() {
this.panelElement.querySelector(".emojis__item")?.classList.add("emojis__item--current");
}
@@ -513,11 +676,11 @@ export const openEmojiPanel = (
<input class="b3-form__icon-input b3-text-field fn__block" placeholder="${window.siyuan.languages.searchPlaceholder}">
</label>
<span class="fn__space"></span>
- <span class="block__icon block__icon--show fn__flex-center ariaLabel" data-action="random" aria-label="${window.siyuan.languages.random}"><svg><use xlink:href="#iconRefresh"></use></svg></span>
+ <span class="block__icon block__icon--show fn__flex-center ariaLabel" data-action="random" aria-label="${window.siyuan.languages.random}"><svg><use xlink:href="#iconDices"></use></svg></span>
<span class="fn__space"></span>
</div>
<div class="emojis__panel"></div>
- <div class="fn__flex">${genEmojiCategoryButtons(options?.custom)}</div>
+ <div class="emojis__types">${genEmojiCategoryButtons(options?.custom)}</div>
</div>
<div class="fn__none" data-type="tab-dynamic">
<div class="fn__flex emoji__dynamic-color">
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 79dce0dce7..482e09929b 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -493,7 +493,7 @@ ${genHintItemHTML(item)}
// max-height:min(402px,40vh) 和 .protyle-hint 保持一致,否则 emoji 不显示底部导航
this.element.innerHTML = `<div style="padding:0;max-height:min(402px,40vh);width:366px" class="emojis">
<div class="emojis__panel"></div>
-<div class="fn__flex">${genEmojiCategoryButtons()}</div>
+<div class="emojis__types">${genEmojiCategoryButtons()}</div>
</div>`;
panelElement = this.element.querySelector(".emojis__panel") as HTMLElement;
this.emojiPanel = new EmojiPanelController(