From 415ec7694267ea29672161239fed70325b01ea3e Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Sun, 22 Jan 2023 20:35:42 +0100 Subject: [PATCH] Mark the actions api as unstable --- packages/theatric/src/index.ts | 32 ++++++------------- theatre/core/src/coreExports.ts | 2 -- .../src/sheetObjects/SheetObjectTemplate.ts | 21 ++++++------ theatre/core/src/sheets/TheatreSheet.ts | 16 +++++++--- .../src/panels/DetailPanel/ObjectDetails.tsx | 2 +- 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/packages/theatric/src/index.ts b/packages/theatric/src/index.ts index e4f74ed..54b3766 100644 --- a/packages/theatric/src/index.ts +++ b/packages/theatric/src/index.ts @@ -1,14 +1,12 @@ import type { IProjectConfig, ISheetObject, - SheetObjectActionsConfig, UnknownShorthandCompoundProps, } from '@theatre/core' import {val} from '@theatre/core' import {getProject} from '@theatre/core' import type {Pointer} from '@theatre/dataverse' import {isPointer} from '@theatre/dataverse' -import type {IStudio} from '@theatre/studio' import studio from '@theatre/studio' import isEqual from 'lodash-es/isEqual' import {useEffect, useMemo, useState, useRef} from 'react' @@ -46,7 +44,7 @@ export function initialize(state: IProjectConfig['state']) { } const allProps: Record = {} -const allActions: Record = {} +const allActions: Record void>[]> = {} type Button = { type: 'button' @@ -143,23 +141,8 @@ export function useControls( Object.fromEntries( Object.entries(buttons).map(([key, value]) => [ `${folder ? `${folder}: ` : ''}${key}`, - (object: ISheetObject, studio: IStudio) => { - value - .onClick - // (path, value) => { - // // this is not ideal because it will create a separate undo level for each set call, - // // but this is the only thing that theatre's public API allows us to do. - // // Wrapping the whole thing in a transaction wouldn't work either because side effects - // // would be run twice. - // maybeTransaction((api) => { - // api.set( - // get(folder ? object.props[folder] : object.props, path), - // value, - // ) - // }) - // }, - // (path) => get(folder ? object.value[folder] : object.value, path), - () + () => { + value.onClick() }, ]), ), @@ -179,7 +162,8 @@ export function useControls( () => sheet.object(panel, Object.assign({}, ...allProps[panel], props), { reconfigure: true, - actions: Object.assign({}, ...allActions[panel], actions), + __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION: + Object.assign({}, ...allActions[panel], actions), }), [panel, props, actions], ) @@ -191,7 +175,8 @@ export function useControls( // the very first values returned are not undefined sheet.object(panel, Object.assign({}, ...allPanelProps), { reconfigure: true, - actions: Object.assign({}, ...allPanelActions), + __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION: + Object.assign({}, ...allPanelActions), }) return () => { @@ -199,7 +184,8 @@ export function useControls( allActions[panel].splice(allPanelActions.indexOf(actions), 1) sheet.object(panel, Object.assign({}, ...allPanelProps), { reconfigure: true, - actions: Object.assign({}, ...allPanelActions), + __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION: + Object.assign({}, ...allPanelActions), }) } }, [props, actions, allPanelActions, allPanelProps, sheet, panel]) diff --git a/theatre/core/src/coreExports.ts b/theatre/core/src/coreExports.ts index 344ccde..f5804fc 100644 --- a/theatre/core/src/coreExports.ts +++ b/theatre/core/src/coreExports.ts @@ -206,5 +206,3 @@ export function val(pointer: PointerType): T { throw new Error(`Called val(p) where p is not a pointer.`) } } - -export type {SheetObjectActionsConfig} from './sheets/TheatreSheet' diff --git a/theatre/core/src/sheetObjects/SheetObjectTemplate.ts b/theatre/core/src/sheetObjects/SheetObjectTemplate.ts index f19e6cf..afaf263 100644 --- a/theatre/core/src/sheetObjects/SheetObjectTemplate.ts +++ b/theatre/core/src/sheetObjects/SheetObjectTemplate.ts @@ -61,7 +61,7 @@ export default class SheetObjectTemplate { readonly address: WithoutSheetInstance readonly type: 'Theatre_SheetObjectTemplate' = 'Theatre_SheetObjectTemplate' protected _config: Atom - readonly _actions: Atom + readonly _temp_actions_atom: Atom readonly _cache = new SimpleCache() readonly project: Project @@ -73,12 +73,12 @@ export default class SheetObjectTemplate { return this._config.pointer } - get actions() { - return this._actions.get() + get _temp_actions() { + return this._temp_actions_atom.get() } - get actionsPointer() { - return this._actions.pointer + get _temp_actionsPointer() { + return this._temp_actions_atom.pointer } constructor( @@ -86,11 +86,11 @@ export default class SheetObjectTemplate { objectKey: ObjectAddressKey, nativeObject: unknown, config: SheetObjectPropTypeConfig, - actions: SheetObjectActionsConfig, + _temp_actions: SheetObjectActionsConfig, ) { this.address = {...sheetTemplate.address, objectKey} this._config = new Atom(config) - this._actions = new Atom(actions) + this._temp_actions_atom = new Atom(_temp_actions) this.project = sheetTemplate.project } @@ -107,8 +107,11 @@ export default class SheetObjectTemplate { this._config.set(config) } - setActions(actions: SheetObjectActionsConfig) { - this._actions.set(actions) + /** + * The `actions` api is temporary until we implement events. + */ + _temp_setActions(actions: SheetObjectActionsConfig) { + this._temp_actions_atom.set(actions) } /** diff --git a/theatre/core/src/sheets/TheatreSheet.ts b/theatre/core/src/sheets/TheatreSheet.ts index 631e683..66cb614 100644 --- a/theatre/core/src/sheets/TheatreSheet.ts +++ b/theatre/core/src/sheets/TheatreSheet.ts @@ -99,7 +99,7 @@ export interface ISheet { props: Props, options?: { reconfigure?: boolean - actions?: SheetObjectActionsConfig + __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION?: SheetObjectActionsConfig }, ): ISheetObject @@ -138,7 +138,10 @@ export default class TheatreSheet implements ISheet { object( key: string, config: Props, - opts?: {reconfigure?: boolean; actions?: SheetObjectActionsConfig}, + opts?: { + reconfigure?: boolean + __actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION?: SheetObjectActionsConfig + }, ): ISheetObject { const internal = privateAPI(this) const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( @@ -157,6 +160,9 @@ export default class TheatreSheet implements ISheet { */ const nativeObject = null + const actions = + opts?.__actions__THIS_API_IS_UNSTABLE_AND_WILL_CHANGE_IN_THE_NEXT_VERSION + if (existingObject) { if (process.env.NODE_ENV !== 'production') { const prevConfig = weakMapOfUnsanitizedProps.get(existingObject) @@ -179,8 +185,8 @@ export default class TheatreSheet implements ISheet { } } - if (opts?.actions) { - existingObject.template.setActions(opts.actions) + if (actions) { + existingObject.template._temp_setActions(actions) } return existingObject.publicApi as $IntentionalAny @@ -190,7 +196,7 @@ export default class TheatreSheet implements ISheet { sanitizedPath as ObjectAddressKey, nativeObject, sanitizedConfig, - opts?.actions, + actions, ) if (process.env.NODE_ENV !== 'production') { weakMapOfUnsanitizedProps.set(object as $FixMe, config) diff --git a/theatre/studio/src/panels/DetailPanel/ObjectDetails.tsx b/theatre/studio/src/panels/DetailPanel/ObjectDetails.tsx index 5f22b66..00f49b8 100644 --- a/theatre/studio/src/panels/DetailPanel/ObjectDetails.tsx +++ b/theatre/studio/src/panels/DetailPanel/ObjectDetails.tsx @@ -43,7 +43,7 @@ const ObjectDetails: React.FC<{ }> = ({objects}) => { const obj = objects[0] const config = useVal(obj.template.configPointer) - const actions = useVal(obj.template.actionsPointer) + const actions = useVal(obj.template._temp_actionsPointer) return ( <>