Skip to content

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

:bug: Keep narrow mobile database columns aligned

Daniel2954a7684fb6
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index 7475142c1e..e3ade0fc8e 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -1985,20 +1985,24 @@ body.mobile-topbar--merged {
}
.av__cell[data-dtype="block"]:not(.av__cell--header) {
- padding-right: 77px;
+ // 按钮留白放在内容内层,避免外层内边距撑大窄列,导致表头与正文错位。
+ & > .av__cellprimary {
+ display: block;
+ padding-right: 69px;
+ }
& > .av__row-actions {
opacity: 1;
pointer-events: auto;
}
- &[data-align="center"] {
- padding-left: 77px;
+ &[data-align="center"] > .av__cellprimary {
+ padding-left: 69px;
}
- &[data-align="right"] {
- padding-left: 77px;
- padding-right: 8px;
+ &[data-align="right"] > .av__cellprimary {
+ padding-left: 69px;
+ padding-right: 0;
}
}
diff --git a/app/src/protyle/render/av/blockAttr.ts b/app/src/protyle/render/av/blockAttr.ts
index 7fc09c60f0..d398565e1f 100644
--- a/app/src/protyle/render/av/blockAttr.ts
+++ b/app/src/protyle/render/av/blockAttr.ts
@@ -706,7 +706,7 @@ const openEdit = (protyle: IProtyle, element: HTMLElement, event: MouseEvent) =>
event.preventDefault();
break;
} else if (["text", "url", "email", "phone", "block"].includes(type) &&
- (target.querySelector(":scope > .av__celltext--template") ||
+ (target.querySelector(":scope > .av__celltext--template, :scope > .av__cellprimary > .av__celltext--template") ||
(type === "text" && target.querySelector(":scope > .av__celltext")))) {
popTextCell(protyle, [target], type as TAVCol);
event.stopPropagation();
diff --git a/app/src/protyle/render/av/blockIcon.test.ts b/app/src/protyle/render/av/blockIcon.test.ts
index baeb971663..53bcd9f84b 100644
--- a/app/src/protyle/render/av/blockIcon.test.ts
+++ b/app/src/protyle/render/av/blockIcon.test.ts
@@ -130,6 +130,31 @@ describe("database item icons", () => {
}
});
+ it("keeps primary content separate from row actions without changing references or template values", () => {
+ for (const detached of [false, true]) {
+ const h = createHarness("table", detached, "1f600");
+ for (const template of [false, true]) {
+ if (template) {
+ h.cell.value.renderedContent = "<strong>Display title</strong>";
+ }
+ for (const showIcon of [false, true]) {
+ const html = h.api.renderCell(h.cell.value, 0, showIcon);
+ assert.match(html, /^<span class="av__cellprimary"><span class="b3-menu__avemoji/);
+ assert.match(html, /<\/span><span class="av__row-actions">/);
+ assert.equal((html.match(/data-type="av-row-open"/g) || []).length, 1);
+ assert.equal((html.match(/data-type="av-row-update"/g) || []).length, 1);
+ if (template) {
+ const value = html.match(/data-cell-value="([^"]+)"/)[1];
+ assert.equal(JSON.parse(decodeURIComponent(value)).block.content, "Title");
+ assert.match(html, /<strong>Display title<\/strong>/);
+ } else if (!detached) {
+ assert.match(html, /data-type="block-ref" data-id="document"/);
+ }
+ }
+ }
+ }
+ });
+
it("reads an explicit empty icon for undo and preserves icons when renaming or unbinding", async () => {
const h = createHarness("list", true, "1f600");
assert.equal(h.api.genCellValueByElement("block", h.cellElement as unknown as HTMLElement).block.icon, "1f600");
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index ef70e656c9..d91a39e309 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -635,7 +635,7 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type
const cssStyle = getComputedStyle(cellElements[0]);
const storedCellValue = getStoredCellValueByElement(cellElements[0]);
const hasRenderedTemplate = cellElements[0].matches(".av__celltext--template") ||
- Boolean(cellElements[0].querySelector(":scope > .av__celltext--template"));
+ Boolean(cellElements[0].querySelector(":scope > .av__celltext--template, :scope > .av__cellprimary > .av__celltext--template"));
if (type === "text" && !hasRenderedTemplate) {
const stableCells = getStableTextCells(cellElements, blockElement);
if (stableCells.length > 0) {
@@ -1366,7 +1366,7 @@ export const renderCell = (cellValue: IAVCellValue, rowIndex = 0, showIcon = tru
const storedValue = cloneAVCellValueSnapshot(cellValue);
text = `<span class="av__celltext av__celltext--template" data-cell-value="${escapeAttr(encodeURIComponent(JSON.stringify(storedValue)))}">${getAVTemplateHTML(cellValue.renderedContent || "")}</span>`;
if (cellValue.type === "block") {
- text = renderAVBlockIcon(cellValue, showIcon) + text;
+ text = `<span class="av__cellprimary">${renderAVBlockIcon(cellValue, showIcon)}${text}</span>`;
const bindLabel = cellValue?.isDetached ? window.siyuan.languages.bind : window.siyuan.languages.rebind;
const updateIcon = cellValue?.isDetached ? "iconRef" : "iconRefresh";
text += `<span class="av__row-actions"><button class="av__row-action av__cell-action ariaLabel" type="button" data-position="4north" aria-label="${window.siyuan.languages.openBy}" data-type="av-row-open"><svg><use xlink:href="#iconOpen"></use></svg></button><button class="av__row-action av__cell-action ariaLabel" type="button" data-position="4north" aria-label="${bindLabel}" data-type="av-row-update"><svg><use xlink:href="#${updateIcon}"></use></svg></button></span>`;
@@ -1392,6 +1392,7 @@ export const renderCell = (cellValue: IAVCellValue, rowIndex = 0, showIcon = tru
} else {
text = `${renderAVBlockIcon(cellValue, showIcon)}<span data-type="block-ref" data-id="${cellValue.block.id}" data-subtype="${getAVBlockRefSubtype(cellValue)}" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(cellValue.block.content)}</span>`;
}
+ text = `<span class="av__cellprimary">${text}</span>`;
const bindLabel = cellValue?.isDetached ? window.siyuan.languages.bind : window.siyuan.languages.rebind;
const updateIcon = cellValue?.isDetached ? "iconRef" : "iconRefresh";
text += `<span class="av__row-actions"><button class="av__row-action av__cell-action ariaLabel" type="button" data-position="4north" aria-label="${window.siyuan.languages.openBy}" data-type="av-row-open"><svg><use xlink:href="#iconOpen"></use></svg></button><button class="av__row-action av__cell-action ariaLabel" type="button" data-position="4north" aria-label="${bindLabel}" data-type="av-row-update"><svg><use xlink:href="#${updateIcon}"></use></svg></button></span>`;
diff --git a/app/tests/avColumnAlignment.test.js b/app/tests/avColumnAlignment.test.js
new file mode 100644
index 0000000000..fa18be7c9a
--- /dev/null
+++ b/app/tests/avColumnAlignment.test.js
@@ -0,0 +1,178 @@
+const assert = require("node:assert/strict");
+const {mkdtempSync, rmSync} = require("node:fs");
+const path = require("node:path");
+const os = require("node:os");
+const {test} = require("node:test");
+const sass = require("sass");
+
+const stylesheet = name => sass.compile(path.join(__dirname, `../src/assets/scss/${name}.scss`), {
+ logger: sass.Logger.silent,
+}).css;
+const declarations = (css, selector) => {
+ const result = {};
+ for (const match of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
+ if (!match[1].split(",").map(item => item.trim()).includes(selector)) {
+ continue;
+ }
+ for (const declaration of match[2].split(";")) {
+ const separator = declaration.indexOf(":");
+ if (separator > -1) {
+ result[declaration.slice(0, separator).trim()] = declaration.slice(separator + 1).trim();
+ }
+ }
+ }
+ return result;
+};
+const sidePadding = (styles, side) => {
+ const shorthand = (styles.padding || "0").split(/\s+/);
+ return parseFloat(styles[`padding-${side}`] ??
+ (side === "left" ? shorthand[3] || shorthand[1] || shorthand[0] : shorthand[1] || shorthand[0]));
+};
+const primarySelector = ".av__cell[data-dtype=block]:not(.av__cell--header)";
+const getCellStyles = (css, header, alignment = "") => ({
+ ...declarations(css, ".av__cell"),
+ ...declarations(css, header ? ".av__cell--header" : primarySelector),
+ ...declarations(css, `${header ? ".av__cell--header" : primarySelector}[data-align=${alignment}]`),
+});
+
+test("mobile primary columns keep the same outer width as headers at every supported alignment", () => {
+ const css = stylesheet("mobile");
+ for (const alignment of ["", "left", "center", "right"]) {
+ const header = getCellStyles(css, true, alignment);
+ const body = getCellStyles(css, false, alignment);
+ assert.equal(body["box-sizing"], "border-box");
+ assert.equal(body["flex-shrink"], "0");
+ for (const width of [25, 26, 64, 85, 86, 100, 154, 155, 200]) {
+ // border-box 的内容宽度不能为负,外框至少要容纳两侧内边距和边框。
+ const outerWidth = styles => Math.max(width, sidePadding(styles, "left") +
+ sidePadding(styles, "right") + parseFloat(styles["border-right"]));
+ assert.equal(outerWidth(body), outerWidth(header), `${alignment || "left"}, ${width}px`);
+ assert.equal(outerWidth(body), width);
+ }
+ }
+});
+
+test("mobile action spacing remains inside primary content while buttons retain their existing behavior", () => {
+ const css = stylesheet("mobile");
+ const base = declarations(css, `${primarySelector} > .av__cellprimary`);
+ assert.equal(base.display, "block");
+ for (const alignment of ["", "left", "center", "right"]) {
+ const inner = {...base, ...declarations(css, `${primarySelector}[data-align=${alignment}] > .av__cellprimary`)};
+ const body = getCellStyles(css, false, alignment);
+ const content = 200 - sidePadding(body, "left") - sidePadding(body, "right") - 1 -
+ sidePadding(inner, "left") - sidePadding(inner, "right");
+ assert.equal(content, alignment === "center" ? 45 : 114, alignment);
+ }
+ const actions = declarations(css, `${primarySelector} > .av__row-actions`);
+ assert.equal(actions.opacity, "1");
+ assert.equal(actions["pointer-events"], "auto");
+ const buttons = declarations(css, ".av__row-actions .av__row-action");
+ assert.equal(buttons.width, "28px");
+ assert.equal(buttons.height, "28px");
+ assert.equal(declarations(stylesheet("base"), `${primarySelector} > .av__cellprimary`).display, undefined,
+ "the content wrapper remains an ordinary inline span on desktop");
+});
+
+const browserCases = css => {
+ const check = require("node:assert/strict");
+ const style = document.createElement("style");
+ style.textContent = css + `
+html,body {display:block;height:auto;overflow:auto;font:16px sans-serif}
+.av {width:366px;--b3-parent-background:white;--b3-theme-surface-lighter:#ccc}
+`;
+ document.head.append(style);
+ const dimensions = [25, 26, 64, 86, 100, 155, 200];
+ for (const width of dimensions) {
+ for (const align of ["", "left", "center", "right"]) {
+ for (const frozen of [false, true]) {
+ const cells = header => [
+ `<div class="av__cell${header ? " av__cell--header" : ""}" data-dtype="block" data-align="${align}" data-wrap="false" style="width:${width}px">${header ?
+ '<span class="av__celltext">Primary</span>' :
+ '<span class="av__cellprimary"><span class="b3-menu__avemoji">-</span><span class="av__celltext">Primary title</span></span><span class="av__row-actions"><button class="av__row-action av__cell-action" data-type="av-row-open">O</button><button class="av__row-action av__cell-action" data-type="av-row-update">B</button></span>'}</div>`,
+ '<div class="av__cell" data-dtype="date" style="width:200px">Date</div>',
+ '<div class="av__cell" data-dtype="text" style="width:200px">Place</div>',
+ ];
+ const row = header => {
+ const content = cells(header);
+ const first = '<div class="av__firstcol"><svg></svg></div>';
+ return `<div class="av__row${header ? " av__row--header" : ""}">${frozen ?
+ `<div class="av__colsticky av__colsticky--freeze">${first}${content[0]}</div>${content.slice(1).join("")}` :
+ `<div class="av__colsticky">${first}</div>${content.join("")}`}</div>`;
+ };
+ document.body.innerHTML = `<div class="av" data-av-type="table"><div class="av__scroll"><div class="av__body" style="float:left">${row(true)}${row(false)}</div></div></div>`;
+ const scroll = document.querySelector(".av__scroll");
+ for (const offset of [0, 100]) {
+ scroll.scrollLeft = offset;
+ const rows = [...document.querySelectorAll(".av__row")];
+ const header = [...rows[0].querySelectorAll(".av__cell")];
+ const body = [...rows[1].querySelectorAll(".av__cell")];
+ header.forEach((cell, index) => {
+ const top = cell.getBoundingClientRect();
+ const bottom = body[index].getBoundingClientRect();
+ const label = `${width}px/${align}/${frozen}/${offset}/${index}`;
+ check.ok(Math.abs(top.width - bottom.width) < 1, `width ${label}`);
+ check.ok(Math.abs(top.left - bottom.left) < 1, `left ${label}`);
+ check.ok(Math.abs(top.right - bottom.right) < 1, `right ${label}`);
+ });
+ if (width >= 86) {
+ const primary = body[0].getBoundingClientRect();
+ for (const button of body[0].querySelectorAll(".av__row-action")) {
+ const bounds = button.getBoundingClientRect();
+ check.equal(Math.round(bounds.width), 28);
+ check.ok(bounds.left >= primary.left && bounds.right <= primary.right);
+ check.equal(getComputedStyle(button.parentElement).pointerEvents, "auto");
+ }
+ }
+ }
+ }
+ }
+ }
+ return "Database column alignment cases passed";
+};
+
+const runElectron = async () => {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", process.argv[2]);
+ app.commandLine.appendSwitch("disable-gpu");
+ await app.whenReady();
+ const win = new BrowserWindow({show: false, width: 414, height: 896,
+ webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ const result = await win.webContents.executeJavaScript(
+ `(${browserCases.toString()})(${JSON.stringify(stylesheet("mobile"))})`);
+ assert.equal(result, "Database column alignment cases passed");
+ console.log(result);
+ win.destroy();
+ app.exit(0);
+ } catch (error) {
+ console.error(error);
+ win.destroy();
+ app.exit(1);
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ runElectron().catch(error => {
+ console.error(error);
+ require("electron").app.exit(1);
+ });
+} else {
+ test("narrow mobile columns align through frozen layouts and horizontal scrolling", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+ }, async () => {
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-column-alignment-"));
+ try {
+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(
+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});
+ assert.match(stdout, /Database column alignment cases passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
+ assert.ok(path.basename(profile).startsWith("siyuan-column-alignment-"));
+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}