@sanity/assist 5.0.4 → 6.0.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.
Files changed (131) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +28 -254
  3. package/dist/index.d.ts +322 -410
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +3205 -2670
  6. package/dist/index.js.map +1 -1
  7. package/package.json +41 -81
  8. package/dist/index.cjs +0 -4239
  9. package/dist/index.cjs.map +0 -1
  10. package/dist/index.d.cts +0 -791
  11. package/sanity.json +0 -8
  12. package/src/_lib/connector/ConnectFromRegion.tsx +0 -25
  13. package/src/_lib/connector/ConnectToRegion.tsx +0 -23
  14. package/src/_lib/connector/ConnectorRegion.tsx +0 -24
  15. package/src/_lib/connector/ConnectorsProvider.tsx +0 -20
  16. package/src/_lib/connector/ConnectorsStore.ts +0 -122
  17. package/src/_lib/connector/ConnectorsStoreContext.ts +0 -5
  18. package/src/_lib/connector/helpers.ts +0 -5
  19. package/src/_lib/connector/index.ts +0 -9
  20. package/src/_lib/connector/mapConnectorToLine.ts +0 -83
  21. package/src/_lib/connector/types.ts +0 -56
  22. package/src/_lib/connector/useConnectorsStore.ts +0 -14
  23. package/src/_lib/connector/useRegionRects.ts +0 -142
  24. package/src/_lib/fixedListenQuery.ts +0 -101
  25. package/src/_lib/form/DocumentForm.tsx +0 -201
  26. package/src/_lib/form/constants.ts +0 -1
  27. package/src/_lib/form/helpers.ts +0 -32
  28. package/src/_lib/form/index.ts +0 -1
  29. package/src/_lib/randomKey.ts +0 -29
  30. package/src/_lib/useListeningQuery.ts +0 -62
  31. package/src/_lib/usePrevious.ts +0 -9
  32. package/src/assistConnectors/AssistConnectorsOverlay.tsx +0 -133
  33. package/src/assistConnectors/ConnectorPath.tsx +0 -63
  34. package/src/assistConnectors/draw/arrowPath.ts +0 -9
  35. package/src/assistConnectors/draw/connectorPath.ts +0 -142
  36. package/src/assistConnectors/index.ts +0 -1
  37. package/src/assistDocument/AssistDocumentContext.tsx +0 -51
  38. package/src/assistDocument/AssistDocumentContextProvider.tsx +0 -17
  39. package/src/assistDocument/AssistDocumentInput.tsx +0 -61
  40. package/src/assistDocument/AssistDocumentLayout.tsx +0 -12
  41. package/src/assistDocument/RequestRunInstructionProvider.tsx +0 -61
  42. package/src/assistDocument/components/AssistDocumentForm.tsx +0 -287
  43. package/src/assistDocument/components/AssistTypeContext.tsx +0 -7
  44. package/src/assistDocument/components/FieldRefPreview.tsx +0 -26
  45. package/src/assistDocument/components/InstructionsArrayField.tsx +0 -8
  46. package/src/assistDocument/components/InstructionsArrayInput.tsx +0 -27
  47. package/src/assistDocument/components/SelectedFieldContext.tsx +0 -10
  48. package/src/assistDocument/components/generic/HiddenFieldTitle.tsx +0 -5
  49. package/src/assistDocument/components/helpers.ts +0 -21
  50. package/src/assistDocument/components/instruction/BackToInstructionsLink.tsx +0 -32
  51. package/src/assistDocument/components/instruction/FieldRefInput.tsx +0 -54
  52. package/src/assistDocument/components/instruction/InstructionInput.tsx +0 -89
  53. package/src/assistDocument/components/instruction/InstructionOutputField.tsx +0 -46
  54. package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +0 -206
  55. package/src/assistDocument/components/instruction/PromptInput.tsx +0 -59
  56. package/src/assistDocument/components/instruction/appearance/IconInput.tsx +0 -46
  57. package/src/assistDocument/components/instruction/appearance/InstructionVisibility.tsx +0 -37
  58. package/src/assistDocument/hooks/useAssistDocumentContextValue.tsx +0 -127
  59. package/src/assistDocument/hooks/useDocumentState.ts +0 -6
  60. package/src/assistDocument/hooks/useInstructionToaster.tsx +0 -75
  61. package/src/assistDocument/hooks/useStudioAssistDocument.ts +0 -99
  62. package/src/assistDocument/index.ts +0 -1
  63. package/src/assistFormComponents/AssistField.tsx +0 -63
  64. package/src/assistFormComponents/AssistFormBlock.tsx +0 -31
  65. package/src/assistFormComponents/AssistInlineFormBlock.tsx +0 -13
  66. package/src/assistFormComponents/AssistItem.tsx +0 -21
  67. package/src/assistFormComponents/validation/listItem.tsx +0 -63
  68. package/src/assistFormComponents/validation/validationList.tsx +0 -90
  69. package/src/assistInspector/AssistInspector.tsx +0 -419
  70. package/src/assistInspector/FieldAutocomplete.tsx +0 -146
  71. package/src/assistInspector/InstructionTaskHistoryButton.tsx +0 -262
  72. package/src/assistInspector/constants.ts +0 -1
  73. package/src/assistInspector/helpers.ts +0 -211
  74. package/src/assistInspector/index.ts +0 -27
  75. package/src/assistLayout/AiAssistanceConfigContext.tsx +0 -32
  76. package/src/assistLayout/AiAssistanceConfigProvider.tsx +0 -98
  77. package/src/assistLayout/AssistLayout.tsx +0 -39
  78. package/src/assistLayout/RunInstructionProvider.tsx +0 -278
  79. package/src/assistLayout/fieldRefCache.tsx +0 -34
  80. package/src/assistTypes.ts +0 -83
  81. package/src/components/AssistFeatureBadge.tsx +0 -9
  82. package/src/components/FadeInContent.tsx +0 -40
  83. package/src/components/HideReferenceChangedBannerInput.tsx +0 -25
  84. package/src/components/ImageContext.tsx +0 -85
  85. package/src/components/SafeValueInput.tsx +0 -74
  86. package/src/components/TimeAgo.tsx +0 -18
  87. package/src/constants.ts +0 -20
  88. package/src/fieldActions/PrivateIcon.tsx +0 -20
  89. package/src/fieldActions/assistFieldActions.tsx +0 -320
  90. package/src/fieldActions/customFieldActions.tsx +0 -333
  91. package/src/fieldActions/generateCaptionActions.tsx +0 -77
  92. package/src/fieldActions/generateImageActions.tsx +0 -58
  93. package/src/fieldActions/useUserInput.ts +0 -107
  94. package/src/globals.d.ts +0 -4
  95. package/src/helpers/assistSupported.ts +0 -49
  96. package/src/helpers/conditionalMembers.test.ts +0 -319
  97. package/src/helpers/conditionalMembers.ts +0 -134
  98. package/src/helpers/ids.test.ts +0 -28
  99. package/src/helpers/ids.ts +0 -23
  100. package/src/helpers/misc.ts +0 -25
  101. package/src/helpers/styleguide.ts +0 -24
  102. package/src/helpers/typeUtils.ts +0 -60
  103. package/src/helpers/useAssistSupported.ts +0 -8
  104. package/src/index.ts +0 -26
  105. package/src/onboarding/FirstAssistedPathProvider.tsx +0 -30
  106. package/src/onboarding/InspectorOnboarding.tsx +0 -47
  107. package/src/onboarding/onboardingStore.ts +0 -32
  108. package/src/plugin.tsx +0 -162
  109. package/src/presence/AiFieldPresence.tsx +0 -28
  110. package/src/presence/AssistAvatar.tsx +0 -96
  111. package/src/presence/AssistDocumentPresence.tsx +0 -50
  112. package/src/presence/useAssistPresence.ts +0 -64
  113. package/src/schemas/assistDocumentSchema.tsx +0 -497
  114. package/src/schemas/contextDocumentSchema.tsx +0 -57
  115. package/src/schemas/index.ts +0 -69
  116. package/src/schemas/serialize/SchemTypeTool.tsx +0 -103
  117. package/src/schemas/serialize/schemaUtils.ts +0 -38
  118. package/src/schemas/serialize/serializeSchema.test.ts +0 -819
  119. package/src/schemas/serialize/serializeSchema.ts +0 -224
  120. package/src/schemas/serializedSchemaTypeSchema.ts +0 -60
  121. package/src/schemas/typeDefExtensions.ts +0 -127
  122. package/src/translate/FieldTranslationProvider.tsx +0 -382
  123. package/src/translate/getLanguageParams.ts +0 -26
  124. package/src/translate/languageStore.ts +0 -18
  125. package/src/translate/paths.test.ts +0 -181
  126. package/src/translate/paths.ts +0 -183
  127. package/src/translate/translateActions.tsx +0 -205
  128. package/src/translate/types.ts +0 -197
  129. package/src/types.ts +0 -220
  130. package/src/useApiClient.ts +0 -338
  131. package/v2-incompatible.js +0 -11
package/sanity.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "parts": [
3
- {
4
- "implements": "part:@sanity/base/sanity-root",
5
- "path": "./v2-incompatible.js"
6
- }
7
- ]
8
- }
@@ -1,25 +0,0 @@
1
- import {HTMLProps, useEffect, useState} from 'react'
2
-
3
- import {ConnectorRegion} from './ConnectorRegion'
4
- import {ConnectorRegionRects} from './types'
5
- import {useConnectorsStore} from './useConnectorsStore'
6
-
7
- export function ConnectFromRegion(
8
- props: {_key: string; zIndex: number} & HTMLProps<HTMLDivElement>,
9
- ) {
10
- const {children, _key: key, zIndex, ...restProps} = props
11
- const store = useConnectorsStore()
12
- const [rects, setRects] = useState<ConnectorRegionRects | null>(null)
13
-
14
- useEffect(() => store.from.subscribe(key, {zIndex}), [key, store, zIndex])
15
-
16
- useEffect(() => {
17
- if (rects) store.from.next(key, rects)
18
- }, [key, rects, store])
19
-
20
- return (
21
- <ConnectorRegion {...restProps} onRectsChange={setRects}>
22
- {children}
23
- </ConnectorRegion>
24
- )
25
- }
@@ -1,23 +0,0 @@
1
- import {HTMLProps, useEffect, useState} from 'react'
2
-
3
- import {ConnectorRegion} from './ConnectorRegion'
4
- import {ConnectorRegionRects} from './types'
5
- import {useConnectorsStore} from './useConnectorsStore'
6
-
7
- export function ConnectToRegion(props: {_key: string} & HTMLProps<HTMLDivElement>) {
8
- const {children, _key: key, ...restProps} = props
9
- const aiConnectors = useConnectorsStore()
10
- const [rects, setRects] = useState<ConnectorRegionRects | null>(null)
11
-
12
- useEffect(() => aiConnectors.to.subscribe(key), [aiConnectors, key])
13
-
14
- useEffect(() => {
15
- if (rects) aiConnectors.to.next(key, rects)
16
- }, [aiConnectors, key, rects])
17
-
18
- return (
19
- <ConnectorRegion {...restProps} onRectsChange={setRects}>
20
- {children}
21
- </ConnectorRegion>
22
- )
23
- }
@@ -1,24 +0,0 @@
1
- import {HTMLProps, useEffect} from 'react'
2
-
3
- import {ConnectorRegionRects} from './types'
4
- import {useRegionRects} from './useRegionRects'
5
-
6
- export function ConnectorRegion(
7
- props: {
8
- onRectsChange?: (rects: ConnectorRegionRects | null) => void
9
- } & HTMLProps<HTMLDivElement>,
10
- ) {
11
- const {children, onRectsChange, ...restProps} = props
12
-
13
- const {bounds, element, ref} = useRegionRects()
14
-
15
- useEffect(() => {
16
- onRectsChange?.(bounds && element ? {bounds, element} : null)
17
- }, [bounds, element, onRectsChange])
18
-
19
- return (
20
- <div {...restProps} ref={ref}>
21
- {children}
22
- </div>
23
- )
24
- }
@@ -1,20 +0,0 @@
1
- import {ReactNode, useEffect, useMemo} from 'react'
2
-
3
- import {ConnectorsStore, createConnectorsStore} from './ConnectorsStore'
4
- import {ConnectorsStoreContext} from './ConnectorsStoreContext'
5
- import {Connector} from './types'
6
-
7
- export function ConnectorsProvider(props: {
8
- children?: ReactNode
9
- onConnectorsChange?: (connectors: Connector[]) => void
10
- }) {
11
- const {children, onConnectorsChange} = props
12
- const store: ConnectorsStore = useMemo(() => createConnectorsStore(), [])
13
-
14
- useEffect(
15
- () => onConnectorsChange && store.connectors.subscribe(onConnectorsChange),
16
- [onConnectorsChange, store],
17
- )
18
-
19
- return <ConnectorsStoreContext.Provider value={store}>{children}</ConnectorsStoreContext.Provider>
20
- }
@@ -1,122 +0,0 @@
1
- import {Connector, ConnectorRegionRects} from './types'
2
-
3
- export interface ConnectorsStore {
4
- connectors: {
5
- subscribe: (observer: (connectors: Connector[]) => void) => () => void
6
- }
7
-
8
- from: {
9
- subscribe: (key: string, payload?: Record<string, unknown>) => () => void
10
- next: (key: string, rects: ConnectorRegionRects) => void
11
- }
12
-
13
- to: {
14
- subscribe: (key: string, payload?: Record<string, unknown>) => () => void
15
- next: (key: string, rects: ConnectorRegionRects) => void
16
- }
17
- }
18
-
19
- export function createConnectorsStore(): ConnectorsStore {
20
- const configKeys: string[] = []
21
- const fieldKeys: string[] = []
22
-
23
- const channels = {
24
- from: new Map<string, ConnectorRegionRects | null>(),
25
- to: new Map<string, ConnectorRegionRects | null>(),
26
- }
27
-
28
- const payloads = {
29
- from: new Map<string, Record<string, unknown> | undefined>(),
30
- to: new Map<string, Record<string, unknown> | undefined>(),
31
- }
32
-
33
- const observers: ((connectors: Connector[]) => void)[] = []
34
-
35
- function notifyObservers() {
36
- const connectors: Connector[] = []
37
-
38
- for (const key of configKeys) {
39
- const toRects = channels.to.get(key)
40
- const toPayload = payloads.from.get(key)
41
-
42
- const fromRects = channels.from.get(key)
43
- const fromPayload = payloads.from.get(key)
44
-
45
- if (toRects && fromRects) {
46
- connectors.push({
47
- key,
48
- from: {...fromRects, payload: fromPayload},
49
- to: {...toRects, payload: toPayload},
50
- })
51
- }
52
- }
53
-
54
- for (const observer of observers) {
55
- observer(connectors)
56
- }
57
- }
58
-
59
- return {
60
- to: {
61
- subscribe(key, payload) {
62
- channels.to.set(key, null)
63
- payloads.to.set(key, payload)
64
-
65
- configKeys.push(key)
66
-
67
- return () => {
68
- channels.to.delete(key)
69
- payloads.to.delete(key)
70
-
71
- const idx = configKeys.indexOf(key)
72
-
73
- if (idx > -1) configKeys.splice(idx, 1)
74
-
75
- notifyObservers()
76
- }
77
- },
78
- next(key, rects) {
79
- channels.to.set(key, rects)
80
-
81
- if (fieldKeys.includes(key)) notifyObservers()
82
- },
83
- },
84
-
85
- connectors: {
86
- subscribe(observer) {
87
- observers.push(observer)
88
-
89
- return () => {
90
- const idx = observers.indexOf(observer)
91
-
92
- if (idx > -1) observers.splice(idx, 1)
93
- }
94
- },
95
- },
96
-
97
- from: {
98
- subscribe(key, payload) {
99
- channels.from.set(key, null)
100
- payloads.from.set(key, payload)
101
-
102
- fieldKeys.push(key)
103
-
104
- return () => {
105
- channels.from.delete(key)
106
- payloads.from.delete(key)
107
-
108
- const idx = fieldKeys.indexOf(key)
109
-
110
- if (idx > -1) fieldKeys.splice(idx, 1)
111
-
112
- notifyObservers()
113
- }
114
- },
115
- next(key, rects) {
116
- channels.from.set(key, rects)
117
-
118
- if (configKeys.includes(key)) notifyObservers()
119
- },
120
- },
121
- }
122
- }
@@ -1,5 +0,0 @@
1
- import {createContext} from 'react'
2
-
3
- import {ConnectorsStore} from './ConnectorsStore'
4
-
5
- export const ConnectorsStoreContext = createContext<ConnectorsStore | null>(null)
@@ -1,5 +0,0 @@
1
- export function hasOverflowScroll(el: HTMLElement): boolean {
2
- const overflow = getComputedStyle(el).overflow
3
-
4
- return overflow.includes('auto') || overflow.includes('hidden') || overflow.includes('scroll')
5
- }
@@ -1,9 +0,0 @@
1
- export * from './ConnectFromRegion'
2
- export * from './ConnectorRegion'
3
- export * from './ConnectorsProvider'
4
- export * from './ConnectorsStore'
5
- export * from './ConnectorsStoreContext'
6
- export * from './ConnectToRegion'
7
- export * from './mapConnectorToLine'
8
- export * from './types'
9
- export * from './useConnectorsStore'
@@ -1,83 +0,0 @@
1
- import {
2
- ConnectorLine,
3
- ConnectorLinePoint,
4
- ConnectorOptions,
5
- ConnectorRegionRects,
6
- Rect,
7
- } from './types'
8
-
9
- function getConnectorLinePoint(
10
- options: ConnectorOptions,
11
- rect: Rect,
12
- bounds: Rect,
13
- ): ConnectorLinePoint {
14
- const centerY = rect.y + rect.h / 2
15
- const isAbove = rect.y + rect.h < bounds.y + options.arrow.marginY
16
- const isBelow = rect.y > bounds.y + bounds.h - options.arrow.marginY
17
-
18
- return {
19
- bounds,
20
- x: rect.x,
21
- y: centerY,
22
- centerY,
23
- startY: rect.y + options.path.marginY,
24
- endY: rect.y + rect.h - options.path.marginY,
25
- isAbove,
26
- isBelow,
27
- outOfBounds: isAbove || isBelow,
28
- }
29
- }
30
-
31
- export function mapConnectorToLine(
32
- options: ConnectorOptions,
33
- connector: {from: ConnectorRegionRects; to: ConnectorRegionRects},
34
- ): ConnectorLine {
35
- const fromBounds: Rect = {
36
- y: connector.from.bounds.y + options.arrow.threshold,
37
- // bottom: connector.from.bounds.y + connector.from.bounds.h - options.arrow.threshold,
38
- x: connector.from.bounds.x,
39
- // right: connector.from.bounds.x + connector.from.bounds.w,
40
- w: connector.from.bounds.w,
41
- h: connector.from.bounds.h - options.arrow.threshold * 2,
42
- }
43
-
44
- const from = getConnectorLinePoint(options, connector.from.element, fromBounds)
45
- from.x = connector.from.element.x + connector.from.element.w // + 1
46
-
47
- const fromBottom = fromBounds.y + fromBounds.h
48
-
49
- const toBounds: Rect = {
50
- y: connector.to.bounds.y + options.arrow.threshold,
51
- // bottom: connector.to.bounds.y + connector.to.bounds.h - options.arrow.threshold,
52
- x: connector.to.bounds.x,
53
- // right: connector.to.bounds.x + connector.to.bounds.w,
54
- w: connector.to.bounds.w,
55
- h: connector.to.bounds.h - options.arrow.threshold * 2,
56
- }
57
-
58
- const toBottom = toBounds.y + toBounds.h
59
-
60
- const to = getConnectorLinePoint(options, connector.to.element, toBounds)
61
-
62
- const maxStartY = Math.max(to.startY, from.startY)
63
-
64
- // Align from <-> to vertically
65
- from.y = Math.min(maxStartY, from.endY)
66
- if (from.y < toBounds.y) {
67
- from.y = Math.min(toBounds.y, from.endY)
68
- } else if (from.y > toBottom) {
69
- from.y = Math.max(toBottom, from.startY)
70
- }
71
- to.y = Math.min(maxStartY, to.endY)
72
- if (to.y < fromBounds.y) {
73
- to.y = Math.min(fromBounds.y, to.endY)
74
- } else if (to.y > fromBottom) {
75
- to.y = Math.max(fromBottom, to.startY)
76
- }
77
-
78
- // Keep within bounds
79
- from.y = Math.min(Math.max(from.y, fromBounds.y), fromBottom)
80
- to.y = Math.min(Math.max(to.y, toBounds.y), toBottom)
81
-
82
- return {from, to}
83
- }
@@ -1,56 +0,0 @@
1
- export interface Rect {
2
- x: number
3
- y: number
4
- w: number
5
- h: number
6
- }
7
-
8
- export interface Scroll {
9
- x: number
10
- y: number
11
- }
12
-
13
- export interface ConnectorLinePoint {
14
- bounds: Rect
15
- x: number
16
- y: number
17
- startY: number
18
- centerY: number
19
- endY: number
20
- isAbove: boolean
21
- isBelow: boolean
22
- outOfBounds: boolean
23
- }
24
-
25
- export interface ConnectorLine {
26
- from: ConnectorLinePoint
27
- to: ConnectorLinePoint
28
- }
29
-
30
- export interface ConnectorOptions {
31
- arrow: {
32
- marginX: number
33
- marginY: number
34
- size: number
35
- threshold: number
36
- }
37
- divider: {
38
- offsetX: number
39
- }
40
- path: {
41
- cornerRadius: number
42
- marginY: number
43
- strokeWidth: number
44
- }
45
- }
46
-
47
- export interface ConnectorRegionRects {
48
- bounds: Rect
49
- element: Rect
50
- }
51
-
52
- export interface Connector {
53
- key: string
54
- from: ConnectorRegionRects & {payload?: Record<string, unknown>}
55
- to: ConnectorRegionRects & {payload?: Record<string, unknown>}
56
- }
@@ -1,14 +0,0 @@
1
- import {useContext} from 'react'
2
-
3
- import {ConnectorsStore} from './ConnectorsStore'
4
- import {ConnectorsStoreContext} from './ConnectorsStoreContext'
5
-
6
- export function useConnectorsStore(): ConnectorsStore {
7
- const store = useContext(ConnectorsStoreContext)
8
-
9
- if (!store) {
10
- throw new Error('Missing connectors store context')
11
- }
12
-
13
- return store
14
- }
@@ -1,142 +0,0 @@
1
- import {useEffect, useMemo, useRef, useState} from 'react'
2
-
3
- import {hasOverflowScroll} from './helpers'
4
- import {Rect, Scroll} from './types'
5
-
6
- export function useRegionRects() {
7
- const ref = useRef<HTMLDivElement>(null)
8
-
9
- const [relativeBoundsRect, setRelativeBoundsRect] = useState<Rect | null>(null)
10
- const [relativeElementRect, setRelativeElementRect] = useState<Rect | null>(null)
11
- const [boundsScroll, setBoundsScroll] = useState<Scroll>({x: 0, y: 0})
12
- const [scroll, setScroll] = useState<Scroll>({x: 0, y: 0})
13
-
14
- const boundsScrollXRef = useRef(0)
15
- const boundsScrollYRef = useRef(0)
16
-
17
- const elementScrollXRef = useRef(0)
18
- const elementScrollYRef = useRef(0)
19
-
20
- useEffect(() => {
21
- const el = ref.current
22
-
23
- if (!el) return undefined
24
-
25
- const scrollParents: HTMLElement[] = []
26
- let parent = el.parentElement
27
-
28
- while (parent && parent !== document.body) {
29
- if (
30
- hasOverflowScroll(parent)
31
- // || parent.scrollHeight > parent.clientHeight
32
- ) {
33
- scrollParents.push(parent)
34
- }
35
-
36
- parent = parent.parentElement
37
- }
38
-
39
- function handleResize() {
40
- const scrollParent = scrollParents[0]
41
-
42
- const boundsRect = scrollParent?.getBoundingClientRect() || {
43
- x: 0,
44
- y: 0,
45
- width: window.innerWidth,
46
- height: window.innerHeight,
47
- }
48
-
49
- const domRect = el!.getBoundingClientRect()
50
-
51
- setRelativeBoundsRect({
52
- x: boundsRect.x + boundsScrollXRef.current,
53
- y: boundsRect.y + boundsScrollYRef.current,
54
- w: boundsRect.width,
55
- h: boundsRect.height,
56
- })
57
-
58
- setRelativeElementRect({
59
- x: domRect.x + elementScrollXRef.current,
60
- y: domRect.y + elementScrollYRef.current,
61
- w: domRect.width,
62
- h: domRect.height,
63
- })
64
- }
65
-
66
- function handleScroll() {
67
- let scrollX = window.scrollX
68
- let scrollY = window.scrollY
69
-
70
- for (const scrollParent of scrollParents) {
71
- scrollX += scrollParent.scrollLeft
72
- scrollY += scrollParent.scrollTop
73
- }
74
-
75
- const scrollParent = scrollParents[0]
76
-
77
- boundsScrollXRef.current = scrollX - (scrollParent?.scrollLeft || window.scrollX)
78
-
79
- boundsScrollYRef.current = scrollY - (scrollParent?.scrollTop || window.scrollY)
80
-
81
- setBoundsScroll({
82
- x: boundsScrollXRef.current,
83
- y: boundsScrollYRef.current,
84
- })
85
-
86
- elementScrollXRef.current = scrollX
87
- elementScrollYRef.current = scrollY
88
-
89
- setScroll({x: scrollX, y: scrollY})
90
- }
91
-
92
- window.addEventListener('scroll', handleScroll, {passive: true})
93
-
94
- const ro = new ResizeObserver(handleResize)
95
-
96
- ro.observe(el)
97
-
98
- for (const scrollParent of scrollParents) {
99
- scrollParent.addEventListener('scroll', handleScroll, {passive: true})
100
- ro.observe(scrollParent)
101
- }
102
-
103
- handleScroll()
104
-
105
- return () => {
106
- ro.unobserve(el)
107
-
108
- for (const scrollParent of scrollParents) {
109
- ro.unobserve(scrollParent)
110
- scrollParent.removeEventListener('scroll', handleScroll)
111
- }
112
-
113
- ro.disconnect()
114
-
115
- window.removeEventListener('scroll', handleScroll)
116
- }
117
- }, [])
118
-
119
- const bounds: Rect | null = useMemo(
120
- () =>
121
- relativeBoundsRect && {
122
- x: relativeBoundsRect.x - boundsScroll.x,
123
- y: relativeBoundsRect.y - boundsScroll.y,
124
- w: relativeBoundsRect.w,
125
- h: relativeBoundsRect.h,
126
- },
127
- [relativeBoundsRect, boundsScroll],
128
- )
129
-
130
- const element: Rect | null = useMemo(
131
- () =>
132
- relativeElementRect && {
133
- x: relativeElementRect.x - scroll.x,
134
- y: relativeElementRect.y - scroll.y,
135
- w: relativeElementRect.w,
136
- h: relativeElementRect.h,
137
- },
138
- [relativeElementRect, scroll],
139
- )
140
-
141
- return {bounds, element, ref}
142
- }
@@ -1,101 +0,0 @@
1
- import type {SanityClient} from '@sanity/client'
2
- import {defer, delay, merge, Observable, of, partition, switchMap, throwError} from 'rxjs'
3
- import {filter, mergeMap, share, take} from 'rxjs/operators'
4
- import {exhaustMapToWithTrailing} from 'rxjs-exhaustmap-with-trailing'
5
- import {MutationEvent, ReconnectEvent, WelcomeEvent} from 'sanity'
6
-
7
- /** @internal */
8
- export type ListenQueryParams = Record<string, string | number | boolean | string[]>
9
-
10
- /** @beta */
11
- export interface ListenQueryOptions {
12
- tag?: string
13
- apiVersion?: string
14
- throttleTime?: number
15
- transitions?: ('update' | 'appear' | 'disappear')[]
16
- }
17
-
18
- const fetch = (
19
- client: SanityClient,
20
- query: string,
21
- params: ListenQueryParams,
22
- options: ListenQueryOptions,
23
- ) =>
24
- defer(() =>
25
- // getVersionedClient(options.apiVersion)
26
- client.observable.fetch(query, params, {
27
- tag: options.tag,
28
- filterResponse: true,
29
- }),
30
- )
31
-
32
- const listen = (
33
- client: SanityClient,
34
- query: string,
35
- params: ListenQueryParams,
36
- options: ListenQueryOptions,
37
- ) =>
38
- defer(() =>
39
- // getVersionedClient(options.apiVersion)
40
- client.listen(query, params, {
41
- events: ['welcome', 'mutation', 'reconnect'],
42
- includeResult: false,
43
- visibility: 'query',
44
- tag: options.tag,
45
- }),
46
- ) as Observable<ReconnectEvent | WelcomeEvent | MutationEvent>
47
-
48
- function isWelcomeEvent(
49
- event: MutationEvent | ReconnectEvent | WelcomeEvent,
50
- ): event is WelcomeEvent {
51
- return event.type === 'welcome'
52
- }
53
-
54
- /** @internal */
55
- export const listenQuery = (
56
- client: SanityClient,
57
- query: string | {fetch: string; listen: string},
58
- params: ListenQueryParams = {},
59
- options: ListenQueryOptions = {},
60
- ) => {
61
- const fetchQuery = typeof query === 'string' ? query : query.fetch
62
- const listenerQuery = typeof query === 'string' ? query : query.listen
63
-
64
- const fetchOnce$ = fetch(client, fetchQuery, params, options)
65
-
66
- const events$ = listen(client, listenerQuery, params, options).pipe(
67
- mergeMap((ev, i) => {
68
- const isFirst = i === 0
69
- if (isFirst && !isWelcomeEvent(ev)) {
70
- // if the first event is not welcome, it is most likely a reconnect and
71
- // if it's not a reconnect something is very wrong
72
- return throwError(
73
- new Error(
74
- ev.type === 'reconnect'
75
- ? 'Could not establish EventSource connection'
76
- : `Received unexpected type of first event "${ev.type}"`,
77
- ),
78
- )
79
- }
80
- return of(ev)
81
- }),
82
- share(),
83
- )
84
-
85
- const [welcome$, mutationAndReconnect$] = partition(events$, isWelcomeEvent)
86
- const isRelevantEvent = (event: MutationEvent | ReconnectEvent | WelcomeEvent): boolean => {
87
- if (!options.transitions || event.type !== 'mutation') {
88
- return true
89
- }
90
-
91
- return options.transitions.includes(event.transition)
92
- }
93
-
94
- return merge(
95
- welcome$.pipe(take(1)),
96
- mutationAndReconnect$.pipe(
97
- filter(isRelevantEvent),
98
- switchMap((event) => merge(of(event), of(event).pipe(delay(options.throttleTime || 1000)))),
99
- ),
100
- ).pipe(exhaustMapToWithTrailing(fetchOnce$))
101
- }