UNPKG

@lynx-js/web-core

Version:

This is an internal experimental package, do not use

51 lines 2.38 kB
// 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 { queryNodes } from './queryNodes.js'; import { getPathInfoEndpoint } from '../../endpoints.js'; import { ErrorCode } from '../../../constants.js'; export function registerGetPathInfoHandler(rpc, lynxViewInstance) { rpc.registerHandler(getPathInfoEndpoint, (type, identifier, component_id, first_only, root_unique_id) => { let code = ErrorCode.UNKNOWN; let data; queryNodes(lynxViewInstance, type, identifier, component_id, first_only, root_unique_id, (element) => { try { const path = []; let currentNode = element; while (currentNode) { const parent = currentNode.parentElement; const parentNodeForChildren = parent ?? lynxViewInstance.rootDom; const children = Array.from(parentNodeForChildren.children); const tag = lynxViewInstance.mainThreadGlobalThis.__GetTag(currentNode); const index = tag === 'page' ? 0 : children.indexOf(currentNode); const id = currentNode.getAttribute('id') || undefined; const className = currentNode.getAttribute('class') || undefined; const dataSet = lynxViewInstance.mainThreadGlobalThis .__GetDataset(currentNode); path.push({ tag, id, class: className, dataSet, index, }); if (tag === 'page' || currentNode.parentNode === lynxViewInstance.rootDom) { break; } currentNode = parent; } data = { path }; code = ErrorCode.SUCCESS; } catch (e) { console.error('[lynx-web] getPathInfo: failed with', e, element); code = ErrorCode.UNKNOWN; } }, (error) => { code = error; }); return { code, data }; }); } //# sourceMappingURL=registerGetPathInfoHandler.js.map