Skip to content

Commit

Permalink
Replace pixel property in image element with source url
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Mar 16, 2023
1 parent 3b20e52 commit 2ea5502
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 48 deletions.
6 changes: 6 additions & 0 deletions api/lib/src/models/converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class DocumentJsonConverter extends JsonConverter<AppDocument, Map> {
},
};
}
if (e['type'] == 'image') {
return {
...e as Map,
'source': UriData.fromBytes(e['pixels'], mimeType: 'image/png'),
};
}
return e;
}).toList();
}
Expand Down
3 changes: 1 addition & 2 deletions api/lib/src/models/element.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:math';
import 'dart:typed_data';

import 'converter.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand Down Expand Up @@ -75,7 +74,7 @@ class PadElement with _$PadElement {
Point<double> position,
@Default(ScaledElementConstraints(scaleX: 1, scaleY: 1))
ElementConstraints? constraints,
@Uint8ListJsonConverter() required Uint8List pixels,
required String source,
required double width,
required double height,
}) = ImageElement;
Expand Down
66 changes: 32 additions & 34 deletions api/lib/src/models/element.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ mixin _$PadElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand Down Expand Up @@ -915,7 +915,7 @@ mixin _$PadElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -951,7 +951,7 @@ mixin _$PadElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -1155,7 +1155,7 @@ class _$PenElement implements PenElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand Down Expand Up @@ -1194,7 +1194,7 @@ class _$PenElement implements PenElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -1233,7 +1233,7 @@ class _$PenElement implements PenElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -1476,7 +1476,7 @@ class _$TextElement implements TextElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand Down Expand Up @@ -1515,7 +1515,7 @@ class _$TextElement implements TextElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -1554,7 +1554,7 @@ class _$TextElement implements TextElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -1664,7 +1664,7 @@ abstract class _$$ImageElementCopyWith<$Res>
{String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height});

Expand All @@ -1685,7 +1685,7 @@ class __$$ImageElementCopyWithImpl<$Res>
Object? layer = null,
Object? position = null,
Object? constraints = freezed,
Object? pixels = null,
Object? source = null,
Object? width = null,
Object? height = null,
}) {
Expand All @@ -1702,10 +1702,10 @@ class __$$ImageElementCopyWithImpl<$Res>
? _value.constraints
: constraints // ignore: cast_nullable_to_non_nullable
as ElementConstraints?,
pixels: null == pixels
? _value.pixels
: pixels // ignore: cast_nullable_to_non_nullable
as Uint8List,
source: null == source
? _value.source
: source // ignore: cast_nullable_to_non_nullable
as String,
width: null == width
? _value.width
: width // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -1737,7 +1737,7 @@ class _$ImageElement implements ImageElement {
{this.layer = '',
@DoublePointJsonConverter() this.position = const Point(0.0, 0.0),
this.constraints = const ScaledElementConstraints(scaleX: 1, scaleY: 1),
@Uint8ListJsonConverter() required this.pixels,
required this.source,
required this.width,
required this.height,
final String? $type})
Expand All @@ -1757,8 +1757,7 @@ class _$ImageElement implements ImageElement {
@JsonKey()
final ElementConstraints? constraints;
@override
@Uint8ListJsonConverter()
final Uint8List pixels;
final String source;
@override
final double width;
@override
Expand All @@ -1769,7 +1768,7 @@ class _$ImageElement implements ImageElement {

@override
String toString() {
return 'PadElement.image(layer: $layer, position: $position, constraints: $constraints, pixels: $pixels, width: $width, height: $height)';
return 'PadElement.image(layer: $layer, position: $position, constraints: $constraints, source: $source, width: $width, height: $height)';
}

@JsonKey(ignore: true)
Expand All @@ -1795,7 +1794,7 @@ class _$ImageElement implements ImageElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand All @@ -1814,7 +1813,7 @@ class _$ImageElement implements ImageElement {
ShapeProperty property)
shape,
}) {
return image(layer, position, constraints, pixels, width, height);
return image(layer, position, constraints, source, width, height);
}

@override
Expand All @@ -1834,7 +1833,7 @@ class _$ImageElement implements ImageElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand All @@ -1853,7 +1852,7 @@ class _$ImageElement implements ImageElement {
ShapeProperty property)?
shape,
}) {
return image?.call(layer, position, constraints, pixels, width, height);
return image?.call(layer, position, constraints, source, width, height);
}

@override
Expand All @@ -1873,7 +1872,7 @@ class _$ImageElement implements ImageElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand All @@ -1894,7 +1893,7 @@ class _$ImageElement implements ImageElement {
required TResult orElse(),
}) {
if (image != null) {
return image(layer, position, constraints, pixels, width, height);
return image(layer, position, constraints, source, width, height);
}
return orElse();
}
Expand Down Expand Up @@ -1952,7 +1951,7 @@ abstract class ImageElement implements PadElement {
{final String layer,
@DoublePointJsonConverter() final Point<double> position,
final ElementConstraints? constraints,
@Uint8ListJsonConverter() required final Uint8List pixels,
required final String source,
required final double width,
required final double height}) = _$ImageElement;

Expand All @@ -1964,8 +1963,7 @@ abstract class ImageElement implements PadElement {
@DoublePointJsonConverter()
Point<double> get position;
ElementConstraints? get constraints;
@Uint8ListJsonConverter()
Uint8List get pixels;
String get source;
double get width;
double get height;
@override
Expand Down Expand Up @@ -2116,7 +2114,7 @@ class _$SvgElement implements SvgElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand Down Expand Up @@ -2155,7 +2153,7 @@ class _$SvgElement implements SvgElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -2194,7 +2192,7 @@ class _$SvgElement implements SvgElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -2406,7 +2404,7 @@ class _$ShapeElement implements ShapeElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)
image,
Expand Down Expand Up @@ -2445,7 +2443,7 @@ class _$ShapeElement implements ShapeElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down Expand Up @@ -2484,7 +2482,7 @@ class _$ShapeElement implements ShapeElement {
String layer,
@DoublePointJsonConverter() Point<double> position,
ElementConstraints? constraints,
@Uint8ListJsonConverter() Uint8List pixels,
String source,
double width,
double height)?
image,
Expand Down
4 changes: 2 additions & 2 deletions api/lib/src/models/element.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions app/lib/helpers/element_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:butterfly_api/butterfly_api.dart';
import 'package:http/http.dart' as http;

Future<List<int>> getDataFromSource(String source) async {
final uri = Uri.parse(source);
final response = await http.get(uri);
return response.bodyBytes;
}

extension ImageElementDataExtension on ImageElement {
Future<List<int>> getData() => getDataFromSource(source);
}
9 changes: 4 additions & 5 deletions app/lib/renderers/elements/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ class ImageRenderer extends Renderer<ImageElement> {
void buildSvg(XmlDocument xml, AppDocument document, Rect viewportRect) {
if (!rect.overlaps(rect)) return;
// Create data url
final data = element.pixels;
final encoded = base64Encode(data);
final dataUrl = 'data:image/png;base64,$encoded';
final data = element.source;
// Create image
xml.getElement('svg')?.createElement('image', attributes: {
'x': '${rect.left}px',
'y': '${rect.top}px',
'width': '${rect.width}px',
'height': '${rect.height}px',
'xlink:href': dataUrl,
'xlink:href': data,
});
}

@override
FutureOr<void> setup(AppDocument document) async {
image = await decodeImageFromList(element.pixels);
image =
await decodeImageFromList(Uint8List.fromList(await element.getData()));
super.setup(document);
}

Expand Down
2 changes: 2 additions & 0 deletions app/lib/renderers/renderer.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:butterfly/helpers/element_helper.dart';
import 'package:butterfly/helpers/offset_helper.dart';
import 'package:butterfly/helpers/rect_helper.dart';
import 'package:butterfly/helpers/point_helper.dart';
Expand Down
6 changes: 3 additions & 3 deletions app/lib/selections/elements/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ImageElementSelection extends ElementSelection<ImageElement> {
leading: const Icon(PhosphorIcons.exportLight),
onTap: () async {
final localization = AppLocalizations.of(context);
final data = element.pixels;
final data = await element.getData();
if (!kIsWeb &&
(Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
var path = await FilePicker.platform.saveFile(
Expand All @@ -32,10 +32,10 @@ class ImageElementSelection extends ElementSelection<ImageElement> {
if (!(await file.exists())) {
file.create(recursive: true);
}
await file.writeAsBytes(data.buffer.asUint8List());
await file.writeAsBytes(data);
}
} else {
openImage(data.buffer.asUint8List());
openImage(data);
}
},
),
Expand Down
1 change: 1 addition & 0 deletions app/lib/selections/selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/current_index.dart';
import 'package:butterfly/dialogs/constraints.dart';
import 'package:butterfly/helpers/element_helper.dart';
import 'package:butterfly/helpers/num_helper.dart';
import 'package:butterfly/helpers/offset_helper.dart';
import 'package:butterfly/helpers/point_helper.dart';
Expand Down
Loading

0 comments on commit 2ea5502

Please sign in to comment.