1/* 2 * Copyright (c) 2021 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 */ 15 16const fs = require('fs'); 17const path = require('path'); 18import ts from 'typescript'; 19import { 20 WHITELISTNEW, 21 readExternalComponents 22} from './external_component_map'; 23const COMPONENTS = 'components'; 24const FORM_COMPONENTS = 'form_components'; 25export const COMPONENT_MAP: any = {}; 26export const FORM_MAP: any = {}; 27 28let EXTERNAL_COMPONENT_MAP: object = {}; 29export let EXT_WHITE_LIST: Array<string> = []; 30export let COMMON_ATTRS: Set<string> = new Set([]); 31 32(function readComponents(): void { 33 EXTERNAL_COMPONENT_MAP = readExternalComponents(); 34 EXT_WHITE_LIST = WHITELISTNEW; 35 const componentPath: Map<string, string> = new Map([ 36 [`${COMPONENTS}`, `../${COMPONENTS}`], 37 [`${FORM_COMPONENTS}`, `../${FORM_COMPONENTS}`] 38 ]); 39 for (const [id, relPath] of componentPath.entries()) { 40 const componentsFile: string = path.join(__dirname, relPath); 41 const files: string[] = fs.readdirSync(componentsFile); 42 files.forEach(function (item) { 43 const fPath: string = path.join(componentsFile, item); 44 const json: any = require(fPath); 45 const stat: any = fs.statSync(fPath); 46 if (stat.isFile()) { 47 if (json.name) { 48 const compName: string = json.name; 49 delete json.name; 50 if (id === COMPONENTS) { 51 COMPONENT_MAP[compName] = json; 52 } else if (id === FORM_COMPONENTS) { 53 FORM_MAP[compName] = json; 54 } 55 } else { 56 if (id === COMPONENTS) { 57 COMMON_ATTRS = new Set(json.attrs); 58 } 59 } 60 } 61 }); 62 } 63 addExternalComponents(); 64})(); 65 66function addExternalComponents(): void { 67 const componentMapKeys = new Set(Object.keys(COMPONENT_MAP)); 68 const extComponentSet: Set<string> = new Set(EXTERNAL_COMPONENT_MAP ? 69 Object.keys(EXTERNAL_COMPONENT_MAP) : []); 70 const extComponentKeys: Array<string> = Array.from(extComponentSet); 71 if (!extComponentKeys.length) { 72 return; 73 } 74 extComponentKeys.forEach((extCompName: string) => { 75 if (componentMapKeys.has(extCompName) && COMPONENT_MAP[extCompName].parents && 76 EXTERNAL_COMPONENT_MAP[extCompName].parents) { 77 const newParents: Array<string> = Array.from(new Set([...COMPONENT_MAP[extCompName].parents, 78 ...EXTERNAL_COMPONENT_MAP[extCompName].parents])); 79 COMPONENT_MAP[extCompName].parents = newParents; 80 } else { 81 COMPONENT_MAP[extCompName] = EXTERNAL_COMPONENT_MAP[extCompName]; 82 } 83 }); 84} 85 86export const TRANSITION_COMMON_ATTRS: Set<string> = new Set([ 87 'slide', 'translate', 'scale', 'opacity' 88]); 89export const GESTURE_ATTRS: Set<string> = new Set([ 90 'gesture', 'parallelGesture', 'priorityGesture' 91]); 92 93export const ID_ATTRS: Map<string, Map<string, string | number>> = new Map(); 94 95export const forbiddenUseStateType: Set<string> = new Set(['Scroller', 'SwiperScroller', 96 'VideoController', 'WebController', 'CustomDialogController', 'SwiperController', 97 'TabsController', 'CalendarController', 'AbilityController', 'XComponentController', 98 'CanvasRenderingContext2D', 'CanvasGradient', 'ImageBitmap', 'ImageData', 'Path2D', 99 'RenderingContextSettings', 'OffscreenCanvasRenderingContext2D', 'PatternLockController', 100 'TextAreaController', 'TextInputController', 'TextTimerController', 'SearchController', 'RichEditorController', 101 'ArcSwiperController' 102]); 103 104export const FOREACH_ATTRIBUTE = ['onMove']; 105export const INNER_COMPONENT_NAMES: Set<string> = new Set(); 106export const NO_DEBUG_LINE_COMPONENT: Set<string> = new Set(); 107export const BUILDIN_CONTAINER_COMPONENT: Set<string> = new Set(); 108export const COMPONENT_SYSTEMAPI_NAMES: Set<string> = new Set(); 109export const BUILDIN_STYLE_NAMES: Set<string> = new Set([ 110 ...COMMON_ATTRS, ...GESTURE_ATTRS, ...TRANSITION_COMMON_ATTRS, ...FOREACH_ATTRIBUTE 111]); 112export const AUTOMIC_COMPONENT: Set<string> = new Set(); 113export const SINGLE_CHILD_COMPONENT: Set<string> = new Set(); 114export const SPECIFIC_CHILD_COMPONENT: Map<string, Set<string>> = new Map(); 115export const SPECIFIC_PARENT_COMPONENT: Map<string, Set<string>> = new Map(); 116export const GESTURE_TYPE_NAMES: Set<string> = new Set([ 117 'TapGesture', 'LongPressGesture', 'PanGesture', 'PinchGesture', 'RotationGesture', 'GestureGroup', 118 'SwipeGesture' 119]); 120export const CUSTOM_BUILDER_METHOD: Set<string> = new Set(); 121export const INNER_CUSTOM_LOCALBUILDER_METHOD: Set<string> = new Set(); 122export const INNER_STYLE_FUNCTION: Map<string, ts.Block> = new Map(); 123export const GLOBAL_STYLE_FUNCTION: Map<string, ts.Block> = new Map(); 124 125export interface ExtendParamterInterfance { 126 attribute: string, 127 parameterCount: number 128} 129export const EXTEND_ATTRIBUTE: Map<string, Set<string>> = new Map(); 130export const STYLES_ATTRIBUTE: Set<string> = new Set(); 131 132export const INTERFACE_NODE_SET: Set<ts.InterfaceDeclaration> = new Set(); 133 134export const INNER_CUSTOM_BUILDER_METHOD: Set<string> = new Set(); 135export const GLOBAL_CUSTOM_BUILDER_METHOD: Set<string> = new Set(); 136 137export const JS_BIND_COMPONENTS: Set<string> = new Set([ 138 'ForEach', 'LazyForEach', ...GESTURE_TYPE_NAMES, 'Gesture', 139 'PanGestureOption', 'CustomDialogController', 'Storage', 'Scroller', 'SwiperController', 140 'TabsController', 'CalendarController', 'AbilityController', 'VideoController', 'WebController', 141 'XComponentController', 'CanvasRenderingContext2D', 'CanvasGradient', 'ImageBitmap', 'ImageData', 142 'Path2D', 'RenderingContextSettings', 'OffscreenCanvasRenderingContext2D', 'DatePickerDialog', 143 'TextPickerDialog', 'AlertDialog', 'ContextMenu', 'ActionSheet', 'PatternLockController', 144 'TimePickerDialog', 'CalendarPickerDialog', 'ArcSwiperController' 145]); 146 147export const NEEDPOP_COMPONENT: Set<string> = new Set(['Blank', 'Search']); 148 149export const CUSTOM_BUILDER_PROPERTIES: Set<string> = new Set(['background', 'bindPopup', 'bindMenu', 'bindContextMenu', 'title', 150 'menus', 'toolBar', 'tabBar', 'toolbar', 'onDragStart', 'onItemDragStart', 'swipeAction', 'bindContentCover', 'bindSheet', 151 'navDestination', 'overlay', 'toolbarConfiguration', 'customKeyboard', 'bindSelectionMenu', 'description', 152 'showUnit', 'customKeyboard', 'create', 'addBuilderSpan', 'dragPreview', 'accessibilityVirtualNode']); 153export const CUSTOM_BUILDER_PROPERTIES_WITHOUTKEY: Set<string> = new Set(['showUnit', 'create']); 154export const CUSTOM_BUILDER_CONSTRUCTORS: Set<string> = new Set(['MenuItem', 'MenuItemGroup', 'Refresh', 'WaterFlow', 'Radio', 'Checkbox']); 155 156(function initComponent(): void { 157 Object.keys(COMPONENT_MAP).forEach((componentName) => { 158 INNER_COMPONENT_NAMES.add(componentName); 159 JS_BIND_COMPONENTS.add(componentName); 160 if (!COMPONENT_MAP[componentName].atomic) { 161 BUILDIN_CONTAINER_COMPONENT.add(componentName); 162 } else { 163 AUTOMIC_COMPONENT.add(componentName); 164 } 165 if (COMPONENT_MAP[componentName].single) { 166 SINGLE_CHILD_COMPONENT.add(componentName); 167 } 168 if (COMPONENT_MAP[componentName].children) { 169 SPECIFIC_CHILD_COMPONENT.set(componentName, 170 new Set([...COMPONENT_MAP[componentName].children])); 171 } 172 if (COMPONENT_MAP[componentName].attrs && COMPONENT_MAP[componentName].attrs.length) { 173 COMPONENT_MAP[componentName].attrs.forEach((item: string) => { 174 BUILDIN_STYLE_NAMES.add(item); 175 }); 176 if (COMPONENT_MAP[componentName].systemApi) { 177 COMPONENT_MAP[componentName].attrs.forEach((item: string) => { 178 COMPONENT_SYSTEMAPI_NAMES.add(item); 179 }); 180 } 181 } 182 if (COMPONENT_MAP[componentName].noDebugLine) { 183 NO_DEBUG_LINE_COMPONENT.add(componentName); 184 } 185 if (COMPONENT_MAP[componentName].parents) { 186 SPECIFIC_PARENT_COMPONENT.set(componentName, 187 new Set([...COMPONENT_MAP[componentName].parents])); 188 } 189 }); 190})(); 191 192export function resetComponentMap(): void { 193 ID_ATTRS.clear(); 194 EXTEND_ATTRIBUTE.clear(); 195 STYLES_ATTRIBUTE.clear(); 196} 197 198export function resetExtContent(): void { 199 EXT_WHITE_LIST = []; 200} 201