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

Add background to maintain selection highlight #2636

Closed

Conversation

FrancisMengx
Copy link
Contributor

@FrancisMengx FrancisMengx commented May 15, 2024

Changes

Add a new option in the setContentModel function to indicate maintaing selection UI even when the focus has moved away.

@FrancisMengx FrancisMengx force-pushed the u/xiameng/add-background-for-copilot-treatment branch from ef023d1 to b42f20a Compare May 16, 2024 18:56
@JiuqingSong
Copy link
Collaborator

Please fix test

@JiuqingSong
Copy link
Collaborator

Please add description

@JiuqingSong
Copy link
Collaborator

Question: how will this new feature be used? It seems the new option is not exposed to any public API?

@FrancisMengx FrancisMengx force-pushed the u/xiameng/add-background-for-copilot-treatment branch from cc77651 to 4ed3e67 Compare June 5, 2024 22:02
}
} else {
core.api.setEditorStyle(core, SelectionClassName, null /*rule*/);
CSS.highlights.delete(SelectionClassName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need to check "highlights in CSS" here?


if (option?.shouldMaintainSelection) {
if ('highlights' in CSS && Highlight) {
const selectionEl = document.querySelector(SelectionSelector);
Copy link
Collaborator

Choose a reason for hiding this comment

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

get document from core.physicalRoot to make it work for projection popout

@@ -15,6 +18,28 @@ import type { SetContentModel } from 'roosterjs-content-model-types';
*/
export const setContentModel: SetContentModel = (core, model, option, onNodeCreated) => {
const editorContext = core.api.createEditorContext(core, true /*saveIndex*/);

if (option?.shouldMaintainSelection) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This need to move down after line 57, and use the selection range from the return value, but not rely on CSS. otherwise what if there are multiple selected elements

@@ -64,7 +64,12 @@ export const formatContentModel: FormatContentModel = (
core.api.setContentModel(
core,
model,
hasFocus ? undefined : { ignoreSelection: true }, // If editor did not have focus before format, do not set focus after format
hasFocus
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be:

hasFocus || options?.shouldMaintainSelection ? {
 ignoreSelection: hasfocus, // ..... (move the comment here) 
 shouldMaintainSelection: options?.shouldMaintainSelection
} : undefined

@JiuqingSong
Copy link
Collaborator

please resolve the conflict and fix build

@FrancisMengx FrancisMengx force-pushed the u/xiameng/add-background-for-copilot-treatment branch from c1ceed2 to b49b845 Compare June 21, 2024 20:52
@FrancisMengx FrancisMengx force-pushed the u/xiameng/add-background-for-copilot-treatment branch from 46998fb to 3e9397d Compare June 21, 2024 21:11
@@ -15,6 +18,18 @@ import type { SetContentModel } from 'roosterjs-content-model-types';
*/
export const setContentModel: SetContentModel = (core, model, option, onNodeCreated) => {
const editorContext = core.api.createEditorContext(core, true /*saveIndex*/);
const currentWindow = core.logicalRoot.ownerDocument.defaultView;
if (currentWindow && isWindowWithHighlight(currentWindow)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel we should check "!" for isWindowWithHighlight(), we only need this CSS based solution when browser does not support highlight, right?

/**
* When pass to true, selection is maintained even when focus is moved out of editor.
*/
shouldMaintainSelection?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's use a simpler name, for example: highlightSelection

}

/**
* @internal
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: if it is not exported, no need to add "@internal"

Copy link
Collaborator

Choose a reason for hiding this comment

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

same for other places

@@ -31,6 +32,7 @@ export const formatContentModel: FormatContentModel = (
rawEvent,
selectionOverride,
scrollCaretIntoView: scroll,
shouldMaintainSelection,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, use a simpler name

@JiuqingSong JiuqingSong closed this Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants