-
Notifications
You must be signed in to change notification settings - Fork 167
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
Selection Management step 1 #247
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import EditorCore, { Select, SelectRange } from '../interfaces/EditorCore'; | ||
import hasFocus from './hasFocus'; | ||
import { Browser, contains, createRange } from 'roosterjs-editor-dom'; | ||
|
||
const selectRange: SelectRange = (core: EditorCore, range: Range, skipSameRange?: boolean) => { | ||
if (contains(core.contentDiv, range)) { | ||
let selection = core.document.defaultView.getSelection(); | ||
if (selection) { | ||
let needAddRange = true; | ||
|
||
if (selection.rangeCount > 0) { | ||
// Workaround IE exception 800a025e | ||
try { | ||
let currentRange: Range; | ||
// Do not remove/add range if current selection is the same with target range | ||
// Without this check, execCommand() may fail in Edge since we changed the selection | ||
if ( | ||
(skipSameRange || Browser.isEdge) && | ||
(currentRange = | ||
selection.rangeCount == 1 ? selection.getRangeAt(0) : null) && | ||
currentRange.startContainer == range.startContainer && | ||
currentRange.startOffset == range.startOffset && | ||
currentRange.endContainer == range.endContainer && | ||
currentRange.endOffset == range.endOffset | ||
) { | ||
needAddRange = false; | ||
} else { | ||
selection.removeAllRanges(); | ||
} | ||
} catch (e) {} | ||
} | ||
|
||
if (needAddRange) { | ||
selection.addRange(range); | ||
} | ||
|
||
if (!hasFocus(core)) { | ||
core.cachedSelectionRange = range; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export default selectRange; | ||
|
||
/** | ||
* @deprecated Only for compatibility with existing code, don't use ths function, use selectRange instead | ||
*/ | ||
export const select: Select = (core: EditorCore, arg1: any, arg2?: any, arg3?: any, arg4?: any) => { | ||
let range = arg1 instanceof Range ? arg1 : createRange(arg1, arg2, arg3, arg4); | ||
return core.api.selectRange(core, range); | ||
titrongt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import createEditorCore from './createEditorCore'; | ||
import EditorCore from '../interfaces/EditorCore'; | ||
import EditorOptions from '../interfaces/EditorOptions'; | ||
import { Browser, getRangeFromSelectionPath, getSelectionPath } from 'roosterjs-editor-dom'; | ||
import { GenericContentEditFeature } from '../interfaces/ContentEditFeature'; | ||
import { | ||
BlockElement, | ||
|
@@ -22,19 +21,23 @@ import { | |
Rect, | ||
} from 'roosterjs-editor-types'; | ||
import { | ||
PositionContentSearcher, | ||
ContentTraverser, | ||
Position, | ||
Browser, | ||
collapseNodes, | ||
contains, | ||
ContentTraverser, | ||
createRange, | ||
findClosestElementAncestor, | ||
fromHtml, | ||
getBlockElementAtNode, | ||
findClosestElementAncestor, | ||
getPositionRect, | ||
getInlineElementAtNode, | ||
getPositionRect, | ||
getRangeFromSelectionPath, | ||
getSelectionPath, | ||
getTagOfNode, | ||
isNodeEmpty, | ||
Position, | ||
PositionContentSearcher, | ||
queryElements, | ||
collapseNodes, | ||
wrap, | ||
} from 'roosterjs-editor-dom'; | ||
|
||
|
@@ -517,7 +520,8 @@ export default class Editor { | |
): boolean; | ||
|
||
public select(arg1: any, arg2?: any, arg3?: any, arg4?: any): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a duplicate of the const defined in selectRange? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the public editor API, while selectRange is a CoreAPI and can't be called outside editor. |
||
return this.core.api.select(this.core, arg1, arg2, arg3, arg4); | ||
let range = arg1 instanceof Range ? arg1 : createRange(arg1, arg2, arg3, arg4); | ||
return this.core.api.selectRange(this.core, range); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the IE exception that selection has no rangeCount? Curious what then is the behavior in IE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be an IE bug.