Skip to content

Commit

Permalink
Merge pull request #2248 from microsoft/u/juliaroldi/list-delete
Browse files Browse the repository at this point in the history
Remove whole list when delete
  • Loading branch information
juliaroldi authored Dec 11, 2023
2 parents 870d387 + 8887015 commit 9ca3d06
Show file tree
Hide file tree
Showing 27 changed files with 1,161 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { BlockGroupContentView } from './BlockGroupContentView';
import { ContentModelDocument } from 'roosterjs-content-model-types';
import { ContentModelView } from '../ContentModelView';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-api';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-core';

const styles = require('./ContentModelDocumentView.scss');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentModelView } from '../ContentModelView';
import { DisplayFormatRenderer } from '../format/formatPart/DisplayFormatRenderer';
import { FormatRenderer } from '../format/utils/FormatRenderer';
import { FormatView } from '../format/FormatView';
import { hasSelectionInBlock } from 'roosterjs-content-model-api';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';
import { SegmentFormatView } from '../format/SegmentFormatView';
import { SizeFormatRenderers } from '../format/formatPart/SizeFormatRenderers';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BlockGroupContentView } from './BlockGroupContentView';
import { ContentModelCodeView } from './ContentModelCodeView';
import { ContentModelLinkView } from './ContentModelLinkView';
import { ContentModelView } from '../ContentModelView';
import { hasSelectionInBlock } from 'roosterjs-content-model-api';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';
import { SegmentFormatView } from '../format/SegmentFormatView';
import {
ContentModelGeneralBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FontFamilyFormatRenderer } from '../format/formatPart/FontFamilyFormatR
import { FontSizeFormatRenderer } from '../format/formatPart/FontSizeFormatRenderer';
import { FormatRenderer } from '../format/utils/FormatRenderer';
import { FormatView } from '../format/FormatView';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-api';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-core';
import { LineHeightFormatRenderer } from '../format/formatPart/LineHeightFormatRenderer';
import { MarginFormatRenderer } from '../format/formatPart/MarginFormatRenderer';
import { TextAlignFormatRenderer } from '../format/formatPart/TextAlignFormatRenderer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { BlockFormatView } from '../format/BlockFormatView';
import { ContentModelSegmentView } from './ContentModelSegmentView';
import { ContentModelView } from '../ContentModelView';
import { hasSelectionInBlock } from 'roosterjs-content-model-api';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';
import { SegmentFormatView } from '../format/SegmentFormatView';
import { useProperty } from '../../hooks/useProperty';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import { ContentModelView } from '../ContentModelView';
import { DirectionFormatRenderer } from '../format/formatPart/DirectionFormatRenderer';
import { FormatRenderer } from '../format/utils/FormatRenderer';
import { FormatView } from '../format/FormatView';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-api';
import { hasSelectionInBlockGroup, updateTableCellMetadata } from 'roosterjs-content-model-core';
import { HtmlAlignFormatRenderer } from '../format/formatPart/HtmlAlignFormatRenderer';
import { MetadataView } from '../format/MetadataView';
import { PaddingFormatRenderer } from '../format/formatPart/PaddingFormatRenderer';
import { SizeFormatRenderers } from '../format/formatPart/SizeFormatRenderers';
import { TableCellMetadataFormatRenders } from '../format/formatPart/TableCellMetadataFormatRenders';
import { TextAlignFormatRenderer } from '../format/formatPart/TextAlignFormatRenderer';
import { TextColorFormatRenderer } from '../format/formatPart/TextColorFormatRenderer';
import { updateTableCellMetadata } from 'roosterjs-content-model-core';
import { useProperty } from '../../hooks/useProperty';
import { VerticalAlignFormatRenderer } from '../format/formatPart/VerticalAlignFormatRenderer';
import { WordBreakFormatRenderer } from '../format/formatPart/WordBreakFormatRenderer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentModelBlockGroupView } from './ContentModelBlockGroupView';
import { ContentModelView } from '../ContentModelView';
import { FormatRenderer } from '../format/utils/FormatRenderer';
import { FormatView } from '../format/FormatView';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-api';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-core';
import { useProperty } from '../../hooks/useProperty';

const styles = require('./ContentModelTableRowView.scss');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ContentModelView } from '../ContentModelView';
import { DisplayFormatRenderer } from '../format/formatPart/DisplayFormatRenderer';
import { FormatRenderer } from '../format/utils/FormatRenderer';
import { FormatView } from '../format/FormatView';
import { hasSelectionInBlock } from 'roosterjs-content-model-api';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';
import { IdFormatRenderer } from '../format/formatPart/IdFormatRenderer';
import { MarginFormatRenderer } from '../format/formatPart/MarginFormatRenderer';
import { MetadataView } from '../format/MetadataView';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export { default as changeCapitalization } from './publicApi/segment/changeCapit
export { default as insertImage } from './publicApi/image/insertImage';
export { default as setListStyle } from './publicApi/list/setListStyle';
export { default as setListStartNumber } from './publicApi/list/setListStartNumber';
export { default as hasSelectionInBlock } from './publicApi/selection/hasSelectionInBlock';
export { default as hasSelectionInSegment } from './publicApi/selection/hasSelectionInSegment';
export { default as hasSelectionInBlockGroup } from './publicApi/selection/hasSelectionInBlockGroup';
export { default as setIndentation } from './publicApi/block/setIndentation';
export { default as setAlignment } from './publicApi/block/setAlignment';
export { default as setDirection } from './publicApi/block/setDirection';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import hasSelectionInBlockGroup from '../../publicApi/selection/hasSelectionInBlockGroup';
import { hasSelectionInBlockGroup } from 'roosterjs-content-model-core';
import type { ContentModelTable } from 'roosterjs-content-model-types';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hasSelectionInBlock from '../selection/hasSelectionInBlock';
import { alignTable } from '../../modelApi/table/alignTable';
import { deleteTable } from '../../modelApi/table/deleteTable';
import { deleteTableColumn } from '../../modelApi/table/deleteTableColumn';
Expand All @@ -11,7 +10,9 @@ import { mergeTableColumn } from '../../modelApi/table/mergeTableColumn';
import { mergeTableRow } from '../../modelApi/table/mergeTableRow';
import { splitTableCellHorizontally } from '../../modelApi/table/splitTableCellHorizontally';
import { splitTableCellVertically } from '../../modelApi/table/splitTableCellVertically';

import {
hasSelectionInBlock,
applyTableFormat,
getFirstSelectedTable,
normalizeTable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hasSelectionInBlockGroup from '../selection/hasSelectionInBlockGroup';
import {
hasSelectionInBlockGroup,
getFirstSelectedTable,
normalizeTable,
setTableCellBackgroundColor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hasSelectionInBlock from '../../../lib/publicApi/selection/hasSelectionInBlock';
import { createTable, createTableCell } from 'roosterjs-content-model-dom';
import { deleteTableColumn } from '../../../lib/modelApi/table/deleteTableColumn';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';

describe('deleteTableColumn', () => {
it('empty table', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hasSelectionInBlock from '../../../lib/publicApi/selection/hasSelectionInBlock';
import { createTable, createTableCell } from 'roosterjs-content-model-dom';
import { deleteTableRow } from '../../../lib/modelApi/table/deleteTableRow';
import { hasSelectionInBlock } from 'roosterjs-content-model-core';

describe('deleteTableRow', () => {
it('empty table', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { addRangeToSelection } from './utils/addRangeToSelection';
import { ChangeSource } from '../constants/ChangeSource';
import { cloneModel } from '../publicApi/model/cloneModel';
import { ColorTransformDirection, PluginEventType } from 'roosterjs-editor-types';
import { deleteEmptyList } from './utils/deleteEmptyList';
import { deleteSelection } from '../publicApi/selection/deleteSelection';
import { extractClipboardItems } from 'roosterjs-editor-dom';
import { iterateSelections } from '../publicApi/selection/iterateSelections';
Expand Down Expand Up @@ -169,7 +170,10 @@ class ContentModelCopyPastePlugin implements PluginWithState<CopyPastePluginStat
if (isCut) {
editor.formatContentModel(
(model, context) => {
if (deleteSelection(model, [], context).deleteResult == 'range') {
if (
deleteSelection(model, [deleteEmptyList], context)
.deleteResult == 'range'
) {
normalizeContentModel(model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import hasSelectionInBlock from '../../publicApi/selection/hasSelectionInBlock';
import hasSelectionInBlockGroup from '../../publicApi/selection/hasSelectionInBlockGroup';
import { getClosestAncestorBlockGroupIndex } from '../../publicApi/model/getClosestAncestorBlockGroupIndex';
import type {
ContentModelBlock,
DeleteSelectionContext,
DeleteSelectionStep,
} from 'roosterjs-content-model-types';

function isEmptyBlock(block: ContentModelBlock | undefined): boolean {
if (block && block.blockType == 'Paragraph') {
return block.segments.every(
segment => segment.segmentType !== 'SelectionMarker' && segment.segmentType == 'Br'
);
}

if (block && block.blockType == 'BlockGroup') {
return block.blocks.every(isEmptyBlock);
}

return !!block;
}

/**
* @internal
* If the first item o the list is selected in a expanded selection, we need to remove the list item levels
* @param context A context object provided by formatContentModel API
*/
export const deleteEmptyList: DeleteSelectionStep = (context: DeleteSelectionContext) => {
const { insertPoint, deleteResult } = context;
if (deleteResult == 'range' && insertPoint?.path) {
const index = getClosestAncestorBlockGroupIndex(
insertPoint.path,
['ListItem'],
['TableCell']
);
const item = insertPoint.path[index];
if (index >= 0 && item && item.blockGroupType == 'ListItem') {
const listItemIndex = insertPoint.path[index + 1].blocks.indexOf(item);
const previousBlock =
listItemIndex > -1
? insertPoint.path[index + 1].blocks[listItemIndex - 1]
: undefined;
const nextBlock =
listItemIndex > -1
? insertPoint.path[index + 1].blocks[listItemIndex + 1]
: undefined;
if (
hasSelectionInBlockGroup(item) &&
(!previousBlock || hasSelectionInBlock(previousBlock)) &&
nextBlock &&
isEmptyBlock(nextBlock)
) {
item.levels = [];
}
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export { getSelectionRootNode } from './publicApi/selection/getSelectionRootNode
export { deleteSelection } from './publicApi/selection/deleteSelection';
export { deleteSegment } from './publicApi/selection/deleteSegment';
export { deleteBlock } from './publicApi/selection/deleteBlock';
export { default as hasSelectionInBlock } from './publicApi/selection/hasSelectionInBlock';
export { default as hasSelectionInSegment } from './publicApi/selection/hasSelectionInSegment';
export { default as hasSelectionInBlockGroup } from './publicApi/selection/hasSelectionInBlockGroup';
export {
OperationalBlocks,
getFirstSelectedListItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ export function deleteSelection(
const context = deleteExpandedSelection(model, formatContext);

additionalSteps.forEach(step => {
if (
step &&
isValidDeleteSelectionContext(context) &&
context.deleteResult == 'notDeleted'
) {
if (step && isValidDeleteSelectionContext(context)) {
step(context);
}
});

mergeParagraphAfterDelete(context);

return context;
}

Expand Down
Loading

0 comments on commit 9ca3d06

Please sign in to comment.