editor_styles.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:google_fonts/google_fonts.dart';
  5. import 'package:provider/provider.dart';
  6. EditorStyle customEditorTheme(BuildContext context) {
  7. final documentStyle = context.watch<DocumentAppearanceCubit>().state;
  8. final theme = Theme.of(context);
  9. var editorStyle = EditorStyle(
  10. // Editor styles
  11. padding: const EdgeInsets.symmetric(horizontal: 100),
  12. backgroundColor: theme.colorScheme.surface,
  13. cursorColor: theme.colorScheme.primary,
  14. // Text styles
  15. textPadding: const EdgeInsets.symmetric(vertical: 8.0),
  16. textStyle: TextStyle(
  17. fontFamily: 'poppins',
  18. fontSize: documentStyle.fontSize,
  19. color: theme.colorScheme.onBackground,
  20. ),
  21. selectionColor: theme.colorScheme.tertiary.withOpacity(0.2),
  22. // Selection menu
  23. selectionMenuBackgroundColor: theme.cardColor,
  24. selectionMenuItemTextColor: theme.iconTheme.color,
  25. selectionMenuItemIconColor: theme.colorScheme.onBackground,
  26. selectionMenuItemSelectedIconColor: theme.colorScheme.onSurface,
  27. selectionMenuItemSelectedTextColor: theme.colorScheme.onSurface,
  28. selectionMenuItemSelectedColor: theme.hoverColor,
  29. // Toolbar and its item's style
  30. toolbarColor: theme.colorScheme.onTertiary,
  31. toolbarElevation: 0,
  32. lineHeight: 1.5,
  33. placeholderTextStyle:
  34. TextStyle(fontSize: documentStyle.fontSize, color: theme.hintColor),
  35. bold: const TextStyle(
  36. fontFamily: 'poppins-Bold',
  37. fontWeight: FontWeight.w600,
  38. ),
  39. italic: const TextStyle(fontStyle: FontStyle.italic),
  40. underline: const TextStyle(decoration: TextDecoration.underline),
  41. strikethrough: const TextStyle(decoration: TextDecoration.lineThrough),
  42. href: TextStyle(
  43. color: theme.colorScheme.primary,
  44. decoration: TextDecoration.underline,
  45. ),
  46. highlightColorHex: '0x6000BCF0',
  47. code: GoogleFonts.robotoMono(
  48. textStyle: TextStyle(
  49. fontSize: documentStyle.fontSize,
  50. fontWeight: FontWeight.normal,
  51. color: Colors.red,
  52. backgroundColor: theme.colorScheme.inverseSurface,
  53. ),
  54. ),
  55. popupMenuFGColor: theme.iconTheme.color,
  56. popupMenuHoverColor: theme.colorScheme.tertiaryContainer,
  57. );
  58. return editorStyle;
  59. }
  60. Iterable<ThemeExtension<dynamic>> customPluginTheme(BuildContext context) {
  61. final documentStyle = context.watch<DocumentAppearanceCubit>().state;
  62. final baseFontSize = documentStyle.fontSize;
  63. const basePadding = 12.0;
  64. var headingPluginStyle = Theme.of(context).brightness == Brightness.dark
  65. ? HeadingPluginStyle.dark
  66. : HeadingPluginStyle.light;
  67. headingPluginStyle = headingPluginStyle.copyWith(
  68. textStyle: (EditorState editorState, Node node) {
  69. final headingToFontSize = {
  70. 'h1': baseFontSize + 12,
  71. 'h2': baseFontSize + 8,
  72. 'h3': baseFontSize + 4,
  73. 'h4': baseFontSize,
  74. 'h5': baseFontSize,
  75. 'h6': baseFontSize,
  76. };
  77. final fontSize =
  78. headingToFontSize[node.attributes.heading] ?? baseFontSize;
  79. return TextStyle(fontSize: fontSize, fontWeight: FontWeight.w600);
  80. },
  81. padding: (EditorState editorState, Node node) {
  82. final headingToPadding = {
  83. 'h1': basePadding + 6,
  84. 'h2': basePadding + 4,
  85. 'h3': basePadding + 2,
  86. 'h4': basePadding,
  87. 'h5': basePadding,
  88. 'h6': basePadding,
  89. };
  90. final padding = headingToPadding[node.attributes.heading] ?? basePadding;
  91. return EdgeInsets.only(bottom: padding);
  92. },
  93. );
  94. var numberListPluginStyle = Theme.of(context).brightness == Brightness.dark
  95. ? NumberListPluginStyle.dark
  96. : NumberListPluginStyle.light;
  97. numberListPluginStyle = numberListPluginStyle.copyWith(
  98. icon: (_, textNode) {
  99. const iconPadding = EdgeInsets.only(left: 5.0, right: 5.0);
  100. return Container(
  101. padding: iconPadding,
  102. child: Text(
  103. '${textNode.attributes.number.toString()}.',
  104. style: customEditorTheme(context).textStyle,
  105. ),
  106. );
  107. },
  108. );
  109. final pluginTheme = Theme.of(context).brightness == Brightness.dark
  110. ? darkPluginStyleExtension
  111. : lightPluginStyleExtension;
  112. return pluginTheme.toList()
  113. ..removeWhere(
  114. (element) =>
  115. element is HeadingPluginStyle || element is NumberListPluginStyle,
  116. )
  117. ..add(headingPluginStyle)
  118. ..add(numberListPluginStyle);
  119. }