diff --git a/packages/r3f/src/main/editable.tsx b/packages/r3f/src/main/editable.tsx index 941a3ac..bb8e9ba 100644 --- a/packages/r3f/src/main/editable.tsx +++ b/packages/r3f/src/main/editable.tsx @@ -135,7 +135,7 @@ Then you can use it in your JSX like any other editable component. Note the make (value) => value.type, ), ), - {override: true}, + {reconfigure: true}, ) return } else { @@ -212,7 +212,7 @@ Then you can use it in your JSX like any other editable component. Note the make return () => { untap() - sheetObject.sheet.deleteObject(theatreKey) + sheetObject.sheet.detachObject(theatreKey) allRegisteredObjects.delete(sheetObject) editorStore.getState().removeEditable(storeKey) } diff --git a/theatre/core/src/sheetObjects/SheetObjectTemplate.ts b/theatre/core/src/sheetObjects/SheetObjectTemplate.ts index e280494..79ebfc8 100644 --- a/theatre/core/src/sheetObjects/SheetObjectTemplate.ts +++ b/theatre/core/src/sheetObjects/SheetObjectTemplate.ts @@ -89,7 +89,7 @@ export default class SheetObjectTemplate { return new SheetObject(sheet, this, nativeObject) } - overrideConfig(config: SheetObjectPropTypeConfig) { + reconfigure(config: SheetObjectPropTypeConfig) { this._config.setState(config) } diff --git a/theatre/core/src/sheets/TheatreSheet.ts b/theatre/core/src/sheets/TheatreSheet.ts index 356f991..5b9cb64 100644 --- a/theatre/core/src/sheets/TheatreSheet.ts +++ b/theatre/core/src/sheets/TheatreSheet.ts @@ -46,7 +46,7 @@ export interface ISheet { * * @param key - Each object is identified by a key, which is a non-empty string * @param props - The props of the object. See examples - * @param options - TODO + * @param options - (Optional) Provide `{reconfigure: true}` to reconfigure an existing object. Reac the example below for details. * * @returns An Object * @@ -65,20 +65,35 @@ export interface ISheet { * // Create an object with nested props * const obj = sheet.object("obj", {position: {x: 0, y: 0}}) * obj.value.position // {x: 0, y: 0} + * + * // you can also reconfigure an existing object: + * const obj = sheet.object("obj", {foo: 0}) + * console.log(object.value.foo) // prints 0 + * + * const obj2 = sheet.object("obj", {bar: 0}, {reconfigure: true}) + * console.log(object.value.foo) // prints undefined, since we've removed this prop via reconfiguring the object + * console.log(object.value.bar) // prints 0, since we've introduced this prop by reconfiguring the object + * + * assert(obj === obj2) // passes, because reconfiguring the object returns the same object * ``` */ object( key: string, props: Props, - options?: {override?: boolean}, + options?: { + reconfigure?: boolean + }, ): ISheetObject /** - * Deletes a previously created child object for the sheet + * Detaches a previously created child object from the sheet. * - * @param key - Delete the child object created with this key + * If you call `sheet.object(key)` again with the same `key`, the object's values of the object's + * props WILL NOT be reset to their initial values. + * + * @param key - The `key` of the object previously given to `sheet.object(key, ...)`. */ - deleteObject(key: string): void + detachObject(key: string): void /** * The Sequence of this Sheet @@ -105,7 +120,7 @@ export default class TheatreSheet implements ISheet { object( key: string, config: Props, - opts?: {override?: boolean}, + opts?: {reconfigure?: boolean}, ): ISheetObject { const internal = privateAPI(this) const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( @@ -129,9 +144,9 @@ export default class TheatreSheet implements ISheet { const prevConfig = weakMapOfUnsanitizedProps.get(existingObject) if (prevConfig) { if (!deepEqual(config, prevConfig)) { - if (opts?.override === true) { + if (opts?.reconfigure === true) { const sanitizedConfig = compound(config) - existingObject.template.overrideConfig(sanitizedConfig) + existingObject.template.reconfigure(sanitizedConfig) weakMapOfUnsanitizedProps.set(existingObject, config) return existingObject.publicApi as $IntentionalAny } else { @@ -139,7 +154,7 @@ export default class TheatreSheet implements ISheet { `You seem to have called sheet.object("${key}", config) twice, with different values for \`config\`. ` + `This is disallowed because changing the config of an object on the fly would make it difficult to reason about.\n\n` + `You can fix this by either re-using the existing object, or calling sheet.object("${key}", config) with the same config.\n\n` + - `If you mean to override the object's config, set \`{override: true}\` in sheet.object("${key}", config, {override: true})`, + `If you mean to reconfigure the object's config, set \`{reconfigure: true}\` in sheet.object("${key}", config, {reconfigure: true})`, ) } } @@ -173,7 +188,7 @@ export default class TheatreSheet implements ISheet { return {...privateAPI(this).address} } - deleteObject(key: string) { + detachObject(key: string) { const internal = privateAPI(this) const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( key,