Skip to content

Commit 3c8bd95

Browse files
johannes-lindgrenfernandacantodemetriusfeijoo
authored
feat(lib): change preview dimension (#463)
* feat: change preview dimension * feat: sandbox and demo * feat: add custom option to preview dimension selection * docs: comment * feat: update custom preview dimension width to dynamic value * feat: reorganize the custom width on preview * feat: create the dimension type * chore: run prettier * chore: remove pure-parse and add valibot * chore: add custom label on sandbox * feat: remove preview dimension callback from the queue list --------- Co-authored-by: Fernanda Nogueira <[email protected]> Co-authored-by: Demetrius Feijóo <[email protected]>
1 parent 89102b2 commit 3c8bd95

21 files changed

+526
-2
lines changed

packages/demo/src/components/NonModalView.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Paper, Stack } from '@mui/material'
1+
import { Divider, Paper, Stack } from '@mui/material'
22
import { ModalToggle } from './ModalToggle'
33
import { ValueMutator } from './ValueMutator'
44
import { HeightChangeDemo } from './HeightChangeDemo'
@@ -8,18 +8,28 @@ import { UserContextRequester } from './UserContextRequester'
88
import { PluginComponent } from './FieldPluginDemo'
99
import { LanguageView } from './LanguageView'
1010
import { PromptAI } from './PromptAI'
11+
import { PreviewDimension } from './PreviewDimension'
1112

1213
export const NonModalView: PluginComponent = (props) => (
1314
<Paper>
1415
<Stack gap={6}>
1516
<ModalToggle {...props} />
17+
<Divider />
1618
<ValueMutator {...props} />
19+
<Divider />
1720
<AssetSelector {...props} />
21+
<Divider />
1822
<ContextRequester {...props} />
23+
<Divider />
1924
<UserContextRequester {...props} />
25+
<Divider />
2026
<HeightChangeDemo {...props} />
27+
<Divider />
2128
<LanguageView {...props} />
29+
<Divider />
2230
<PromptAI {...props} />
31+
<Divider />
32+
<PreviewDimension {...props} />
2333
</Stack>
2434
</Paper>
2535
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Button, Input, Stack, Typography, InputLabel } from '@mui/material'
2+
import { PluginComponent } from './FieldPluginDemo'
3+
import { useState } from 'react'
4+
5+
export const PreviewDimension: PluginComponent = (props) => {
6+
const { data, actions } = props
7+
8+
const [width, setWidth] = useState(300)
9+
10+
const handleClickMobile = () => {
11+
actions.setPreviewDimension({
12+
tag: 'mobile',
13+
})
14+
}
15+
const handleClickTablet = () => {
16+
actions.setPreviewDimension({
17+
tag: 'tablet',
18+
})
19+
}
20+
const handleClickDesktop = () => {
21+
actions.setPreviewDimension({
22+
tag: 'desktop',
23+
})
24+
}
25+
const handleClickCustom = () => {
26+
actions.setPreviewDimension({
27+
tag: 'custom',
28+
width: width,
29+
})
30+
}
31+
32+
return (
33+
<Stack gap={2}>
34+
<Typography variant="subtitle1">Dimension</Typography>
35+
<Button
36+
variant="outlined"
37+
color="secondary"
38+
onClick={handleClickMobile}
39+
>
40+
Mobile
41+
</Button>
42+
<Button
43+
variant="outlined"
44+
color="secondary"
45+
onClick={handleClickTablet}
46+
>
47+
Tablet
48+
</Button>
49+
<Button
50+
variant="outlined"
51+
color="secondary"
52+
onClick={handleClickDesktop}
53+
>
54+
Desktop
55+
</Button>
56+
<InputLabel
57+
htmlFor="field-plugin-custom-width"
58+
shrink
59+
>
60+
Set custom width:
61+
</InputLabel>
62+
<Stack direction="row" spacing={2} >
63+
<Input
64+
id="field-plugin-custom-width"
65+
fullWidth
66+
type="number"
67+
value={width}
68+
onChange={(e) => setWidth(Number(e.target.value))}
69+
placeholder="Custom Width"
70+
/>
71+
<Button
72+
fullWidth
73+
variant="outlined"
74+
color="secondary"
75+
onClick={handleClickCustom}
76+
>
77+
Custom
78+
</Button>
79+
</Stack>
80+
</Stack>
81+
)
82+
}

packages/field-plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@
5454
"vite": "5.4.16",
5555
"vite-plugin-dts": "4.2.1",
5656
"vitest": "2.1.9"
57+
},
58+
"dependencies": {
59+
"valibot": "1.1.0"
5760
}
5861
}

packages/field-plugin/src/createFieldPlugin/FieldPluginActions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
ModalSize,
66
PromptAIPayload,
77
PromptAIResponse,
8+
Dimension,
89
} from '../messaging'
910
import type { FieldPluginData } from './FieldPluginData'
1011

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

2426
export type FieldPluginActions<Content> = {
2527
setContent: SetContent<Content>
@@ -28,4 +30,5 @@ export type FieldPluginActions<Content> = {
2830
promptAI: PromptAI
2931
requestUserContext: RequestUserContext
3032
selectAsset: SelectAsset
33+
setPreviewDimension: SetPreviewWidth
3134
}

packages/field-plugin/src/createFieldPlugin/createFieldPlugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export const createFieldPlugin: CreateFieldPlugin = ({
5555
const { uid, host } = params
5656

5757
// ToDo: In development we need to load localhost:3300
58+
// const origin = 'http://localhost:7070'
59+
// const origin = 'http://localhost:3300'
60+
5861
const origin =
5962
typeof targetOrigin === 'string'
6063
? targetOrigin

packages/field-plugin/src/createFieldPlugin/createPluginActions/callbackQueue.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
LoadedMessage,
77
OnMessage,
88
StateChangedMessage,
9+
PreviewDimensionResponseMessage,
910
} from '../../messaging'
1011
import { getRandomUid } from '../../utils'
1112

@@ -18,6 +19,10 @@ type CallbackMap = {
1819
stateChanged: Record<CallbackId, OnMessage<StateChangedMessage>>
1920
loaded: Record<CallbackId, OnMessage<LoadedMessage>>
2021
promptAI: Record<CallbackId, OnMessage<PromptAIResponseMessage>>
22+
previewDimension: Record<
23+
CallbackId,
24+
OnMessage<PreviewDimensionResponseMessage>
25+
>
2126
}
2227
type CallbackType = keyof CallbackMap
2328

@@ -29,6 +34,7 @@ export const callbackQueue = () => {
2934
stateChanged: {},
3035
loaded: {},
3136
promptAI: {},
37+
previewDimension: {},
3238
}
3339

3440
const pushCallback = <T extends CallbackType>(

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
pluginLoadedMessage,
1212
getPluginPromptAIMessage,
1313
valueChangeMessage,
14+
previewDimensionsChangeMessage,
1415
type OnAssetSelectMessage,
1516
type OnContextRequestMessage,
1617
type OnUserContextRequestMessage,
@@ -19,6 +20,7 @@ import {
1920
type OnUnknownPluginMessage,
2021
type OnPromptAIMessage,
2122
type PromptAIPayload,
23+
type OnPreviewDimensionMessage,
2224
} from '../../messaging'
2325
import { FieldPluginActions, Initialize } from '../FieldPluginActions'
2426
import { pluginStateFromStateChangeMessage } from './partialPluginStateFromStateChangeMessage'
@@ -81,6 +83,10 @@ export const createPluginActions: CreatePluginActions = ({
8183
popCallback('promptAI', data.callbackId)?.(data)
8284
}
8385

86+
const onPreviewDimension: OnPreviewDimensionMessage = (data) => {
87+
popCallback('previewDimension', data.callbackId)
88+
}
89+
8490
const onUnknownMessage: OnUnknownPluginMessage = (data) => {
8591
// TODO remove side-effect, making functions in this file pure.
8692
// perhaps only show this message in development mode?
@@ -100,6 +106,7 @@ export const createPluginActions: CreatePluginActions = ({
100106
onUserContextRequest,
101107
onAssetSelect,
102108
onPromptAI,
109+
onPreviewDimension,
103110
onUnknownMessage,
104111
}
105112

@@ -176,6 +183,18 @@ export const createPluginActions: CreatePluginActions = ({
176183
postToContainer(getUserContextMessage({ uid, callbackId }))
177184
})
178185
},
186+
setPreviewDimension: (previewWidth) => {
187+
return new Promise((resolve) => {
188+
const callbackId = pushCallback('previewDimension', () => resolve())
189+
postToContainer(
190+
previewDimensionsChangeMessage({
191+
uid,
192+
callbackId,
193+
data: previewWidth,
194+
}),
195+
)
196+
})
197+
},
179198
},
180199
messageCallbacks,
181200
onHeightChange,

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginMessageListener/createPluginMessageListener.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
OnPromptAIMessage,
77
OnStateChangeMessage,
88
OnUnknownPluginMessage,
9+
OnPreviewDimensionMessage,
910
} from '../../../messaging'
1011
import { handlePluginMessage } from './handlePluginMessage'
1112

@@ -16,6 +17,7 @@ export type PluginMessageCallbacks = {
1617
onUserContextRequest: OnUserContextRequestMessage
1718
onAssetSelect: OnAssetSelectMessage
1819
onPromptAI: OnPromptAIMessage
20+
onPreviewDimension: OnPreviewDimensionMessage
1921
onUnknownMessage: OnUnknownPluginMessage
2022
}
2123

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginMessageListener/handlePluginMessage.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const mockCallbacks = (): PluginMessageCallbacks => ({
1818
onAssetSelect: vi.fn(),
1919
onUnknownMessage: vi.fn(),
2020
onLoaded: vi.fn(),
21+
onPreviewDimension: vi.fn(),
2122
onPromptAI: vi.fn(),
2223
})
2324

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginMessageListener/handlePluginMessage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../../../messaging'
99
import { PluginMessageCallbacks } from './createPluginMessageListener'
1010
import { isStateMessage } from '../../../messaging/pluginMessage/containerToPluginMessage/StateChangedMessage'
11+
import { isPreviewDimensionResponse } from '../../../messaging/pluginMessage/containerToPluginMessage//PreviewDimensionResponseMessage'
1112

1213
export const handlePluginMessage = (
1314
data: unknown,
@@ -38,6 +39,8 @@ export const handlePluginMessage = (
3839
callbacks.onAssetSelect(data)
3940
} else if (isPromptAIMessage(data)) {
4041
callbacks.onPromptAI(data)
42+
} else if (isPreviewDimensionResponse(data)) {
43+
callbacks.onPreviewDimension(data)
4144
} else {
4245
callbacks.onUnknownMessage(data)
4346
}

0 commit comments

Comments
 (0)