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