|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "ParagraphEventEmitter.h" |
| 9 | + |
| 10 | +namespace facebook { |
| 11 | +namespace react { |
| 12 | + |
| 13 | +static jsi::Value linesMeasurementsPayload( |
| 14 | + jsi::Runtime &runtime, |
| 15 | + LinesMeasurements const &linesMeasurements) { |
| 16 | + auto payload = jsi::Object(runtime); |
| 17 | + auto lines = jsi::Array(runtime, linesMeasurements.size()); |
| 18 | + |
| 19 | + for (size_t i = 0; i < linesMeasurements.size(); ++i) { |
| 20 | + auto const &lineMeasurement = linesMeasurements[i]; |
| 21 | + auto jsiLine = jsi::Object(runtime); |
| 22 | + jsiLine.setProperty(runtime, "text", lineMeasurement.text); |
| 23 | + jsiLine.setProperty(runtime, "x", lineMeasurement.frame.origin.x); |
| 24 | + jsiLine.setProperty(runtime, "y", lineMeasurement.frame.origin.y); |
| 25 | + jsiLine.setProperty(runtime, "width", lineMeasurement.frame.size.width); |
| 26 | + jsiLine.setProperty(runtime, "height", lineMeasurement.frame.size.height); |
| 27 | + jsiLine.setProperty(runtime, "descender", lineMeasurement.descender); |
| 28 | + jsiLine.setProperty(runtime, "capHeight", lineMeasurement.capHeight); |
| 29 | + jsiLine.setProperty(runtime, "ascender", lineMeasurement.ascender); |
| 30 | + jsiLine.setProperty(runtime, "xHeight", lineMeasurement.xHeight); |
| 31 | + lines.setValueAtIndex(runtime, i, jsiLine); |
| 32 | + } |
| 33 | + |
| 34 | + payload.setProperty(runtime, "lines", lines); |
| 35 | + |
| 36 | + return payload; |
| 37 | +} |
| 38 | + |
| 39 | +void ParagraphEventEmitter::onTextLayout( |
| 40 | + LinesMeasurements const &linesMeasurements) const { |
| 41 | + dispatchEvent( |
| 42 | + "textLayout", |
| 43 | + [linesMeasurements](jsi::Runtime &runtime) { |
| 44 | + return linesMeasurementsPayload(runtime, linesMeasurements); |
| 45 | + }, |
| 46 | + EventPriority::AsynchronousBatched); |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace react |
| 50 | +} // namespace facebook |
0 commit comments