Skip to content

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

:bug: Restore mobile toolbar context visibility https://github.com/siyuan-note/siyuan/issues/20034

Daniel7b3a27f75a90
diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.ts
index f33895783c..1ebca10f5c 100644
--- a/app/src/config/entryVisibility/catalog.ts
+++ b/app/src/config/entryVisibility/catalog.ts
@@ -502,6 +502,7 @@ const gutterSingle = () => [
export const SLASH_MENU_ROOT_PATH = "editor.slash.menu";
// 共享工具栏声明决定目录默认顺序,块类型位于首位,字体和字号位于外观之前。
+// 移动端操作按钮默认以插入和块菜单开头,显隐和排序继续使用已有配置标识。
const toolbarBuiltinChildren = [
...MOBILE_TOOLBAR_ACTIONS.map(item => node(getMobileToolbarActionKey(item.name), lang(item.lang), true)),
separator("mobile-separator"),
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 3874257e6b..bf7f153cd9 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -81,7 +81,7 @@ import {isMobile} from "../../util/functions";
import {createFontSizePicker} from "../../protyle/toolbar/fontControls";
import {applyMobileToolbarEntries} from "./toolbarEntries";
import {getEntryOrder, isEntryVisible} from "../../config/entryVisibility/runtime";
-import {TOOLBAR_ENTRY_ROOT_PATH} from "../../protyle/toolbar/defaults";
+import {MOBILE_TOOLBAR_NAMES, TOOLBAR_ENTRY_ROOT_PATH} from "../../protyle/toolbar/defaults";
import {getKeyboardPanelHeight} from "./keyboardPanelHeight";
import {restoreGutterBySelection} from "../../protyle/gutter/restore";
import {mountLiteSlashMenu} from "./liteSlashMenu";
@@ -98,21 +98,28 @@ let toolbarProtyle: IProtyle;
let unmountLiteSlashMenu: (() => void) | undefined;
const applyKeyboardToolbarEntries = (element: HTMLElement, toolbar: Array<string | IMenuItem>) => {
+ const protyle = getCurrentEditor()?.protyle;
+ const selection = getSelection();
+ const range = selection?.rangeCount > 0 && protyle?.wysiwyg.element.contains(selection.anchorNode) &&
+ protyle.wysiwyg.element.contains(selection.focusNode) ? selection.getRangeAt(0) : undefined;
+ const nodeElement = (range && hasClosestBlock(range.startContainer)) || undefined;
+ const inCode = nodeElement?.classList.contains("code-block");
+ const hasText = !!range && !!stripSemanticMarkersFromRangeText(range).split(Constants.ZWSP).join("");
applyMobileToolbarEntries(element, toolbar, {
order: getEntryOrder(TOOLBAR_ENTRY_ROOT_PATH),
isVisible: key => isEntryVisible(`${TOOLBAR_ENTRY_ROOT_PATH}.${key}`),
isAvailable: name => {
- const protyle = getCurrentEditor()?.protyle;
if (name === "block-type") {
- const selection = getSelection();
- return !!protyle && selection?.rangeCount > 0 &&
- !!getBlockTypeSelection(protyle, selection.getRangeAt(0));
+ return !!range && !!getBlockTypeSelection(protyle, range);
}
if (name === "copy" || name === "cut") {
- const selection = getSelection();
- return !!protyle && !protyle.disabled && selection?.rangeCount > 0 && !selection.isCollapsed &&
- protyle.wysiwyg.element.contains(selection.anchorNode) &&
- protyle.wysiwyg.element.contains(selection.focusNode);
+ return !!range && !protyle.disabled && !range.collapsed;
+ }
+ if (name === "outdent" || name === "indent") {
+ return !!nodeElement?.parentElement.classList.contains("li") || !!inCode && hasText;
+ }
+ if (name !== "block-type" && MOBILE_TOOLBAR_NAMES.includes(name)) {
+ return !inCode && hasText;
}
if (name === "block") {
return !!protyle?.gutter || !!protyle && !!getTableCellRichContext(protyle);
diff --git a/app/src/mobile/util/toolbarActions.ts b/app/src/mobile/util/toolbarActions.ts
index c499a535bf..4ac7dca8ff 100644
--- a/app/src/mobile/util/toolbarActions.ts
+++ b/app/src/mobile/util/toolbarActions.ts
@@ -1,12 +1,12 @@
import type {Constants} from "../../constants";
export const MOBILE_TOOLBAR_ACTIONS = [
- {name: "copy", lang: "copy", icon: "iconCopy"},
- {name: "cut", lang: "cut", icon: "iconCut"},
- {name: "outdent", lang: "outdent", icon: "iconOutdent"},
- {name: "indent", lang: "indent", icon: "iconIndent"},
{name: "add", lang: "addAttr", icon: "iconAdd"},
{name: "block", lang: "contentBlock", icon: "iconParagraph"},
+ {name: "outdent", lang: "outdent", icon: "iconOutdent"},
+ {name: "indent", lang: "indent", icon: "iconIndent"},
+ {name: "copy", lang: "copy", icon: "iconCopy"},
+ {name: "cut", lang: "cut", icon: "iconCut"},
{name: "softLine", lang: "wrap", icon: "iconSoftWrap"},
{name: "undo", lang: "undo", icon: "iconUndo"},
{name: "redo", lang: "redo", icon: "iconRedo"},
diff --git a/app/src/mobile/util/toolbarEntries.test.ts b/app/src/mobile/util/toolbarEntries.test.ts
index 4b6a003847..df79546a65 100644
--- a/app/src/mobile/util/toolbarEntries.test.ts
+++ b/app/src/mobile/util/toolbarEntries.test.ts
@@ -5,6 +5,18 @@ import {getDefaultToolbar, getPluginToolbarEntryKey, markPluginToolbarEntries} f
import {getEntryCatalogChildren} from "../../config/entryVisibility/catalog";
import {resolveEntryOrder} from "../../config/entryVisibility/order";
+test("mobile default actions start with add and block without replacing saved order", () => {
+ const catalog = getEntryCatalogChildren("editor.toolbar");
+ const defaults = catalog.map(item => item.key);
+ assert.deepEqual(defaults.slice(0, 6), [
+ "mobile-add", "mobile-block", "mobile-outdent", "mobile-indent", "mobile-copy", "mobile-cut",
+ ]);
+ const saved = ["mobile-copy", "mobile-indent", "mobile-block", "mobile-add", "mobile-outdent"];
+ const order = resolveEntryOrder(defaults, saved,
+ new Set(catalog.filter(item => item.type === "separator").map(item => item.key)));
+ assert.deepEqual(order.filter(key => saved.includes(key)), saved);
+});
+
test("mobile block type availability and visibility do not change the existing block menu", () => {
const root = new ToolbarElement();
const type = new ToolbarElement("block-type");
diff --git a/app/tests/mobileEditingToolbar.test.js b/app/tests/mobileEditingToolbar.test.js
index 1d26e8d1ac..0e18db5bf2 100644
--- a/app/tests/mobileEditingToolbar.test.js
+++ b/app/tests/mobileEditingToolbar.test.js
@@ -16,7 +16,8 @@ const runCases = async (sources, platform) => {
const commands = [];
const nativeKeyboard = platform === "android" || platform === "harmony";
const visible = new Set(["mobile-copy", "mobile-cut", "mobile-undo", "mobile-redo", "mobile-indent",
- "mobile-outdent", "mobile-block", "mobile-add", "mobile-heading1", "strong", "em", "mobile-separator"]);
+ "mobile-outdent", "mobile-block", "mobile-add", "mobile-heading1", "strong", "em", "a", "block-ref", "text",
+ "mobile-separator"]);
const order = ["strong", "mobile-undo", "mobile-separator", "mobile-heading1", "em", "mobile-indent", "mobile-outdent"];
const constants = {INLINE_TYPE: ["a", "block-ref", "strong", "em", "u", "s", "code"], ZWSP: "\u200b",
TIMEOUT_TRANSITION: 20, TIMEOUT_COUNT: 50};
@@ -135,6 +136,12 @@ const runCases = async (sources, platform) => {
assert.ok(inputRequests > 0);
assert.equal(toolbar.querySelector('[data-type="copy"]').classList.contains("fn__none"), false);
assert.equal(toolbar.querySelector('[data-type="cut"]').classList.contains("fn__none"), false);
+ const hidden = name => toolbar.querySelector(`[data-type="${name}"]`).classList.contains("fn__none");
+ assert.equal(hidden("indent"), true);
+ assert.equal(hidden("outdent"), true);
+ for (const name of ["strong", "em", "a", "block-ref", "text"]) {
+ assert.equal(hidden(name), false);
+ }
const contextMenu = new MouseEvent("contextmenu", {bubbles: true, cancelable: true});
editable.dispatchEvent(contextMenu);
assert.equal(contextMenu.defaultPrevented, false);
@@ -183,6 +190,37 @@ const runCases = async (sources, platform) => {
await settle();
assert.equal(toolbar.querySelector('[data-type="heading1"]').classList.contains("fn__none"), true);
current.lite = undefined;
+ // 代码块选中内容时可缩进,列表首项保留禁用状态,外观配置仍可隐藏按钮。
+ const block = editable.parentElement;
+ block.classList.add("code-block");
+ select();
+ await settle();
+ assert.equal(hidden("indent"), false);
+ assert.equal(hidden("outdent"), false);
+ assert.equal(toolbar.querySelector('[data-type="indent"]').hasAttribute("disabled"), false);
+ assert.equal(hidden("strong"), true);
+ select(0);
+ await settle();
+ assert.equal(hidden("indent"), true);
+ block.classList.remove("code-block");
+ const listItem = document.createElement("div");
+ listItem.className = "li";
+ root.append(listItem);
+ listItem.append(block);
+ editable.focus();
+ select(0);
+ await settle();
+ assert.equal(hidden("indent"), false);
+ assert.equal(hidden("outdent"), false);
+ assert.equal(toolbar.querySelector('[data-type="indent"]').hasAttribute("disabled"), true);
+ visible.delete("mobile-outdent");
+ await settle();
+ assert.equal(hidden("outdent"), true);
+ root.append(block);
+ listItem.remove();
+ editable.focus();
+ select(0);
+ await settle();
const showsBeforeClose = keyboardShows;
action("done");
assert.equal(document.activeElement, document.body);
@@ -201,12 +239,15 @@ const runCases = async (sources, platform) => {
assert.equal(editable.getAttribute("inputmode"), "text");
assert.equal(toolbar.querySelector('[data-type="copy"]').classList.contains("fn__none"), true);
assert.equal(toolbar.querySelector('[data-type="cut"]').classList.contains("fn__none"), true);
+ for (const name of ["indent", "outdent", "strong", "em", "a", "block-ref", "text"]) {
+ assert.equal(hidden(name), true);
+ }
// 输入框不会因残留文档选区恢复文档工具栏。
const input = document.createElement("input");
document.body.append(input);
select();
input.focus();
- await new Promise(resolve => requestAnimationFrame(resolve));
+ await settle();
assert.equal(toolbar.classList.contains("fn__none"), true);
assert.equal(editable.getAttribute("inputmode"), "text");
assert.equal(document.activeElement, input);
@@ -280,14 +321,14 @@ if (process.versions.electron && process.type === "browser") {
} else {
require("node:test").it("keeps touch selections in the input flow and shares toolbar configuration", {
skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
- timeout: 45000,
+ timeout: 125000,
}, async () => {
const env = {...process.env};
delete env.ELECTRON_RUN_AS_NODE;
const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-mobile-editing-"));
try {
const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(
- require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});
+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 120000});
assert.match(stdout, /Mobile editing toolbar cases passed/);
} finally {
assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));