1/* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { BaseElement } from '../../../../base-ui/BaseElement'; 16import { SpSystemTrace } from '../../SpSystemTrace'; 17export class ShadowRootInput { 18 public static preventBubbling(page: BaseElement | Element): void { 19 let pageInputList = ShadowRootInput.findInputListInShadowDOM(page); 20 let sp = document?.querySelector('body > sp-application')?.shadowRoot?.querySelector('#sp-system-trace') as SpSystemTrace; 21 pageInputList.forEach(input => { 22 input.addEventListener('focus', (e) => { 23 sp.keyboardEnable = false; 24 }); 25 input.addEventListener('blur', (e) => { 26 sp.keyboardEnable = true; 27 }); 28 }); 29 } 30 public static findInputListInShadowDOM(page: BaseElement | Element | null): Element[] { 31 let queue: (Element | null)[] = [page]; 32 let inputList: Element[] = []; 33 while (queue.length > 0) { 34 let currentNode = queue.shift(); // 从队列中取出一个节点 35 if (!currentNode) { continue }; 36 if (currentNode.tagName === 'INPUT') { 37 inputList.push(currentNode); 38 } 39 if (currentNode.shadowRoot) { 40 Array.from(currentNode.shadowRoot.children).forEach(child => { 41 queue.push(child); 42 }); 43 } 44 Array.from(currentNode.children).forEach(child => { 45 queue.push(child); 46 }); 47 } 48 // 返回所有找到的input元素 49 return inputList; 50 } 51}