Changeset View
Changeset View
Standalone View
Standalone View
native/markdown/rules.react.js
// @flow | // @flow | ||||
import _memoize from 'lodash/memoize.js'; | import _memoize from 'lodash/memoize.js'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { Text, View, Platform } from 'react-native'; | import { Text, View, Platform } from 'react-native'; | ||||
import * as SimpleMarkdown from 'simple-markdown'; | import * as SimpleMarkdown from 'simple-markdown'; | ||||
import * as SharedMarkdown from 'lib/shared/markdown.js'; | import * as SharedMarkdown from 'lib/shared/markdown.js'; | ||||
import { chatMentionRegex } from 'lib/shared/mention-utils.js'; | import { chatMentionRegex } from 'lib/shared/mention-utils.js'; | ||||
import type { | import type { | ||||
ChatMentionCandidates, | ChatMentionCandidates, | ||||
RelativeMemberInfo, | |||||
ThreadInfo, | ThreadInfo, | ||||
} from 'lib/types/thread-types.js'; | } from 'lib/types/thread-types.js'; | ||||
import MarkdownChatMention from './markdown-chat-mention.react.js'; | import MarkdownChatMention from './markdown-chat-mention.react.js'; | ||||
import MarkdownLink from './markdown-link.react.js'; | import MarkdownLink from './markdown-link.react.js'; | ||||
import MarkdownParagraph from './markdown-paragraph.react.js'; | import MarkdownParagraph from './markdown-paragraph.react.js'; | ||||
import MarkdownSpoiler from './markdown-spoiler.react.js'; | import MarkdownSpoiler from './markdown-spoiler.react.js'; | ||||
import MarkdownUserMention from './markdown-user-mention.react.js'; | import MarkdownUserMention from './markdown-user-mention.react.js'; | ||||
▲ Show 20 Lines • Show All 337 Lines • ▼ Show 20 Lines | const fullMarkdownRules: boolean => MarkdownRules = _memoize(useDarkStyle => { | ||||
}; | }; | ||||
}); | }); | ||||
function useTextMessageRulesFunc( | function useTextMessageRulesFunc( | ||||
threadInfo: ThreadInfo, | threadInfo: ThreadInfo, | ||||
chatMentionCandidates: ChatMentionCandidates, | chatMentionCandidates: ChatMentionCandidates, | ||||
): (useDarkStyle: boolean) => MarkdownRules { | ): (useDarkStyle: boolean) => MarkdownRules { | ||||
const { members } = threadInfo; | const { members } = threadInfo; | ||||
const membersMap = SharedMarkdown.useMemberMapForUserMentions(members); | |||||
return React.useMemo( | return React.useMemo( | ||||
() => | () => | ||||
_memoize<[boolean], MarkdownRules>((useDarkStyle: boolean) => | _memoize<[boolean], MarkdownRules>((useDarkStyle: boolean) => | ||||
textMessageRules(members, chatMentionCandidates, useDarkStyle), | textMessageRules(chatMentionCandidates, useDarkStyle, membersMap), | ||||
), | ), | ||||
[members, chatMentionCandidates], | [chatMentionCandidates, membersMap], | ||||
); | ); | ||||
} | } | ||||
function textMessageRules( | function textMessageRules( | ||||
members: $ReadOnlyArray<RelativeMemberInfo>, | |||||
chatMentionCandidates: ChatMentionCandidates, | chatMentionCandidates: ChatMentionCandidates, | ||||
useDarkStyle: boolean, | useDarkStyle: boolean, | ||||
membersMap: $ReadOnlyMap<string, string>, | |||||
): MarkdownRules { | ): MarkdownRules { | ||||
const baseRules = fullMarkdownRules(useDarkStyle); | const baseRules = fullMarkdownRules(useDarkStyle); | ||||
const membersMap = SharedMarkdown.createMemberMapForUserMentions(members); | |||||
return { | return { | ||||
...baseRules, | ...baseRules, | ||||
simpleMarkdownRules: { | simpleMarkdownRules: { | ||||
...baseRules.simpleMarkdownRules, | ...baseRules.simpleMarkdownRules, | ||||
userMention: { | userMention: { | ||||
...SimpleMarkdown.defaultRules.strong, | ...SimpleMarkdown.defaultRules.strong, | ||||
match: SharedMarkdown.matchUserMentions(membersMap), | match: SharedMarkdown.matchUserMentions(membersMap), | ||||
Show All 33 Lines | simpleMarkdownRules: { | ||||
<Text key={state.key}>{node.content}</Text> | <Text key={state.key}>{node.content}</Text> | ||||
), | ), | ||||
}, | }, | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
let defaultTextMessageRules = null; | let defaultTextMessageRules = null; | ||||
const defaultMembersMap = new Map<string, string>(); | |||||
function getDefaultTextMessageRules( | function getDefaultTextMessageRules( | ||||
overrideDefaultChatMentionCandidates: ChatMentionCandidates = {}, | overrideDefaultChatMentionCandidates: ChatMentionCandidates = {}, | ||||
): MarkdownRules { | ): MarkdownRules { | ||||
if (Object.keys(overrideDefaultChatMentionCandidates).length > 0) { | if (Object.keys(overrideDefaultChatMentionCandidates).length > 0) { | ||||
return textMessageRules([], overrideDefaultChatMentionCandidates, false); | return textMessageRules( | ||||
overrideDefaultChatMentionCandidates, | |||||
false, | |||||
defaultMembersMap, | |||||
); | |||||
} | } | ||||
if (!defaultTextMessageRules) { | if (!defaultTextMessageRules) { | ||||
defaultTextMessageRules = textMessageRules([], {}, false); | defaultTextMessageRules = textMessageRules({}, false, defaultMembersMap); | ||||
} | } | ||||
return defaultTextMessageRules; | return defaultTextMessageRules; | ||||
} | } | ||||
export { | export { | ||||
inlineMarkdownRules, | inlineMarkdownRules, | ||||
useTextMessageRulesFunc, | useTextMessageRulesFunc, | ||||
getDefaultTextMessageRules, | getDefaultTextMessageRules, | ||||
}; | }; |