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 2 shadow edit issue #2356

Merged
merged 3 commits into from
Jan 25, 2024
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 @@ -36,7 +36,7 @@ export const setContentModel: SetContentModel = (core, model, option, onNodeCrea

if (!option?.ignoreSelection && selection) {
core.api.setDOMSelection(core, selection);
} else if (!selection || selection.type !== 'range') {
} else {
core.selection.selection = selection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import * as createModelToDomContext from 'roosterjs-content-model-dom/lib/modelT
import { setContentModel } from '../../lib/coreApi/setContentModel';
import { StandaloneEditorCore } from 'roosterjs-content-model-types';

const mockedRange = {
type: 'image',
} as any;
const mockedDoc = 'DOCUMENT' as any;
const mockedModel = 'MODEL' as any;
const mockedEditorContext = 'EDITORCONTEXT' as any;
Expand All @@ -23,9 +20,7 @@ describe('setContentModel', () => {
let getDOMSelectionSpy: jasmine.Spy;

beforeEach(() => {
contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom').and.returnValue(
mockedRange
);
contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom');
createEditorContext = jasmine
.createSpy('createEditorContext')
.and.returnValue(mockedEditorContext);
Expand Down Expand Up @@ -56,6 +51,12 @@ describe('setContentModel', () => {
});

it('no default option, no shadow edit', () => {
const mockedRange = {
type: 'image',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

setContentModel(core, mockedModel);

expect(createModelToDomContextSpy).not.toHaveBeenCalled();
Expand All @@ -76,6 +77,12 @@ describe('setContentModel', () => {
});

it('with default option, no shadow edit', () => {
const mockedRange = {
type: 'image',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

setContentModel(core, mockedModel);

expect(createModelToDomContextWithConfigSpy).toHaveBeenCalledWith(
Expand All @@ -95,6 +102,11 @@ describe('setContentModel', () => {
it('with default option, no shadow edit, with additional option', () => {
const defaultOption = { o: 'OPTION' } as any;
const additionalOption = { o: 'OPTION1', o2: 'OPTION2' } as any;
const mockedRange = {
type: 'image',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

core.modelToDomSettings.builtIn = defaultOption;
setContentModel(core, mockedModel, additionalOption);
Expand All @@ -117,6 +129,11 @@ describe('setContentModel', () => {

it('no default option, with shadow edit', () => {
core.lifecycle.shadowEditFragment = {} as any;
const mockedRange = {
type: 'image',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

setContentModel(core, mockedModel);

Expand All @@ -135,6 +152,12 @@ describe('setContentModel', () => {
});

it('restore selection ', () => {
const mockedRange = {
type: 'image',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

core.selection = {
selection: null,
selectionStyleNode: null,
Expand All @@ -161,4 +184,68 @@ describe('setContentModel', () => {
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(mockedRange);
});

it('restore range selection ', () => {
const mockedRange = {
type: 'range',
} as any;

contentModelToDomSpy.and.returnValue(mockedRange);

core.selection = {
selection: null,
selectionStyleNode: null,
};
setContentModel(core, mockedModel, {
ignoreSelection: true,
});

expect(createModelToDomContextSpy).toHaveBeenCalledWith(
mockedEditorContext,
undefined,
undefined,
{
ignoreSelection: true,
}
);
expect(contentModelToDomSpy).toHaveBeenCalledWith(
mockedDoc,
mockedDiv,
mockedModel,
mockedContext,
undefined
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(mockedRange);
});

it('restore null selection ', () => {
contentModelToDomSpy.and.returnValue(null);

core.selection = {
selection: null,
selectionStyleNode: null,
};
setContentModel(core, mockedModel, {
ignoreSelection: true,
});

expect(createModelToDomContextSpy).toHaveBeenCalledWith(
mockedEditorContext,
undefined,
undefined,
{
ignoreSelection: true,
}
);
expect(contentModelToDomSpy).toHaveBeenCalledWith(
mockedDoc,
mockedDiv,
mockedModel,
mockedContext,
undefined
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import { handleKeyDownEvent } from './keyUtils/handleKeyDownEvent';
import { handleKeyUpEvent } from './keyUtils/handleKeyUpEvent';
import { handleMouseDownEvent } from './mouseUtils/handleMouseDownEvent';
import { handleScrollEvent } from './mouseUtils/handleScrollEvent';
import { PluginEventType, SelectionRangeTypes } from 'roosterjs-editor-types';
import { PluginEventType } from 'roosterjs-editor-types';
import type { TableCellSelectionState } from './TableCellSelectionState';
import type { EditorPlugin, IEditor, PluginEvent, TableSelection } from 'roosterjs-editor-types';
import type { EditorPlugin, IEditor, PluginEvent } from 'roosterjs-editor-types';

/**
* TableCellSelectionPlugin help highlight table cells
*/
export default class TableCellSelection implements EditorPlugin {
private editor: IEditor | null = null;
private state: TableCellSelectionState | null;
private shadowEditCoordinatesBackup: TableSelection | null = null;

constructor() {
this.state = {
Expand Down Expand Up @@ -62,12 +61,6 @@ export default class TableCellSelection implements EditorPlugin {
onPluginEvent(event: PluginEvent) {
if (this.editor && this.state) {
switch (event.eventType) {
case PluginEventType.EnteredShadowEdit:
this.handleEnteredShadowEdit(this.state, this.editor);
break;
case PluginEventType.LeavingShadowEdit:
this.handleLeavingShadowEdit(this.state, this.editor);
break;
case PluginEventType.MouseDown:
if (!this.state.startedSelection) {
handleMouseDownEvent(event, this.state, this.editor);
Expand Down Expand Up @@ -100,25 +93,4 @@ export default class TableCellSelection implements EditorPlugin {
}
}
}

private handleLeavingShadowEdit(state: TableCellSelectionState, editor: IEditor) {
if (state.firstTable && state.tableSelection && state.firstTable) {
const table = editor.queryElements('#' + state.firstTable.id);
if (table.length == 1) {
state.firstTable = table[0] as HTMLTableElement;
editor.select(state.firstTable, this.shadowEditCoordinatesBackup);
this.shadowEditCoordinatesBackup = null;
}
}
}

private handleEnteredShadowEdit(state: TableCellSelectionState, editor: IEditor) {
const selection = editor.getSelectionRangeEx();
if (selection.type == SelectionRangeTypes.TableSelection) {
this.shadowEditCoordinatesBackup = selection.coordinates ?? null;
state.firstTable = selection.table;
state.tableSelection = true;
editor.select(selection.table, null);
}
}
}
Loading