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: backspace in list when select all list content #2230

Merged
merged 1 commit into from
May 16, 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
36 changes: 21 additions & 15 deletions packages/docs/src/commands/commands/delete.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const DeleteLeftCommand: ICommand = {

type: CommandType.COMMAND,

// eslint-disable-next-line max-lines-per-function
handler: async (accessor) => {
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const docSkeletonManagerService = accessor.get(DocSkeletonManagerService);
Expand All @@ -62,23 +63,23 @@ export const DeleteLeftCommand: ICommand = {

const { startOffset, collapsed, segmentId, style } = activeRange;

const preSpan = skeleton.findNodeByCharIndex(startOffset);
const curGlyph = skeleton.findNodeByCharIndex(startOffset);

// is in bullet list?
const preIsBullet = hasListGlyph(preSpan);
const isBullet = hasListGlyph(curGlyph);
// is in indented paragraph?
const preIsIndent = isIndentByGlyph(preSpan, docDataModel.getBody());
const isIndent = isIndentByGlyph(curGlyph, docDataModel.getBody());

let cursor = startOffset;

// Get the deleted span. It maybe null or undefined when the preSpan is first span in skeleton.
const span = skeleton.findNodeByCharIndex(startOffset - 1);
// Get the deleted glyph. It maybe null or undefined when the curGlyph is first glyph in skeleton.
const preGlyph = skeleton.findNodeByCharIndex(startOffset - 1);

const isUpdateParagraph =
isFirstGlyph(preSpan) && span !== preSpan && (preIsBullet === true || preIsIndent === true);
isFirstGlyph(curGlyph) && preGlyph !== curGlyph && (isBullet === true || isIndent === true);

if (isUpdateParagraph) {
const paragraph = getParagraphByGlyph(preSpan, docDataModel.getBody());
if (isUpdateParagraph && collapsed) {
const paragraph = getParagraphByGlyph(curGlyph, docDataModel.getBody());

if (paragraph == null) {
return false;
Expand All @@ -90,7 +91,7 @@ export const DeleteLeftCommand: ICommand = {

const paragraphStyle = paragraph.paragraphStyle;

if (preIsBullet === true) {
if (isBullet === true) {
const paragraphStyle = paragraph.paragraphStyle;

if (paragraphStyle) {
Expand All @@ -102,7 +103,7 @@ export const DeleteLeftCommand: ICommand = {
updateParagraph.paragraphStyle.hanging = undefined;
}
}
} else if (preIsIndent === true) {
} else if (isIndent === true) {
const bullet = paragraph.bullet;

if (bullet) {
Expand Down Expand Up @@ -141,17 +142,17 @@ export const DeleteLeftCommand: ICommand = {
} else {
if (collapsed === true) {
// No need to delete when the cursor is at the first position of the first paragraph.
if (span == null) {
if (preGlyph == null) {
return true;
}

if (span.content === '\r') {
if (preGlyph.content === '\r') {
result = await commandService.executeCommand(MergeTwoParagraphCommand.id, {
direction: DeleteDirection.LEFT,
range: activeRange,
});
} else {
cursor -= span.count;
cursor -= preGlyph.count;

const textRanges = [
{
Expand All @@ -160,12 +161,13 @@ export const DeleteLeftCommand: ICommand = {
style,
},
];

result = await commandService.executeCommand(DeleteCommand.id, {
unitId: docDataModel.getUnitId(),
range: activeRange,
segmentId,
direction: DeleteDirection.LEFT,
len: span.count,
len: preGlyph.count,
textRanges,
});
}
Expand Down Expand Up @@ -270,6 +272,7 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {

type: CommandType.COMMAND,

// eslint-disable-next-line max-lines-per-function
handler: async (accessor, params: IMergeTwoParagraphParams) => {
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);
Expand All @@ -296,7 +299,10 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
}

const startIndex = direction === DeleteDirection.LEFT ? startOffset : startOffset + 1;
const endIndex = docDataModel.getBody()?.paragraphs?.find((p) => p.startIndex >= startIndex)?.startIndex!;
const endIndex = docDataModel
.getBody()
?.paragraphs
?.find((p) => p.startIndex >= startIndex)?.startIndex!;
const body = getParagraphBody(docDataModel.getBody()!, startIndex, endIndex);

const cursor = direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset;
Expand Down
3 changes: 0 additions & 3 deletions packages/engine-render/src/basics/document-node-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ export function isLastGlyph(glyph: Nullable<IDocumentSkeletonGlyph>) {

export function isFirstGlyph(glyph: Nullable<IDocumentSkeletonGlyph>) {
const divide = glyph?.parent;

const line = divide?.parent;

const glyphGroup = divide?.glyphGroup;

const divides = line?.divides;

if (glyphGroup && glyph && divides && divide) {
Expand Down
Loading