editor_styles.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:app_flowy/plugins/document/document.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. EditorStyle customEditorTheme(BuildContext context) {
  6. final documentStyle = context.watch<DocumentStyle>();
  7. var editorStyle = Theme.of(context).brightness == Brightness.dark
  8. ? EditorStyle.dark
  9. : EditorStyle.light;
  10. editorStyle = editorStyle.copyWith(
  11. padding: const EdgeInsets.all(0),
  12. textStyle: editorStyle.textStyle?.copyWith(
  13. fontFamily: 'poppins',
  14. fontSize: documentStyle.fontSize,
  15. ),
  16. placeholderTextStyle: editorStyle.placeholderTextStyle?.copyWith(
  17. fontFamily: 'poppins',
  18. fontSize: documentStyle.fontSize,
  19. ),
  20. bold: editorStyle.bold?.copyWith(
  21. fontWeight: FontWeight.w500,
  22. ),
  23. backgroundColor: Theme.of(context).colorScheme.surface,
  24. );
  25. return editorStyle;
  26. }
  27. Iterable<ThemeExtension<dynamic>> customPluginTheme(BuildContext context) {
  28. final documentStyle = context.watch<DocumentStyle>();
  29. final baseFontSize = documentStyle.fontSize;
  30. const basePadding = 12.0;
  31. var headingPluginStyle = Theme.of(context).brightness == Brightness.dark
  32. ? HeadingPluginStyle.dark
  33. : HeadingPluginStyle.light;
  34. headingPluginStyle = headingPluginStyle.copyWith(
  35. textStyle: (EditorState editorState, Node node) {
  36. final headingToFontSize = {
  37. 'h1': baseFontSize + 12,
  38. 'h2': baseFontSize + 8,
  39. 'h3': baseFontSize + 4,
  40. 'h4': baseFontSize,
  41. 'h5': baseFontSize,
  42. 'h6': baseFontSize,
  43. };
  44. final fontSize =
  45. headingToFontSize[node.attributes.heading] ?? baseFontSize;
  46. return TextStyle(fontSize: fontSize, fontWeight: FontWeight.w600);
  47. },
  48. padding: (EditorState editorState, Node node) {
  49. final headingToPadding = {
  50. 'h1': basePadding + 6,
  51. 'h2': basePadding + 4,
  52. 'h3': basePadding + 2,
  53. 'h4': basePadding,
  54. 'h5': basePadding,
  55. 'h6': basePadding,
  56. };
  57. final padding = headingToPadding[node.attributes.heading] ?? basePadding;
  58. return EdgeInsets.only(bottom: padding);
  59. },
  60. );
  61. var numberListPluginStyle = Theme.of(context).brightness == Brightness.dark
  62. ? NumberListPluginStyle.dark
  63. : NumberListPluginStyle.light;
  64. numberListPluginStyle = numberListPluginStyle.copyWith(
  65. icon: (_, textNode) {
  66. const iconPadding = EdgeInsets.only(left: 5.0, right: 5.0);
  67. return Container(
  68. padding: iconPadding,
  69. child: Text(
  70. '${textNode.attributes.number.toString()}.',
  71. style: customEditorTheme(context).textStyle,
  72. ),
  73. );
  74. },
  75. );
  76. final pluginTheme = Theme.of(context).brightness == Brightness.dark
  77. ? darkPlguinStyleExtension
  78. : lightPlguinStyleExtension;
  79. return pluginTheme.toList()
  80. ..removeWhere((element) =>
  81. element is HeadingPluginStyle || element is NumberListPluginStyle)
  82. ..add(headingPluginStyle)
  83. ..add(numberListPluginStyle);
  84. }