editor_style.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart';
  3. import 'package:collection/collection.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. class EditorStyleCustomizer {
  8. EditorStyleCustomizer({
  9. required this.context,
  10. });
  11. static double get horizontalPadding =>
  12. PlatformExtension.isDesktop ? 100.0 : 10.0;
  13. final BuildContext context;
  14. EditorStyle style() {
  15. if (PlatformExtension.isDesktopOrWeb) {
  16. return desktop();
  17. } else if (PlatformExtension.isMobile) {
  18. return mobile();
  19. }
  20. throw UnimplementedError();
  21. }
  22. EditorStyle desktop() {
  23. final theme = Theme.of(context);
  24. final fontSize = context.read<DocumentAppearanceCubit>().state.fontSize;
  25. return EditorStyle.desktop(
  26. padding: EdgeInsets.only(
  27. left: horizontalPadding / 2.0,
  28. right: horizontalPadding,
  29. ),
  30. backgroundColor: theme.colorScheme.surface,
  31. cursorColor: theme.colorScheme.primary,
  32. textStyleConfiguration: TextStyleConfiguration(
  33. text: TextStyle(
  34. fontFamily: 'poppins',
  35. fontSize: fontSize,
  36. color: theme.colorScheme.onBackground,
  37. height: 1.5,
  38. ),
  39. bold: const TextStyle(
  40. fontFamily: 'poppins-Bold',
  41. fontWeight: FontWeight.w600,
  42. ),
  43. italic: const TextStyle(fontStyle: FontStyle.italic),
  44. underline: const TextStyle(decoration: TextDecoration.underline),
  45. strikethrough: const TextStyle(decoration: TextDecoration.lineThrough),
  46. href: TextStyle(
  47. color: theme.colorScheme.primary,
  48. decoration: TextDecoration.underline,
  49. ),
  50. code: GoogleFonts.robotoMono(
  51. textStyle: TextStyle(
  52. fontSize: fontSize,
  53. fontWeight: FontWeight.normal,
  54. color: Colors.red,
  55. backgroundColor: theme.colorScheme.inverseSurface,
  56. ),
  57. ),
  58. ),
  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: EdgeInsets.symmetric(horizontal: horizontalPadding),
  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. }