sanity-plugin-pte-interpolation 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/sanity-plugin-pte-interpolation.svg?style=flat-square)](https://www.npmjs.com/package/sanity-plugin-pte-interpolation)
4
4
 
5
+ ![Demo](https://raw.githubusercontent.com/jordanl17/sanity-pte-interpolation/main/.github/assets/demo.png)
6
+
5
7
  Sanity Studio schema helper that adds dynamic variable picker inline blocks to the [Portable Text Editor](https://www.sanity.io/docs/portable-text-editor). Editors can insert named variables (like `{firstName}` or `{email}`) directly into rich text content, which are then resolved to real values at render time.
6
8
 
7
9
  Part of [sanity-pte-interpolation](https://github.com/jordanl17/sanity-pte-interpolation). For rendering the variables in React, see [`pte-interpolation-react`](https://www.npmjs.com/package/pte-interpolation-react).
@@ -85,6 +87,16 @@ import {InterpolatedPortableText} from 'pte-interpolation-react'
85
87
  />
86
88
  ```
87
89
 
90
+ For framework-agnostic use cases - plain string output, variable key extraction, server-side rendering, or any non-React environment - use [`pte-interpolation-core`](https://www.npmjs.com/package/pte-interpolation-core) directly:
91
+
92
+ ```ts
93
+ import {interpolateToString, extractVariableKeys} from 'pte-interpolation-core'
94
+
95
+ const keys = extractVariableKeys(blocks) // ['firstName', 'email']
96
+ const text = interpolateToString(blocks, {firstName: 'Jo', email: 'jo@example.com'})
97
+ // "Hello, Jo! Your email is jo@example.com."
98
+ ```
99
+
88
100
  ## Data Shape
89
101
 
90
102
  Variables are stored as inline objects within Portable Text blocks:
@@ -125,7 +137,7 @@ interface InterpolationVariable {
125
137
 
126
138
  ### `VARIABLE_TYPE_PREFIX`
127
139
 
128
- The constant `'pteInterpolationVariable'` the `_type` string used for variable inline blocks in stored Portable Text. Exported for advanced use cases.
140
+ The constant `'pteInterpolationVariable'` - the `_type` string used for variable inline blocks in stored Portable Text. Exported for advanced use cases.
129
141
 
130
142
  ## License
131
143
 
package/dist/index.cjs CHANGED
@@ -1,14 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var icons = require("@sanity/icons"), sanity = require("sanity"), jsxRuntime = require("react/jsx-runtime"), ui = require("@sanity/ui");
3
+ var icons = require("@sanity/icons"), sanity = require("sanity"), jsxRuntime = require("react/jsx-runtime"), ui = require("@sanity/ui"), react = require("react");
4
4
  function VariableKeyField(props) {
5
5
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: props.children });
6
6
  }
7
7
  function createVariableKeyInput(variables) {
8
+ const options = variables.map((variable) => ({
9
+ value: variable.id,
10
+ name: variable.name,
11
+ description: variable.description
12
+ }));
8
13
  return function(props) {
9
- const selectedVariable = variables.find((variable) => variable.id === props.value);
14
+ const autocompleteId = react.useId(), selectedVariable = variables.find((variable) => variable.id === props.value), handleChange = react.useCallback(
15
+ (selectedValue) => {
16
+ props.onChange(selectedValue ? sanity.set(selectedValue) : sanity.unset());
17
+ },
18
+ [props]
19
+ ), filterOption = react.useCallback((query, option) => option.name.toLowerCase().includes(query.toLowerCase()), []), renderOption = react.useCallback((option) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Card, { as: "button", padding: 3, children: [
20
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, weight: "medium", children: option.name }),
21
+ option.description && /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { marginTop: 2, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 0, muted: !0, children: option.description }) })
22
+ ] }), []), renderValue = react.useCallback((_value, option) => option ? option.name : options.find((candidate) => candidate.value === _value)?.name ?? _value, []);
10
23
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
11
- props.renderDefault(props),
24
+ /* @__PURE__ */ jsxRuntime.jsx(
25
+ ui.Autocomplete,
26
+ {
27
+ id: autocompleteId,
28
+ options,
29
+ value: typeof props.value == "string" ? props.value : void 0,
30
+ onChange: handleChange,
31
+ filterOption,
32
+ renderOption,
33
+ renderValue,
34
+ openButton: !0,
35
+ icon: icons.SearchIcon,
36
+ placeholder: "Search variables...",
37
+ fontSize: 1,
38
+ padding: 3
39
+ }
40
+ ),
12
41
  selectedVariable?.description && /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { marginTop: 2, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, muted: !0, children: selectedVariable.description }) })
13
42
  ] });
14
43
  };
@@ -37,12 +66,6 @@ function interpolationVariables(variables, block) {
37
66
  name: "variableKey",
38
67
  title: "Variable",
39
68
  type: "string",
40
- options: {
41
- list: variables.map((variable) => ({
42
- title: variable.name,
43
- value: variable.id
44
- }))
45
- },
46
69
  validation: (rule) => rule.required(),
47
70
  components: {
48
71
  field: VariableKeyField,
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/components/VariableInlineBlock.tsx","../src/interpolationVariables.ts"],"sourcesContent":["import {Box, Text} from '@sanity/ui'\nimport type {BlockProps, FieldProps, InputProps} from 'sanity'\nimport type {InterpolationVariable} from '../types'\n\nexport function VariableKeyField(props: FieldProps) {\n return <>{props.children}</>\n}\n\nexport function createVariableKeyInput(variables: InterpolationVariable[]) {\n return function VariableKeyInput(props: InputProps) {\n const selectedVariable = variables.find((variable) => variable.id === props.value)\n\n return (\n <>\n {props.renderDefault(props)}\n {selectedVariable?.description && (\n <Box marginTop={2}>\n <Text size={1} muted>\n {selectedVariable.description}\n </Text>\n </Box>\n )}\n </>\n )\n }\n}\n\nexport function createVariableInlineBlock(variables: InterpolationVariable[]) {\n return function VariableInlineBlock(props: BlockProps) {\n const value = props.value as {variableKey?: string}\n const variableKey = value?.variableKey\n const variable = variables.find((candidate) => candidate.id === variableKey)\n\n return props.renderDefault({\n ...props,\n renderPreview: () => (\n <Box padding={2}>\n <Text size={0} weight=\"medium\">\n {variable?.name ?? variableKey ?? 'Select variable'}\n </Text>\n </Box>\n ),\n })\n }\n}\n","import {TagIcon} from '@sanity/icons'\nimport {defineArrayMember, defineField} from 'sanity'\nimport {\n createVariableInlineBlock,\n createVariableKeyInput,\n VariableKeyField,\n} from './components/VariableInlineBlock'\nimport type {InterpolationVariable} from './types'\n\n/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n\n/** @public */\nexport function interpolationVariables(\n variables: InterpolationVariable[],\n block?: ReturnType<typeof defineArrayMember>,\n) {\n const variableType = defineArrayMember({\n type: 'object',\n name: VARIABLE_TYPE_PREFIX,\n title: 'Variable',\n icon: TagIcon,\n options: {\n modal: {width: 0},\n },\n fields: [\n defineField({\n name: 'variableKey',\n title: 'Variable',\n type: 'string',\n options: {\n list: variables.map((variable) => ({\n title: variable.name,\n value: variable.id,\n })),\n },\n validation: (rule) => rule.required(),\n components: {\n field: VariableKeyField,\n input: createVariableKeyInput(variables),\n },\n }),\n ],\n components: {\n inlineBlock: createVariableInlineBlock(variables),\n },\n })\n\n const baseBlock = block ?? defineArrayMember({type: 'block'})\n\n return {\n ...baseBlock,\n of: [...((baseBlock as {of?: unknown[]}).of ?? []), variableType],\n }\n}\n"],"names":["jsx","Fragment","jsxs","Box","Text","defineArrayMember","TagIcon","defineField"],"mappings":";;;AAIO,SAAS,iBAAiB,OAAmB;AAClD,SAAOA,2BAAAA,IAAAC,WAAAA,UAAA,EAAG,gBAAM,SAAA,CAAS;AAC3B;AAEO,SAAS,uBAAuB,WAAoC;AACzE,SAAO,SAA0B,OAAmB;AAClD,UAAM,mBAAmB,UAAU,KAAK,CAAC,aAAa,SAAS,OAAO,MAAM,KAAK;AAEjF,WACEC,2BAAAA,KAAAD,qBAAA,EACG,UAAA;AAAA,MAAA,MAAM,cAAc,KAAK;AAAA,MACzB,kBAAkB,eACjBD,+BAACG,GAAAA,KAAA,EAAI,WAAW,GACd,UAAAH,2BAAAA,IAACI,GAAAA,MAAA,EAAK,MAAM,GAAG,OAAK,IACjB,UAAA,iBAAiB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,MAAM,CAAA,GAAK,YAAY;AAAA,EAAA;AAEpE;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/components/VariableInlineBlock.tsx","../src/interpolationVariables.ts"],"sourcesContent":["import {SearchIcon} from '@sanity/icons'\nimport {Autocomplete, Box, Card, Text} from '@sanity/ui'\nimport {useCallback, useId} from 'react'\nimport {set, unset} from 'sanity'\nimport type {BlockProps, FieldProps, InputProps} from 'sanity'\nimport type {InterpolationVariable} from '../types'\n\ninterface VariableOption {\n value: string\n name: string\n description?: string\n}\n\nexport function VariableKeyField(props: FieldProps) {\n return <>{props.children}</>\n}\n\nexport function createVariableKeyInput(variables: InterpolationVariable[]) {\n const options: VariableOption[] = variables.map((variable) => ({\n value: variable.id,\n name: variable.name,\n description: variable.description,\n }))\n\n return function VariableKeyInput(props: InputProps) {\n const autocompleteId = useId()\n const selectedVariable = variables.find((variable) => variable.id === props.value)\n\n const handleChange = useCallback(\n (selectedValue: string) => {\n props.onChange(selectedValue ? set(selectedValue) : unset())\n },\n [props],\n )\n\n const filterOption = useCallback((query: string, option: VariableOption) => {\n return option.name.toLowerCase().includes(query.toLowerCase())\n }, [])\n\n const renderOption = useCallback((option: VariableOption) => {\n return (\n <Card as=\"button\" padding={3}>\n <Text size={1} weight=\"medium\">\n {option.name}\n </Text>\n {option.description && (\n <Box marginTop={2}>\n <Text size={0} muted>\n {option.description}\n </Text>\n </Box>\n )}\n </Card>\n )\n }, [])\n\n const renderValue = useCallback((_value: string, option?: VariableOption) => {\n if (option) return option.name\n const matchedOption = options.find((candidate) => candidate.value === _value)\n return matchedOption?.name ?? _value\n }, [])\n\n return (\n <>\n <Autocomplete\n id={autocompleteId}\n options={options}\n value={typeof props.value === 'string' ? props.value : undefined}\n onChange={handleChange}\n filterOption={filterOption}\n renderOption={renderOption}\n renderValue={renderValue}\n openButton\n icon={SearchIcon}\n placeholder=\"Search variables...\"\n fontSize={1}\n padding={3}\n />\n {selectedVariable?.description && (\n <Box marginTop={2}>\n <Text size={1} muted>\n {selectedVariable.description}\n </Text>\n </Box>\n )}\n </>\n )\n }\n}\n\nexport function createVariableInlineBlock(variables: InterpolationVariable[]) {\n return function VariableInlineBlock(props: BlockProps) {\n const value = props.value as {variableKey?: string}\n const variableKey = value?.variableKey\n const variable = variables.find((candidate) => candidate.id === variableKey)\n\n return props.renderDefault({\n ...props,\n renderPreview: () => (\n <Box padding={2}>\n <Text size={0} weight=\"medium\">\n {variable?.name ?? variableKey ?? 'Select variable'}\n </Text>\n </Box>\n ),\n })\n }\n}\n","import {TagIcon} from '@sanity/icons'\nimport {defineArrayMember, defineField} from 'sanity'\nimport {\n createVariableInlineBlock,\n createVariableKeyInput,\n VariableKeyField,\n} from './components/VariableInlineBlock'\nimport type {InterpolationVariable} from './types'\n\n/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n\n/** @public */\nexport function interpolationVariables(\n variables: InterpolationVariable[],\n block?: ReturnType<typeof defineArrayMember>,\n) {\n const variableType = defineArrayMember({\n type: 'object',\n name: VARIABLE_TYPE_PREFIX,\n title: 'Variable',\n icon: TagIcon,\n options: {\n modal: {width: 0},\n },\n fields: [\n defineField({\n name: 'variableKey',\n title: 'Variable',\n type: 'string',\n validation: (rule) => rule.required(),\n components: {\n field: VariableKeyField,\n input: createVariableKeyInput(variables),\n },\n }),\n ],\n components: {\n inlineBlock: createVariableInlineBlock(variables),\n },\n })\n\n const baseBlock = block ?? defineArrayMember({type: 'block'})\n\n return {\n ...baseBlock,\n of: [...((baseBlock as {of?: unknown[]}).of ?? []), variableType],\n }\n}\n"],"names":["jsx","Fragment","useId","useCallback","set","unset","jsxs","Card","Text","Box","Autocomplete","SearchIcon","defineArrayMember","TagIcon","defineField"],"mappings":";;;AAaO,SAAS,iBAAiB,OAAmB;AAClD,SAAOA,2BAAAA,IAAAC,WAAAA,UAAA,EAAG,gBAAM,SAAA,CAAS;AAC3B;AAEO,SAAS,uBAAuB,WAAoC;AACzE,QAAM,UAA4B,UAAU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D,GAEK,YAAY,SAASF,OAAAA,kBAAkB,EAAC,MAAM,SAAQ;AAE5D,SAAO;AAAA,IACL,GAAG;AAAA,IACH,IAAI,CAAC,GAAK,UAA+B,MAAM,CAAA,GAAK,YAAY;AAAA,EAAA;AAEpE;;;"}
package/dist/index.d.cts CHANGED
@@ -107,11 +107,11 @@ export declare function interpolationVariables(
107
107
  readOnly?: ConditionalProperty
108
108
  placeholder?: string | undefined
109
109
  title?: string | undefined
110
- description?: (string | React.JSX.Element) | undefined
110
+ icon?: ReactNode | ComponentType
111
111
  options?: StringOptions | undefined
112
+ description?: (string | React.JSX.Element) | undefined
112
113
  initialValue?: InitialValueProperty<any, any>
113
114
  components?: StringComponents | undefined
114
- icon?: ReactNode | ComponentType
115
115
  deprecated?: DeprecatedProperty | undefined
116
116
  }
117
117
  | {
@@ -130,11 +130,11 @@ export declare function interpolationVariables(
130
130
  readOnly?: ConditionalProperty
131
131
  placeholder?: string | undefined
132
132
  title?: string | undefined
133
- description?: (string | React.JSX.Element) | undefined
133
+ icon?: ReactNode | ComponentType
134
134
  options?: NumberOptions | undefined
135
+ description?: (string | React.JSX.Element) | undefined
135
136
  initialValue?: InitialValueProperty<any, any>
136
137
  components?: NumberComponents | undefined
137
- icon?: ReactNode | ComponentType
138
138
  deprecated?: DeprecatedProperty | undefined
139
139
  }
140
140
  | {
@@ -152,11 +152,11 @@ export declare function interpolationVariables(
152
152
  | undefined
153
153
  readOnly?: ConditionalProperty
154
154
  title?: string | undefined
155
- description?: (string | React.JSX.Element) | undefined
155
+ icon?: ReactNode | ComponentType
156
156
  options?: BooleanOptions | undefined
157
+ description?: (string | React.JSX.Element) | undefined
157
158
  initialValue?: InitialValueProperty<any, any>
158
159
  components?: BooleanComponents | undefined
159
- icon?: ReactNode | ComponentType
160
160
  deprecated?: DeprecatedProperty | undefined
161
161
  }
162
162
  | {
@@ -175,14 +175,14 @@ export declare function interpolationVariables(
175
175
  readOnly?: ConditionalProperty
176
176
  groups?: FieldGroupDefinition[] | undefined
177
177
  title?: string | undefined
178
+ icon?: ReactNode | ComponentType
179
+ options?: ObjectOptions | undefined
178
180
  description?: (string | React.JSX.Element) | undefined
179
181
  fields: FieldDefinition[]
180
182
  fieldsets?: FieldsetDefinition[] | undefined
181
- options?: ObjectOptions | undefined
182
183
  initialValue?: InitialValueProperty<any, any>
183
184
  components?: ObjectComponents | undefined
184
185
  renderMembers?: ObjectRenderMembersCallback | undefined
185
- icon?: ReactNode | ComponentType
186
186
  deprecated?: DeprecatedProperty | undefined
187
187
  }
188
188
  | {
@@ -200,20 +200,69 @@ export declare function interpolationVariables(
200
200
  | undefined
201
201
  readOnly?: ConditionalProperty
202
202
  title?: string | undefined
203
- description?: (string | React.JSX.Element) | undefined
203
+ icon?: ReactNode | ComponentType
204
204
  options?: BlockOptions | undefined
205
+ description?: (string | React.JSX.Element) | undefined
205
206
  initialValue?: InitialValueProperty<any, any>
206
207
  components?:
207
208
  | {
208
209
  block?: ComponentType<BlockProps>
209
210
  }
210
211
  | undefined
211
- icon?: ReactNode | ComponentType
212
212
  deprecated?: DeprecatedProperty | undefined
213
213
  styles?: BlockStyleDefinition[] | undefined
214
214
  lists?: BlockListDefinition[] | undefined
215
215
  marks?: BlockMarksDefinition | undefined
216
216
  }
217
+ | {
218
+ of: unknown[]
219
+ type: 'image'
220
+ name?: string | undefined
221
+ validation?:
222
+ | (ValidationBuilder<ImageRule, ImageValue> &
223
+ (
224
+ | false
225
+ | Rule
226
+ | SchemaValidationValue[]
227
+ | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
228
+ ))
229
+ | undefined
230
+ readOnly?: ConditionalProperty
231
+ title?: string | undefined
232
+ icon?: ReactNode | ComponentType
233
+ options?: ImageOptions | undefined
234
+ description?: (string | React.JSX.Element) | undefined
235
+ fields?: FieldDefinition[] | undefined
236
+ fieldsets?: FieldsetDefinition[] | undefined
237
+ initialValue?: InitialValueProperty<any, any>
238
+ components?: ImageComponents | undefined
239
+ renderMembers?: ObjectRenderMembersCallback | undefined
240
+ deprecated?: DeprecatedProperty | undefined
241
+ }
242
+ | {
243
+ of: unknown[]
244
+ type: 'text'
245
+ name?: string | undefined
246
+ validation?:
247
+ | (ValidationBuilder<TextRule, string> &
248
+ (
249
+ | false
250
+ | Rule
251
+ | SchemaValidationValue[]
252
+ | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
253
+ ))
254
+ | undefined
255
+ readOnly?: ConditionalProperty
256
+ placeholder?: string | undefined
257
+ rows?: number | undefined
258
+ title?: string | undefined
259
+ icon?: ReactNode | ComponentType
260
+ options?: TextOptions | undefined
261
+ description?: (string | React.JSX.Element) | undefined
262
+ initialValue?: InitialValueProperty<any, any>
263
+ components?: TextComponents | undefined
264
+ deprecated?: DeprecatedProperty | undefined
265
+ }
217
266
  | {
218
267
  of: unknown[]
219
268
  type: 'array'
@@ -229,11 +278,11 @@ export declare function interpolationVariables(
229
278
  | undefined
230
279
  readOnly?: ConditionalProperty
231
280
  title?: string | undefined
232
- description?: (string | React.JSX.Element) | undefined
281
+ icon?: ReactNode | ComponentType
233
282
  options?: ArrayOptions | undefined
283
+ description?: (string | React.JSX.Element) | undefined
234
284
  initialValue?: InitialValueProperty<any, any>
235
285
  components?: (ArrayOfObjectsComponents | ArrayOfPrimitivesComponents) | undefined
236
- icon?: ReactNode | ComponentType
237
286
  deprecated?: DeprecatedProperty | undefined
238
287
  }
239
288
  | {
@@ -252,11 +301,11 @@ export declare function interpolationVariables(
252
301
  readOnly?: ConditionalProperty
253
302
  placeholder?: string | undefined
254
303
  title?: string | undefined
255
- description?: (string | React.JSX.Element) | undefined
304
+ icon?: ReactNode | ComponentType
256
305
  options?: DateOptions | undefined
306
+ description?: (string | React.JSX.Element) | undefined
257
307
  initialValue?: InitialValueProperty<any, any>
258
308
  components?: DateComponents | undefined
259
- icon?: ReactNode | ComponentType
260
309
  deprecated?: DeprecatedProperty | undefined
261
310
  }
262
311
  | {
@@ -275,11 +324,11 @@ export declare function interpolationVariables(
275
324
  readOnly?: ConditionalProperty
276
325
  placeholder?: string | undefined
277
326
  title?: string | undefined
278
- description?: (string | React.JSX.Element) | undefined
327
+ icon?: ReactNode | ComponentType
279
328
  options?: DatetimeOptions | undefined
329
+ description?: (string | React.JSX.Element) | undefined
280
330
  initialValue?: InitialValueProperty<any, any>
281
331
  components?: DatetimeComponents | undefined
282
- icon?: ReactNode | ComponentType
283
332
  deprecated?: DeprecatedProperty | undefined
284
333
  }
285
334
  | {
@@ -298,14 +347,14 @@ export declare function interpolationVariables(
298
347
  readOnly?: ConditionalProperty
299
348
  groups?: FieldGroupDefinition[] | undefined
300
349
  title?: string | undefined
350
+ icon?: ReactNode | ComponentType
351
+ options?: DocumentOptions | undefined
301
352
  description?: (string | React.JSX.Element) | undefined
302
353
  fields: FieldDefinition[]
303
354
  fieldsets?: FieldsetDefinition[] | undefined
304
- options?: DocumentOptions | undefined
305
355
  initialValue?: InitialValueProperty<any, any>
306
356
  components?: DocumentComponents | undefined
307
357
  renderMembers?: ObjectRenderMembersCallback | undefined
308
- icon?: ReactNode | ComponentType
309
358
  deprecated?: DeprecatedProperty | undefined
310
359
  liveEdit?: boolean | undefined
311
360
  orderings?: SortOrdering[] | undefined
@@ -334,14 +383,14 @@ export declare function interpolationVariables(
334
383
  | undefined
335
384
  readOnly?: ConditionalProperty
336
385
  title?: string | undefined
386
+ icon?: ReactNode | ComponentType
387
+ options?: FileOptions | undefined
337
388
  description?: (string | React.JSX.Element) | undefined
338
389
  fields?: ObjectDefinition['fields'] | undefined
339
390
  fieldsets?: FieldsetDefinition[] | undefined
340
- options?: FileOptions | undefined
341
391
  initialValue?: InitialValueProperty<any, any>
342
392
  components?: FileComponents | undefined
343
393
  renderMembers?: ObjectRenderMembersCallback | undefined
344
- icon?: ReactNode | ComponentType
345
394
  deprecated?: DeprecatedProperty | undefined
346
395
  }
347
396
  | {
@@ -359,36 +408,11 @@ export declare function interpolationVariables(
359
408
  | undefined
360
409
  readOnly?: ConditionalProperty
361
410
  title?: string | undefined
362
- description?: (string | React.JSX.Element) | undefined
363
- options?: GeopointOptions | undefined
364
- initialValue?: InitialValueProperty<any, any>
365
- components?: GeopointComponents | undefined
366
411
  icon?: ReactNode | ComponentType
367
- deprecated?: DeprecatedProperty | undefined
368
- }
369
- | {
370
- of: unknown[]
371
- type: 'image'
372
- name?: string | undefined
373
- validation?:
374
- | (ValidationBuilder<ImageRule, ImageValue> &
375
- (
376
- | false
377
- | Rule
378
- | SchemaValidationValue[]
379
- | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
380
- ))
381
- | undefined
382
- readOnly?: ConditionalProperty
383
- title?: string | undefined
412
+ options?: GeopointOptions | undefined
384
413
  description?: (string | React.JSX.Element) | undefined
385
- fields?: FieldDefinition[] | undefined
386
- fieldsets?: FieldsetDefinition[] | undefined
387
- options?: ImageOptions | undefined
388
414
  initialValue?: InitialValueProperty<any, any>
389
- components?: ImageComponents | undefined
390
- renderMembers?: ObjectRenderMembersCallback | undefined
391
- icon?: ReactNode | ComponentType
415
+ components?: GeopointComponents | undefined
392
416
  deprecated?: DeprecatedProperty | undefined
393
417
  }
394
418
  | {
@@ -406,13 +430,13 @@ export declare function interpolationVariables(
406
430
  | undefined
407
431
  readOnly?: ConditionalProperty
408
432
  title?: string | undefined
409
- description?: (string | React.JSX.Element) | undefined
433
+ to: ReferenceTo
434
+ icon?: ReactNode | ComponentType
410
435
  options?: ReferenceOptions | undefined
436
+ description?: (string | React.JSX.Element) | undefined
411
437
  initialValue?: InitialValueProperty<any, any>
412
438
  components?: ReferenceComponents | undefined
413
- icon?: ReactNode | ComponentType
414
439
  deprecated?: DeprecatedProperty | undefined
415
- to: ReferenceTo
416
440
  weak?: boolean | undefined
417
441
  }
418
442
  | {
@@ -422,12 +446,6 @@ export declare function interpolationVariables(
422
446
  validation?: SchemaValidationValue
423
447
  readOnly?: ConditionalProperty
424
448
  title?: string | undefined
425
- description?: (string | React.JSX.Element) | undefined
426
- options?: ReferenceOptions | undefined
427
- initialValue?: InitialValueProperty<any, any>
428
- components?: CrossDatasetReferenceComponents | undefined
429
- icon?: ReactNode | ComponentType
430
- deprecated?: DeprecatedProperty | undefined
431
449
  to: {
432
450
  type: string
433
451
  title?: string
@@ -439,6 +457,12 @@ export declare function interpolationVariables(
439
457
  mapWith?: string
440
458
  }[]
441
459
  }[]
460
+ icon?: ReactNode | ComponentType
461
+ options?: ReferenceOptions | undefined
462
+ description?: (string | React.JSX.Element) | undefined
463
+ initialValue?: InitialValueProperty<any, any>
464
+ components?: CrossDatasetReferenceComponents | undefined
465
+ deprecated?: DeprecatedProperty | undefined
442
466
  weak?: boolean | undefined
443
467
  dataset: string
444
468
  studioUrl?: ((document: {id: string; type?: string}) => string | null) | undefined
@@ -452,17 +476,17 @@ export declare function interpolationVariables(
452
476
  validation?: SchemaValidationValue
453
477
  readOnly?: ConditionalProperty
454
478
  title?: string | undefined
455
- description?: (string | React.JSX.Element) | undefined
456
- options?: ReferenceOptions | undefined
457
- initialValue?: InitialValueProperty<any, any>
458
- icon?: ReactNode | ComponentType
459
- deprecated?: DeprecatedProperty | undefined
460
479
  to: {
461
480
  type: string
462
481
  title?: string
463
482
  icon?: ComponentType
464
483
  preview?: PreviewConfig
465
484
  }[]
485
+ icon?: ReactNode | ComponentType
486
+ options?: ReferenceOptions | undefined
487
+ description?: (string | React.JSX.Element) | undefined
488
+ initialValue?: InitialValueProperty<any, any>
489
+ deprecated?: DeprecatedProperty | undefined
466
490
  weak?: boolean | undefined
467
491
  studioUrl?: string | ((document: {id: string; type?: string}) => string | null) | undefined
468
492
  resourceType: string
@@ -483,35 +507,11 @@ export declare function interpolationVariables(
483
507
  | undefined
484
508
  readOnly?: ConditionalProperty
485
509
  title?: string | undefined
486
- description?: (string | React.JSX.Element) | undefined
487
- options?: SlugOptions | undefined
488
- initialValue?: InitialValueProperty<any, any>
489
- components?: SlugComponents | undefined
490
510
  icon?: ReactNode | ComponentType
491
- deprecated?: DeprecatedProperty | undefined
492
- }
493
- | {
494
- of: unknown[]
495
- type: 'text'
496
- name?: string | undefined
497
- validation?:
498
- | (ValidationBuilder<TextRule, string> &
499
- (
500
- | false
501
- | Rule
502
- | SchemaValidationValue[]
503
- | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
504
- ))
505
- | undefined
506
- readOnly?: ConditionalProperty
507
- placeholder?: string | undefined
508
- rows?: number | undefined
509
- title?: string | undefined
511
+ options?: SlugOptions | undefined
510
512
  description?: (string | React.JSX.Element) | undefined
511
- options?: TextOptions | undefined
512
513
  initialValue?: InitialValueProperty<any, any>
513
- components?: TextComponents | undefined
514
- icon?: ReactNode | ComponentType
514
+ components?: SlugComponents | undefined
515
515
  deprecated?: DeprecatedProperty | undefined
516
516
  }
517
517
  | {
@@ -530,11 +530,11 @@ export declare function interpolationVariables(
530
530
  readOnly?: ConditionalProperty
531
531
  placeholder?: string | undefined
532
532
  title?: string | undefined
533
- description?: (string | React.JSX.Element) | undefined
533
+ icon?: ReactNode | ComponentType
534
534
  options?: UrlOptions | undefined
535
+ description?: (string | React.JSX.Element) | undefined
535
536
  initialValue?: InitialValueProperty<any, any>
536
537
  components?: UrlComponents | undefined
537
- icon?: ReactNode | ComponentType
538
538
  deprecated?: DeprecatedProperty | undefined
539
539
  }
540
540
  | {
@@ -553,11 +553,11 @@ export declare function interpolationVariables(
553
553
  readOnly?: ConditionalProperty
554
554
  placeholder?: string | undefined
555
555
  title?: string | undefined
556
- description?: (string | React.JSX.Element) | undefined
556
+ icon?: ReactNode | ComponentType
557
557
  options?: EmailOptions | undefined
558
+ description?: (string | React.JSX.Element) | undefined
558
559
  initialValue?: InitialValueProperty<any, any>
559
560
  components?: EmailComponents | undefined
560
- icon?: ReactNode | ComponentType
561
561
  deprecated?: DeprecatedProperty | undefined
562
562
  }
563
563
  | {
@@ -567,9 +567,10 @@ export declare function interpolationVariables(
567
567
  validation?: SchemaValidationValue
568
568
  readOnly?: ConditionalProperty
569
569
  title?: string | undefined
570
+ icon?: ReactNode | ComponentType
571
+ options?: unknown
570
572
  description?: (string | React.JSX.Element) | undefined
571
573
  preview?: PreviewConfig | undefined
572
- options?: unknown
573
574
  initialValue?: InitialValueProperty<any, any>
574
575
  components?:
575
576
  | {
@@ -583,7 +584,6 @@ export declare function interpolationVariables(
583
584
  preview?: ComponentType<any>
584
585
  }
585
586
  | undefined
586
- icon?: ReactNode | ComponentType
587
587
  deprecated?: DeprecatedProperty | undefined
588
588
  }
589
589
 
package/dist/index.d.ts CHANGED
@@ -107,11 +107,11 @@ export declare function interpolationVariables(
107
107
  readOnly?: ConditionalProperty
108
108
  placeholder?: string | undefined
109
109
  title?: string | undefined
110
- description?: (string | React.JSX.Element) | undefined
110
+ icon?: ReactNode | ComponentType
111
111
  options?: StringOptions | undefined
112
+ description?: (string | React.JSX.Element) | undefined
112
113
  initialValue?: InitialValueProperty<any, any>
113
114
  components?: StringComponents | undefined
114
- icon?: ReactNode | ComponentType
115
115
  deprecated?: DeprecatedProperty | undefined
116
116
  }
117
117
  | {
@@ -130,11 +130,11 @@ export declare function interpolationVariables(
130
130
  readOnly?: ConditionalProperty
131
131
  placeholder?: string | undefined
132
132
  title?: string | undefined
133
- description?: (string | React.JSX.Element) | undefined
133
+ icon?: ReactNode | ComponentType
134
134
  options?: NumberOptions | undefined
135
+ description?: (string | React.JSX.Element) | undefined
135
136
  initialValue?: InitialValueProperty<any, any>
136
137
  components?: NumberComponents | undefined
137
- icon?: ReactNode | ComponentType
138
138
  deprecated?: DeprecatedProperty | undefined
139
139
  }
140
140
  | {
@@ -152,11 +152,11 @@ export declare function interpolationVariables(
152
152
  | undefined
153
153
  readOnly?: ConditionalProperty
154
154
  title?: string | undefined
155
- description?: (string | React.JSX.Element) | undefined
155
+ icon?: ReactNode | ComponentType
156
156
  options?: BooleanOptions | undefined
157
+ description?: (string | React.JSX.Element) | undefined
157
158
  initialValue?: InitialValueProperty<any, any>
158
159
  components?: BooleanComponents | undefined
159
- icon?: ReactNode | ComponentType
160
160
  deprecated?: DeprecatedProperty | undefined
161
161
  }
162
162
  | {
@@ -175,14 +175,14 @@ export declare function interpolationVariables(
175
175
  readOnly?: ConditionalProperty
176
176
  groups?: FieldGroupDefinition[] | undefined
177
177
  title?: string | undefined
178
+ icon?: ReactNode | ComponentType
179
+ options?: ObjectOptions | undefined
178
180
  description?: (string | React.JSX.Element) | undefined
179
181
  fields: FieldDefinition[]
180
182
  fieldsets?: FieldsetDefinition[] | undefined
181
- options?: ObjectOptions | undefined
182
183
  initialValue?: InitialValueProperty<any, any>
183
184
  components?: ObjectComponents | undefined
184
185
  renderMembers?: ObjectRenderMembersCallback | undefined
185
- icon?: ReactNode | ComponentType
186
186
  deprecated?: DeprecatedProperty | undefined
187
187
  }
188
188
  | {
@@ -200,20 +200,69 @@ export declare function interpolationVariables(
200
200
  | undefined
201
201
  readOnly?: ConditionalProperty
202
202
  title?: string | undefined
203
- description?: (string | React.JSX.Element) | undefined
203
+ icon?: ReactNode | ComponentType
204
204
  options?: BlockOptions | undefined
205
+ description?: (string | React.JSX.Element) | undefined
205
206
  initialValue?: InitialValueProperty<any, any>
206
207
  components?:
207
208
  | {
208
209
  block?: ComponentType<BlockProps>
209
210
  }
210
211
  | undefined
211
- icon?: ReactNode | ComponentType
212
212
  deprecated?: DeprecatedProperty | undefined
213
213
  styles?: BlockStyleDefinition[] | undefined
214
214
  lists?: BlockListDefinition[] | undefined
215
215
  marks?: BlockMarksDefinition | undefined
216
216
  }
217
+ | {
218
+ of: unknown[]
219
+ type: 'image'
220
+ name?: string | undefined
221
+ validation?:
222
+ | (ValidationBuilder<ImageRule, ImageValue> &
223
+ (
224
+ | false
225
+ | Rule
226
+ | SchemaValidationValue[]
227
+ | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
228
+ ))
229
+ | undefined
230
+ readOnly?: ConditionalProperty
231
+ title?: string | undefined
232
+ icon?: ReactNode | ComponentType
233
+ options?: ImageOptions | undefined
234
+ description?: (string | React.JSX.Element) | undefined
235
+ fields?: FieldDefinition[] | undefined
236
+ fieldsets?: FieldsetDefinition[] | undefined
237
+ initialValue?: InitialValueProperty<any, any>
238
+ components?: ImageComponents | undefined
239
+ renderMembers?: ObjectRenderMembersCallback | undefined
240
+ deprecated?: DeprecatedProperty | undefined
241
+ }
242
+ | {
243
+ of: unknown[]
244
+ type: 'text'
245
+ name?: string | undefined
246
+ validation?:
247
+ | (ValidationBuilder<TextRule, string> &
248
+ (
249
+ | false
250
+ | Rule
251
+ | SchemaValidationValue[]
252
+ | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
253
+ ))
254
+ | undefined
255
+ readOnly?: ConditionalProperty
256
+ placeholder?: string | undefined
257
+ rows?: number | undefined
258
+ title?: string | undefined
259
+ icon?: ReactNode | ComponentType
260
+ options?: TextOptions | undefined
261
+ description?: (string | React.JSX.Element) | undefined
262
+ initialValue?: InitialValueProperty<any, any>
263
+ components?: TextComponents | undefined
264
+ deprecated?: DeprecatedProperty | undefined
265
+ }
217
266
  | {
218
267
  of: unknown[]
219
268
  type: 'array'
@@ -229,11 +278,11 @@ export declare function interpolationVariables(
229
278
  | undefined
230
279
  readOnly?: ConditionalProperty
231
280
  title?: string | undefined
232
- description?: (string | React.JSX.Element) | undefined
281
+ icon?: ReactNode | ComponentType
233
282
  options?: ArrayOptions | undefined
283
+ description?: (string | React.JSX.Element) | undefined
234
284
  initialValue?: InitialValueProperty<any, any>
235
285
  components?: (ArrayOfObjectsComponents | ArrayOfPrimitivesComponents) | undefined
236
- icon?: ReactNode | ComponentType
237
286
  deprecated?: DeprecatedProperty | undefined
238
287
  }
239
288
  | {
@@ -252,11 +301,11 @@ export declare function interpolationVariables(
252
301
  readOnly?: ConditionalProperty
253
302
  placeholder?: string | undefined
254
303
  title?: string | undefined
255
- description?: (string | React.JSX.Element) | undefined
304
+ icon?: ReactNode | ComponentType
256
305
  options?: DateOptions | undefined
306
+ description?: (string | React.JSX.Element) | undefined
257
307
  initialValue?: InitialValueProperty<any, any>
258
308
  components?: DateComponents | undefined
259
- icon?: ReactNode | ComponentType
260
309
  deprecated?: DeprecatedProperty | undefined
261
310
  }
262
311
  | {
@@ -275,11 +324,11 @@ export declare function interpolationVariables(
275
324
  readOnly?: ConditionalProperty
276
325
  placeholder?: string | undefined
277
326
  title?: string | undefined
278
- description?: (string | React.JSX.Element) | undefined
327
+ icon?: ReactNode | ComponentType
279
328
  options?: DatetimeOptions | undefined
329
+ description?: (string | React.JSX.Element) | undefined
280
330
  initialValue?: InitialValueProperty<any, any>
281
331
  components?: DatetimeComponents | undefined
282
- icon?: ReactNode | ComponentType
283
332
  deprecated?: DeprecatedProperty | undefined
284
333
  }
285
334
  | {
@@ -298,14 +347,14 @@ export declare function interpolationVariables(
298
347
  readOnly?: ConditionalProperty
299
348
  groups?: FieldGroupDefinition[] | undefined
300
349
  title?: string | undefined
350
+ icon?: ReactNode | ComponentType
351
+ options?: DocumentOptions | undefined
301
352
  description?: (string | React.JSX.Element) | undefined
302
353
  fields: FieldDefinition[]
303
354
  fieldsets?: FieldsetDefinition[] | undefined
304
- options?: DocumentOptions | undefined
305
355
  initialValue?: InitialValueProperty<any, any>
306
356
  components?: DocumentComponents | undefined
307
357
  renderMembers?: ObjectRenderMembersCallback | undefined
308
- icon?: ReactNode | ComponentType
309
358
  deprecated?: DeprecatedProperty | undefined
310
359
  liveEdit?: boolean | undefined
311
360
  orderings?: SortOrdering[] | undefined
@@ -334,14 +383,14 @@ export declare function interpolationVariables(
334
383
  | undefined
335
384
  readOnly?: ConditionalProperty
336
385
  title?: string | undefined
386
+ icon?: ReactNode | ComponentType
387
+ options?: FileOptions | undefined
337
388
  description?: (string | React.JSX.Element) | undefined
338
389
  fields?: ObjectDefinition['fields'] | undefined
339
390
  fieldsets?: FieldsetDefinition[] | undefined
340
- options?: FileOptions | undefined
341
391
  initialValue?: InitialValueProperty<any, any>
342
392
  components?: FileComponents | undefined
343
393
  renderMembers?: ObjectRenderMembersCallback | undefined
344
- icon?: ReactNode | ComponentType
345
394
  deprecated?: DeprecatedProperty | undefined
346
395
  }
347
396
  | {
@@ -359,36 +408,11 @@ export declare function interpolationVariables(
359
408
  | undefined
360
409
  readOnly?: ConditionalProperty
361
410
  title?: string | undefined
362
- description?: (string | React.JSX.Element) | undefined
363
- options?: GeopointOptions | undefined
364
- initialValue?: InitialValueProperty<any, any>
365
- components?: GeopointComponents | undefined
366
411
  icon?: ReactNode | ComponentType
367
- deprecated?: DeprecatedProperty | undefined
368
- }
369
- | {
370
- of: unknown[]
371
- type: 'image'
372
- name?: string | undefined
373
- validation?:
374
- | (ValidationBuilder<ImageRule, ImageValue> &
375
- (
376
- | false
377
- | Rule
378
- | SchemaValidationValue[]
379
- | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
380
- ))
381
- | undefined
382
- readOnly?: ConditionalProperty
383
- title?: string | undefined
412
+ options?: GeopointOptions | undefined
384
413
  description?: (string | React.JSX.Element) | undefined
385
- fields?: FieldDefinition[] | undefined
386
- fieldsets?: FieldsetDefinition[] | undefined
387
- options?: ImageOptions | undefined
388
414
  initialValue?: InitialValueProperty<any, any>
389
- components?: ImageComponents | undefined
390
- renderMembers?: ObjectRenderMembersCallback | undefined
391
- icon?: ReactNode | ComponentType
415
+ components?: GeopointComponents | undefined
392
416
  deprecated?: DeprecatedProperty | undefined
393
417
  }
394
418
  | {
@@ -406,13 +430,13 @@ export declare function interpolationVariables(
406
430
  | undefined
407
431
  readOnly?: ConditionalProperty
408
432
  title?: string | undefined
409
- description?: (string | React.JSX.Element) | undefined
433
+ to: ReferenceTo
434
+ icon?: ReactNode | ComponentType
410
435
  options?: ReferenceOptions | undefined
436
+ description?: (string | React.JSX.Element) | undefined
411
437
  initialValue?: InitialValueProperty<any, any>
412
438
  components?: ReferenceComponents | undefined
413
- icon?: ReactNode | ComponentType
414
439
  deprecated?: DeprecatedProperty | undefined
415
- to: ReferenceTo
416
440
  weak?: boolean | undefined
417
441
  }
418
442
  | {
@@ -422,12 +446,6 @@ export declare function interpolationVariables(
422
446
  validation?: SchemaValidationValue
423
447
  readOnly?: ConditionalProperty
424
448
  title?: string | undefined
425
- description?: (string | React.JSX.Element) | undefined
426
- options?: ReferenceOptions | undefined
427
- initialValue?: InitialValueProperty<any, any>
428
- components?: CrossDatasetReferenceComponents | undefined
429
- icon?: ReactNode | ComponentType
430
- deprecated?: DeprecatedProperty | undefined
431
449
  to: {
432
450
  type: string
433
451
  title?: string
@@ -439,6 +457,12 @@ export declare function interpolationVariables(
439
457
  mapWith?: string
440
458
  }[]
441
459
  }[]
460
+ icon?: ReactNode | ComponentType
461
+ options?: ReferenceOptions | undefined
462
+ description?: (string | React.JSX.Element) | undefined
463
+ initialValue?: InitialValueProperty<any, any>
464
+ components?: CrossDatasetReferenceComponents | undefined
465
+ deprecated?: DeprecatedProperty | undefined
442
466
  weak?: boolean | undefined
443
467
  dataset: string
444
468
  studioUrl?: ((document: {id: string; type?: string}) => string | null) | undefined
@@ -452,17 +476,17 @@ export declare function interpolationVariables(
452
476
  validation?: SchemaValidationValue
453
477
  readOnly?: ConditionalProperty
454
478
  title?: string | undefined
455
- description?: (string | React.JSX.Element) | undefined
456
- options?: ReferenceOptions | undefined
457
- initialValue?: InitialValueProperty<any, any>
458
- icon?: ReactNode | ComponentType
459
- deprecated?: DeprecatedProperty | undefined
460
479
  to: {
461
480
  type: string
462
481
  title?: string
463
482
  icon?: ComponentType
464
483
  preview?: PreviewConfig
465
484
  }[]
485
+ icon?: ReactNode | ComponentType
486
+ options?: ReferenceOptions | undefined
487
+ description?: (string | React.JSX.Element) | undefined
488
+ initialValue?: InitialValueProperty<any, any>
489
+ deprecated?: DeprecatedProperty | undefined
466
490
  weak?: boolean | undefined
467
491
  studioUrl?: string | ((document: {id: string; type?: string}) => string | null) | undefined
468
492
  resourceType: string
@@ -483,35 +507,11 @@ export declare function interpolationVariables(
483
507
  | undefined
484
508
  readOnly?: ConditionalProperty
485
509
  title?: string | undefined
486
- description?: (string | React.JSX.Element) | undefined
487
- options?: SlugOptions | undefined
488
- initialValue?: InitialValueProperty<any, any>
489
- components?: SlugComponents | undefined
490
510
  icon?: ReactNode | ComponentType
491
- deprecated?: DeprecatedProperty | undefined
492
- }
493
- | {
494
- of: unknown[]
495
- type: 'text'
496
- name?: string | undefined
497
- validation?:
498
- | (ValidationBuilder<TextRule, string> &
499
- (
500
- | false
501
- | Rule
502
- | SchemaValidationValue[]
503
- | ((rule: Rule, context?: ValidationContext) => SchemaValidationValue)
504
- ))
505
- | undefined
506
- readOnly?: ConditionalProperty
507
- placeholder?: string | undefined
508
- rows?: number | undefined
509
- title?: string | undefined
511
+ options?: SlugOptions | undefined
510
512
  description?: (string | React.JSX.Element) | undefined
511
- options?: TextOptions | undefined
512
513
  initialValue?: InitialValueProperty<any, any>
513
- components?: TextComponents | undefined
514
- icon?: ReactNode | ComponentType
514
+ components?: SlugComponents | undefined
515
515
  deprecated?: DeprecatedProperty | undefined
516
516
  }
517
517
  | {
@@ -530,11 +530,11 @@ export declare function interpolationVariables(
530
530
  readOnly?: ConditionalProperty
531
531
  placeholder?: string | undefined
532
532
  title?: string | undefined
533
- description?: (string | React.JSX.Element) | undefined
533
+ icon?: ReactNode | ComponentType
534
534
  options?: UrlOptions | undefined
535
+ description?: (string | React.JSX.Element) | undefined
535
536
  initialValue?: InitialValueProperty<any, any>
536
537
  components?: UrlComponents | undefined
537
- icon?: ReactNode | ComponentType
538
538
  deprecated?: DeprecatedProperty | undefined
539
539
  }
540
540
  | {
@@ -553,11 +553,11 @@ export declare function interpolationVariables(
553
553
  readOnly?: ConditionalProperty
554
554
  placeholder?: string | undefined
555
555
  title?: string | undefined
556
- description?: (string | React.JSX.Element) | undefined
556
+ icon?: ReactNode | ComponentType
557
557
  options?: EmailOptions | undefined
558
+ description?: (string | React.JSX.Element) | undefined
558
559
  initialValue?: InitialValueProperty<any, any>
559
560
  components?: EmailComponents | undefined
560
- icon?: ReactNode | ComponentType
561
561
  deprecated?: DeprecatedProperty | undefined
562
562
  }
563
563
  | {
@@ -567,9 +567,10 @@ export declare function interpolationVariables(
567
567
  validation?: SchemaValidationValue
568
568
  readOnly?: ConditionalProperty
569
569
  title?: string | undefined
570
+ icon?: ReactNode | ComponentType
571
+ options?: unknown
570
572
  description?: (string | React.JSX.Element) | undefined
571
573
  preview?: PreviewConfig | undefined
572
- options?: unknown
573
574
  initialValue?: InitialValueProperty<any, any>
574
575
  components?:
575
576
  | {
@@ -583,7 +584,6 @@ export declare function interpolationVariables(
583
584
  preview?: ComponentType<any>
584
585
  }
585
586
  | undefined
586
- icon?: ReactNode | ComponentType
587
587
  deprecated?: DeprecatedProperty | undefined
588
588
  }
589
589
 
package/dist/index.js CHANGED
@@ -1,15 +1,45 @@
1
- import { TagIcon } from "@sanity/icons";
2
- import { defineArrayMember, defineField } from "sanity";
1
+ import { SearchIcon, TagIcon } from "@sanity/icons";
2
+ import { set, unset, defineArrayMember, defineField } from "sanity";
3
3
  import { jsx, Fragment, jsxs } from "react/jsx-runtime";
4
- import { Box, Text } from "@sanity/ui";
4
+ import { Box, Text, Card, Autocomplete } from "@sanity/ui";
5
+ import { useId, useCallback } from "react";
5
6
  function VariableKeyField(props) {
6
7
  return /* @__PURE__ */ jsx(Fragment, { children: props.children });
7
8
  }
8
9
  function createVariableKeyInput(variables) {
10
+ const options = variables.map((variable) => ({
11
+ value: variable.id,
12
+ name: variable.name,
13
+ description: variable.description
14
+ }));
9
15
  return function(props) {
10
- const selectedVariable = variables.find((variable) => variable.id === props.value);
16
+ const autocompleteId = useId(), selectedVariable = variables.find((variable) => variable.id === props.value), handleChange = useCallback(
17
+ (selectedValue) => {
18
+ props.onChange(selectedValue ? set(selectedValue) : unset());
19
+ },
20
+ [props]
21
+ ), filterOption = useCallback((query, option) => option.name.toLowerCase().includes(query.toLowerCase()), []), renderOption = useCallback((option) => /* @__PURE__ */ jsxs(Card, { as: "button", padding: 3, children: [
22
+ /* @__PURE__ */ jsx(Text, { size: 1, weight: "medium", children: option.name }),
23
+ option.description && /* @__PURE__ */ jsx(Box, { marginTop: 2, children: /* @__PURE__ */ jsx(Text, { size: 0, muted: !0, children: option.description }) })
24
+ ] }), []), renderValue = useCallback((_value, option) => option ? option.name : options.find((candidate) => candidate.value === _value)?.name ?? _value, []);
11
25
  return /* @__PURE__ */ jsxs(Fragment, { children: [
12
- props.renderDefault(props),
26
+ /* @__PURE__ */ jsx(
27
+ Autocomplete,
28
+ {
29
+ id: autocompleteId,
30
+ options,
31
+ value: typeof props.value == "string" ? props.value : void 0,
32
+ onChange: handleChange,
33
+ filterOption,
34
+ renderOption,
35
+ renderValue,
36
+ openButton: !0,
37
+ icon: SearchIcon,
38
+ placeholder: "Search variables...",
39
+ fontSize: 1,
40
+ padding: 3
41
+ }
42
+ ),
13
43
  selectedVariable?.description && /* @__PURE__ */ jsx(Box, { marginTop: 2, children: /* @__PURE__ */ jsx(Text, { size: 1, muted: !0, children: selectedVariable.description }) })
14
44
  ] });
15
45
  };
@@ -38,12 +68,6 @@ function interpolationVariables(variables, block) {
38
68
  name: "variableKey",
39
69
  title: "Variable",
40
70
  type: "string",
41
- options: {
42
- list: variables.map((variable) => ({
43
- title: variable.name,
44
- value: variable.id
45
- }))
46
- },
47
71
  validation: (rule) => rule.required(),
48
72
  components: {
49
73
  field: VariableKeyField,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/components/VariableInlineBlock.tsx","../src/interpolationVariables.ts"],"sourcesContent":["import {Box, Text} from '@sanity/ui'\nimport type {BlockProps, FieldProps, InputProps} from 'sanity'\nimport type {InterpolationVariable} from '../types'\n\nexport function VariableKeyField(props: FieldProps) {\n return <>{props.children}</>\n}\n\nexport function createVariableKeyInput(variables: InterpolationVariable[]) {\n return function VariableKeyInput(props: InputProps) {\n const selectedVariable = variables.find((variable) => variable.id === props.value)\n\n return (\n <>\n {props.renderDefault(props)}\n {selectedVariable?.description && (\n <Box marginTop={2}>\n <Text size={1} muted>\n {selectedVariable.description}\n </Text>\n </Box>\n )}\n </>\n )\n }\n}\n\nexport function createVariableInlineBlock(variables: InterpolationVariable[]) {\n return function VariableInlineBlock(props: BlockProps) {\n const value = props.value as {variableKey?: string}\n const variableKey = value?.variableKey\n const variable = variables.find((candidate) => candidate.id === variableKey)\n\n return props.renderDefault({\n ...props,\n renderPreview: () => (\n <Box padding={2}>\n <Text size={0} weight=\"medium\">\n {variable?.name ?? variableKey ?? 'Select variable'}\n </Text>\n </Box>\n ),\n })\n }\n}\n","import {TagIcon} from '@sanity/icons'\nimport {defineArrayMember, defineField} from 'sanity'\nimport {\n createVariableInlineBlock,\n createVariableKeyInput,\n VariableKeyField,\n} from './components/VariableInlineBlock'\nimport type {InterpolationVariable} from './types'\n\n/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n\n/** @public */\nexport function interpolationVariables(\n variables: InterpolationVariable[],\n block?: ReturnType<typeof defineArrayMember>,\n) {\n const variableType = defineArrayMember({\n type: 'object',\n name: VARIABLE_TYPE_PREFIX,\n title: 'Variable',\n icon: TagIcon,\n options: {\n modal: {width: 0},\n },\n fields: [\n defineField({\n name: 'variableKey',\n title: 'Variable',\n type: 'string',\n options: {\n list: variables.map((variable) => ({\n title: variable.name,\n value: variable.id,\n })),\n },\n validation: (rule) => rule.required(),\n components: {\n field: VariableKeyField,\n input: createVariableKeyInput(variables),\n },\n }),\n ],\n components: {\n inlineBlock: createVariableInlineBlock(variables),\n },\n })\n\n const baseBlock = block ?? defineArrayMember({type: 'block'})\n\n return {\n ...baseBlock,\n of: [...((baseBlock as {of?: unknown[]}).of ?? []), variableType],\n }\n}\n"],"names":[],"mappings":";;;;AAIO,SAAS,iBAAiB,OAAmB;AAClD,SAAO,oBAAA,UAAA,EAAG,gBAAM,SAAA,CAAS;AAC3B;AAEO,SAAS,uBAAuB,WAAoC;AACzE,SAAO,SAA0B,OAAmB;AAClD,UAAM,mBAAmB,UAAU,KAAK,CAAC,aAAa,SAAS,OAAO,MAAM,KAAK;AAEjF,WACE,qBAAA,UAAA,EACG,UAAA;AAAA,MAA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,kBAAkB,EAAC,MAAM,SAAQ;AAE5D,SAAO;AAAA,IACL,GAAG;AAAA,IACH,IAAI,CAAC,GAAK,UAA+B,MAAM,CAAA,GAAK,YAAY;AAAA,EAAA;AAEpE;"}
1
+ {"version":3,"file":"index.js","sources":["../src/components/VariableInlineBlock.tsx","../src/interpolationVariables.ts"],"sourcesContent":["import {SearchIcon} from '@sanity/icons'\nimport {Autocomplete, Box, Card, Text} from '@sanity/ui'\nimport {useCallback, useId} from 'react'\nimport {set, unset} from 'sanity'\nimport type {BlockProps, FieldProps, InputProps} from 'sanity'\nimport type {InterpolationVariable} from '../types'\n\ninterface VariableOption {\n value: string\n name: string\n description?: string\n}\n\nexport function VariableKeyField(props: FieldProps) {\n return <>{props.children}</>\n}\n\nexport function createVariableKeyInput(variables: InterpolationVariable[]) {\n const options: VariableOption[] = variables.map((variable) => ({\n value: variable.id,\n name: variable.name,\n description: variable.description,\n }))\n\n return function VariableKeyInput(props: InputProps) {\n const autocompleteId = useId()\n const selectedVariable = variables.find((variable) => variable.id === props.value)\n\n const handleChange = useCallback(\n (selectedValue: string) => {\n props.onChange(selectedValue ? set(selectedValue) : unset())\n },\n [props],\n )\n\n const filterOption = useCallback((query: string, option: VariableOption) => {\n return option.name.toLowerCase().includes(query.toLowerCase())\n }, [])\n\n const renderOption = useCallback((option: VariableOption) => {\n return (\n <Card as=\"button\" padding={3}>\n <Text size={1} weight=\"medium\">\n {option.name}\n </Text>\n {option.description && (\n <Box marginTop={2}>\n <Text size={0} muted>\n {option.description}\n </Text>\n </Box>\n )}\n </Card>\n )\n }, [])\n\n const renderValue = useCallback((_value: string, option?: VariableOption) => {\n if (option) return option.name\n const matchedOption = options.find((candidate) => candidate.value === _value)\n return matchedOption?.name ?? _value\n }, [])\n\n return (\n <>\n <Autocomplete\n id={autocompleteId}\n options={options}\n value={typeof props.value === 'string' ? props.value : undefined}\n onChange={handleChange}\n filterOption={filterOption}\n renderOption={renderOption}\n renderValue={renderValue}\n openButton\n icon={SearchIcon}\n placeholder=\"Search variables...\"\n fontSize={1}\n padding={3}\n />\n {selectedVariable?.description && (\n <Box marginTop={2}>\n <Text size={1} muted>\n {selectedVariable.description}\n </Text>\n </Box>\n )}\n </>\n )\n }\n}\n\nexport function createVariableInlineBlock(variables: InterpolationVariable[]) {\n return function VariableInlineBlock(props: BlockProps) {\n const value = props.value as {variableKey?: string}\n const variableKey = value?.variableKey\n const variable = variables.find((candidate) => candidate.id === variableKey)\n\n return props.renderDefault({\n ...props,\n renderPreview: () => (\n <Box padding={2}>\n <Text size={0} weight=\"medium\">\n {variable?.name ?? variableKey ?? 'Select variable'}\n </Text>\n </Box>\n ),\n })\n }\n}\n","import {TagIcon} from '@sanity/icons'\nimport {defineArrayMember, defineField} from 'sanity'\nimport {\n createVariableInlineBlock,\n createVariableKeyInput,\n VariableKeyField,\n} from './components/VariableInlineBlock'\nimport type {InterpolationVariable} from './types'\n\n/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n\n/** @public */\nexport function interpolationVariables(\n variables: InterpolationVariable[],\n block?: ReturnType<typeof defineArrayMember>,\n) {\n const variableType = defineArrayMember({\n type: 'object',\n name: VARIABLE_TYPE_PREFIX,\n title: 'Variable',\n icon: TagIcon,\n options: {\n modal: {width: 0},\n },\n fields: [\n defineField({\n name: 'variableKey',\n title: 'Variable',\n type: 'string',\n validation: (rule) => rule.required(),\n components: {\n field: VariableKeyField,\n input: createVariableKeyInput(variables),\n },\n }),\n ],\n components: {\n inlineBlock: createVariableInlineBlock(variables),\n },\n })\n\n const baseBlock = block ?? defineArrayMember({type: 'block'})\n\n return {\n ...baseBlock,\n of: [...((baseBlock as {of?: unknown[]}).of ?? []), variableType],\n }\n}\n"],"names":[],"mappings":";;;;;AAaO,SAAS,iBAAiB,OAAmB;AAClD,SAAO,oBAAA,UAAA,EAAG,gBAAM,SAAA,CAAS;AAC3B;AAEO,SAAS,uBAAuB,WAAoC;AACzE,QAAM,UAA4B,UAAU,IAAI,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"name": "sanity-plugin-pte-interpolation",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Sanity Studio plugin for interpolating values into Portable Text Editor fields",
@@ -4,6 +4,15 @@ import {createVariableInlineBlock, createVariableKeyInput} from '../components/V
4
4
  import type {BlockProps, InputProps} from 'sanity'
5
5
  import type {InterpolationVariable} from '../types'
6
6
 
7
+ vi.mock('react', async () => {
8
+ const actual = await vi.importActual('react')
9
+ return {
10
+ ...actual,
11
+ useId: () => 'test-autocomplete-id',
12
+ useCallback: (callback: unknown) => callback,
13
+ }
14
+ })
15
+
7
16
  const testVariables: InterpolationVariable[] = [
8
17
  {id: 'firstName', name: 'First name', description: 'First name of the recipient'},
9
18
  {id: 'email', name: 'Email address'},
@@ -19,6 +28,21 @@ function findTextInElement(element: unknown): string[] {
19
28
  return findTextInElement(children)
20
29
  }
21
30
 
31
+ function findPropInElement(element: unknown, propName: string): unknown {
32
+ if (!element || typeof element !== 'object') return undefined
33
+ const el = element as {props?: Record<string, unknown>}
34
+ if (!el.props) return undefined
35
+ if (propName in el.props) return el.props[propName]
36
+ const {children} = el.props
37
+ if (Array.isArray(children)) {
38
+ for (const child of children) {
39
+ const found = findPropInElement(child, propName)
40
+ if (found !== undefined) return found
41
+ }
42
+ }
43
+ return findPropInElement(children, propName)
44
+ }
45
+
22
46
  describe('createVariableInlineBlock', () => {
23
47
  function captureRenderPreview(variableKey: string | undefined): ReactElement | undefined {
24
48
  const VariableInlineBlock = createVariableInlineBlock(testVariables)
@@ -54,7 +78,7 @@ describe('createVariableKeyInput', () => {
54
78
  const VariableKeyInput = createVariableKeyInput(testVariables)
55
79
  return VariableKeyInput({
56
80
  value,
57
- renderDefault: vi.fn(() => null),
81
+ onChange: vi.fn(),
58
82
  } as unknown as InputProps) as ReactElement
59
83
  }
60
84
 
@@ -63,10 +87,22 @@ describe('createVariableKeyInput', () => {
63
87
  })
64
88
 
65
89
  it('does not include description when the selected variable has none', () => {
66
- expect(findTextInElement(renderInput('email'))).toHaveLength(0)
90
+ const element = renderInput('email')
91
+ const allText = findTextInElement(element)
92
+ const textsOutsideAutocomplete = allText.filter((text) => text !== 'Search variables...')
93
+ expect(textsOutsideAutocomplete).toHaveLength(0)
67
94
  })
68
95
 
69
96
  it('does not include description when no variable is selected', () => {
70
- expect(findTextInElement(renderInput(undefined))).toHaveLength(0)
97
+ const element = renderInput(undefined)
98
+ const allText = findTextInElement(element)
99
+ const textsOutsideAutocomplete = allText.filter((text) => text !== 'Search variables...')
100
+ expect(textsOutsideAutocomplete).toHaveLength(0)
101
+ })
102
+
103
+ it('renders a search placeholder', () => {
104
+ const element = renderInput(undefined)
105
+ const placeholder = findPropInElement(element, 'placeholder')
106
+ expect(placeholder).toBe('Search variables...')
71
107
  })
72
108
  })
@@ -39,7 +39,7 @@ describe('interpolationVariables', () => {
39
39
  expect(ofArray[0].icon).toBeDefined()
40
40
  })
41
41
 
42
- it('should have a variableKey string field with options.list matching the provided variables', () => {
42
+ it('should have a variableKey string field without options.list', () => {
43
43
  const result = interpolationVariables(testVariables)
44
44
  const ofArray = result.of as Array<{fields?: Array<Record<string, unknown>>}>
45
45
  const fields = ofArray[0].fields
@@ -48,13 +48,8 @@ describe('interpolationVariables', () => {
48
48
  expect(fields?.[0]).toMatchObject({
49
49
  name: 'variableKey',
50
50
  type: 'string',
51
- options: {
52
- list: [
53
- {title: 'First name', value: 'firstName'},
54
- {title: 'Email address', value: 'email'},
55
- ],
56
- },
57
51
  })
52
+ expect((fields?.[0] as Record<string, unknown>)?.options).toBeUndefined()
58
53
  })
59
54
 
60
55
  it('should set required validation on the variableKey field', () => {
@@ -1,18 +1,81 @@
1
- import {Box, Text} from '@sanity/ui'
1
+ import {SearchIcon} from '@sanity/icons'
2
+ import {Autocomplete, Box, Card, Text} from '@sanity/ui'
3
+ import {useCallback, useId} from 'react'
4
+ import {set, unset} from 'sanity'
2
5
  import type {BlockProps, FieldProps, InputProps} from 'sanity'
3
6
  import type {InterpolationVariable} from '../types'
4
7
 
8
+ interface VariableOption {
9
+ value: string
10
+ name: string
11
+ description?: string
12
+ }
13
+
5
14
  export function VariableKeyField(props: FieldProps) {
6
15
  return <>{props.children}</>
7
16
  }
8
17
 
9
18
  export function createVariableKeyInput(variables: InterpolationVariable[]) {
19
+ const options: VariableOption[] = variables.map((variable) => ({
20
+ value: variable.id,
21
+ name: variable.name,
22
+ description: variable.description,
23
+ }))
24
+
10
25
  return function VariableKeyInput(props: InputProps) {
26
+ const autocompleteId = useId()
11
27
  const selectedVariable = variables.find((variable) => variable.id === props.value)
12
28
 
29
+ const handleChange = useCallback(
30
+ (selectedValue: string) => {
31
+ props.onChange(selectedValue ? set(selectedValue) : unset())
32
+ },
33
+ [props],
34
+ )
35
+
36
+ const filterOption = useCallback((query: string, option: VariableOption) => {
37
+ return option.name.toLowerCase().includes(query.toLowerCase())
38
+ }, [])
39
+
40
+ const renderOption = useCallback((option: VariableOption) => {
41
+ return (
42
+ <Card as="button" padding={3}>
43
+ <Text size={1} weight="medium">
44
+ {option.name}
45
+ </Text>
46
+ {option.description && (
47
+ <Box marginTop={2}>
48
+ <Text size={0} muted>
49
+ {option.description}
50
+ </Text>
51
+ </Box>
52
+ )}
53
+ </Card>
54
+ )
55
+ }, [])
56
+
57
+ const renderValue = useCallback((_value: string, option?: VariableOption) => {
58
+ if (option) return option.name
59
+ const matchedOption = options.find((candidate) => candidate.value === _value)
60
+ return matchedOption?.name ?? _value
61
+ }, [])
62
+
13
63
  return (
14
64
  <>
15
- {props.renderDefault(props)}
65
+ <Autocomplete
66
+ id={autocompleteId}
67
+ options={options}
68
+ value={typeof props.value === 'string' ? props.value : undefined}
69
+ onChange={handleChange}
70
+ filterOption={filterOption}
71
+ renderOption={renderOption}
72
+ renderValue={renderValue}
73
+ openButton
74
+ icon={SearchIcon}
75
+ placeholder="Search variables..."
76
+ fontSize={1}
77
+ padding={3}
78
+ />
16
79
  {selectedVariable?.description && (
17
80
  <Box marginTop={2}>
18
81
  <Text size={1} muted>
@@ -28,12 +28,6 @@ export function interpolationVariables(
28
28
  name: 'variableKey',
29
29
  title: 'Variable',
30
30
  type: 'string',
31
- options: {
32
- list: variables.map((variable) => ({
33
- title: variable.name,
34
- value: variable.id,
35
- })),
36
- },
37
31
  validation: (rule) => rule.required(),
38
32
  components: {
39
33
  field: VariableKeyField,