LogBoxInspectorCodeFrame.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow strict-local
  8. * @format
  9. */
  10. 'use strict';
  11. import * as React from 'react';
  12. import Platform from '../../Utilities/Platform';
  13. import ScrollView from '../../Components/ScrollView/ScrollView';
  14. import StyleSheet from '../../StyleSheet/StyleSheet';
  15. import Text from '../../Text/Text';
  16. import View from '../../Components/View/View';
  17. import * as LogBoxStyle from './LogBoxStyle';
  18. import type {CodeFrame} from '../Data/parseLogBoxLog';
  19. import LogBoxButton from './LogBoxButton';
  20. import openFileInEditor from '../../Core/Devtools/openFileInEditor';
  21. import AnsiHighlight from './AnsiHighlight';
  22. import LogBoxInspectorSection from './LogBoxInspectorSection';
  23. import * as LogBoxData from '../Data/LogBoxData';
  24. type Props = $ReadOnly<{|
  25. codeFrame: ?CodeFrame,
  26. |}>;
  27. function LogBoxInspectorCodeFrame(props: Props): React.Node {
  28. const codeFrame = props.codeFrame;
  29. if (codeFrame == null) {
  30. return null;
  31. }
  32. function getFileName() {
  33. const matches = /[^/]*$/.exec(codeFrame.fileName);
  34. if (matches && matches.length > 0) {
  35. return matches[0];
  36. }
  37. return codeFrame.fileName;
  38. }
  39. function getLocation() {
  40. const location = codeFrame.location;
  41. if (location != null) {
  42. return ` (${location.row}:${
  43. location.column + 1 /* Code frame columns are zero indexed */
  44. })`;
  45. }
  46. return null;
  47. }
  48. return (
  49. <LogBoxInspectorSection heading="Source" action={<AppInfo />}>
  50. <View style={styles.box}>
  51. <View style={styles.frame}>
  52. <ScrollView horizontal>
  53. <AnsiHighlight style={styles.content} text={codeFrame.content} />
  54. </ScrollView>
  55. </View>
  56. <LogBoxButton
  57. backgroundColor={{
  58. default: 'transparent',
  59. pressed: LogBoxStyle.getBackgroundDarkColor(1),
  60. }}
  61. style={styles.button}
  62. onPress={() => {
  63. openFileInEditor(codeFrame.fileName, codeFrame.location?.row ?? 0);
  64. }}>
  65. <Text style={styles.fileText}>
  66. {getFileName()}
  67. {getLocation()}
  68. </Text>
  69. </LogBoxButton>
  70. </View>
  71. </LogBoxInspectorSection>
  72. );
  73. }
  74. function AppInfo() {
  75. const appInfo = LogBoxData.getAppInfo();
  76. if (appInfo == null) {
  77. return null;
  78. }
  79. return (
  80. <LogBoxButton
  81. backgroundColor={{
  82. default: 'transparent',
  83. pressed: appInfo.onPress
  84. ? LogBoxStyle.getBackgroundColor(1)
  85. : 'transparent',
  86. }}
  87. style={appInfoStyles.buildButton}
  88. onPress={appInfo.onPress}>
  89. <Text style={appInfoStyles.text}>
  90. {appInfo.appVersion} ({appInfo.engine})
  91. </Text>
  92. </LogBoxButton>
  93. );
  94. }
  95. const appInfoStyles = StyleSheet.create({
  96. text: {
  97. color: LogBoxStyle.getTextColor(0.4),
  98. fontSize: 12,
  99. lineHeight: 12,
  100. },
  101. buildButton: {
  102. flex: 0,
  103. flexGrow: 0,
  104. paddingVertical: 4,
  105. paddingHorizontal: 5,
  106. borderRadius: 5,
  107. marginRight: -8,
  108. },
  109. });
  110. const styles = StyleSheet.create({
  111. box: {
  112. backgroundColor: LogBoxStyle.getBackgroundColor(),
  113. marginLeft: 10,
  114. marginRight: 10,
  115. marginTop: 5,
  116. borderRadius: 3,
  117. },
  118. frame: {
  119. padding: 10,
  120. borderBottomColor: LogBoxStyle.getTextColor(0.1),
  121. borderBottomWidth: 1,
  122. },
  123. button: {
  124. paddingTop: 10,
  125. paddingBottom: 10,
  126. },
  127. content: {
  128. color: LogBoxStyle.getTextColor(1),
  129. fontSize: 12,
  130. includeFontPadding: false,
  131. lineHeight: 20,
  132. fontFamily: Platform.select({android: 'monospace', ios: 'Menlo'}),
  133. },
  134. fileText: {
  135. color: LogBoxStyle.getTextColor(0.5),
  136. textAlign: 'center',
  137. flex: 1,
  138. fontSize: 12,
  139. includeFontPadding: false,
  140. lineHeight: 16,
  141. fontFamily: Platform.select({android: 'monospace', ios: 'Menlo'}),
  142. },
  143. });
  144. export default LogBoxInspectorCodeFrame;