Skip to content

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

:art: Support relative font sizes in the mobile inline element menu https://github.com/siyuan-note/siyuan/issues/15606

Daniel21505d120d1f
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index aef29149ef..b284c9e58a 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -502,6 +502,34 @@
margin-left: 6px;
}
}
+
+ &__font-size {
+ align-items: center;
+ box-sizing: border-box;
+ display: flex;
+ min-height: 48px;
+ padding-right: 8px;
+ width: 100%;
+
+ .b3-slider {
+ height: 32px;
+ }
+
+ [data-type="fontSizeValue"] {
+ flex-shrink: 0;
+ text-align: right;
+ width: 48px;
+ }
+
+ &-toggle {
+ align-items: center;
+ box-sizing: border-box;
+ display: flex;
+ min-height: 40px;
+ padding-right: 8px;
+ width: 100%;
+ }
+ }
}
#empty {
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 0a94ca3570..bac1ee02a0 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -9,7 +9,7 @@ import {moveToDown, moveToUp} from "../../protyle/wysiwyg/move";
import {Constants} from "../../constants";
import {focusBlock, focusByRange, getSelectionPosition} from "../../protyle/util/selection";
import {getCurrentEditor} from "../editor";
-import {fontEvent, getFontNodeElements} from "../../protyle/toolbar/Font";
+import {convertFontSize, fontEvent, getFontNodeElements, getFontSizeInfo} from "../../protyle/toolbar/Font";
import {hideElements} from "../../protyle/ui/hideElements";
import {softEnter} from "../../protyle/wysiwyg/enter";
import {isInAndroid, isInEdge, isInHarmony, isInMobileApp} from "../../protyle/util/compatibility";
@@ -153,19 +153,7 @@ export const renderTextMenu = (protyle: IProtyle, toolbarElement: Element) => {
});
lastColorHTML += "</div>";
}
- let textElement: HTMLElement;
- let fontSize = "16px";
- if (nodeElements && nodeElements.length > 0) {
- textElement = nodeElements[0] as HTMLElement;
- } else {
- textElement = protyle.toolbar.range.cloneContents().querySelector('[data-type~="text"]') as HTMLElement;
- if (!textElement) {
- textElement = hasClosestByAttribute(protyle.toolbar.range.startContainer, "data-type", "text") as HTMLElement;
- }
- }
- if (textElement) {
- fontSize = textElement.style.fontSize || "16px";
- }
+ const {fontSize, baseFontSize} = getFontSizeInfo(protyle, nodeElements);
const utilElement = toolbarElement.querySelector(".keyboard__util") as HTMLElement;
utilElement.innerHTML = `${lastColorHTML}
<div data-id="color" class="keyboard__slash-title">${window.siyuan.languages.color}</div>
@@ -214,23 +202,53 @@ export const renderTextMenu = (protyle: IProtyle, toolbarElement: Element) => {
</div>
<div data-id="fontSize" class="keyboard__slash-title${disableFont ? " fn__none" : ""}">${window.siyuan.languages.fontSize}</div>
<div data-id="fontSizeWrap" class="keyboard__slash-block${disableFont ? " fn__none" : ""}">
- <select class="b3-select fn__block" style="width: calc(50% - 8px);margin: 4px 0 8px 0;">
- <option ${fontSize === "12px" ? "selected" : ""} value="12px">12px</option>
- <option ${fontSize === "13px" ? "selected" : ""} value="13px">13px</option>
- <option ${fontSize === "14px" ? "selected" : ""} value="14px">14px</option>
- <option ${fontSize === "15px" ? "selected" : ""} value="15px">15px</option>
- <option ${fontSize === "16px" ? "selected" : ""} value="16px">16px</option>
- <option ${fontSize === "19px" ? "selected" : ""} value="19px">19px</option>
- <option ${fontSize === "22px" ? "selected" : ""} value="22px">22px</option>
- <option ${fontSize === "24px" ? "selected" : ""} value="24px">24px</option>
- <option ${fontSize === "29px" ? "selected" : ""} value="29px">29px</option>
- <option ${fontSize === "32px" ? "selected" : ""} value="32px">32px</option>
- <option ${fontSize === "40px" ? "selected" : ""} value="40px">40px</option>
- <option ${fontSize === "48px" ? "selected" : ""} value="48px">48px</option>
- </select>
+ <label class="keyboard__font-size-toggle">
+ ${window.siyuan.languages.relativeFontSize}
+ <span class="fn__flex-1"></span>
+ <input class="b3-switch fn__flex-center" ${fontSize.endsWith("em") ? "checked" : ""} type="checkbox">
+ </label>
+ <label class="keyboard__font-size${fontSize.endsWith("em") ? " fn__none" : ""}">
+ <input class="b3-slider fn__flex-1" data-type="fontSizePX" max="72" min="9" step="1" type="range" value="${parseFloat(fontSize)}">
+ <span data-type="fontSizeValue">${parseFloat(fontSize)}px</span>
+ </label>
+ <label class="keyboard__font-size${fontSize.endsWith("em") ? "" : " fn__none"}">
+ <input class="b3-slider fn__flex-1" data-type="fontSizeEM" max="4.5" min="0.56" step="0.01" type="range" value="${parseFloat(fontSize)}">
+ <span data-type="fontSizeValue">${(parseFloat(fontSize) * 100).toFixed(0)}%</span>
+ </label>
</div>`;
- utilElement.querySelector("select").addEventListener("change", function (event: Event) {
- fontEvent(protyle, nodeElements, "fontSize", (event.target as HTMLSelectElement).value);
+ const switchElement = utilElement.querySelector('[data-id="fontSizeWrap"] .b3-switch') as HTMLInputElement;
+ const fontSizePXElement = utilElement.querySelector('[data-type="fontSizePX"]') as HTMLInputElement;
+ const fontSizeEMElement = utilElement.querySelector('[data-type="fontSizeEM"]') as HTMLInputElement;
+ const updatePXValue = () => {
+ fontSizePXElement.nextElementSibling.textContent = fontSizePXElement.value + "px";
+ };
+ const updateEMValue = () => {
+ fontSizeEMElement.nextElementSibling.textContent = (parseFloat(fontSizeEMElement.value) * 100).toFixed(0) + "%";
+ };
+ switchElement.addEventListener("change", () => {
+ if (switchElement.checked) {
+ const em = convertFontSize(fontSizePXElement.value + "px", "em", baseFontSize);
+ fontSizeEMElement.value = parseFloat(em).toString();
+ updateEMValue();
+ fontSizePXElement.parentElement.classList.add("fn__none");
+ fontSizeEMElement.parentElement.classList.remove("fn__none");
+ fontEvent(protyle, nodeElements, "fontSize", fontSizeEMElement.value + "em");
+ } else {
+ const px = convertFontSize(fontSizeEMElement.value + "em", "px", baseFontSize);
+ fontSizePXElement.value = parseFloat(px).toString();
+ updatePXValue();
+ fontSizePXElement.parentElement.classList.remove("fn__none");
+ fontSizeEMElement.parentElement.classList.add("fn__none");
+ fontEvent(protyle, nodeElements, "fontSize", fontSizePXElement.value + "px");
+ }
+ });
+ fontSizePXElement.addEventListener("input", updatePXValue);
+ fontSizeEMElement.addEventListener("input", updateEMValue);
+ fontSizePXElement.addEventListener("change", () => {
+ fontEvent(protyle, nodeElements, "fontSize", fontSizePXElement.value + "px");
+ });
+ fontSizeEMElement.addEventListener("change", () => {
+ fontEvent(protyle, nodeElements, "fontSize", fontSizeEMElement.value + "em");
});
};
diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts
index 91b516208c..e3cba56dfa 100644
--- a/app/src/protyle/toolbar/Font.ts
+++ b/app/src/protyle/toolbar/Font.ts
@@ -29,6 +29,47 @@ export class Font extends ToolbarItem {
}
}
+export const getFontSizeInfo = (protyle: IProtyle, nodeElements?: Element[]) => {
+ let textElement: HTMLElement;
+ let fontSizeElement: HTMLElement;
+ if (nodeElements && nodeElements.length > 0) {
+ textElement = nodeElements[0] as HTMLElement;
+ fontSizeElement = textElement;
+ } else {
+ textElement = hasClosestByAttribute(protyle.toolbar.range.startContainer, "data-type", "text") as HTMLElement;
+ if (!textElement) {
+ textElement = protyle.toolbar.range.cloneContents().querySelector('[data-type~="text"]') as HTMLElement;
+ }
+ const startContainer = protyle.toolbar.range.startContainer;
+ fontSizeElement = startContainer.nodeType === Node.ELEMENT_NODE ?
+ startContainer as HTMLElement : startContainer.parentElement;
+ }
+
+ let baseFontSize = window.siyuan.config.editor.fontSize;
+ const baseElement = textElement?.isConnected ? textElement.parentElement : fontSizeElement;
+ if (baseElement) {
+ baseFontSize = parseFloat(getComputedStyle(baseElement).fontSize) || baseFontSize;
+ }
+
+ let fontSize = textElement?.style.fontSize;
+ if (!fontSize && fontSizeElement) {
+ fontSize = getComputedStyle(fontSizeElement).fontSize;
+ }
+ return {
+ fontSize: fontSize || window.siyuan.config.editor.fontSize + "px",
+ baseFontSize,
+ };
+};
+
+export const convertFontSize = (fontSize: string, unit: "px" | "em", baseFontSize: number) => {
+ const value = parseFloat(fontSize);
+ const base = baseFontSize || window.siyuan.config.editor.fontSize;
+ if (unit === "em") {
+ return fontSize.endsWith("em") ? value + "em" : parseFloat((value / base).toFixed(2)) + "em";
+ }
+ return fontSize.endsWith("px") ? Math.round(value) + "px" : Math.round(value * base) + "px";
+};
+
export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[]) => {
let colorHTML = "";
["", "var(--b3-font-color1)", "var(--b3-font-color2)", "var(--b3-font-color3)", "var(--b3-font-color4)",
@@ -94,19 +135,7 @@ export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[]) => {
});
lastColorHTML += "</div>";
}
- let textElement: HTMLElement;
- let fontSize = window.siyuan.config.editor.fontSize + "px";
- if (nodeElements && nodeElements.length > 0) {
- textElement = nodeElements[0] as HTMLElement;
- } else {
- textElement = protyle.toolbar.range.cloneContents().querySelector('[data-type~="text"]') as HTMLElement;
- if (!textElement) {
- textElement = hasClosestByAttribute(protyle.toolbar.range.startContainer, "data-type", "text") as HTMLElement;
- }
- }
- if (textElement) {
- fontSize = textElement.style.fontSize || window.siyuan.config.editor.fontSize + "px";
- }
+ const {fontSize, baseFontSize} = getFontSizeInfo(protyle, nodeElements);
element.innerHTML = `${lastColorHTML}
<div class="fn__hr"></div>
<div data-id="color">${window.siyuan.languages.color}</div>
@@ -191,18 +220,17 @@ export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[]) => {
const fontSizeEMElement = element.querySelector("#fontSizeEM") as HTMLInputElement;
switchElement.addEventListener("change", function () {
if (switchElement.checked) {
- // px -> em
- const em = parseFloat((parseInt(fontSizePXElement.value) / 16).toFixed(2));
- fontSizeEMElement.parentElement.setAttribute("aria-label", (em * 100).toString() + "%");
- fontSizeEMElement.value = em.toString();
+ const em = convertFontSize(fontSizePXElement.value + "px", "em", baseFontSize);
+ fontSizeEMElement.parentElement.setAttribute("aria-label", (parseFloat(em) * 100).toFixed(0) + "%");
+ fontSizeEMElement.value = parseFloat(em).toString();
fontSizePXElement.parentElement.classList.add("fn__none");
fontSizeEMElement.parentElement.classList.remove("fn__none");
fontEvent(protyle, nodeElements, "fontSize", fontSizeEMElement.value + "em");
} else {
- const px = Math.round(parseFloat(fontSizeEMElement.value) * 16);
- fontSizePXElement.parentElement.setAttribute("aria-label", px + "px");
- fontSizePXElement.value = px.toString();
+ const px = convertFontSize(fontSizeEMElement.value + "em", "px", baseFontSize);
+ fontSizePXElement.parentElement.setAttribute("aria-label", px);
+ fontSizePXElement.value = parseFloat(px).toString();
fontSizePXElement.parentElement.classList.remove("fn__none");
fontSizeEMElement.parentElement.classList.add("fn__none");