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(docs): merge two paragraphs #3871

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,6 +14,9 @@
* limitations under the License.
*/

import type { DocumentDataModel, IAccessor, ICommand, ICustomBlock, IDocumentBody, IMutationInfo, IParagraph, ITextRange, ITextRun, JSONXActions, Nullable } from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render';
import {
BuildTextUtils,
CommandType,
Expand All @@ -28,14 +31,12 @@ import {
TextX,
TextXActionType,
Tools,
UniverInstanceType,
UpdateDocsAttributeType,
} from '@univerjs/core';

import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { getParagraphByGlyph, hasListGlyph, isFirstGlyph, isIndentByGlyph } from '@univerjs/engine-render';
import type { IAccessor, ICommand, ICustomBlock, IDocumentBody, IMutationInfo, IParagraph, ITextRange, ITextRun, JSONXActions, Nullable } from '@univerjs/core';

import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render';
import { DeleteDirection } from '../../types/delete-direction';
import { getCommandSkeleton, getRichTextEditPath } from '../util';
import { CutContentCommand } from './clipboard.inner.command';
Expand Down Expand Up @@ -141,6 +142,7 @@ interface IMergeTwoParagraphParams {
}

export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {

id: 'doc.command.merge-two-paragraph',

type: CommandType.COMMAND,
Expand All @@ -159,24 +161,37 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
return false;
}
const { segmentId, style } = activeRange;
const docDataModel = univerInstanceService.getCurrentUniverDocInstance();
const docDataModel = univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
const originBody = docDataModel?.getSelfOrHeaderFooterModel(segmentId).getBody();
if (!docDataModel || !originBody) {
if (docDataModel == null || originBody == null) {
return false;
}

const actualRange = getDeleteSelection(activeRange, originBody);
const unitId = docDataModel.getUnitId();
const { startOffset, collapsed } = actualRange;

const { startOffset, collapsed } = actualRange;
if (!collapsed) {
return false;
}

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

let curParagraph;
let nextParagraph;

for (const paragraph of originBody.paragraphs!) {
if (paragraph.startIndex >= startIndex) {
nextParagraph = paragraph;
break;
}
curParagraph = paragraph;
}

if (curParagraph == null || nextParagraph == null) {
return false;
}

const cursor = direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset;

const textRanges = [
Expand All @@ -200,18 +215,25 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
const textX = new TextX();
const jsonX = JSONX.getInstance();

if (curParagraph.startIndex > 0) {
textX.push({
t: TextXActionType.RETAIN,
len: curParagraph.startIndex,
segmentId,
});
}

textX.push({
t: TextXActionType.RETAIN,
len: direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset,
t: TextXActionType.DELETE,
len: 1,
line: 0,
segmentId,
});

if (body.dataStream.length) {
if (nextParagraph.startIndex > curParagraph.startIndex + 1) {
textX.push({
t: TextXActionType.INSERT,
body,
len: body.dataStream.length,
line: 0,
t: TextXActionType.RETAIN,
len: nextParagraph.startIndex - curParagraph.startIndex - 1,
segmentId,
});
}
Expand All @@ -220,13 +242,16 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
t: TextXActionType.RETAIN,
len: 1,
segmentId,
});

textX.push({
t: TextXActionType.DELETE,
len: endIndex + 1 - startIndex,
line: 0,
segmentId,
coverType: UpdateDocsAttributeType.REPLACE,
body: {
dataStream: '',
paragraphs: [
{
...Tools.deepClone(curParagraph),
startIndex: 0,
},
],
},
});

const path = getRichTextEditPath(docDataModel, segmentId);
Expand Down Expand Up @@ -292,6 +317,7 @@ export function getCursorWhenDelete(textRanges: Readonly<Nullable<ITextRangeWith
// Handle BACKSPACE key.
export const DeleteLeftCommand: ICommand = {
id: 'doc.command.delete-left',

type: CommandType.COMMAND,
// eslint-disable-next-line max-lines-per-function, complexity
handler: async (accessor) => {
Expand All @@ -302,7 +328,7 @@ export const DeleteLeftCommand: ICommand = {
let result = true;

const docDataModel = univerInstanceService.getCurrentUniverDocInstance();
if (!docDataModel) {
if (docDataModel == null) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/controllers/menu.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { MenuSchemaType } from '@univerjs/ui';
import { ContextMenuGroup, ContextMenuPosition, RibbonStartGroup } from '@univerjs/ui';
import { DocCopyCommand, DocCutCommand, DocPasteCommand } from '../commands/commands/clipboard.command';
import { DeleteLeftCommand } from '../commands/commands/delete.command';
import { DeleteLeftCommand } from '../commands/commands/doc-delete.command';
import { OpenHeaderFooterPanelCommand } from '../commands/commands/doc-header-footer.command';
import { ResetInlineFormatTextBackgroundColorCommand, SetInlineFormatBoldCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from '../commands/commands/inline-format.command';
import { BulletListCommand, CheckListCommand, OrderListCommand } from '../commands/commands/list.command';
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/controllers/menu/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DocSelectionManagerService } from '@univerjs/docs';
import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
import { combineLatest, Observable } from 'rxjs';
import { DocCopyCommand, DocCutCommand, DocPasteCommand } from '../../commands/commands/clipboard.command';
import { DeleteLeftCommand } from '../../commands/commands/delete.command';
import { DeleteLeftCommand } from '../../commands/commands/doc-delete.command';
import { DocTableDeleteColumnsCommand, DocTableDeleteRowsCommand, DocTableDeleteTableCommand } from '../../commands/commands/table/doc-table-delete.command';
import { DocTableInsertColumnLeftCommand, DocTableInsertColumnRightCommand, DocTableInsertRowAboveCommand, DocTableInsertRowBellowCommand } from '../../commands/commands/table/doc-table-insert.command';
import { DocParagraphSettingPanelOperation } from '../../commands/operations/doc-paragraph-setting-panel.operation';
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/docs-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { BreakLineCommand } from './commands/commands/break-line.command';
import { DocCopyCommand, DocCutCommand, DocPasteCommand } from './commands/commands/clipboard.command';
import { CutContentCommand, InnerPasteCommand } from './commands/commands/clipboard.inner.command';
import { DeleteCommand, InsertCommand, UpdateCommand } from './commands/commands/core-editing.command';
import { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/delete.command';
import { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/doc-delete.command';
import { CloseHeaderFooterCommand } from './commands/commands/doc-header-footer.command';
import { DocParagraphSettingCommand } from './commands/commands/doc-paragraph-setting.command';
import { IMEInputCommand } from './commands/commands/ime-input.command';
Expand Down
4 changes: 2 additions & 2 deletions packages/docs-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export {
type IUpdateCommandParams,
UpdateCommand,
} from './commands/commands/core-editing.command';
export { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, type IDeleteCustomBlockParams, MergeTwoParagraphCommand } from './commands/commands/delete.command';
export { getCursorWhenDelete } from './commands/commands/delete.command';
export { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, type IDeleteCustomBlockParams, MergeTwoParagraphCommand } from './commands/commands/doc-delete.command';
export { getCursorWhenDelete } from './commands/commands/doc-delete.command';

export { IMEInputCommand } from './commands/commands/ime-input.command';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BuildTextUtils, createIdentifier, DataStreamTreeTokenType, Disposable,
import { DocSelectionManagerService } from '@univerjs/docs';
import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';
import { CutContentCommand, InnerPasteCommand } from '../../commands/commands/clipboard.inner.command';
import { getCursorWhenDelete } from '../../commands/commands/delete.command';
import { getCursorWhenDelete } from '../../commands/commands/doc-delete.command';
import { copyContentCache, extractId, genId } from './copy-content-cache';
import { HtmlToUDMService } from './html-to-udm/converter';
import PastePluginLark from './html-to-udm/paste-plugins/plugin-lark';
Expand Down
4 changes: 2 additions & 2 deletions packages/docs-ui/src/shortcuts/core-editing.shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { KeyCode } from '@univerjs/ui';
import type { IShortcutItem } from '@univerjs/ui';
import { KeyCode } from '@univerjs/ui';
import { EnterCommand } from '../commands/commands/auto-format.command';
import { DeleteLeftCommand, DeleteRightCommand } from '../commands/commands/delete.command';
import { DeleteLeftCommand, DeleteRightCommand } from '../commands/commands/doc-delete.command';
import { whenDocAndEditorFocused, whenDocAndEditorFocusedWithBreakLine } from './utils';

export const BreakLineShortcut: IShortcutItem = {
Expand Down
Loading