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
This commit is contained in:
Valentin Shergin
2019-12-03 14:19:52 -08:00
committed by Facebook Github Bot
parent 0a21cca9a2
commit 7368f79169
2 changed files with 34 additions and 0 deletions
@@ -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<Fragment, 1>;
/*
@@ -7,6 +7,7 @@
#pragma once
#include <folly/Conv.h>
#include <folly/dynamic.h>
#include <react/attributedstring/AttributedString.h>
#include <react/attributedstring/ParagraphAttributes.h>
@@ -444,6 +445,26 @@ inline ParagraphAttributes convertRawProp(
return paragraphAttributes;
}
inline void fromRawValue(
RawValue const &value,
AttributedString::Range &result) {
auto map = (better::map<std::string, int>)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<std::string>(range.location) +
", length: " + folly::to<std::string>(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