Skip to content

Commit

Permalink
refactor: refactor doc view model cache (#4130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs authored Nov 23, 2024
1 parent e1d7713 commit 2c3efff
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 436 deletions.
4 changes: 2 additions & 2 deletions examples/src/docs/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { UniverDocsThreadCommentUIPlugin } from '@univerjs/docs-thread-comment-u
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_DOCUMENT_DATA_SIMPLE } from '@univerjs/mockdata';
import { DEFAULT_DOCUMENT_DATA_CN } from '@univerjs/mockdata';
import { UniverUIPlugin } from '@univerjs/ui';
import { enUS, faIR, ruRU, zhCN } from '../locales';

Expand Down Expand Up @@ -69,7 +69,7 @@ univer.registerPlugin(UniverDocsHyperLinkUIPlugin);
univer.registerPlugin(UniverDocsMentionUIPlugin);

if (!IS_E2E) {
univer.createUnit(UniverInstanceType.UNIVER_DOC, DEFAULT_DOCUMENT_DATA_SIMPLE);
univer.createUnit(UniverInstanceType.UNIVER_DOC, DEFAULT_DOCUMENT_DATA_CN);
}

// use for console test
Expand Down
14 changes: 7 additions & 7 deletions packages/docs-ui/src/commands/commands/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function getInsertRowActionsParams(rangeInfo: IRangeInfo, position: INSER
let rowIndex = 0;

// TODO: handle nested tables
for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const table = children[0];
Expand Down Expand Up @@ -324,7 +324,7 @@ export function getInsertColumnActionsParams(rangeInfo: IRangeInfo, position: IN
let table: Nullable<DataStreamTreeNode> = null;
let columnIndex = -1;

for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const tableNode = children[0];
Expand Down Expand Up @@ -418,7 +418,7 @@ export function getDeleteRowsActionsParams(rangeInfo: IRangeInfo, viewModel: Doc
let cursor = -1;
let selectWholeTable = false;

for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const table = children[0];
Expand Down Expand Up @@ -494,7 +494,7 @@ export function getDeleteColumnsActionParams(rangeInfo: IRangeInfo, viewModel: D
let startColumnIndex = -1;
let endColumnIndex = -1;

for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const tableNode = children[0];
Expand Down Expand Up @@ -573,7 +573,7 @@ export function getDeleteTableActionParams(rangeInfo: IRangeInfo, viewModel: Doc
let len = 0;
let cursor = -1;

for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const table = children[0];
Expand Down Expand Up @@ -624,7 +624,7 @@ export function getDeleteRowContentActionParams(rangeInfo: IRangeInfo, viewModel
let startColumnIndex = -1;
let endColumnIndex = -1;

for (const section of vm.children) {
for (const section of vm.getChildren()) {
for (const paragraph of section.children) {
const { children } = paragraph;
const tableNode = children[0];
Expand Down Expand Up @@ -706,7 +706,7 @@ export function getCellOffsets(viewModel: DocumentViewModel, range: ITextRangeWi

let targetTable = null;

for (const section of viewModel.children) {
for (const section of viewModel.getChildren()) {
for (const paragraph of section.children) {
const table = paragraph.children[0];
if (table) {
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/services/selection/selection-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function getRangeListFromSelection(
let end = endOffset;

// TODO: @JOCS handle in header and footer.
for (const section of viewModel.children) {
for (const section of viewModel.getChildren()) {
for (const paragraph of section.children) {
const { startIndex, endIndex, children } = paragraph;
const table = children[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function lineAdjustment(
sectionBreakConfig: ISectionBreakConfig
) {
const { endIndex } = paragraphNode;
const paragraph = viewModel.getParagraph(endIndex, true) || { startIndex: 0 };
const paragraph = viewModel.getParagraph(endIndex) || { startIndex: 0 };

lineIterator(pages, (line) => {
// Only need to adjust the current paragraph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function lineBreaking(
const { endIndex, blocks = [], children } = paragraphNode;
const { segmentId } = curPage;

const paragraph = viewModel.getParagraph(endIndex, true) || { startIndex: 0 };
const paragraph = viewModel.getParagraph(endIndex) || { startIndex: 0 };

const { paragraphStyle = {}, bullet } = paragraph;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function shaping(
const shapedTextList: IShapedText[] = [];
let breaker = new LineBreaker(content);
const { endIndex } = paragraphNode;
const paragraph = viewModel.getParagraph(endIndex, true) || { startIndex: 0 };
const paragraph = viewModel.getParagraph(endIndex) || { startIndex: 0 };
const { paragraphStyle = {} } = paragraph;
const { snapToGrid = BooleanNumber.TRUE } = paragraphStyle;
let last = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function createTableSkeleton(
sectionBreakConfig: ISectionBreakConfig
): IDocumentSkeletonTable {
const { startIndex, endIndex, children: rowNodes } = tableNode;
const table = viewModel.getTable(startIndex);
const table = viewModel.getTableByStartIndex(startIndex)?.tableSource;
if (table == null) {
throw new Error('Table not found');
}
Expand Down Expand Up @@ -166,7 +166,7 @@ export function createTableSkeletons(
const pageContentHeight = pageHeight - marginTop - marginBottom;

const { startIndex, endIndex, children: rowNodes } = tableNode;
const table = viewModel.getTable(startIndex);
const table = viewModel.getTableByStartIndex(startIndex)?.tableSource;
if (table == null) {
throw new Error('Table not found when creating table skeletons');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ export class DocumentSkeleton extends Skeleton {
private _prepareLayoutContext(): ILayoutContext {
const viewModel = this.getViewModel();
const dataModel = viewModel.getDataModel();
const { headerTreeMap, footerTreeMap } = viewModel;
const { headerTreeMap, footerTreeMap } = viewModel.getHeaderFooterTreeMap();
const { documentStyle, drawings, lists: customLists = {} } = dataModel;
const lists = {
...PRESET_LIST_TYPE,
Expand Down Expand Up @@ -1089,8 +1089,6 @@ export class DocumentSkeleton extends Skeleton {

const allSkeletonPages = skeleton.pages;

viewModel.resetCache();

let startSectionIndex = 0;

const layoutAnchor = ctx.layoutStartPointer[''];
Expand All @@ -1099,8 +1097,8 @@ export class DocumentSkeleton extends Skeleton {
ctx.layoutStartPointer[''] = null;

if (layoutAnchor != null) {
for (let sectionIndex = 0; sectionIndex < viewModel.children.length; sectionIndex++) {
const sectionNode = viewModel.children[sectionIndex];
for (let sectionIndex = 0; sectionIndex < viewModel.getChildren().length; sectionIndex++) {
const sectionNode = viewModel.getChildren()[sectionIndex];
const { endIndex, startIndex } = sectionNode;
if (layoutAnchor >= startIndex && layoutAnchor <= endIndex) {
startSectionIndex = sectionIndex;
Expand All @@ -1110,8 +1108,8 @@ export class DocumentSkeleton extends Skeleton {
}

// Loop the sections with the start section index.
for (let i = startSectionIndex, len = viewModel.children.length; i < len; i++) {
const sectionNode = viewModel.children[i];
for (let i = startSectionIndex, len = viewModel.getChildren().length; i < len; i++) {
const sectionNode = viewModel.getChildren()[i];
const sectionBreakConfig = prepareSectionBreakConfig(ctx, i);
const { sectionType, columnProperties, columnSeparatorType, sectionTypeNext, pageNumberStart = 1 } = sectionBreakConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function _createSkeletonHeaderFooter(
const page = dealWithSection(
ctx,
headerOrFooterViewModel,
headerOrFooterViewModel.children[0],
headerOrFooterViewModel.getChildren()[0],
areaPage,
headerFooterConfig,
layoutAnchor
Expand All @@ -248,7 +248,6 @@ function _createSkeletonHeaderFooter(
if (ctx.isDirty && count < 10) {
count++;
resetContext(ctx);
headerOrFooterViewModel.resetCache();

return _createSkeletonHeaderFooter(
ctx,
Expand Down
4 changes: 2 additions & 2 deletions packages/engine-render/src/components/docs/layout/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ const DEFAULT_MODERN_SECTION_BREAK: Partial<ISectionBreak> = {

export function prepareSectionBreakConfig(ctx: ILayoutContext, nodeIndex: number) {
const { viewModel, dataModel, docsConfig } = ctx;
const sectionNode = viewModel.children[nodeIndex];
const sectionNode = viewModel.getChildren()[nodeIndex];
let { documentStyle } = dataModel;
const { documentFlavor } = documentStyle;
let sectionBreak = viewModel.getSectionBreak(sectionNode.endIndex) || DEFAULT_SECTION_BREAK;
Expand Down Expand Up @@ -1081,7 +1081,7 @@ export function prepareSectionBreakConfig(ctx: ILayoutContext, nodeIndex: number
renderConfig = global_renderConfig,
} = sectionBreak;

const sectionNodeNext = viewModel.children[nodeIndex + 1];
const sectionNodeNext = viewModel.getChildren()[nodeIndex + 1];
const sectionTypeNext = viewModel.getSectionBreak(sectionNodeNext?.endIndex)?.sectionType;

const headerIds = { defaultHeaderId, evenPageHeaderId, firstPageHeaderId };
Expand Down
Loading

0 comments on commit 2c3efff

Please sign in to comment.