Tldraw/packages/tldraw/src/lib/tools/SelectTool/childStates/Crop/children/crop_helpers.ts

65 wiersze
1.6 KiB
TypeScript
Czysty Zwykły widok Historia

tldraw zero - package shuffle (#1710) This PR moves code between our packages so that: - @tldraw/editor is a “core” library with the engine and canvas but no shapes, tools, or other things - @tldraw/tldraw contains everything particular to the experience we’ve built for tldraw At first look, this might seem like a step away from customization and configuration, however I believe it greatly increases the configuration potential of the @tldraw/editor while also providing a more accurate reflection of what configuration options actually exist for @tldraw/tldraw. ## Library changes @tldraw/editor re-exports its dependencies and @tldraw/tldraw re-exports @tldraw/editor. - users of @tldraw/editor WITHOUT @tldraw/tldraw should almost always only import things from @tldraw/editor. - users of @tldraw/tldraw should almost always only import things from @tldraw/tldraw. - @tldraw/polyfills is merged into @tldraw/editor - @tldraw/indices is merged into @tldraw/editor - @tldraw/primitives is merged mostly into @tldraw/editor, partially into @tldraw/tldraw - @tldraw/file-format is merged into @tldraw/tldraw - @tldraw/ui is merged into @tldraw/tldraw Many (many) utils and other code is moved from the editor to tldraw. For example, embeds now are entirely an feature of @tldraw/tldraw. The only big chunk of code left in core is related to arrow handling. ## API Changes The editor can now be used without tldraw's assets. We load them in @tldraw/tldraw instead, so feel free to use whatever fonts or images or whatever that you like with the editor. All tools and shapes (except for the `Group` shape) are moved to @tldraw/tldraw. This includes the `select` tool. You should use the editor with at least one tool, however, so you now also need to send in an `initialState` prop to the Editor / <TldrawEditor> component indicating which state the editor should begin in. The `components` prop now also accepts `SelectionForeground`. The complex selection component that we use for tldraw is moved to @tldraw/tldraw. The default component is quite basic but can easily be replaced via the `components` prop. We pass down our tldraw-flavored SelectionFg via `components`. Likewise with the `Scribble` component: the `DefaultScribble` no longer uses our freehand tech and is a simple path instead. We pass down the tldraw-flavored scribble via `components`. The `ExternalContentManager` (`Editor.externalContentManager`) is removed and replaced with a mapping of types to handlers. - Register new content handlers with `Editor.registerExternalContentHandler`. - Register new asset creation handlers (for files and URLs) with `Editor.registerExternalAssetHandler` ### Change Type - [x] `major` — Breaking change ### Test Plan - [x] Unit Tests - [x] End to end tests ### Release Notes - [@tldraw/editor] lots, wip - [@tldraw/ui] gone, merged to tldraw/tldraw - [@tldraw/polyfills] gone, merged to tldraw/editor - [@tldraw/primitives] gone, merged to tldraw/editor / tldraw/tldraw - [@tldraw/indices] gone, merged to tldraw/editor - [@tldraw/file-format] gone, merged to tldraw/tldraw --------- Co-authored-by: alex <alex@dytry.ch>
2023-07-17 21:22:34 +00:00
import {
Editor,
TLBaseShape,
TLImageShapeCrop,
TLShapePartial,
Vec2d,
deepCopy,
} from '@tldraw/editor'
2023-04-25 11:01:25 +00:00
export type ShapeWithCrop = TLBaseShape<string, { w: number; h: number; crop: TLImageShapeCrop }>
2023-04-25 11:01:25 +00:00
export function getTranslateCroppedImageChange(
editor: Editor,
shape: TLBaseShape<string, { w: number; h: number; crop: TLImageShapeCrop }>,
2023-04-25 11:01:25 +00:00
delta: Vec2d
) {
if (!shape) {
throw Error('Needs to translate a cropped shape!')
}
const { crop: oldCrop } = shape.props
if (!oldCrop) {
// can't translate a shape that doesn't have an existing crop
return
}
const flatten: 'x' | 'y' | null = editor.inputs.shiftKey
2023-04-25 11:01:25 +00:00
? Math.abs(delta.x) < Math.abs(delta.y)
? 'x'
: 'y'
: null
if (flatten === 'x') {
delta.x = 0
} else if (flatten === 'y') {
delta.y = 0
}
delta.rot(-shape.rotation)
// original (uncropped) width and height of shape
const w = (1 / (oldCrop.bottomRight.x - oldCrop.topLeft.x)) * shape.props.w
const h = (1 / (oldCrop.bottomRight.y - oldCrop.topLeft.y)) * shape.props.h
const yCrop = oldCrop.bottomRight.y - oldCrop.topLeft.y
const xCrop = oldCrop.bottomRight.x - oldCrop.topLeft.x
const newCrop = deepCopy(oldCrop)
newCrop.topLeft.x = Math.min(1 - xCrop, Math.max(0, newCrop.topLeft.x - delta.x / w))
newCrop.topLeft.y = Math.min(1 - yCrop, Math.max(0, newCrop.topLeft.y - delta.y / h))
newCrop.bottomRight.x = newCrop.topLeft.x + xCrop
newCrop.bottomRight.y = newCrop.topLeft.y + yCrop
const partial: TLShapePartial<typeof shape> = {
id: shape.id,
type: shape.type,
props: {
crop: newCrop,
},
}
return partial
}