Skip to content

Commit

Permalink
Fix elements not being selected after moving
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 8, 2024
1 parent 17e37d3 commit f3bd8ce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
28 changes: 14 additions & 14 deletions app/lib/bloc/document_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
e.copyWith(source: importImage(e.source, 'svg')),
_ => e,
})
.map((e) => e.copyWith(id: createUniqueId()))
.map((e) => e.copyWith(id: e.id ?? createUniqueId()))
.toList();
final renderers = elements.map((e) => Renderer.fromInstance(e)).toList();
if (renderers.isEmpty) return;
Expand All @@ -190,7 +190,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
page: current.page
.copyWith(content: [...current.page.content, ...elements])),
addedElements: renderers,
refresh: current.currentIndexCubit
shouldRefresh: () => current.currentIndexCubit
.getHandler()
.onRenderersCreated(current.page, renderers),
);
Expand All @@ -202,10 +202,11 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
final renderers = <Renderer<PadElement>>[];
var selection = current.currentIndexCubit.state.selection;
final page = current.page;
bool shouldRefresh = false;
final oldRenderers = current.renderers;
final elements = event.elements.map((key, value) => MapEntry(
key, value.map((e) => e.copyWith(id: createUniqueId())).toList()));
final replacedRenderers =
<Renderer<PadElement>, List<Renderer<PadElement>>>{};
for (final renderer in oldRenderers) {
final id = renderer.element.id;
final updated = elements[id];
Expand All @@ -226,11 +227,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
selection = newSelection;
}
}
shouldRefresh = current.currentIndexCubit
.getHandler()
.onRendererUpdated(
current.page, renderer, updatedRenderers) ||
shouldRefresh;
replacedRenderers[renderer] = updatedRenderers;
} else {
renderers.add(renderer);
}
Expand All @@ -244,7 +241,10 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
page: page.copyWith(content: content),
),
replacedElements: renderers,
refresh: true,
shouldRefresh: () => replacedRenderers.entries.any((element) => current
.currentIndexCubit
.getHandler()
.onRendererUpdated(page, element.key, element.value)),
);
}, transformer: sequential());
on<ElementsArranged>((event, emit) async {
Expand Down Expand Up @@ -546,7 +546,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
? event.newName
: current.currentLayer),
addedElements: null,
refresh: true,
shouldRefresh: () => true,
);
});

Expand Down Expand Up @@ -662,7 +662,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
return _saveState(
emit,
state: current.copyWith(page: currentDocument),
refresh: true,
shouldRefresh: () => true,
reset: shouldRepaint,
);
});
Expand All @@ -683,7 +683,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
_saveState(
emit,
state: current.copyWith(page: currentPage),
refresh: true,
shouldRefresh: () => true,
reset: shouldRepaint,
);
});
Expand Down Expand Up @@ -889,7 +889,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
List<Renderer<PadElement>>? replacedElements,
List<Renderer<Background>>? backgrounds,
bool reset = false,
bool refresh = false,
bool Function()? shouldRefresh,
bool updateIndex = false,
}) {
if (this.state is! DocumentLoadSuccess && state == null) return;
Expand All @@ -902,7 +902,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
replacedElements: replacedElements,
backgrounds: backgrounds,
reset: reset,
refresh: refresh,
shouldRefresh: shouldRefresh,
updateIndex: updateIndex,
);
}
Expand Down
7 changes: 3 additions & 4 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
List<Renderer<PadElement>>? replacedElements,
List<Renderer<Background>>? backgrounds,
bool reset = false,
bool refresh = false,
bool Function()? shouldRefresh,
bool updateIndex = false,
}) async {
final cameraViewport = current.cameraViewport;
Expand Down Expand Up @@ -969,9 +969,8 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
if (reset) {
loadElements(current);
}
if (reset || refresh) {
this.refresh(
current.data, current.assetService, current.page, current.info);
if (reset || shouldRefresh?.call() == true) {
refresh(current.data, current.assetService, current.page, current.info);
}
if (updateIndex) {
this.updateIndex(bloc);
Expand Down
30 changes: 21 additions & 9 deletions app/lib/handlers/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SelectHandler extends Handler<SelectTool> {
})
.whereNotNull()
.toList();
_updateSelectionRect();
return changed;
}

Expand All @@ -60,11 +61,12 @@ class SelectHandler extends Handler<SelectTool> {
DocumentPage page, Renderer old, List<Renderer> updated) {
bool changed = false;
if (old is Renderer<PadElement> &&
_selected.contains(old) &&
_selected.any((e) => e.id == old.id) &&
updated is List<Renderer<PadElement>>) {
_selected.remove(old);
_selected.removeWhere((e) => e.id == old.id);
_selected.addAll(updated);
changed = true;
_updateSelectionRect();
}
return changed;
}
Expand Down Expand Up @@ -131,20 +133,24 @@ class SelectHandler extends Handler<SelectTool> {
return foregrounds;
}

bool _submitTransform(DocumentBloc bloc) {
if (!_selectionManager.isTransforming) return false;
List<Renderer<PadElement>>? _submitTransform(DocumentBloc bloc) {
if (!_selectionManager.isTransforming) return null;
final state = bloc.state;
if (state is! DocumentLoadSuccess) return false;
if (state is! DocumentLoadSuccess) return null;
final current = _getTransformed();
_selectionManager.deselect();
if (current == null) return null;
bloc.add(_duplicate
? ElementsCreated(current!.map((e) => e.element).toList())
: ElementsChanged(Map.fromEntries(current!.mapIndexed((i, e) {
? ElementsCreated(current.map((e) => e.element).toList())
: ElementsChanged(Map.fromEntries(current.mapIndexed((i, e) {
final id = _selected[i].element.id;
if (id == null) return null;
return MapEntry(id, [e.element]);
}).whereNotNull())));
return true;
if (_duplicate) {
return _selected;
}
return current;
}

@override
Expand Down Expand Up @@ -376,7 +382,13 @@ class SelectHandler extends Handler<SelectTool> {
_rulerRotation = null;
return;
}
if (_submitTransform(context.getDocumentBloc())) return;
final transformed = _submitTransform(context.getDocumentBloc());
if (transformed != null) {
_selected.clear();
_selected.addAll(transformed);
_updateSelectionRect();
return;
}
_lassoFreeSelection = null;
_rectangleFreeSelection = null;
if (!context.isCtrlPressed) {
Expand Down
1 change: 1 addition & 0 deletions metadata/en-US/changelogs/112.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* Save utilities states after restart
* Ignore invalid files with invalid syntax
* Fix elements not being selected after moving
* Fix file gets duplicated on save (again)
* Fix image background wrong offset when moving on canvas
* Fix packs dialog showing templates
Expand Down

0 comments on commit f3bd8ce

Please sign in to comment.