editor_style.dart 6.4 KB

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