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: fix SelectionManager to make the code running

Wenzhao Hua2822ff2a987
diff --git a/packages/base-render/src/Component/Docs/DocSkeleton.ts b/packages/base-render/src/Component/Docs/DocSkeleton.ts
index f676ab9164..1d50685b4b 100644
--- a/packages/base-render/src/Component/Docs/DocSkeleton.ts
+++ b/packages/base-render/src/Component/Docs/DocSkeleton.ts
@@ -1,339 +1,339 @@
-import {
- ColumnSeparatorType,
- ContextBase,
- DocumentModelOrSimple,
- GridType,
- HorizontalAlign,
- ISectionBreak,
- ISectionColumnProperties,
- Observable,
- PageOrientType,
- SectionType,
- VerticalAlign,
- WrapStrategy,
-} from '@univerjs/core';
-import { getLastPage, updateBlockIndex } from './Common/Tools';
-
-import { createSkeletonPage } from './Common/Page';
-
-import { createSkeletonSection } from './Common/Section';
-
-import { dealWithSections } from './Block/Section';
-
-import { IDocumentSkeletonCached, IDocumentSkeletonPage, ISkeletonResourceReference } from '../../Basics/IDocumentSkeletonCached';
-import { IDocsConfig, ISectionBreakConfig } from '../../Basics/Interfaces';
-import { Skeleton } from '../Skeleton';
-import { IBoundRect } from '../../Basics/Vector2';
-
-const DEFAULT_SECTION_BREAK: ISectionBreak = {
- columnProperties: [],
- columnSeparatorType: ColumnSeparatorType.NONE,
- sectionType: SectionType.SECTION_TYPE_UNSPECIFIED,
- startIndex: 0,
-};
-
-export enum DocumentSkeletonState {
- PENDING = 'pending',
- CALCULATING = 'calculating',
- READY = 'ready',
- INVALID = 'invalid',
-}
-
-export class DocumentSkeleton extends Skeleton {
- onRecalculateChangeObservable = new Observable<IDocumentSkeletonCached>();
-
- private _docModel: DocumentModelOrSimple;
-
- private _skeletonData: IDocumentSkeletonCached;
-
- private _renderedBlockIdMap = new Map<string, boolean>();
-
- constructor(docModel: DocumentModelOrSimple, context: ContextBase) {
- super(context);
- this._docModel = docModel;
- }
-
- static create(docModel: DocumentModelOrSimple, context: ContextBase) {
- return new DocumentSkeleton(docModel, context);
- }
-
- getModel() {
- return this._docModel;
- }
-
- calculate(bounds?: IBoundRect) {
- if (!this.dirty) {
- return;
- }
- this._skeletonData = this._createSkeleton(bounds);
-
- this.onRecalculateChangeObservable.notifyObservers(this._skeletonData);
- }
-
- getSkeletonData() {
- return this._skeletonData;
- }
-
- getPageSize() {
- return this._docModel.documentStyle.pageSize;
- }
-
- // updateDocumentDataPageSize(width?: number, height?: number) {
- // const documentStyle = this._docModel.documentStyle;
- // if (!documentStyle.pageSize) {
- // width = width ?? Infinity;
- // height = height ?? Infinity;
- // documentStyle.pageSize = {
- // width,
- // height,
- // };
- // return;
- // }
-
- // if (width !== undefined) {
- // documentStyle.pageSize.width = width;
- // }
-
- // if (height !== undefined) {
- // documentStyle.pageSize.height = height;
- // }
- // }
-
- /**
- * \v COLUMN_BREAK 换列
- * \f PAGE_BREAK 换页
- * \0 DOCS_END 文档结尾
- * \t TAB 制表符
- *
- * Needs to be changed:
- * \r PARAGRAPH 段落
- * \n SECTION_BREAK 章节
- *
- * \b customBlock 图片 mention等不参与文档流的场景
- *
- * 表格
- * \x1A table start 表格开始
- * \x1B table row start 表格开始
- * \x1C table cell start 表格开始
- * \x1D table cell end 表格开始
- * \x1E table row end 表格开始
- * \x1F table end 表格结束
- *
- * 文档流内的特殊范围:超链接,field,structured document tags, bookmark,comment
- * \x1F customRange start 自定义范围开始
- * \x1E customRange end 自定义范围结束
- *
- * 按照SectionBreak分割文档,进行布局计算
- * @returns view model: skeleton
- */
- // eslint-disable-next-line max-lines-per-function
- private _createSkeleton(bounds?: IBoundRect) {
- // 每一个布局
- const DEFAULT_PAGE_SIZE = { width: Infinity, height: Infinity };
- const { documentStyle, headerTreeMap, footerTreeMap, lists, drawings } = this._docModel;
- const {
- pageNumberStart: global_pageNumberStart = 1, // pageNumberStart
- pageSize: global_pageSize = DEFAULT_PAGE_SIZE,
- pageOrient: global_pageOrient = PageOrientType.PORTRAIT,
- defaultHeaderId: global_defaultHeaderId,
- defaultFooterId: global_defaultFooterId,
- evenPageHeaderId: global_evenPageHeaderId,
- evenPageFooterId: global_evenPageFooterId,
- firstPageHeaderId: global_firstPageHeaderId,
- firstPageFooterId: global_firstPageFooterId,
- useFirstPageHeaderFooter: global_useFirstPageHeaderFooter,
- useEvenPageHeaderFooter: global_useEvenPageHeaderFooter,
-
- marginTop: global_marginTop = 0,
- marginBottom: global_marginBottom = 0,
- marginRight: global_marginRight = 0,
- marginLeft: global_marginLeft = 0,
- marginHeader: global_marginHeader = 0,
- marginFooter: global_marginFooter = 0,
-
- charSpace = 0, // charSpace
- linePitch = 15.6, // linePitch pt
- gridType = GridType.LINES, // gridType
- paragraphLineGapDefault = 0,
- defaultTabStop = 10.5,
- textStyle = {
- fs: 14,
- },
- renderConfig: global_renderConfig = {
- horizontalAlign: HorizontalAlign.UNSPECIFIED,
- verticalAlign: VerticalAlign.UNSPECIFIED,
- centerAngle: 0,
- vertexAngle: 0,
- wrapStrategy: WrapStrategy.UNSPECIFIED,
- },
- } = documentStyle;
- const skeleton = this.__getNullSke();
-
- const fontLocale = this.getFontLocale();
-
- const docsConfig: IDocsConfig = {
- headerTreeMap,
- footerTreeMap,
- lists,
- drawings,
-
- charSpace,
- linePitch,
- gridType,
- fontLocale,
- paragraphLineGapDefault,
- defaultTabStop,
- documentTextStyle: textStyle,
- };
-
- const { skeHeaders, skeFooters, skeListLevel, drawingAnchor } = skeleton;
-
- const skeletonResourceReference: ISkeletonResourceReference = {
- skeHeaders,
- skeFooters,
- skeListLevel,
- drawingAnchor,
- };
-
- const allSkeletonPages: IDocumentSkeletonPage[] = [];
-
- skeleton.pages = allSkeletonPages;
-
- const bodyModel = this._docModel.bodyModel;
- bodyModel.resetCache();
- for (let i = 0, len = bodyModel.children.length; i < len; i++) {
- const sectionNode = bodyModel.children[i];
- const sectionBreak = bodyModel.getSectionBreak(sectionNode.endIndex) || DEFAULT_SECTION_BREAK;
- const {
- pageNumberStart = global_pageNumberStart,
- pageSize = global_pageSize,
- pageOrient = global_pageOrient,
- marginTop = global_marginTop,
- marginBottom = global_marginBottom,
- marginRight = global_marginRight,
- marginLeft = global_marginLeft,
- marginHeader = global_marginHeader,
- marginFooter = global_marginFooter,
-
- defaultHeaderId = global_defaultHeaderId,
- defaultFooterId = global_defaultFooterId,
- evenPageHeaderId = global_evenPageHeaderId,
- evenPageFooterId = global_evenPageFooterId,
- firstPageHeaderId = global_firstPageHeaderId,
- firstPageFooterId = global_firstPageFooterId,
- useFirstPageHeaderFooter = global_useFirstPageHeaderFooter,
- useEvenPageHeaderFooter = global_useEvenPageHeaderFooter,
-
- columnProperties = [],
- columnSeparatorType = ColumnSeparatorType.NONE,
- contentDirection,
- sectionType,
- textDirection,
- renderConfig = global_renderConfig,
- } = sectionBreak;
-
- const sectionNodeNext = bodyModel.children[i + 1];
- const sectionTypeNext = bodyModel.getSectionBreak(sectionNodeNext?.endIndex)?.sectionType;
-
- const headerIds = { defaultHeaderId, evenPageHeaderId, firstPageHeaderId };
- const footerIds = { defaultFooterId, evenPageFooterId, firstPageFooterId };
-
- if (pageSize.width === null) {
- pageSize.width = Infinity;
- }
-
- if (pageSize.height === null) {
- pageSize.height = Infinity;
- }
-
- const sectionBreakConfig: ISectionBreakConfig = {
- pageNumberStart,
- pageSize,
- pageOrient,
- marginTop,
- marginBottom,
- marginRight,
- marginLeft,
- marginHeader,
- marginFooter,
-
- headerIds,
- footerIds,
-
- useFirstPageHeaderFooter,
- useEvenPageHeaderFooter,
-
- columnProperties,
- columnSeparatorType,
- contentDirection,
- sectionType,
- sectionTypeNext,
- textDirection,
- renderConfig,
-
- ...docsConfig,
- };
-
- let curSkeletonPage: IDocumentSkeletonPage = getLastPage(allSkeletonPages);
- let isContinuous = false;
- if (sectionType === SectionType.CONTINUOUS) {
- updateBlockIndex(allSkeletonPages);
- this.__addNewSectionByContinuous(curSkeletonPage, columnProperties, columnSeparatorType);
- isContinuous = true;
- } else {
- curSkeletonPage = createSkeletonPage(sectionBreakConfig, skeletonResourceReference, curSkeletonPage?.pageNumber);
- }
- // 计算页内布局,block结构
- const context = this.getContext();
- const blockInfo = dealWithSections(bodyModel, sectionNode, curSkeletonPage, sectionBreakConfig, skeletonResourceReference, this._renderedBlockIdMap, context);
-
- // todo: 当本节有多个列,且下一节为连续节类型的时候,需要按照列数分割,重新计算lines
- if (sectionTypeNext === SectionType.CONTINUOUS && columnProperties.length > 0) {
- // TODO
- }
-
- const { pages, renderedBlockIdMap } = blockInfo;
-
- // renderedBlockIdMap.forEach((value, blockId) => {
- // this._renderedBlockIdMap.set(blockId, value);
- // });
-
- if (isContinuous) {
- pages.splice(0, 1);
- }
-
- allSkeletonPages.push(...pages);
- }
-
- // 计算页和节的位置信息
- updateBlockIndex(allSkeletonPages);
-
- return skeleton;
- }
-
- // 一页存在多个section的情况,仅在SectionType.CONTINUOUS的情况下出现
- private __addNewSectionByContinuous(curSkeletonPage: IDocumentSkeletonPage, columnProperties: ISectionColumnProperties[], columnSeparatorType: ColumnSeparatorType) {
- const sections = curSkeletonPage.sections;
- const lastSection = sections[sections.length - 1];
- const { pageWidth, pageHeight, marginTop: curPageMT, marginBottom: curPageMB, marginLeft: curPageML, marginRight: curPageMR } = curSkeletonPage;
- const pageContentWidth = pageWidth - curPageML - curPageMR;
- const pageContentHeight = pageHeight - curPageMT - curPageMB;
- const lastSectionBottom = (lastSection?.top || 0) + (lastSection?.height || 0);
- const newSection = createSkeletonSection(columnProperties, columnSeparatorType, lastSectionBottom, 0, pageContentWidth, pageContentHeight - lastSectionBottom);
- newSection.parent = curSkeletonPage;
- sections.push(newSection);
- }
-
- private __getNullSke(): IDocumentSkeletonCached {
- return {
- pages: [],
- left: 0,
- top: 0,
- st: 0,
- skeHeaders: new Map(),
- skeFooters: new Map(),
- skeListLevel: new Map(),
- drawingAnchor: new Map(),
- };
- }
-}
+import {
+ ColumnSeparatorType,
+ DocumentModelOrSimple,
+ GridType,
+ HorizontalAlign,
+ ISectionBreak,
+ ISectionColumnProperties,
+ LocaleService,
+ Observable,
+ PageOrientType,
+ SectionType,
+ VerticalAlign,
+ WrapStrategy,
+} from '@univerjs/core';
+import { getLastPage, updateBlockIndex } from './Common/Tools';
+
+import { createSkeletonPage } from './Common/Page';
+
+import { createSkeletonSection } from './Common/Section';
+
+import { dealWithSections } from './Block/Section';
+
+import { IDocumentSkeletonCached, IDocumentSkeletonPage, ISkeletonResourceReference } from '../../Basics/IDocumentSkeletonCached';
+import { IDocsConfig, ISectionBreakConfig } from '../../Basics/Interfaces';
+import { Skeleton } from '../Skeleton';
+import { IBoundRect } from '../../Basics/Vector2';
+
+const DEFAULT_SECTION_BREAK: ISectionBreak = {
+ columnProperties: [],
+ columnSeparatorType: ColumnSeparatorType.NONE,
+ sectionType: SectionType.SECTION_TYPE_UNSPECIFIED,
+ startIndex: 0,
+};
+
+export enum DocumentSkeletonState {
+ PENDING = 'pending',
+ CALCULATING = 'calculating',
+ READY = 'ready',
+ INVALID = 'invalid',
+}
+
+export class DocumentSkeleton extends Skeleton {
+ onRecalculateChangeObservable = new Observable<IDocumentSkeletonCached>();
+
+ private _docModel: DocumentModelOrSimple;
+
+ private _skeletonData: IDocumentSkeletonCached;
+
+ private _renderedBlockIdMap = new Map<string, boolean>();
+
+ constructor(docModel: DocumentModelOrSimple, localeService: LocaleService) {
+ super(localeService);
+ this._docModel = docModel;
+ }
+
+ static create(docModel: DocumentModelOrSimple, localeService: LocaleService) {
+ return new DocumentSkeleton(docModel, localeService);
+ }
+
+ getModel() {
+ return this._docModel;
+ }
+
+ calculate(bounds?: IBoundRect) {
+ if (!this.dirty) {
+ return;
+ }
+ this._skeletonData = this._createSkeleton(bounds);
+
+ this.onRecalculateChangeObservable.notifyObservers(this._skeletonData);
+ }
+
+ getSkeletonData() {
+ return this._skeletonData;
+ }
+
+ getPageSize() {
+ return this._docModel.documentStyle.pageSize;
+ }
+
+ // updateDocumentDataPageSize(width?: number, height?: number) {
+ // const documentStyle = this._docModel.documentStyle;
+ // if (!documentStyle.pageSize) {
+ // width = width ?? Infinity;
+ // height = height ?? Infinity;
+ // documentStyle.pageSize = {
+ // width,
+ // height,
+ // };
+ // return;
+ // }
+
+ // if (width !== undefined) {
+ // documentStyle.pageSize.width = width;
+ // }
+
+ // if (height !== undefined) {
+ // documentStyle.pageSize.height = height;
+ // }
+ // }
+
+ /**
+ * \v COLUMN_BREAK 换列
+ * \f PAGE_BREAK 换页
+ * \0 DOCS_END 文档结尾
+ * \t TAB 制表符
+ *
+ * Needs to be changed:
+ * \r PARAGRAPH 段落
+ * \n SECTION_BREAK 章节
+ *
+ * \b customBlock 图片 mention等不参与文档流的场景
+ *
+ * 表格
+ * \x1A table start 表格开始
+ * \x1B table row start 表格开始
+ * \x1C table cell start 表格开始
+ * \x1D table cell end 表格开始
+ * \x1E table row end 表格开始
+ * \x1F table end 表格结束
+ *
+ * 文档流内的特殊范围:超链接,field,structured document tags, bookmark,comment
+ * \x1F customRange start 自定义范围开始
+ * \x1E customRange end 自定义范围结束
+ *
+ * 按照SectionBreak分割文档,进行布局计算
+ * @returns view model: skeleton
+ */
+ // eslint-disable-next-line max-lines-per-function
+ private _createSkeleton(bounds?: IBoundRect) {
+ // 每一个布局
+ const DEFAULT_PAGE_SIZE = { width: Infinity, height: Infinity };
+ const { documentStyle, headerTreeMap, footerTreeMap, lists, drawings } = this._docModel;
+ const {
+ pageNumberStart: global_pageNumberStart = 1, // pageNumberStart
+ pageSize: global_pageSize = DEFAULT_PAGE_SIZE,
+ pageOrient: global_pageOrient = PageOrientType.PORTRAIT,
+ defaultHeaderId: global_defaultHeaderId,
+ defaultFooterId: global_defaultFooterId,
+ evenPageHeaderId: global_evenPageHeaderId,
+ evenPageFooterId: global_evenPageFooterId,
+ firstPageHeaderId: global_firstPageHeaderId,
+ firstPageFooterId: global_firstPageFooterId,
+ useFirstPageHeaderFooter: global_useFirstPageHeaderFooter,
+ useEvenPageHeaderFooter: global_useEvenPageHeaderFooter,
+
+ marginTop: global_marginTop = 0,
+ marginBottom: global_marginBottom = 0,
+ marginRight: global_marginRight = 0,
+ marginLeft: global_marginLeft = 0,
+ marginHeader: global_marginHeader = 0,
+ marginFooter: global_marginFooter = 0,
+
+ charSpace = 0, // charSpace
+ linePitch = 15.6, // linePitch pt
+ gridType = GridType.LINES, // gridType
+ paragraphLineGapDefault = 0,
+ defaultTabStop = 10.5,
+ textStyle = {
+ fs: 14,
+ },
+ renderConfig: global_renderConfig = {
+ horizontalAlign: HorizontalAlign.UNSPECIFIED,
+ verticalAlign: VerticalAlign.UNSPECIFIED,
+ centerAngle: 0,
+ vertexAngle: 0,
+ wrapStrategy: WrapStrategy.UNSPECIFIED,
+ },
+ } = documentStyle;
+ const skeleton = this.__getNullSke();
+
+ const fontLocale = this.getFontLocale();
+
+ const docsConfig: IDocsConfig = {
+ headerTreeMap,
+ footerTreeMap,
+ lists,
+ drawings,
+
+ charSpace,
+ linePitch,
+ gridType,
+ fontLocale,
+ paragraphLineGapDefault,
+ defaultTabStop,
+ documentTextStyle: textStyle,
+ };
+
+ const { skeHeaders, skeFooters, skeListLevel, drawingAnchor } = skeleton;
+
+ const skeletonResourceReference: ISkeletonResourceReference = {
+ skeHeaders,
+ skeFooters,
+ skeListLevel,
+ drawingAnchor,
+ };
+
+ const allSkeletonPages: IDocumentSkeletonPage[] = [];
+
+ skeleton.pages = allSkeletonPages;
+
+ const bodyModel = this._docModel.bodyModel;
+ bodyModel.resetCache();
+ for (let i = 0, len = bodyModel.children.length; i < len; i++) {
+ const sectionNode = bodyModel.children[i];
+ const sectionBreak = bodyModel.getSectionBreak(sectionNode.endIndex) || DEFAULT_SECTION_BREAK;
+ const {
+ pageNumberStart = global_pageNumberStart,
+ pageSize = global_pageSize,
+ pageOrient = global_pageOrient,
+ marginTop = global_marginTop,
+ marginBottom = global_marginBottom,
+ marginRight = global_marginRight,
+ marginLeft = global_marginLeft,
+ marginHeader = global_marginHeader,
+ marginFooter = global_marginFooter,
+
+ defaultHeaderId = global_defaultHeaderId,
+ defaultFooterId = global_defaultFooterId,
+ evenPageHeaderId = global_evenPageHeaderId,
+ evenPageFooterId = global_evenPageFooterId,
+ firstPageHeaderId = global_firstPageHeaderId,
+ firstPageFooterId = global_firstPageFooterId,
+ useFirstPageHeaderFooter = global_useFirstPageHeaderFooter,
+ useEvenPageHeaderFooter = global_useEvenPageHeaderFooter,
+
+ columnProperties = [],
+ columnSeparatorType = ColumnSeparatorType.NONE,
+ contentDirection,
+ sectionType,
+ textDirection,
+ renderConfig = global_renderConfig,
+ } = sectionBreak;
+
+ const sectionNodeNext = bodyModel.children[i + 1];
+ const sectionTypeNext = bodyModel.getSectionBreak(sectionNodeNext?.endIndex)?.sectionType;
+
+ const headerIds = { defaultHeaderId, evenPageHeaderId, firstPageHeaderId };
+ const footerIds = { defaultFooterId, evenPageFooterId, firstPageFooterId };
+
+ if (pageSize.width === null) {
+ pageSize.width = Infinity;
+ }
+
+ if (pageSize.height === null) {
+ pageSize.height = Infinity;
+ }
+
+ const sectionBreakConfig: ISectionBreakConfig = {
+ pageNumberStart,
+ pageSize,
+ pageOrient,
+ marginTop,
+ marginBottom,
+ marginRight,
+ marginLeft,
+ marginHeader,
+ marginFooter,
+
+ headerIds,
+ footerIds,
+
+ useFirstPageHeaderFooter,
+ useEvenPageHeaderFooter,
+
+ columnProperties,
+ columnSeparatorType,
+ contentDirection,
+ sectionType,
+ sectionTypeNext,
+ textDirection,
+ renderConfig,
+
+ ...docsConfig,
+ };
+
+ let curSkeletonPage: IDocumentSkeletonPage = getLastPage(allSkeletonPages);
+ let isContinuous = false;
+ if (sectionType === SectionType.CONTINUOUS) {
+ updateBlockIndex(allSkeletonPages);
+ this.__addNewSectionByContinuous(curSkeletonPage, columnProperties, columnSeparatorType);
+ isContinuous = true;
+ } else {
+ curSkeletonPage = createSkeletonPage(sectionBreakConfig, skeletonResourceReference, curSkeletonPage?.pageNumber);
+ }
+
+ // 计算页内布局,block结构
+ const blockInfo = dealWithSections(bodyModel, sectionNode, curSkeletonPage, sectionBreakConfig, skeletonResourceReference, this._renderedBlockIdMap);
+
+ // todo: 当本节有多个列,且下一节为连续节类型的时候,需要按照列数分割,重新计算lines
+ if (sectionTypeNext === SectionType.CONTINUOUS && columnProperties.length > 0) {
+ // TODO
+ }
+
+ const { pages, renderedBlockIdMap } = blockInfo;
+
+ // renderedBlockIdMap.forEach((value, blockId) => {
+ // this._renderedBlockIdMap.set(blockId, value);
+ // });
+
+ if (isContinuous) {
+ pages.splice(0, 1);
+ }
+
+ allSkeletonPages.push(...pages);
+ }
+
+ // 计算页和节的位置信息
+ updateBlockIndex(allSkeletonPages);
+
+ return skeleton;
+ }
+
+ // 一页存在多个section的情况,仅在SectionType.CONTINUOUS的情况下出现
+ private __addNewSectionByContinuous(curSkeletonPage: IDocumentSkeletonPage, columnProperties: ISectionColumnProperties[], columnSeparatorType: ColumnSeparatorType) {
+ const sections = curSkeletonPage.sections;
+ const lastSection = sections[sections.length - 1];
+ const { pageWidth, pageHeight, marginTop: curPageMT, marginBottom: curPageMB, marginLeft: curPageML, marginRight: curPageMR } = curSkeletonPage;
+ const pageContentWidth = pageWidth - curPageML - curPageMR;
+ const pageContentHeight = pageHeight - curPageMT - curPageMB;
+ const lastSectionBottom = (lastSection?.top || 0) + (lastSection?.height || 0);
+ const newSection = createSkeletonSection(columnProperties, columnSeparatorType, lastSectionBottom, 0, pageContentWidth, pageContentHeight - lastSectionBottom);
+ newSection.parent = curSkeletonPage;
+ sections.push(newSection);
+ }
+
+ private __getNullSke(): IDocumentSkeletonCached {
+ return {
+ pages: [],
+ left: 0,
+ top: 0,
+ st: 0,
+ skeHeaders: new Map(),
+ skeFooters: new Map(),
+ skeListLevel: new Map(),
+ drawingAnchor: new Map(),
+ };
+ }
+}
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index eaa7e8f2bd..c67bbad642 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -1,6 +1,5 @@
import {
BooleanNumber,
- SheetContext,
HorizontalAlign,
IBorderStyleData,
ICellData,
@@ -29,6 +28,7 @@ import {
IDocumentRenderConfig,
DocumentModelSimple,
Tools,
+ LocaleService,
} from '@univerjs/core';
import { BORDER_TYPE, COLOR_BLACK_RGB } from '../../Basics/Const';
import { IStylesCache, BorderCache } from './Interfaces';
@@ -104,8 +104,9 @@ export class SpreadsheetSkeleton extends Skeleton {
private _showGridlines: BooleanNumber;
- constructor(private _config: IWorksheetConfig, private _cellData: ObjectMatrix<ICellData>, private _styles: Styles, context: SheetContext) {
- super(context);
+ constructor(private _config: IWorksheetConfig, private _cellData: ObjectMatrix<ICellData>, private _styles: Styles, _localeService: LocaleService) {
+ super(_localeService);
+
this.updateLayout();
this.updateDataMerge();
}
@@ -158,8 +159,8 @@ export class SpreadsheetSkeleton extends Skeleton {
return this._dataMergeCacheAll;
}
- static create(config: IWorksheetConfig, cellData: ObjectMatrix<ICellData>, styles: Styles, context: SheetContext) {
- return new SpreadsheetSkeleton(config, cellData, styles, context);
+ static create(config: IWorksheetConfig, cellData: ObjectMatrix<ICellData>, styles: Styles, LocaleService: LocaleService) {
+ return new SpreadsheetSkeleton(config, cellData, styles, LocaleService);
}
getWorksheetConfig() {
@@ -750,7 +751,7 @@ export class SpreadsheetSkeleton extends Skeleton {
angle = 90;
}
if (documentModel) {
- const documentSkeleton = DocumentSkeleton.create(documentModel, this.getContext());
+ const documentSkeleton = DocumentSkeleton.create(documentModel, this._localService);
if (angle === 0 || wrapStrategy !== WrapStrategy.WRAP) {
documentSkeleton.calculate();
// console.log(cell.v, documentSkeleton);
diff --git a/packages/base-render/src/Component/Skeleton.ts b/packages/base-render/src/Component/Skeleton.ts
index 110e561f81..f5c063fc3e 100644
--- a/packages/base-render/src/Component/Skeleton.ts
+++ b/packages/base-render/src/Component/Skeleton.ts
@@ -1,16 +1,14 @@
-import { ContextBase } from '@univerjs/core';
+import { LocaleService } from '@univerjs/core';
+import { Inject } from '@wendellhu/redi';
import { en, zh } from '../Locale';
import { IFontLocale } from '../Basics/Interfaces';
export class Skeleton {
- private _context: ContextBase;
-
private _fontLocale: IFontLocale;
private _dirty = true;
- constructor(context: ContextBase) {
- this._context = context;
+ constructor(@Inject(LocaleService) protected readonly _localService: LocaleService) {
this._localeInitial();
}
@@ -22,20 +20,12 @@ export class Skeleton {
return this._fontLocale;
}
- getContext() {
- return this._context;
- }
-
makeDirty(state: boolean) {
this._dirty = state;
}
private _localeInitial() {
- if (!this._context) {
- return;
- }
-
- const locale = this._context.getUniver().getGlobalContext().getLocale();
+ const locale = this._localService.getLocale();
const renderFont = locale.getObject('renderFont');
if (!renderFont) {
locale.load({
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
index a116c5de7e..a1afcebf0d 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
@@ -93,11 +93,6 @@ export class SelectionManager {
return this._sheetView.getScene();
}
- /** @deprecated */
- getContext() {
- return this._sheetView.getContext();
- }
-
/** @deprecated */
getMainComponent() {
return this._mainComponent;
@@ -237,10 +232,6 @@ export class SelectionManager {
return this._plugin;
}
- getWorksheetId() {
- return this._worksheet?.getSheetId();
- }
-
/**
* add a selection
* @param selectionRange
@@ -757,7 +748,7 @@ export class SelectionManager {
this._leftTopEventInitial();
- this._worksheet = this.getContext().getWorkBook().getActiveSheet();
+ this._worksheet = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook().getActiveSheet();
this._initModels();
@@ -1073,4 +1064,8 @@ export class SelectionManager {
this.renderCurrentControls();
});
}
-}
+
+ private getWorksheetId(): string {
+ return this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook().getActiveSheet().getSheetId();
+ }
+}
\ No newline at end of file
diff --git a/packages/base-sheets/src/SheetPlugin.ts b/packages/base-sheets/src/SheetPlugin.ts
index caca1396e7..f0a239b55b 100644
--- a/packages/base-sheets/src/SheetPlugin.ts
+++ b/packages/base-sheets/src/SheetPlugin.ts
@@ -1,5 +1,5 @@
import { Engine, IRenderingEngine } from '@univerjs/base-render';
-import { SheetContext, Plugin, PLUGIN_NAMES, DEFAULT_SELECTION, UniverSheet, UIObserver, PluginType } from '@univerjs/core';
+import { Plugin, PLUGIN_NAMES, DEFAULT_SELECTION, UniverSheet, PluginType, CommandManager, ICurrentUniverService, LocaleService } from '@univerjs/core';
import { Dependency, Inject, Injector } from '@wendellhu/redi';
import { SheetPluginObserve, uninstall } from './Basics/Observer';
@@ -30,7 +30,7 @@ import { HideColumnRulerFactory } from './Basics/Register/HideColumnRuler';
/**
* The main sheet base, construct the sheet container and layout, mount the rendering engine
*/
-export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
+export class SheetPlugin extends Plugin<SheetPluginObserve> {
static override type = PluginType.Sheet;
private _config: ISheetPluginConfig;
@@ -63,29 +63,30 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
private _hideColumnRulerFactory: HideColumnRulerFactory;
- constructor(config: Partial<ISheetPluginConfig>, @Inject(Injector) private readonly sheetInjector: Injector) {
+ constructor(
+ config: Partial<ISheetPluginConfig>,
+ @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,
+ @Inject(LocaleService) private readonly _localeService: LocaleService,
+ @Inject(Injector) private readonly _sheetInjector: Injector,
+ @Inject(CommandManager) private readonly _commandManager: CommandManager
+ ) {
super(PLUGIN_NAMES.SPREADSHEET);
this._config = Object.assign(DEFAULT_SPREADSHEET_PLUGIN_DATA, config);
- this.initializeDependencies(sheetInjector);
- }
-
- static create(config?: Partial<ISheetPluginConfig>) {
- return new SheetPlugin(config || {}, new Injector()); // TODO: change this
+ this.initializeDependencies(_sheetInjector);
}
installTo(universheetInstance: UniverSheet) {
universheetInstance.installPlugin(this);
}
- initialize(context: SheetContext): void {
- this.context = context;
-
- this.getGlobalContext().getLocale().load({
+ initialize(): void {
+ this._localeService.getLocale().load({
en,
zh,
});
+
install(this);
this.initConfig();
@@ -102,7 +103,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
initConfig() {
const config = this._config;
if (!config.selections) {
- const worksheetId = this.getContext().getWorkBook().getActiveSheet().getSheetId();
+ const worksheetId = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook().getActiveSheet().getSheetId();
config.selections = {
[worksheetId]: [
{
@@ -115,30 +116,30 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
// TODO@huwenzhao: We don't need to init controllers manually
initController() {
- this._sheetContainerController = this.sheetInjector.get(SheetContainerController);
- this._cellEditorController = this.sheetInjector.get(CellEditorController);
- this._formulaBarController = this.sheetInjector.get(FormulaBarController);
- this._editTooltipsController = this.sheetInjector.get(EditTooltipsController);
- this._sheetBarController = this.sheetInjector.get(SheetBarController);
- this._toolbarController = this.sheetInjector.get(ToolbarController);
- this._rightMenuController = this.sheetInjector.get(RightMenuController);
- this._countBarController = this.sheetInjector.get(CountBarController);
- this._hideColumnController = this.sheetInjector.get(HideColumnController);
+ this._sheetContainerController = this._sheetInjector.get(SheetContainerController);
+ this._cellEditorController = this._sheetInjector.get(CellEditorController);
+ this._formulaBarController = this._sheetInjector.get(FormulaBarController);
+ this._editTooltipsController = this._sheetInjector.get(EditTooltipsController);
+ this._sheetBarController = this._sheetInjector.get(SheetBarController);
+ this._toolbarController = this._sheetInjector.get(ToolbarController);
+ this._rightMenuController = this._sheetInjector.get(RightMenuController);
+ this._countBarController = this._sheetInjector.get(CountBarController);
+ this._hideColumnController = this._sheetInjector.get(HideColumnController);
}
initCanvasView() {
- this._canvasEngine = this.sheetInjector.get(IRenderingEngine);
+ this._canvasEngine = this._sheetInjector.get(IRenderingEngine);
}
- override onMounted(ctx: SheetContext): void {
- this.initialize(ctx);
+ override onMounted(): void {
+ this.initialize();
}
override onDestroy(): void {
super.onDestroy();
uninstall(this);
- const actionRegister = this.context.getCommandManager().getActionExtensionManager().getRegister();
+ const actionRegister = this._commandManager.getActionExtensionManager().getRegister();
actionRegister.delete(this._namedRangeActionExtensionFactory);
const rulerRegister = this._columnRulerManager.getRegister();
@@ -146,11 +147,11 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
}
registerExtension() {
- const actionRegister = this.context.getCommandManager().getActionExtensionManager().getRegister();
+ const actionRegister = this._commandManager.getActionExtensionManager().getRegister();
this._namedRangeActionExtensionFactory = new NamedRangeActionExtensionFactory(this);
actionRegister.add(this._namedRangeActionExtensionFactory);
- this._columnRulerManager = this.sheetInjector.get(ColumnRulerManager);
+ this._columnRulerManager = this._sheetInjector.get(ColumnRulerManager);
const rulerRegister = this._columnRulerManager.getRegister();
this._hideColumnRulerFactory = new HideColumnRulerFactory(this);
rulerRegister.add(this._hideColumnRulerFactory);
@@ -176,7 +177,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
/** @deprecated DI */
getCanvasView() {
- return this.sheetInjector.get(CanvasView);
+ return this._sheetInjector.get(CanvasView);
}
/** @deprecated DI */
@@ -191,7 +192,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
/** @deprecated DI */
getSelectionManager() {
- return this.sheetInjector.get(ISelectionManager);
+ return this._sheetInjector.get(ISelectionManager);
}
/** @deprecated DI */
@@ -229,11 +230,6 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
return this.getSheetView().getSpreadsheetSkeleton();
}
- /** @deprecated DI */
- getWorkbook() {
- return this.context.getWorkBook();
- }
-
/** @deprecated DI */
getRightMenuControl() {
return this._rightMenuController;
@@ -264,10 +260,6 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
return this._hideColumnController;
}
- protected _getCoreObserver<T>(type: string) {
- return this.getGlobalContext().getObserverManager().requiredObserver<UIObserver<T>>(type, 'core');
- }
-
private initializeDependencies(sheetInjector: Injector) {
const self = this;
diff --git a/packages/base-sheets/src/View/BaseView.ts b/packages/base-sheets/src/View/BaseView.ts
index 1f7f2d09c4..ab2ce19ed6 100644
--- a/packages/base-sheets/src/View/BaseView.ts
+++ b/packages/base-sheets/src/View/BaseView.ts
@@ -1,5 +1,6 @@
import { Scene } from '@univerjs/base-render';
-import { Registry, Worksheet, Injector } from '@univerjs/core';
+import { Registry, Worksheet } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
export class BaseView {
viewKey = '';
@@ -14,8 +15,7 @@ export class BaseView {
updateToSheet(worksheet: Worksheet) {}
- initialize(scene: Scene, injector: Injector) {
- this._injector = injector;
+ initialize(scene: Scene) {
this._scene = scene;
this._initialize();
return this;
diff --git a/packages/base-sheets/src/View/Views/SheetView.ts b/packages/base-sheets/src/View/Views/SheetView.ts
index a17adaa081..7e4a2857a3 100644
--- a/packages/base-sheets/src/View/Views/SheetView.ts
+++ b/packages/base-sheets/src/View/Views/SheetView.ts
@@ -1,6 +1,6 @@
import { getColor, Rect, Scene, Spreadsheet, SpreadsheetColumnTitle, SpreadsheetRowTitle, SpreadsheetSkeleton } from '@univerjs/base-render';
-import { Worksheet } from '@univerjs/core';
-import { Injector } from '@wendellhu/redi';
+import { ICurrentUniverService, LocaleService, Worksheet } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
import { BaseView, CANVAS_VIEW_KEY, CanvasViewRegistry } from '../BaseView';
export enum SHEET_VIEW_KEY {
@@ -23,6 +23,10 @@ export class SheetView extends BaseView {
private _spreadsheetLeftTopPlaceholder: Rect;
+ constructor(@ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService, @Inject(LocaleService) private readonly _localeService: LocaleService) {
+ super();
+ }
+
getSpreadsheetSkeleton() {
return this._spreadsheetSkeleton;
}
@@ -68,8 +72,7 @@ export class SheetView extends BaseView {
protected override _initialize() {
const scene = this.getScene();
- const context = this.getContext();
- const workbook = context.getWorkBook();
+ const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();
let worksheet = workbook.getActiveSheet();
if (!worksheet) {
worksheet = workbook.getSheets()[0];
@@ -143,12 +146,9 @@ export class SheetView extends BaseView {
}
private _buildSkeleton(worksheet: Worksheet) {
- const context = this.getContext();
- const workbook = context.getWorkBook();
+ const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();
const config = worksheet.getConfig();
- // const { rowTitle, columnTitle } = config;
- const spreadsheetSkeleton = SpreadsheetSkeleton.create(config, worksheet.getCellMatrix(), workbook.getStyles(), context);
-
+ const spreadsheetSkeleton = SpreadsheetSkeleton.create(config, worksheet.getCellMatrix(), workbook.getStyles(), this._localeService);
return spreadsheetSkeleton;
}
@@ -173,8 +173,9 @@ export class SheetViewFactory {
* @returns
*/
create(scene: Scene, injector: Injector): SheetView {
- // TODO@wzhudev: should be created from DI system and remove the initialize method
- return new SheetView().initialize(scene, injector);
+ const sheetView = injector.createInstance(SheetView);
+ sheetView.initialize(scene);
+ return sheetView;
}
}
diff --git a/packages/core/src/Plugin/Plugin.ts b/packages/core/src/Plugin/Plugin.ts
index 8c42b08da0..fe347dce8c 100644
--- a/packages/core/src/Plugin/Plugin.ts
+++ b/packages/core/src/Plugin/Plugin.ts
@@ -96,6 +96,7 @@ export abstract class Plugin<Obs = any, O extends ContextBase = ContextBase> imp
return this._name;
}
+ /** @deprecated */
getContext(): O {
return this.context;
}
@@ -179,4 +180,4 @@ export class PluginRegistry {
getRegisterPlugins(type: PluginType): [IPluginRegistryItem] {
return this.pluginsRegisteredByBusiness.get(type) || ([] as unknown[] as [IPluginRegistryItem]);
}
-}
\ No newline at end of file
+}