Skip to content

Commit

Permalink
Merge branch 'main' into @maksg/performance-fixes-ios
Browse files Browse the repository at this point in the history
  • Loading branch information
maksg committed Sep 12, 2024
2 parents c9aef26 + a246d77 commit 2672153
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions ios/MarkdownCommitHook.mm
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,53 @@
}
}

// apply markdown
auto newString = [utils parseMarkdown:nsAttributedString
withAttributes:defaultNSTextAttributes];

// create a clone of the old TextInputState and update the
// attributed string box to point to the string with markdown
// applied
auto newStateData = std::make_shared<TextInputState>(stateData);
newStateData->attributedStringBox =
RCTAttributedStringBoxFromNSAttributedString(newString);

// clone the text input with the new state
return node.clone({
.state = std::make_shared<const ConcreteState<TextInputState>>(
newStateData, textInputState),
});
});
} else if (stateData.attributedStringBox.getMode() ==
AttributedStringBox::Mode::OpaquePointer) {
rootNode = rootNode->cloneTree(
nodes.textInput->getFamily(),
[&nodes, &textInputState, &stateData,
fontSizeMultiplier](const ShadowNode &node) {
const auto &markdownProps = *std::static_pointer_cast<
MarkdownTextInputDecoratorViewProps const>(
nodes.decorator->getProps());
const auto &textInputProps =
*std::static_pointer_cast<TextInputProps const>(
nodes.textInput->getProps());

const auto defaultTextAttributes =
textInputProps.getEffectiveTextAttributes(fontSizeMultiplier);
const auto defaultNSTextAttributes =
RCTNSTextAttributesFromTextAttributes(defaultTextAttributes);

// this can possibly be optimized
RCTMarkdownStyle *markdownStyle = [[RCTMarkdownStyle alloc]
initWithStruct:markdownProps.markdownStyle];
RCTMarkdownUtils *utils = [[RCTMarkdownUtils alloc] init];
[utils setMarkdownStyle:markdownStyle];

// convert the attibuted string stored in state to
// NSAttributedString
auto nsAttributedString =
RCTNSAttributedStringFromAttributedStringBox(
stateData.attributedStringBox);

// apply markdown
auto newString = [utils parseMarkdown:nsAttributedString
withAttributes:defaultNSTextAttributes];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@expensify/react-native-live-markdown",
"version": "0.1.141",
"version": "0.1.142",
"description": "Drop-in replacement for React Native's TextInput component with Markdown formatting.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 2672153

Please sign in to comment.