Skip to content

feat(lib): change preview dimension #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/demo/src/components/NonModalView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Paper, Stack } from '@mui/material'
import { Divider, Paper, Stack } from '@mui/material'
import { ModalToggle } from './ModalToggle'
import { ValueMutator } from './ValueMutator'
import { HeightChangeDemo } from './HeightChangeDemo'
Expand All @@ -8,18 +8,28 @@ import { UserContextRequester } from './UserContextRequester'
import { PluginComponent } from './FieldPluginDemo'
import { LanguageView } from './LanguageView'
import { PromptAI } from './PromptAI'
import { PreviewDimension } from './PreviewDimension'

export const NonModalView: PluginComponent = (props) => (
<Paper>
<Stack gap={6}>
<ModalToggle {...props} />
<Divider />
<ValueMutator {...props} />
<Divider />
<AssetSelector {...props} />
<Divider />
<ContextRequester {...props} />
<Divider />
<UserContextRequester {...props} />
<Divider />
<HeightChangeDemo {...props} />
<Divider />
<LanguageView {...props} />
<Divider />
<PromptAI {...props} />
<Divider />
<PreviewDimension {...props} />
</Stack>
</Paper>
)
82 changes: 82 additions & 0 deletions packages/demo/src/components/PreviewDimension.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Button, Input, Stack, Typography, InputLabel } from '@mui/material'
import { PluginComponent } from './FieldPluginDemo'
import { useState } from 'react'

export const PreviewDimension: PluginComponent = (props) => {
const { data, actions } = props

const [width, setWidth] = useState(300)

const handleClickMobile = () => {
actions.setPreviewDimension({
tag: 'mobile',
})
}
const handleClickTablet = () => {
actions.setPreviewDimension({
tag: 'tablet',
})
}
const handleClickDesktop = () => {
actions.setPreviewDimension({
tag: 'desktop',
})
}
const handleClickCustom = () => {
actions.setPreviewDimension({
tag: 'custom',
width: width,
})
}

return (
<Stack gap={2}>
<Typography variant="subtitle1">Dimension</Typography>
<Button
variant="outlined"
color="secondary"
onClick={handleClickMobile}
>
Mobile
</Button>
<Button
variant="outlined"
color="secondary"
onClick={handleClickTablet}
>
Tablet
</Button>
<Button
variant="outlined"
color="secondary"
onClick={handleClickDesktop}
>
Desktop
</Button>
<InputLabel
htmlFor="field-plugin-custom-width"
shrink
>
Set custom width:
</InputLabel>
<Stack direction="row" spacing={2} >
<Input
id="field-plugin-custom-width"
fullWidth
type="number"
value={width}
onChange={(e) => setWidth(Number(e.target.value))}
placeholder="Custom Width"
/>
<Button
fullWidth
variant="outlined"
color="secondary"
onClick={handleClickCustom}
>
Custom
</Button>
</Stack>
</Stack>
)
}
3 changes: 3 additions & 0 deletions packages/field-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
"vite": "5.4.16",
"vite-plugin-dts": "4.2.1",
"vitest": "2.1.9"
},
"dependencies": {
"valibot": "1.1.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ModalSize,
PromptAIPayload,
PromptAIResponse,
Dimension,
} from '../messaging'
import type { FieldPluginData } from './FieldPluginData'

Expand All @@ -20,6 +21,7 @@ export type PromptAI = (payload: PromptAIPayload) => Promise<PromptAIResponse>
export type RequestUserContext = () => Promise<UserData>
export type SelectAsset = () => Promise<Asset>
export type Initialize<Content> = () => Promise<FieldPluginData<Content>>
export type SetPreviewWidth = (previewWidth: Dimension) => Promise<void>

export type FieldPluginActions<Content> = {
setContent: SetContent<Content>
Expand All @@ -28,4 +30,5 @@ export type FieldPluginActions<Content> = {
promptAI: PromptAI
requestUserContext: RequestUserContext
selectAsset: SelectAsset
setPreviewDimension: SetPreviewWidth
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const createFieldPlugin: CreateFieldPlugin = ({
const { uid, host } = params

// ToDo: In development we need to load localhost:3300
// const origin = 'http://localhost:7070'
// const origin = 'http://localhost:3300'

const origin =
typeof targetOrigin === 'string'
? targetOrigin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
LoadedMessage,
OnMessage,
StateChangedMessage,
PreviewDimensionResponseMessage,
} from '../../messaging'
import { getRandomUid } from '../../utils'

Expand All @@ -18,6 +19,10 @@ type CallbackMap = {
stateChanged: Record<CallbackId, OnMessage<StateChangedMessage>>
loaded: Record<CallbackId, OnMessage<LoadedMessage>>
promptAI: Record<CallbackId, OnMessage<PromptAIResponseMessage>>
previewDimension: Record<
CallbackId,
OnMessage<PreviewDimensionResponseMessage>
>
}
type CallbackType = keyof CallbackMap

Expand All @@ -29,6 +34,7 @@ export const callbackQueue = () => {
stateChanged: {},
loaded: {},
promptAI: {},
previewDimension: {},
}

const pushCallback = <T extends CallbackType>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
pluginLoadedMessage,
getPluginPromptAIMessage,
valueChangeMessage,
previewDimensionsChangeMessage,
type OnAssetSelectMessage,
type OnContextRequestMessage,
type OnUserContextRequestMessage,
Expand All @@ -19,6 +20,7 @@ import {
type OnUnknownPluginMessage,
type OnPromptAIMessage,
type PromptAIPayload,
type OnPreviewDimensionMessage,
} from '../../messaging'
import { FieldPluginActions, Initialize } from '../FieldPluginActions'
import { pluginStateFromStateChangeMessage } from './partialPluginStateFromStateChangeMessage'
Expand Down Expand Up @@ -81,6 +83,10 @@ export const createPluginActions: CreatePluginActions = ({
popCallback('promptAI', data.callbackId)?.(data)
}

const onPreviewDimension: OnPreviewDimensionMessage = (data) => {
popCallback('previewDimension', data.callbackId)
}

const onUnknownMessage: OnUnknownPluginMessage = (data) => {
// TODO remove side-effect, making functions in this file pure.
// perhaps only show this message in development mode?
Expand All @@ -100,6 +106,7 @@ export const createPluginActions: CreatePluginActions = ({
onUserContextRequest,
onAssetSelect,
onPromptAI,
onPreviewDimension,
onUnknownMessage,
}

Expand Down Expand Up @@ -176,6 +183,18 @@ export const createPluginActions: CreatePluginActions = ({
postToContainer(getUserContextMessage({ uid, callbackId }))
})
},
setPreviewDimension: (previewWidth) => {
return new Promise((resolve) => {
const callbackId = pushCallback('previewDimension', () => resolve())
postToContainer(
previewDimensionsChangeMessage({
uid,
callbackId,
data: previewWidth,
}),
)
})
},
},
messageCallbacks,
onHeightChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
OnPromptAIMessage,
OnStateChangeMessage,
OnUnknownPluginMessage,
OnPreviewDimensionMessage,
} from '../../../messaging'
import { handlePluginMessage } from './handlePluginMessage'

Expand All @@ -16,6 +17,7 @@ export type PluginMessageCallbacks = {
onUserContextRequest: OnUserContextRequestMessage
onAssetSelect: OnAssetSelectMessage
onPromptAI: OnPromptAIMessage
onPreviewDimension: OnPreviewDimensionMessage
onUnknownMessage: OnUnknownPluginMessage
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const mockCallbacks = (): PluginMessageCallbacks => ({
onAssetSelect: vi.fn(),
onUnknownMessage: vi.fn(),
onLoaded: vi.fn(),
onPreviewDimension: vi.fn(),
onPromptAI: vi.fn(),
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../../messaging'
import { PluginMessageCallbacks } from './createPluginMessageListener'
import { isStateMessage } from '../../../messaging/pluginMessage/containerToPluginMessage/StateChangedMessage'
import { isPreviewDimensionResponse } from '../../../messaging/pluginMessage/containerToPluginMessage//PreviewDimensionResponseMessage'

export const handlePluginMessage = (
data: unknown,
Expand Down Expand Up @@ -38,6 +39,8 @@ export const handlePluginMessage = (
callbacks.onAssetSelect(data)
} else if (isPromptAIMessage(data)) {
callbacks.onPromptAI(data)
} else if (isPreviewDimensionResponse(data)) {
callbacks.onPreviewDimension(data)
} else {
callbacks.onUnknownMessage(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UserContextRequestMessage } from './UserContextRequestMessage'
import { MessageToPlugin } from './MessageToPlugin'
import { StateChangedMessage } from './StateChangedMessage'
import { PromptAIResponseMessage } from './PromptAIResponseMessage'
import { PreviewDimensionResponseMessage } from './PreviewDimensionResponseMessage'

/**
* The plugin container's sends it's state to the plugin
Expand All @@ -14,6 +15,9 @@ export type OnStateChangeMessage = (message: StateChangedMessage) => void
export type OnLoadedMessage = (message: LoadedMessage) => void
export type OnAssetSelectMessage = (message: AssetSelectedMessage) => void
export type OnPromptAIMessage = (message: PromptAIResponseMessage) => void
export type OnPreviewDimensionMessage = (
message: PreviewDimensionResponseMessage,
) => void
export type OnContextRequestMessage = (message: ContextRequestMessage) => void
export type OnUserContextRequestMessage = (
message: UserContextRequestMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
type PreviewDimensionResponseMessage,
isPreviewDimensionResponse,
} from './PreviewDimensionResponseMessage'

const stub: PreviewDimensionResponseMessage = {
action: 'preview-dimension',
uid: '-preview',
callbackId: 'test-callback-id',
}

describe('PreviewDimensionResponseMessage', function () {
it('is a message to plugin', () => {
expect(isPreviewDimensionResponse(stub)).toEqual(true)
})
describe('the action property', () => {
it('equals "preview-dimension"', () => {
expect(
isPreviewDimensionResponse({
...stub,
action: 'preview-dimension',
}),
).toEqual(true)
expect(
isPreviewDimensionResponse({
...stub,
action: 'something-else',
}),
).toEqual(false)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { isMessageToPlugin, type MessageToPlugin } from './MessageToPlugin'

export type PreviewDimensionResponseMessage =
MessageToPlugin<'preview-dimension'>

export const isPreviewDimensionResponse = (
data: unknown,
): data is PreviewDimensionResponseMessage =>
isMessageToPlugin(data) && data.action === 'preview-dimension'
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export * from './UserContextRequestMessage'
export * from './MessageToPlugin'
export * from './StoryData'
export * from './Asset'
export * from './PreviewDimensionResponseMessage'
export * from './PromptAIResponseMessage'
export * from './UserData'
Loading