mini defineShape
API (#1563)
Based on #1549, but with a lot of code-structure related changes backed out. Shape schemas are still defined in tlschemas with this diff. Couple differences between this and #1549: - This tightens up the relationship between store schemas and editor schemas a bit - Reduces the number of places we need to remember to include core shapes - Only `<TLdrawEditor />` sets default shapes by default. If you're doing something funky with lower-level APIs, you need to specify `defaultShapes` manually - Replaces `validator` with `props` for shapes ### Change Type - [x] `major` — Breaking Change ### Test Plan 1. Add a step-by-step description of how to test your PR here. 2. - [x] Unit Tests - [ ] Webdriver tests ### Release Notes [dev-facing, notes to come]
This commit is contained in:
parent
4b680d9451
commit
1927f88041
72 changed files with 1081 additions and 673 deletions
|
@ -1,20 +1,13 @@
|
|||
import { HistoryEntry, Migrations, Store, StoreSnapshot } from '@tldraw/store'
|
||||
import { TLRecord, TLStore, createTLSchema } from '@tldraw/tlschema'
|
||||
import { TLShapeUtilConstructor } from '../editor/shapeutils/ShapeUtil'
|
||||
|
||||
/** @public */
|
||||
export type TLShapeInfo = {
|
||||
util: TLShapeUtilConstructor<any>
|
||||
migrations?: Migrations
|
||||
validator?: { validate: (record: any) => any }
|
||||
}
|
||||
import { HistoryEntry, Store, StoreSchema, StoreSnapshot } from '@tldraw/store'
|
||||
import { TLRecord, TLStore, TLStoreProps, createTLSchema } from '@tldraw/tlschema'
|
||||
import { checkShapesAndAddCore } from './defaultShapes'
|
||||
import { AnyTLShapeInfo, TLShapeInfo } from './defineShape'
|
||||
|
||||
/** @public */
|
||||
export type TLStoreOptions = {
|
||||
customShapes?: Record<string, TLShapeInfo>
|
||||
initialData?: StoreSnapshot<TLRecord>
|
||||
defaultName?: string
|
||||
}
|
||||
} & ({ shapes: readonly AnyTLShapeInfo[] } | { schema: StoreSchema<TLRecord, TLStoreProps> })
|
||||
|
||||
/** @public */
|
||||
export type TLStoreEventInfo = HistoryEntry<TLRecord>
|
||||
|
@ -25,14 +18,20 @@ export type TLStoreEventInfo = HistoryEntry<TLRecord>
|
|||
* @param opts - Options for creating the store.
|
||||
*
|
||||
* @public */
|
||||
export function createTLStore(opts = {} as TLStoreOptions): TLStore {
|
||||
const { customShapes = {}, initialData, defaultName = '' } = opts
|
||||
|
||||
export function createTLStore({ initialData, defaultName = '', ...rest }: TLStoreOptions): TLStore {
|
||||
const schema =
|
||||
'schema' in rest
|
||||
? rest.schema
|
||||
: createTLSchema({ shapes: shapesArrayToShapeMap(checkShapesAndAddCore(rest.shapes)) })
|
||||
return new Store({
|
||||
schema: createTLSchema({ customShapes }),
|
||||
schema,
|
||||
initialData,
|
||||
props: {
|
||||
defaultName,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function shapesArrayToShapeMap(shapes: TLShapeInfo[]) {
|
||||
return Object.fromEntries(shapes.map((s) => [s.type, s]))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue