Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Oct 30, 2024
1 parent 9fd90cf commit f104e3f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,39 +160,38 @@ export function addCustomRangeTextX(param: IAddCustomRangeTextXParam) {
// paragraph information needs to be preserved when performing the CUT operation

export function deleteSelectionTextX(
selection: ITextRange,
selections: ITextRange[],
body: IDocumentBody,
memoryCursor: number = 0,
insertBody: Nullable<IDocumentBody> = null,
keepBullet: boolean = true
): Array<TextXAction> {
const { startOffset, endOffset } = selection;
selections.sort((a, b) => a.startOffset - b.startOffset);
const dos: Array<TextXAction> = [];
const { paragraphs = [] } = body;

const textStart = startOffset - memoryCursor;
const textEnd = endOffset - memoryCursor;

const paragraphInRange = paragraphs?.find(
(p) => p.startIndex - memoryCursor >= textStart && p.startIndex - memoryCursor < textEnd
(p) => p.startIndex >= selections[0].startOffset && p.startIndex < selections[0].endOffset
);
let cursor = memoryCursor;
selections.forEach((selection) => {
const { startOffset, endOffset } = selection;
if (startOffset - memoryCursor > cursor) {
dos.push({
t: TextXActionType.RETAIN,
len: startOffset - memoryCursor,
});
cursor = startOffset;
}

if (textStart > 0) {
dos.push({
t: TextXActionType.RETAIN,
len: textStart,
});
}

let cursor = textStart;

if (cursor < textEnd) {
dos.push({
t: TextXActionType.DELETE,
len: textEnd - cursor,
});
cursor = textEnd;
}
if (cursor < endOffset) {
dos.push({
t: TextXActionType.DELETE,
len: endOffset - memoryCursor - cursor,
});
cursor = endOffset;
}
});

if (insertBody) {
dos.push({
Expand All @@ -203,7 +202,7 @@ export function deleteSelectionTextX(
}

if (paragraphInRange && keepBullet) {
const nextParagraph = paragraphs.find((p) => p.startIndex - memoryCursor >= textEnd);
const nextParagraph = paragraphs.find((p) => p.startIndex - memoryCursor >= (selections[selections.length - 1].endOffset - 1));
if (nextParagraph) {
if (nextParagraph.startIndex > cursor) {
dos.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const InsertDocDrawingCommand: ICommand = {
});
}
} else {
const dos = BuildTextUtils.selection.getDeleteActions(activeTextRange, body, 0, null, false);
const dos = BuildTextUtils.selection.getDeleteActions([activeTextRange], body, 0, null, false);
textX.push(...dos);

const removedCustomBlockIds = getCustomBlockIdsInSelections(body, [activeTextRange]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
len: body.dataStream.length,
});
} else {
const dos = BuildTextUtils.selection.getDeleteActions(selection, body, memoryCursor.cursor, cloneBody, i === selections.length - 1);
const dos = BuildTextUtils.selection.getDeleteActions([selection], body, memoryCursor.cursor, cloneBody, false);
textX.push(...dos);
}

Expand Down Expand Up @@ -256,7 +256,7 @@ function getCutActionsFromTextRanges(
len,
});
} else {
textX.push(...BuildTextUtils.selection.getDeleteActions(selection, originBody, memoryCursor.cursor, null, i === selections.length - 1));
textX.push(...BuildTextUtils.selection.getDeleteActions([selection], originBody, memoryCursor.cursor, null, false));
}

memoryCursor.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
len: body.dataStream.length,
});
} else {
const dos = BuildTextUtils.selection.getDeleteActions(range, originBody, 0, body);
const dos = BuildTextUtils.selection.getDeleteActions([range], originBody, 0, body);
textX.push(...dos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {
const jsonX = JSONX.getInstance();

if (!previousActiveRange.collapsed && isCompositionStart) {
const dos = BuildTextUtils.selection.getDeleteActions(previousActiveRange, body, 0, null, false);
const dos = BuildTextUtils.selection.getDeleteActions([previousActiveRange], body, 0, null, false);
textX.push(...dos);
doMutation.params!.textRanges = [{
startOffset: startOffset + len,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export const ReplaceSelectionCommand: ICommand<IReplaceSelectionCommandParams> =
const textX = new TextX();
const jsonX = JSONX.getInstance();
// delete
textX.push(...BuildTextUtils.selection.getDeleteActions(selection, body, 0, insertBody));
textX.push(...BuildTextUtils.selection.getDeleteActions([selection], body, 0, insertBody));
doMutation.params.actions = jsonX.editOp(textX.serialize());

return true;
Expand Down

0 comments on commit f104e3f

Please sign in to comment.