editor_style.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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).copyWith(
  40. fontWeight: FontWeight.w600,
  41. ),
  42. italic: baseTextStyle(fontFamily).copyWith(fontStyle: FontStyle.italic),
  43. underline: baseTextStyle(fontFamily)
  44. .copyWith(decoration: TextDecoration.underline),
  45. strikethrough:
  46. baseTextStyle(fontFamily)
  47. .copyWith(decoration: TextDecoration.lineThrough),
  48. href: baseTextStyle(fontFamily).copyWith(
  49. color: theme.colorScheme.primary,
  50. decoration: TextDecoration.underline,
  51. ),
  52. code: GoogleFonts.robotoMono(
  53. textStyle: baseTextStyle(fontFamily).copyWith(
  54. fontSize: fontSize,
  55. fontWeight: FontWeight.normal,
  56. color: Colors.red,
  57. backgroundColor: theme.colorScheme.inverseSurface,
  58. ),
  59. ),
  60. ),
  61. textSpanDecorator: customizeAttributeDecorator,
  62. );
  63. }
  64. EditorStyle mobile() {
  65. final theme = Theme.of(context);
  66. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  67. final fontFamily = context.read<DocumentAppearanceCubit>().state.fontFamily;
  68. return EditorStyle.desktop(
  69. padding: padding,
  70. backgroundColor: theme.colorScheme.surface,
  71. cursorColor: theme.colorScheme.primary,
  72. textStyleConfiguration: TextStyleConfiguration(
  73. text: baseTextStyle(fontFamily).copyWith(
  74. fontSize: fontSize,
  75. color: theme.colorScheme.onBackground,
  76. height: 1.5,
  77. ),
  78. bold: baseTextStyle(fontFamily).copyWith(
  79. fontWeight: FontWeight.w600,
  80. ),
  81. italic: baseTextStyle(fontFamily).copyWith(fontStyle: FontStyle.italic),
  82. underline: baseTextStyle(fontFamily)
  83. .copyWith(decoration: TextDecoration.underline),
  84. strikethrough:
  85. baseTextStyle(fontFamily)
  86. .copyWith(decoration: TextDecoration.lineThrough),
  87. href: baseTextStyle(fontFamily).copyWith(
  88. color: theme.colorScheme.primary,
  89. decoration: TextDecoration.underline,
  90. ),
  91. code: GoogleFonts.robotoMono(
  92. textStyle: baseTextStyle(fontFamily).copyWith(
  93. fontSize: fontSize,
  94. fontWeight: FontWeight.normal,
  95. color: Colors.red,
  96. backgroundColor: theme.colorScheme.inverseSurface,
  97. ),
  98. ),
  99. ),
  100. );
  101. }
  102. TextStyle headingStyleBuilder(int level) {
  103. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  104. final fontSizes = [
  105. fontSize + 16,
  106. fontSize + 12,
  107. fontSize + 8,
  108. fontSize + 4,
  109. fontSize + 2,
  110. fontSize
  111. ];
  112. return TextStyle(
  113. fontSize: fontSizes.elementAtOrNull(level - 1) ?? fontSize,
  114. fontWeight: FontWeight.bold,
  115. );
  116. }
  117. TextStyle codeBlockStyleBuilder() {
  118. final theme = Theme.of(context);
  119. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  120. final fontFamily = context.read<DocumentAppearanceCubit>().state.fontFamily;
  121. return baseTextStyle(fontFamily).copyWith(
  122. fontSize: fontSize,
  123. height: 1.5,
  124. color: theme.colorScheme.onBackground,
  125. );
  126. }
  127. TextStyle outlineBlockPlaceholderStyleBuilder() {
  128. final theme = Theme.of(context);
  129. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  130. return TextStyle(
  131. fontFamily: 'poppins',
  132. fontSize: fontSize,
  133. height: 1.5,
  134. color: theme.colorScheme.onBackground.withOpacity(0.6),
  135. );
  136. }
  137. SelectionMenuStyle selectionMenuStyleBuilder() {
  138. final theme = Theme.of(context);
  139. return SelectionMenuStyle(
  140. selectionMenuBackgroundColor: theme.cardColor,
  141. selectionMenuItemTextColor: theme.colorScheme.onBackground,
  142. selectionMenuItemIconColor: theme.colorScheme.onBackground,
  143. selectionMenuItemSelectedIconColor: theme.colorScheme.onSurface,
  144. selectionMenuItemSelectedTextColor: theme.colorScheme.onSurface,
  145. selectionMenuItemSelectedColor: theme.hoverColor,
  146. );
  147. }
  148. FloatingToolbarStyle floatingToolbarStyleBuilder() {
  149. final theme = Theme.of(context);
  150. return FloatingToolbarStyle(
  151. backgroundColor: theme.colorScheme.onTertiary,
  152. );
  153. }
  154. TextStyle baseTextStyle(String fontFamily) {
  155. try {
  156. return GoogleFonts.getFont(
  157. fontFamily,
  158. );
  159. } on Exception {
  160. return GoogleFonts.getFont('Poppins');
  161. }
  162. }
  163. InlineSpan customizeAttributeDecorator(
  164. Node node,
  165. int index,
  166. TextInsert text,
  167. TextSpan textSpan,
  168. ) {
  169. final attributes = text.attributes;
  170. if (attributes == null) {
  171. return textSpan;
  172. }
  173. // customize the inline mention block, like inline page
  174. final mention = attributes[MentionBlockKeys.mention] as Map?;
  175. if (mention != null) {
  176. final type = mention[MentionBlockKeys.type];
  177. if (type == MentionType.page.name) {
  178. return WidgetSpan(
  179. alignment: PlaceholderAlignment.middle,
  180. child: MentionBlock(
  181. key: ValueKey(mention[MentionBlockKeys.pageId]),
  182. mention: mention,
  183. ),
  184. );
  185. }
  186. }
  187. // customize the inline math equation block
  188. final formula = attributes[InlineMathEquationKeys.formula] as String?;
  189. if (formula != null) {
  190. return WidgetSpan(
  191. alignment: PlaceholderAlignment.middle,
  192. child: InlineMathEquation(
  193. node: node,
  194. index: index,
  195. formula: formula,
  196. textStyle: style().textStyleConfiguration.text,
  197. ),
  198. );
  199. }
  200. return textSpan;
  201. }
  202. }