@lynx-js/web-core 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/CHANGELOG.md +597 -0
  2. package/LICENSE.txt +202 -0
  3. package/Notice.txt +1 -0
  4. package/README.md +21 -0
  5. package/dist/apis/LynxView.d.ts +137 -0
  6. package/dist/apis/LynxView.js +368 -0
  7. package/dist/apis/createLynxView.d.ts +18 -0
  8. package/dist/apis/createLynxView.js +17 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.js +7 -0
  11. package/dist/types/LynxExposureModule.d.ts +4 -0
  12. package/dist/types/LynxExposureModule.js +5 -0
  13. package/dist/types/RuntimePropertyOnElement.d.ts +17 -0
  14. package/dist/types/RuntimePropertyOnElement.js +2 -0
  15. package/dist/types/UpdatePageCallback.d.ts +7 -0
  16. package/dist/types/UpdatePageCallback.js +2 -0
  17. package/dist/uiThread/bootWorkers.d.ts +8 -0
  18. package/dist/uiThread/bootWorkers.js +60 -0
  19. package/dist/uiThread/crossThreadHandlers/bootTimingSystem.d.ts +5 -0
  20. package/dist/uiThread/crossThreadHandlers/bootTimingSystem.js +50 -0
  21. package/dist/uiThread/crossThreadHandlers/createDispose.d.ts +3 -0
  22. package/dist/uiThread/crossThreadHandlers/createDispose.js +11 -0
  23. package/dist/uiThread/crossThreadHandlers/createExposureService.d.ts +2 -0
  24. package/dist/uiThread/crossThreadHandlers/createExposureService.js +55 -0
  25. package/dist/uiThread/crossThreadHandlers/createUpdateData.d.ts +3 -0
  26. package/dist/uiThread/crossThreadHandlers/createUpdateData.js +14 -0
  27. package/dist/uiThread/crossThreadHandlers/queryNodes.d.ts +2 -0
  28. package/dist/uiThread/crossThreadHandlers/queryNodes.js +65 -0
  29. package/dist/uiThread/crossThreadHandlers/registerFlushElementTreeHandler.d.ts +18 -0
  30. package/dist/uiThread/crossThreadHandlers/registerFlushElementTreeHandler.js +116 -0
  31. package/dist/uiThread/crossThreadHandlers/registerInvokeUIMethodHandler.d.ts +2 -0
  32. package/dist/uiThread/crossThreadHandlers/registerInvokeUIMethodHandler.js +51 -0
  33. package/dist/uiThread/crossThreadHandlers/registerLoadNewTagHandler.d.ts +3 -0
  34. package/dist/uiThread/crossThreadHandlers/registerLoadNewTagHandler.js +7 -0
  35. package/dist/uiThread/crossThreadHandlers/registerNativeModulesCallHandler.d.ts +3 -0
  36. package/dist/uiThread/crossThreadHandlers/registerNativeModulesCallHandler.js +5 -0
  37. package/dist/uiThread/crossThreadHandlers/registerReportErrorHandler.d.ts +2 -0
  38. package/dist/uiThread/crossThreadHandlers/registerReportErrorHandler.js +10 -0
  39. package/dist/uiThread/crossThreadHandlers/registerSelectComponentHandler.d.ts +2 -0
  40. package/dist/uiThread/crossThreadHandlers/registerSelectComponentHandler.js +15 -0
  41. package/dist/uiThread/crossThreadHandlers/registerSetNativePropsHandler.d.ts +2 -0
  42. package/dist/uiThread/crossThreadHandlers/registerSetNativePropsHandler.js +32 -0
  43. package/dist/uiThread/crossThreadHandlers/registerTriggerComponentEventHandler.d.ts +2 -0
  44. package/dist/uiThread/crossThreadHandlers/registerTriggerComponentEventHandler.js +14 -0
  45. package/dist/uiThread/decodeElementOperation.d.ts +13 -0
  46. package/dist/uiThread/decodeElementOperation.js +183 -0
  47. package/dist/uiThread/getElementTag.d.ts +1 -0
  48. package/dist/uiThread/getElementTag.js +20 -0
  49. package/dist/uiThread/startUIThread.d.ts +7 -0
  50. package/dist/uiThread/startUIThread.js +78 -0
  51. package/dist/utils/browser.d.ts +3 -0
  52. package/dist/utils/browser.js +9 -0
  53. package/dist/utils/createCrossThreadEvent.d.ts +2 -0
  54. package/dist/utils/createCrossThreadEvent.js +43 -0
  55. package/dist/utils/loadTemplate.d.ts +2 -0
  56. package/dist/utils/loadTemplate.js +53 -0
  57. package/index.css +66 -0
  58. package/package.json +34 -0
@@ -0,0 +1,14 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { updateDataEndpoint, } from '@lynx-js/web-constants';
5
+ export function createUpdateData(mainThreadRpc, backgroundRpc) {
6
+ return (data, _updateDataType, callback) => {
7
+ Promise.all([
8
+ // There is no need to process options for now, as they have default values.
9
+ mainThreadRpc.invoke(updateDataEndpoint, [data, {}]),
10
+ backgroundRpc.invoke(updateDataEndpoint, [data, {}]),
11
+ ]).then(() => callback?.());
12
+ };
13
+ }
14
+ //# sourceMappingURL=createUpdateData.js.map
@@ -0,0 +1,2 @@
1
+ import { ErrorCode, IdentifierType } from '@lynx-js/web-constants';
2
+ export declare function queryNodes(rootDom: Element, type: IdentifierType, identifier: string, component_id: string, first_only: boolean, root_unique_id: number | undefined, callback: (dom: Element) => void, error?: (code: ErrorCode) => void): void;
@@ -0,0 +1,65 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { componentIdAttribute, ErrorCode, IdentifierType, lynxUniqueIdAttribute, } from '@lynx-js/web-constants';
5
+ export function queryNodes(rootDom, type, identifier, component_id, first_only, root_unique_id, callback, error) {
6
+ let queryRoot = rootDom;
7
+ if (root_unique_id) {
8
+ const root = rootDom.querySelector(`[${lynxUniqueIdAttribute}="${root_unique_id}"]`);
9
+ if (root) {
10
+ queryRoot = root;
11
+ }
12
+ else {
13
+ console.error(`[lynx-web] cannot find dom for root_unique_id: ${root_unique_id}`);
14
+ error?.(ErrorCode.NODE_NOT_FOUND);
15
+ return;
16
+ }
17
+ }
18
+ else if (component_id) {
19
+ const root = rootDom.querySelector(`[${componentIdAttribute}="${component_id}"]`);
20
+ if (root) {
21
+ queryRoot = root;
22
+ }
23
+ else {
24
+ console.error(`[lynx-web] cannot find dom for component_id: ${component_id}`);
25
+ error?.(ErrorCode.NODE_NOT_FOUND);
26
+ return;
27
+ }
28
+ }
29
+ let selector;
30
+ if (type === IdentifierType.ID_SELECTOR) {
31
+ selector = identifier;
32
+ }
33
+ else if (type === IdentifierType.UNIQUE_ID) {
34
+ selector = `[${lynxUniqueIdAttribute}="${identifier}"]`;
35
+ }
36
+ else {
37
+ console.error(`[lynx-web] NYI: setnativeprops type ${type}`);
38
+ error?.(ErrorCode.UNKNOWN);
39
+ return;
40
+ }
41
+ if (first_only) {
42
+ let targetElement = null;
43
+ try {
44
+ targetElement = queryRoot.querySelector(selector);
45
+ }
46
+ catch (e) {
47
+ console.error(`[lynx-web] cannot use selector: ${selector}`);
48
+ error?.(ErrorCode.SELECTOR_NOT_SUPPORTED);
49
+ return;
50
+ }
51
+ if (targetElement) {
52
+ callback(targetElement);
53
+ }
54
+ else {
55
+ console.error(`[lynx-web] cannot find from for selector ${identifier} under`, queryRoot);
56
+ error?.(ErrorCode.NODE_NOT_FOUND);
57
+ }
58
+ }
59
+ else {
60
+ queryRoot.querySelectorAll(selector).forEach((element) => {
61
+ callback(element);
62
+ });
63
+ }
64
+ }
65
+ //# sourceMappingURL=queryNodes.js.map
@@ -0,0 +1,18 @@
1
+ import { type PageConfig, type flushElementTreeEndpoint } from '@lynx-js/web-constants';
2
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
3
+ import type { RuntimePropertyOnElement } from '../../types/RuntimePropertyOnElement.js';
4
+ export declare function registerFlushElementTreeHandler(mainThreadRpc: Rpc, endpoint: typeof flushElementTreeEndpoint, options: {
5
+ pageConfig: PageConfig;
6
+ overrideTagMap: Record<string, string>;
7
+ backgroundRpc: Rpc;
8
+ rootDom: HTMLElement;
9
+ entryId: string;
10
+ currentLoadingTags: Promise<void>[];
11
+ }, onCommit: (info: {
12
+ pipelineId: string | undefined;
13
+ timingFlags: string[];
14
+ isFP: boolean;
15
+ }) => void, markTimingInternal: (timingKey: string, pipelineId?: string, timeStamp?: number) => void): {
16
+ uniqueIdToElement: WeakRef<HTMLElement & RuntimePropertyOnElement>[];
17
+ uniqueIdToCssInJsRule: WeakRef<CSSStyleRule>[];
18
+ };
@@ -0,0 +1,116 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { cardIdAttribute, componentIdAttribute, lynxDefaultDisplayLinearAttribute, lynxRuntimeValue, lynxTagAttribute, lynxUniqueIdAttribute, parentComponentUniqueIdAttribute, postMainThreadEvent, publicComponentEventEndpoint, publishEventEndpoint, } from '@lynx-js/web-constants';
5
+ import { decodeElementOperation } from '../decodeElementOperation.js';
6
+ import { getElementTag } from '../getElementTag.js';
7
+ import { createCrossThreadEvent } from '../../utils/createCrossThreadEvent.js';
8
+ import { isWebkit, supportAtScope } from '../../utils/browser.js';
9
+ function applyPageAttributes(page, pageConfig, entryId) {
10
+ page.setAttribute(cardIdAttribute, entryId);
11
+ if (pageConfig.defaultDisplayLinear === false) {
12
+ page.setAttribute(lynxDefaultDisplayLinearAttribute, 'false');
13
+ }
14
+ }
15
+ export function registerFlushElementTreeHandler(mainThreadRpc, endpoint, options, onCommit, markTimingInternal) {
16
+ const { pageConfig, overrideTagMap, backgroundRpc, rootDom, entryId, currentLoadingTags, } = options;
17
+ const uniqueIdToElement = [];
18
+ const uniqueIdToCssInJsRule = [];
19
+ const rootStyleElementForCssInJs = document.createElement('style');
20
+ if (!pageConfig.enableCSSSelector) {
21
+ rootStyleElementForCssInJs.innerHTML = `/* enableCSSSelector: false */ ${supportAtScope && !isWebkit ? '@scope { :scope{} }' : ''}`;
22
+ // safari testing needs this :scope{} see: https://github.com/microsoft/playwright/issues/33647
23
+ // for 18.2 the :scope{} placeholder dose not work neither. we fired an issue for this https://bugs.webkit.org/show_bug.cgi?id=285130
24
+ rootDom.append(rootStyleElementForCssInJs);
25
+ }
26
+ // dom must connected to get the sheet property
27
+ const rootScopeRule = rootStyleElementForCssInJs?.sheet?.cssRules[0];
28
+ const createElementImpl = (tag) => {
29
+ const htmlTag = getElementTag(tag, overrideTagMap);
30
+ const element = document.createElement(htmlTag);
31
+ element[lynxRuntimeValue] = {
32
+ dataset: {},
33
+ eventHandler: {},
34
+ };
35
+ return element;
36
+ };
37
+ const createStyleRuleImpl = (uniqueId, initialStyle) => {
38
+ const commonStyleSheetText = `[${lynxUniqueIdAttribute}="${uniqueId.toString()}"]{${initialStyle}}`;
39
+ if (rootScopeRule) {
40
+ const idx = rootScopeRule.insertRule(commonStyleSheetText);
41
+ return rootScopeRule.cssRules[idx];
42
+ }
43
+ else {
44
+ const idx = rootStyleElementForCssInJs.sheet.insertRule(`[${cardIdAttribute}="${entryId}"] ${commonStyleSheetText}`);
45
+ return rootStyleElementForCssInJs.sheet.cssRules[idx];
46
+ }
47
+ };
48
+ const mtsHandler = (event) => {
49
+ const crossThreadEvent = createCrossThreadEvent(event);
50
+ mainThreadRpc.invoke(postMainThreadEvent, [crossThreadEvent]);
51
+ };
52
+ const btsHandler = (event) => {
53
+ const crossThreadEvent = createCrossThreadEvent(event);
54
+ const currentTarget = event.currentTarget;
55
+ const parentComponentUniqueId = currentTarget.getAttribute(parentComponentUniqueIdAttribute) ?? '0';
56
+ const componentTargetDom = rootDom.querySelector(`[${lynxUniqueIdAttribute}="${parentComponentUniqueId}"]`);
57
+ const componentId = componentTargetDom?.getAttribute(lynxTagAttribute) !== 'page'
58
+ ? componentTargetDom?.getAttribute(componentIdAttribute) ?? undefined
59
+ : undefined;
60
+ const hname = currentTarget[lynxRuntimeValue]
61
+ .eventHandler[crossThreadEvent.type].hname;
62
+ if (componentId) {
63
+ backgroundRpc.invoke(publicComponentEventEndpoint, [
64
+ componentId,
65
+ hname,
66
+ crossThreadEvent,
67
+ ]);
68
+ }
69
+ else {
70
+ backgroundRpc.invoke(publishEventEndpoint, [
71
+ hname,
72
+ crossThreadEvent,
73
+ ]);
74
+ }
75
+ };
76
+ mainThreadRpc.registerHandler(endpoint, (operations, options, cardCss) => {
77
+ const { pipelineOptions } = options;
78
+ const pipelineId = pipelineOptions?.pipelineID;
79
+ const timingFlags = [];
80
+ markTimingInternal('dispatch_start', pipelineId);
81
+ Promise.all(currentLoadingTags).then(() => {
82
+ markTimingInternal('layout_start', pipelineId);
83
+ markTimingInternal('ui_operation_flush_start', pipelineId);
84
+ const page = decodeElementOperation(operations, {
85
+ timingFlags,
86
+ uniqueIdToElement,
87
+ uniqueIdToCssInJsRule,
88
+ createElementImpl,
89
+ createStyleRuleImpl,
90
+ eventHandler: {
91
+ mtsHandler,
92
+ btsHandler,
93
+ },
94
+ });
95
+ markTimingInternal('ui_operation_flush_end', pipelineId);
96
+ const isFP = !!page;
97
+ if (isFP) {
98
+ // on FP
99
+ const styleElement = document.createElement('style');
100
+ styleElement.innerHTML = cardCss;
101
+ rootDom.append(styleElement);
102
+ rootDom.append(page);
103
+ applyPageAttributes(page, pageConfig, entryId);
104
+ }
105
+ markTimingInternal('layout_end', pipelineId);
106
+ markTimingInternal('dispatch_end', pipelineId);
107
+ onCommit({
108
+ pipelineId,
109
+ timingFlags,
110
+ isFP,
111
+ });
112
+ });
113
+ });
114
+ return { uniqueIdToElement, uniqueIdToCssInJsRule };
115
+ }
116
+ //# sourceMappingURL=registerFlushElementTreeHandler.js.map
@@ -0,0 +1,2 @@
1
+ import { Rpc } from '@lynx-js/web-worker-rpc';
2
+ export declare function registerInvokeUIMethodHandler(rpc: Rpc, rootDom: Element): void;
@@ -0,0 +1,51 @@
1
+ import { Rpc } from '@lynx-js/web-worker-rpc';
2
+ import { queryNodes } from './queryNodes.js';
3
+ import { ErrorCode, lynxRuntimeValue, invokeUIMethodEndpoint, } from '@lynx-js/web-constants';
4
+ const methodAlias = {
5
+ 'boundingClientRect': (element) => {
6
+ const rect = element.getBoundingClientRect();
7
+ return {
8
+ id: element.id,
9
+ dataset: element[lynxRuntimeValue]
10
+ .dataset,
11
+ width: rect.width,
12
+ height: rect.height,
13
+ left: rect.left,
14
+ right: rect.right,
15
+ top: rect.top,
16
+ bottom: rect.bottom,
17
+ };
18
+ },
19
+ };
20
+ export function registerInvokeUIMethodHandler(rpc, rootDom) {
21
+ let code = ErrorCode.UNKNOWN;
22
+ let data = undefined;
23
+ rpc.registerHandler(invokeUIMethodEndpoint, (type, identifier, component_id, method, params, root_unique_id) => {
24
+ queryNodes(rootDom, type, identifier, component_id, true, root_unique_id, (element) => {
25
+ try {
26
+ const aliasMethod = methodAlias[method];
27
+ const hasDomMethod = typeof element[method] === 'function';
28
+ if (!aliasMethod && !hasDomMethod) {
29
+ code = ErrorCode.METHOD_NOT_FOUND;
30
+ }
31
+ else {
32
+ if (aliasMethod) {
33
+ data = aliasMethod(element, params);
34
+ }
35
+ else {
36
+ data = element[method](params);
37
+ }
38
+ code = ErrorCode.SUCCESS;
39
+ }
40
+ }
41
+ catch (e) {
42
+ console.error(`[lynx-web] invokeUIMethod: apply method faild with`, e, element);
43
+ code = ErrorCode.PARAM_INVALID;
44
+ }
45
+ }, (error) => {
46
+ code = error;
47
+ });
48
+ return { code, data };
49
+ });
50
+ }
51
+ //# sourceMappingURL=registerInvokeUIMethodHandler.js.map
@@ -0,0 +1,3 @@
1
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
2
+ import type { loadNewTagEndpoint } from '@lynx-js/web-constants';
3
+ export declare function registerLoadNewTagHandler(rpc: Rpc, endpoint: typeof loadNewTagEndpoint, loadTag: (tag: string) => void, tagMap: Record<string, string>, currentLoadingTags: Promise<any>[]): void;
@@ -0,0 +1,7 @@
1
+ import { getElementTag } from '../getElementTag.js';
2
+ export function registerLoadNewTagHandler(rpc, endpoint, loadTag, tagMap, currentLoadingTags) {
3
+ rpc.registerHandler(endpoint, (tag) => {
4
+ loadTag(getElementTag(tag, tagMap, currentLoadingTags));
5
+ });
6
+ }
7
+ //# sourceMappingURL=registerLoadNewTagHandler.js.map
@@ -0,0 +1,3 @@
1
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
2
+ import { type NativeModulesCall } from '@lynx-js/web-constants';
3
+ export declare function registerNativeModulesCallHandler(rpc: Rpc, nativeModulesCall: NativeModulesCall): void;
@@ -0,0 +1,5 @@
1
+ import { nativeModulesCallEndpoint, } from '@lynx-js/web-constants';
2
+ export function registerNativeModulesCallHandler(rpc, nativeModulesCall) {
3
+ rpc.registerHandler(nativeModulesCallEndpoint, nativeModulesCall);
4
+ }
5
+ //# sourceMappingURL=registerNativeModulesCallHandler.js.map
@@ -0,0 +1,2 @@
1
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
2
+ export declare function registerReportErrorHandler(rpc: Rpc, onError?: (e: string) => void): void;
@@ -0,0 +1,10 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { reportErrorEndpoint } from '@lynx-js/web-constants';
5
+ export function registerReportErrorHandler(rpc, onError) {
6
+ rpc.registerHandler(reportErrorEndpoint, (e) => {
7
+ onError?.(e);
8
+ });
9
+ }
10
+ //# sourceMappingURL=registerReportErrorHandler.js.map
@@ -0,0 +1,2 @@
1
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
2
+ export declare function registerSelectComponentHandler(rpc: Rpc, rootDom: Element): void;
@@ -0,0 +1,15 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { componentIdAttribute, IdentifierType, selectComponentEndpoint, } from '@lynx-js/web-constants';
5
+ import { queryNodes } from './queryNodes.js';
6
+ export function registerSelectComponentHandler(rpc, rootDom) {
7
+ let element;
8
+ rpc.registerHandler(selectComponentEndpoint, (componentId, idSelector, single) => {
9
+ queryNodes(rootDom, IdentifierType.ID_SELECTOR, idSelector, componentId === 'card' ? '0' : componentId, single, undefined, (ele) => {
10
+ element = ele;
11
+ });
12
+ return [element?.getAttribute(componentIdAttribute) ?? undefined];
13
+ });
14
+ }
15
+ //# sourceMappingURL=registerSelectComponentHandler.js.map
@@ -0,0 +1,2 @@
1
+ import { Rpc } from '@lynx-js/web-worker-rpc';
2
+ export declare function registerNativePropsHandler(rpc: Rpc, rootDom: Element): void;
@@ -0,0 +1,32 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { Rpc } from '@lynx-js/web-worker-rpc';
5
+ import { queryNodes } from './queryNodes.js';
6
+ import { setNativePropsEndpoint } from '@lynx-js/web-constants';
7
+ function applyNativeProps(element, nativeProps) {
8
+ for (const key in nativeProps) {
9
+ const value = nativeProps[key];
10
+ if (key === 'text' && element?.tagName === 'X-TEXT') {
11
+ if (element.firstElementChild
12
+ && element.firstElementChild.tagName == 'RAW-TEXT') {
13
+ element = element.firstElementChild;
14
+ }
15
+ }
16
+ if (CSS.supports(key, value)
17
+ && element.style) {
18
+ element.style.setProperty(key, value);
19
+ }
20
+ else {
21
+ element.setAttribute(key, value);
22
+ }
23
+ }
24
+ }
25
+ export function registerNativePropsHandler(rpc, rootDom) {
26
+ rpc.registerHandler(setNativePropsEndpoint, (type, identifier, component_id, first_only, native_props, root_unique_id) => {
27
+ queryNodes(rootDom, type, identifier, component_id, first_only, root_unique_id, (element) => {
28
+ applyNativeProps(element, native_props);
29
+ });
30
+ });
31
+ }
32
+ //# sourceMappingURL=registerSetNativePropsHandler.js.map
@@ -0,0 +1,2 @@
1
+ import type { Rpc } from '@lynx-js/web-worker-rpc';
2
+ export declare function registerTriggerComponentEventHandler(rpc: Rpc, rootDom: Element): void;
@@ -0,0 +1,14 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { componentIdAttribute, triggerComponentEventEndpoint, } from '@lynx-js/web-constants';
5
+ export function registerTriggerComponentEventHandler(rpc, rootDom) {
6
+ rpc.registerHandler(triggerComponentEventEndpoint, (id, params) => {
7
+ const componentDom = rootDom.querySelector(`[${componentIdAttribute}="${params.componentId}"]`);
8
+ componentDom?.dispatchEvent(new CustomEvent(id, {
9
+ ...params.eventOption,
10
+ detail: params.eventDetail,
11
+ }));
12
+ });
13
+ }
14
+ //# sourceMappingURL=registerTriggerComponentEventHandler.js.map
@@ -0,0 +1,13 @@
1
+ import type { ElementOperation } from '@lynx-js/web-constants';
2
+ import type { RuntimePropertyOnElement } from '../types/RuntimePropertyOnElement.js';
3
+ export declare function decodeElementOperation<T extends HTMLElement & RuntimePropertyOnElement>(operations: ElementOperation[], options: {
4
+ timingFlags: string[];
5
+ uniqueIdToElement: (WeakRef<T> | undefined)[];
6
+ uniqueIdToCssInJsRule: (WeakRef<CSSStyleRule> | undefined)[];
7
+ createElementImpl: (tag: string) => T;
8
+ createStyleRuleImpl: (uniqueId: number, initialStyle: string) => CSSStyleRule;
9
+ eventHandler: {
10
+ mtsHandler: (event: Event) => void;
11
+ btsHandler: (event: Event) => void;
12
+ };
13
+ }): T | undefined;
@@ -0,0 +1,183 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { cssIdAttribute, lynxTagAttribute, lynxUniqueIdAttribute, OperationType, lynxRuntimeValue, LynxEventNameToW3cByTagName, LynxEventNameToW3cCommon, W3cEventNameToLynx, parentComponentUniqueIdAttribute, __lynx_timing_flag, } from '@lynx-js/web-constants';
5
+ function getElement(uniqueId, uniqueIdToElement) {
6
+ const element = uniqueIdToElement[uniqueId]?.deref();
7
+ if (element) {
8
+ return element;
9
+ }
10
+ else {
11
+ throw new Error(`[lynx-web] cannot find element with uniqueId: ${uniqueId}`);
12
+ }
13
+ }
14
+ function createElement(tag, uniqueId, uniqueIdToElement, createElementImpl) {
15
+ const current = uniqueIdToElement[uniqueId]?.deref();
16
+ if (current) {
17
+ throw new Error(`[lynx-web] uniqueid is occupied: cannot create new element ${tag} with uniqueId: ${uniqueId}`);
18
+ }
19
+ const element = createElementImpl(tag);
20
+ element.setAttribute(lynxUniqueIdAttribute, uniqueId.toString());
21
+ element.setAttribute(lynxTagAttribute, tag);
22
+ uniqueIdToElement[uniqueId] = new WeakRef(element);
23
+ return element;
24
+ }
25
+ function handleHtmlEvent(event) {
26
+ const currentTarget = event.currentTarget;
27
+ if (!currentTarget)
28
+ return;
29
+ const { eventHandler, } = currentTarget[lynxRuntimeValue];
30
+ const lynxEventName = W3cEventNameToLynx[event.type] ?? event.type;
31
+ const eventHandlerInfo = eventHandler[lynxEventName];
32
+ if (eventHandlerInfo) {
33
+ const { type: eventType, handler } = eventHandlerInfo;
34
+ const isCatch = eventType === 'catchEvent' || eventType === 'capture-catch';
35
+ handler(event);
36
+ if (isCatch) {
37
+ event.stopPropagation();
38
+ }
39
+ }
40
+ }
41
+ export function decodeElementOperation(operations, options) {
42
+ const { uniqueIdToElement, uniqueIdToCssInJsRule, createElementImpl, createStyleRuleImpl, eventHandler, timingFlags, } = options;
43
+ let pageElement;
44
+ for (const op of operations) {
45
+ if (op.type === OperationType.Create) {
46
+ const element = createElement(op.tag, op.uid, uniqueIdToElement, createElementImpl);
47
+ if (typeof op.cssId === 'number') {
48
+ element.setAttribute(cssIdAttribute, op.cssId.toString());
49
+ }
50
+ if (op.puid) {
51
+ element.setAttribute(parentComponentUniqueIdAttribute, op.puid);
52
+ }
53
+ if (op.tag === 'page')
54
+ pageElement = element;
55
+ }
56
+ else {
57
+ const target = getElement(op.uid, uniqueIdToElement);
58
+ switch (op.type) {
59
+ case OperationType.Append:
60
+ {
61
+ const children = op.cid.map(id => getElement(id, uniqueIdToElement));
62
+ target.append(...children);
63
+ }
64
+ break;
65
+ case OperationType.InsertBefore:
66
+ {
67
+ const child = getElement(op.cid, uniqueIdToElement);
68
+ const ref = op.ref ? getElement(op.ref, uniqueIdToElement) : null;
69
+ target.insertBefore(child, ref);
70
+ }
71
+ break;
72
+ case OperationType.Remove:
73
+ {
74
+ for (const kidId of op.cid) {
75
+ const kid = getElement(kidId, uniqueIdToElement);
76
+ target.removeChild(kid);
77
+ }
78
+ }
79
+ break;
80
+ case OperationType.Replace:
81
+ {
82
+ const newElements = op.nid.map(id => getElement(id, uniqueIdToElement));
83
+ target.replaceWith(...newElements);
84
+ }
85
+ break;
86
+ case OperationType.SetAttribute:
87
+ {
88
+ if (op.value === null) {
89
+ target.removeAttribute(op.key);
90
+ }
91
+ else {
92
+ target.setAttribute(op.key, op.value);
93
+ if (op.value && op.key === __lynx_timing_flag) {
94
+ timingFlags.push(op.value);
95
+ }
96
+ }
97
+ }
98
+ break;
99
+ case OperationType.SwapElement:
100
+ {
101
+ const targetB = getElement(op.tid, uniqueIdToElement);
102
+ const temp = document.createElement('div');
103
+ target.replaceWith(temp);
104
+ targetB.replaceWith(target);
105
+ temp.replaceWith(targetB);
106
+ }
107
+ break;
108
+ case OperationType.SetProperty:
109
+ target[lynxRuntimeValue][op.key] = op.value;
110
+ if (op.key === 'dataset') {
111
+ if (op.value) {
112
+ for (const [key, value] of Object.entries(op.value)) {
113
+ if (value) {
114
+ target.setAttribute(`data-${key}`, value.toString());
115
+ }
116
+ else {
117
+ target.removeAttribute(`data-${key}`);
118
+ }
119
+ }
120
+ }
121
+ else {
122
+ target[lynxRuntimeValue]['dataset'] = {};
123
+ }
124
+ }
125
+ break;
126
+ case OperationType.SetDatasetProperty:
127
+ target[lynxRuntimeValue].dataset[op.key] = op.value;
128
+ if (op.value) {
129
+ target.setAttribute(`data-${op.key}`, op.value.toString());
130
+ }
131
+ else {
132
+ target.removeAttribute(`data-${op.key}`);
133
+ }
134
+ break;
135
+ case OperationType.RegisterEventHandler:
136
+ const isMtsHandler = op.hname === null;
137
+ const lynxEventName = op.ename.toLowerCase();
138
+ const htmlEventName = LynxEventNameToW3cByTagName[target.tagName]?.[lynxEventName]
139
+ ?? LynxEventNameToW3cCommon[lynxEventName] ?? lynxEventName;
140
+ const currentHandlerInfo = target[lynxRuntimeValue].eventHandler[lynxEventName];
141
+ if (currentHandlerInfo) {
142
+ target.removeEventListener(htmlEventName, handleHtmlEvent);
143
+ }
144
+ const isCaptureEvent = op.eventType === 'capture-bind'
145
+ || op.eventType === 'capture-catch';
146
+ if (op.hname === undefined) {
147
+ target.removeAttribute(`x-enable-${lynxEventName}-event`);
148
+ target[lynxRuntimeValue].eventHandler[lynxEventName] = undefined;
149
+ }
150
+ else {
151
+ target.setAttribute(`x-enable-${lynxEventName}-event`, '');
152
+ target.addEventListener(htmlEventName, handleHtmlEvent, {
153
+ passive: true,
154
+ capture: isCaptureEvent,
155
+ });
156
+ target[lynxRuntimeValue].eventHandler[lynxEventName] = {
157
+ type: op.eventType,
158
+ handler: isMtsHandler
159
+ ? eventHandler.mtsHandler
160
+ : eventHandler.btsHandler,
161
+ hname: op.hname ?? '',
162
+ };
163
+ }
164
+ break;
165
+ case OperationType.SetStyleProperty:
166
+ target.style.setProperty(op.key, op.value, op.im ? '!important' : undefined);
167
+ break;
168
+ case OperationType.UpdateCssInJs:
169
+ let rule = uniqueIdToCssInJsRule[op.uid]?.deref();
170
+ if (rule) {
171
+ rule.style.cssText = op.classStyleStr;
172
+ }
173
+ else {
174
+ rule = createStyleRuleImpl(op.uid, op.classStyleStr);
175
+ uniqueIdToCssInJsRule[op.uid] = new WeakRef(rule);
176
+ }
177
+ break;
178
+ }
179
+ }
180
+ }
181
+ return pageElement;
182
+ }
183
+ //# sourceMappingURL=decodeElementOperation.js.map
@@ -0,0 +1 @@
1
+ export declare function getElementTag(tag: string, tagMap: Record<string, string>, currentLoadingTags?: Promise<any>[]): string;
@@ -0,0 +1,20 @@
1
+ // Copyright 2023 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ export function getElementTag(tag, tagMap, currentLoadingTags) {
5
+ if (tagMap[tag]) {
6
+ return tagMap[tag];
7
+ }
8
+ if (customElements.get(tag)) {
9
+ tagMap[tag] = tag;
10
+ return tag;
11
+ }
12
+ const normizedTag = tag.includes('-') ? tag : `x-${tag}`;
13
+ if (customElements.get(normizedTag)) {
14
+ tagMap[tag] = normizedTag;
15
+ return normizedTag;
16
+ }
17
+ currentLoadingTags?.push(customElements.whenDefined(normizedTag));
18
+ return normizedTag;
19
+ }
20
+ //# sourceMappingURL=getElementTag.js.map
@@ -0,0 +1,7 @@
1
+ import type { LynxView } from '../apis/createLynxView.js';
2
+ import { type MainThreadStartConfigs, type NativeModulesCall } from '@lynx-js/web-constants';
3
+ export declare function startUIThread(templateUrl: string, configs: Omit<MainThreadStartConfigs, 'template'>, rootDom: HTMLElement, callbacks: {
4
+ loadNewTag?: (tag: string) => void;
5
+ nativeModulesCall: NativeModulesCall;
6
+ onError?: () => void;
7
+ }, overrideTagMap: Record<string, string> | undefined, nativeModulesUrl: string | undefined): LynxView;