Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support scaling the svg #722

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensions:
10 changes: 4 additions & 6 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:example/pages/customize_theme_for_editor.dart';
import 'package:example/pages/editor.dart';
import 'package:example/pages/editor_list.dart';
import 'package:example/pages/focus_example_for_editor.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_html/html.dart' as html;

import 'package:appflowy_editor/appflowy_editor.dart';

enum ExportFileType {
documentJson,
markdown,
Expand Down
1 change: 1 addition & 0 deletions example/lib/pages/mobile_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class _MobileEditorState extends State<MobileEditor> {
// showcase 1: customize the editor style.
EditorStyle _buildMobileEditorStyle() {
return EditorStyle.mobile(
textScaleFactor: 1.0,
cursorColor: const Color.fromARGB(255, 134, 46, 247),
dragHandleColor: const Color.fromARGB(255, 134, 46, 247),
selectionColor: const Color.fromARGB(50, 134, 46, 247),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/block_component/base_component/block_icon_builder.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BulletedListBlockKeys {
const BulletedListBlockKeys._();
Expand Down Expand Up @@ -218,14 +219,17 @@ class _BulletedListIcon extends StatelessWidget {

@override
Widget build(BuildContext context) {
final textScaleFactor =
context.read<EditorState>().editorStyle.textScaleFactor;
return Container(
constraints: const BoxConstraints(minWidth: 26, minHeight: 22),
constraints:
const BoxConstraints(minWidth: 26, minHeight: 22) * textScaleFactor,
padding: const EdgeInsets.only(right: 4.0),
child: Center(
child: Text(
icon,
style: textStyle,
textScaler: const TextScaler.linear(0.5),
textScaler: TextScaler.linear(0.5 * textScaleFactor),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,14 @@ class _NumberedListIcon extends StatelessWidget {
Widget build(BuildContext context) {
final editorState = context.read<EditorState>();
final text = editorState.editorStyle.textStyleConfiguration.text;
final textScaleFactor = editorState.editorStyle.textScaleFactor;
return Container(
constraints: const BoxConstraints(minWidth: 26, minHeight: 22),
constraints:
const BoxConstraints(minWidth: 26, minHeight: 22) * textScaleFactor,
padding: const EdgeInsets.only(right: 4.0),
child: Center(
child: Text.rich(
textScaler: TextScaler.linear(textScaleFactor),
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
applyHeightToLastDescent: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,15 @@ class _QuoteIcon extends StatelessWidget {

@override
Widget build(BuildContext context) {
final textScaleFactor =
context.read<EditorState>().editorStyle.textScaleFactor;
return Container(
alignment: Alignment.center,
constraints: const BoxConstraints(minWidth: 26, minHeight: 22),
constraints:
const BoxConstraints(minWidth: 26, minHeight: 22) * textScaleFactor,
padding: const EdgeInsets.only(right: 4.0),
child: Container(
width: 4,
width: 4 * textScaleFactor,
color: '#00BCF0'.tryToColor(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
? widget.placeholderTextSpanDecorator!(textSpan)
: textSpan,
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
);
}

Expand All @@ -301,6 +303,8 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
? widget.textSpanDecorator!(textSpan)
: textSpan,
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/block_component/base_component/block_icon_builder.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class TodoListBlockKeys {
const TodoListBlockKeys._();
Expand Down Expand Up @@ -269,13 +270,16 @@ class _TodoListIcon extends StatelessWidget {

@override
Widget build(BuildContext context) {
final textScaleFactor =
context.read<EditorState>().editorStyle.textScaleFactor;
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: Container(
constraints: const BoxConstraints(minWidth: 26, minHeight: 22),
constraints: const BoxConstraints(minWidth: 26, minHeight: 22) *
textScaleFactor,
padding: const EdgeInsets.only(right: 4.0),
child: EditorSvg(
width: 22,
Expand Down
7 changes: 7 additions & 0 deletions lib/src/editor/editor_component/style/editor_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EditorStyle {
this.cursorWidth = 2.0,
this.defaultTextDirection,
this.enableHapticFeedbackOnAndroid = true,
this.textScaleFactor = 1.0,
});

// The padding of the editor.
Expand Down Expand Up @@ -70,6 +71,8 @@ class EditorStyle {
// enable haptic feedback when updating selection by dragging the drag handler
final bool enableHapticFeedbackOnAndroid;

final double textScaleFactor;

const EditorStyle.desktop({
EdgeInsets? padding,
Color? cursorColor,
Expand All @@ -78,6 +81,7 @@ class EditorStyle {
TextSpanDecoratorForAttribute? textSpanDecorator,
this.defaultTextDirection,
this.cursorWidth = 2.0,
this.textScaleFactor = 1.0,
}) : padding = padding ?? const EdgeInsets.symmetric(horizontal: 100),
cursorColor = cursorColor ?? const Color(0xFF00BCF0),
selectionColor =
Expand Down Expand Up @@ -107,6 +111,7 @@ class EditorStyle {
this.mobileDragHandleWidth = 2.0,
this.cursorWidth = 2.0,
this.enableHapticFeedbackOnAndroid = true,
this.textScaleFactor = 1.0,
}) : padding = padding ?? const EdgeInsets.symmetric(horizontal: 20),
cursorColor = cursorColor ?? const Color(0xFF00BCF0),
dragHandleColor = dragHandleColor ?? const Color(0xFF00BCF0),
Expand All @@ -132,6 +137,7 @@ class EditorStyle {
double? mobileDragHandleWidth,
bool? enableHapticFeedbackOnAndroid,
double? cursorWidth,
double? textScaleFactor,
}) {
return EditorStyle(
padding: padding ?? this.padding,
Expand All @@ -150,6 +156,7 @@ class EditorStyle {
enableHapticFeedbackOnAndroid:
enableHapticFeedbackOnAndroid ?? this.enableHapticFeedbackOnAndroid,
cursorWidth: cursorWidth ?? this.cursorWidth,
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
);
}
}
20 changes: 16 additions & 4 deletions lib/src/infra/flowy_svg.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

class EditorSvg extends StatelessWidget {
const EditorSvg({
Expand All @@ -24,13 +26,23 @@ class EditorSvg extends StatelessWidget {

@override
Widget build(BuildContext context) {
final scaleFactor =
context.read<EditorState?>()?.editorStyle.textScaleFactor ?? 1.0;
final height = (this.height ?? _defaultHeight) * scaleFactor;
final width = (this.width ?? _defaultWidth) * scaleFactor;
return Padding(
padding: padding ?? const EdgeInsets.all(0),
child: _buildSvg(),
child: _buildSvg(
height,
width,
),
);
}

Widget _buildSvg() {
Widget _buildSvg(
double height,
double width,
) {
if (name != null) {
return SvgPicture.asset(
'assets/images/$name.svg',
Expand All @@ -46,8 +58,8 @@ class EditorSvg extends StatelessWidget {
'<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"><text x="30" y="150" fill="black" font-size="160">$number.</text></svg>';
return SvgPicture.string(
numberText,
width: width ?? _defaultWidth,
height: height ?? _defaultHeight,
width: width,
height: height,
);
}
return Container();
Expand Down
Loading