editor_style.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. return EditorStyle.desktop(
  28. padding: padding,
  29. backgroundColor: theme.colorScheme.surface,
  30. cursorColor: theme.colorScheme.primary,
  31. textStyleConfiguration: TextStyleConfiguration(
  32. text: TextStyle(
  33. fontFamily: 'Poppins',
  34. fontSize: fontSize,
  35. color: theme.colorScheme.onBackground,
  36. height: 1.5,
  37. ),
  38. bold: const TextStyle(
  39. fontFamily: 'Poppins-Bold',
  40. fontWeight: FontWeight.w600,
  41. ),
  42. italic: const TextStyle(fontStyle: FontStyle.italic),
  43. underline: const TextStyle(decoration: TextDecoration.underline),
  44. strikethrough: const TextStyle(decoration: TextDecoration.lineThrough),
  45. href: TextStyle(
  46. color: theme.colorScheme.primary,
  47. decoration: TextDecoration.underline,
  48. ),
  49. code: GoogleFonts.robotoMono(
  50. textStyle: TextStyle(
  51. fontSize: fontSize,
  52. fontWeight: FontWeight.normal,
  53. color: Colors.red,
  54. backgroundColor: theme.colorScheme.inverseSurface,
  55. ),
  56. ),
  57. ),
  58. textSpanDecorator: customizeAttributeDecorator,
  59. );
  60. }
  61. EditorStyle mobile() {
  62. final theme = Theme.of(context);
  63. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  64. return EditorStyle.desktop(
  65. padding: padding,
  66. backgroundColor: theme.colorScheme.surface,
  67. cursorColor: theme.colorScheme.primary,
  68. textStyleConfiguration: TextStyleConfiguration(
  69. text: TextStyle(
  70. fontFamily: 'poppins',
  71. fontSize: fontSize,
  72. color: theme.colorScheme.onBackground,
  73. height: 1.5,
  74. ),
  75. bold: const TextStyle(
  76. fontFamily: 'poppins-Bold',
  77. fontWeight: FontWeight.w600,
  78. ),
  79. italic: const TextStyle(fontStyle: FontStyle.italic),
  80. underline: const TextStyle(decoration: TextDecoration.underline),
  81. strikethrough: const TextStyle(decoration: TextDecoration.lineThrough),
  82. href: TextStyle(
  83. color: theme.colorScheme.primary,
  84. decoration: TextDecoration.underline,
  85. ),
  86. code: GoogleFonts.robotoMono(
  87. textStyle: TextStyle(
  88. fontSize: fontSize,
  89. fontWeight: FontWeight.normal,
  90. color: Colors.red,
  91. backgroundColor: theme.colorScheme.inverseSurface,
  92. ),
  93. ),
  94. ),
  95. );
  96. }
  97. TextStyle headingStyleBuilder(int level) {
  98. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  99. final fontSizes = [
  100. fontSize + 16,
  101. fontSize + 12,
  102. fontSize + 8,
  103. fontSize + 4,
  104. fontSize + 2,
  105. fontSize
  106. ];
  107. return TextStyle(
  108. fontSize: fontSizes.elementAtOrNull(level - 1) ?? fontSize,
  109. fontWeight: FontWeight.bold,
  110. );
  111. }
  112. TextStyle codeBlockStyleBuilder() {
  113. final theme = Theme.of(context);
  114. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  115. return TextStyle(
  116. fontFamily: 'poppins',
  117. fontSize: fontSize,
  118. height: 1.5,
  119. color: theme.colorScheme.onBackground,
  120. );
  121. }
  122. SelectionMenuStyle selectionMenuStyleBuilder() {
  123. final theme = Theme.of(context);
  124. return SelectionMenuStyle(
  125. selectionMenuBackgroundColor: theme.cardColor,
  126. selectionMenuItemTextColor: theme.colorScheme.onBackground,
  127. selectionMenuItemIconColor: theme.colorScheme.onBackground,
  128. selectionMenuItemSelectedIconColor: theme.colorScheme.onSurface,
  129. selectionMenuItemSelectedTextColor: theme.colorScheme.onSurface,
  130. selectionMenuItemSelectedColor: theme.hoverColor,
  131. );
  132. }
  133. FloatingToolbarStyle floatingToolbarStyleBuilder() {
  134. final theme = Theme.of(context);
  135. return FloatingToolbarStyle(
  136. backgroundColor: theme.colorScheme.onTertiary,
  137. );
  138. }
  139. InlineSpan customizeAttributeDecorator(
  140. TextInsert textInsert,
  141. TextSpan textSpan,
  142. ) {
  143. final attributes = textInsert.attributes;
  144. if (attributes == null) {
  145. return textSpan;
  146. }
  147. final mention = attributes[MentionBlockKeys.mention] as Map?;
  148. if (mention != null) {
  149. final type = mention[MentionBlockKeys.type];
  150. if (type == MentionType.page.name) {
  151. return WidgetSpan(
  152. alignment: PlaceholderAlignment.middle,
  153. child: MentionBlock(
  154. key: ValueKey(mention[MentionBlockKeys.pageId]),
  155. mention: mention,
  156. ),
  157. );
  158. }
  159. }
  160. return textSpan;
  161. }
  162. }