mirror of
https://github.com/status-im/react-native.git
synced 2025-02-24 15:18:10 +00:00
Summary: When a user clicks on some text, `RCTSurfaceTouchHandler` will call into a method on `RCTParagraphComponentView`. That method (i.e: `touchEventEmitter`) would be responsible for identifying the closest ancestral `<Text/>` component to which we should dispatch the `onPress` event, given the point where the user clicked. To answer this query, we'll use a data structure called `UIAttributedString`. This data structure represents a string, and a corresponding mapping from sequences of its characters to some arbitrary data. In this attributed string, we'll map sequences of characters to their closest ancestral `ParagraphShadowNode` or `TextShadowNode`. That way, when we get a click event on `RCTParagraphComponentView`, we can just look at the character that was clicked, and use that information to do a lookup in the attributed string to find the shadow node who's EventEmitter is responsible for processing the click event. Reviewed By: shergin Differential Revision: D9696904 fbshipit-source-id: a199649981ad271afa85414ce4c3f056851348be
81 lines
1.9 KiB
C++
81 lines
1.9 KiB
C++
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
#include <fabric/attributedstring/TextAttributes.h>
|
|
#include <fabric/core/Sealable.h>
|
|
#include <fabric/core/ShadowNode.h>
|
|
#include <fabric/debug/DebugStringConvertible.h>
|
|
#include <folly/Optional.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
class AttributedString;
|
|
|
|
using SharedAttributedString = std::shared_ptr<const AttributedString>;
|
|
|
|
/*
|
|
* Simple, cross-platfrom, React-specific implementation of attributed string
|
|
* (aka spanned string).
|
|
* `AttributedString` is basically a list of `Fragments` which have `string` and
|
|
* `textAttributes` + `shadowNode` associated with the `string`.
|
|
*/
|
|
class AttributedString:
|
|
public Sealable,
|
|
public DebugStringConvertible {
|
|
|
|
public:
|
|
|
|
class Fragment {
|
|
public:
|
|
std::string string;
|
|
TextAttributes textAttributes;
|
|
SharedShadowNode shadowNode;
|
|
SharedShadowNode parentShadowNode;
|
|
};
|
|
|
|
using Fragments = std::vector<Fragment>;
|
|
|
|
/*
|
|
* Appends and prepends a `fragment` to the string.
|
|
*/
|
|
void appendFragment(const Fragment &fragment);
|
|
void prependFragment(const Fragment &fragment);
|
|
|
|
/*
|
|
* Appends and prepends an `attributedString` (all its fragments) to
|
|
* the string.
|
|
*/
|
|
void appendAttributedString(const AttributedString &attributedString);
|
|
void prependAttributedString(const AttributedString &attributedString);
|
|
|
|
/*
|
|
* Returns read-only reference to a list of fragments.
|
|
*/
|
|
const Fragments &getFragments() const;
|
|
|
|
/*
|
|
* Returns a string constructed from all strings in all fragments.
|
|
*/
|
|
std::string getString() const;
|
|
|
|
#pragma mark - DebugStringConvertible
|
|
|
|
SharedDebugStringConvertibleList getDebugChildren() const override;
|
|
|
|
private:
|
|
|
|
Fragments fragments_;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|