Changeset View
Changeset View
Standalone View
Standalone View
web/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 * 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 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'; | ||||
export type MarkdownRules = { | export type MarkdownRules = { | ||||
▲ Show 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | const markdownRules: boolean => MarkdownRules = _memoize(useDarkStyle => { | ||||
}; | }; | ||||
}); | }); | ||||
function useTextMessageRulesFunc( | function useTextMessageRulesFunc( | ||||
threadInfo: ThreadInfo, | threadInfo: ThreadInfo, | ||||
chatMentionCandidates: ChatMentionCandidates, | chatMentionCandidates: ChatMentionCandidates, | ||||
): boolean => MarkdownRules { | ): 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), | ||||
), | ), | ||||
[chatMentionCandidates, members], | [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 = markdownRules(useDarkStyle); | const baseRules = markdownRules(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 31 Lines | simpleMarkdownRules: { | ||||
/> | /> | ||||
), | ), | ||||
}, | }, | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
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 { linkRules, useTextMessageRulesFunc, getDefaultTextMessageRules }; | export { linkRules, useTextMessageRulesFunc, getDefaultTextMessageRules }; |