mattermost-mobile-upstream/app/components/markdown/markdown_code_block/index.tsx
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

230 lines
7.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useManagedConfig} from '@mattermost/react-native-emm';
import Clipboard from '@react-native-clipboard/clipboard';
import React, {useCallback, useMemo} from 'react';
import {useIntl} from 'react-intl';
import {Keyboard, StyleSheet, Text, type TextStyle, TouchableOpacity, View} from 'react-native';
import FormattedText from '@components/formatted_text';
import SlideUpPanelItem, {ITEM_HEIGHT} from '@components/slide_up_panel_item';
import {Screens} from '@constants';
import {usePreventDoubleTap} from '@hooks/utils';
import {bottomSheet, dismissBottomSheet, navigateToScreen} from '@screens/navigation';
import {bottomSheetSnapPoint} from '@utils/helpers';
import {getHighlightLanguageFromNameOrAlias, getHighlightLanguageName} from '@utils/markdown';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
import type {SyntaxHiglightProps} from '@typings/components/syntax_highlight';
type MarkdownCodeBlockProps = {
language: string;
content: string;
textStyle: TextStyle;
theme: Theme;
};
const MAX_LINES = 4;
let syntaxHighlighter: (props: SyntaxHiglightProps) => React.JSX.Element;
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
return {
bottomSheet: {
flex: 1,
},
container: {
borderColor: changeOpacity(theme.centerChannelColor, 0.15),
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
flexDirection: 'row',
},
code: {
flexDirection: 'row',
overflow: 'scroll', // Doesn't actually cause a scrollbar, but stops text from wrapping
},
plusMoreLinesText: {
color: changeOpacity(theme.centerChannelColor, 0.4),
fontSize: 11,
marginTop: 2,
},
language: {
alignItems: 'center',
backgroundColor: theme.sidebarHeaderBg,
justifyContent: 'center',
opacity: 0.8,
padding: 6,
position: 'absolute',
right: 0,
top: 0,
},
languageText: {
color: theme.sidebarHeaderTextColor,
fontSize: 12,
},
};
});
const MarkdownCodeBlock = ({language = '', content, textStyle, theme}: MarkdownCodeBlockProps) => {
const intl = useIntl();
const managedConfig = useManagedConfig<ManagedConfig>();
const style = getStyleSheet(theme);
const SyntaxHighlighter = useMemo(() => {
if (!syntaxHighlighter) {
syntaxHighlighter = require('@components/syntax_highlight').default;
}
return syntaxHighlighter;
}, []);
const handlePress = usePreventDoubleTap(useCallback(() => {
const languageDisplayName = getHighlightLanguageName(language);
let title: string;
if (languageDisplayName) {
title = intl.formatMessage(
{
id: 'mobile.routes.code',
defaultMessage: '{language} Code',
},
{
language: languageDisplayName,
},
);
} else {
title = intl.formatMessage({
id: 'mobile.routes.code.noLanguage',
defaultMessage: 'Code',
});
}
const passProps = {
code: content,
language: getHighlightLanguageFromNameOrAlias(language),
textStyle,
title,
};
Keyboard.dismiss();
requestAnimationFrame(() => {
navigateToScreen(Screens.CODE, passProps);
});
}, [content, intl, language, textStyle]));
const handleLongPress = useCallback(() => {
if (managedConfig?.copyAndPasteProtection !== 'true') {
const renderContent = () => {
return (
<View
testID='at_mention.bottom_sheet'
style={style.bottomSheet}
>
<SlideUpPanelItem
leftIcon='content-copy'
onPress={() => {
dismissBottomSheet();
Clipboard.setString(content);
}}
testID='at_mention.bottom_sheet.copy_code'
text={intl.formatMessage({id: 'mobile.markdown.code.copy_code', defaultMessage: 'Copy Code'})}
/>
<SlideUpPanelItem
destructive={true}
leftIcon='cancel'
onPress={() => {
dismissBottomSheet();
}}
testID='at_mention.bottom_sheet.cancel'
text={intl.formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'})}
/>
</View>
);
};
bottomSheet(renderContent, [1, bottomSheetSnapPoint(2, ITEM_HEIGHT)]);
}
}, [managedConfig?.copyAndPasteProtection, intl, style.bottomSheet, content]);
const trimContent = (text: string) => {
const lines = text.split('\n');
const numberOfLines = lines.length;
if (numberOfLines > MAX_LINES) {
return {
content: lines.slice(0, MAX_LINES).join('\n'),
numberOfLines,
};
}
return {
content: text,
numberOfLines,
};
};
const renderLanguageBlock = () => {
if (language) {
const languageDisplayName = getHighlightLanguageName(language);
if (languageDisplayName) {
return (
<View style={style.language}>
<Text style={style.languageText}>
{languageDisplayName}
</Text>
</View>
);
}
}
return null;
};
const {content: codeContent, numberOfLines} = trimContent(content);
const renderPlusMoreLines = () => {
if (numberOfLines > MAX_LINES) {
return (
<FormattedText
style={style.plusMoreLinesText}
id='mobile.markdown.code.plusMoreLines'
defaultMessage='+{count, number} more {count, plural, one {line} other {lines}}'
values={{
count: numberOfLines - MAX_LINES,
}}
/>
);
}
return null;
};
return (
<>
<TouchableOpacity
onPress={handlePress}
onLongPress={handleLongPress}
testID='markdown_code_block'
>
<View
style={style.container}
pointerEvents='none'
>
<View>
<View style={style.code}>
<SyntaxHighlighter
code={codeContent}
language={getHighlightLanguageFromNameOrAlias(language)}
textStyle={textStyle}
/>
</View>
{renderPlusMoreLines()}
</View>
{renderLanguageBlock()}
</View>
</TouchableOpacity>
</>
);
};
export default MarkdownCodeBlock;