Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(docs): spacing defaults and paragraph hover menu (#7042)

Univerc68c4129a93e
diff --git a/common/mockdata/src/docs/default-document-data-en.ts b/common/mockdata/src/docs/default-document-data-en.ts
index 6f24ce73c0..a5e27b9c5b 100644
--- a/common/mockdata/src/docs/default-document-data-en.ts
+++ b/common/mockdata/src/docs/default-document-data-en.ts
@@ -199,6 +199,7 @@ export const DEFAULT_DOCUMENT_DATA_EN: IDocumentData = {
{
startIndex: 67,
},
+ // Keep the English default demo aligned with the current docs default spacing.
{
startIndex: 253,
paragraphStyle: {
diff --git a/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/default-doc-ci-chromium-linux.png b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/default-doc-ci-chromium-linux.png
index 63c0c69485..f61f7982d6 100644
Binary files a/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/default-doc-ci-chromium-linux.png and b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/default-doc-ci-chromium-linux.png differ
diff --git a/packages/core/src/docs/data-model/__tests__/empty-snapshot.spec.ts b/packages/core/src/docs/data-model/__tests__/empty-snapshot.spec.ts
index dbf14f1f58..4639fe4e8f 100644
--- a/packages/core/src/docs/data-model/__tests__/empty-snapshot.spec.ts
+++ b/packages/core/src/docs/data-model/__tests__/empty-snapshot.spec.ts
@@ -18,11 +18,11 @@ import { describe, expect, it } from 'vitest';
import { getEmptySnapshot } from '../empty-snapshot';
describe('getEmptySnapshot', () => {
- it('uses comfortable paragraph spacing defaults for new docs', () => {
+ it('uses the configured paragraph spacing defaults for new docs', () => {
expect(getEmptySnapshot().body?.paragraphs?.[0].paragraphStyle).toEqual({
spaceAbove: { v: 0 },
lineSpacing: 1.5,
- spaceBelow: { v: 8 },
+ spaceBelow: { v: 12 },
});
});
});
diff --git a/packages/core/src/types/const/const.ts b/packages/core/src/types/const/const.ts
index c35a96db46..065e733d81 100644
--- a/packages/core/src/types/const/const.ts
+++ b/packages/core/src/types/const/const.ts
@@ -186,7 +186,8 @@ const BOTTOM_P = 4;
export const DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING = 1.5;
export const DEFAULT_DOCUMENT_PARAGRAPH_SPACE_ABOVE = 0;
-export const DEFAULT_DOCUMENT_PARAGRAPH_SPACE_BELOW = 8;
+// Docs defaults keep fixed paragraph-after spacing in px to match the current product setting model.
+export const DEFAULT_DOCUMENT_PARAGRAPH_SPACE_BELOW = 12;
export const NAMED_STYLE_SPACE_MAP: Record<NamedStyleType, Nullable<IParagraphStyle>> = {
[NamedStyleType.HEADING_1]: {
diff --git a/packages/docs-ui/src/components/paragraph-menu/index.tsx b/packages/docs-ui/src/components/paragraph-menu/index.tsx
index 4b24c5a655..c634a2e0d1 100644
--- a/packages/docs-ui/src/components/paragraph-menu/index.tsx
+++ b/packages/docs-ui/src/components/paragraph-menu/index.tsx
@@ -46,7 +46,7 @@ export function getParagraphMenuPopupDirection(anchorLeft: number, menuWidth = 2
return anchorLeft - menuWidth < viewportPadding ? 'right' : 'left';
}
-export const PARAGRAPH_MENU_HOVER_OPEN_DELAY = 260;
+export const PARAGRAPH_MENU_HOVER_OPEN_DELAY = 800;
export function createParagraphMenuHoverOpenScheduler(openMenu: () => void, delay = PARAGRAPH_MENU_HOVER_OPEN_DELAY) {
let openTimer: number | null = null;
diff --git a/packages/docs-ui/src/locale/ar-SA.ts b/packages/docs-ui/src/locale/ar-SA.ts
index 095adb79ff..20b0590af2 100644
--- a/packages/docs-ui/src/locale/ar-SA.ts
+++ b/packages/docs-ui/src/locale/ar-SA.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'بعد',
lineSpace: 'تباعد الأسطر',
multiSpace: 'تباعد متعدد',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'قيمة ثابتة (بكسل)',
},
},
diff --git a/packages/docs-ui/src/locale/ca-ES.ts b/packages/docs-ui/src/locale/ca-ES.ts
index dcccc72156..ddd7689a15 100644
--- a/packages/docs-ui/src/locale/ca-ES.ts
+++ b/packages/docs-ui/src/locale/ca-ES.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Després',
lineSpace: 'Espai entre línies',
multiSpace: 'Espai múltiple',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Valor fix (px)',
},
},
diff --git a/packages/docs-ui/src/locale/de-DE.ts b/packages/docs-ui/src/locale/de-DE.ts
index 375c64d838..9c75e4ea35 100644
--- a/packages/docs-ui/src/locale/de-DE.ts
+++ b/packages/docs-ui/src/locale/de-DE.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Nach',
lineSpace: 'Zeilenabstand',
multiSpace: 'Mehrfacher Abstand',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Fester Wert (px)',
},
},
diff --git a/packages/docs-ui/src/locale/en-US.ts b/packages/docs-ui/src/locale/en-US.ts
index 69706a0a34..3515d4b404 100644
--- a/packages/docs-ui/src/locale/en-US.ts
+++ b/packages/docs-ui/src/locale/en-US.ts
@@ -93,6 +93,8 @@ const locale = {
after: 'After',
lineSpace: 'Line Space',
multiSpace: 'Multi Space',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Fixed Value(px)',
},
},
diff --git a/packages/docs-ui/src/locale/es-ES.ts b/packages/docs-ui/src/locale/es-ES.ts
index 2c6a4fc4a2..61e2c5352d 100644
--- a/packages/docs-ui/src/locale/es-ES.ts
+++ b/packages/docs-ui/src/locale/es-ES.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Después',
lineSpace: 'Espacio entre líneas',
multiSpace: 'Espacio múltiple',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Valor fijo (px)',
},
},
diff --git a/packages/docs-ui/src/locale/fa-IR.ts b/packages/docs-ui/src/locale/fa-IR.ts
index 29b5938812..e03aab25a8 100644
--- a/packages/docs-ui/src/locale/fa-IR.ts
+++ b/packages/docs-ui/src/locale/fa-IR.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'بعد',
lineSpace: 'فاصله خط',
multiSpace: 'فاصله چندگانه',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'مقدار ثابت(px)',
},
},
diff --git a/packages/docs-ui/src/locale/fr-FR.ts b/packages/docs-ui/src/locale/fr-FR.ts
index d6ce9be3ec..9ca4059ff8 100644
--- a/packages/docs-ui/src/locale/fr-FR.ts
+++ b/packages/docs-ui/src/locale/fr-FR.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Après',
lineSpace: 'Espacement de ligne',
multiSpace: 'Espacement multiple',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Valeur fixe(px)',
},
},
diff --git a/packages/docs-ui/src/locale/id-ID.ts b/packages/docs-ui/src/locale/id-ID.ts
index cb423efcf5..efac43fe08 100644
--- a/packages/docs-ui/src/locale/id-ID.ts
+++ b/packages/docs-ui/src/locale/id-ID.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Sesudah',
lineSpace: 'Spasi Baris',
multiSpace: 'Spasi Ganda',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Nilai Tetap(px)',
},
},
diff --git a/packages/docs-ui/src/locale/it-IT.ts b/packages/docs-ui/src/locale/it-IT.ts
index d22d7d1b4d..0b526367a1 100644
--- a/packages/docs-ui/src/locale/it-IT.ts
+++ b/packages/docs-ui/src/locale/it-IT.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Dopo',
lineSpace: 'Interlinea',
multiSpace: 'Spaziatura multipla',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Valore fisso (px)',
},
},
diff --git a/packages/docs-ui/src/locale/ja-JP.ts b/packages/docs-ui/src/locale/ja-JP.ts
index 5addf98157..a51e09f22f 100644
--- a/packages/docs-ui/src/locale/ja-JP.ts
+++ b/packages/docs-ui/src/locale/ja-JP.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: '後の間隔',
lineSpace: '行間',
multiSpace: '複数行間',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: '固定値(px)',
},
},
diff --git a/packages/docs-ui/src/locale/ko-KR.ts b/packages/docs-ui/src/locale/ko-KR.ts
index 1a7bc149d6..b066e68aab 100644
--- a/packages/docs-ui/src/locale/ko-KR.ts
+++ b/packages/docs-ui/src/locale/ko-KR.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: '아래 간격',
lineSpace: '줄 간격',
multiSpace: '복수 줄 간격',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: '고정 값(px)',
},
},
diff --git a/packages/docs-ui/src/locale/pl-PL.ts b/packages/docs-ui/src/locale/pl-PL.ts
index a2b09e99be..4f9fa01c26 100644
--- a/packages/docs-ui/src/locale/pl-PL.ts
+++ b/packages/docs-ui/src/locale/pl-PL.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Po',
lineSpace: 'Odstęp między wierszami',
multiSpace: 'Wielokrotny odstęp',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Wartość stała (px)',
},
},
diff --git a/packages/docs-ui/src/locale/pt-BR.ts b/packages/docs-ui/src/locale/pt-BR.ts
index c50452272d..3312e34c88 100644
--- a/packages/docs-ui/src/locale/pt-BR.ts
+++ b/packages/docs-ui/src/locale/pt-BR.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Depois',
lineSpace: 'Espaçamento entre linhas',
multiSpace: 'Espaçamento múltiplo',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Valor fixo (px)',
},
},
diff --git a/packages/docs-ui/src/locale/ru-RU.ts b/packages/docs-ui/src/locale/ru-RU.ts
index ba0942d35d..3942349caa 100644
--- a/packages/docs-ui/src/locale/ru-RU.ts
+++ b/packages/docs-ui/src/locale/ru-RU.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'После',
lineSpace: 'Высота строки',
multiSpace: 'Двойной отступ',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Фиксированное значение (px)',
},
},
diff --git a/packages/docs-ui/src/locale/sk-SK.ts b/packages/docs-ui/src/locale/sk-SK.ts
index f83eee99f9..bb07aeb412 100644
--- a/packages/docs-ui/src/locale/sk-SK.ts
+++ b/packages/docs-ui/src/locale/sk-SK.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'Za',
lineSpace: 'Riadkovanie',
multiSpace: 'Viacnásobné',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Pevná hodnota (px)',
},
},
diff --git a/packages/docs-ui/src/locale/vi-VN.ts b/packages/docs-ui/src/locale/vi-VN.ts
index febb7eabc3..dca4eb7878 100644
--- a/packages/docs-ui/src/locale/vi-VN.ts
+++ b/packages/docs-ui/src/locale/vi-VN.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'After',
lineSpace: 'Line Space',
multiSpace: 'Multi Space',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Fixed Value(px)',
},
},
diff --git a/packages/docs-ui/src/locale/zh-CN.ts b/packages/docs-ui/src/locale/zh-CN.ts
index d3f486f5e0..f4c6620c99 100644
--- a/packages/docs-ui/src/locale/zh-CN.ts
+++ b/packages/docs-ui/src/locale/zh-CN.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: '段落后',
lineSpace: '行距',
multiSpace: '多倍行距',
+ atLeast: '至少值(px)',
+ exactly: '固定值(px)',
fixedValue: '固定值(px)',
},
},
diff --git a/packages/docs-ui/src/locale/zh-HK.ts b/packages/docs-ui/src/locale/zh-HK.ts
index 293b3d11b6..d7c395fe6f 100644
--- a/packages/docs-ui/src/locale/zh-HK.ts
+++ b/packages/docs-ui/src/locale/zh-HK.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'After',
lineSpace: 'Line Space',
multiSpace: 'Multi Space',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Fixed Value(px)',
},
},
diff --git a/packages/docs-ui/src/locale/zh-TW.ts b/packages/docs-ui/src/locale/zh-TW.ts
index 9a882ff073..a4d0e4b8b2 100644
--- a/packages/docs-ui/src/locale/zh-TW.ts
+++ b/packages/docs-ui/src/locale/zh-TW.ts
@@ -95,6 +95,8 @@ const locale: typeof enUS = {
after: 'After',
lineSpace: 'Line Space',
multiSpace: 'Multi Space',
+ atLeast: 'At Least (px)',
+ exactly: 'Exactly (px)',
fixedValue: 'Fixed Value(px)',
},
},
diff --git a/packages/docs-ui/src/views/paragraph-setting/Setting.tsx b/packages/docs-ui/src/views/paragraph-setting/Setting.tsx
index 2d3de04bcd..9e952fe20e 100644
--- a/packages/docs-ui/src/views/paragraph-setting/Setting.tsx
+++ b/packages/docs-ui/src/views/paragraph-setting/Setting.tsx
@@ -15,7 +15,7 @@
*/
import type { ReactNode } from 'react';
-import { HorizontalAlign, LocaleService, SpacingRule } from '@univerjs/core';
+import { HorizontalAlign, LocaleService } from '@univerjs/core';
import { borderClassName, clsx, InputNumber, Select, Tooltip } from '@univerjs/design';
import { AlignTextBothIcon, HorizontallyIcon, LeftJustifyingIcon, RightJustifyingIcon } from '@univerjs/icons';
import { useDependency } from '@univerjs/ui';
@@ -31,6 +31,7 @@ import {
useFirstParagraphLineSpacing,
useFirstParagraphSpaceBelow,
} from './hook/utils';
+import { getLineSpacingInputConfig, getLineSpacingRuleOptions } from './line-spacing';
const PARAGRAPH_SETTING_CONTROL_CLASS = 'univer-w-full';
@@ -131,12 +132,8 @@ export function ParagraphSetting() {
const [spaceBelow, setSpaceBelow] = useFirstParagraphSpaceBelow(currentParagraph);
const { lineSpacing: [lineSpacing, setLineSpacing], spacingRule: [spacingRule, setSpacingRule] } = useFirstParagraphLineSpacing(currentParagraph);
- const lineSpaceConfig = useMemo(() => {
- if (spacingRule === SpacingRule.AUTO) {
- return { min: 1, max: 5, step: 0.1 };
- }
- return { min: 1, max: 100 };
- }, [spacingRule]);
+ const lineSpaceConfig = useMemo(() => getLineSpacingInputConfig(spacingRule), [spacingRule]);
+ const lineSpacingOptions = useMemo(() => getLineSpacingRuleOptions(localeService), [localeService]);
return (
<div className="univer-box-border univer-w-full">
@@ -210,10 +207,7 @@ export function ParagraphSetting() {
<Select
className={PARAGRAPH_SETTING_CONTROL_CLASS}
value={`${spacingRule}`}
- options={[
- { label: localeService.t('docs-ui.doc.paragraphSetting.multiSpace'), value: `${SpacingRule.AUTO}` },
- { label: localeService.t('docs-ui.doc.paragraphSetting.fixedValue'), value: `${SpacingRule.AT_LEAST}` },
- ]}
+ options={lineSpacingOptions}
onChange={(v) => setSpacingRule(Number(v))}
/>
<AutoFocusInputNumber
diff --git a/packages/docs-ui/src/views/paragraph-setting/__tests__/line-spacing.spec.ts b/packages/docs-ui/src/views/paragraph-setting/__tests__/line-spacing.spec.ts
new file mode 100644
index 0000000000..044b23d68e
--- /dev/null
+++ b/packages/docs-ui/src/views/paragraph-setting/__tests__/line-spacing.spec.ts
@@ -0,0 +1,63 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { SpacingRule } from '@univerjs/core';
+import { describe, expect, it } from 'vitest';
+import {
+ convertDisplayLineSpacingToStoredValue,
+ convertLineSpacingForRuleChange,
+ convertStoredLineSpacingToDisplayValue,
+ getLineSpacingInputConfig,
+ getLineSpacingRuleOptions,
+} from '../line-spacing';
+
+describe('paragraph line spacing helpers', () => {
+ it('exposes Word-style spacing rule options', () => {
+ expect(getLineSpacingRuleOptions({
+ t: (key: string) => key,
+ } as any)).toEqual([
+ { label: 'docs-ui.doc.paragraphSetting.multiSpace', value: `${SpacingRule.AUTO}` },
+ { label: 'docs-ui.doc.paragraphSetting.atLeast', value: `${SpacingRule.AT_LEAST}` },
+ { label: 'docs-ui.doc.paragraphSetting.exactly', value: `${SpacingRule.EXACT}` },
+ ]);
+ });
+
+ it('uses separate input ranges for multiple and fixed spacing', () => {
+ expect(getLineSpacingInputConfig(SpacingRule.AUTO)).toEqual({ min: 1, max: 5, step: 0.1 });
+ expect(getLineSpacingInputConfig(SpacingRule.AT_LEAST)).toEqual({ min: 1, max: 100 });
+ expect(getLineSpacingInputConfig(SpacingRule.EXACT)).toEqual({ min: 1, max: 100 });
+ });
+
+ it('keeps fixed spacing values in pixels to avoid changing existing input semantics', () => {
+ expect(convertStoredLineSpacingToDisplayValue(24, SpacingRule.EXACT)).toBeCloseTo(24, 4);
+ expect(convertDisplayLineSpacingToStoredValue(18, SpacingRule.EXACT)).toBeCloseTo(18, 4);
+ expect(convertStoredLineSpacingToDisplayValue(1.5, SpacingRule.AUTO)).toBe(1.5);
+ });
+
+ it('preserves the current rendered height when switching between multiple and exact spacing', () => {
+ const toExact = convertLineSpacingForRuleChange(1.5, SpacingRule.AUTO, SpacingRule.EXACT, {
+ glyphLineHeight: 16,
+ renderedLineHeight: 24,
+ });
+ const backToAuto = convertLineSpacingForRuleChange(toExact, SpacingRule.EXACT, SpacingRule.AUTO, {
+ glyphLineHeight: 16,
+ renderedLineHeight: 24,
+ });
+
+ expect(toExact).toBeCloseTo(24, 4);
+ expect(backToAuto).toBeCloseTo(1.5, 4);
+ });
+});
diff --git a/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts b/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
index 557fc764d3..6f73431ce9 100644
--- a/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
+++ b/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
@@ -20,12 +20,13 @@ import { BuildTextUtils, DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING, DEFAULT_DOCUME
import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';
import { getNumberUnitValue, IRenderManagerService } from '@univerjs/engine-render';
import { useDependency } from '@univerjs/ui';
-import { useEffect, useMemo, useRef, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import { BehaviorSubject } from 'rxjs';
import { bufferTime, filter, map } from 'rxjs/operators';
import { DocParagraphSettingCommand } from '../../../commands/commands/doc-paragraph-setting.command';
import { findNearestSectionBreak } from '../../../commands/commands/list.command';
import { DocParagraphSettingController } from '../../../controllers/doc-paragraph-setting.controller';
+import { convertDisplayLineSpacingToStoredValue, convertLineSpacingForRuleChange, convertStoredLineSpacingToDisplayValue, getLineSpacingMetrics } from '../line-spacing';
const useDocRanges = () => {
const docSelectionManagerService = useDependency(DocSelectionManagerService);
@@ -246,51 +247,53 @@ export const useFirstParagraphLineSpacing = (paragraph: IParagraph[]) => {
const stateChange$ = useMemo(() => new BehaviorSubject<{ spacingRule?: SpacingRule; lineSpacing?: number }>({}), []);
- const [lineSpacing, setLineSpacingInternal] = useState(() => {
+ const [spacingRule, setSpacingRuleInternal] = useState<SpacingRule>(() => {
const firstParagraph = paragraph[0];
if (!firstParagraph) {
- return 1.5;
+ return SpacingRule.AUTO;
}
- return firstParagraph.paragraphStyle?.lineSpacing ?? DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING;
+ return firstParagraph.paragraphStyle?.spacingRule ?? SpacingRule.AUTO;
});
- const lineSpacingCache = useRef<number>(lineSpacing);
-
- const [spacingRule, setSpacingRuleInternal] = useState<SpacingRule>(() => {
+ const [lineSpacing, setLineSpacingInternal] = useState(() => {
const firstParagraph = paragraph[0];
if (!firstParagraph) {
- return SpacingRule.AUTO;
+ return DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING;
}
- return firstParagraph.paragraphStyle?.spacingRule ?? SpacingRule.AUTO;
+ const currentSpacingRule = firstParagraph.paragraphStyle?.spacingRule ?? SpacingRule.AUTO;
+ const storedLineSpacing = firstParagraph.paragraphStyle?.lineSpacing ?? DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING;
+
+ return convertStoredLineSpacingToDisplayValue(storedLineSpacing, currentSpacingRule);
});
const setLineSpacing = async (v: number) => {
setLineSpacingInternal(v);
- stateChange$.next({ lineSpacing: v, spacingRule });
+ stateChange$.next({
+ lineSpacing: convertDisplayLineSpacingToStoredValue(v, spacingRule),
+ spacingRule,
+ });
};
const setSpacingRule = async (v: SpacingRule) => {
if (v !== spacingRule) {
- let cache = lineSpacingCache.current;
- if (v === SpacingRule.AT_LEAST) {
- const glyphNode = skeleton?.findNodeByCharIndex(paragraph[0].startIndex);
- const divideNode = glyphNode?.parent;
- const lineNode = divideNode?.parent;
- if (lineNode?.contentHeight !== undefined) {
- cache = Math.max(lineNode.contentHeight, cache);
- }
- } else {
- // If the paragraph is set to fixed-spacing by default,
- // the first time you enter the panel,
- // you will set the fixed-spacing value to the initial value of multiple-spacing
- if (cache > 5) {
- cache = 2;
- }
- }
- lineSpacingCache.current = lineSpacing;
- setLineSpacing(cache);
+ const glyphNode = skeleton?.findNodeByCharIndex(paragraph[0].startIndex);
+ const divideNode = glyphNode?.parent;
+ const lineNode = divideNode?.parent;
+ const metrics = getLineSpacingMetrics(lineNode as any);
+ const nextStoredLineSpacing = convertLineSpacingForRuleChange(
+ convertDisplayLineSpacingToStoredValue(lineSpacing, spacingRule),
+ spacingRule,
+ v,
+ metrics
+ );
+ const nextDisplayLineSpacing = convertStoredLineSpacingToDisplayValue(nextStoredLineSpacing, v);
+
+ setLineSpacingInternal(nextDisplayLineSpacing);
setSpacingRuleInternal(v);
- stateChange$.next({ spacingRule: v });
+ stateChange$.next({
+ spacingRule: v,
+ lineSpacing: nextStoredLineSpacing,
+ });
}
};
diff --git a/packages/docs-ui/src/views/paragraph-setting/line-spacing.ts b/packages/docs-ui/src/views/paragraph-setting/line-spacing.ts
new file mode 100644
index 0000000000..da865ef8da
--- /dev/null
+++ b/packages/docs-ui/src/views/paragraph-setting/line-spacing.ts
@@ -0,0 +1,118 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { LocaleService } from '@univerjs/core';
+import { DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING, DEFAULT_STYLES, SpacingRule } from '@univerjs/core';
+
+interface ILineSpacingMetrics {
+ glyphLineHeight: number;
+ renderedLineHeight: number;
+}
+
+interface ILineSpacingLike {
+ contentHeight?: number;
+ paddingTop?: number;
+ paddingBottom?: number;
+ divides?: Array<{
+ glyphGroup?: Array<{
+ bBox?: {
+ ba?: number;
+ bd?: number;
+ };
+ }>;
+ }>;
+}
+
+const AUTO_LINE_SPACING_CONFIG = { min: 1, max: 5, step: 0.1 } as const;
+const FIXED_LINE_SPACING_CONFIG = { min: 1, max: 100 } as const;
+
+function round(value: number, digits: number) {
+ const factor = 10 ** digits;
+ return Math.round(value * factor) / factor;
+}
+
+function clamp(value: number, min: number, max: number) {
+ return Math.min(max, Math.max(min, value));
+}
+
+export function getLineSpacingRuleOptions(localeService: LocaleService) {
+ return [
+ { label: localeService.t('docs-ui.doc.paragraphSetting.multiSpace'), value: `${SpacingRule.AUTO}` },
+ { label: localeService.t('docs-ui.doc.paragraphSetting.atLeast'), value: `${SpacingRule.AT_LEAST}` },
+ { label: localeService.t('docs-ui.doc.paragraphSetting.exactly'), value: `${SpacingRule.EXACT}` },
+ ];
+}
+
+export function getLineSpacingInputConfig(spacingRule: SpacingRule) {
+ return spacingRule === SpacingRule.AUTO ? AUTO_LINE_SPACING_CONFIG : FIXED_LINE_SPACING_CONFIG;
+}
+
+export function convertStoredLineSpacingToDisplayValue(lineSpacing: number, spacingRule: SpacingRule) {
+ return spacingRule === SpacingRule.AUTO ? lineSpacing : round(lineSpacing, 2);
+}
+
+export function convertDisplayLineSpacingToStoredValue(lineSpacing: number, spacingRule: SpacingRule) {
+ return spacingRule === SpacingRule.AUTO ? lineSpacing : round(lineSpacing, 4);
+}
+
+export function getLineSpacingMetrics(lineNode?: ILineSpacingLike | null): ILineSpacingMetrics | null {
+ if (!lineNode) {
+ return null;
+ }
+
+ const glyphLineHeight = Math.max(
+ 0,
+ ...(lineNode.divides ?? []).flatMap((divide) =>
+ (divide.glyphGroup ?? []).map((glyph) => (glyph.bBox?.ba ?? 0) + (glyph.bBox?.bd ?? 0))
+ )
+ ) || lineNode.contentHeight || 0;
+
+ if (!glyphLineHeight) {
+ return null;
+ }
+
+ const renderedLineHeight = (lineNode.paddingTop ?? 0) + (lineNode.contentHeight ?? glyphLineHeight) + (lineNode.paddingBottom ?? 0);
+
+ return {
+ glyphLineHeight,
+ renderedLineHeight: renderedLineHeight || glyphLineHeight,
+ };
+}
+
+export function convertLineSpacingForRuleChange(
+ currentStoredValue: number,
+ currentRule: SpacingRule,
+ nextRule: SpacingRule,
+ metrics?: ILineSpacingMetrics | null
+) {
+ if (currentRule === nextRule) {
+ return currentStoredValue;
+ }
+
+ if (nextRule === SpacingRule.AUTO) {
+ if (metrics?.glyphLineHeight && metrics.renderedLineHeight) {
+ return round(clamp(metrics.renderedLineHeight / metrics.glyphLineHeight, AUTO_LINE_SPACING_CONFIG.min, AUTO_LINE_SPACING_CONFIG.max), 2);
+ }
+
+ return DEFAULT_DOCUMENT_PARAGRAPH_LINE_SPACING;
+ }
+
+ if (metrics?.renderedLineHeight) {
+ return round(metrics.renderedLineHeight, 4);
+ }
+
+ return round(DEFAULT_STYLES.fs * currentStoredValue, 4);
+}
diff --git a/packages/engine-render/src/basics/interfaces.ts b/packages/engine-render/src/basics/interfaces.ts
index ab91943f6b..eaf903aeb0 100644
--- a/packages/engine-render/src/basics/interfaces.ts
+++ b/packages/engine-render/src/basics/interfaces.ts
@@ -127,6 +127,7 @@ export interface IParagraphTableCache {
export interface IParagraphConfig {
paragraphIndex: number;
+ useWordStyleLineHeight?: boolean;
paragraphNonInlineSkeDrawings?: Map<string, IDocumentSkeletonDrawing>;
paragraphInlineSkeDrawings?: Map<string, IDocumentSkeletonDrawing>;
skeTablesInParagraph?: IParagraphTableCache[];
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
index cb94c0079d..8be220a061 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
@@ -15,13 +15,18 @@
*/
import type { IParagraphConfig } from '../../../../../../basics/interfaces';
+import { BooleanNumber, GridType, SpacingRule } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
-import { layoutParagraph } from '../layout-ruler';
+import { getLineHeightMetrics, layoutParagraph } from '../layout-ruler';
import { lineBreaking } from '../linebreaking';
import { shaping } from '../shaping';
import { createParagraphLayoutTestBed } from './create-paragraph-layout-test-bed';
describe('layout-ruler', () => {
+ function getLineBoxHeight(metrics: ReturnType<typeof getLineHeightMetrics>) {
+ return metrics.paddingTop + metrics.contentHeight + metrics.paddingBottom;
+ }
+
it('lays out first shaped text with bullet skeleton', () => {
const { ctx, paragraphNode, sectionBreakConfig, curPage } = createParagraphLayoutTestBed('Item');
const shapedTextList = shaping(ctx, paragraphNode.content!, ctx.viewModel, paragraphNode, sectionBreakConfig);
@@ -121,4 +126,37 @@ describe('layout-ruler', () => {
const lastPage = result[result.length - 1];
expect(lastPage.sections.length).toBeGreaterThan(0);
});
+
+ it('uses glyph height as the base for auto line spacing when grid snapping is not explicitly enabled', () => {
+ const metrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 1.5, SpacingRule.AUTO, BooleanNumber.FALSE, true);
+
+ expect(getLineBoxHeight(metrics)).toBeCloseTo(24, 4);
+ });
+
+ it('keeps document-grid line pitch behavior when auto line spacing explicitly snaps to the grid', () => {
+ const metrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 1.5, SpacingRule.AUTO, BooleanNumber.TRUE, true);
+
+ expect(getLineBoxHeight(metrics)).toBeCloseTo(23.4, 4);
+ });
+
+ it('treats at-least spacing as a minimum line box height', () => {
+ const compactMetrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 10, SpacingRule.AT_LEAST, BooleanNumber.FALSE, true);
+ const expandedMetrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 40, SpacingRule.AT_LEAST, BooleanNumber.FALSE, true);
+
+ expect(getLineBoxHeight(compactMetrics)).toBeCloseTo(16, 4);
+ expect(getLineBoxHeight(expandedMetrics)).toBeCloseTo(40, 4);
+ });
+
+ it('treats exact spacing as the requested line box height even when glyphs are taller', () => {
+ const metrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 10, SpacingRule.EXACT, BooleanNumber.FALSE, true);
+
+ expect(getLineBoxHeight(metrics)).toBeCloseTo(10, 4);
+ expect(metrics.contentHeight).toBeGreaterThan(getLineBoxHeight(metrics));
+ });
+
+ it('keeps the legacy line-height behavior for embedded sheet documents', () => {
+ const metrics = getLineHeightMetrics(16, 0, 15.6, GridType.LINES, 1.5, SpacingRule.AUTO, BooleanNumber.TRUE, false);
+
+ expect(getLineBoxHeight(metrics)).toBeCloseTo(23.4, 4);
+ });
});
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/linebreaking.spec.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/linebreaking.spec.ts
index fb2f546ab9..ee18d24405 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/linebreaking.spec.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/linebreaking.spec.ts
@@ -360,8 +360,9 @@ describe('linebreaking', () => {
expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.paragraphStyle).toEqual({
spaceAbove: { v: 0 },
lineSpacing: 1.5,
- spaceBelow: { v: 8 },
+ spaceBelow: { v: 12 },
});
+ expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.useWordStyleLineHeight).toBe(true);
expect(paragraphStyle).toEqual({});
});
@@ -408,6 +409,7 @@ describe('linebreaking', () => {
);
expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.paragraphStyle).toEqual({});
+ expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.useWordStyleLineHeight).toBe(false);
expect(paragraphStyle).toEqual({});
});
@@ -452,6 +454,7 @@ describe('linebreaking', () => {
);
expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.paragraphStyle).toEqual({});
+ expect(ctx.paragraphConfigCache.get('segment-1')?.get(3)?.useWordStyleLineHeight).toBe(false);
expect(paragraphStyle).toEqual({});
});
});
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
index 3ac183bdf4..239eccb574 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
@@ -338,14 +338,15 @@ function _divideOperator(
getLineHeightConfig(sectionBreakConfig, paragraphConfig);
const { boundingBoxAscent, boundingBoxDescent } = maxBox;
const spanLineHeight = boundingBoxAscent + boundingBoxDescent;
- const { contentHeight } = __getLineHeight(
+ const { contentHeight } = getLineHeightMetrics(
spanLineHeight,
paragraphLineGapDefault,
linePitch,
gridType,
lineSpacing,
spacingRule,
- snapToGrid
+ snapToGrid,
+ paragraphConfig.useWordStyleLineHeight
);
if (currentLine.contentHeight < contentHeight) {
@@ -480,14 +481,15 @@ function _lineOperator(
paragraphConfig
);
- const { paddingTop, paddingBottom, contentHeight, lineSpacingApply } = __getLineHeight(
+ const { paddingTop, paddingBottom, contentHeight, lineSpacingApply } = getLineHeightMetrics(
glyphLineHeight,
paragraphLineGapDefault,
linePitch,
gridType,
lineSpacing,
spacingRule,
- snapToGrid
+ snapToGrid,
+ paragraphConfig.useWordStyleLineHeight
);
const { marginTop, spaceBelowApply } = __getParagraphSpace(
@@ -1131,58 +1133,97 @@ function __getParagraphSpace(
};
}
-function __getLineHeight(
+export function getLineHeightMetrics(
glyphLineHeight: number,
paragraphLineGapDefault: number,
linePitch: number,
gridType: GridType,
lineSpacing: number,
spacingRule: SpacingRule,
- snapToGrid: BooleanNumber
+ snapToGrid: BooleanNumber,
+ useWordStyleLineHeight = true
) {
- let paddingTop = paragraphLineGapDefault;
- let paddingBottom = paragraphLineGapDefault;
+ if (!useWordStyleLineHeight) {
+ let paddingTop = paragraphLineGapDefault;
+ let paddingBottom = paragraphLineGapDefault;
+
+ if (gridType === GridType.DEFAULT || snapToGrid === BooleanNumber.FALSE) {
+ if (spacingRule === SpacingRule.AUTO) {
+ return {
+ paddingTop,
+ paddingBottom,
+ contentHeight: lineSpacing * glyphLineHeight,
+ lineSpacingApply: glyphLineHeight,
+ };
+ }
- if (gridType === GridType.DEFAULT || snapToGrid === BooleanNumber.FALSE) {
- // Scenario where doc grid is not applied, layout is determined by character height and width
- if (spacingRule === SpacingRule.AUTO) {
- // In auto mode, lineSpacing represents the number of lines
return {
paddingTop,
paddingBottom,
- contentHeight: lineSpacing * glyphLineHeight,
- lineSpacingApply: glyphLineHeight,
+ contentHeight: Math.max(lineSpacing, glyphLineHeight),
+ lineSpacingApply: lineSpacing,
};
}
+ let lineSpacingApply = 0;
+ if (spacingRule === SpacingRule.AUTO) {
+ lineSpacingApply = lineSpacing * linePitch;
+ } else {
+ lineSpacingApply = lineSpacing;
+ }
+
+ if (glyphLineHeight + paragraphLineGapDefault * 2 < lineSpacingApply) {
+ paddingTop = paddingBottom = (lineSpacingApply - glyphLineHeight) / 2;
+ } else {
+ lineSpacingApply = glyphLineHeight;
+ }
+
return {
paddingTop,
paddingBottom,
- contentHeight: Math.max(lineSpacing, glyphLineHeight),
- lineSpacingApply: lineSpacing,
+ contentHeight: glyphLineHeight,
+ lineSpacingApply,
};
}
- // open xml $17.18.14 ST_DocGrid (Document Grid Types)
- let lineSpacingApply = 0;
+ const usesDocumentGrid =
+ spacingRule === SpacingRule.AUTO
+ && snapToGrid === BooleanNumber.TRUE
+ && gridType !== GridType.DEFAULT;
+
if (spacingRule === SpacingRule.AUTO) {
- // In auto mode, lineSpacing represents the number of lines
- lineSpacingApply = lineSpacing * linePitch;
- } else {
- lineSpacingApply = lineSpacing;
+ const lineSpacingApply = usesDocumentGrid ? lineSpacing * linePitch : lineSpacing * glyphLineHeight;
+ const padding = (lineSpacingApply - glyphLineHeight) / 2;
+
+ return {
+ paddingTop: padding,
+ paddingBottom: padding,
+ contentHeight: glyphLineHeight,
+ lineSpacingApply,
+ };
}
- if (glyphLineHeight + paragraphLineGapDefault * 2 < lineSpacingApply) {
- paddingTop = paddingBottom = (lineSpacingApply - glyphLineHeight) / 2;
- } else {
- lineSpacingApply = glyphLineHeight;
+ if (spacingRule === SpacingRule.AT_LEAST) {
+ const lineSpacingApply = Math.max(lineSpacing, glyphLineHeight);
+ const padding = (lineSpacingApply - glyphLineHeight) / 2;
+
+ return {
+ paddingTop: padding,
+ paddingBottom: padding,
+ contentHeight: glyphLineHeight,
+ lineSpacingApply,
+ };
}
+ // EXACT follows the requested line box height even when it is smaller than the glyph box.
+ // Negative padding lets subsequent lines advance by the exact value, which is closer to Word.
+ const exactPadding = (lineSpacing - glyphLineHeight) / 2;
+
return {
- paddingTop,
- paddingBottom,
+ paddingTop: exactPadding,
+ paddingBottom: exactPadding,
contentHeight: glyphLineHeight,
- lineSpacingApply,
+ lineSpacingApply: lineSpacing,
};
}
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/linebreaking.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/linebreaking.ts
index b42c9a5109..e5f022f28b 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/linebreaking.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/linebreaking.ts
@@ -268,6 +268,7 @@ export function lineBreaking(
const paragraph = viewModel.getParagraph(endIndex) || { startIndex: 0 };
const { paragraphStyle = {}, bullet } = paragraph;
+ const useWordStyleLineHeight = _shouldApplyDocumentDefaultParagraphStyle(viewModel);
const { skeHeaders, skeFooters, skeListLevel, drawingAnchor } = skeletonResourceReference;
@@ -287,8 +288,9 @@ export function lineBreaking(
viewModel.getBody?.() ?? null,
paragraph,
paragraphStyle,
- _shouldApplyDocumentDefaultParagraphStyle(viewModel)
+ useWordStyleLineHeight
),
+ useWordStyleLineHeight,
paragraphNonInlineSkeDrawings,
paragraphInlineSkeDrawings,
skeTablesInParagraph: tableSkeleton
diff --git a/packages/engine-render/src/components/docs/layout/tools.ts b/packages/engine-render/src/components/docs/layout/tools.ts
index 89c945bee4..d3f7d5c8ee 100644
--- a/packages/engine-render/src/components/docs/layout/tools.ts
+++ b/packages/engine-render/src/components/docs/layout/tools.ts
@@ -290,17 +290,26 @@ export function validationGrid(gridType = GridType.LINES, snapToGrid = BooleanNu
}
export function getLineHeightConfig(sectionBreakConfig: ISectionBreakConfig, paragraphConfig: IParagraphConfig) {
- const { paragraphStyle = {} } = paragraphConfig;
+ const { paragraphStyle = {}, useWordStyleLineHeight = false } = paragraphConfig;
const { linePitch = 15.6, gridType = GridType.LINES, paragraphLineGapDefault = 0 } = sectionBreakConfig;
- const { lineSpacing = 0, spacingRule = SpacingRule.AUTO, snapToGrid = BooleanNumber.TRUE } = paragraphStyle;
+ const defaultSnapToGrid = useWordStyleLineHeight ? BooleanNumber.FALSE : BooleanNumber.TRUE;
+ const { lineSpacing = 0, spacingRule = SpacingRule.AUTO, snapToGrid = defaultSnapToGrid } = paragraphStyle;
- // The default line spacing in Word is 1. Here, if the lines layout is used, the default line spacing is set to 1.
+ // Flavored docs use Word-style single spacing by default.
+ // Embedded sheet/slides documents keep the legacy grid-based fallback.
let lineSpacingApply = lineSpacing;
- if ((gridType === GridType.LINES || gridType === GridType.LINES_AND_CHARS) && lineSpacing === 0 && spacingRule === SpacingRule.AUTO) {
+ if (useWordStyleLineHeight && lineSpacing === 0 && spacingRule === SpacingRule.AUTO) {
+ lineSpacingApply = 1;
+ } else if (
+ !useWordStyleLineHeight &&
+ (gridType === GridType.LINES || gridType === GridType.LINES_AND_CHARS) &&
+ lineSpacing === 0 &&
+ spacingRule === SpacingRule.AUTO
+ ) {
lineSpacingApply = 1;
}
- return { paragraphLineGapDefault, linePitch, gridType, lineSpacing: lineSpacingApply, spacingRule, snapToGrid };
+ return { paragraphLineGapDefault, linePitch, gridType, lineSpacing: lineSpacingApply, spacingRule, snapToGrid, useWordStyleLineHeight };
}
export function getCharSpaceConfig(sectionBreakConfig: ISectionBreakConfig, paragraphConfig: IParagraphConfig) {