editor_style.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import 'package:appflowy/plugins/document/presentation/editor_plugins/inline_math_equation/inline_math_equation.dart';
  2. import 'package:appflowy/plugins/document/presentation/editor_plugins/inline_page/inline_page_reference.dart';
  3. import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart';
  4. import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  5. import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
  6. import 'package:collection/collection.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:google_fonts/google_fonts.dart';
  10. class EditorStyleCustomizer {
  11. EditorStyleCustomizer({
  12. required this.context,
  13. required this.padding,
  14. });
  15. final BuildContext context;
  16. final EdgeInsets padding;
  17. EditorStyle style() {
  18. if (PlatformExtension.isDesktopOrWeb) {
  19. return desktop();
  20. } else if (PlatformExtension.isMobile) {
  21. return mobile();
  22. }
  23. throw UnimplementedError();
  24. }
  25. EditorStyle desktop() {
  26. final theme = Theme.of(context);
  27. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  28. final fontFamily = context.read<DocumentAppearanceCubit>().state.fontFamily;
  29. return EditorStyle.desktop(
  30. padding: padding,
  31. backgroundColor: theme.colorScheme.surface,
  32. cursorColor: theme.colorScheme.primary,
  33. textStyleConfiguration: TextStyleConfiguration(
  34. text: baseTextStyle(fontFamily).copyWith(
  35. fontSize: fontSize,
  36. color: theme.colorScheme.onBackground,
  37. height: 1.5,
  38. ),
  39. bold: baseTextStyle(fontFamily, fontWeight: FontWeight.bold).copyWith(
  40. fontWeight: FontWeight.w600,
  41. ),
  42. italic: baseTextStyle(fontFamily).copyWith(
  43. fontStyle: FontStyle.italic,
  44. ),
  45. underline: baseTextStyle(fontFamily).copyWith(
  46. decoration: TextDecoration.underline,
  47. ),
  48. strikethrough: baseTextStyle(fontFamily).copyWith(
  49. decoration: TextDecoration.lineThrough,
  50. ),
  51. href: baseTextStyle(fontFamily).copyWith(
  52. color: theme.colorScheme.primary,
  53. decoration: TextDecoration.underline,
  54. ),
  55. code: GoogleFonts.robotoMono(
  56. textStyle: baseTextStyle(fontFamily).copyWith(
  57. fontSize: fontSize,
  58. fontWeight: FontWeight.normal,
  59. color: Colors.red,
  60. backgroundColor: theme.colorScheme.inverseSurface,
  61. ),
  62. ),
  63. ),
  64. textSpanDecorator: customizeAttributeDecorator,
  65. );
  66. }
  67. EditorStyle mobile() {
  68. final theme = Theme.of(context);
  69. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  70. final fontFamily = context.read<DocumentAppearanceCubit>().state.fontFamily;
  71. return EditorStyle.desktop(
  72. padding: padding,
  73. backgroundColor: theme.colorScheme.surface,
  74. cursorColor: theme.colorScheme.primary,
  75. textStyleConfiguration: TextStyleConfiguration(
  76. text: baseTextStyle(fontFamily).copyWith(
  77. fontSize: fontSize,
  78. color: theme.colorScheme.onBackground,
  79. height: 1.5,
  80. ),
  81. bold: baseTextStyle(fontFamily).copyWith(
  82. fontWeight: FontWeight.w600,
  83. ),
  84. italic: baseTextStyle(fontFamily).copyWith(fontStyle: FontStyle.italic),
  85. underline: baseTextStyle(fontFamily)
  86. .copyWith(decoration: TextDecoration.underline),
  87. strikethrough: baseTextStyle(fontFamily)
  88. .copyWith(decoration: TextDecoration.lineThrough),
  89. href: baseTextStyle(fontFamily).copyWith(
  90. color: theme.colorScheme.primary,
  91. decoration: TextDecoration.underline,
  92. ),
  93. code: GoogleFonts.robotoMono(
  94. textStyle: baseTextStyle(fontFamily).copyWith(
  95. fontSize: fontSize,
  96. fontWeight: FontWeight.normal,
  97. color: Colors.red,
  98. backgroundColor: theme.colorScheme.inverseSurface,
  99. ),
  100. ),
  101. ),
  102. );
  103. }
  104. TextStyle headingStyleBuilder(int level) {
  105. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  106. final fontSizes = [
  107. fontSize + 16,
  108. fontSize + 12,
  109. fontSize + 8,
  110. fontSize + 4,
  111. fontSize + 2,
  112. fontSize
  113. ];
  114. return TextStyle(
  115. fontSize: fontSizes.elementAtOrNull(level - 1) ?? fontSize,
  116. fontWeight: FontWeight.bold,
  117. );
  118. }
  119. TextStyle codeBlockStyleBuilder() {
  120. final theme = Theme.of(context);
  121. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  122. final fontFamily = context.read<DocumentAppearanceCubit>().state.fontFamily;
  123. return baseTextStyle(fontFamily).copyWith(
  124. fontSize: fontSize,
  125. height: 1.5,
  126. color: theme.colorScheme.onBackground,
  127. );
  128. }
  129. TextStyle outlineBlockPlaceholderStyleBuilder() {
  130. final theme = Theme.of(context);
  131. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  132. return TextStyle(
  133. fontFamily: 'poppins',
  134. fontSize: fontSize,
  135. height: 1.5,
  136. color: theme.colorScheme.onBackground.withOpacity(0.6),
  137. );
  138. }
  139. SelectionMenuStyle selectionMenuStyleBuilder() {
  140. final theme = Theme.of(context);
  141. return SelectionMenuStyle(
  142. selectionMenuBackgroundColor: theme.cardColor,
  143. selectionMenuItemTextColor: theme.colorScheme.onBackground,
  144. selectionMenuItemIconColor: theme.colorScheme.onBackground,
  145. selectionMenuItemSelectedIconColor: theme.colorScheme.onSurface,
  146. selectionMenuItemSelectedTextColor: theme.colorScheme.onSurface,
  147. selectionMenuItemSelectedColor: theme.hoverColor,
  148. );
  149. }
  150. FloatingToolbarStyle floatingToolbarStyleBuilder() {
  151. final theme = Theme.of(context);
  152. return FloatingToolbarStyle(
  153. backgroundColor: theme.colorScheme.onTertiary,
  154. );
  155. }
  156. TextStyle baseTextStyle(
  157. String fontFamily, {
  158. FontWeight? fontWeight,
  159. }) {
  160. try {
  161. return GoogleFonts.getFont(
  162. fontFamily,
  163. fontWeight: fontWeight,
  164. );
  165. } on Exception {
  166. return GoogleFonts.getFont('Poppins');
  167. }
  168. }
  169. InlineSpan customizeAttributeDecorator(
  170. BuildContext context,
  171. Node node,
  172. int index,
  173. TextInsert text,
  174. TextSpan textSpan,
  175. ) {
  176. final attributes = text.attributes;
  177. if (attributes == null) {
  178. return textSpan;
  179. }
  180. // customize the inline mention block, like inline page
  181. final mention = attributes[MentionBlockKeys.mention] as Map?;
  182. if (mention != null) {
  183. final type = mention[MentionBlockKeys.type];
  184. if (type == MentionType.page.name) {
  185. return WidgetSpan(
  186. alignment: PlaceholderAlignment.middle,
  187. child: MentionBlock(
  188. key: ValueKey(mention[MentionBlockKeys.pageId]),
  189. mention: mention,
  190. ),
  191. );
  192. }
  193. }
  194. // customize the inline math equation block
  195. final formula = attributes[InlineMathEquationKeys.formula] as String?;
  196. if (formula != null) {
  197. return WidgetSpan(
  198. alignment: PlaceholderAlignment.middle,
  199. child: InlineMathEquation(
  200. node: node,
  201. index: index,
  202. formula: formula,
  203. textStyle: style().textStyleConfiguration.text,
  204. ),
  205. );
  206. }
  207. return textSpan;
  208. }
  209. }