-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add background to maintain selection highlight
- Loading branch information
1 parent
be7889d
commit 3e9397d
Showing
8 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/roosterjs-content-model-core/lib/coreApi/setContentModel/persistHighlight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { DOMSelection, EditorCore } from 'roosterjs-content-model-types'; | ||
|
||
const SelectionClassName = '__persistedSelection'; | ||
/** | ||
* @internal | ||
* Shim class to pass TS interpreter if the TS version does not have context of Highlight API | ||
*/ | ||
declare class Highlight { | ||
constructor(textRange: Range); | ||
} | ||
|
||
/** | ||
* @internal | ||
* Shim interface to pass TS interpreter if the TS version does not have context of Highlight API | ||
*/ | ||
interface WindowWithHighlight extends Window { | ||
Highlight: typeof Highlight; | ||
} | ||
|
||
/** | ||
* @internal | ||
* Shim class for HighlightRegistry to pass TS interpreter | ||
*/ | ||
interface HighlightRegistryWithMap extends HighlightRegistry { | ||
set(name: string, highlight: Highlight): void; | ||
delete(name: string): void; | ||
} | ||
|
||
interface HighlightRegistry {} | ||
|
||
interface CSSShim { | ||
highlights: HighlightRegistry; | ||
} | ||
|
||
declare const CSS: CSSShim; | ||
|
||
/** | ||
* @internal | ||
* @param win current window that Highlight is being used. | ||
* @returns boolean indicates if Highlight api is available | ||
*/ | ||
function isHighlightRegistryWithMap( | ||
highlight: HighlightRegistry | ||
): highlight is HighlightRegistryWithMap { | ||
return !!(highlight as HighlightRegistryWithMap).set; | ||
} | ||
|
||
/** | ||
* @internal | ||
* @param win current window that Highlight is being used. | ||
* @returns boolean indicates if Highlight api is available | ||
*/ | ||
export function isWindowWithHighlight(win: Window): win is WindowWithHighlight { | ||
return !!(win as WindowWithHighlight).Highlight; | ||
} | ||
|
||
/** | ||
* @internal | ||
* Persist highlight of a indicated selection object | ||
* @param core The editor core object | ||
* @param shouldMaintainSelection The flag indicate if the selection should be persisted | ||
* @param selection The selection object that needs to be persisted. | ||
*/ | ||
export function persistHighlight( | ||
core: EditorCore, | ||
shouldMaintainSelection: boolean, | ||
selection: DOMSelection | null | ||
) { | ||
const currentWindow = core.logicalRoot.ownerDocument.defaultView; | ||
|
||
if ( | ||
currentWindow && | ||
isWindowWithHighlight(currentWindow) && | ||
isHighlightRegistryWithMap(CSS.highlights) | ||
) { | ||
if (shouldMaintainSelection) { | ||
if (selection && selection.type == 'range') { | ||
const highlight = new currentWindow.Highlight(selection.range); | ||
CSS.highlights.set(SelectionClassName, highlight); | ||
} | ||
} else { | ||
CSS.highlights.delete(SelectionClassName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters