Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/tidy-lemons-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@lynx-js/web-mainthread-apis": patch
"@lynx-js/web-core": patch
---

feat: support mts event with target methods

After this commit, developers are allowed to invoke `event.target.setStyleProperty` in mts handler
11 changes: 11 additions & 0 deletions packages/web-platform/web-constants/src/types/EventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export interface LynxCrossThreadEvent<
[key: string]: string | number | undefined | null | {};
}

export interface MainThreadScriptEvent<
T = {
[key: string]: string | number | undefined | null;
},
> extends LynxCrossThreadEvent<T> {
target: LynxCrossThreadEventTarget & { elementRefptr: unknown };
currentTarget:
| (LynxCrossThreadEventTarget & { elementRefptr: unknown })
| null;
}

export type ExposureEventDetail = {
'exposure-id': string;
'exposure-scene': string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
lynxTagAttribute,
W3cEventNameToLynx,
type LynxEventType,
type MainThreadScriptEvent,
} from '@lynx-js/web-constants';
import {
elementToRuntimeInfoMap,
Expand All @@ -30,12 +31,12 @@ export function createEventFunctions(runtime: MainThreadRuntime) {
?.handler
: runtimeInfo.eventHandlerMap[lynxEventName]?.bind
?.handler;
const crossThreadEvent = createCrossThreadEvent(
runtime,
event,
lynxEventName,
);
if (typeof hname === 'string') {
const crossThreadEvent = createCrossThreadEvent(
runtime,
event,
lynxEventName,
);
const parentComponentUniqueId = runtimeInfo.parentComponentUniqueId;
const parentComponent = runtime[getElementByUniqueId](
Number(parentComponentUniqueId),
Expand All @@ -58,7 +59,13 @@ export function createEventFunctions(runtime: MainThreadRuntime) {
}
return true;
} else if (hname) {
runtime.runWorklet?.(hname.value, []);
(crossThreadEvent as MainThreadScriptEvent).target.elementRefptr =
event.target;
if (crossThreadEvent.currentTarget) {
(crossThreadEvent as MainThreadScriptEvent).currentTarget!
.elementRefptr = event.currentTarget;
}
runtime.runWorklet?.(hname.value, [crossThreadEvent]);
}
return false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ export function createStyleFunctions(
key: number | string,
value: string | number | null | undefined,
): void {
const lynxStyleInfo = queryCSSProperty(Number(key));
let dashName: string | undefined;
if (typeof key === 'number') {
dashName = queryCSSProperty(key).dashName;
} else {
dashName = key;
}
const valueStr = typeof value === 'number' ? value.toString() : value;
if (!valueStr) { // null or undefined
element.style.removeProperty(lynxStyleInfo.dashName);
element.style.removeProperty(dashName);
} else {
const { transformedStyle } = transfromParsedStyles([[
lynxStyleInfo.dashName,
dashName,
valueStr,
]]);
for (const [property, value] of transformedStyle) {
Expand Down
10 changes: 10 additions & 0 deletions packages/web-platform/web-tests/tests/main-thread-apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,16 @@ test.describe('main thread api tests', () => {
await expect(pageElement).toHaveCSS('height', '80px');
});

test('__AddInlineStyle_key_is_name', async ({ page }, { title }) => {
await page.evaluate(() => {
let root = globalThis.__CreatePage('page', 0);
globalThis.__AddInlineStyle(root, 'height', '80px');
globalThis.__FlushElementTree();
});
const pageElement = page.locator(`[lynx-tag='page']`);
await expect(pageElement).toHaveCSS('height', '80px');
});

test('__AddInlineStyle_raw_string', async ({ page }, { title }) => {
await page.evaluate(() => {
let root = globalThis.__CreatePage('page', 0);
Expand Down
10 changes: 10 additions & 0 deletions packages/web-platform/web-tests/tests/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

const expectHasText = async (page: Page, text: string) => {
const hasText = (await page.getByText(text).count()) === 1;
await expect(hasText).toBe(true);

Check failure on line 37 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[webkit] › tests/react.spec.ts:116:5 › reactlynx3 tests › basic › basic-setstate-in-constructor

3) [webkit] › tests/react.spec.ts:116:5 › reactlynx3 tests › basic › basic-setstate-in-constructor Error: expect(received).toBe(expected) // Object.is equality Expected: true Received: false 35 | const expectHasText = async (page: Page, text: string) => { 36 | const hasText = (await page.getByText(text).count()) === 1; > 37 | await expect(hasText).toBe(true); | ^ 38 | }; 39 | 40 | const expectNoText = async (page: Page, text: string) => { at expectHasText (/home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:37:25) at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:119:7

Check failure on line 37 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[webkit] › tests/react.spec.ts:121:5 › reactlynx3 tests › basic › basic-setsate-with-cb

4) [webkit] › tests/react.spec.ts:121:5 › reactlynx3 tests › basic › basic-setsate-with-cb ─────── Error: expect(received).toBe(expected) // Object.is equality Expected: true Received: false 35 | const expectHasText = async (page: Page, text: string) => { 36 | const hasText = (await page.getByText(text).count()) === 1; > 37 | await expect(hasText).toBe(true); | ^ 38 | }; 39 | 40 | const expectNoText = async (page: Page, text: string) => { at expectHasText (/home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:37:25) at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:124:7
};

const expectNoText = async (page: Page, text: string) => {
Expand Down Expand Up @@ -91,7 +91,7 @@
await wait(100);
const target = page.locator('#target');
await target.click();
await expect(await target.getAttribute('style')).toContain('green');

Check failure on line 94 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[webkit] › tests/react.spec.ts:89:5 › reactlynx3 tests › basic › basic-bindtap

2) [webkit] › tests/react.spec.ts:89:5 › reactlynx3 tests › basic › basic-bindtap ──────────────── Error: expect(received).toContain(expected) // indexOf Expected substring: "green" Received string: "height: 100px; width: 100px; background: pink;" 92 | const target = page.locator('#target'); 93 | await target.click(); > 94 | await expect(await target.getAttribute('style')).toContain('green'); | ^ 95 | await target.click(); 96 | await expect(await target.getAttribute('style')).toContain('pink'); 97 | }); at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:94:56
await target.click();
await expect(await target.getAttribute('style')).toContain('pink');
});
Expand Down Expand Up @@ -281,6 +281,16 @@
await wait(100);
expect(eventHandlerTriggered).toBe(true);
});
test(
'basic-mts-bindtap-change-element-background',
async ({ page }, { title }) => {
await goto(page, title);
await wait(100);
const target = page.locator('#target');
await target.click();
await expect(target).toHaveCSS('background-color', 'rgb(0, 128, 0)'); // green
},
);
});
test.describe('basic-css', () => {
test('basic-css-asset-in-css', async ({ page }, { title }) => {
Expand Down Expand Up @@ -830,7 +840,7 @@
globalThis.lynxView.sendGlobalEvent('event-test', ['change']);
});
await wait(100);
await expect(target).toHaveCSS(

Check failure on line 843 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[webkit] › tests/react.spec.ts:834:5 › reactlynx3 tests › apis › api-sendGlobalEvent

5) [webkit] › tests/react.spec.ts:834:5 › reactlynx3 tests › apis › api-sendGlobalEvent ────────── Error: Timed out 5000ms waiting for expect(locator).toHaveCSS(expected) Locator: locator('#target') Expected string: "rgb(0, 128, 0)" Received string: "rgb(255, 192, 203)" Call log: - expect.toHaveCSS with timeout 5000ms - waiting for locator('#target') 9 × locator resolved to <x-view id="target" lynx-tag="view" lynx-unique-id="2"></x-view> - unexpected value "rgb(255, 192, 203)" 841 | }); 842 | await wait(100); > 843 | await expect(target).toHaveCSS( | ^ 844 | 'background-color', 845 | 'rgb(0, 128, 0)', 846 | ); // green; at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:843:28
'background-color',
'rgb(0, 128, 0)',
); // green;
Expand Down Expand Up @@ -1031,7 +1041,7 @@
await wait(100);
const target = page.locator('#target');
await target.click();
await expect(await target.getAttribute('style')).toContain('green');

Check failure on line 1044 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[webkit] › tests/react.spec.ts:1039:5 › reactlynx3 tests › configs › config-mode-dev-with-all-in-one

6) [webkit] › tests/react.spec.ts:1039:5 › reactlynx3 tests › configs › config-mode-dev-with-all-in-one Error: expect(received).toContain(expected) // indexOf Expected substring: "green" Received string: "height: 100px; width: 100px; background: pink;" 1042 | const target = page.locator('#target'); 1043 | await target.click(); > 1044 | await expect(await target.getAttribute('style')).toContain('green'); | ^ 1045 | await target.click(); 1046 | await expect(await target.getAttribute('style')).toContain('pink'); 1047 | }); at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:1044:56
await target.click();
await expect(await target.getAttribute('style')).toContain('pink');
});
Expand Down Expand Up @@ -2905,7 +2915,7 @@
expect(autoplay[3]).toBe(true);
expect(programming).toBe(true);
if (browserName === 'chromium') {
expect(manual).toBe(true);

Check failure on line 2918 in packages/web-platform/web-tests/tests/react.spec.ts

View workflow job for this annotation

GitHub Actions / playwright-linux / check

[chromium] › tests/react.spec.ts:2863:7 › reactlynx3 tests › elements › x-swiper › basic-element-x-swiper-bindchange

8) [chromium] › tests/react.spec.ts:2863:7 › reactlynx3 tests › elements › x-swiper › basic-element-x-swiper-bindchange Error: expect(received).toBe(expected) // Object.is equality Expected: true Received: false 2916 | expect(programming).toBe(true); 2917 | if (browserName === 'chromium') { > 2918 | expect(manual).toBe(true); | ^ 2919 | } 2920 | }, 2921 | ); at /home/runner/_work/lynx-stack/lynx-stack/packages/web-platform/web-tests/tests/react.spec.ts:2918:28
}
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';
function App() {
return (
<view
id='target'
main-thread:bindTap={(event) => {
'main thread';
Comment thread
PupilTong marked this conversation as resolved.
event.currentTarget.setStyleProperty('background-color', 'green');
}}
style={{
height: '100px',
width: '100px',
background: 'pink',
}}
/>
);
}
root.render(<App />);
Loading