Skip to content

[NOT A BUG] How to pass sharedValue to custom parser worklet #611

Description

@OzymandiasTheGreat

First let me apologize for posting a question, I don't see that you have discussions enabled.
Also this might be reanimated question, I don't have much experience with either, so I'm not sure.

I'm trying to build a rich text editor based on live markdown, but using custom parser that depends on external state rather than parsing input on every render. To do this I store the external state in a sharedValue that I update from js and expect to see the changed values in parser worklet.
However this doesn't seem to work. No matter what I do the sharedValue.value returns initial value.
Doesn't matter if I access it on js thread (expected, since it's async) or UI (or whatever thread parser runs on) thread.

I'm putting my code below, maybe you can tell me what I'm doing wrong.

export interface RichTextInputChangeEventData extends TextInputChangeEventData {
  fragments?: Fragment[]
}

export interface RichTextInputProps extends TextInputProps {
  onChange?: (e: NativeSyntheticEvent<RichTextInputChangeEventData>) => void
}

type RichTextInputRef = MarkdownTextInput & { toggleBold: () => void }

const RichTextInput = forwardRef<RichTextInputRef, RichTextInputProps>(
  (props, ref) => {
    const inputRef = useRef<MarkdownTextInput>(null)
    const fragments = useSharedValue<Fragment[]>([])

    const toggleBold = useCallback(() => {
      if (fragments.value.length) {
        fragments.value = []
      } else {
        fragments.value = [
          {
            type: DISPLAY_TYPE.BOLD,
            start: 0,
            length:1,
            content: null,
          },
        ]
      }
    }, [])

    // Checking sharedValue
    useAnimatedReaction(
      () => fragments.value,
      (current, previous) => console.log({ current, previous }),
    )

    useImperativeHandle(
      ref,
      () =>
        ({
          ...inputRef.current,
          toggleBold,
        } as RichTextInputRef),
    )

    const onChange = useCallback(
      (e: NativeSyntheticEvent<TextInputChangeEventData>) => {
        props.onChange?.({
          ...e,
          nativeEvent: {
            ...e.nativeEvent,
            fragments: fragments.value,
          },
        })
      },
      [fragments, props.onChange],
    )

    const displayParser = useCallback(
      (text: string) => {
        "worklet"
        console.log("PARSER", fragments.value)
        return fragments.value
          .map((fragment) => {
            let type: MarkdownType
            switch (fragment.type) {
              case DISPLAY_TYPE.BOLD:
                type = "bold"
                break
              default:
                return console.error("Unknown Display type")
            }
            return {
              type,
              start: fragment.start,
              length: text.length,
            }
          })
          .filter((fragment) => !!fragment)
      },
      [fragments],
    )

    return (
      <MarkdownTextInput
        {...props}
        ref={inputRef}
        parser={displayParser}
        onChange={onChange}
      />
    )
  },
)

export default memo(RichTextInput)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions