stylesheet.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {
  2. type TextStyle,
  3. type ViewStyle,
  4. StyleSheet,
  5. Platform
  6. } from "react-native";
  7. import type {
  8. ICodeEditorDynamicStyleProps
  9. } from "./type";
  10. import {
  11. webStyle
  12. } from "../../utils";
  13. const stylesheet = StyleSheet.create({
  14. editorInput: {
  15. fontFamily: Platform.OS === "web" ? "Consolas, 'Courier New', monospace" : "monospace",
  16. backgroundColor: "transparent",
  17. textAlignVertical: "top",
  18. position: "absolute",
  19. color: "transparent",
  20. fontWeight: "400",
  21. letterSpacing: 0,
  22. borderWidth: 0,
  23. lineHeight: 24,
  24. fontSize: 14,
  25. padding: 0,
  26. margin: 0,
  27. bottom: 0,
  28. right: 0,
  29. left: 0,
  30. top: 0,
  31. ...webStyle({
  32. fontVariantLigatures: "none",
  33. caretColor: "inherit",
  34. outline: "none",
  35. resize: "none"
  36. })
  37. },
  38. content: {
  39. fontFamily: Platform.OS === "web" ? "Consolas, 'Courier New', monospace" : "monospace",
  40. fontWeight: "400",
  41. letterSpacing: 0,
  42. ...webStyle({
  43. whiteSpace: Platform.OS === "web" ? "pre" : "pre-wrap",
  44. fontVariantLigatures: "none"
  45. })
  46. },
  47. codeText: {
  48. fontFamily: Platform.OS === "web" ? "Consolas, 'Courier New', monospace" : "monospace",
  49. fontWeight: "400",
  50. letterSpacing: 0,
  51. fontSize: 14,
  52. ...webStyle({
  53. fontVariantLigatures: "none"
  54. })
  55. },
  56. lineNumber: {
  57. fontFamily: Platform.OS === "web" ? "monospace" : "System",
  58. textAlign: "right",
  59. marginRight: 4,
  60. lineHeight: 24,
  61. fontSize: 12,
  62. ...webStyle({
  63. userSelect: "none"
  64. })
  65. },
  66. toolbarContainer: {
  67. justifyContent: "flex-start",
  68. borderBottomWidth: 0,
  69. alignItems: "center",
  70. flexDirection: "row",
  71. position: "relative",
  72. paddingVertical: 4,
  73. flexWrap: "wrap",
  74. zIndex: 1
  75. },
  76. gutter: {
  77. justifyContent: "flex-end",
  78. alignItems: "flex-start",
  79. flexDirection: "row",
  80. borderRightWidth: 1,
  81. ...webStyle({
  82. userSelect: "none"
  83. })
  84. },
  85. lineRow: {
  86. alignItems: "flex-start",
  87. flexDirection: "row",
  88. minHeight: 24
  89. },
  90. container: {
  91. flexDirection: "column",
  92. position: "relative",
  93. width: "100%"
  94. },
  95. editorContainer: {
  96. position: "relative",
  97. zIndex: 0
  98. },
  99. scrollContent: {
  100. minWidth: "100%",
  101. flexGrow: 1
  102. },
  103. text: {
  104. lineHeight: 24
  105. },
  106. scrollView: {
  107. flex: 1
  108. }
  109. });
  110. export const useStyles = ({
  111. isLineNumberVisible = true,
  112. isToolbarVisible = false,
  113. hasLanguage = false,
  114. spreadBehaviour,
  115. typography,
  116. radiuses,
  117. borders,
  118. colors,
  119. spaces
  120. }: ICodeEditorDynamicStyleProps) => {
  121. let gutterPaddingRight = 6;
  122. let gutterMarginRight = 8;
  123. let gutterWidth = 32;
  124. if (!isLineNumberVisible) {
  125. gutterPaddingRight = 0;
  126. gutterMarginRight = 0;
  127. gutterWidth = 0;
  128. }
  129. const languageMarginLeft = spaces.spacingSm;
  130. const lineHeight = (typography.bodyMediumSize?.lineHeight as number) || 24;
  131. const styles = {
  132. container: {
  133. alignSelf: spreadBehaviour === "stretch" ? "stretch" : (spreadBehaviour === "baseline" ? "baseline" : "auto"),
  134. width: spreadBehaviour === "baseline" ? "auto" : "100%",
  135. marginVertical: spaces.spacingSm
  136. } as ViewStyle,
  137. editorInput: {
  138. ...typography.bodyMediumSize,
  139. paddingLeft: (isLineNumberVisible ? (gutterWidth + gutterMarginRight) : spaces.spacingSm),
  140. fontFamily: Platform.OS === "web" ? "Consolas, 'Courier New', monospace" : "monospace",
  141. paddingTop: hasLanguage ? (lineHeight + spaces.spacingSm) : 0,
  142. paddingRight: spaces.spacingMd,
  143. ...webStyle({
  144. caretColor: colors.content.text.high
  145. })
  146. } as TextStyle,
  147. text: {
  148. ...typography.bodyMediumSize,
  149. fontFamily: Platform.OS === "web" ? "Consolas, 'Courier New', monospace" : "monospace"
  150. } as TextStyle,
  151. editorContainer: {
  152. paddingTop: isToolbarVisible ? spaces.spacingXs : spaces.spacingMd,
  153. borderTopRightRadius: isToolbarVisible ? 0 : radiuses.form,
  154. borderTopLeftRadius: isToolbarVisible ? 0 : radiuses.form,
  155. backgroundColor: colors.content.container.subtle,
  156. borderColor: colors.content.border.subtle,
  157. borderBottomRightRadius: radiuses.form,
  158. borderBottomLeftRadius: radiuses.form,
  159. paddingHorizontal: spaces.spacingMd,
  160. paddingBottom: spaces.spacingMd,
  161. borderWidth: borders.line,
  162. borderTopWidth: isToolbarVisible ? 0 : borders.line
  163. } as ViewStyle,
  164. lineRow: {
  165. marginLeft: !isLineNumberVisible ? spaces.spacingSm : 0,
  166. minHeight: lineHeight,
  167. height: lineHeight
  168. } as ViewStyle,
  169. toolbarContainer: {
  170. backgroundColor: colors.content.container.subtle,
  171. borderColor: colors.content.border.subtle,
  172. borderTopRightRadius: radiuses.form,
  173. paddingHorizontal: spaces.spacingMd,
  174. borderTopLeftRadius: radiuses.form,
  175. paddingBottom: spaces.spacingXs,
  176. borderRightWidth: borders.line,
  177. borderLeftWidth: borders.line,
  178. borderTopWidth: borders.line,
  179. paddingTop: spaces.spacingMd,
  180. gap: spaces.spacingXs
  181. } as ViewStyle,
  182. toolbarActionBtn: {
  183. paddingHorizontal: spaces.spacingMd,
  184. paddingVertical: spaces.spacingSm
  185. } as ViewStyle,
  186. gutter: {
  187. paddingRight: gutterPaddingRight,
  188. marginRight: gutterMarginRight,
  189. width: gutterWidth
  190. } as ViewStyle,
  191. languageText: {
  192. marginBottom: spaces.spacingSm,
  193. color: colors.content.text.low,
  194. marginLeft: languageMarginLeft,
  195. ...webStyle({
  196. userSelect: "none"
  197. })
  198. } as TextStyle
  199. };
  200. return styles;
  201. };
  202. export default stylesheet;