[tiny] add isPageId (#1482)
This PR adds an `isPageId` helper. ### Change Type - [x] `internal` — Any other changes that don't affect the published package (will not publish a new version) ### Release Notes - [tlschema] Add `isPageId`
This commit is contained in:
parent
0bc397c946
commit
7578fff2b1
6 changed files with 33 additions and 18 deletions
|
@ -60,6 +60,7 @@ import {
|
||||||
Vec2dModel,
|
Vec2dModel,
|
||||||
createCustomShapeId,
|
createCustomShapeId,
|
||||||
createShapeId,
|
createShapeId,
|
||||||
|
isPageId,
|
||||||
isShape,
|
isShape,
|
||||||
isShapeId,
|
isShapeId,
|
||||||
} from '@tldraw/tlschema'
|
} from '@tldraw/tlschema'
|
||||||
|
@ -651,7 +652,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
*/
|
*/
|
||||||
@computed private get _pageTransformCache(): ComputedCache<Matrix2d, TLShape> {
|
@computed private get _pageTransformCache(): ComputedCache<Matrix2d, TLShape> {
|
||||||
return this.store.createComputedCache<Matrix2d, TLShape>('pageTransformCache', (shape) => {
|
return this.store.createComputedCache<Matrix2d, TLShape>('pageTransformCache', (shape) => {
|
||||||
if (PageRecordType.isId(shape.parentId)) {
|
if (isPageId(shape.parentId)) {
|
||||||
return this.getTransform(shape)
|
return this.getTransform(shape)
|
||||||
}
|
}
|
||||||
// some weird circular type thing here that I had to work wround with (as any)
|
// some weird circular type thing here that I had to work wround with (as any)
|
||||||
|
@ -687,7 +688,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
*/
|
*/
|
||||||
@computed private get _pageMaskCache(): ComputedCache<VecLike[], TLShape> {
|
@computed private get _pageMaskCache(): ComputedCache<VecLike[], TLShape> {
|
||||||
return this.store.createComputedCache<VecLike[], TLShape>('pageMaskCache', (shape) => {
|
return this.store.createComputedCache<VecLike[], TLShape>('pageMaskCache', (shape) => {
|
||||||
if (PageRecordType.isId(shape.parentId)) {
|
if (isPageId(shape.parentId)) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1415,7 +1416,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// if this shape moved to a new page, clean up any previous page's instance state
|
// if this shape moved to a new page, clean up any previous page's instance state
|
||||||
if (prev.parentId !== next.parentId && PageRecordType.isId(next.parentId)) {
|
if (prev.parentId !== next.parentId && isPageId(next.parentId)) {
|
||||||
const allMovingIds = new Set([prev.id])
|
const allMovingIds = new Set([prev.id])
|
||||||
this.visitDescendants(prev.id, (id) => {
|
this.visitDescendants(prev.id, (id) => {
|
||||||
allMovingIds.add(id)
|
allMovingIds.add(id)
|
||||||
|
@ -1792,7 +1793,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getParentTransform(shape: TLShape) {
|
getParentTransform(shape: TLShape) {
|
||||||
if (PageRecordType.isId(shape.parentId)) {
|
if (isPageId(shape.parentId)) {
|
||||||
return Matrix2d.Identity()
|
return Matrix2d.Identity()
|
||||||
}
|
}
|
||||||
return this._pageTransformCache.get(shape.parentId) ?? Matrix2d.Identity()
|
return this._pageTransformCache.get(shape.parentId) ?? Matrix2d.Identity()
|
||||||
|
@ -2070,7 +2071,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
*/
|
*/
|
||||||
getAncestors(shape: TLShape, acc: TLShape[] = []): TLShape[] {
|
getAncestors(shape: TLShape, acc: TLShape[] = []): TLShape[] {
|
||||||
const parentId = shape.parentId
|
const parentId = shape.parentId
|
||||||
if (PageRecordType.isId(parentId)) {
|
if (isPageId(parentId)) {
|
||||||
acc.reverse()
|
acc.reverse()
|
||||||
return acc
|
return acc
|
||||||
}
|
}
|
||||||
|
@ -2112,7 +2113,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
findAncestor(shape: TLShape, predicate: (parent: TLShape) => boolean): TLShape | undefined {
|
findAncestor(shape: TLShape, predicate: (parent: TLShape) => boolean): TLShape | undefined {
|
||||||
const parentId = shape.parentId
|
const parentId = shape.parentId
|
||||||
|
|
||||||
if (PageRecordType.isId(parentId)) {
|
if (isPageId(parentId)) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2150,7 +2151,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
}
|
}
|
||||||
if (shapes.length === 1) {
|
if (shapes.length === 1) {
|
||||||
const parentId = shapes[0].parentId
|
const parentId = shapes[0].parentId
|
||||||
if (PageRecordType.isId(parentId)) {
|
if (isPageId(parentId)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return predicate ? this.findAncestor(shapes[0], predicate)?.id : parentId
|
return predicate ? this.findAncestor(shapes[0], predicate)?.id : parentId
|
||||||
|
@ -2400,7 +2401,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
if (!shape) {
|
if (!shape) {
|
||||||
return new Vec2d(0, 0)
|
return new Vec2d(0, 0)
|
||||||
}
|
}
|
||||||
if (PageRecordType.isId(shape.parentId)) return Vec2d.From(point)
|
if (isPageId(shape.parentId)) return Vec2d.From(point)
|
||||||
|
|
||||||
const parentTransform = this.getPageTransformById(shape.parentId)
|
const parentTransform = this.getPageTransformById(shape.parentId)
|
||||||
if (!parentTransform) return Vec2d.From(point)
|
if (!parentTransform) return Vec2d.From(point)
|
||||||
|
@ -2441,7 +2442,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getDeltaInParentSpace(shape: TLShape, delta: VecLike): Vec2d {
|
getDeltaInParentSpace(shape: TLShape, delta: VecLike): Vec2d {
|
||||||
if (PageRecordType.isId(shape.parentId)) return Vec2d.From(delta)
|
if (isPageId(shape.parentId)) return Vec2d.From(delta)
|
||||||
|
|
||||||
const parent = this.getShapeById(shape.parentId)
|
const parent = this.getShapeById(shape.parentId)
|
||||||
if (!parent) return Vec2d.From(delta)
|
if (!parent) return Vec2d.From(delta)
|
||||||
|
@ -3157,7 +3158,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
/** Get the id of the containing page for a given shape. */
|
/** Get the id of the containing page for a given shape. */
|
||||||
getParentPageId(shape?: TLShape): TLPageId | undefined {
|
getParentPageId(shape?: TLShape): TLPageId | undefined {
|
||||||
if (shape === undefined) return undefined
|
if (shape === undefined) return undefined
|
||||||
if (PageRecordType.isId(shape.parentId)) {
|
if (isPageId(shape.parentId)) {
|
||||||
return shape.parentId
|
return shape.parentId
|
||||||
} else {
|
} else {
|
||||||
return this.getParentPageId(this.getShapeById(shape.parentId))
|
return this.getParentPageId(this.getShapeById(shape.parentId))
|
||||||
|
@ -4220,7 +4221,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
|
|
||||||
let isDuplicating = false
|
let isDuplicating = false
|
||||||
|
|
||||||
if (!PageRecordType.isId(pasteParentId)) {
|
if (!isPageId(pasteParentId)) {
|
||||||
const parent = this.getShapeById(pasteParentId)
|
const parent = this.getShapeById(pasteParentId)
|
||||||
if (parent) {
|
if (parent) {
|
||||||
if (!this.viewportPageBounds.includes(this.getPageBounds(parent)!)) {
|
if (!this.viewportPageBounds.includes(this.getPageBounds(parent)!)) {
|
||||||
|
@ -4416,7 +4417,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
const bounds = Box2d.Common(newCreatedShapes.map((s) => this.getPageBounds(s)!))
|
const bounds = Box2d.Common(newCreatedShapes.map((s) => this.getPageBounds(s)!))
|
||||||
|
|
||||||
if (point === undefined) {
|
if (point === undefined) {
|
||||||
if (!PageRecordType.isId(pasteParentId)) {
|
if (!isPageId(pasteParentId)) {
|
||||||
// Put the shapes in the middle of the (on screen) parent
|
// Put the shapes in the middle of the (on screen) parent
|
||||||
const shape = this.getShapeById(pasteParentId)!
|
const shape = this.getShapeById(pasteParentId)!
|
||||||
const util = this.getShapeUtil(shape)
|
const util = this.getShapeUtil(shape)
|
||||||
|
@ -6974,7 +6975,7 @@ export class App extends EventEmitter<TLEventMap> {
|
||||||
reparentShapesById(ids: TLShapeId[], parentId: TLParentId, insertIndex?: string) {
|
reparentShapesById(ids: TLShapeId[], parentId: TLParentId, insertIndex?: string) {
|
||||||
const changes: TLShapePartial[] = []
|
const changes: TLShapePartial[] = []
|
||||||
|
|
||||||
const parentTransform = PageRecordType.isId(parentId)
|
const parentTransform = isPageId(parentId)
|
||||||
? Matrix2d.Identity()
|
? Matrix2d.Identity()
|
||||||
: this.getPageTransformById(parentId)!
|
: this.getPageTransformById(parentId)!
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {
|
import {
|
||||||
|
isPageId,
|
||||||
isShape,
|
isShape,
|
||||||
isShapeId,
|
isShapeId,
|
||||||
PageRecordType,
|
|
||||||
TLPageId,
|
TLPageId,
|
||||||
TLShape,
|
TLShape,
|
||||||
TLShapeId,
|
TLShapeId,
|
||||||
|
@ -18,7 +18,7 @@ import { computed, isUninitialized, RESET_VALUE, withDiff } from 'signia'
|
||||||
* @param shape - The the shape to check.
|
* @param shape - The the shape to check.
|
||||||
*/
|
*/
|
||||||
const isShapeInPage = (store: TLStore, pageId: TLPageId, shape: TLShape): boolean => {
|
const isShapeInPage = (store: TLStore, pageId: TLPageId, shape: TLShape): boolean => {
|
||||||
while (!PageRecordType.isId(shape.parentId)) {
|
while (!isPageId(shape.parentId)) {
|
||||||
const parent = store.get(shape.parentId)
|
const parent = store.get(shape.parentId)
|
||||||
if (!parent) return false
|
if (!parent) return false
|
||||||
shape = parent
|
shape = parent
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Box2d, Matrix2d, Matrix2dModel, Vec2d } from '@tldraw/primitives'
|
import { Box2d, Matrix2d, Matrix2dModel, Vec2d } from '@tldraw/primitives'
|
||||||
import { PageRecordType, TLShape, TLShapePartial } from '@tldraw/tlschema'
|
import { PageRecordType, TLShape, TLShapePartial, isPageId } from '@tldraw/tlschema'
|
||||||
import { compact } from '@tldraw/utils'
|
import { compact } from '@tldraw/utils'
|
||||||
import type { App } from '../../../App'
|
import type { App } from '../../../App'
|
||||||
import { DragAndDropManager } from '../../../managers/DragAndDropManager'
|
import { DragAndDropManager } from '../../../managers/DragAndDropManager'
|
||||||
|
@ -257,7 +257,7 @@ export class Translating extends StateNode {
|
||||||
if (!shape) return null
|
if (!shape) return null
|
||||||
movingShapes.push(shape)
|
movingShapes.push(shape)
|
||||||
|
|
||||||
const parentTransform = PageRecordType.isId(shape.parentId)
|
const parentTransform = isPageId(shape.parentId)
|
||||||
? null
|
? null
|
||||||
: Matrix2d.Inverse(app.getPageTransformById(shape.parentId)!)
|
: Matrix2d.Inverse(app.getPageTransformById(shape.parentId)!)
|
||||||
|
|
||||||
|
|
|
@ -423,6 +423,9 @@ export const instanceTypeMigrations: Migrations;
|
||||||
// @public (undocumented)
|
// @public (undocumented)
|
||||||
export const instanceTypeValidator: T.Validator<TLInstance>;
|
export const instanceTypeValidator: T.Validator<TLInstance>;
|
||||||
|
|
||||||
|
// @public (undocumented)
|
||||||
|
export function isPageId(id: string): id is TLPageId;
|
||||||
|
|
||||||
// @public (undocumented)
|
// @public (undocumented)
|
||||||
export function isShape(record?: UnknownRecord): record is TLShape;
|
export function isShape(record?: UnknownRecord): record is TLShape;
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,13 @@ export {
|
||||||
type TLInstancePageStateId,
|
type TLInstancePageStateId,
|
||||||
} from './records/TLInstancePageState'
|
} from './records/TLInstancePageState'
|
||||||
export { InstancePresenceRecordType, type TLInstancePresence } from './records/TLInstancePresence'
|
export { InstancePresenceRecordType, type TLInstancePresence } from './records/TLInstancePresence'
|
||||||
export { PageRecordType, pageTypeValidator, type TLPage, type TLPageId } from './records/TLPage'
|
export {
|
||||||
|
PageRecordType,
|
||||||
|
isPageId,
|
||||||
|
pageTypeValidator,
|
||||||
|
type TLPage,
|
||||||
|
type TLPageId,
|
||||||
|
} from './records/TLPage'
|
||||||
export {
|
export {
|
||||||
PointerRecordType,
|
PointerRecordType,
|
||||||
TLPOINTER_ID,
|
TLPOINTER_ID,
|
||||||
|
|
|
@ -34,3 +34,8 @@ export const PageRecordType = createRecordType<TLPage>('page', {
|
||||||
|
|
||||||
/** @public */
|
/** @public */
|
||||||
export const pageTypeMigrations = defineMigrations({})
|
export const pageTypeMigrations = defineMigrations({})
|
||||||
|
|
||||||
|
/** @public */
|
||||||
|
export function isPageId(id: string): id is TLPageId {
|
||||||
|
return PageRecordType.isId(id)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue