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

Convert DeleteResult from const enum to string literal type #2191

Merged
merged 2 commits into from
Nov 7, 2023
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 @@ -3,7 +3,6 @@ import { addRangeToSelection } from '../../domUtils/addRangeToSelection';
import { ChangeSource } from '../../publicTypes/event/ContentModelContentChangedEvent';
import { cloneModel } from '../../modelApi/common/cloneModel';
import { ColorTransformDirection, PluginEventType } from 'roosterjs-editor-types';
import { DeleteResult } from '../../modelApi/edit/utils/DeleteSelectionStep';
import { deleteSelection } from '../../modelApi/edit/deleteSelection';
import { extractClipboardItems } from 'roosterjs-editor-dom';
import { iterateSelections } from '../../modelApi/selection/iterateSelections';
Expand Down Expand Up @@ -161,10 +160,7 @@ export default class ContentModelCopyPastePlugin implements PluginWithState<Copy
if (isCut) {
editor.formatContentModel(
(model, context) => {
if (
deleteSelection(model, [], context).deleteResult ==
DeleteResult.Range
) {
if (deleteSelection(model, [], context).deleteResult == 'range') {
normalizeContentModel(model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DeleteResult } from '../../modelApi/edit/utils/DeleteSelectionStep';
import { normalizeContentModel } from 'roosterjs-content-model-dom';
import { PluginEventType } from 'roosterjs-editor-types';
import type { DeleteResult } from '../../modelApi/edit/utils/DeleteSelectionStep';
import type { ContentModelDocument } from 'roosterjs-content-model-types';
import type { FormatWithContentModelContext } from '../../publicTypes/parameter/FormatWithContentModelContext';
import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor';
Expand All @@ -20,25 +20,25 @@ export function handleKeyboardEventResult(
context.clearModelCache = false;

switch (result) {
case DeleteResult.NotDeleted:
case 'notDeleted':
// We have not delete anything, we will let browser handle this event, so that current cached model may be invalid
context.clearModelCache = true;

// Return false here since we didn't do any change to Content Model, so no need to rewrite with Content Model
return false;

case DeleteResult.NothingToDelete:
case 'nothingToDelete':
// We known there is nothing to delete, no need to let browser keep handling the event
rawEvent.preventDefault();
return false;

case DeleteResult.Range:
case DeleteResult.SingleChar:
case 'range':
case 'singleChar':
// We have deleted what we need from content model, no need to let browser keep handling the event
rawEvent.preventDefault();
normalizeContentModel(model);

if (result == DeleteResult.Range) {
if (result == 'range') {
// A range is about to be deleted, so add an undo snapshot immediately
context.skipUndoSnapshot = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { deleteExpandedSelection } from './utils/deleteExpandedSelection';
import { DeleteResult } from './utils/DeleteSelectionStep';
import type { ContentModelDocument } from 'roosterjs-content-model-types';
import type { FormatWithContentModelContext } from '../../publicTypes/parameter/FormatWithContentModelContext';
import type {
Expand All @@ -23,7 +22,7 @@ export function deleteSelection(
if (
step &&
isValidDeleteSelectionContext(context) &&
context.deleteResult == DeleteResult.NotDeleted
context.deleteResult == 'notDeleted'
) {
step(context);
}
Expand All @@ -46,8 +45,8 @@ function mergeParagraphAfterDelete(context: DeleteSelectionContext) {

if (
insertPoint &&
deleteResult != DeleteResult.NotDeleted &&
deleteResult != DeleteResult.NothingToDelete &&
deleteResult != 'notDeleted' &&
deleteResult != 'nothingToDelete' &&
lastParagraph &&
lastParagraph != insertPoint.paragraph &&
lastTableContext == insertPoint.tableContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DeleteResult } from '../utils/DeleteSelectionStep';
import { deleteSegment } from '../utils/deleteSegment';
import type { DeleteSelectionStep } from '../utils/DeleteSelectionStep';

Expand All @@ -15,7 +14,7 @@ export const deleteAllSegmentBefore: DeleteSelectionStep = context => {
segment.isSelected = true;

if (deleteSegment(paragraph, segment, context.formatContext)) {
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createInsertPoint } from '../utils/createInsertPoint';
import { deleteBlock } from '../utils/deleteBlock';
import { DeleteResult } from '../utils/DeleteSelectionStep';
import { deleteSegment } from '../utils/deleteSegment';
import { getLeafSiblingBlock } from '../../block/getLeafSiblingBlock';
import { setParagraphNotImplicit } from 'roosterjs-content-model-dom';
Expand All @@ -22,7 +21,7 @@ function getDeleteCollapsedSelection(direction: 'forward' | 'backward'): DeleteS

if (segmentToDelete) {
if (deleteSegment(paragraph, segmentToDelete, context.formatContext, direction)) {
context.deleteResult = DeleteResult.SingleChar;
context.deleteResult = 'singleChar';

// It is possible that we have deleted everything from this paragraph, so we need to mark it as not implicit
// to avoid losing its format. See https://github.com/microsoft/roosterjs/issues/1953
Expand All @@ -35,7 +34,7 @@ function getDeleteCollapsedSelection(direction: 'forward' | 'backward'): DeleteS
if (siblingSegment) {
// When selection is under general segment, need to check if it has a sibling sibling, and delete from it
if (deleteSegment(block, siblingSegment, context.formatContext, direction)) {
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
} else {
if (isForward) {
Expand All @@ -50,7 +49,7 @@ function getDeleteCollapsedSelection(direction: 'forward' | 'backward'): DeleteS
delete block.cachedElement;
}

context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}

// When go across table, getLeafSiblingBlock will return null, when we are here, we must be in the same table context
Expand All @@ -65,14 +64,14 @@ function getDeleteCollapsedSelection(direction: 'forward' | 'backward'): DeleteS
direction
)
) {
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
}
} else {
// We have nothing to delete, in this case we don't want browser handle it as well.
// Because when Backspace on an empty document, it will also delete the only DIV and SPAN element, causes
// editor is really empty. We don't want that happen. So the handling should stop here.
context.deleteResult = DeleteResult.NothingToDelete;
context.deleteResult = 'nothingToDelete';
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DeleteResult } from '../utils/DeleteSelectionStep';
import { isPunctuation, isSpace, normalizeText } from '../../../domUtils/stringUtil';
import { isWhiteSpacePreserved } from 'roosterjs-content-model-dom';
import type { ContentModelParagraph } from 'roosterjs-content-model-types';
Expand Down Expand Up @@ -124,7 +123,7 @@ function* iterateSegments(
newText = normalizeText(newText, forward);
}

context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';

if (newText) {
segment.text = newText;
Expand Down Expand Up @@ -155,7 +154,7 @@ function* iterateSegments(
i -= step;
}

context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,80 @@ import type { TableSelectionContext } from '../../../publicTypes/selection/Table

/**
* @internal
* Delete selection result
*/
export const enum DeleteResult {
NotDeleted,
SingleChar,
Range,
NothingToDelete,
}
export type DeleteResult =
/**
* Content Model could not finish deletion, need to let browser handle it
*/
| 'notDeleted'

/**
* Deleted a single char, no need to let browser keep handling
*/
| 'singleChar'

/**
* Deleted a range, no need to let browser keep handling
*/
| 'range'

/**
* There is nothing to delete, no need to let browser keep handling
*/
| 'nothingToDelete';

/**
* @internal
* Result of deleteSelection API
*/
export interface DeleteSelectionResult {
/**
* Insert point position after delete, or null if there is no insert point
*/
insertPoint: InsertPoint | null;

/**
* Delete result
*/
deleteResult: DeleteResult;
}

/**
* @internal
* A context object used by DeleteSelectionStep
*/
export interface DeleteSelectionContext extends DeleteSelectionResult {
/**
* Last paragraph after previous step
*/
lastParagraph?: ContentModelParagraph;

/**
* Last table context after previous step
*/
lastTableContext?: TableSelectionContext;

/**
* Format context provided by formatContentModel API
*/
formatContext?: FormatWithContentModelContext;
}

/**
* @internal
* DeleteSelectionContext with a valid insert point that can be handled by next step
*/
export interface ValidDeleteSelectionContext extends DeleteSelectionContext {
/**
* Insert point position after delete
*/
insertPoint: InsertPoint;
}

/**
* @internal
* Represents a step function for deleteSelection API
* @param context The valid delete selection context object returned from previous step
*/
export type DeleteSelectionStep = (context: ValidDeleteSelectionContext) => void;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createInsertPoint } from '../utils/createInsertPoint';
import { deleteBlock } from '../utils/deleteBlock';
import { DeleteResult } from '../utils/DeleteSelectionStep';
import { deleteSegment } from '../utils/deleteSegment';
import { iterateSelections } from '../../selection/iterateSelections';
import type { ContentModelDocument } from 'roosterjs-content-model-types';
Expand Down Expand Up @@ -30,7 +29,7 @@ export function deleteExpandedSelection(
formatContext?: FormatWithContentModelContext
): DeleteSelectionContext {
const context: DeleteSelectionContext = {
deleteResult: DeleteResult.NotDeleted,
deleteResult: 'notDeleted',
insertPoint: null,
formatContext,
};
Expand Down Expand Up @@ -75,14 +74,14 @@ export function deleteExpandedSelection(
tableContext
);
} else if (deleteSegment(block, segment, context.formatContext)) {
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
});

// Since we are operating on this paragraph and it possible we delete everything from this paragraph,
// Need to make it "not implicit" so that it will always have a container element, so that when we do normalization
// of this paragraph, a BR can be added if need
if (context.deleteResult == DeleteResult.Range) {
if (context.deleteResult == 'range') {
setParagraphNotImplicit(block);
}
}
Expand All @@ -91,7 +90,7 @@ export function deleteExpandedSelection(
const blocks = path[0].blocks;

if (deleteBlock(blocks, block, paragraph, context.formatContext)) {
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}
} else if (tableContext) {
// Delete a whole table cell
Expand All @@ -105,7 +104,7 @@ export function deleteExpandedSelection(

delete cell.cachedElement;
delete row.cachedElement;
context.deleteResult = DeleteResult.Range;
context.deleteResult = 'range';
}

if (!context.insertPoint) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DeleteResult } from '../edit/utils/DeleteSelectionStep';
import { deleteSelection } from '../edit/deleteSelection';
import { getClosestAncestorBlockGroupIndex } from '../common/getClosestAncestorBlockGroupIndex';
import { setSelection } from '../selection/setSelection';
Expand Down Expand Up @@ -40,7 +39,7 @@ export function insertEntityModel(
} else if ((deleteResult = deleteSelection(model, [], context)).insertPoint) {
const { marker, paragraph, path } = deleteResult.insertPoint;

if (deleteResult.deleteResult == DeleteResult.Range) {
if (deleteResult.deleteResult == 'range') {
normalizeContentModel(model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DeleteResult } from '../../modelApi/edit/utils/DeleteSelectionStep';
import { deleteSelection } from '../../modelApi/edit/deleteSelection';
import { isBlockElement, isNodeOfType, normalizeContentModel } from 'roosterjs-content-model-dom';
import type { ContentModelSegmentFormat } from 'roosterjs-content-model-types';
Expand Down Expand Up @@ -40,13 +39,13 @@ export function applyDefaultFormat(
editor.formatContentModel((model, context) => {
const result = deleteSelection(model, [], context);

if (result.deleteResult == DeleteResult.Range) {
if (result.deleteResult == 'range') {
normalizeContentModel(model);
editor.addUndoSnapshot();

return true;
} else if (
result.deleteResult == DeleteResult.NotDeleted &&
result.deleteResult == 'notDeleted' &&
result.insertPoint &&
posContainer &&
posOffset !== null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChangeSource } from '../../publicTypes/event/ContentModelContentChangedEvent';
import { deleteAllSegmentBefore } from '../../modelApi/edit/deleteSteps/deleteAllSegmentBefore';
import { DeleteResult } from '../../modelApi/edit/utils/DeleteSelectionStep';
import { deleteSelection } from '../../modelApi/edit/deleteSelection';
import { isModifierKey } from '../../domUtils/eventUtils';
import { isNodeOfType } from 'roosterjs-content-model-dom';
Expand Down Expand Up @@ -44,7 +43,7 @@ export default function keyboardDelete(
context
).deleteResult;

isDeleted = result != DeleteResult.NotDeleted;
isDeleted = result != 'notDeleted';

return handleKeyboardEventResult(editor, model, rawEvent, result, context);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as PasteFile from '../../../lib/publicApi/utils/paste';
import { ContentModelDocument, DOMSelection } from 'roosterjs-content-model-types';
import { createModelToDomContext } from 'roosterjs-content-model-dom';
import { createRange } from 'roosterjs-editor-dom';
import { DeleteResult } from '../../../lib/modelApi/edit/utils/DeleteSelectionStep';
import { IContentModelEditor } from '../../../lib/publicTypes/IContentModelEditor';
import { setEntityElementClasses } from 'roosterjs-content-model-dom/test/domUtils/entityUtilTest';
import {
Expand Down Expand Up @@ -449,7 +448,7 @@ describe('ContentModelCopyPastePlugin |', () => {
};

spyOn(deleteSelectionsFile, 'deleteSelection').and.returnValue({
deleteResult: DeleteResult.Range,
deleteResult: 'range',
insertPoint: null!,
});
spyOn(contentModelToDomFile, 'contentModelToDom').and.callFake(() => {
Expand Down Expand Up @@ -501,7 +500,7 @@ describe('ContentModelCopyPastePlugin |', () => {
};

spyOn(deleteSelectionsFile, 'deleteSelection').and.returnValue({
deleteResult: DeleteResult.Range,
deleteResult: 'range',
insertPoint: null!,
});
spyOn(contentModelToDomFile, 'contentModelToDom').and.callFake(() => {
Expand Down
Loading
Loading