Files
divkit/client/ios/DivKit/Extensions/DivTextRangeBuilderExtensions.swift
booster 288bb54812 Range builder implemented
commit_hash:8b1998d149e5374f192de21a9dbc54e278c64108
2026-06-02 00:47:23 +03:00

48 lines
1.3 KiB
Swift

import Foundation
import LayoutKit
import VGSL
extension DivText {
func makeRanges(
_ ranges: [Range]?,
rangeBuilder: RangeBuilder?,
context: DivBlockModelingContext
) -> [(Range, DivBlockModelingContext)] {
if let rangeBuilder {
return rangeBuilder.makeRanges(context: context)
}
return (ranges ?? []).map { ($0, context) }
}
}
extension DivText.RangeBuilder {
func makeRanges(
context: DivBlockModelingContext
) -> [(DivText.Range, DivBlockModelingContext)] {
let items = resolveData(context.expressionResolver) ?? []
return items.enumerated().compactMap { index, item in
let itemDict = item as? DivDictionary ?? [:]
let itemContext = context.modifying(
pathSuffix: String(index),
prototypeParams: PrototypeParams(
index: index,
variableName: dataElementName,
value: itemDict
)
)
itemContext.variablesStorage.initializeIfNeeded(path: itemContext.path, variables: [
DivVariableName(rawValue: dataElementName): .dict(itemDict),
"index": .integer(index),
])
let prototype = prototypes.first(where: {
$0.resolveSelector(itemContext.expressionResolver)
})
return if let prototype {
(prototype.range, itemContext)
} else {
nil
}
}
}
}