Skip to content

Commit

Permalink
refactor(docs): refactor of doc selections (#3299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs authored Sep 11, 2024
1 parent 83238fa commit 8b42b45
Show file tree
Hide file tree
Showing 173 changed files with 2,062 additions and 1,985 deletions.
15 changes: 8 additions & 7 deletions packages-experimental/uni-docs-ui/src/controllers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import type { IAccessor } from '@univerjs/core';
import { BooleanNumber, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { DocSkeletonManagerService, SetInlineFormatCommand, SetTextSelectionsOperation, TextSelectionManagerService } from '@univerjs/docs';
import { DocSelectionManagerService, DocSkeletonManagerService, SetTextSelectionsOperation } from '@univerjs/docs';
import { SetInlineFormatCommand } from '@univerjs/docs-ui';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';

import { getHeaderFooterMenuHiddenObservable, getMenuHiddenObservable, type IMenuButtonItem, type IMenuItem, MenuItemType } from '@univerjs/ui';
Expand Down Expand Up @@ -180,7 +181,7 @@ export function DocTableMenuFactory(accessor: IAccessor): IMenuItem {

function getFontStyleAtCursor(accessor: IAccessor) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const textSelectionService = accessor.get(TextSelectionManagerService);
const textSelectionService = accessor.get(DocSelectionManagerService);

const editorDataModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
const activeTextRange = textSelectionService.getActiveTextRange();
Expand All @@ -196,10 +197,10 @@ function getFontStyleAtCursor(accessor: IAccessor) {
}

function getTableDisabledObservable(accessor: IAccessor): Observable<boolean> {
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const docSelectionManagerService = accessor.get(DocSelectionManagerService);

return new Observable((subscriber) => {
const subscription = textSelectionManagerService.textSelection$.subscribe((selection) => {
const subscription = docSelectionManagerService.textSelection$.subscribe((selection) => {
if (selection == null) {
subscriber.next(true);
return;
Expand All @@ -213,15 +214,15 @@ function getTableDisabledObservable(accessor: IAccessor): Observable<boolean> {
}

const textRange = textRanges[0];
const { collapsed, anchorNodePosition } = textRange;
const { collapsed, startNodePosition } = textRange;

if (!collapsed) {
subscriber.next(true);
return;
}

if (anchorNodePosition != null) {
const { path } = anchorNodePosition;
if (startNodePosition != null) {
const { path } = startNodePosition;

// TODO: Not support insert table in table cell now.
if (path.indexOf('cells') !== -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import { ICommandService, IConfigService, Inject, Injector, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { ComponentManager, ILayoutService, IMenuManagerService, IShortcutService, IUIPartsService } from '@univerjs/ui';
import { BuiltinUniToolbarItemId, generateCloneMutation, UniToolbarService } from '@univerjs/uniui';
import { DocCreateTableOperation, DocUIController } from '@univerjs/docs-ui';
import { BulletListCommand, OrderListCommand, SetInlineFormatBoldCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from '@univerjs/docs';
import { BulletListCommand, DocCreateTableOperation, DocUIController, OrderListCommand, SetInlineFormatBoldCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from '@univerjs/docs-ui';
import { IMAGE_MENU_ID as DocsImageMenuId } from '@univerjs/docs-drawing-ui';
import { DOC_BOLD_MUTATION_ID, DOC_ITALIC_MUTATION_ID, DOC_STRIKE_MUTATION_ID, DOC_TABLE_MUTATION_ID, DOC_UNDERLINE_MUTATION_ID } from './menu';
import { menuSchema } from './menu.schema';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { ICommand, IDocumentBody, IMutationInfo } from '@univerjs/core';
import { CommandType, CustomRangeType, generateRandomId, ICommandService, LocaleService, makeCustomRangeStream, sequenceExecute } from '@univerjs/core';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs-ui';
import type { IAddDocUniFormulaMutationParams, IRemoveDocUniFormulaMutationParams, IUpdateDocUniFormulaMutationParams } from '@univerjs/uni-formula';
import { AddDocUniFormulaMutation, RemoveDocUniFormulaMutation, UpdateDocUniFormulaMutation } from '@univerjs/uni-formula';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { ICommand, IDocumentBody } from '@univerjs/core';
import { CommandType, CustomRangeType, generateRandomId, ICommandService, LocaleService, makeCustomRangeStream } from '@univerjs/core';
import { SLIDE_EDITOR_ID } from '@univerjs/slides-ui';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs-ui';
import { SlideUIFormulaCacheService } from '../../services/slide-ui-formula-cache.service';

export interface IAddSlideUniFormulaCommandParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import type { DocumentDataModel } from '@univerjs/core';
import { CustomRangeType, Disposable, ICommandService, ILogService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { IInsertCommandParams } from '@univerjs/docs';
import { DeleteLeftCommand, InsertCommand, MoveCursorOperation, TextSelectionManagerService } from '@univerjs/docs';
import { DocSelectionManagerService } from '@univerjs/docs';
import { IEditorService } from '@univerjs/ui';
import { DocEventManagerService } from '@univerjs/docs-ui';
import type { IInsertCommandParams } from '@univerjs/docs-ui';
import { DeleteLeftCommand, DocEventManagerService, InsertCommand, MoveCursorOperation } from '@univerjs/docs-ui';

import { filter, map, mergeMap } from 'rxjs';
import { IRenderManagerService } from '@univerjs/engine-render';
Expand All @@ -39,8 +39,8 @@ export class DocUniFormulaInputController extends Disposable {
@IEditorService private readonly _editorService: IEditorService,
@ILogService private readonly _logService: ILogService,
@Inject(UniFormulaPopupService) private readonly _formulaPopupSrv: UniFormulaPopupService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@Inject(DocSelectionManagerService) private readonly _textSelectionManagerService: DocSelectionManagerService
) {
super();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import { Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { IEditorService } from '@univerjs/ui';
import type { IInsertCommandParams } from '@univerjs/docs';
import { InsertCommand, TextSelectionManagerService } from '@univerjs/docs';
import { DocSelectionManagerService } from '@univerjs/docs';
import { ISlideEditorBridgeService } from '@univerjs/slides-ui';
import type { IInsertCommandParams } from '@univerjs/docs-ui';
import { InsertCommand } from '@univerjs/docs-ui';
import { AddSlideUniFormulaCommand } from '../commands/commands/slide.command';
import { UNI_FORMULA_EDITOR_ID } from '../views/components/DocFormulaPopup';
import { UniFormulaPopupService } from '../services/formula-popup.service';
Expand All @@ -34,7 +35,7 @@ export class SlideUniFormulaInputController extends Disposable {
@IUniverInstanceService private readonly _instanceSrv: IUniverInstanceService,
@ICommandService private readonly _commandSrv: ICommandService,
@IEditorService private readonly _editorSrv: IEditorService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@Inject(DocSelectionManagerService) private readonly _textSelectionManagerService: DocSelectionManagerService,
@Inject(UniFormulaPopupService) private readonly _formulaPopupSrv: UniFormulaPopupService
) {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import type { IDisposable, Nullable } from '@univerjs/core';
import { Disposable, FORMULA_EDITOR_ACTIVATED, ICommandService, IContextService, ILogService, Inject } from '@univerjs/core';
import { makeSelection } from '@univerjs/docs';
import { DocCanvasPopManagerService } from '@univerjs/docs-ui';
import { DocCanvasPopManagerService, makeSelection } from '@univerjs/docs-ui';
import { BehaviorSubject, Subject } from 'rxjs';
import type { IShortcutItem } from '@univerjs/ui';
import { IShortcutService, KeyCode } from '@univerjs/ui';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ import {
toDisposable,
UniverInstanceType,
} from '@univerjs/core';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs';
import { DataSyncPrimaryController } from '@univerjs/rpc';
import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';
import type { IDocFormulaCache, ISlideFormulaCache } from '@univerjs/uni-formula';
import { DumbUniFormulaService, IUniFormulaService } from '@univerjs/uni-formula';
import { take } from 'rxjs';
import { RichText } from '@univerjs/engine-render';
import { CanvasView } from '@univerjs/slides-ui';
import { makeSelection, replaceSelectionFactory } from '@univerjs/docs-ui';
import { type IDocPopupPosition, type ISlidePopupPosition, isSlidePosition } from '../commands/operations/operation';

const PSEUDO_SUBUNIT = 'PSEUDO_SUBUNIT';
Expand Down
5 changes: 3 additions & 2 deletions packages-experimental/uni-sheets-ui/src/controllers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { IAccessor } from '@univerjs/core';
import { BooleanNumber, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, EDITOR_ACTIVATED, FOCUSING_SHEET, FontItalic, FontWeight, ICommandService, IContextService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { SetInlineFormatCommand, SetTextSelectionsOperation, TextSelectionManagerService } from '@univerjs/docs';
import { DocSelectionManagerService, SetTextSelectionsOperation } from '@univerjs/docs';
import {
RangeProtectionPermissionEditPoint,
SetRangeValuesMutation,
Expand All @@ -27,6 +27,7 @@ import {
WorksheetEditPermission,
WorksheetSetCellStylePermission,
} from '@univerjs/sheets';
import { SetInlineFormatCommand } from '@univerjs/docs-ui';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { getMenuHiddenObservable, type IMenuButtonItem, MenuItemType } from '@univerjs/ui';
import { Observable } from 'rxjs';
Expand Down Expand Up @@ -266,7 +267,7 @@ export function SheetStrikeThroughMenuItemFactory(accessor: IAccessor): IMenuBut

function getFontStyleAtCursor(accessor: IAccessor) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const textSelectionService = accessor.get(TextSelectionManagerService);
const textSelectionService = accessor.get(DocSelectionManagerService);

const editorDataModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
const activeTextRange = textSelectionService.getActiveRectRange();
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/sheets/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ export interface ISelection {
}

/**
* Selection range Info, contains selectionrange & primaryrange
* primaryrange is the range of the highlighted cell.
* Selection range Info, contains selection range & primary range
* primary range is the range of the highlighted cell.
*/
export interface ISelectionWithCoord {
rangeWithCoord: IRangeWithCoord;
Expand All @@ -578,15 +578,28 @@ export interface ITextRangeStart {
startOffset: number;
}

export enum RANGE_DIRECTION {
NONE = 'none',
BACKWARD = 'backward',
FORWARD = 'forward',
}

export interface ITextRange extends ITextRangeStart {
endOffset: number;
collapsed: boolean;
direction?: RANGE_DIRECTION;
}

export enum DOC_RANGE_TYPE {
RECT = 'RECT',
TEXT = 'TEXT',
}

export interface ITextRangeParam extends ITextRange {
segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.
segmentPage?: number; //The page number of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.
isActive?: boolean; // Whether the text range is active or current range.
rangeType?: DOC_RANGE_TYPE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
TextXActionType,
} from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { getCustomBlockIdsInSelections, getRetainAndDeleteFromReplace, getRichTextEditPath, RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs';
import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { getCustomBlockIdsInSelections, getRetainAndDeleteFromReplace, getRichTextEditPath } from '@univerjs/docs-ui';
import type { IInsertDrawingCommandParams } from './interfaces';

/**
Expand All @@ -42,10 +43,10 @@ export const InsertDocDrawingCommand: ICommand = {
}

const commandService = accessor.get(ICommandService);
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const docSelectionManagerService = accessor.get(DocSelectionManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);

const activeTextRange = textSelectionManagerService.getActiveTextRangeWithStyle();
const activeTextRange = docSelectionManagerService.getActiveTextRange();
const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();
if (activeTextRange == null || documentDataModel == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import {
TextXActionType,
} from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { getRichTextEditPath, RichTextEditingMutation } from '@univerjs/docs';
import { type ITextRangeWithStyle, ITextSelectionRenderManager } from '@univerjs/engine-render';
import { RichTextEditingMutation } from '@univerjs/docs';
import { IRenderManagerService, type ITextRangeWithStyle } from '@univerjs/engine-render';
import { DocSelectionRenderService, getRichTextEditPath } from '@univerjs/docs-ui';
import type { IDeleteDrawingCommandParams } from './interfaces';

/**
Expand All @@ -39,16 +40,18 @@ export const RemoveDocDrawingCommand: ICommand = {
handler: (accessor: IAccessor, params?: IDeleteDrawingCommandParams) => {
const commandService = accessor.get(ICommandService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const textSelectionRenderManager = accessor.get(ITextSelectionRenderManager);
const renderManagerService = accessor.get(IRenderManagerService);
const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();

if (params == null || documentDataModel == null) {
return false;
}

const docSelectionRenderService = renderManagerService.getRenderById(params.unitId)!.with(DocSelectionRenderService)!;

const { drawings: removeDrawings } = params;

const segmentId = textSelectionRenderManager.getSegment() ?? '';
const segmentId = docSelectionRenderService.getSegment() ?? '';

const textX = new TextX();
const jsonX = JSONX.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import {
Tools,
} from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { DocSkeletonManagerService, getRichTextEditPath, RichTextEditingMutation } from '@univerjs/docs';
import { DocSkeletonManagerService, RichTextEditingMutation } from '@univerjs/docs';
import type { IDocDrawing } from '@univerjs/docs-drawing';
import { DocumentEditArea, IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/engine-render';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
import { DocSelectionRenderService, getRichTextEditPath } from '@univerjs/docs-ui';
import { DocRefreshDrawingsService } from '../../services/doc-refresh-drawings.service';

export enum TextWrappingStyle {
Expand All @@ -58,7 +59,7 @@ function getDeleteAndInsertCustomBlockActions(
offset: number,
drawingId: string,
documentDataModel: DocumentDataModel,
textSelectionRenderManager: ITextSelectionRenderManager
docSelectionRenderManager: DocSelectionRenderService
) {
const textX = new TextX();
const jsonX = JSONX.getInstance();
Expand Down Expand Up @@ -210,8 +211,8 @@ function getDeleteAndInsertCustomBlockActions(
action = jsonX.editOp(textX.serialize(), path);
rawActions.push(action!);

textSelectionRenderManager.setSegment(segmentId);
textSelectionRenderManager.setSegmentPage(segmentPage);
docSelectionRenderManager.setSegment(segmentId);
docSelectionRenderManager.setSegmentPage(segmentPage);
}

return rawActions;
Expand Down Expand Up @@ -669,12 +670,13 @@ export const IMoveInlineDrawingCommand: ICommand = {
}

const renderManagerService = accessor.get(IRenderManagerService);
const textSelectionRenderManager = accessor.get(ITextSelectionRenderManager);
const docSelectionRenderService = renderManagerService.getRenderById(params.unitId)?.with(DocSelectionRenderService);

const docRefreshDrawingsService = accessor.get(DocRefreshDrawingsService);
const renderObject = renderManagerService.getRenderById(params.unitId);
const scene = renderObject?.scene;
const skeleton = renderObject?.with(DocSkeletonManagerService).getSkeleton();
if (scene == null) {
if (scene == null || docSelectionRenderService == null) {
return false;
}
const transformer = scene.getTransformerByCreate();
Expand All @@ -699,7 +701,7 @@ export const IMoveInlineDrawingCommand: ICommand = {
const rawActions: JSONXActions = [];

const { drawingId } = drawing;
const segmentId = textSelectionRenderManager.getSegment() ?? '';
const segmentId = docSelectionRenderService.getSegment() ?? '';

const actions = getDeleteAndInsertCustomBlockActions(
newSegmentId,
Expand All @@ -708,7 +710,7 @@ export const IMoveInlineDrawingCommand: ICommand = {
offset,
drawingId,
documentDataModel,
textSelectionRenderManager
docSelectionRenderService
);

if (actions == null || actions.length === 0) {
Expand Down Expand Up @@ -767,12 +769,13 @@ export const ITransformNonInlineDrawingCommand: ICommand = {
return false;
}

const textSelectionRenderManager = accessor.get(ITextSelectionRenderManager);

const renderManagerService = accessor.get(IRenderManagerService);

const docSelectionRenderService = renderManagerService.getRenderById(params.unitId)?.with(DocSelectionRenderService);

const renderObject = renderManagerService.getRenderById(params.unitId);
const scene = renderObject?.scene;
if (scene == null) {
if (scene == null || docSelectionRenderService == null) {
return false;
}
const transformer = scene.getTransformerByCreate();
Expand All @@ -790,7 +793,7 @@ export const ITransformNonInlineDrawingCommand: ICommand = {

const { drawingId } = drawing;

const segmentId = textSelectionRenderManager.getSegment() ?? '';
const segmentId = docSelectionRenderService.getSegment() ?? '';

const actions = getDeleteAndInsertCustomBlockActions(
newSegmentId,
Expand All @@ -799,7 +802,7 @@ export const ITransformNonInlineDrawingCommand: ICommand = {
offset,
drawingId,
documentDataModel,
textSelectionRenderManager
docSelectionRenderService
);

if (actions == null) {
Expand Down
Loading

0 comments on commit 8b42b45

Please sign in to comment.