Skip to content

Commit

Permalink
Updated focus handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Oct 7, 2024
1 parent 8b5daff commit 54a65fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions packages/ckeditor5-engine/src/view/domconverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import {
isComment,
isValidAttributeName,
first,
env,
getSelection
getSelection,
getParentOrHostElement,
env
} from '@ckeditor/ckeditor5-utils';

import type ViewNode from './node.js';
Expand Down Expand Up @@ -1090,9 +1091,11 @@ export default class DomConverter {
*/
public focus( viewEditable: EditableElement ): void {
const domEditable = this.mapViewToDom( viewEditable );
const rootNode = domEditable && ( domEditable.getRootNode() as ShadowRoot | DomDocument );
const activeElement = rootNode && rootNode.activeElement;

// TODO ShadowRoot
if ( domEditable && domEditable.getRootNode().activeElement !== domEditable ) {
if ( domEditable && activeElement !== domEditable ) {
// Save the scrollX and scrollY positions before the focus.
const { scrollX, scrollY } = global.window;
const scrollPositions: Array<[ number, number ]> = [];
Expand Down Expand Up @@ -1851,7 +1854,8 @@ function forEachDomElementAncestor( element: DomElement, callback: ( node: DomEl

while ( node ) {
callback( node );
node = node.parentElement; // TODO ShadowRoot
// TODO ShadowRoot
node = getParentOrHostElement( node ) as DomElement | null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/manual/shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</style>
</head>

<p><button id="focus-editor">Focus editor component</button></p>
<p><button id="focus-editor-component">Focus editor component</button></p>

<div id="editor-component"></div>

Expand Down
8 changes: 5 additions & 3 deletions tests/manual/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ for ( const sheet of document.styleSheets ) {
}

// This requires delegatesFocus: true
editorComponent.addEventListener( 'focus', () => window.editor.focus() );
editorComponent.addEventListener( 'focus', () => {
window.editor.focus();
}, { capture: true } );

document.querySelector( 'button#focus-editor' ).addEventListener( 'click', () => {
editorComponent.focus();
document.querySelector( 'button#focus-editor-component' ).addEventListener( 'click', () => {
editorComponent.focus( { preventScroll: true } );
} );

ClassicEditor
Expand Down

0 comments on commit 54a65fe

Please sign in to comment.