API naming tweaks

This commit is contained in:
Aria Minaei 2021-09-01 10:36:44 +02:00
parent 36017c6a73
commit a8df97a06a
21 changed files with 86 additions and 74 deletions

View file

@ -6,6 +6,8 @@ import {types as t} from '@theatre/core'
import type {UseDragOpts} from './useDrag' import type {UseDragOpts} from './useDrag'
import useDrag from './useDrag' import useDrag from './useDrag'
studio.initialize()
const boxObjectConfig = t.compound({ const boxObjectConfig = t.compound({
x: t.number(0), x: t.number(0),
y: t.number(0), y: t.number(0),

View file

@ -4,7 +4,7 @@ import studio from '@theatre/studio'
import {getProject} from '@theatre/core' import {getProject} from '@theatre/core'
import {Scene} from './Scene' import {Scene} from './Scene'
studio.ui studio.initialize()
ReactDOM.render( ReactDOM.render(
<Scene project={getProject('Sample project')} />, <Scene project={getProject('Sample project')} />,

View file

@ -3,6 +3,8 @@ import {useLayoutEffect, useMemo, useState} from 'react'
import {types as t} from '@theatre/core' import {types as t} from '@theatre/core'
import useDrag from './useDrag' import useDrag from './useDrag'
studio.initialize()
const boxObjectConfig = t.compound({ const boxObjectConfig = t.compound({
x: t.number(0), x: t.number(0),
y: t.number(0), y: t.number(0),

View file

@ -1,9 +1,11 @@
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import './index.css' import './index.css'
import reportWebVitals from './reportWebVitals' import reportWebVitals from './reportWebVitals'
import '@theatre/studio' import studio from '@theatre/studio'
import {getProject} from '@theatre/core' import {getProject} from '@theatre/core'
studio.initialize()
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
<App project={getProject('CRA project')} /> <App project={getProject('CRA project')} />

View file

@ -6,6 +6,8 @@ import type {IProject, ISheet, ISheetObject} from '@theatre/core'
import {types as t} from '@theatre/core' import {types as t} from '@theatre/core'
import type {IScrub, IStudio} from '@theatre/studio' import type {IScrub, IStudio} from '@theatre/studio'
studio.initialize()
const boxObjectConfig = t.compound({ const boxObjectConfig = t.compound({
x: t.number(0), x: t.number(0),
y: t.number(0), y: t.number(0),
@ -44,7 +46,7 @@ const Box: React.FC<{
}, },
onDrag(x, y) { onDrag(x, y) {
if (!firstOnDragCalled) { if (!firstOnDragCalled) {
studio.__experimental_setSelection([obj]) studio.setSelection([obj])
firstOnDragCalled = true firstOnDragCalled = true
} }
scrub!.capture(({set}) => { scrub!.capture(({set}) => {
@ -67,7 +69,7 @@ const Box: React.FC<{
return ( return (
<div <div
onClick={() => { onClick={() => {
studio.__experimental_setSelection([obj]) studio.setSelection([obj])
}} }}
ref={setDivRef} ref={setDivRef}
style={{ style={{
@ -94,7 +96,7 @@ export const Scene: React.FC<{project: IProject}> = ({project}) => {
const [selection, setSelection] = useState<IStudio['selection']>() const [selection, setSelection] = useState<IStudio['selection']>()
useLayoutEffect(() => { useLayoutEffect(() => {
return studio.__experimental_onSelectionChange((newState) => { return studio.onSelectionChange((newState) => {
setSelection(newState) setSelection(newState)
}) })
}) })

View file

@ -4,7 +4,7 @@ import studio from '@theatre/studio'
import {getProject} from '@theatre/core' import {getProject} from '@theatre/core'
import {Scene} from './Scene' import {Scene} from './Scene'
studio.ui studio.initialize()
ReactDOM.render( ReactDOM.render(
<Scene project={getProject('Sample project')} />, <Scene project={getProject('Sample project')} />,

View file

@ -6,6 +6,8 @@ import type {IProject, ISheet, ISheetObject} from '@theatre/core'
import {types as t} from '@theatre/core' import {types as t} from '@theatre/core'
import type {IScrub, IStudio} from '@theatre/studio' import type {IScrub, IStudio} from '@theatre/studio'
studio.initialize()
const boxObjectConfig = t.compound({ const boxObjectConfig = t.compound({
position: t.compound({ position: t.compound({
x: t.number(0), x: t.number(0),
@ -57,7 +59,7 @@ const Box: React.FC<{
}, },
onDrag(x, y) { onDrag(x, y) {
if (!firstOnDragCalled) { if (!firstOnDragCalled) {
studio.__experimental_setSelection([obj]) studio.setSelection([obj])
firstOnDragCalled = true firstOnDragCalled = true
} }
scrub!.capture(({set}) => { scrub!.capture(({set}) => {
@ -84,7 +86,7 @@ const Box: React.FC<{
return ( return (
<div <div
onClick={() => { onClick={() => {
studio.__experimental_setSelection([obj]) studio.setSelection([obj])
}} }}
ref={setDivRef} ref={setDivRef}
style={{ style={{
@ -111,7 +113,7 @@ export const Scene: React.FC<{project: IProject}> = ({project}) => {
const [selection, setSelection] = useState<IStudio['selection']>() const [selection, setSelection] = useState<IStudio['selection']>()
useLayoutEffect(() => { useLayoutEffect(() => {
return studio.__experimental_onSelectionChange((newState) => { return studio.onSelectionChange((newState) => {
setSelection(newState) setSelection(newState)
}) })
}) })

View file

@ -5,7 +5,7 @@ import {getProject} from '@theatre/core'
import {Scene} from './Scene' import {Scene} from './Scene'
import bg from '../../xeno/bgs/8.png' import bg from '../../xeno/bgs/8.png'
studio.ui studio.initialize()
document.body.style.cssText = ` document.body.style.cssText = `
background-image: url(${bg}); background-image: url(${bg});

View file

@ -5,6 +5,9 @@ import React, {Suspense} from 'react'
import {Canvas} from '@react-three/fiber' import {Canvas} from '@react-three/fiber'
import {useGLTF} from '@react-three/drei' import {useGLTF} from '@react-three/drei'
import sceneGLB from './scene.glb' import sceneGLB from './scene.glb'
import studio from '@theatre/studio'
studio.initialize()
document.body.style.backgroundColor = '#171717' document.body.style.backgroundColor = '#171717'

View file

@ -13,6 +13,8 @@ import {types} from '@theatre/core'
import type {ITurtle} from './turtle' import type {ITurtle} from './turtle'
import {drawTurtlePlan, makeTurtlePlan} from './turtle' import {drawTurtlePlan, makeTurtlePlan} from './turtle'
studio.initialize()
const objConfig = types.compound({ const objConfig = types.compound({
startingPoint: types.compound({ startingPoint: types.compound({
x: types.number(0.5, {range: [0, 1]}), x: types.number(0.5, {range: [0, 1]}),

View file

@ -172,7 +172,7 @@ const EditableProxy: VFC<EditableProxyProps> = ({
if (!theatreObject) { if (!theatreObject) {
console.log('no theatre object for', uniqueName) console.log('no theatre object for', uniqueName)
} else { } else {
studio.__experimental_setSelection([theatreObject]) studio.setSelection([theatreObject])
} }
} }
}} }}
@ -206,7 +206,7 @@ const EditableProxy: VFC<EditableProxyProps> = ({
if (!theatreObject) { if (!theatreObject) {
console.log('no theatre object for', uniqueName) console.log('no theatre object for', uniqueName)
} else { } else {
studio.__experimental_setSelection([theatreObject]) studio.setSelection([theatreObject])
} }
} }
}} }}

View file

@ -120,7 +120,7 @@ const SnapshotEditor: React.FC<{paneId: string}> = (props) => {
}, [editorOpen]) }, [editorOpen])
const onPointerMissed = useCallback(() => { const onPointerMissed = useCallback(() => {
if (sheet !== null) studio.__experimental_setSelection([sheet]) if (sheet !== null) studio.setSelection([sheet])
}, [sheet]) }, [sheet])
if (!editorObject) return <></> if (!editorObject) return <></>

View file

@ -20,7 +20,7 @@ export function useSelected(): undefined | string {
} }
} }
setFromStudio(studio.selection) setFromStudio(studio.selection)
return studio.__experimental_onSelectionChange(setFromStudio) return studio.onSelectionChange(setFromStudio)
}, [sheet]) }, [sheet])
return state return state

View file

@ -64,10 +64,6 @@ export default class SheetObject implements IdentityDerivationProvider {
this.template.overrideConfig(nativeObject, config) this.template.overrideConfig(nativeObject, config)
} }
// getValues(): Record<string, unknown> {
// return {}
// }
getValues(): IDerivation<Pointer<SerializableMap>> { getValues(): IDerivation<Pointer<SerializableMap>> {
return this._cache.get('getValues()', () => return this._cache.get('getValues()', () =>
prism(() => { prism(() => {

View file

@ -19,8 +19,9 @@ export interface ISheetObject<
Props extends PropTypeConfig_Compound<$IntentionalAny> = PropTypeConfig_Compound<$IntentionalAny>, Props extends PropTypeConfig_Compound<$IntentionalAny> = PropTypeConfig_Compound<$IntentionalAny>,
> { > {
readonly type: 'Theatre_SheetObject_PublicAPI' readonly type: 'Theatre_SheetObject_PublicAPI'
/** /**
* The type of the values of the SheetObject. *
*/ */
readonly value: Props['valueType'] readonly value: Props['valueType']
readonly props: Pointer<Props['valueType']> readonly props: Pointer<Props['valueType']>

View file

@ -49,20 +49,42 @@ export type PaneInstance<ClassName extends string> = {
export interface IStudio { export interface IStudio {
readonly ui: { readonly ui: {
show(): void /**
* Temporarily hides the studio
*/
hide(): void hide(): void
readonly showing: boolean /**
* Whether the studio is currently visible or hidden
*/
readonly isHidden: boolean
/**
* Makes the studio visible again.
*/
restore(): void restore(): void
} }
/**
* Initializes the studio. Call this in your index.js/index.ts module.
*
* Usage with **tree-shaking**:
* ```ts
* import studio from '@theratre/studio'
*
* // Add this check if you wish to not include the studio in your production bundle
* if (process.env.NODE_ENV === "development") {
* studio.initialize()
* }
* ```
*/
initialize(): void
transaction(fn: (api: ITransactionAPI) => void): void transaction(fn: (api: ITransactionAPI) => void): void
scrub(): IScrub scrub(): IScrub
debouncedScrub(threshhold: number): Pick<IScrub, 'capture'> debouncedScrub(threshhold: number): Pick<IScrub, 'capture'>
__experimental_setSelection(selection: Array<ISheetObject | ISheet>): void setSelection(selection: Array<ISheetObject | ISheet>): void
__experimental_onSelectionChange(
fn: (s: Array<ISheetObject>) => void, onSelectionChange(fn: (s: Array<ISheetObject>) => void): VoidFunction
): VoidFunction
readonly selection: Array<ISheetObject> readonly selection: Array<ISheetObject>
@ -81,16 +103,12 @@ export interface IStudio {
export default class TheatreStudio implements IStudio { export default class TheatreStudio implements IStudio {
readonly ui = { readonly ui = {
show() {
getStudio().ui.show()
},
hide() { hide() {
getStudio().ui.hide() getStudio().ui.hide()
}, },
get showing(): boolean { get isHidden(): boolean {
return getStudio().ui._showing return getStudio().ui.isHidden
}, },
restore() { restore() {
@ -105,6 +123,10 @@ export default class TheatreStudio implements IStudio {
*/ */
constructor(internals: Studio) {} constructor(internals: Studio) {}
initialize() {
getStudio().ui.render()
}
extend(extension: IExtension): void { extend(extension: IExtension): void {
getStudio().extend(extension) getStudio().extend(extension)
} }
@ -129,7 +151,7 @@ export default class TheatreStudio implements IStudio {
return this._getSelectionDerivation().getValue() return this._getSelectionDerivation().getValue()
} }
__experimental_setSelection(selection: Array<ISheetObject | ISheet>): void { setSelection(selection: Array<ISheetObject | ISheet>): void {
const sanitizedSelection = [...selection] const sanitizedSelection = [...selection]
.filter((s) => isSheetObjectPublicAPI(s) || isSheetPublicAPI(s)) .filter((s) => isSheetObjectPublicAPI(s) || isSheetPublicAPI(s))
.map((s) => getStudio().corePrivateAPI!(s)) .map((s) => getStudio().corePrivateAPI!(s))
@ -141,7 +163,7 @@ export default class TheatreStudio implements IStudio {
}) })
} }
__experimental_onSelectionChange(fn: (s: ISheetObject[]) => void): VoidFn { onSelectionChange(fn: (s: ISheetObject[]) => void): VoidFn {
return this._getSelectionDerivation().tapImmediate(studioTicker, fn) return this._getSelectionDerivation().tapImmediate(studioTicker, fn)
} }

View file

@ -3,10 +3,11 @@ import type {$IntentionalAny} from '@theatre/shared/utils/types'
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import type {Studio} from './Studio' import type {Studio} from './Studio'
import {val} from '@theatre/dataverse'
export default class UI { export default class UI {
readonly containerEl = document.createElement('div') readonly containerEl = document.createElement('div')
_showing = false private _rendered = false
private _renderTimeout: NodeJS.Timer | undefined = undefined private _renderTimeout: NodeJS.Timer | undefined = undefined
private _documentBodyUIIsRenderedIn: HTMLElement | undefined = undefined private _documentBodyUIIsRenderedIn: HTMLElement | undefined = undefined
readonly containerShadow: ShadowRoot & HTMLElement readonly containerShadow: ShadowRoot & HTMLElement
@ -27,20 +28,16 @@ export default class UI {
this.containerShadow = this.containerEl.attachShadow({ this.containerShadow = this.containerEl.attachShadow({
mode: 'open', mode: 'open',
// To see why I had to cast this value to HTMLElement, take a look at its // To see why I had to cast this value to HTMLElement, take a look at its
// references. There are a few functions that actually work with a ShadowRoot // references of this prop. There are a few functions that actually work
// but are typed to accept HTMLElement // with a ShadowRoot but are typed to accept HTMLElement
}) as $IntentionalAny as ShadowRoot & HTMLElement }) as $IntentionalAny as ShadowRoot & HTMLElement
} }
show() { render() {
if (this._showing) { if (this._rendered) {
if (this._documentBodyUIIsRenderedIn && document.body) { return
this.hide()
} else {
return
}
} }
this._showing = true this._rendered = true
this._render() this._render()
} }
@ -60,24 +57,21 @@ export default class UI {
} }
hide() { hide() {
if (!this._showing) return this.studio.transaction(({drafts}) => {
this._showing = false drafts.ahistoric.visibilityState = 'everythingIsHidden'
if (this._renderTimeout) { })
clearTimeout(this._renderTimeout)
this._renderTimeout = undefined
} else {
ReactDOM.unmountComponentAtNode(this.containerShadow)
try {
this._documentBodyUIIsRenderedIn!.removeChild(this.containerEl)
this._documentBodyUIIsRenderedIn = undefined
} catch (e) {}
}
} }
restore() { restore() {
this.show() this.render()
this.studio.transaction(({drafts}) => { this.studio.transaction(({drafts}) => {
drafts.ahistoric.visibilityState = 'everythingIsVisible' drafts.ahistoric.visibilityState = 'everythingIsVisible'
}) })
} }
get isHidden() {
return (
val(this.studio.atomP.ahistoric.visibilityState) === 'everythingIsHidden'
)
}
} }

View file

@ -1,6 +0,0 @@
import React from 'react'
export default function TheTrigger() {
// @todo
return <div>The trigger</div>
}

View file

@ -5,7 +5,6 @@ import React from 'react'
import styled, {createGlobalStyle, StyleSheetManager} from 'styled-components' import styled, {createGlobalStyle, StyleSheetManager} from 'styled-components'
import PanelsRoot from './PanelsRoot' import PanelsRoot from './PanelsRoot'
import ProvideTheme from './ProvideTheme' import ProvideTheme from './ProvideTheme'
import TheTrigger from './TheTrigger'
import GlobalToolbar from '@theatre/studio/toolbars/GlobalToolbar/GlobalToolbar' import GlobalToolbar from '@theatre/studio/toolbars/GlobalToolbar/GlobalToolbar'
import useRefAndState from '@theatre/studio/utils/useRefAndState' import useRefAndState from '@theatre/studio/utils/useRefAndState'
import {PortalContext} from 'reakit' import {PortalContext} from 'reakit'
@ -66,7 +65,6 @@ export default function UIRoot() {
const visiblityState = val(studio.atomP.ahistoric.visibilityState) const visiblityState = val(studio.atomP.ahistoric.visibilityState)
const initialised = val(studio.atomP.ephemeral.initialised) const initialised = val(studio.atomP.ephemeral.initialised)
const shouldShowTrigger = visiblityState === 'onlyTriggerIsVisible'
const shouldShowPanels = visiblityState === 'everythingIsVisible' const shouldShowPanels = visiblityState === 'everythingIsVisible'
const shouldShowGlobalToolbar = visiblityState !== 'everythingIsHidden' const shouldShowGlobalToolbar = visiblityState !== 'everythingIsHidden'
@ -83,7 +81,6 @@ export default function UIRoot() {
<Container> <Container>
<PortalLayer ref={portalLayerRef} /> <PortalLayer ref={portalLayerRef} />
{shouldShowGlobalToolbar && <GlobalToolbar />} {shouldShowGlobalToolbar && <GlobalToolbar />}
{shouldShowTrigger && <TheTrigger />}
{shouldShowPanels && <PanelsRoot />} {shouldShowPanels && <PanelsRoot />}
</Container> </Container>
</TooltipContext> </TooltipContext>

View file

@ -15,10 +15,6 @@ setStudio(studioPrivateAPI)
export const studio = studioPrivateAPI.publicApi export const studio = studioPrivateAPI.publicApi
export default studio export default studio
if (process.env.NODE_ENV !== 'test') {
studio.ui.show()
}
registerStudioBundle() registerStudioBundle()
function registerStudioBundle() { function registerStudioBundle() {

View file

@ -2,10 +2,7 @@ import type {ProjectState} from '@theatre/core/projects/store/storeTypes'
import type {IRange, StrictRecord} from '@theatre/shared/utils/types' import type {IRange, StrictRecord} from '@theatre/shared/utils/types'
export type StudioAhistoricState = { export type StudioAhistoricState = {
visibilityState: visibilityState: 'everythingIsHidden' | 'everythingIsVisible'
| 'everythingIsHidden'
| 'everythingIsVisible'
| 'onlyTriggerIsVisible'
theTrigger: { theTrigger: {
position: { position: {