Skip to content

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

:art: Support setting height for code blocks https://github.com/siyuan-note/siyuan/issues/11055

Danieldca844449b13
diff --git a/app/src/assets/scss/component/_typography.scss b/app/src/assets/scss/component/_typography.scss
index 04565b649c..dab7777a83 100644
--- a/app/src/assets/scss/component/_typography.scss
+++ b/app/src/assets/scss/component/_typography.scss
@@ -438,6 +438,18 @@
border-radius: var(--b3-border-radius);
}
+ pre.code-block[style*="max-height:"],
+ .code-block:not(pre)[data-type="NodeCodeBlock"]:not(.render-node)[style*="max-height:"] {
+ // 最大高度仅约束代码内容,操作区和属性区保持在滚动容器之外。
+ overflow: visible;
+
+ & > .hljs {
+ box-sizing: border-box;
+ max-height: inherit;
+ overflow-y: auto;
+ }
+ }
+
.code-block:not(pre) {
margin: 2px 0;
box-sizing: border-box;
diff --git a/app/src/assets/scss/export.scss b/app/src/assets/scss/export.scss
index 319129fdd3..72eb8b11a1 100644
--- a/app/src/assets/scss/export.scss
+++ b/app/src/assets/scss/export.scss
@@ -58,6 +58,17 @@ svg {
}
@media print {
+ .b3-typography pre.code-block[style*="max-height:"],
+ .protyle-wysiwyg .code-block[data-type="NodeCodeBlock"]:not(.render-node):not(pre)[style*="max-height:"] {
+ max-height: none !important;
+ overflow: visible;
+
+ & > .hljs {
+ max-height: none !important;
+ overflow: visible !important;
+ }
+ }
+
// 尽量避免块内分页
.protyle-wysiwyg [data-node-id] {
&[data-type=NodeAudio],
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.ts
index 7abff1ed49..596ed945e1 100644
--- a/app/src/config/entryVisibility/catalog.test.ts
+++ b/app/src/config/entryVisibility/catalog.test.ts
@@ -388,6 +388,38 @@ test("super block actions and vertical alignment use their respective menu group
assert.equal(getEntryCatalogChildren("gutter.multi.layout").some((item) => item.key === "alignTop"), false);
});
+test("gutter height menus follow width and stay aligned across selection scopes", () => {
+ const expectedHeightOrder = [
+ "heightInput",
+ "height_25%",
+ "height_33%",
+ "height_50%",
+ "height_67%",
+ "height_75%",
+ "height_100%",
+ "separator_1",
+ "heightDrag",
+ "separator_2",
+ "default",
+ ];
+
+ ["gutter.single", "gutter.multi"].forEach((scope) => {
+ const scopeChildren = getEntryCatalogChildren(scope);
+ const widthIndex = scopeChildren.findIndex((item) => item.key === "width");
+ const width = getEntryCatalogNode(`${scope}.width`);
+ const height = getEntryCatalogNode(`${scope}.height`);
+
+ assert.ok(0 <= widthIndex, scope);
+ assert.equal(scopeChildren[widthIndex + 1]?.key, "height", scope);
+ assert.deepEqual(height?.children?.map((item) => item.key), expectedHeightOrder, scope);
+ assert.equal(height?.simple, width?.simple, scope);
+ assert.deepEqual(height?.children?.map((item) => item.simple),
+ width?.children?.map((item) => item.simple), scope);
+ assert.deepEqual(height?.children?.map((item) => item.type),
+ width?.children?.map((item) => item.type), scope);
+ });
+});
+
test("super block column insertion actions follow block insertion actions", () => {
const keys = getEntryCatalogChildren("gutter.single").map((item) => item.key);
const insertBeforeIndex = keys.indexOf("insertBefore");
@@ -543,6 +575,7 @@ test("configuration labels distinguish block scopes and size controls", () => {
entryPercentageWidth: "Percentage width",
entryPixelHeight: "Pixel height",
entryPercentageHeight: "Percentage height",
+ height: "Height",
entryDocumentStatistics: "Document statistics",
},
},
@@ -555,6 +588,11 @@ test("configuration labels distinguish block scopes and size controls", () => {
"Editor - Block icon menu - Multiple blocks");
assert.equal(getEntryCatalogNode("gutter.single.width.widthInput")?.label(), "Pixel width");
assert.equal(getEntryCatalogNode("gutter.single.width.widthDrag")?.label(), "Percentage width");
+ assert.equal(getEntryCatalogNode("gutter.single.height")?.label(), "Height");
+ assert.equal(getEntryCatalogNode("gutter.single.height.heightInput")?.label(), "Pixel height");
+ assert.equal(getEntryCatalogNode("gutter.single.height.heightDrag")?.label(), "Percentage height");
+ assert.equal(getEntryCatalogNode("gutter.multi.height.heightInput")?.label(), "Pixel height");
+ assert.equal(getEntryCatalogNode("gutter.multi.height.heightDrag")?.label(), "Percentage height");
assert.equal(getEntryCatalogNode("inline.image.height.heightInput")?.label(), "Pixel height");
assert.equal(getEntryCatalogNode("inline.image.height.heightDrag")?.label(), "Percentage height");
assert.equal(getEntryCatalogNode("document.more.docInfo")?.label(), "Document statistics");
diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.ts
index 69a1d7aabd..4c0a436f0f 100644
--- a/app/src/config/entryVisibility/catalog.ts
+++ b/app/src/config/entryVisibility/catalog.ts
@@ -252,6 +252,20 @@ const gutterWidth = () => node("width", lang("width"), true, [
node("default", lang("default")),
]);
+const gutterHeight = () => node("height", lang("height"), true, [
+ node("heightInput", lang("entryPixelHeight")),
+ node("height_25%", literal("25%")),
+ node("height_33%", literal("33%")),
+ node("height_50%", literal("50%")),
+ node("height_67%", literal("67%")),
+ node("height_75%", literal("75%")),
+ node("height_100%", literal("100%")),
+ separator("separator_1"),
+ node("heightDrag", lang("entryPercentageHeight")),
+ separator("separator_2"),
+ node("default", lang("default")),
+]);
+
const gutterTable = () => node("table", lang("table"), true, [
node("useDefaultWidth", lang("useDefaultWidth")),
node("distributeAllColWidths", lang("distributeAllColWidths")),
@@ -309,6 +323,7 @@ const gutterMultiple = () => [
node("appearance", lang("appearance")),
gutterLayout(),
gutterWidth(),
+ gutterHeight(),
separator("separator_quickMakeCard"),
node("quickMakeCard", lang("quickMakeCard"), false),
node("removeCard", lang("removeCard"), false),
@@ -410,6 +425,7 @@ const gutterSingle = () => [
node("appearance", lang("appearance")),
gutterLayout(true),
gutterWidth(),
+ gutterHeight(),
separator("separator_4"),
node("wechatReminder", lang("wechatReminder"), false),
node("quickMakeCard", lang("quickMakeCard"), false),
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 44b8eb7cda..2bb224121b 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -1445,7 +1445,7 @@ export class Gutter {
}
this.genAlign(selectsElement, protyle);
this.genWidths(selectsElement, protyle);
- // this.genHeights(selectsElement, protyle);
+ this.genHeights(selectsElement, protyle);
}
if (!window.siyuan.config.readonly && !isEncryptedBox(protyle.notebookId)) {
window.siyuan.menus.menu.append(new MenuItem({
@@ -2785,7 +2785,7 @@ export class Gutter {
}
this.genAlign([nodeElement], protyle);
this.genWidths([nodeElement], protyle);
- // this.genHeights([nodeElement], protyle);
+ this.genHeights([nodeElement], protyle);
}
if (type !== "NodeThematicBreak" || !protyle.disabled) {
window.siyuan.menus.menu.append(new MenuItem({id: "separator_4", type: "separator"}).element);
@@ -3201,14 +3201,9 @@ export class Gutter {
}).element);
}
- // TODO https://github.com/siyuan-note/siyuan/issues/11055
private genHeights(nodeElements: Element[], protyle: IProtyle) {
- const matchHeight = nodeElements.find(item => {
- if (!item.classList.contains("p") && !item.classList.contains("code-block") && !item.classList.contains("render-node")) {
- return true;
- }
- });
- if (matchHeight) {
+ if (nodeElements.length === 0 ||
+ nodeElements.some(item => item.getAttribute("data-type") !== "NodeCodeBlock" || item.getAttribute("data-subtype"))) {
return;
}
let rangeElement: HTMLInputElement;
@@ -3217,16 +3212,16 @@ export class Gutter {
id: "heightInput",
iconHTML: "",
type: "readonly",
- label: `<div class="fn__flex"><input class="b3-text-field fn__flex-1" value="${firstElement.style.height.endsWith("px") ? parseInt(firstElement.style.height) : ""}" type="number" style="margin: 4px 8px 4px 0" placeholder="${window.siyuan.languages.height}"><span class="fn__flex-center">px</span></div>`,
+ label: `<div class="fn__flex"><input class="b3-text-field fn__flex-1" value="${firstElement.style.maxHeight.endsWith("px") ? parseInt(firstElement.style.maxHeight) : ""}" type="number" min="1" style="margin: 4px 8px 4px 0" placeholder="${window.siyuan.languages.height}"><span class="fn__flex-center">px</span></div>`,
bind: (element) => {
const inputElement = element.querySelector("input");
inputElement.addEventListener("input", () => {
+ const maxHeight = inputElement.value ? inputElement.value + "px" : "";
nodeElements.forEach((item: HTMLElement) => {
- item.style.height = inputElement.value + "px";
- item.style.flex = "none";
+ item.style.maxHeight = maxHeight;
});
rangeElement.value = "0";
- rangeElement.parentElement.setAttribute("aria-label", inputElement.value + "px");
+ rangeElement.parentElement.setAttribute("aria-label", maxHeight || window.siyuan.languages.default);
});
this.updateNodeElements(nodeElements, protyle, inputElement);
}
@@ -3238,8 +3233,7 @@ export class Gutter {
label: item,
click: () => {
this.genClick(nodeElements, protyle, (e: HTMLElement) => {
- e.style.height = item;
- e.style.flex = "none";
+ e.style.maxHeight = parseInt(item) + "vh";
});
}
});
@@ -3248,20 +3242,20 @@ export class Gutter {
id: "separator_1",
type: "separator"
});
- const height = firstElement.style.height.endsWith("%") ? parseInt(firstElement.style.height) : 0;
+ const height = firstElement.style.maxHeight.endsWith("vh") ? parseInt(firstElement.style.maxHeight) : 0;
window.siyuan.menus.menu.append(new MenuItem({
- id: "heightDrag",
+ id: "height",
label: window.siyuan.languages.height,
submenu: styles.concat([{
+ id: "heightDrag",
iconHTML: "",
type: "readonly",
- label: `<div style="margin: 4px 0;" aria-label="${firstElement.style.height.endsWith("px") ? firstElement.style.height : (firstElement.style.height || window.siyuan.languages.default)}" class="b3-tooltips b3-tooltips__n"><input style="box-sizing: border-box" value="${height}" class="b3-slider fn__block" max="100" min="1" step="1" type="range"></div>`,
+ label: `<div style="margin: 4px 0;" aria-label="${firstElement.style.maxHeight ? firstElement.style.maxHeight.replace("vh", "%") : window.siyuan.languages.default}" class="b3-tooltips b3-tooltips__n"><input style="box-sizing: border-box" value="${height}" class="b3-slider fn__block" max="100" min="1" step="1" type="range"></div>`,
bind: (element) => {
rangeElement = element.querySelector("input");
rangeElement.addEventListener("input", () => {
nodeElements.forEach((e: HTMLElement) => {
- e.style.height = rangeElement.value + "%";
- e.style.flex = "none";
+ e.style.maxHeight = rangeElement.value + "vh";
});
rangeElement.parentElement.setAttribute("aria-label", `${rangeElement.value}%`);
});
@@ -3276,9 +3270,8 @@ export class Gutter {
label: window.siyuan.languages.default,
click: () => {
this.genClick(nodeElements, protyle, (e: HTMLElement) => {
- if (e.style.height) {
- e.style.height = "";
- e.style.overflow = "";
+ if (e.style.maxHeight) {
+ e.style.maxHeight = "";
}
});
}