-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from alsterverse/fix/storyblok-bug-handling
Handle storyblok rich text copy-paste bug
- Loading branch information
Showing
9 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
/// Used to convert a color hex string to a Flutter Color | ||
final class StoryblokColor extends Color { | ||
StoryblokColor(super.value); | ||
factory StoryblokColor.fromString(String colorString) { | ||
final cssColorMatch = cssColorRegex.firstMatch(colorString); | ||
if (cssColorMatch != null) return StoryblokColor(_getColorFromCSS(cssColorMatch)); | ||
return StoryblokColor(_getColorFromHex(colorString)); | ||
} | ||
|
||
static int _getColorFromHex(String hexColor) { | ||
hexColor = hexColor.replaceAll("#", ""); | ||
if (hexColor.length == 6) hexColor = "FF$hexColor"; | ||
return int.parse(hexColor, radix: 16); | ||
} | ||
|
||
static final cssColorRegex = RegExp(r"rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)", caseSensitive: false); | ||
static int _getColorFromCSS(RegExpMatch match) { | ||
final r = int.parse(match.group(1)!); | ||
final g = int.parse(match.group(2)!); | ||
final b = int.parse(match.group(3)!); | ||
return 0xFF << 8 * 3 | // | ||
r << 8 * 2 | // | ||
g << 8 * 1 | // | ||
b << 8 * 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
library; | ||
|
||
export 'src/widgets/hex_color.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_storyblok/src/fields/rich_text.dart'; | ||
import 'package:flutter_storyblok/src/widgets/storyblok_color.dart'; | ||
|
||
export 'src/widgets/storyblok_color.dart'; | ||
export 'src/widgets/rich_text.dart'; | ||
|
||
extension RichTextLeafMarkTextStyleColor on RichTextLeafMarkTextStyle { | ||
Color get color => StoryblokColor.fromString(colorString); | ||
} | ||
|
||
extension RichTextLeafMarkHighlightColor on RichTextLeafMarkHighlight { | ||
Color get color => StoryblokColor.fromString(colorString); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ dependencies: | |
|
||
dev_dependencies: | ||
lints: ^4.0.0 | ||
test: ^1.25.8 | ||
build_runner: ^2.4.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'package:flutter_storyblok/fields.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Test parsing rich-text', () { | ||
test('Test parse rich-text basic text', () { | ||
final text = RichTextLeafText.fromJson({ | ||
"type": "text", | ||
"text": "Hello world", | ||
}); | ||
expect(text.text, "Hello world"); | ||
expect(text.anchor, null); | ||
expect(text.backgroundColor, null); | ||
expect(text.foregroundColor, null); | ||
expect(text.isBold, false); | ||
expect(text.isCode, false); | ||
expect(text.isItalic, false); | ||
expect(text.isStriked, false); | ||
expect(text.isSubscript, false); | ||
expect(text.isSuperscript, false); | ||
expect(text.isUnderlined, false); | ||
expect(text.link, null); | ||
}); | ||
|
||
test('Test parse rich-text text with foreground color', () { | ||
final text = RichTextLeafText.fromJson({ | ||
"text": "Hello world", | ||
"marks": [ | ||
{ | ||
"type": "textStyle", | ||
"attrs": {"color": "#FF0000"} | ||
} | ||
] | ||
}); | ||
expect(text.foregroundColor!.colorString, "#FF0000"); | ||
}); | ||
|
||
test('Test parse rich-text text with foreground color css', () { | ||
final text = RichTextLeafText.fromJson({ | ||
"text": "Hello world", | ||
"marks": [ | ||
{ | ||
"type": "textStyle", | ||
"attrs": {"color": "rgb(255, 0, 0)"} | ||
} | ||
] | ||
}); | ||
expect(text.foregroundColor!.colorString, "rgb(255, 0, 0)"); | ||
}); | ||
|
||
test('Test parse rich-text image', () { | ||
final image = RichTextLeafImage.fromJson({ | ||
"type": "image", | ||
"attrs": { | ||
"id": 123, | ||
"alt": "hello", | ||
"src": "https://placehold.it/100x100", | ||
"title": "hello", | ||
"source": "hello", | ||
"copyright": "hello", | ||
"meta_data": {"alt": "hello"} | ||
} | ||
}); | ||
expect(image.imageUrl, Uri.parse("https://placehold.it/100x100")); | ||
expect(image.alt, "hello"); | ||
expect(image.metadata["alt"], "hello"); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:flutter_storyblok/widgets.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Test parsing colors from storyblok', () { | ||
test('Test parse color hexadecimal', () { | ||
final color = StoryblokColor.fromString("#FF8800"); | ||
expect(color.alpha, 0xFF); | ||
expect(color.red, 0xFF); | ||
expect(color.green, 0x88); | ||
expect(color.blue, 0x00); | ||
}); | ||
|
||
test('Test parse color css', () { | ||
final color = StoryblokColor.fromString("rgb(255, 136, 0)"); | ||
expect(color.alpha, 0xFF); | ||
expect(color.red, 255); | ||
expect(color.green, 136); | ||
expect(color.blue, 0); | ||
}); | ||
}); | ||
} |