Skip to content

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

:bug: Keep block font styles separate from inline text https://github.com/siyuan-note/siyuan/issues/19261

Daniel4fad2d3c40e4
diff --git a/app/scripts/blockFontStyle.test.js b/app/scripts/blockFontStyle.test.js
new file mode 100644
index 0000000000..45e6a1ae2c
--- /dev/null
+++ b/app/scripts/blockFontStyle.test.js
@@ -0,0 +1,174 @@
+const assert = require("node:assert/strict");
+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");
+const path = require("node:path");
+const os = require("node:os");
+
+const runCases = () => {
+ const assert = require("node:assert/strict");
+ document.body.innerHTML = '<div id="editor"><div data-node-id="quote" data-type="NodeBlockquote" style="font-family: Georgia">' +
+ '<div data-node-id="one" data-type="NodeParagraph"><div contenteditable="true">plain<span data-type="text" style="font-family: Arial; color: red">inline</span></div><div class="protyle-attr">badge</div></div>' +
+ '<div data-node-id="empty" data-type="NodeParagraph"><div contenteditable="true"></div></div></div>' +
+ '<div data-node-id="two" data-type="NodeParagraph"><div contenteditable="true">two</div></div>' +
+ '<div data-node-id="code" data-type="NodeCodeBlock"><div contenteditable="true">code</div></div>' +
+ '<div data-node-id="math" data-type="NodeMathBlock"></div>' +
+ '<div data-node-id="av" data-type="NodeAttributeView"></div>' +
+ '<div data-node-id="list" data-type="NodeList"><div data-node-id="li" data-type="NodeListItem" class="li"><div data-node-id="child" data-type="NodeParagraph"><div contenteditable="true">child</div></div></div></div>' +
+ '<div data-node-id="table" data-type="NodeTable"><table><tbody><tr><td><span data-type="text" style="font-family: Arial">cell</span></td></tr></tbody></table></div></div>';
+ const editor = document.getElementById("editor");
+ const block = id => editor.querySelector(`[data-node-id="${id}"]`);
+ const one = block("one");
+ const range = document.createRange();
+ range.selectNodeContents(one.firstElementChild);
+ const inlineCalls = [];
+ const protyle = {wysiwyg: {element: editor}, toolbar: {range,
+ setInlineMark: (...args) => inlineCalls.push(args),
+ setBlockElementsInlineMark: () => { throw new Error("Unexpected inline block formatting"); },
+ }};
+ window.siyuan = {storage: {recent: []}};
+ const state = nodes => window.family.getFontFamilyState(protyle, nodes);
+ const apply = (nodes, type, value) => window.font.fontEvent(protyle, nodes, type, value, false);
+ assert.equal(state([one]).family, "Georgia");
+ assert.equal(state([block("quote")]).family, "Georgia");
+ assert.equal(state([block("empty")]).family, "Georgia");
+ assert.equal(state([one, block("two")]).mixed, true);
+ assert.equal(state([block("code"), block("math"), block("av")]).disabled, true);
+ assert.equal(state([block("li")]).disabled, true);
+ assert.equal(state([block("list")]).disabled, false);
+ assert.equal(state([one, block("code")]).family, "Georgia");
+ assert.equal(window.family.getInlineFontFamilyState(protyle, [editor.querySelector("td")]).family, "Arial");
+ assert.equal(state([block("table")]).family, undefined);
+
+ const body = one.innerHTML;
+ apply([one, block("empty"), block("two")], "fontFamily", "Verdana");
+ assert.equal(one.style.fontFamily, "Verdana");
+ assert.equal(one.innerHTML, body);
+ assert.equal(block("empty").style.fontFamily, "Verdana");
+ assert.equal(state([one, block("two")]).mixed, false);
+ assert.equal(state([one]).family, "Verdana");
+ const batch = window.batches.at(-1);
+ assert.equal(batch.operations.length, 3);
+ assert.equal(batch.undoOperations.length, 3);
+ const replay = operations => operations.forEach(operation => { block(operation.id).outerHTML = operation.data; });
+ replay(batch.undoOperations);
+ assert.equal(block("one").style.fontFamily, "");
+ assert.equal(block("one").innerHTML, body);
+ replay(batch.operations);
+ assert.equal(block("one").style.fontFamily, "Verdana");
+ assert.equal(block("one").innerHTML, body);
+ apply([block("one")], "fontFamily", "");
+ assert.equal(state([block("one")]).family, "Georgia");
+ apply([block("one")], "fontFamily", "Verdana");
+ block("one").style.fontSize = "24px";
+ block("one").style.color = "blue";
+ apply([block("one")], "clear");
+ assert.equal(block("one").innerHTML, body);
+ assert.equal(block("one").style.fontFamily, "");
+ assert.equal(block("one").style.fontSize, "");
+ assert.equal(block("one").style.color, "");
+ const listBody = block("list").innerHTML;
+ apply([block("list"), block("table")], "fontFamily", "Verdana");
+ assert.equal(block("list").innerHTML, listBody);
+ assert.equal(block("table").querySelector("span").style.fontFamily, "Arial");
+ apply([block("code"), block("math"), block("av")], "fontFamily", "Verdana");
+ ["code", "math", "av"].forEach(id => assert.equal(block(id).style.fontFamily, ""));
+ apply([], "fontFamily", "Arial");
+ assert.equal(inlineCalls.at(-1)[1], "text");
+ assert.deepEqual(inlineCalls.at(-1)[3], {type: "fontFamily", color: "Arial"});
+ apply([], "clear");
+ assert.equal(inlineCalls.at(-1)[1], "clear");
+ range.setStart(block("one").querySelector("span").firstChild, 1);
+ range.collapse(true);
+ assert.equal(state().family, "Arial");
+ document.documentElement.style.setProperty("--b3-font-family", "Tahoma");
+ assert.equal(getComputedStyle(block("one").querySelector(".protyle-attr")).fontFamily, "Tahoma");
+ assert.equal(getComputedStyle(block("one").firstElementChild).fontFamily, "Georgia");
+};
+
+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, webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ let exitCode = 0;
+ try {
+ const ts = require("typescript");
+ const root = path.join(__dirname, "../src/protyle");
+ const read = name => readFileSync(path.join(root, name + ".ts"), "utf8");
+ const extract = (name, names) => {
+ const source = ts.createSourceFile(name, read(name), ts.ScriptTarget.Latest, true);
+ const statements = source.statements.filter(statement => ts.isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(declaration => names.includes(declaration.name.getText(source))));
+ assert.equal(statements.length, names.length);
+ return statements.map(statement => statement.getText(source)).join("\n");
+ };
+ // 使用实际字体分流、状态读取和事务序列化,隔离存储与内核通信。
+ const modules = {
+ "toolbar/fontFamilyCore": read("toolbar/fontFamilyCore"),
+ "util/hasClosest": read("util/hasClosest"),
+ "toolbar/fontFamilyMenu": 'import {FONT_FAMILY_EXCLUDED_BLOCK_TYPES, getInlineFontFamilySelection, hasInlineFontFamilyExcludedType} from "./fontFamilyCore";\n' +
+ 'import {hasClosestBlock} from "../util/hasClosest"; const Constants = {ZWSP:"\\u200b"};\n' +
+ extract("toolbar/fontFamilyMenu", ["getNodeFontFamily", "getFontFamilyState", "getInlineFontFamilyState"]),
+ "wysiwyg/transaction": 'const Constants = {ATTRIBUTE_EDITING:"data-editing"}; const cleanHeadingNumberHTML = html => html;\n' +
+ "const transaction = (protyle, operations, undoOperations) => window.batches.push({operations, undoOperations});\n" +
+ extract("wysiwyg/transaction", ["updateBatchTransaction"]),
+ "toolbar/Font": 'import {updateBatchTransaction} from "../wysiwyg/transaction";\n' +
+ 'const Constants = {LOCAL_FONTSTYLES:"recent", ZWSP:"\\u200b"}; const MAX_RECENT_FONT_STYLES = 14;\n' +
+ "const getRecentInlineStyleKey = value => value; const setStorageVal = () => {};\n" +
+ extract("toolbar/Font", ["fontEvent"]),
+ };
+ const sources = Object.fromEntries(Object.entries(modules).map(([name, source]) => [name,
+ ts.transpileModule(source, {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText]));
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ await win.webContents.insertCSS(require("sass").compile(path.join(__dirname, "../src/assets/scss/protyle/_attr.scss")).css);
+ await win.webContents.executeJavaScript(`(() => {
+ const sources = ${JSON.stringify(sources)}, cache = {};
+ const load = name => {
+ if (!cache[name]) {
+ if (!sources[name]) { throw new Error(name); }
+ cache[name] = {};
+ const resolve = dependency => load(require("node:path").posix.normalize(
+ require("node:path").posix.dirname(name) + "/" + dependency));
+ new Function("require", "exports", sources[name])(resolve, cache[name]);
+ }
+ return cache[name];
+ };
+ window.batches = [];
+ window.family = load("toolbar/fontFamilyMenu");
+ window.font = load("toolbar/Font");
+ })()`);
+ await win.webContents.executeJavaScript(`(${runCases.toString()})()`);
+ console.log("Block font style cases passed");
+ } catch (error) {
+ console.error(error);
+ exitCode = 1;
+ } finally {
+ win.destroy();
+ app.exit(exitCode);
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ runElectron().catch(error => {
+ console.error(error);
+ require("electron").app.exit(1);
+ });
+} else {
+ require("node:test").it("keeps block font styles separate from inline text and attribute badges", {
+ 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-block-font-"));
+ 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, /Block font style cases passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
+ assert.ok(path.basename(profile).startsWith("siyuan-block-font-"));
+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}
diff --git a/app/src/assets/scss/protyle/_attr.scss b/app/src/assets/scss/protyle/_attr.scss
index 4643cd1bd0..610e5f58e0 100644
--- a/app/src/assets/scss/protyle/_attr.scss
+++ b/app/src/assets/scss/protyle/_attr.scss
@@ -5,6 +5,7 @@
top: -12px;
opacity: 0;
font-size: 12px;
+ font-family: var(--b3-font-family);
font-weight: normal;
align-items: center;
line-height: 16px;
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index a66b8e1894..edb1aaa9b9 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -65,7 +65,7 @@ import {
} from "../../protyle/util/inlineElementMarker";
import {
getInlineFontFamilyLabel,
- getInlineFontFamilyState,
+ getFontFamilyState,
getInlineFontFamilyValue,
renderMobileFontFamilyMenu,
} from "../../protyle/toolbar/fontFamilyMenu";
@@ -569,7 +569,7 @@ export const renderTextMenu = (protyle: IProtyle, toolbarElement: Element) => {
lastColorHTML += "</div>";
}
const {fontSize, baseFontSize} = getFontSizeInfo(protyle, nodeElements);
- const fontFamilyState = getInlineFontFamilyState(protyle, nodeElements);
+ const fontFamilyState = getFontFamilyState(protyle, nodeElements);
const disableFontFamily = disableFont || fontFamilyState.disabled;
const utilElement = toolbarElement.querySelector(".keyboard__util") as HTMLElement;
utilElement.innerHTML = `${lastColorHTML}
@@ -1369,7 +1369,7 @@ export const initKeyboardToolbar = () => {
if (slashBtnElement && slashBtnElement.dataset.action === "fontFamilyMenu") {
const range = protyle.toolbar.range.cloneRange();
const nodeElements = getFontNodeElements(protyle);
- const fontFamilyState = getInlineFontFamilyState(protyle, nodeElements);
+ const fontFamilyState = getFontFamilyState(protyle, nodeElements);
const utilElement = toolbarElement.querySelector(".keyboard__util") as HTMLElement;
const isFontFamilyMenuValid = () => getCurrentEditor()?.protyle === protyle &&
toolbarElement.clientHeight > 100 && range.startContainer.isConnected &&
@@ -1560,7 +1560,7 @@ export const initKeyboardToolbar = () => {
};
preventKeyboardToolbarRender();
if (type === "font-family") {
- const state = getInlineFontFamilyState(protyle, nodes);
+ const state = getFontFamilyState(protyle, nodes);
if (state.disabled) {
finish();
return;
diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts
index 8862f2ebf3..ba56a684cd 100644
--- a/app/src/protyle/toolbar/Font.ts
+++ b/app/src/protyle/toolbar/Font.ts
@@ -42,6 +42,7 @@ import {setInlineMemoContentIfMissing} from "./inlineMemoSelection";
import {
getInlineFontFamilyLabel,
getInlineFontFamilyState,
+ getFontFamilyState,
getInlineFontFamilyValue,
openFontFamilyMenu,
} from "./fontFamilyMenu";
@@ -266,7 +267,8 @@ export const appearanceMenu = (protyle: IProtyle, nodeElements?: Element[],
lastColorHTML += "</div>";
}
const {fontSize, baseFontSize} = getFontSizeInfo(protyle, nodeElements);
- const fontFamilyState = getInlineFontFamilyState(protyle, fontFamilyElements || nodeElements);
+ const fontFamilyState = fontFamilyElements ? getInlineFontFamilyState(protyle, fontFamilyElements) :
+ getFontFamilyState(protyle, nodeElements);
const disableFontFamily = disableFont || fontFamilyState.disabled;
const showInlineDirection = !nodeElements || nodeElements.length === 0 || !!onChange;
const applyFontStyle = (type: string, color?: string) => {
@@ -496,20 +498,6 @@ export const fontEvent = (protyle: IProtyle, nodeElements: Element[], type?: str
return;
}
if (nodeElements && nodeElements.length > 0) {
- if (type === "clear" &&
- protyle.toolbar.setBlockElementsInlineMark(protyle, nodeElements, type, {type: "text"}, true)) {
- if (focusRange) {
- focusByRange(protyle.toolbar.range);
- }
- return;
- }
- if (type === "fontFamily" &&
- protyle.toolbar.setBlockElementsInlineMark(protyle, nodeElements, "text", {type, color})) {
- if (focusRange) {
- focusByRange(protyle.toolbar.range);
- }
- return;
- }
updateBatchTransaction(nodeElements, protyle, (e: HTMLElement) => {
if (type === "clear") {
e.style.color = "";
diff --git a/app/src/protyle/toolbar/fontControls.ts b/app/src/protyle/toolbar/fontControls.ts
index 3e6d3ff50a..45414924fa 100644
--- a/app/src/protyle/toolbar/fontControls.ts
+++ b/app/src/protyle/toolbar/fontControls.ts
@@ -1,7 +1,7 @@
import {Menu} from "../../plugin/Menu";
import {ToolbarItem} from "./ToolbarItem";
import {convertFontSize, fontEvent, getFontNodeElements, getFontSizeInfo} from "./Font";
-import {getInlineFontFamilyLabel, getInlineFontFamilyState, getInlineFontFamilyValue, openFontFamilyMenu} from "./fontFamilyMenu";
+import {getInlineFontFamilyLabel, getFontFamilyState, getInlineFontFamilyValue, openFontFamilyMenu} from "./fontFamilyMenu";
import {focusByRange, getBlockRanges} from "../util/selection";
import {escapeAttr} from "../../util/escape";
import {isMixedFontSize, normalizeFontSizeInput} from "./fontSizeCore";
@@ -94,7 +94,7 @@ export const updateFontControls = (protyle: IProtyle) => {
const familyButton = protyle.toolbar.element.querySelector<HTMLButtonElement>('[data-type="font-family"]');
const sizeButton = protyle.toolbar.element.querySelector<HTMLButtonElement>('[data-type="font-size"]');
if (familyButton) {
- const state = getInlineFontFamilyState(protyle, getFontNodeElements(protyle));
+ const state = getFontFamilyState(protyle, getFontNodeElements(protyle));
setLabel(familyButton, getInlineFontFamilyLabel(state), "fontFamily");
familyButton.disabled = state.disabled;
}
@@ -131,7 +131,7 @@ export class FontControl extends ToolbarItem {
};
if (item.name === "font-family") {
void openFontFamilyMenu(this.element, {
- ...getInlineFontFamilyState(protyle, nodes),
+ ...getFontFamilyState(protyle, nodes),
isOpenValid: valid,
onSelect: family => apply("fontFamily", getInlineFontFamilyValue(family)),
});
diff --git a/app/src/protyle/toolbar/fontFamilyMenu.ts b/app/src/protyle/toolbar/fontFamilyMenu.ts
index 50cfb99b0b..209b4f3023 100644
--- a/app/src/protyle/toolbar/fontFamilyMenu.ts
+++ b/app/src/protyle/toolbar/fontFamilyMenu.ts
@@ -52,6 +52,25 @@ const getNodeFontFamily = (node: Node, root: HTMLElement) => {
return {excluded: false, fontFamily};
};
+export const getFontFamilyState = (protyle: IProtyle, nodeElements?: Element[]) => {
+ if (!nodeElements?.length) {
+ return getInlineFontFamilyState(protyle);
+ }
+ // 块级菜单读取块及祖先的字体,不受正文内联样式和角标影响。
+ const fontFamilies: (string | undefined)[] = [];
+ nodeElements.forEach((element: HTMLElement) => {
+ if (FONT_FAMILY_EXCLUDED_BLOCK_TYPES.includes(element.getAttribute("data-type")) ||
+ element.classList.contains("li")) {
+ return;
+ }
+ const info = getNodeFontFamily(element, protyle.wysiwyg.element);
+ if (!info.excluded) {
+ fontFamilies.push(info.fontFamily);
+ }
+ });
+ return getInlineFontFamilySelection(fontFamilies, fontFamilies.length > 0) as IInlineFontFamilyState;
+};
+
export const getInlineFontFamilyState = (protyle: IProtyle, nodeElements?: Element[]) => {
const fontFamilies: (string | undefined)[] = [];
let eligible = false;