Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18192

Vanessa696fa432ad28
diff --git a/app/src/assets/scss/protyle/_toolbar.scss b/app/src/assets/scss/protyle/_toolbar.scss
index 0309652b90..eb3121e72e 100644
--- a/app/src/assets/scss/protyle/_toolbar.scss
+++ b/app/src/assets/scss/protyle/_toolbar.scss
@@ -86,6 +86,20 @@
left: 0;
width: 100vw;
}
+
+ // 行号栏与文本域共同的滚动容器,二者同高,由父层统一滚动,避免 JS 同步滚动
+ &__scroll {
+ overflow: auto;
+ flex: 1;
+ min-height: 0;
+ border-radius: 0 0 var(--b3-border-radius-b) var(--b3-border-radius-b);
+ }
+
+ .protyle-linenumber__rows {
+ line-height: 20px;
+ padding: 4px 8px;
+ position: static;
+ }
}
&-font {
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 07df46746d..65176ff066 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -961,22 +961,16 @@ export class Toolbar {
} else if (isInlineMemo) {
title = window.siyuan.languages.memo;
}
- const isPin = this.subElement.querySelector('[data-type="pin"]')?.getAttribute("aria-label") === window.siyuan.languages.unpin;
- const pinData: Record<string, string> = {};
- if (isPin) {
- const textElement = this.subElement.querySelector(".b3-text-field") as HTMLTextAreaElement;
- pinData.styleH = textElement.style.height;
- pinData.styleW = textElement.style.width;
- } else {
- this.subElement.style.width = "";
- this.subElement.style.padding = "0";
- }
- this.subElement.innerHTML = `<div ${(isPin && this.subElement.firstElementChild.getAttribute("data-drag") === "true") ? 'data-drag="true"' : ""}><div class="block__icons block__icons--menu fn__flex" style="border-radius: var(--b3-border-radius-b) var(--b3-border-radius-b) 0 0;">
+ this.clearSubElement();
+ this.subElement.style.padding = "0";
+ this.subElement.style.display = "flex";
+ this.subElement.style.flexDirection = "column";
+ this.subElement.innerHTML = `<div class="fn__flex-column"><div class="block__icons block__icons--menu fn__flex" style="border-radius: var(--b3-border-radius-b) var(--b3-border-radius-b) 0 0;">
<span class="fn__flex-1 resize__move" style="line-height: 24px;">
${title}
</span>
<span class="fn__space"></span>
- <button data-type="refresh" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw${(isPin && !this.subElement.querySelector('[data-type="refresh"]').classList.contains("block__icon--active")) ? "" : " block__icon--active"}${types.includes("NodeBlockQueryEmbed") ? " fn__none" : ""}" aria-label="${window.siyuan.languages.refresh}"><svg><use xlink:href="#iconRefresh"></use></svg></button>
+ <button data-type="refresh" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw block__icon--active${types.includes("NodeBlockQueryEmbed") ? " fn__none" : ""}" aria-label="${window.siyuan.languages.refresh}"><svg><use xlink:href="#iconRefresh"></use></svg></button>
<span class="fn__space"></span>
<button data-type="before" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw${protyle.disabled ? " fn__none" : ""}" aria-label="${window.siyuan.languages.insertBefore}"><svg><use xlink:href="#iconBefore"></use></svg></button>
<span class="fn__space${protyle.disabled ? " fn__none" : ""}"></span>
@@ -984,13 +978,48 @@ export class Toolbar {
<span class="fn__space${protyle.disabled ? " fn__none" : ""}"></span>
<button data-type="export" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.export} ${window.siyuan.languages.image}"><svg><use xlink:href="#iconImage"></use></svg></button>
<span class="fn__space"></span>
- <button data-type="pin" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw" aria-label="${isPin ? window.siyuan.languages.unpin : window.siyuan.languages.pin}"><svg><use xlink:href="#icon${isPin ? "Unpin" : "Pin"}"></use></svg></button>
+ <button data-type="pin" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.pin}"><svg><use xlink:href="#iconPin"></use></svg></button>
<span class="fn__space"></span>
<button data-type="close" class="block__icon block__icon--show b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.close}"><svg><use xlink:href="#iconClose"></use></svg></button>
</div>
-<textarea ${protyle.disabled ? " readonly" : ""} spellcheck="false" class="b3-text-field b3-text-field--text fn__block" placeholder="${placeholder}" style="${isMobile() ? "" : "width:" + Math.max(480, renderElement.clientWidth * 0.7) + "px"};max-height:calc(80vh - 44px);min-height: 48px;min-width: 268px;border-radius: 0 0 var(--b3-border-radius-b) var(--b3-border-radius-b);font-family: var(--b3-font-family-code);"></textarea></div>`;
+<div class="protyle-util__scroll"><div class="fn__flex"><div class="protyle-linenumber__rows"></div><textarea ${protyle.disabled ? " readonly" : ""} spellcheck="false" class="b3-text-field b3-text-field--text fn__flex-1" placeholder="${placeholder}" style="resize:none;font-family: var(--b3-font-family-code);${isMobile() ? "" : "width:" + Math.max(480, renderElement.clientWidth * 0.7) + "px"}"></textarea></div></div></div>
+<div class="resize__rd"></div><div class="resize__ld"></div><div class="resize__lt"></div><div class="resize__rt"></div><div class="resize__r"></div><div class="resize__d"></div><div class="resize__t"></div><div class="resize__l"></div>`;
+ const gutter = this.subElement.querySelector(".protyle-linenumber__rows") as HTMLElement;
+ const renderTextareaLineNumber = () => {
+ // textarea 末尾的 \n 视觉上会占一行空行,行号需保留该空行,故不去除末尾空串
+ const lineList = textElement.value.split(/\r\n|\r|\n/);
+ // 先用占位 span 撑出行号栏宽度,便于同步 textarea 左内边距
+ gutter.innerHTML = `<span></span>${"<span></span>".repeat(Math.max(0, lineList.length - 1))}`;
+ // 行号栏宽度随数字位数变化,需同步 textarea 左内边距避免文字被遮挡
+ const gutterWidth = gutter.clientWidth;
+ // textarea 默认软换行,需用镜像元素测量每个源码行折行后的实际高度,行号才能与可视行对齐
+ const textCs = window.getComputedStyle(textElement);
+ const mirror = document.createElement("div");
+ mirror.style.position = "absolute";
+ mirror.style.visibility = "hidden";
+ mirror.style.whiteSpace = "pre-wrap";
+ mirror.style.wordBreak = textCs.wordBreak || "break-word";
+ mirror.style.fontFamily = textCs.fontFamily;
+ mirror.style.fontSize = textCs.fontSize;
+ mirror.style.lineHeight = textCs.lineHeight;
+ mirror.style.fontWeight = textCs.fontWeight;
+ mirror.style.fontVariantLigatures = textCs.fontVariantLigatures;
+ // 宽度需与 textarea 文本区一致(去掉左右内边距,textarea 无边框)
+ mirror.style.width = textElement.clientWidth - gutterWidth - 8 - parseInt(textCs.paddingRight) + "px";
+ mirror.style.boxSizing = "border-box";
+ mirror.style.padding = "0";
+ mirror.innerHTML = lineList.map(line => `<div>${line.trim() ? escapeHtml(line) : "&nbsp;"}</div>`).join("");
+ // 挂到行号栏所在容器,保证字体上下文一致
+ (gutter.parentElement || document.body).appendChild(mirror);
+ let spansHTML = "";
+ for (let i = 0; i < lineList.length; i++) {
+ spansHTML += `<span style="height:${mirror.children[i].clientHeight}px"></span>`;
+ }
+ mirror.remove();
+ gutter.innerHTML = spansHTML;
+ };
const autoHeight = () => {
- textElement.style.height = textElement.scrollHeight + "px";
+ renderTextareaLineNumber();
if (isMobile()) {
setPosition(this.subElement, 0, 0);
return;
@@ -1105,9 +1134,7 @@ export class Toolbar {
if (!renderElement.parentElement) {
return;
}
- if (textElement.clientHeight !== textElement.scrollHeight) {
- autoHeight();
- }
+ autoHeight();
if (!this.subElement.querySelector('[data-type="refresh"]').classList.contains("block__icon--active")) {
return;
}
@@ -1266,12 +1293,7 @@ export class Toolbar {
this.subElement.classList.remove("fn__none");
const nodeRect = renderElement.getBoundingClientRect();
this.element.classList.add("fn__none");
- if (isPin) {
- textElement.style.width = pinData.styleW;
- textElement.style.height = pinData.styleH;
- } else {
- autoHeight();
- }
+ autoHeight();
if (!protyle.disabled) {
textElement.select();
}
@@ -1293,9 +1315,7 @@ export class Toolbar {
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
this.range = getEditorRange(nodeElement);
-
- this.subElement.style.width = "";
- this.subElement.style.padding = "";
+ this.clearSubElement();
this.subElement.innerHTML = `<div data-id="codeLanguage" class="fn__flex-column" style="max-height:50vh">
<input placeholder="${window.siyuan.languages.search}" style="margin: 0 8px 4px 8px" class="b3-text-field"/>
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative"></div>
@@ -1432,7 +1452,7 @@ export class Toolbar {
public showMultiSelectMode(protyle: IProtyle, blockElement: HTMLElement) {
blockElement.classList.add("protyle-wysiwyg--select");
window.siyuan.menus.menu.remove();
-
+ this.clearSubElement();
this.subElement.style.width = window.innerWidth - 16 + "px";
this.subElement.style.padding = "0";
this.subElement.innerHTML = `<div class="block__icons">
@@ -1473,21 +1493,11 @@ export class Toolbar {
activeBlur();
}
- public isMultiSelectMode() {
- let result = false;
- /// #if MOBILE
- result = !this.subElement.classList.contains("fn__none") &&
- !!this.subElement.querySelector('[data-type="exitMultiSelectMode"]');
- /// #endif
- return result;
- }
-
public showTpl(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
this.range = range;
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
- this.subElement.style.width = "";
- this.subElement.style.padding = "";
+ this.clearSubElement();
this.subElement.innerHTML = `<div style="max-height:50vh" class="fn__flex">
<div class="fn__flex-column" style="${isMobile() ? "width: 100%" : "width: 256px"}">
<div class="fn__flex" style="margin: 0 8px 4px 8px">
@@ -1670,8 +1680,7 @@ export class Toolbar {
this.range = range;
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
- this.subElement.style.width = "";
- this.subElement.style.padding = "";
+ this.clearSubElement();
this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh">
<input style="margin: 0 8px 4px 8px" class="b3-text-field"/>
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative"><img style="margin: 0 auto;display: block;width: 64px;height:64px" src="/stage/loading-pure.svg"></div>
@@ -1748,7 +1757,7 @@ export class Toolbar {
public showContent(protyle: IProtyle, range: Range, nodeElement: Element) {
this.range = range;
hideElements(["hint"], protyle);
-
+ this.clearSubElement();
this.subElement.style.width = "auto";
this.subElement.style.padding = "0 8px";
let html = "";
@@ -1840,6 +1849,15 @@ export class Toolbar {
setPosition(this.subElement, rangePosition.left, rangePosition.top - 48, this.LINE_HEIGHT);
}
+ public isMultiSelectMode() {
+ let result = false;
+ /// #if MOBILE
+ result = !this.subElement.classList.contains("fn__none") &&
+ !!this.subElement.querySelector('[data-type="exitMultiSelectMode"]');
+ /// #endif
+ return result;
+ }
+
private genItem(protyle: IProtyle, menuItem: IMenuItem) {
let menuItemObj;
switch (menuItem.name) {
@@ -1960,4 +1978,8 @@ export class Toolbar {
this.subElement.classList.add("fn__none");
focusByRange(this.range);
}
+
+ private clearSubElement() {
+ this.subElement.removeAttribute("style");
+ }
}