Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(docs-ui): preserve focus across sibling iframes (#7956)
Univer4da11a2cd3a5
diff --git a/packages/docs-ui/src/services/selection/__tests__/doc-selection-render.service.spec.ts b/packages/docs-ui/src/services/selection/__tests__/doc-selection-render.service.spec.tsindex c8a3024696..8228da6af3 100644--- a/packages/docs-ui/src/services/selection/__tests__/doc-selection-render.service.spec.ts+++ b/packages/docs-ui/src/services/selection/__tests__/doc-selection-render.service.spec.ts@@ -1462,6 +1462,68 @@ describe('DocSelectionRenderService', () => {expect(document.activeElement).toBe(input);});+ it.each([+ { geometry: false, retainedEditor: false },+ { geometry: false, retainedEditor: true },+ { geometry: true, retainedEditor: false },+ { geometry: true, retainedEditor: true },+ ])('preserves external frame focus during passive selection refresh: %j', ({ geometry, retainedEditor }) => {+ const frame = document.createElement('iframe');+ document.body.appendChild(frame);+ const ownerDocument = frame.contentDocument!;+ const { input, renderUnit, service, univer } = createRealSelectionRenderService({+ scene: {+ getViewports: () => geometry ? [{ getAbsoluteVector: () => ({ x: 30, y: 40 }) }] : [],+ getEngine: () => ({ name: 'engine' }),+ },+ });+ cleanup.push(() => renderUnit.dispose(), () => univer.dispose());+ ownerDocument.body.appendChild(TestLayoutService.root);+ if (geometry) {+ (service as unknown as { _rangeList: TextRange[] })._rangeList = [{+ isActive: () => true,+ getAnchor: () => ({ left: 12, top: 20, visible: true }),+ dispose: vi.fn(),+ } as never];+ }+ if (retainedEditor) {+ input.focus();+ }+ const peerInput = document.createElement('div');+ peerInput.tabIndex = -1;+ document.body.appendChild(peerInput);+ peerInput.focus();+ vi.spyOn(ownerDocument, 'hasFocus').mockReturnValue(false);+ const focus = vi.spyOn(input, 'focus');++ expect(service.canFocusing).toBe(false);+ service.sync();+ if (geometry) {+ expect(service.selectionContainer.style.left).toBe('30px');+ expect(service.selectionContainer.style.top).toBe('40px');+ }+ service.activate(12, 34);++ expect(document.activeElement).toBe(peerInput);+ expect(focus).not.toHaveBeenCalled();+ expect(service.selectionContainer.style.left).toBe('12px');+ expect(service.selectionContainer.style.top).toBe('34px');++ service.activate(12, 34, true);+ expect(focus).toHaveBeenCalledOnce();+ expect(ownerDocument.activeElement).toBe(input);+ });++ it('retains top-level focus recovery when the browser window is inactive', () => {+ const { input, renderUnit, service, univer } = createRealSelectionRenderService();+ cleanup.push(() => renderUnit.dispose(), () => univer.dispose());+ vi.spyOn(document, 'hasFocus').mockReturnValue(false);++ service.sync();++ expect(document.activeElement).toBe(input);+ });+it('publishes hidden editor input, paste, focus, and blur events with the typed content', () => {const { input, renderUnit, service, univer } = createRealSelectionRenderService();cleanup.push(() => renderUnit.dispose(), () => univer.dispose());diff --git a/packages/docs-ui/src/services/selection/doc-selection-render.service.ts b/packages/docs-ui/src/services/selection/doc-selection-render.service.tsindex 60e2ae31e8..4355f21bf6 100644--- a/packages/docs-ui/src/services/selection/doc-selection-render.service.ts+++ b/packages/docs-ui/src/services/selection/doc-selection-render.service.ts@@ -33,7 +33,6 @@ import type {ITextSelectionStyle,} from '@univerjs/engine-render';import type { Subscription } from 'rxjs';-import type { RectRange } from './rect-range';import {DataStreamTreeTokenType,DOC_RANGE_TYPE,@@ -61,6 +60,7 @@ import {} from '@univerjs/engine-render';import { ILayoutService, KeyCode } from '@univerjs/ui';import { BehaviorSubject, filter, fromEvent, merge, Subject, takeUntil } from 'rxjs';+import type { RectRange } from './rect-range';import {DOC_EMBED_INTERACTION_BOUNDARY_OWNER_ATTRIBUTE,IDocEmbedInteractionBoundaryService,@@ -197,7 +197,7 @@ export class DocSelectionRenderService extends RxDisposable implements IRenderMoget canFocusing() {const ownerDocument = this._getOwnerDocument();const activeElement = ownerDocument.activeElement;- return !this._shouldPreserveExternalFocus() && !this._isAnotherEditorFocused() && (+ return !this._isInUnfocusedFrame() && !this._shouldPreserveExternalFocus() && !this._isAnotherEditorFocused() && (this.isFocusing ||activeElement === ownerDocument.body ||activeElement === null ||@@ -1300,7 +1300,8 @@ export class DocSelectionRenderService extends RxDisposable implements IRenderMoconst anchor = activeRangeInstance?.getAnchor();if (!anchor || (anchor && !anchor.visible) || this.activeViewPort == null) {- if (preserveFocus || this._shouldPreserveExternalFocus() || (!forceFocus && this._isAnotherEditorFocused())) {+ if (preserveFocus || this._shouldPreserveExternalFocus() ||+ (!forceFocus && (this._isInUnfocusedFrame() || this._isAnotherEditorFocused()))) {return;}this.focus();@@ -1847,6 +1848,12 @@ export class DocSelectionRenderService extends RxDisposable implements IRenderMoreturn this._embedInteractionBoundaryService?.hasRecentInteractionFor?.(currentEmbedOwner, ownerDocument) === true;}+ private _isInUnfocusedFrame(): boolean {+ const ownerDocument = this._getOwnerDocument();+ const ownerWindow = ownerDocument.defaultView;+ return ownerWindow != null && ownerWindow.parent !== ownerWindow && !ownerDocument.hasFocus();+ }+private _isAnotherEditorFocused(): boolean {const activeElement = this._getOwnerDocument().activeElement;return activeElement instanceof HTMLElement &&