2020-04-09 17:07:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2020/4/9.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <YogaKit/UIView+Yoga.h>
|
|
|
|
#import "DoricFlexNode.h"
|
|
|
|
#import "DoricExtensions.h"
|
2022-09-05 15:52:47 +08:00
|
|
|
#import <YogaKit/UIView+Yoga.h>
|
2020-04-09 17:07:24 +08:00
|
|
|
|
|
|
|
@interface DoricFlexView : UIView
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricFlexView
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricFlexNode
|
|
|
|
- (UIView *)build {
|
|
|
|
return [[DoricFlexView new] also:^(DoricFlexView *it) {
|
2020-04-10 20:41:43 +08:00
|
|
|
it.clipsToBounds = YES;
|
2022-09-05 15:52:47 +08:00
|
|
|
it.doricLayout.layoutType = DoricFlexLayout;
|
2020-04-10 11:37:48 +08:00
|
|
|
[it configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
|
2020-04-09 20:01:22 +08:00
|
|
|
layout.isEnabled = YES;
|
|
|
|
}];
|
2020-04-09 17:07:24 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
|
|
|
|
if ([name isEqualToString:@"flexConfig"]) {
|
2022-09-05 15:52:47 +08:00
|
|
|
view.doricLayout.flexConfig = prop;
|
2020-04-09 20:01:22 +08:00
|
|
|
[self blendYoga:view.yoga from:prop];
|
2020-04-09 17:07:24 +08:00
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 19:02:45 +08:00
|
|
|
- (void)afterBlended:(NSDictionary *)props {
|
|
|
|
[super afterBlended:props];
|
|
|
|
[self.childNodes forEach:^(DoricViewNode *viewNode) {
|
|
|
|
NSString *viewId = viewNode.viewId;
|
|
|
|
NSDictionary *model = [self subModelOf:viewId];
|
2021-09-29 18:29:51 +08:00
|
|
|
NSDictionary *dictionary = model[@"props"];
|
2021-09-16 19:02:45 +08:00
|
|
|
if (!dictionary[@"flexConfig"]) {
|
|
|
|
viewNode.view.yoga.width = YGValueAuto;
|
|
|
|
viewNode.view.yoga.height = YGValueAuto;
|
|
|
|
}
|
2022-09-05 15:52:47 +08:00
|
|
|
viewNode.view.yoga.isEnabled = YES;
|
2021-09-16 19:02:45 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:07:24 +08:00
|
|
|
- (void)blendSubNode:(DoricViewNode *)subNode flexConfig:(NSDictionary *)flexConfig {
|
2020-04-10 11:37:48 +08:00
|
|
|
[subNode.view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
|
2020-04-09 20:01:22 +08:00
|
|
|
layout.isEnabled = YES;
|
|
|
|
}];
|
2021-09-16 19:02:45 +08:00
|
|
|
subNode.view.yoga.width = YGValueAuto;
|
|
|
|
subNode.view.yoga.height = YGValueAuto;
|
2020-04-09 20:01:22 +08:00
|
|
|
[self blendYoga:subNode.view.yoga from:flexConfig];
|
2020-04-09 17:07:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 13:27:44 +08:00
|
|
|
- (void)blendYoga:(YGLayout *)yoga from:(NSDictionary *)flexConfig {
|
|
|
|
[flexConfig enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
|
|
|
|
[self blendYoga:yoga name:key value:obj];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendYoga:(YGLayout *)yoga name:(NSString *)name value:(id)value {
|
|
|
|
if ([name isEqualToString:@"direction"]) {
|
|
|
|
yoga.direction = (YGDirection) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"flexDirection"]) {
|
|
|
|
yoga.flexDirection = (YGFlexDirection) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"justifyContent"]) {
|
|
|
|
yoga.justifyContent = (YGJustify) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"alignContent"]) {
|
|
|
|
yoga.alignContent = (YGAlign) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"alignItems"]) {
|
|
|
|
yoga.alignItems = (YGAlign) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"alignSelf"]) {
|
|
|
|
yoga.alignSelf = (YGAlign) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"positionType"]) {
|
|
|
|
yoga.position = (YGPositionType) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"flexWrap"]) {
|
|
|
|
yoga.flexWrap = (YGWrap) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"overFlow"]) {
|
|
|
|
yoga.overflow = (YGOverflow) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"display"]) {
|
|
|
|
yoga.display = (YGDisplay) [(NSNumber *) value integerValue];
|
|
|
|
} else if ([name isEqualToString:@"flex"]) {
|
|
|
|
yoga.flex = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"flexGrow"]) {
|
|
|
|
yoga.flexGrow = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"flexShrink"]) {
|
|
|
|
yoga.flexShrink = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"flexBasis"]) {
|
|
|
|
yoga.flexBasis = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginLeft"]) {
|
|
|
|
yoga.marginLeft = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginRight"]) {
|
|
|
|
yoga.marginRight = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginTop"]) {
|
|
|
|
yoga.marginTop = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginBottom"]) {
|
|
|
|
yoga.marginBottom = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginStart"]) {
|
|
|
|
yoga.marginStart = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginEnd"]) {
|
|
|
|
yoga.marginEnd = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginHorizontal"]) {
|
|
|
|
yoga.marginHorizontal = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"marginVertical"]) {
|
|
|
|
yoga.marginVertical = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"margin"]) {
|
|
|
|
yoga.margin = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingLeft"]) {
|
|
|
|
yoga.paddingLeft = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingRight"]) {
|
|
|
|
yoga.paddingRight = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingTop"]) {
|
|
|
|
yoga.paddingTop = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingBottom"]) {
|
|
|
|
yoga.paddingBottom = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingStart"]) {
|
|
|
|
yoga.paddingStart = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingEnd"]) {
|
|
|
|
yoga.paddingEnd = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingHorizontal"]) {
|
|
|
|
yoga.paddingHorizontal = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"paddingVertical"]) {
|
|
|
|
yoga.paddingVertical = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"padding"]) {
|
|
|
|
yoga.padding = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"borderLeftWidth"]) {
|
|
|
|
yoga.borderLeftWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderRightWidth"]) {
|
|
|
|
yoga.borderRightWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderTopWidth"]) {
|
|
|
|
yoga.borderTopWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderBottomWidth"]) {
|
|
|
|
yoga.borderBottomWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderStartWidth"]) {
|
|
|
|
yoga.borderStartWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderEndWidth"]) {
|
|
|
|
yoga.borderEndWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"borderWidth"]) {
|
|
|
|
yoga.borderWidth = [(NSNumber *) value floatValue];
|
|
|
|
} else if ([name isEqualToString:@"left"]) {
|
|
|
|
yoga.left = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"right"]) {
|
|
|
|
yoga.right = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"top"]) {
|
|
|
|
yoga.top = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"bottom"]) {
|
|
|
|
yoga.bottom = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"start"]) {
|
|
|
|
yoga.start = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"end"]) {
|
|
|
|
yoga.end = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"width"]) {
|
|
|
|
yoga.width = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"height"]) {
|
|
|
|
yoga.height = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"minWidth"]) {
|
|
|
|
yoga.minWidth = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"minHeight"]) {
|
|
|
|
yoga.minHeight = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"maxWidth"]) {
|
|
|
|
yoga.maxWidth = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"maxHeight"]) {
|
|
|
|
yoga.maxHeight = [self translateYGValueFromProperty:value];
|
|
|
|
} else if ([name isEqualToString:@"aspectRatio"]) {
|
|
|
|
yoga.aspectRatio = [(NSNumber *) value floatValue];
|
|
|
|
} else {
|
|
|
|
NSLog(@"Should not exists in flex box:%@,%@", name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (YGValue)translateYGValueFromProperty:(id)prop {
|
|
|
|
if ([prop isKindOfClass:[NSDictionary class]]) {
|
|
|
|
NSNumber *type = prop[@"type"];
|
|
|
|
NSNumber *value = prop[@"value"];
|
|
|
|
switch (type.integerValue) {
|
|
|
|
case YGUnitPoint:
|
|
|
|
return YGPointValue(value.floatValue);
|
|
|
|
case YGUnitPercent:
|
|
|
|
return YGPercentValue(value.floatValue);
|
|
|
|
case YGUnitUndefined:
|
|
|
|
return YGValueUndefined;
|
|
|
|
default:
|
|
|
|
return YGValueAuto;
|
|
|
|
}
|
|
|
|
} else if ([prop isKindOfClass:[NSNumber class]]) {
|
|
|
|
return YGPointValue([prop floatValue]);
|
|
|
|
} else {
|
|
|
|
return YGValueAuto;
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 20:01:22 +08:00
|
|
|
@end
|