-
Notifications
You must be signed in to change notification settings - Fork 299
Rotate and Delete from the ground up #660
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
Open
hchiam
wants to merge
17
commits into
GoogleChromeLabs:main
Choose a base branch
from
hchiam:rotate-and-delete-613-ground-up
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bb65179
setup rotatable Rotation (to fix #613 - try number 2 after PR #657)
hchiam 5a52cca
create a visbug-delete element
hchiam 30f5f6a
tweak styling of rotation and delete elements
hchiam 8c17558
rotate rotation icon with rotation degrees
hchiam 50bfb7d
fix the posiitioning of the handle during rotation
hchiam ba7a377
selectable has both delete and rotation, and delete deletes rotation …
hchiam 3429524
avoid annoying hover bug when rotating by making rotation handle stay…
hchiam cc565d9
avoid Uncaught DOMException: HTMLElement.showPopover: Element is not …
hchiam 3080ee7
rotation handle stays centered on cursor
hchiam 217063c
show a line to the rotation handle during rotation
hchiam 4e53191
fixed rotatiion "flip" at certain angles
hchiam 256dc76
you can continue rotating from where you last let go
hchiam 4bd3fa7
revert/cleanup
hchiam 5e51cdf
import Delete more consistently
hchiam 991e155
fix pressing delete on the keyboard also now deletes associated rotat…
hchiam 5a0229b
Rotation changes display inline to inline-block - should be safe sinc…
hchiam 9e97ba6
animateViewTransition (with fallback)
hchiam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
@import "../_variables.css"; | ||
|
||
:host { | ||
position: var(--position, absolute); | ||
top: var(--top, 0); | ||
left: var(--left, 0); | ||
z-index: var(--layer-top); | ||
} | ||
|
||
:host button { | ||
background: var(--neon-pink); | ||
border: none; | ||
border-radius: 50%; | ||
width: 24px; | ||
height: 24px; | ||
cursor: pointer; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
color: white; | ||
font-weight: bold; | ||
padding: 0; | ||
transition: transform 0.2s ease; | ||
} | ||
|
||
:host button:hover { | ||
transform: scale(1.1); | ||
} | ||
|
||
:host svg { | ||
width: 16px; | ||
height: 16px; | ||
fill: currentColor; | ||
} |
This file contains hidden or 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,82 @@ | ||
import { DeleteStyles } from '../styles.store' | ||
import { animateViewTransition } from '../../utilities/' | ||
|
||
export class Delete extends HTMLElement { | ||
constructor() { | ||
super() | ||
this.$shadow = this.attachShadow({mode: 'closed'}) | ||
this.styles = [DeleteStyles] | ||
this.observers = [] | ||
} | ||
|
||
addObservers(observers) { | ||
this.observers = observers | ||
} | ||
|
||
set linkedElementsToDeleteToo(elements) { | ||
this._linkedElementsToDeleteToo = elements | ||
} | ||
|
||
connectedCallback() { | ||
this.$shadow.adoptedStyleSheets = this.styles | ||
this.$shadow.innerHTML = this.render() | ||
|
||
const deleteBtn = this.$shadow.querySelector('button') | ||
deleteBtn.addEventListener('click', this.deleteElement.bind(this)) | ||
} | ||
|
||
set position({el}) { | ||
this.targetElement = el | ||
const {top, right} = el.getBoundingClientRect() | ||
const isFixed = getComputedStyle(el).position === 'fixed' | ||
|
||
this.style.setProperty('--top', `${top + (isFixed ? 0 : window.scrollY)}px`) | ||
this.style.setProperty('--left', `${right + 10}px`) | ||
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute') | ||
} | ||
|
||
async deleteElement(e) { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
|
||
if (!this.targetElement) { | ||
console.warn('No target element found to delete') | ||
return | ||
} | ||
|
||
this.observers.forEach(observer => observer.disconnect()) | ||
|
||
const elements = [ | ||
this.targetElement, | ||
...this._linkedElementsToDeleteToo || [], | ||
...Array.from(document.querySelectorAll(`[data-label-id="${this.targetElement.getAttribute('data-label-id')}"]`)), | ||
this | ||
] | ||
|
||
await animateViewTransition(elements, () => this.performDeletion()) | ||
} | ||
|
||
performDeletion() { | ||
this._linkedElementsToDeleteToo?.forEach(el => el.remove()) | ||
this.targetElement.remove() | ||
|
||
const labelId = this.targetElement.getAttribute('data-label-id') | ||
if (labelId) { | ||
document.querySelectorAll(`[data-label-id="${labelId}"]`).forEach(el => el.remove()) | ||
} | ||
|
||
this.remove() | ||
} | ||
|
||
render() { | ||
return ` | ||
<button type="button" aria-label="Delete element"> | ||
<svg viewBox="0 0 24 24"> | ||
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/> | ||
</svg> | ||
</button> | ||
` | ||
} | ||
} | ||
|
||
customElements.define('visbug-delete', Delete) |
This file contains hidden or 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,56 @@ | ||
@import "../_variables.css"; | ||
|
||
:host { | ||
position: var(--position); | ||
top: var(--top); | ||
left: var(--left); | ||
pointer-events: none; | ||
z-index: var(--layer-3); | ||
} | ||
|
||
:host .rotation-line { | ||
display: none; | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
pointer-events: none; | ||
} | ||
|
||
:host .rotation-line.active { | ||
display: block; | ||
} | ||
|
||
:host .rotation-line line { | ||
stroke: var(--neon-pink); | ||
stroke-width: 1; | ||
} | ||
|
||
:host .rotation-handle { | ||
position: absolute; | ||
width: 24px; | ||
height: 24px; | ||
top: -30px; | ||
left: calc(var(--width) / 2 - 12px); | ||
cursor: grab; | ||
pointer-events: all; | ||
background: var(--neon-pink); | ||
border-radius: 50%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
user-select: none; | ||
-webkit-user-select: none; | ||
} | ||
|
||
:host .rotation-handle:active { | ||
cursor: grabbing; | ||
} | ||
|
||
:host .rotation-icon { | ||
width: 16px; | ||
height: 16px; | ||
fill: white; | ||
pointer-events: none; | ||
} |
This file contains hidden or 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,132 @@ | ||
import { HandlesStyles, RotationStyles } from '../styles.store' | ||
|
||
export class Rotation extends HTMLElement { | ||
constructor() { | ||
super() | ||
this.$shadow = this.attachShadow({mode: 'closed'}) | ||
this.styles = [HandlesStyles, RotationStyles] | ||
this.totalAngle = 0 | ||
} | ||
|
||
connectedCallback() { | ||
this.$shadow.adoptedStyleSheets = this.styles | ||
} | ||
|
||
set position({el}) { | ||
this.targetElement = el | ||
|
||
const computedStyle = getComputedStyle(el) | ||
if (computedStyle.display === 'inline') el.style.display = 'inline-block' | ||
|
||
const {left, top, width, height} = el.getBoundingClientRect() | ||
const isFixed = computedStyle.position === 'fixed' | ||
|
||
this.style.setProperty('--top', `${top + (isFixed ? 0 : window.scrollY)}px`) | ||
this.style.setProperty('--left', `${left}px`) | ||
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute') | ||
this.style.setProperty('--width', `${width}px`) | ||
|
||
this.$shadow.innerHTML = this.render() | ||
this.setupRotationHandlers() | ||
} | ||
|
||
setupRotationHandlers() { | ||
const handle = this.$shadow.querySelector('.rotation-handle') | ||
const line = this.$shadow.querySelector('.rotation-line') | ||
|
||
const onMouseDown = e => { | ||
e.preventDefault() | ||
const {left, top, width, height} = this.targetElement.getBoundingClientRect() | ||
this.originalCenter = { | ||
x: left + width / 2, | ||
y: top + height / 2 | ||
} | ||
this.lastAngle = Math.atan2( | ||
e.clientY - this.originalCenter.y, | ||
e.clientX - this.originalCenter.x | ||
) | ||
|
||
this.handleRadius = Math.sqrt( | ||
Math.pow(e.clientX - this.originalCenter.x, 2) + | ||
Math.pow(e.clientY - this.originalCenter.y, 2) | ||
) | ||
|
||
line.classList.add('active') | ||
|
||
document.addEventListener('mousemove', onMouseMove) | ||
document.addEventListener('mouseup', onMouseUp) | ||
} | ||
|
||
const onMouseMove = e => { | ||
const currentAngle = Math.atan2( | ||
e.clientY - this.originalCenter.y, | ||
e.clientX - this.originalCenter.x | ||
) | ||
|
||
// track accumulated rotation to allow multiple revolutions | ||
if (!this.lastAngle) this.lastAngle = currentAngle | ||
let delta = currentAngle - this.lastAngle | ||
// normalize the delta to avoid "flipping" at boundary crossing | ||
if (delta > Math.PI) { | ||
delta -= 2 * Math.PI | ||
} else if (delta < -Math.PI) { | ||
delta += 2 * Math.PI | ||
} | ||
|
||
this.totalAngle += delta | ||
this.lastAngle = currentAngle | ||
|
||
const rotationDegrees = this.totalAngle * (180 / Math.PI) | ||
this.targetElement.style.transform = `rotate(${rotationDegrees}deg)` | ||
|
||
const handleX = e.clientX | ||
const handleY = e.clientY | ||
|
||
const hostRect = this.getBoundingClientRect() | ||
const handleRect = handle.getBoundingClientRect() | ||
const handleSize = handleRect.width | ||
|
||
// position handle centered on cursor | ||
handle.style.left = `${handleX - hostRect.left - handleSize/2}px` | ||
handle.style.top = `${handleY - hostRect.top - handleSize/2}px` | ||
|
||
const lineSvg = line.querySelector('line') | ||
lineSvg.setAttribute('x1', this.originalCenter.x) | ||
lineSvg.setAttribute('y1', this.originalCenter.y) | ||
lineSvg.setAttribute('x2', handleX) | ||
lineSvg.setAttribute('y2', handleY) | ||
} | ||
|
||
const onMouseUp = () => { | ||
this.lastAngle = 0 | ||
document.removeEventListener('mousemove', onMouseMove) | ||
document.removeEventListener('mouseup', onMouseUp) | ||
line.classList.remove('active') | ||
} | ||
|
||
handle.addEventListener('mousedown', onMouseDown) | ||
|
||
this.cleanup = () => { | ||
handle.removeEventListener('mousedown', onMouseDown) | ||
} | ||
} | ||
|
||
disconnectedCallback() { | ||
this.cleanup && this.cleanup() | ||
} | ||
|
||
render() { | ||
return ` | ||
<svg class="rotation-line"> | ||
<line/> | ||
</svg> | ||
<div class="rotation-handle"> | ||
<svg class="rotation-icon" viewBox="0 0 24 24"> | ||
<path d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"/> | ||
</svg> | ||
</div> | ||
` | ||
} | ||
} | ||
|
||
customElements.define('visbug-rotation', Rotation) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
love this so much
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.
human + ai teamwork for the win! i remember cleaning up the comments and code and making suggestions and asking for eli5 explanations of why this weird math works haha