Skip to content

Commit 89657d8

Browse files
author
wuzhuoheng
committed
feat: focus and blur
1 parent b242d0d commit 89657d8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/transfer/TransferrableMutation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const enum TransferrableMutationType {
1414
STORAGE = 12,
1515
FUNCTION_CALL = 13,
1616
SCROLL_INTO_VIEW = 14,
17+
FOCUS = 15,
18+
BLUR = 16,
1719
}
1820

1921
/**

src/worker-thread/dom/HTMLElement.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { Element } from './Element';
22
import { reflectProperties } from './enhanceElement';
33
import { matchNearestParent, tagNameConditionPredicate } from './matchElements';
4-
import { TransferrableObjectType } from '../../transfer/TransferrableMutation';
4+
import { TransferrableMutationType, TransferrableObjectType } from '../../transfer/TransferrableMutation';
55
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
6+
import { transfer } from '../MutationTransfer';
7+
import { Document } from './Document';
8+
9+
interface FocusOptions {
10+
preventScroll?: boolean;
11+
}
612

713
export const appendGlobalEventProperties = (keys: Array<string>): void => {
814
const keysToAppend = keys.filter((key) => !HTMLElement.prototype.hasOwnProperty(key));
@@ -48,6 +54,18 @@ export class HTMLElement extends Element {
4854
[TransferrableKeys.serializeAsTransferrableObject](): number[] {
4955
return [TransferrableObjectType.HTMLElement, this[TransferrableKeys.index]];
5056
}
57+
58+
focus(options?: FocusOptions) {
59+
if (this.isConnected) {
60+
transfer(this.ownerDocument as Document, [TransferrableMutationType.FOCUS, this[TransferrableKeys.index]]);
61+
}
62+
}
63+
64+
blur() {
65+
if (this.isConnected) {
66+
transfer(this.ownerDocument as Document, [TransferrableMutationType.BLUR, this[TransferrableKeys.index]]);
67+
}
68+
}
5169
}
5270

5371
// Reflected properties

0 commit comments

Comments
 (0)