{
  "version": 3,
  "sources": ["../../../../src/lib/ui/context/i18n.tsx"],
  "sourcesContent": ["import { TLI18n, TLI18nAdapter, TLI18nMessageInput } from '@tldraw/editor'\nimport { ReactNode, useCallback, useRef } from 'react'\nimport { FormattedMessage, useIntl } from 'react-intl'\nimport {\n\tTLUiTranslationValues,\n\tuseCurrentTranslation,\n} from '../hooks/useTranslation/useTranslation'\n\n/**\n * Values for a message rendered as an element. Alongside the primitives a string message takes, a\n * value may be an element, and a function may be given for a tag that appears in the message, which\n * receives the text that tag encloses. Either way the markup stays out of the string, so a\n * translator gets one whole sentence to reorder rather than fragments that can only be concatenated.\n *\n * @example\n *\n * ```tsx\n * <F\n * \tdefaultMessage=\"<who>{name}</who> joined. {help}\"\n * \tid=\"example.joined\"\n * \tvalues={{\n * \t\tname: 'Ada',\n * \t\twho: (chunks) => <strong>{chunks}</strong>,\n * \t\thelp: <a href=\"/help\">Need help?</a>,\n * \t}}\n * />\n * ```\n *\n * @public\n */\nexport type TLUiMessageValues = Record<\n\tstring,\n\tTLUiTranslationValues[string] | ReactNode | ((chunks: ReactNode) => ReactNode)\n>\n\n/** @public */\nexport interface TLUiMessageProps extends TLI18nMessageInput {\n\tvalues?: TLUiMessageValues\n}\n\n/**\n * Renders a translated message. Use this wherever the message is text in the document; where a\n * string is needed instead, translate the message's id through `useTranslation`.\n *\n * The `id` is optional, so a message whose id a bundler plugin injects can be rendered from source\n * that carries none. The output is wrapped in a `<span className=\"i18n-msg\">`, which lets a tool\n * tell text that came through here from text that never did.\n *\n * @example\n *\n * ```tsx\n * <F defaultMessage=\"Close\" id=\"ui.close\" />\n * ```\n *\n * @public @react\n */\nexport function F({ values, ...descriptor }: TLUiMessageProps) {\n\t// Without a plugin to inject one, react-intl throws on a missing id. Same default as\n\t// `defineMessages`, so the id an untransformed `<F>` looks up is the one it was declared under.\n\tconst id = descriptor.id ?? descriptor.defaultMessage\n\treturn (\n\t\t// Inline, so the marker span doesn't disturb the layout around it. Why it's here at all is\n\t\t// on `F` above.\n\t\t<span className=\"i18n-msg\">\n\t\t\t{/* The rule can't see a spread; `F`'s own callers are what it needs to check. */}\n\t\t\t{/* eslint-disable-next-line tldraw/enforce-default-message */}\n\t\t\t<FormattedMessage {...descriptor} id={id} values={values} />\n\t\t</span>\n\t)\n}\n\n/**\n * Builds the `TLI18nAdapter` the editor hands to code that has no access to React context,\n * such as a `ShapeUtil`. `<Tldraw />` wires this up for you; call it yourself only when composing\n * `<TldrawEditor />` by hand, and from inside a `<TldrawUiTranslationProvider />`.\n *\n * @example\n *\n * ```tsx\n * function MyEditor(props: TldrawEditorProps) {\n * \tconst i18n = useTldrawI18n()\n * \treturn <TldrawEditor {...props} i18n={i18n} />\n * }\n * ```\n *\n * @public\n */\nexport function useTldrawI18n(): TLI18nAdapter {\n\tconst translation = useCurrentTranslation()\n\tconst intl = useIntl()\n\n\t// A stable function that reads the current translation, so whatever holds the adapter \u2014 the\n\t// editor, or an app that stores it \u2014 never sees it change identity on a locale switch.\n\tconst i18nRef = useRef<TLI18n | null>(null)\n\ti18nRef.current = {\n\t\t// The requested locale, as the `IntlProvider` has it, not the catalog that loaded. They\n\t\t// differ while a catalog is in flight and for a locale the SDK ships none for, and either\n\t\t// way the loaded one is English \u2014 so a French user's dates formatted as English.\n\t\tlocale: intl.locale,\n\t\t// The loaded catalog's, deliberately: it has to agree with `useDirection`, which lays out\n\t\t// the rest of the UI.\n\t\tdir: translation.dir,\n\t\t// The whole descriptor, so react-intl formats `defaultMessage` itself for an id the\n\t\t// catalog doesn't have. Given only the id it returns the id, and the English then had to\n\t\t// be substituted in verbatim \u2014 which showed a plural message's own ICU source in a toast.\n\t\t// eslint-disable-next-line tldraw/enforce-default-message\n\t\ttranslate: (message, values) => intl.formatMessage(message, values),\n\t}\n\n\treturn useCallback(() => i18nRef.current!, [])\n}\n"],
  "mappings": "AAkEG;AAjEH,SAAoB,aAAa,cAAc;AAC/C,SAAS,kBAAkB,eAAe;AAC1C;AAAA,EAEC;AAAA,OACM;AAkDA,SAAS,EAAE,EAAE,QAAQ,GAAG,WAAW,GAAqB;AAG9D,QAAM,KAAK,WAAW,MAAM,WAAW;AACvC;AAAA;AAAA;AAAA,IAGC,oBAAC,UAAK,WAAU,YAGf,8BAAC,oBAAkB,GAAG,YAAY,IAAQ,QAAgB,GAC3D;AAAA;AAEF;AAkBO,SAAS,gBAA+B;AAC9C,QAAM,cAAc,sBAAsB;AAC1C,QAAM,OAAO,QAAQ;AAIrB,QAAM,UAAU,OAAsB,IAAI;AAC1C,UAAQ,UAAU;AAAA;AAAA;AAAA;AAAA,IAIjB,QAAQ,KAAK;AAAA;AAAA;AAAA,IAGb,KAAK,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA,IAKjB,WAAW,CAAC,SAAS,WAAW,KAAK,cAAc,SAAS,MAAM;AAAA,EACnE;AAEA,SAAO,YAAY,MAAM,QAAQ,SAAU,CAAC,CAAC;AAC9C;",
  "names": []
}
