From 7578fff2b1799e568082a62e72d33a32c11b9f3a Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Sat, 27 May 2023 18:53:18 +0100 Subject: [PATCH] [tiny] add isPageId (#1482) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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` --- packages/editor/src/lib/app/App.ts | 27 ++++++++++--------- .../app/derivations/shapeIdsInCurrentPage.ts | 4 +-- .../TLSelectTool/children/Translating.ts | 4 +-- packages/tlschema/api-report.md | 3 +++ packages/tlschema/src/index.ts | 8 +++++- packages/tlschema/src/records/TLPage.ts | 5 ++++ 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/packages/editor/src/lib/app/App.ts b/packages/editor/src/lib/app/App.ts index 60ed49aa9..f394d71e9 100644 --- a/packages/editor/src/lib/app/App.ts +++ b/packages/editor/src/lib/app/App.ts @@ -60,6 +60,7 @@ import { Vec2dModel, createCustomShapeId, createShapeId, + isPageId, isShape, isShapeId, } from '@tldraw/tlschema' @@ -651,7 +652,7 @@ export class App extends EventEmitter { */ @computed private get _pageTransformCache(): ComputedCache { return this.store.createComputedCache('pageTransformCache', (shape) => { - if (PageRecordType.isId(shape.parentId)) { + if (isPageId(shape.parentId)) { return this.getTransform(shape) } // some weird circular type thing here that I had to work wround with (as any) @@ -687,7 +688,7 @@ export class App extends EventEmitter { */ @computed private get _pageMaskCache(): ComputedCache { return this.store.createComputedCache('pageMaskCache', (shape) => { - if (PageRecordType.isId(shape.parentId)) { + if (isPageId(shape.parentId)) { return undefined } @@ -1415,7 +1416,7 @@ export class App extends EventEmitter { } // 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]) this.visitDescendants(prev.id, (id) => { allMovingIds.add(id) @@ -1792,7 +1793,7 @@ export class App extends EventEmitter { * @public */ getParentTransform(shape: TLShape) { - if (PageRecordType.isId(shape.parentId)) { + if (isPageId(shape.parentId)) { return Matrix2d.Identity() } return this._pageTransformCache.get(shape.parentId) ?? Matrix2d.Identity() @@ -2070,7 +2071,7 @@ export class App extends EventEmitter { */ getAncestors(shape: TLShape, acc: TLShape[] = []): TLShape[] { const parentId = shape.parentId - if (PageRecordType.isId(parentId)) { + if (isPageId(parentId)) { acc.reverse() return acc } @@ -2112,7 +2113,7 @@ export class App extends EventEmitter { findAncestor(shape: TLShape, predicate: (parent: TLShape) => boolean): TLShape | undefined { const parentId = shape.parentId - if (PageRecordType.isId(parentId)) { + if (isPageId(parentId)) { return undefined } @@ -2150,7 +2151,7 @@ export class App extends EventEmitter { } if (shapes.length === 1) { const parentId = shapes[0].parentId - if (PageRecordType.isId(parentId)) { + if (isPageId(parentId)) { return } return predicate ? this.findAncestor(shapes[0], predicate)?.id : parentId @@ -2400,7 +2401,7 @@ export class App extends EventEmitter { if (!shape) { 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) if (!parentTransform) return Vec2d.From(point) @@ -2441,7 +2442,7 @@ export class App extends EventEmitter { * @public */ 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) if (!parent) return Vec2d.From(delta) @@ -3157,7 +3158,7 @@ export class App extends EventEmitter { /** Get the id of the containing page for a given shape. */ getParentPageId(shape?: TLShape): TLPageId | undefined { if (shape === undefined) return undefined - if (PageRecordType.isId(shape.parentId)) { + if (isPageId(shape.parentId)) { return shape.parentId } else { return this.getParentPageId(this.getShapeById(shape.parentId)) @@ -4220,7 +4221,7 @@ export class App extends EventEmitter { let isDuplicating = false - if (!PageRecordType.isId(pasteParentId)) { + if (!isPageId(pasteParentId)) { const parent = this.getShapeById(pasteParentId) if (parent) { if (!this.viewportPageBounds.includes(this.getPageBounds(parent)!)) { @@ -4416,7 +4417,7 @@ export class App extends EventEmitter { const bounds = Box2d.Common(newCreatedShapes.map((s) => this.getPageBounds(s)!)) if (point === undefined) { - if (!PageRecordType.isId(pasteParentId)) { + if (!isPageId(pasteParentId)) { // Put the shapes in the middle of the (on screen) parent const shape = this.getShapeById(pasteParentId)! const util = this.getShapeUtil(shape) @@ -6974,7 +6975,7 @@ export class App extends EventEmitter { reparentShapesById(ids: TLShapeId[], parentId: TLParentId, insertIndex?: string) { const changes: TLShapePartial[] = [] - const parentTransform = PageRecordType.isId(parentId) + const parentTransform = isPageId(parentId) ? Matrix2d.Identity() : this.getPageTransformById(parentId)! diff --git a/packages/editor/src/lib/app/derivations/shapeIdsInCurrentPage.ts b/packages/editor/src/lib/app/derivations/shapeIdsInCurrentPage.ts index e66edb5f6..a71e74cb7 100644 --- a/packages/editor/src/lib/app/derivations/shapeIdsInCurrentPage.ts +++ b/packages/editor/src/lib/app/derivations/shapeIdsInCurrentPage.ts @@ -1,7 +1,7 @@ import { + isPageId, isShape, isShapeId, - PageRecordType, TLPageId, TLShape, TLShapeId, @@ -18,7 +18,7 @@ import { computed, isUninitialized, RESET_VALUE, withDiff } from 'signia' * @param shape - The the shape to check. */ const isShapeInPage = (store: TLStore, pageId: TLPageId, shape: TLShape): boolean => { - while (!PageRecordType.isId(shape.parentId)) { + while (!isPageId(shape.parentId)) { const parent = store.get(shape.parentId) if (!parent) return false shape = parent diff --git a/packages/editor/src/lib/app/statechart/TLSelectTool/children/Translating.ts b/packages/editor/src/lib/app/statechart/TLSelectTool/children/Translating.ts index 4c5a4f392..cba9da201 100644 --- a/packages/editor/src/lib/app/statechart/TLSelectTool/children/Translating.ts +++ b/packages/editor/src/lib/app/statechart/TLSelectTool/children/Translating.ts @@ -1,5 +1,5 @@ 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 type { App } from '../../../App' import { DragAndDropManager } from '../../../managers/DragAndDropManager' @@ -257,7 +257,7 @@ export class Translating extends StateNode { if (!shape) return null movingShapes.push(shape) - const parentTransform = PageRecordType.isId(shape.parentId) + const parentTransform = isPageId(shape.parentId) ? null : Matrix2d.Inverse(app.getPageTransformById(shape.parentId)!) diff --git a/packages/tlschema/api-report.md b/packages/tlschema/api-report.md index c85f66ea0..712d5a80b 100644 --- a/packages/tlschema/api-report.md +++ b/packages/tlschema/api-report.md @@ -423,6 +423,9 @@ export const instanceTypeMigrations: Migrations; // @public (undocumented) export const instanceTypeValidator: T.Validator; +// @public (undocumented) +export function isPageId(id: string): id is TLPageId; + // @public (undocumented) export function isShape(record?: UnknownRecord): record is TLShape; diff --git a/packages/tlschema/src/index.ts b/packages/tlschema/src/index.ts index 7c1f1c3ff..56281aeca 100644 --- a/packages/tlschema/src/index.ts +++ b/packages/tlschema/src/index.ts @@ -64,7 +64,13 @@ export { type TLInstancePageStateId, } from './records/TLInstancePageState' 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 { PointerRecordType, TLPOINTER_ID, diff --git a/packages/tlschema/src/records/TLPage.ts b/packages/tlschema/src/records/TLPage.ts index b3b68ea83..26fb1ab23 100644 --- a/packages/tlschema/src/records/TLPage.ts +++ b/packages/tlschema/src/records/TLPage.ts @@ -34,3 +34,8 @@ export const PageRecordType = createRecordType('page', { /** @public */ export const pageTypeMigrations = defineMigrations({}) + +/** @public */ +export function isPageId(id: string): id is TLPageId { + return PageRecordType.isId(id) +}