Skip to content

Commit 7fbcb71

Browse files
authored
Fix #2984 Hide caret for shadow edit (#2990)
* Fix #2984 * fix test * fix build
1 parent 3cf50a4 commit 7fbcb71

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

packages/roosterjs-content-model-core/lib/coreApi/setDOMSelection/setDOMSelection.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { areSameSelections } from '../../corePlugin/cache/areSameSelections';
33
import { ensureUniqueId } from '../setEditorStyle/ensureUniqueId';
44
import { findLastedCoInMergedCell } from './findLastedCoInMergedCell';
55
import { findTableCellElement } from './findTableCellElement';
6+
import { toggleCaret } from './toggleCaret';
67
import {
78
getSafeIdSelector,
89
isNodeOfType,
@@ -17,11 +18,9 @@ import type {
1718
} from 'roosterjs-content-model-types';
1819

1920
const DOM_SELECTION_CSS_KEY = '_DOMSelection';
20-
const HIDE_CURSOR_CSS_KEY = '_DOMSelectionHideCursor';
2121
const HIDE_SELECTION_CSS_KEY = '_DOMSelectionHideSelection';
2222
const IMAGE_ID = 'image';
2323
const TABLE_ID = 'table';
24-
const CARET_CSS_RULE = 'caret-color: transparent';
2524
const TRANSPARENT_SELECTION_CSS_RULE = 'background-color: transparent !important;';
2625
const SELECTION_SELECTOR = '*::selection';
2726
const DEFAULT_SELECTION_BORDER_COLOR = '#DB626C';
@@ -44,9 +43,10 @@ export const setDOMSelection: SetDOMSelection = (core, selection, skipSelectionC
4443
const isDarkMode = core.lifecycle.isDarkMode;
4544
core.selection.skipReselectOnFocus = true;
4645
core.api.setEditorStyle(core, DOM_SELECTION_CSS_KEY, null /*cssRule*/);
47-
core.api.setEditorStyle(core, HIDE_CURSOR_CSS_KEY, null /*cssRule*/);
4846
core.api.setEditorStyle(core, HIDE_SELECTION_CSS_KEY, null /*cssRule*/);
4947

48+
toggleCaret(core, false /* hide */);
49+
5050
try {
5151
switch (selection?.type) {
5252
case 'image':
@@ -137,14 +137,15 @@ export const setDOMSelection: SetDOMSelection = (core, selection, skipSelectionC
137137
`background-color:${tableSelectionColor}!important;`,
138138
tableSelectors
139139
);
140-
core.api.setEditorStyle(core, HIDE_CURSOR_CSS_KEY, CARET_CSS_RULE);
141140
core.api.setEditorStyle(
142141
core,
143142
HIDE_SELECTION_CSS_KEY,
144143
TRANSPARENT_SELECTION_CSS_RULE,
145144
[SELECTION_SELECTOR]
146145
);
147146

147+
toggleCaret(core, true /* hide */);
148+
148149
const nodeToSelect = firstCell.cell?.firstElementChild || firstCell.cell;
149150

150151
if (nodeToSelect) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { EditorCore } from 'roosterjs-content-model-types';
2+
3+
const CARET_CSS_RULE = 'caret-color: transparent';
4+
const HIDE_CURSOR_CSS_KEY = '_DOMSelectionHideCursor';
5+
6+
/**
7+
* @internal Show/Hide caret in editor
8+
* @param core The editor core
9+
* @param isHiding True to hide caret, false to show caret
10+
*/
11+
export function toggleCaret(core: EditorCore, isHiding: boolean) {
12+
core.api.setEditorStyle(core, HIDE_CURSOR_CSS_KEY, isHiding ? CARET_CSS_RULE : null);
13+
}

packages/roosterjs-content-model-core/lib/coreApi/switchShadowEdit/switchShadowEdit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { iterateSelections, moveChildNodes } from 'roosterjs-content-model-dom';
2+
import { toggleCaret } from '../setDOMSelection/toggleCaret';
23
import type { SwitchShadowEdit } from 'roosterjs-content-model-types';
34

45
/**
@@ -32,10 +33,14 @@ export const switchShadowEdit: SwitchShadowEdit = (editorCore, isOn): void => {
3233
core.cache.cachedModel = model;
3334
}
3435

36+
toggleCaret(core, true /* hide */);
37+
3538
core.lifecycle.shadowEditFragment = fragment;
3639
} else {
3740
core.lifecycle.shadowEditFragment = null;
3841

42+
toggleCaret(core, false /* hide */);
43+
3944
core.api.triggerEvent(
4045
core,
4146
{

packages/roosterjs-content-model-core/test/coreApi/switchShadowEdit/switchShadowEditTest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as iterateSelections from 'roosterjs-content-model-dom/lib/modelApi/selection/iterateSelections';
2+
import * as toggleCaret from '../../../lib/coreApi/setDOMSelection/toggleCaret';
23
import { EditorCore } from 'roosterjs-content-model-types';
34
import { switchShadowEdit } from '../../../lib/coreApi/switchShadowEdit/switchShadowEdit';
45

@@ -11,12 +12,14 @@ describe('switchShadowEdit', () => {
1112
let setContentModel: jasmine.Spy;
1213
let getSelectionRange: jasmine.Spy;
1314
let triggerEvent: jasmine.Spy;
15+
let toggleCaretSpy: jasmine.Spy;
1416

1517
beforeEach(() => {
1618
createContentModel = jasmine.createSpy('createContentModel').and.returnValue(mockedModel);
1719
setContentModel = jasmine.createSpy('setContentModel');
1820
getSelectionRange = jasmine.createSpy('getSelectionRange');
1921
triggerEvent = jasmine.createSpy('triggerEvent');
22+
toggleCaretSpy = spyOn(toggleCaret, 'toggleCaret');
2023

2124
const contentDiv = document.createElement('div');
2225

@@ -50,6 +53,7 @@ describe('switchShadowEdit', () => {
5053
},
5154
false
5255
);
56+
expect(toggleCaretSpy).toHaveBeenCalledWith(core, true);
5357
});
5458

5559
it('with cache, isOn', () => {
@@ -69,6 +73,7 @@ describe('switchShadowEdit', () => {
6973
},
7074
false
7175
);
76+
expect(toggleCaretSpy).toHaveBeenCalledWith(core, true);
7277
});
7378

7479
it('no cache, isOff', () => {
@@ -79,6 +84,7 @@ describe('switchShadowEdit', () => {
7984
expect(core.cache.cachedModel).toBe(undefined);
8085

8186
expect(triggerEvent).not.toHaveBeenCalled();
87+
expect(toggleCaretSpy).not.toHaveBeenCalled();
8288
});
8389

8490
it('with cache, isOff', () => {
@@ -91,6 +97,7 @@ describe('switchShadowEdit', () => {
9197
expect(core.cache.cachedModel).toBe(mockedCachedModel);
9298

9399
expect(triggerEvent).not.toHaveBeenCalled();
100+
expect(toggleCaretSpy).not.toHaveBeenCalled();
94101
});
95102
});
96103

@@ -107,6 +114,7 @@ describe('switchShadowEdit', () => {
107114
expect(core.cache.cachedModel).toBe(undefined);
108115

109116
expect(triggerEvent).not.toHaveBeenCalled();
117+
expect(toggleCaretSpy).not.toHaveBeenCalled();
110118
});
111119

112120
it('with cache, isOn', () => {
@@ -119,6 +127,7 @@ describe('switchShadowEdit', () => {
119127
expect(core.cache.cachedModel).toBe(mockedCachedModel);
120128

121129
expect(triggerEvent).not.toHaveBeenCalled();
130+
expect(toggleCaretSpy).not.toHaveBeenCalled();
122131
});
123132

124133
it('no cache, isOff', () => {
@@ -136,6 +145,7 @@ describe('switchShadowEdit', () => {
136145
},
137146
false
138147
);
148+
expect(toggleCaretSpy).toHaveBeenCalledWith(core, false);
139149
});
140150

141151
it('with cache, isOff', () => {
@@ -159,6 +169,7 @@ describe('switchShadowEdit', () => {
159169
},
160170
false
161171
);
172+
expect(toggleCaretSpy).toHaveBeenCalledWith(core, false);
162173
});
163174
});
164175
});

0 commit comments

Comments
 (0)