From 7368f7916960282edfbbfefd8a82117bb0d9e460 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 3 Dec 2019 14:17:46 -0800 Subject: [PATCH] Fabric: Introducing AttributedString::Range Summary: We need this type to work with string ranges (e.g. working with selection ranges). This diff implements parsing and printing that as well. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D18607656 fbshipit-source-id: f2cfd7c5b7ba9f225a9a0c5a078947a220b2f30d --- .../attributedstring/AttributedString.h | 6 ++++ .../fabric/attributedstring/conversions.h | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/ReactCommon/fabric/attributedstring/AttributedString.h b/ReactCommon/fabric/attributedstring/AttributedString.h index 7a952e4bfcf..8b74d9abca0 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.h +++ b/ReactCommon/fabric/attributedstring/AttributedString.h @@ -51,6 +51,12 @@ class AttributedString : public Sealable, public DebugStringConvertible { bool operator!=(const Fragment &rhs) const; }; + class Range { + public: + int location{0}; + int length{0}; + }; + using Fragments = better::small_vector; /* diff --git a/ReactCommon/fabric/attributedstring/conversions.h b/ReactCommon/fabric/attributedstring/conversions.h index 05528837edb..a5d3b73a07b 100644 --- a/ReactCommon/fabric/attributedstring/conversions.h +++ b/ReactCommon/fabric/attributedstring/conversions.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -444,6 +445,26 @@ inline ParagraphAttributes convertRawProp( return paragraphAttributes; } +inline void fromRawValue( + RawValue const &value, + AttributedString::Range &result) { + auto map = (better::map)value; + + auto start = map.find("start"); + if (start != map.end()) { + result.location = start->second; + } + auto end = map.find("end"); + if (end != map.end()) { + result.length = start->second - result.location; + } +} + +inline std::string toString(AttributedString::Range const &range) { + return "{location: " + folly::to(range.location) + + ", length: " + folly::to(range.length) + "}"; +} + #ifdef ANDROID inline folly::dynamic toDynamic( @@ -562,6 +583,13 @@ inline folly::dynamic toDynamic(const AttributedString &attributedString) { return value; } +inline folly::dynamic toDynamic(AttributedString::Range const &range) { + folly::dynamic dynamicValue = folly::dynamic::object(); + dynamicValue["location"] = range.location; + dynamicValue["length"] = range.length; + return dynamicValue; +} + #endif } // namespace react