Fix move to page (#143)
* Remove references to window * Improve drawn strokes for other dash styles
This commit is contained in:
parent
519c18f300
commit
9b2de95ec9
8 changed files with 35 additions and 30 deletions
|
@ -13,7 +13,7 @@ export default function Embedded(): JSX.Element {
|
||||||
marginBottom: '32px',
|
marginBottom: '32px',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TLDraw id="small1" />
|
<TLDraw id="small3" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
@ -24,7 +24,7 @@ export default function Embedded(): JSX.Element {
|
||||||
overflow: 'hidden',
|
overflow: 'hidden',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TLDraw id="small2" />
|
<TLDraw id="small4" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -48,8 +48,6 @@ export function StylePanel(): JSX.Element {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const showKbds = !Utils.isMobileSize()
|
|
||||||
|
|
||||||
const selectedShapesCountSelector = (s: Data) =>
|
const selectedShapesCountSelector = (s: Data) =>
|
||||||
s.document.pageStates[s.appState.currentPageId].selectedIds.length
|
s.document.pageStates[s.appState.currentPageId].selectedIds.length
|
||||||
|
|
||||||
|
@ -57,6 +55,8 @@ function SelectedShapeContent(): JSX.Element {
|
||||||
const { tlstate, useSelector } = useTLDrawContext()
|
const { tlstate, useSelector } = useTLDrawContext()
|
||||||
const selectedShapesCount = useSelector(selectedShapesCountSelector)
|
const selectedShapesCount = useSelector(selectedShapesCountSelector)
|
||||||
|
|
||||||
|
const [showKbds] = React.useState(() => !Utils.isMobileSize())
|
||||||
|
|
||||||
const handleCopy = React.useCallback(() => {
|
const handleCopy = React.useCallback(() => {
|
||||||
tlstate.copy()
|
tlstate.copy()
|
||||||
}, [tlstate])
|
}, [tlstate])
|
||||||
|
|
|
@ -215,7 +215,7 @@ function InnerTldraw({
|
||||||
page={page}
|
page={page}
|
||||||
pageState={pageState}
|
pageState={pageState}
|
||||||
users={users}
|
users={users}
|
||||||
userId={tlstate.state.room.userId}
|
userId={tlstate.state.room?.userId}
|
||||||
shapeUtils={tldrawShapeUtils}
|
shapeUtils={tldrawShapeUtils}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
meta={meta}
|
meta={meta}
|
||||||
|
|
|
@ -103,18 +103,18 @@ export const Draw = new ShapeUtil<DrawShape, SVGSVGElement, TLDrawMeta>(() => ({
|
||||||
const strokeDasharray = {
|
const strokeDasharray = {
|
||||||
[DashStyle.Draw]: 'none',
|
[DashStyle.Draw]: 'none',
|
||||||
[DashStyle.Solid]: `none`,
|
[DashStyle.Solid]: `none`,
|
||||||
[DashStyle.Dotted]: `${strokeWidth / 10} ${strokeWidth * 3}`,
|
[DashStyle.Dotted]: `0.1 ${strokeWidth * 4}`,
|
||||||
[DashStyle.Dashed]: `${strokeWidth * 3} ${strokeWidth * 3}`,
|
[DashStyle.Dashed]: `${strokeWidth * 4} ${strokeWidth * 4}`,
|
||||||
}[style.dash]
|
}[style.dash]
|
||||||
|
|
||||||
const strokeDashoffset = {
|
const strokeDashoffset = {
|
||||||
[DashStyle.Draw]: 'none',
|
[DashStyle.Draw]: 'none',
|
||||||
[DashStyle.Solid]: `none`,
|
[DashStyle.Solid]: `none`,
|
||||||
[DashStyle.Dotted]: `-${strokeWidth / 20}`,
|
[DashStyle.Dotted]: `0`,
|
||||||
[DashStyle.Dashed]: `-${strokeWidth}`,
|
[DashStyle.Dashed]: `0`,
|
||||||
}[style.dash]
|
}[style.dash]
|
||||||
|
|
||||||
const sw = 1 + strokeWidth * 2
|
const sw = 1 + strokeWidth * 1.5
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SVGContainer ref={ref} id={shape.id + '_svg'} {...events}>
|
<SVGContainer ref={ref} id={shape.id + '_svg'} {...events}>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { Data, TLDrawCommand, TLDrawPage } from '~types'
|
import type { Data, TLDrawCommand, TLDrawPage } from '~types'
|
||||||
import { Utils, TLPageState } from '@tldraw/core'
|
import { Utils, TLPageState } from '@tldraw/core'
|
||||||
|
|
||||||
export function createPage(data: Data, pageId = Utils.uniqueId()): TLDrawCommand {
|
export function createPage(data: Data, center: number[], pageId = Utils.uniqueId()): TLDrawCommand {
|
||||||
const { currentPageId } = data.appState
|
const { currentPageId } = data.appState
|
||||||
|
|
||||||
const topPage = Object.values(data.document.pages).sort(
|
const topPage = Object.values(data.document.pages).sort(
|
||||||
|
@ -11,20 +11,20 @@ export function createPage(data: Data, pageId = Utils.uniqueId()): TLDrawCommand
|
||||||
const nextChildIndex = topPage?.childIndex ? topPage?.childIndex + 1 : 1
|
const nextChildIndex = topPage?.childIndex ? topPage?.childIndex + 1 : 1
|
||||||
|
|
||||||
// TODO: Iterate the name better
|
// TODO: Iterate the name better
|
||||||
const nextName = `Page ${nextChildIndex}`
|
const nextName = `New Page`
|
||||||
|
|
||||||
const page: TLDrawPage = {
|
const page: TLDrawPage = {
|
||||||
id: pageId,
|
id: pageId,
|
||||||
name: nextName,
|
name: nextName,
|
||||||
shapes: {},
|
|
||||||
childIndex: nextChildIndex,
|
childIndex: nextChildIndex,
|
||||||
|
shapes: {},
|
||||||
bindings: {},
|
bindings: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageState: TLPageState = {
|
const pageState: TLPageState = {
|
||||||
id: pageId,
|
id: pageId,
|
||||||
selectedIds: [],
|
selectedIds: [],
|
||||||
camera: { point: [-window.innerWidth / 2, -window.innerHeight / 2], zoom: 1 },
|
camera: { point: center, zoom: 1 },
|
||||||
currentParentId: pageId,
|
currentParentId: pageId,
|
||||||
editingId: undefined,
|
editingId: undefined,
|
||||||
bindingId: undefined,
|
bindingId: undefined,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { Data, TLDrawCommand } from '~types'
|
import type { Data, TLDrawCommand } from '~types'
|
||||||
import { Utils } from '@tldraw/core'
|
import { Utils } from '@tldraw/core'
|
||||||
|
|
||||||
export function duplicatePage(data: Data, pageId: string): TLDrawCommand {
|
export function duplicatePage(data: Data, center: number[], pageId: string): TLDrawCommand {
|
||||||
const newId = Utils.uniqueId()
|
const newId = Utils.uniqueId()
|
||||||
const { currentPageId } = data.appState
|
const { currentPageId } = data.appState
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ export function duplicatePage(data: Data, pageId: string): TLDrawCommand {
|
||||||
const nextPage = {
|
const nextPage = {
|
||||||
...page,
|
...page,
|
||||||
id: newId,
|
id: newId,
|
||||||
|
name: page.name + ' Copy',
|
||||||
shapes: Object.fromEntries(
|
shapes: Object.fromEntries(
|
||||||
Object.entries(page.shapes).map(([id, shape]) => {
|
Object.entries(page.shapes).map(([id, shape]) => {
|
||||||
return [
|
return [
|
||||||
|
@ -51,7 +52,7 @@ export function duplicatePage(data: Data, pageId: string): TLDrawCommand {
|
||||||
...page,
|
...page,
|
||||||
id: newId,
|
id: newId,
|
||||||
selectedIds: [],
|
selectedIds: [],
|
||||||
camera: { point: [-window.innerWidth / 2, -window.innerHeight / 2], zoom: 1 },
|
camera: data.document.pageStates[currentPageId].camera,
|
||||||
currentParentId: newId,
|
currentParentId: newId,
|
||||||
editingId: undefined,
|
editingId: undefined,
|
||||||
bindingId: undefined,
|
bindingId: undefined,
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
import type { ArrowShape, Data, PagePartial, TLDrawCommand, TLDrawShape } from '~types'
|
import type { ArrowShape, Data, PagePartial, TLDrawCommand, TLDrawShape } from '~types'
|
||||||
import { TLDR } from '~state/tldr'
|
import { TLDR } from '~state/tldr'
|
||||||
import { Utils } from '@tldraw/core'
|
import { Utils, TLBounds } from '@tldraw/core'
|
||||||
import { Vec } from '@tldraw/vec'
|
import { Vec } from '@tldraw/vec'
|
||||||
|
|
||||||
export function moveToPage(
|
export function moveToPage(
|
||||||
data: Data,
|
data: Data,
|
||||||
ids: string[],
|
ids: string[],
|
||||||
|
viewportBounds: TLBounds,
|
||||||
fromPageId: string,
|
fromPageId: string,
|
||||||
toPageId: string
|
toPageId: string
|
||||||
): TLDrawCommand {
|
): TLDrawCommand {
|
||||||
|
@ -157,14 +158,18 @@ export function moveToPage(
|
||||||
// Finally, center camera on selection
|
// Finally, center camera on selection
|
||||||
|
|
||||||
const toPageState = data.document.pageStates[toPageId]
|
const toPageState = data.document.pageStates[toPageId]
|
||||||
|
|
||||||
const bounds = Utils.getCommonBounds(movingShapes.map((shape) => TLDR.getBounds(shape)))
|
const bounds = Utils.getCommonBounds(movingShapes.map((shape) => TLDR.getBounds(shape)))
|
||||||
|
|
||||||
const zoom = TLDR.getCameraZoom(
|
const zoom = TLDR.getCameraZoom(
|
||||||
window.innerWidth < window.innerHeight
|
viewportBounds.width < viewportBounds.height
|
||||||
? (window.innerWidth - 128) / bounds.width
|
? (viewportBounds.width - 128) / bounds.width
|
||||||
: (window.innerHeight - 128) / bounds.height
|
: (viewportBounds.height - 128) / bounds.height
|
||||||
)
|
)
|
||||||
const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
|
|
||||||
const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
|
const mx = (viewportBounds.width - bounds.width * zoom) / 2 / zoom
|
||||||
|
const my = (viewportBounds.height - bounds.height * zoom) / 2 / zoom
|
||||||
|
|
||||||
const point = Vec.round(Vec.add([-bounds.minX, -bounds.minY], [mx, my]))
|
const point = Vec.round(Vec.add([-bounds.minX, -bounds.minY], [mx, my]))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -940,7 +940,9 @@ export class TLDrawState extends StateManager<Data> {
|
||||||
* @param pageId (optional) The new page's id.
|
* @param pageId (optional) The new page's id.
|
||||||
*/
|
*/
|
||||||
createPage = (id?: string): this => {
|
createPage = (id?: string): this => {
|
||||||
return this.setState(Commands.createPage(this.state, id))
|
return this.setState(
|
||||||
|
Commands.createPage(this.state, [-this.bounds.width / 2, -this.bounds.height / 2], id)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -965,7 +967,9 @@ export class TLDrawState extends StateManager<Data> {
|
||||||
* @param pageId The id of the page to duplicate.
|
* @param pageId The id of the page to duplicate.
|
||||||
*/
|
*/
|
||||||
duplicatePage = (pageId: string): this => {
|
duplicatePage = (pageId: string): this => {
|
||||||
return this.setState(Commands.duplicatePage(this.state, pageId))
|
return this.setState(
|
||||||
|
Commands.duplicatePage(this.state, [-this.bounds.width / 2, -this.bounds.height / 2], pageId)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2091,7 +2095,7 @@ export class TLDrawState extends StateManager<Data> {
|
||||||
ids = this.selectedIds
|
ids = this.selectedIds
|
||||||
): this => {
|
): this => {
|
||||||
if (ids.length === 0) return this
|
if (ids.length === 0) return this
|
||||||
this.setState(Commands.moveToPage(this.state, ids, fromPageId, toPageId))
|
this.setState(Commands.moveToPage(this.state, ids, this.bounds, fromPageId, toPageId))
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2557,11 +2561,6 @@ export class TLDrawState extends StateManager<Data> {
|
||||||
}
|
}
|
||||||
|
|
||||||
onPinchEnd: TLPinchEventHandler = () => {
|
onPinchEnd: TLPinchEventHandler = () => {
|
||||||
// if (this.state.settings.isZoomSnap) {
|
|
||||||
// const i = Math.round((this.pageState.camera.zoom * 100) / 25)
|
|
||||||
// const nextZoom = TLDR.getCameraZoom(i * 0.25)
|
|
||||||
// this.zoomTo(nextZoom, inputs.pointer?.point)
|
|
||||||
// }
|
|
||||||
this.undoSelect()
|
this.undoSelect()
|
||||||
this.setStatus(TLDrawStatus.Idle)
|
this.setStatus(TLDrawStatus.Idle)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue