-
Notifications
You must be signed in to change notification settings - Fork 5
Implement item layers #78
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { Draft } from 'immer' | ||
| import { max, merge, sortBy, sum } from 'lodash-es' | ||
| import { max, merge, partition, sortBy, sum } from 'lodash-es' | ||
| import { StoreMutationCommand } from 'tapestry-core-client/src/lib/store/index' | ||
| import { tween } from 'tapestry-core-client/src/view-model/tweening' | ||
| import { | ||
|
|
@@ -26,6 +26,7 @@ import { itemUpload } from '../../../../services/item-upload' | |
| import { | ||
| getGridDimensions, | ||
| getGridIndices, | ||
| getMaxLayer, | ||
| getMultiselectRectangle, | ||
| GridState, | ||
| reassignPresentationStep, | ||
|
|
@@ -81,8 +82,10 @@ export function addAndPositionItems( | |
| ? translate(centerAtPoint, mul(-1, translation), scale) | ||
| : centerAtPoint | ||
| const move = vector(boundingRect.center, center) | ||
| let maxLayer = getMaxLayer(store) | ||
| items.forEach((item) => { | ||
| item.dto.position = translate(item.dto.position, move) | ||
| item.dto.layer = ++maxLayer | ||
| }) | ||
|
|
||
| store.dispatch(insertItems(items), selectItems(items.map((i) => i.dto.id))) | ||
|
|
@@ -353,3 +356,19 @@ export function removeFromGroup(itemId: string): StoreMutationCommand<EditableTa | |
| } | ||
| } | ||
| } | ||
|
|
||
| export function reorderItems( | ||
| ids: OneOrMore<string>, | ||
| to: 'back' | 'front', | ||
| ): StoreMutationCommand<EditableTapestryViewModel> { | ||
| return (_, { store }) => { | ||
| const itemIds = new Set(ensureArray(ids)) | ||
|
|
||
| const [frontItems, backItems] = partition(idMapToArray(store.get(`items`)), (i) => | ||
| to === 'front' ? itemIds.has(i.dto.id) : !itemIds.has(i.dto.id), | ||
| ) | ||
|
|
||
| const allItems = [...sortBy(backItems, ['layer']), ...sortBy(frontItems, ['layer'])] | ||
| store.dispatch(...allItems.map((item, layer) => updateItem(item.dto.id, { dto: { layer } }))) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make a single batch-mutation request instead of N updates, right?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this updateItem command is also used when manipulating a selection - deleting, arranging, grouping.... |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import { | |
| } from 'tapestry-core-client/src/view-model' | ||
| import { getSelectionItems, isItemInSelection } from 'tapestry-core-client/src/view-model/utils' | ||
| import { Id } from 'tapestry-core/src/data-format/schemas/common' | ||
| import { Point, Rectangle, translate, vector } from 'tapestry-core/src/lib/geometry' | ||
| import { translate, vector } from 'tapestry-core/src/lib/geometry' | ||
| import { router } from '../../main' | ||
| import { InteractionMode, TapestryEditorStore } from '../../pages/tapestry/view-model' | ||
| import { | ||
|
|
@@ -61,12 +61,6 @@ const { eventListener, attachListeners, detachListeners } = createEventRegistry< | |
| InteractionMode | 'desktop' | 'mobile' | ||
| >() | ||
|
|
||
| function isDraggingItems( | ||
| event: DragEvent<HoveredDragTarget> | DragEndEvent<HoveredDragTarget>, | ||
| ): event is DragEvent<HoveredDragTarget> | DragEndEvent<HoveredDragTarget> { | ||
| return isHoveredDragTarget(event.detail.dragTarget) | ||
| } | ||
|
|
||
| function getDraggedItem(event: DragEvent<HoveredDragTarget> | DragStartEvent<HoveredDragTarget>) { | ||
| const { dragTarget: draggedElement } = event.detail | ||
|
|
||
|
|
@@ -302,9 +296,7 @@ export class EditorItemController extends ItemController { | |
| protected onDragEnd(e: DragEndEvent<HoveredDragTarget | ResizeTarget>) { | ||
| this.stage.gestureDetector.activate() | ||
| this.editorStore.dispatch( | ||
| isDraggingItems(e) | ||
| ? setPointerInteraction('hover', e.detail.dragTarget) | ||
| : setPointerInteraction(null), | ||
| setPointerInteraction('hover', e.detail.dragTarget), | ||
| setSelectionRect(null), | ||
| updateSelectionItems({ dragState: null }), | ||
| (model) => { | ||
|
|
@@ -315,28 +307,11 @@ export class EditorItemController extends ItemController { | |
|
|
||
| @eventListener('dragHandler', 'drag') | ||
| protected onDrag(event: DragEvent<HoveredDragTarget>) { | ||
| if (this.editorStore.get('interactionMode') === 'edit' && isDraggingItems(event)) { | ||
| if (this.editorStore.get('interactionMode') === 'edit') { | ||
| this.onDragItems(event) | ||
| } else { | ||
| this.onDragSelectionRect(event.detail.currentPoint) | ||
| } | ||
| } | ||
|
|
||
| private onDragSelectionRect(cursorLocation: Point) { | ||
| const pointerSelection = this.store.get('pointerSelection') | ||
| if (!pointerSelection) return | ||
|
|
||
| const point = this.stage.pixi.tapestry.app.stage.worldTransform.applyInverse(cursorLocation) | ||
| this.editorStore.dispatch( | ||
| setSelectionRect( | ||
| new Rectangle(pointerSelection.rect.position, { | ||
| width: point.x - pointerSelection.rect.position.x, | ||
| height: point.y - pointerSelection.rect.position.y, | ||
| }), | ||
| ), | ||
| ) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this (and isDraggingItems) no longer needed?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is being handled in the core-client item controller, and this function here was not being called at all. Also in the editor-item-controller the drag handler is set so that it is impossible to start a drag operation without dragging items. |
||
|
|
||
| private onDragItems(event: DragEvent<HoveredDragTarget>) { | ||
| const { worldTransform } = this.stage.pixi.tapestry.app.stage | ||
| const guidelineSpacing = event.detail.originalEvent?.ctrlKey | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the
satisfiesoperator?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To enforce type constraint on the object. Otherwise a missing layer prop would not result a compile time error.