Skip to content

Commit

Permalink
fix(text-editor): resolve issue where text styles (bold/italic/underl…
Browse files Browse the repository at this point in the history
…ine) are not saved in history

This resolves issue [#118](#118).
  • Loading branch information
hm21 committed Jun 19, 2024
1 parent f77e920 commit 1fffb55
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Version 4.0.5
- **fix(text-editor)**: Resolve misapplication of secondary color. This resolve issue [#105](https://github.com/hm21/pro_image_editor/discussions/105).
- **fix(text-editor)**: Resolve issue where text styles (bold/italic/underline) are not saved in history. This resolves issue [#118](https://github.com/hm21/pro_image_editor/discussions/118).


## Version 4.0.4
- **feat(text-editor)**: Added the ability to programmatically set the secondary color next to the primary color.
Expand Down
60 changes: 58 additions & 2 deletions lib/models/layer/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,55 @@ class TextLayerData extends Layer {
'fontScale': fontScale,
'type': 'text',
if (customSecondaryColor) 'customSecondaryColor': customSecondaryColor,
'fontFamily': textStyle?.fontFamily
if (textStyle?.fontFamily != null) 'fontFamily': textStyle?.fontFamily,
if (textStyle?.fontStyle != null) 'fontStyle': textStyle?.fontStyle!.name,
if (textStyle?.fontWeight != null)
'fontWeight': textStyle?.fontWeight!.value,
if (textStyle?.letterSpacing != null)
'letterSpacing': textStyle?.letterSpacing,
if (textStyle?.height != null) 'height': textStyle?.height,
if (textStyle?.wordSpacing != null) 'wordSpacing': textStyle?.wordSpacing,
if (textStyle?.decoration != null)
'decoration': textStyle?.decoration.toString(),
};
}

factory TextLayerData.fromMap(Layer layer, Map map) {
TextDecoration getDecoration(String decoration) {
if (decoration.contains('combine')) {
List<TextDecoration> decorations = [];

if (decoration.contains('lineThrough')) {
decorations.add(TextDecoration.lineThrough);
}
if (decoration.contains('overline')) {
decorations.add(TextDecoration.overline);
}
if (decoration.contains('underline')) {
decorations.add(TextDecoration.underline);
}

return TextDecoration.combine(decorations);
} else {
if (decoration.contains('lineThrough')) {
return TextDecoration.lineThrough;
} else if (decoration.contains('overline')) {
return TextDecoration.overline;
} else if (decoration.contains('underline')) {
return TextDecoration.underline;
}
}

return TextDecoration.none;
}

String? fontFamily = map['fontFamily'];
double? wordSpacing = map['wordSpacing'];
double? height = map['height'];
double? letterSpacing = map['letterSpacing'];
int? fontWeight = map['fontWeight'];
String? fontStyle = map['fontStyle'];
String? decoration = map['decoration'];
return TextLayerData(
flipX: layer.flipX,
flipY: layer.flipY,
Expand All @@ -179,7 +223,19 @@ class TextLayerData extends Layer {
fontScale: map['fontScale'] ?? 1.0,
textStyle: map['fontFamily'] != null
? TextStyle(
fontFamily: map['fontFamily'],
fontFamily: fontFamily,
height: height,
wordSpacing: wordSpacing,
letterSpacing: letterSpacing,
decoration: decoration != null ? getDecoration(decoration) : null,
fontStyle: fontStyle != null
? FontStyle.values
.firstWhere((element) => element.name == fontStyle)
: null,
fontWeight: fontWeight != null
? FontWeight.values
.firstWhere((element) => element.value == fontWeight)
: null,
)
: null,
colorMode: LayerBackgroundMode.values
Expand Down
25 changes: 11 additions & 14 deletions lib/modules/text_editor/text_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,16 @@ class TextEditorState extends State<TextEditor>
if (_textCtrl.text.trim().isNotEmpty) {
Navigator.of(context).pop(
TextLayerData(
text: _textCtrl.text.trim(),
background: _getBackgroundColor,
color: _getTextColor,
align: align,
fontScale: _fontScale,
colorMode: backgroundColorMode,
colorPickerPosition: colorPosition,
textStyle: selectedTextStyle,
customSecondaryColor: _secondaryColor != null
// fontFamily: 'Roboto',
),
text: _textCtrl.text.trim(),
background: _getBackgroundColor,
color: _getTextColor,
align: align,
fontScale: _fontScale,
colorMode: backgroundColorMode,
colorPickerPosition: colorPosition,
textStyle: selectedTextStyle,
customSecondaryColor: _secondaryColor != null,
),
);
} else {
Navigator.of(context).pop();
Expand Down Expand Up @@ -600,7 +599,6 @@ class TextEditorState extends State<TextEditor>
style: selectedTextStyle.copyWith(
color: _getTextColor,
fontSize: _getTextFontSize,
fontWeight: FontWeight.w400,
height: 1.35,
letterSpacing: 0,
),
Expand Down Expand Up @@ -633,15 +631,14 @@ class TextEditorState extends State<TextEditor>
hintStyle: selectedTextStyle.copyWith(
color: imageEditorTheme.textEditor.inputHintColor,
fontSize: _getTextFontSize,
fontWeight: FontWeight.w400,
height: 1.35,
)),
style: selectedTextStyle.copyWith(
color: Colors.transparent,
fontSize: _getTextFontSize,
fontWeight: FontWeight.w400,
height: 1.35,
letterSpacing: 0,
decoration: TextDecoration.none,
),
autofocus: true,
),
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/layer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ class _LayerWidgetState extends State<LayerWidget>
var layer = _layer as TextLayerData;
var style = TextStyle(
fontSize: fontSize * layer.fontScale,
fontWeight: FontWeight.w400,
color: layer.color,
overflow: TextOverflow.ellipsis,
);
Expand Down

0 comments on commit 1fffb55

Please sign in to comment.