Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): header footer setting not focus #3698

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import type { ICommand, IDocumentBody, IMutationInfo, JSONXActions } from '@univerjs/core';
import { BooleanNumber, CommandType, ICommandService, IUniverInstanceService, JSONX, Tools } from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { BooleanNumber, CommandType, ICommandService, IUniverInstanceService, JSONX, Tools } from '@univerjs/core';
import { DocSkeletonManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService, type ITextRangeWithStyle } from '@univerjs/engine-render';
import { HeaderFooterType } from '../../controllers/doc-header-footer.controller';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,44 @@
* limitations under the License.
*/

import type { IDocumentStyle } from '@univerjs/core';
import { BooleanNumber, ICommandService, IUniverInstanceService, LocaleService, Tools, useDependency } from '@univerjs/core';
import { Button, Checkbox, InputNumber } from '@univerjs/design';
import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
import { ILayoutService } from '@univerjs/ui';
import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { CoreHeaderFooterCommandId, type IHeaderFooterProps } from '../../../commands/commands/doc-header-footer.command';
import { DocSelectionRenderService } from '../../../services/selection/doc-selection-render.service';
import styles from './index.module.less';

function getSegmentId(documentStyle: IDocumentStyle, editArea: DocumentEditArea, pageIndex: number): string {
const { useFirstPageHeaderFooter, evenAndOddHeaders, defaultHeaderId, defaultFooterId, firstPageHeaderId, firstPageFooterId, evenPageHeaderId, evenPageFooterId } = documentStyle;

if (editArea === DocumentEditArea.HEADER) {
if (useFirstPageHeaderFooter === BooleanNumber.TRUE) {
if (pageIndex === 0) {
return firstPageHeaderId!;
} else {
return evenAndOddHeaders === BooleanNumber.TRUE && pageIndex % 2 === 1 ? evenPageHeaderId! : defaultHeaderId!;
}
} else {
return evenAndOddHeaders === BooleanNumber.TRUE && pageIndex % 2 === 1 ? evenPageHeaderId! : defaultHeaderId!;
}
} else {
if (useFirstPageHeaderFooter === BooleanNumber.TRUE) {
if (pageIndex === 0) {
return firstPageFooterId!;
} else {
return evenAndOddHeaders === BooleanNumber.TRUE && pageIndex % 2 === 1 ? evenPageFooterId! : defaultFooterId!;
}
} else {
return evenAndOddHeaders === BooleanNumber.TRUE && pageIndex % 2 === 1 ? evenPageFooterId! : defaultFooterId!;
}
}
}

export interface IDocHeaderFooterOptionsProps {
unitId: string;
}
Expand All @@ -34,6 +62,7 @@ export const DocHeaderFooterOptions = (props: IDocHeaderFooterOptionsProps) => {
const renderManagerService = useDependency(IRenderManagerService);
const commandService = useDependency(ICommandService);
const docSelectionManagerService = useDependency(DocSelectionManagerService);
const layoutService = useDependency(ILayoutService);
const { unitId } = props;

const docSelectionRenderService = renderManagerService.getRenderById(unitId)!.with(DocSelectionRenderService)!;
Expand Down Expand Up @@ -100,13 +129,31 @@ export const DocHeaderFooterOptions = (props: IDocHeaderFooterOptionsProps) => {
},
});
} else {
const segmentPageIndex = docSelectionRenderService.getSegmentPage();
const prevSegmentId = docSelectionRenderService.getSegment();

const needFocusSegmentId = getSegmentId(
{
...documentStyle,
[type]: val ? BooleanNumber.TRUE : BooleanNumber.FALSE,
},
editArea,
segmentPageIndex
);

if (needFocusSegmentId && needFocusSegmentId !== prevSegmentId) {
docSelectionRenderService.setSegment(needFocusSegmentId);
}

commandService.executeCommand(CoreHeaderFooterCommandId, {
unitId,
headerFooterProps: {
[type]: val ? BooleanNumber.TRUE : BooleanNumber.FALSE,
},
});
}

layoutService.focus();
};

const handleMarginChange = async (val: number, type: 'marginHeader' | 'marginFooter') => {
Expand Down
Loading