Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Jun 5, 2024
1 parent 82d4ce3 commit aaa58f6
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/roosterjs-content-model-dom/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ export { cacheGetEventData } from './domUtils/event/cacheGetEventData';

export { isBlockGroupOfType } from './modelApi/typeCheck/isBlockGroupOfType';

export { getClosestAncestorBlockGroupIndex } from './modelApi/editing/getClosestAncestorBlockGroupIndex';

export { iterateSelections } from './modelApi/selection/iterateSelections';
export {
getFirstSelectedListItem,
Expand Down Expand Up @@ -134,6 +132,8 @@ export { setTableCellBackgroundColor } from './modelApi/editing/setTableCellBack
export { retrieveModelFormatState } from './modelApi/editing/retrieveModelFormatState';
export { getListStyleTypeFromString } from './modelApi/editing/getListStyleTypeFromString';
export { getSegmentTextFormat } from './modelApi/editing/getSegmentTextFormat';
export { getClosestAncestorBlockGroupIndex } from './modelApi/editing/getClosestAncestorBlockGroupIndex';
export { runEditSteps } from './modelApi/editing/runEditSteps';

export { updateImageMetadata, getImageMetadata } from './modelApi/metadata/updateImageMetadata';
export {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { deleteExpandedSelection } from './deleteExpandedSelection';
import { mutateBlock } from '../common/mutate';
import { runEditSteps } from './runEditSteps';
import type {
DeleteSelectionContext,
DeleteSelectionResult,
DeleteSelectionStep,
FormatContentModelContext,
ReadonlyContentModelDocument,
ValidDeleteSelectionContext,
} from 'roosterjs-content-model-types';

/**
Expand All @@ -22,23 +22,16 @@ export function deleteSelection(
formatContext?: FormatContentModelContext
): DeleteSelectionResult {
const context = deleteExpandedSelection(model, formatContext);
const steps = additionalSteps.filter(
(x: DeleteSelectionStep | null): x is DeleteSelectionStep => !!x
);

additionalSteps.forEach(step => {
if (step && isValidDeleteSelectionContext(context)) {
step(context);
}
});
runEditSteps(steps, context);

mergeParagraphAfterDelete(context);
return context;
}

function isValidDeleteSelectionContext(
context: DeleteSelectionContext
): context is ValidDeleteSelectionContext {
return !!context.insertPoint;
}

// If we end up with multiple paragraphs impacted, we need to merge them
function mergeParagraphAfterDelete(context: DeleteSelectionContext) {
const { insertPoint, deleteResult, lastParagraph, lastTableContext } = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {
DeleteSelectionContext,
DeleteSelectionResult,
DeleteSelectionStep,
ValidDeleteSelectionContext,
} from 'roosterjs-content-model-types';

/**
* Run editing steps on top of a given context object which includes current insert point and previous editing result
* @param steps The editing steps to run
* @param context Context for the editing steps.
*/
export function runEditSteps(steps: DeleteSelectionStep[], context: DeleteSelectionResult) {
steps.forEach(step => {
if (step && isValidDeleteSelectionContext(context)) {
step(context);
}
});
}

function isValidDeleteSelectionContext(
context: DeleteSelectionContext
): context is ValidDeleteSelectionContext {
return !!context.insertPoint;
}
10 changes: 3 additions & 7 deletions packages/roosterjs-content-model-plugins/lib/edit/EditPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class EditPlugin implements EditorPlugin {
private disposer: (() => void) | null = null;
private shouldHandleNextInputEvent = false;
private selectionAfterDelete: DOMSelection | null = null;
private handleEnterKey = false;
private handleNormalEnter = false;

/**
* Get name of this plugin
Expand All @@ -43,7 +43,7 @@ export class EditPlugin implements EditorPlugin {
*/
initialize(editor: IEditor) {
this.editor = editor;
this.handleEnterKey = this.editor.isExperimentalFeatureEnabled('PersistCache');
this.handleNormalEnter = this.editor.isExperimentalFeatureEnabled('PersistCache');

if (editor.getEnvironment().isAndroid) {
this.disposer = this.editor.attachDomEvent({
Expand Down Expand Up @@ -157,11 +157,7 @@ export class EditPlugin implements EditorPlugin {
break;

case 'Enter':
if (this.handleEnterKey) {
keyboardEnter(editor, rawEvent);
} else {
keyboardInput(editor, rawEvent);
}
keyboardEnter(editor, rawEvent, this.handleNormalEnter);
break;

default:
Expand Down
34 changes: 13 additions & 21 deletions packages/roosterjs-content-model-plugins/lib/edit/keyboardEnter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { deleteEmptyQuote } from './deleteSteps/deleteEmptyQuote';
import { deleteSelection, normalizeContentModel } from 'roosterjs-content-model-dom';
import { deleteSelection, normalizeContentModel, runEditSteps } from 'roosterjs-content-model-dom';
import { handleEnterOnList } from './inputSteps/handleEnterOnList';
import { handleEnterOnParagraph } from './inputSteps/handleEnterOnParagraph';
import type {
DeleteSelectionContext,
IEditor,
ValidDeleteSelectionContext,
} from 'roosterjs-content-model-types';
import type { IEditor } from 'roosterjs-content-model-types';

/**
* @internal
*/
export function keyboardEnter(editor: IEditor, rawEvent: KeyboardEvent) {
export function keyboardEnter(
editor: IEditor,
rawEvent: KeyboardEvent,
handleNormalEnter: boolean
) {
const selection = editor.getDOMSelection();

editor.formatContentModel(
Expand All @@ -25,15 +25,13 @@ export function keyboardEnter(editor: IEditor, rawEvent: KeyboardEvent) {
// so further delete steps can keep working
result.deleteResult = 'notDeleted';

const steps = rawEvent.shiftKey
? [handleEnterOnParagraph]
: [handleEnterOnList, deleteEmptyQuote, handleEnterOnParagraph];
const steps = rawEvent.shiftKey ? [] : [handleEnterOnList, deleteEmptyQuote];

steps.forEach(step => {
if (isValidDeleteSelectionContext(result)) {
step(result);
}
});
if (handleNormalEnter) {
steps.push(handleEnterOnParagraph);
}

runEditSteps(steps, result);
}

if (result.deleteResult == 'range') {
Expand All @@ -54,9 +52,3 @@ export function keyboardEnter(editor: IEditor, rawEvent: KeyboardEvent) {
}
);
}

function isValidDeleteSelectionContext(
context: DeleteSelectionContext
): context is ValidDeleteSelectionContext {
return !!context.insertPoint;
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('EditPlugin', () => {
expect(keyboardEnterSpy).not.toHaveBeenCalled();
});

it('Enter, keyboardEnter not enabled', () => {
it('Enter, normal enter not enabled', () => {
plugin = new EditPlugin();
const rawEvent = { which: 13, key: 'Enter' } as any;
const addUndoSnapshotSpy = jasmine.createSpy('addUndoSnapshot');
Expand All @@ -138,12 +138,12 @@ describe('EditPlugin', () => {
});

expect(keyboardDeleteSpy).not.toHaveBeenCalled();
expect(keyboardInputSpy).toHaveBeenCalledWith(editor, rawEvent);
expect(keyboardEnterSpy).not.toHaveBeenCalled();
expect(keyboardInputSpy).not.toHaveBeenCalled();
expect(keyboardEnterSpy).toHaveBeenCalledWith(editor, rawEvent, false);
expect(keyboardTabSpy).not.toHaveBeenCalled();
});

it('Enter, keyboardEnter enabled', () => {
it('Enter, normal enter enabled', () => {
isExperimentalFeatureEnabledSpy.and.callFake(
(featureName: string) => featureName == 'PersistCache'
);
Expand All @@ -162,7 +162,7 @@ describe('EditPlugin', () => {

expect(keyboardDeleteSpy).not.toHaveBeenCalled();
expect(keyboardInputSpy).not.toHaveBeenCalled();
expect(keyboardEnterSpy).toHaveBeenCalledWith(editor, rawEvent);
expect(keyboardEnterSpy).toHaveBeenCalledWith(editor, rawEvent, true);
expect(keyboardTabSpy).not.toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ describe('handleEnterOnList - keyboardEnter', () => {
},
});

keyboardEnter(editor, mockedEvent);
keyboardEnter(editor, mockedEvent, true);
},
input,
expectedResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('keyboardEnter', () => {
expect();
});

keyboardEnter(editor, rawEvent);
keyboardEnter(editor, rawEvent, true);

expect(formatContentModelSpy).toHaveBeenCalledTimes(1);
expect(input).toEqual(output);
Expand Down

0 comments on commit aaa58f6

Please sign in to comment.