Fix flips (#829)

This commit is contained in:
Steve Ruiz 2022-07-18 18:01:15 +01:00 committed by GitHub
parent b6c4059791
commit 54b66246d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View file

@ -34,8 +34,6 @@ export default function IFrameWarning({ url = 'https://tldraw.com' }: { url?: st
} }
document.execCommand('copy') document.execCommand('copy')
} catch (err) { } catch (err) {
console.log('nope')
null // Could not copy to clipboard null // Could not copy to clipboard
} finally { } finally {
document.body.removeChild(textarea) document.body.removeChild(textarea)

View file

@ -19,31 +19,46 @@ describe('Flip command', () => {
}) })
it('does, undoes and redoes command', () => { it('does, undoes and redoes command', () => {
app.select('rect1', 'rect2') expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 0])
app.flipHorizontal() expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([100, 100])
app.select('rect1', 'rect2').flipHorizontal()
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0]) expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([0, 100])
app.undo() app.undo()
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 0]) expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([100, 100])
app.redo() app.redo()
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0]) expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([0, 100])
}) })
it('flips horizontally', () => { it('flips horizontally', () => {
app.select('rect1', 'rect2') app.select('rect1', 'rect2')
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([100, 100])
app.flipHorizontal() app.flipHorizontal()
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0]) expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([100, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([0, 100])
}) })
it('flips vertically', () => { it('flips vertically', () => {
app.select('rect1', 'rect2') app.select('rect1', 'rect2')
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 0])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([100, 100])
app.flipVertical() app.flipVertical()
expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 100]) expect(app.getShape<RectangleShape>('rect1').point).toStrictEqual([0, 100])
expect(app.getShape<RectangleShape>('rect2').point).toStrictEqual([100, 0])
}) })
}) })

View file

@ -5,9 +5,13 @@ import type { TldrawApp } from '../../internal'
import { TLDR } from '~state/TLDR' import { TLDR } from '~state/TLDR'
export function flipShapes(app: TldrawApp, ids: string[], type: FlipType): TldrawCommand { export function flipShapes(app: TldrawApp, ids: string[], type: FlipType): TldrawCommand {
const { selectedIds, currentPageId, shapes } = app const {
selectedIds,
currentPageId,
page: { shapes },
} = app
const boundsForShapes = shapes.map((shape) => TLDR.getBounds(shape)) const boundsForShapes = ids.map((id) => TLDR.getBounds(shapes[id]))
const commonBounds = Utils.getCommonBounds(boundsForShapes) const commonBounds = Utils.getCommonBounds(boundsForShapes)