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

Delete table with backspace, if whole table is selected. #1583

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -48,6 +48,7 @@ const EditFeatureDescriptionMap: Record<keyof ContentEditFeatureSettings, string
autoNumberingList:
'When press space after an number, a letter or roman number followed by ), ., -, or between parenthesis in an empty line, toggle numbering',
mergeListOnBackspaceAfterList: 'When backspacing between lists, merge the lists',
deleteTableWithBackspace: 'Delete table with backspace key with whole table is selected',
};

export interface ContentEditFeaturessProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const FeatureNames: Partial<Record<ExperimentalFeatures, string>> = {
[ExperimentalFeatures.DefaultFormatInSpan]:
'When apply default format when initialize or user type, apply the format on a SPAN element.',
[ExperimentalFeatures.VariableBasedDarkColor]: 'Use variable-based color for dark mode',
[ExperimentalFeatures.DeleteTableWithBackspace]:
'Delete a table selected with the table selector pressing Backspace key',
};

export default class ExperimentalFeaturesPane extends React.Component<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SelectionRangeTypes,
TableSelectionRange,
Indentation,
ExperimentalFeatures,
} from 'roosterjs-editor-types';
import {
Browser,
Expand Down Expand Up @@ -151,6 +152,23 @@ const UpDownInTable: BuildInEditFeature<PluginKeyboardEvent> = {
defaultDisabled: !Browser.isChrome && !Browser.isSafari,
};

/**
* Requires @see ExperimentalFeatures.DeleteTableWithBackspace
* Delete a table selected with the table selector pressing Backspace key
*/
const DeleteTableWithBackspace: BuildInEditFeature<PluginKeyboardEvent> = {
keys: [Keys.BACKSPACE],
shouldHandleEvent: (event: PluginKeyboardEvent, editor: IEditor) =>
cacheIsWholeTableSelected(event, editor) &&
editor.isFeatureEnabled(ExperimentalFeatures.DeleteTableWithBackspace),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check editor.isFeatureEnabled(ExperimentalFeatures.DeleteTableWithBackspace), first? Since it could take less time that trying to create a VTable and check if it is selected

handleEvent: (event, editor) => {
const td = cacheGetTableCell(event, editor);
const vtable = new VTable(td);
vtable.edit(TableOperation.DeleteTable);
vtable.writeBack();
},
};

function cacheGetTableCell(event: PluginEvent, editor: IEditor): HTMLTableCellElement {
return cacheGetEventData(event, 'TABLE_CELL_FOR_TABLE_FEATURES', () => {
let pos = editor.getFocusedPosition();
Expand Down Expand Up @@ -189,4 +207,5 @@ export const TableFeatures: Record<
tabInTable: TabInTable,
upDownInTable: UpDownInTable,
indentTableOnTab: IndentTableOnTab,
deleteTableWithBackspace: DeleteTableWithBackspace,
};
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('TableFeature', () => {
expect(!!shouldHandleEvent).toBeFalsy();
});
});

describe('HandleEvent', () => {
let setIndentationFn: jasmine.Spy;

Expand Down Expand Up @@ -338,4 +339,48 @@ describe('TableFeature', () => {
});
});
});

describe('deleteTable | ', () => {
const feature = TableFeatures.deleteTableWithBackspace;

describe('ShouldHandle', () => {
it('Should not handle, is not in a table', () => {
editor.setContent(`<span id="${TEST_ELEMENT_ID}"><span>`);
editor.focus();
editor.select(document.getElementById('TEST_ELEMENT_ID')!, 0);
const shouldHandleEvent = feature.shouldHandleEvent(keyboardEvent, editor, false);
expect(!!shouldHandleEvent).toBeFalsy();
});
it('Should handle, table is fully selected', () => {
editor.select(table!, <TableSelection>{
firstCell: { x: 0, y: 0 },
lastCell: { y: 1, x: 1 },
});
spyOn(editor, 'isFeatureEnabled').and.returnValue(true);
const shouldHandleEvent = feature.shouldHandleEvent(keyboardEvent, editor, false);
expect(!!shouldHandleEvent).toBeTruthy();
});
it('Should not handle, table is not fully selected', () => {
editor.select(table!, <TableSelection>{
firstCell: { x: 0, y: 0 },
lastCell: { y: 0, x: 1 },
});
const shouldHandleEvent = feature.shouldHandleEvent(keyboardEvent, editor, false);
expect(!!shouldHandleEvent).toBeFalsy();
});
});

describe('HandleEvent', () => {
it('Should delete table', () => {
editor.select(table!, <TableSelection>{
firstCell: { x: 0, y: 0 },
lastCell: { y: 1, x: 1 },
});

feature.handleEvent(keyboardEvent, editor);
const deletedTable = document.getElementById('TEST_ELEMENT_ID');
expect(deletedTable).toBe(null);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ export const enum ExperimentalFeatures {
* if you need them work for dark mode
*/
VariableBasedDarkColor = 'VariableBasedDarkColor',

/**
* Delete table with Backspace key with the whole was selected with table selector
*/
DeleteTableWithBackspace = 'DeleteTableWithBackspace',
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ export interface TableFeatureSettings {
* IndentTableOnTab edit feature, provides the ability to indent the table if it is all cells are selected.
*/
indentTableOnTab: boolean;

/**
* Requires @see ExperimentalFeatures.DeleteTableWithBackspace
* Delete a table selected with the table selector pressing Backspace key
*/
deleteTableWithBackspace: boolean;
}

/**
Expand Down