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

feat: add isEditing param #1428

Merged
merged 2 commits into from
Feb 28, 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 @@ -43,7 +43,7 @@ export const SelectAllOperation: ICommand<ISelectAllOperationParams> = {
},
];

textSelectionManagerService.replaceTextRanges(textRanges);
textSelectionManagerService.replaceTextRanges(textRanges, false);

return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface ISetTextSelectionsOperationParams {
unitId: string;
subUnitId: string;
segmentId: string;
// Whether it occurs at the same time as text editing.
isEditing: boolean;
style: ITextSelectionStyle;
ranges: ITextRangeWithStyle[];
}
Expand All @@ -31,8 +33,8 @@ export const SetTextSelectionsOperation: IOperation<ISetTextSelectionsOperationP

type: CommandType.OPERATION,

handler: (accessor, prams) => {
// for live share and share cursor.
handler: (_, __) => {
// for menu highlight use and share cursor.
return true;
},
};
14 changes: 7 additions & 7 deletions packages/docs/src/controllers/move-cursor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class MoveCursorController extends Disposable {
endOffset: direction === Direction.LEFT || direction === Direction.UP ? min : max,
style,
},
]);
], false);

return;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ export class MoveCursorController extends Disposable {
endOffset: focusOffset,
style,
},
]);
], false);
} else {
const focusSpan = skeleton.findNodeByCharIndex(focusOffset);

Expand All @@ -172,7 +172,7 @@ export class MoveCursorController extends Disposable {
endOffset: newFocusOffset,
style,
},
]);
], false);

return;
}
Expand All @@ -189,7 +189,7 @@ export class MoveCursorController extends Disposable {
endOffset: newActiveRange.endOffset,
style,
},
]);
], false);
}
}

Expand Down Expand Up @@ -241,7 +241,7 @@ export class MoveCursorController extends Disposable {
endOffset: cursor,
style,
},
]);
], false);
} else {
const startNode = skeleton.findNodeByCharIndex(startOffset);
const endNode = skeleton.findNodeByCharIndex(endOffset);
Expand Down Expand Up @@ -273,7 +273,7 @@ export class MoveCursorController extends Disposable {
endOffset: cursor,
style,
},
]);
], false);
return;
}

Expand All @@ -288,7 +288,7 @@ export class MoveCursorController extends Disposable {
...newActiveRange,
style,
},
]);
], false);
}
}

Expand Down
21 changes: 12 additions & 9 deletions packages/docs/src/services/text-selection-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class TextSelectionManagerService extends RxDisposable {
}

// **Only used in test case** because this does not go through the render layer.
add(textRanges: ISuccinctTextRangeParam[]) {
add(textRanges: ISuccinctTextRangeParam[], isEditing = true) {
if (this._currentSelection == null) {
return;
}
Expand All @@ -163,18 +163,20 @@ export class TextSelectionManagerService extends RxDisposable {
...this._currentSelection,
textRanges: textRanges as TextRange[],
segmentId: '',
isEditing,
style: NORMAL_TEXT_SELECTION_PLUGIN_STYLE, // mock style.
});
}

replaceTextRanges(textRanges: ISuccinctTextRangeParam[]) {
replaceTextRanges(textRanges: ISuccinctTextRangeParam[], isEditing = true) {
if (this._currentSelection == null) {
return;
}

// Remove all textRanges.
this._textSelectionRenderManager.removeAllTextRanges();
this._textSelectionRenderManager.addTextRanges(textRanges);
// Add new textRanges.
this._textSelectionRenderManager.addTextRanges(textRanges, isEditing);
}

// All textRanges should be synchronized from the render layer.
Expand Down Expand Up @@ -204,14 +206,15 @@ export class TextSelectionManagerService extends RxDisposable {
// Broadcast textSelection changes, this should be used within the application.
this._textSelection$.next(params);

const { unitId, subUnitId, segmentId, style, textRanges } = params;
const { unitId, subUnitId, segmentId, style, textRanges, isEditing } = params;

// For live share only.
// For menu status.
this._commandService.executeCommand(SetTextSelectionsOperation.id, {
unitId,
subUnitId,
segmentId,
style,
isEditing,
ranges: textRanges.map(serializeTextRange),
});
}
Expand Down Expand Up @@ -242,19 +245,19 @@ export class TextSelectionManagerService extends RxDisposable {
}

private _replaceByParam(insertParam: ITextSelectionManagerInsertParam) {
const { unitId, subUnitId, style, segmentId, textRanges } = insertParam;
const { unitId, subUnitId, style, segmentId, textRanges, isEditing } = insertParam;

if (!this._textSelectionInfo.has(unitId)) {
this._textSelectionInfo.set(unitId, new Map());
}

const unitTextRange = this._textSelectionInfo.get(unitId)!;

unitTextRange.set(subUnitId, { textRanges, style, segmentId });
unitTextRange.set(subUnitId, { textRanges, style, segmentId, isEditing });
}

private _addByParam(insertParam: ITextSelectionManagerInsertParam): void {
const { unitId, subUnitId, textRanges, style, segmentId } = insertParam;
const { unitId, subUnitId, textRanges, style, segmentId, isEditing } = insertParam;

if (!this._textSelectionInfo.has(unitId)) {
this._textSelectionInfo.set(unitId, new Map());
Expand All @@ -263,7 +266,7 @@ export class TextSelectionManagerService extends RxDisposable {
const unitTextRange = this._textSelectionInfo.get(unitId)!;

if (!unitTextRange.has(subUnitId)) {
unitTextRange.set(subUnitId, { textRanges, style, segmentId });
unitTextRange.set(subUnitId, { textRanges, style, segmentId, isEditing });
} else {
const OldTextRanges = unitTextRange.get(subUnitId)!;
OldTextRanges.textRanges.push(...textRanges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ interface IAddTextRangesConfig {
export interface ITextSelectionInnerParam {
textRanges: TextRange[];
segmentId: string;
isEditing: boolean;
style: ITextSelectionStyle;
}

Expand Down Expand Up @@ -151,7 +152,7 @@ export interface ITextSelectionRenderManager {

removeAllTextRanges(): void;

addTextRanges(ranges: ISuccinctTextRangeParam[], config?: IAddTextRangesConfig): void;
addTextRanges(ranges: ISuccinctTextRangeParam[], isEditing?: boolean): void;

sync(): void;

Expand Down Expand Up @@ -290,7 +291,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._isSelectionEnabled = false;
}

addTextRanges(ranges: ISuccinctTextRangeParam[]) {
addTextRanges(ranges: ISuccinctTextRangeParam[], isEditing = true) {
const { _scene: scene, _docSkeleton: docSkeleton } = this;

for (const range of ranges) {
Expand All @@ -303,6 +304,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
textRanges: this._getAllTextRanges(),
segmentId: this._currentSegmentId,
style: this._selectionStyle,
isEditing,
});

this._updateDomCursorPositionAndSize();
Expand Down Expand Up @@ -389,8 +391,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return;
}

// Firefox do not support Segmenter, so you need a Segmenter polyfill if you want use it in Firefox.
// TODO: @JOCS write this in DOCS or README when we publish the package.
// Firefox do not support Segmenter in an old version, so you need a Segmenter polyfill if you want use it in Firefox.
if (Intl.Segmenter == null) {
return;
}
Expand Down Expand Up @@ -518,10 +519,10 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return;
}

this._moving(moveOffsetX, moveOffsetY, scrollTimer);
this._moving(moveOffsetX, moveOffsetY);

scrollTimer.scrolling(moveOffsetX, moveOffsetY, () => {
this._moving(moveOffsetX, moveOffsetY, scrollTimer);
this._moving(moveOffsetX, moveOffsetY);
});

preMoveOffsetX = moveOffsetX;
Expand All @@ -538,6 +539,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
textRanges: this._getAllTextRanges(),
segmentId: this._currentSegmentId,
style: this._selectionStyle,
isEditing: false,
});

scrollTimer.dispose();
Expand Down Expand Up @@ -914,7 +916,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this.activate(canvasLeft, canvasTop);
}

private _moving(moveOffsetX: number, moveOffsetY: number, scrollTimer: ScrollTimer) {
private _moving(moveOffsetX: number, moveOffsetY: number) {
if (this._docSkeleton == null) {
return;
}
Expand Down
Loading