Skip to content

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

:art: Improve editor content width resizing https://github.com/siyuan-note/siyuan/issues/19120

Daniel1d40db9fa978
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index d3141c7c89..68981b6bc9 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -432,25 +432,98 @@
outline: none;
}
- &--transition {
+ &--backlink-reveal {
> .protyle-wysiwyg,
> .sy__backlink--bottom {
- transition: padding .3s cubic-bezier(.25, .46, .45, .94) 0ms;
+ transition: none;
}
+ }
+}
+.protyle-content[data-padding-mode="responsive"],
+.protyle-preview[data-padding-mode="responsive"] {
+ container-name: protyle-padding;
+ container-type: inline-size;
+}
+
+// 水平边距的断点与 getEditorHorizontalPadding 保持一致,容器尺寸变化时由浏览器实时更新。
+.protyle-content[data-padding-mode="responsive"] {
+ > .protyle-top,
+ > .protyle-wysiwyg,
+ > .sy__backlink--bottom {
+ --b3-protyle-padding-left: 24px;
+ --b3-protyle-padding-right: 16px;
+ }
+
+ > .protyle-wysiwyg,
+ > .sy__backlink--bottom {
+ padding-left: var(--b3-protyle-padding-left);
+ padding-right: var(--b3-protyle-padding-right);
+ }
+
+ > .protyle-top {
.protyle-title,
.protyle-background__ia,
.protyle-db-attr {
- transition: margin .3s cubic-bezier(.25, .46, .45, .94) 0ms;
+ margin-left: var(--b3-protyle-padding-left);
+ margin-right: var(--b3-protyle-padding-right);
}
}
+}
- &--backlink-reveal {
+.protyle-preview[data-padding-mode="responsive"][data-device="desktop"] > .b3-typography {
+ --b3-protyle-padding-left: 24px;
+ --b3-protyle-padding-right: 16px;
+ padding-left: var(--b3-protyle-padding-left);
+ padding-right: var(--b3-protyle-padding-right);
+}
+
+@container protyle-padding (width > 760px) {
+ .protyle-content[data-padding-mode="responsive"] {
+ > .protyle-top,
> .protyle-wysiwyg,
> .sy__backlink--bottom {
- transition: none;
+ --b3-protyle-padding-left: 96px;
+ --b3-protyle-padding-right: 96px;
}
}
+
+ .protyle-preview[data-padding-mode="responsive"][data-device="desktop"] > .b3-typography {
+ --b3-protyle-padding-left: 96px;
+ --b3-protyle-padding-right: 96px;
+ }
+}
+
+@container protyle-padding (width > 952px) {
+ .protyle-content[data-padding-mode="responsive"]:not([data-fullwidth]) {
+ > .protyle-top,
+ > .protyle-wysiwyg,
+ > .sy__backlink--bottom {
+ --b3-protyle-padding-left: calc((100cqi - 760px) / 2);
+ --b3-protyle-padding-right: calc((100cqi - 760px) / 2);
+ }
+ }
+
+ .protyle-preview[data-padding-mode="responsive"][data-device="desktop"]:not([data-fullwidth]) > .b3-typography {
+ --b3-protyle-padding-left: calc((100cqi - 760px) / 2);
+ --b3-protyle-padding-right: calc((100cqi - 760px) / 2);
+ }
+}
+
+@container protyle-padding (width > 2280px) {
+ .protyle-content[data-padding-mode="responsive"]:not([data-fullwidth]) {
+ > .protyle-top,
+ > .protyle-wysiwyg,
+ > .sy__backlink--bottom {
+ --b3-protyle-padding-left: calc(100cqi * .382 / 1.382);
+ --b3-protyle-padding-right: calc(100cqi * .382 / 1.382);
+ }
+ }
+
+ .protyle-preview[data-padding-mode="responsive"][data-device="desktop"]:not([data-fullwidth]) > .b3-typography {
+ --b3-protyle-padding-left: calc(100cqi * .382 / 1.382);
+ --b3-protyle-padding-right: calc(100cqi * .382 / 1.382);
+ }
}
.sy__backlink {
diff --git a/app/src/config/tabs/editorRuntime.ts b/app/src/config/tabs/editorRuntime.ts
index f12fafc7c8..6b88d15d57 100644
--- a/app/src/config/tabs/editorRuntime.ts
+++ b/app/src/config/tabs/editorRuntime.ts
@@ -44,8 +44,10 @@ const applyEditorConfig = (data: Config.IEditor) => {
resize(protyle);
if (isFullWidth === "true") {
protyle.contentElement.setAttribute("data-fullwidth", "true");
+ protyle.preview.element.setAttribute("data-fullwidth", "true");
} else {
protyle.contentElement.removeAttribute("data-fullwidth");
+ protyle.preview.element.removeAttribute("data-fullwidth");
}
});
if (refreshHeadingNumbers) {
diff --git a/app/src/layout/index.ts b/app/src/layout/index.ts
index 0704219bca..0078d656bf 100644
--- a/app/src/layout/index.ts
+++ b/app/src/layout/index.ts
@@ -89,17 +89,6 @@ export class Layout {
} else {
this.children.splice(index, 0, child);
}
- if (this.direction === "lr") {
- // 向右分屏,左侧文档抖动,移除动画和边距
- item.element.querySelectorAll(".protyle-content").forEach((element: HTMLElement) => {
- const wysiwygElement = element.querySelector<HTMLElement>(".protyle-wysiwyg");
- if (wysiwygElement && !element.parentElement.classList.contains("fn__none")) {
- element.classList.remove("protyle-content--transition");
- wysiwygElement.style.padding = "";
- element.classList.add("protyle-content--transition");
- }
- });
- }
if (after) {
item.element.after(child.element);
} else {
diff --git a/app/src/protyle/preview/index.ts b/app/src/protyle/preview/index.ts
index f7fe62e908..a00861e627 100644
--- a/app/src/protyle/preview/index.ts
+++ b/app/src/protyle/preview/index.ts
@@ -46,6 +46,9 @@ export class Preview {
previewElement.classList.add(protyle.options.classes.preview);
}
const actions = protyle.options.preview.actions;
+ if (actions.includes("desktop")) {
+ this.element.dataset.device = "desktop";
+ }
const actionElement = document.createElement("div");
actionElement.className = "protyle-preview__action";
const actionHtml: string[] = [];
@@ -153,6 +156,9 @@ export class Preview {
item.classList.remove("protyle-preview__action--current");
});
target.classList.add("protyle-preview__action--current");
+ if (["desktop", "tablet", "mobile"].includes(type)) {
+ this.element.dataset.device = type;
+ }
}
const actionCustom = actions.find((w: IPreviewActionCustom) => w?.key === type) as IPreviewActionCustom;
if (actionCustom) {
@@ -218,7 +224,13 @@ export class Preview {
public updatePadding(padding: { left: number, right: number, bottom: number, top: number }) {
if (!this.element.classList.contains("fn__none") &&
this.element.querySelector('.protyle-preview__action [data-type="desktop"]')?.classList.contains("protyle-preview__action--current")) {
- this.previewElement.style.padding = `${padding.top}px ${padding.left}px ${padding.bottom}px ${padding.right}px`;
+ if (this.element.dataset.paddingMode === "responsive") {
+ this.previewElement.style.padding = "";
+ this.previewElement.style.paddingTop = `${padding.top}px`;
+ this.previewElement.style.paddingBottom = `${padding.bottom}px`;
+ } else {
+ this.previewElement.style.padding = `${padding.top}px ${padding.left}px ${padding.bottom}px ${padding.right}px`;
+ }
}
}
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index c91a2c0d23..12210d0534 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -32,6 +32,14 @@ const focusMobileAppEditor = (element: HTMLElement) => {
export const initUI = (protyle: IProtyle) => {
protyle.contentElement = document.createElement("div");
protyle.contentElement.className = "protyle-content";
+ if (!isMobile() && !protyle.lite && CSS.supports("container-type", "inline-size") &&
+ CSS.supports("width", "1cqi") && !protyle.options.action.includes(Constants.CB_GET_HISTORY) &&
+ !protyle.options.backlinkData) {
+ protyle.contentElement.dataset.paddingMode = "responsive";
+ if (protyle.options.preview.actions.includes("desktop")) {
+ protyle.preview.element.dataset.paddingMode = "responsive";
+ }
+ }
if (protyle.options.render.background || protyle.options.render.title) {
protyle.contentElement.innerHTML = '<div class="protyle-top"></div>';
@@ -342,32 +350,64 @@ export const setPadding = (protyle: IProtyle) => {
}
const padding = getPadding(protyle);
protyle.preview.updatePadding(padding);
- const paddingLeft = protyle.options.backlinkData ? 24 : padding.left;
- const paddingRight = protyle.options.backlinkData ? 16 : padding.right;
+ const responsivePadding = protyle.contentElement.dataset.paddingMode === "responsive";
+ let paddingLeft = protyle.options.backlinkData ? 24 : padding.left;
+ let paddingRight = protyle.options.backlinkData ? 16 : padding.right;
const backlinkBottomElement = protyle.contentElement.querySelector(".sy__backlink--bottom") as HTMLElement;
const backlinkBottomVisible = backlinkBottomElement &&
!backlinkBottomElement.classList.contains("fn__none") &&
!backlinkBottomElement.classList.contains("sy__backlink--pending");
const backlinkBottomGap = 128;
- if (protyle.options.backlinkData) {
+ if (responsivePadding) {
+ const paddingBottom = backlinkBottomVisible && protyle.options.typewriterMode ? backlinkBottomGap : padding.bottom;
+ protyle.wysiwyg.element.style.padding = "";
+ protyle.wysiwyg.element.style.paddingTop = `${padding.top}px`;
+ protyle.wysiwyg.element.style.paddingBottom = `${paddingBottom}px`;
+ const wysiwygStyle = getComputedStyle(protyle.wysiwyg.element);
+ paddingLeft = parseFloat(wysiwygStyle.paddingLeft);
+ paddingRight = parseFloat(wysiwygStyle.paddingRight);
+ } else if (protyle.options.backlinkData) {
protyle.wysiwyg.element.style.padding = `4px ${paddingRight}px 4px ${paddingLeft}px`;
} else {
const paddingBottom = backlinkBottomVisible && protyle.options.typewriterMode ? backlinkBottomGap : padding.bottom;
protyle.wysiwyg.element.style.padding = `${padding.top}px ${paddingRight}px ${paddingBottom}px ${paddingLeft}px`;
}
if (protyle.options.render.background) {
- protyle.background.element.querySelector(".protyle-background__ia").setAttribute("style", `margin-left:${paddingLeft}px;margin-right:${paddingRight}px`);
+ const backgroundIAElement = protyle.background.element.querySelector<HTMLElement>(".protyle-background__ia");
+ if (responsivePadding) {
+ backgroundIAElement.style.margin = "";
+ } else {
+ backgroundIAElement.style.marginLeft = `${paddingLeft}px`;
+ backgroundIAElement.style.marginRight = `${paddingRight}px`;
+ }
}
if (protyle.options.render.title) {
// pc 端 文档名 attr 过长和添加标签等按钮重合
- protyle.title.element.style.margin = `16px ${paddingRight}px 0 ${paddingLeft}px`;
+ if (responsivePadding) {
+ protyle.title.element.style.margin = "";
+ protyle.title.element.style.marginTop = "16px";
+ } else {
+ protyle.title.element.style.margin = `16px ${paddingRight}px 0 ${paddingLeft}px`;
+ }
}
if (protyle.databaseAttributePanel) {
- protyle.databaseAttributePanel.element.style.margin = `8px ${paddingRight}px 8px ${paddingLeft}px`;
+ if (responsivePadding) {
+ protyle.databaseAttributePanel.element.style.margin = "";
+ protyle.databaseAttributePanel.element.style.marginTop = "8px";
+ protyle.databaseAttributePanel.element.style.marginBottom = "8px";
+ } else {
+ protyle.databaseAttributePanel.element.style.margin = `8px ${paddingRight}px 8px ${paddingLeft}px`;
+ }
}
if (backlinkBottomElement) {
- backlinkBottomElement.style.padding = `0 ${paddingRight}px 16px ${paddingLeft}px`;
+ if (responsivePadding) {
+ backlinkBottomElement.style.padding = "";
+ backlinkBottomElement.style.paddingTop = "0";
+ backlinkBottomElement.style.paddingBottom = "16px";
+ } else {
+ backlinkBottomElement.style.padding = `0 ${paddingRight}px 16px ${paddingLeft}px`;
+ }
backlinkBottomElement.style.marginBottom = backlinkBottomVisible && protyle.options.typewriterMode ?
`${Math.max(padding.bottom - backlinkBottomGap, 0)}px` : "";
}
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 4c728e3c97..e459c0ae80 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -1195,9 +1195,10 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
};
const getAdjustedDragTarget = (event: DragEvent) => {
const contentRect = protyle.contentElement.getBoundingClientRect();
- const editorLeft = contentRect.left + (parseInt(editorElement.style.paddingLeft) || 0);
+ const editorStyle = getComputedStyle(editorElement);
+ const editorLeft = contentRect.left + (parseFloat(editorStyle.paddingLeft) || 0);
const editorRight = contentRect.left + protyle.contentElement.clientWidth -
- (parseInt(editorElement.style.paddingRight) || 0);
+ (parseFloat(editorStyle.paddingRight) || 0);
const x = event.clientX < editorLeft ? editorLeft :
(event.clientX >= editorRight ? editorRight - 6 : event.clientX);
return document.elementFromPoint(x, event.clientY);
@@ -2505,9 +2506,10 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
point.className = "dragover__top";
} else {
const contentRect = protyle.contentElement.getBoundingClientRect();
+ const editorStyle = getComputedStyle(editorElement);
const editorPosition = {
- left: contentRect.left + parseInt(editorElement.style.paddingLeft),
- right: contentRect.left + protyle.contentElement.clientWidth - parseInt(editorElement.style.paddingRight)
+ left: contentRect.left + parseFloat(editorStyle.paddingLeft),
+ right: contentRect.left + protyle.contentElement.clientWidth - parseFloat(editorStyle.paddingRight)
};
if (event.clientX < editorPosition.left) {
// 左侧
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index e75fa1f457..05a3623925 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -515,8 +515,10 @@ export class WYSIWYG {
}
if (isFullWidth === "true") {
this.element.parentElement.setAttribute("data-fullwidth", "true");
+ this.protyle.preview.element.setAttribute("data-fullwidth", "true");
} else {
this.element.parentElement.removeAttribute("data-fullwidth");
+ this.protyle.preview.element.removeAttribute("data-fullwidth");
}
const ialKeys = Object.keys(ial);
for (let i = 0; i < this.element.attributes.length; i++) {
@@ -5052,7 +5054,8 @@ export class WYSIWYG {
if (embedElement) {
blockElement = embedElement;
}
- newRange = focusBlock(blockElement, undefined, event.clientX < rect.left + parseInt(this.element.style.paddingLeft)) || newRange;
+ const paddingLeft = parseFloat(getComputedStyle(this.element).paddingLeft);
+ newRange = focusBlock(blockElement, undefined, event.clientX < rect.left + paddingLeft) || newRange;
if (protyle.options.render.breadcrumb) {
protyle.breadcrumb.render(protyle, false, blockElement);
}