@prismicio/simulator 0.1.4 → 0.2.0
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 +7 -18
- package/dist/SimulatorAPI.cjs +38 -46
- package/dist/SimulatorAPI.cjs.map +1 -1
- package/dist/SimulatorAPI.d.cts +18 -0
- package/dist/SimulatorAPI.d.ts +16 -10
- package/dist/SimulatorAPI.js +37 -46
- package/dist/SimulatorAPI.js.map +1 -1
- package/dist/SimulatorClient.cjs +41 -51
- package/dist/SimulatorClient.cjs.map +1 -1
- package/dist/SimulatorClient.d.cts +16 -0
- package/dist/SimulatorClient.d.ts +15 -9
- package/dist/SimulatorClient.js +40 -51
- package/dist/SimulatorClient.js.map +1 -1
- package/dist/channel/ChannelEmitter.cjs +112 -143
- package/dist/channel/ChannelEmitter.cjs.map +1 -1
- package/dist/channel/ChannelEmitter.d.cts +38 -0
- package/dist/channel/ChannelEmitter.d.ts +36 -36
- package/dist/channel/ChannelEmitter.js +110 -141
- package/dist/channel/ChannelEmitter.js.map +1 -1
- package/dist/channel/ChannelNetwork.cjs +81 -109
- package/dist/channel/ChannelNetwork.cjs.map +1 -1
- package/dist/channel/ChannelNetwork.d.cts +28 -0
- package/dist/channel/ChannelNetwork.d.ts +25 -21
- package/dist/channel/ChannelNetwork.js +81 -109
- package/dist/channel/ChannelNetwork.js.map +1 -1
- package/dist/channel/ChannelReceiver.cjs +69 -92
- package/dist/channel/ChannelReceiver.cjs.map +1 -1
- package/dist/channel/ChannelReceiver.d.cts +20 -0
- package/dist/channel/ChannelReceiver.d.ts +18 -18
- package/dist/channel/ChannelReceiver.js +67 -90
- package/dist/channel/ChannelReceiver.js.map +1 -1
- package/dist/channel/errors.cjs +41 -46
- package/dist/channel/errors.cjs.map +1 -1
- package/dist/channel/errors.d.cts +28 -0
- package/dist/channel/errors.d.ts +22 -19
- package/dist/channel/errors.js +40 -52
- package/dist/channel/errors.js.map +1 -1
- package/dist/channel/messages.cjs +48 -43
- package/dist/channel/messages.cjs.map +1 -1
- package/dist/channel/messages.d.cts +12 -0
- package/dist/channel/messages.d.ts +12 -10
- package/dist/channel/messages.js +50 -53
- package/dist/channel/messages.js.map +1 -1
- package/dist/channel/types.cjs +15 -11
- package/dist/channel/types.cjs.map +1 -1
- package/dist/channel/types.d.cts +53 -0
- package/dist/channel/types.d.ts +42 -50
- package/dist/channel/types.js +13 -13
- package/dist/channel/types.js.map +1 -1
- package/dist/globalExtensions.d.cts +15 -0
- package/dist/globalExtensions.d.ts +13 -10
- package/dist/index.cjs +24 -33
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -8
- package/dist/index.js +5 -25
- package/dist/kit/SimulatorManager.cjs +106 -137
- package/dist/kit/SimulatorManager.cjs.map +1 -1
- package/dist/kit/SimulatorManager.d.cts +19 -0
- package/dist/kit/SimulatorManager.d.ts +16 -12
- package/dist/kit/SimulatorManager.js +103 -134
- package/dist/kit/SimulatorManager.js.map +1 -1
- package/dist/kit/State.cjs +82 -80
- package/dist/kit/State.cjs.map +1 -1
- package/dist/kit/State.d.cts +29 -0
- package/dist/kit/State.d.ts +26 -22
- package/dist/kit/State.js +80 -78
- package/dist/kit/State.js.map +1 -1
- package/dist/kit/domHelpers.cjs +25 -37
- package/dist/kit/domHelpers.cjs.map +1 -1
- package/dist/kit/domHelpers.d.cts +11 -0
- package/dist/kit/domHelpers.d.ts +10 -8
- package/dist/kit/domHelpers.js +25 -43
- package/dist/kit/domHelpers.js.map +1 -1
- package/dist/kit/eventHandlers.cjs +11 -9
- package/dist/kit/eventHandlers.cjs.map +1 -1
- package/dist/kit/eventHandlers.d.cts +8 -0
- package/dist/kit/eventHandlers.d.ts +7 -3
- package/dist/kit/eventHandlers.js +11 -11
- package/dist/kit/eventHandlers.js.map +1 -1
- package/dist/kit/getDefault.cjs +9 -7
- package/dist/kit/getDefault.cjs.map +1 -1
- package/dist/kit/getDefault.d.cts +10 -0
- package/dist/kit/getDefault.d.ts +10 -8
- package/dist/kit/getDefault.js +9 -10
- package/dist/kit/getDefault.js.map +1 -1
- package/dist/kit/messages.cjs +8 -22
- package/dist/kit/messages.cjs.map +1 -1
- package/dist/kit/messages.d.cts +5 -0
- package/dist/kit/messages.d.ts +5 -1
- package/dist/kit/messages.js +8 -23
- package/dist/kit/messages.js.map +1 -1
- package/dist/kit/types.cjs +12 -9
- package/dist/kit/types.cjs.map +1 -1
- package/dist/kit/types.d.cts +28 -0
- package/dist/kit/types.d.ts +24 -19
- package/dist/kit/types.js +11 -10
- package/dist/kit/types.js.map +1 -1
- package/dist/kit.cjs +21 -26
- package/dist/kit.d.cts +8 -0
- package/dist/kit.d.ts +8 -0
- package/dist/kit.js +7 -21
- package/dist/lib/EventEmitter.cjs +16 -25
- package/dist/lib/EventEmitter.cjs.map +1 -1
- package/dist/lib/EventEmitter.d.cts +12 -0
- package/dist/lib/EventEmitter.d.ts +11 -9
- package/dist/lib/EventEmitter.js +16 -26
- package/dist/lib/EventEmitter.js.map +1 -1
- package/dist/lib/throttle.cjs +21 -21
- package/dist/lib/throttle.cjs.map +1 -1
- package/dist/lib/throttle.js +21 -22
- package/dist/lib/throttle.js.map +1 -1
- package/dist/types.cjs +18 -14
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +39 -0
- package/dist/types.d.ts +32 -27
- package/dist/types.js +16 -16
- package/dist/types.js.map +1 -1
- package/package.json +27 -68
- package/src/SimulatorAPI.ts +24 -22
- package/src/SimulatorClient.ts +24 -25
- package/src/channel/ChannelEmitter.ts +71 -74
- package/src/channel/ChannelNetwork.ts +53 -55
- package/src/channel/ChannelReceiver.ts +46 -48
- package/src/channel/errors.ts +14 -14
- package/src/channel/index.ts +10 -10
- package/src/channel/messages.ts +39 -31
- package/src/channel/types.ts +40 -40
- package/src/globalExtensions.ts +6 -6
- package/src/index.ts +8 -8
- package/src/kit/SimulatorManager.ts +64 -63
- package/src/kit/State.ts +45 -44
- package/src/kit/domHelpers.ts +24 -24
- package/src/kit/eventHandlers.ts +6 -6
- package/src/kit/getDefault.ts +8 -8
- package/src/kit/index.ts +8 -8
- package/src/kit/messages.ts +5 -5
- package/src/kit/types.ts +13 -13
- package/src/lib/EventEmitter.ts +8 -8
- package/src/lib/__PRODUCTION__.ts +2 -2
- package/src/lib/throttle.ts +12 -12
- package/src/types.ts +20 -20
- package/dist/channel/index.d.ts +0 -10
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/kit/index.d.ts +0 -8
- package/dist/kit.cjs.map +0 -1
- package/dist/kit.js.map +0 -1
- package/dist/lib/__PRODUCTION__.d.ts +0 -7
- package/dist/lib/throttle.d.ts +0 -1
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export {
|
|
2
2
|
SimulatorClient,
|
|
3
3
|
simulatorClientDefaultOptions,
|
|
4
|
-
} from "./SimulatorClient"
|
|
4
|
+
} from "./SimulatorClient"
|
|
5
5
|
|
|
6
|
-
export { SimulatorAPI, simulatorAPIDefaultOptions } from "./SimulatorAPI"
|
|
7
|
-
export type { SimulatorAPIOptions } from "./SimulatorAPI"
|
|
6
|
+
export { SimulatorAPI, simulatorAPIDefaultOptions } from "./SimulatorAPI"
|
|
7
|
+
export type { SimulatorAPIOptions } from "./SimulatorAPI"
|
|
8
8
|
|
|
9
9
|
export {
|
|
10
10
|
// Validators
|
|
@@ -21,7 +21,7 @@ export {
|
|
|
21
21
|
NotReadyError,
|
|
22
22
|
PortNotSetError,
|
|
23
23
|
ChannelNotSetError,
|
|
24
|
-
} from "./channel"
|
|
24
|
+
} from "./channel"
|
|
25
25
|
export type {
|
|
26
26
|
RequestMessage,
|
|
27
27
|
ResponseMessage,
|
|
@@ -29,14 +29,14 @@ export type {
|
|
|
29
29
|
SuccessResponseMessage,
|
|
30
30
|
AllChannelEmitterOptions,
|
|
31
31
|
AllChannelReceiverOptions,
|
|
32
|
-
} from "./channel"
|
|
32
|
+
} from "./channel"
|
|
33
33
|
|
|
34
|
-
export { APIRequestType, ClientRequestType } from "./types"
|
|
34
|
+
export { APIRequestType, ClientRequestType } from "./types"
|
|
35
35
|
export type {
|
|
36
36
|
ActiveSlice,
|
|
37
37
|
SliceZoneSize,
|
|
38
38
|
APITransactions,
|
|
39
39
|
ClientTransactions,
|
|
40
|
-
} from "./types"
|
|
40
|
+
} from "./types"
|
|
41
41
|
|
|
42
|
-
export * from "./globalExtensions"
|
|
42
|
+
export * from "./globalExtensions"
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import type { SliceZone } from "@prismicio/client"
|
|
1
|
+
import type { SliceZone } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { throttle } from "../lib/throttle"
|
|
3
|
+
import { throttle } from "../lib/throttle"
|
|
4
4
|
|
|
5
|
-
import { ClientRequestType } from "../types"
|
|
6
|
-
import { StateEventType } from "./types"
|
|
5
|
+
import { ClientRequestType } from "../types"
|
|
6
|
+
import { StateEventType } from "./types"
|
|
7
7
|
|
|
8
|
-
import { SimulatorAPI } from "../SimulatorAPI"
|
|
9
|
-
import { ResponseError } from "../channel"
|
|
8
|
+
import { SimulatorAPI } from "../SimulatorAPI"
|
|
9
|
+
import { ResponseError } from "../channel"
|
|
10
10
|
|
|
11
|
-
import { State } from "./State"
|
|
11
|
+
import { State } from "./State"
|
|
12
12
|
import {
|
|
13
13
|
getSimulatorDOM,
|
|
14
14
|
getSimulatorRootDOM,
|
|
15
15
|
getSliceZoneDOM,
|
|
16
|
-
} from "./domHelpers"
|
|
17
|
-
import { sliceSimulatorAccessedDirectly } from "./messages"
|
|
16
|
+
} from "./domHelpers"
|
|
17
|
+
import { sliceSimulatorAccessedDirectly } from "./messages"
|
|
18
18
|
|
|
19
19
|
type ManagerConstructorArgs = {
|
|
20
|
-
slices?: SliceZone
|
|
21
|
-
}
|
|
20
|
+
slices?: SliceZone
|
|
21
|
+
}
|
|
22
22
|
|
|
23
23
|
export class SimulatorManager {
|
|
24
|
-
public state: State
|
|
25
|
-
private _api: SimulatorAPI | null
|
|
26
|
-
private _initialized: boolean
|
|
24
|
+
public state: State
|
|
25
|
+
private _api: SimulatorAPI | null
|
|
26
|
+
private _initialized: boolean
|
|
27
27
|
|
|
28
28
|
constructor(args?: ManagerConstructorArgs) {
|
|
29
|
-
this.state = new State(args)
|
|
30
|
-
this._api = null
|
|
31
|
-
this._initialized = false
|
|
29
|
+
this.state = new State(args)
|
|
30
|
+
this._api = null
|
|
31
|
+
this._initialized = false
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
async init(): Promise<void> {
|
|
35
35
|
if (this._initialized) {
|
|
36
|
-
return
|
|
36
|
+
return
|
|
37
37
|
} else {
|
|
38
|
-
this._initialized = true
|
|
38
|
+
this._initialized = true
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// Init state manager
|
|
42
|
-
await this.state.init()
|
|
42
|
+
await this.state.init()
|
|
43
43
|
|
|
44
44
|
// Init API
|
|
45
45
|
try {
|
|
46
|
-
await this._initAPI()
|
|
46
|
+
await this._initAPI()
|
|
47
47
|
} catch (error) {
|
|
48
48
|
if (
|
|
49
49
|
error instanceof Error &&
|
|
@@ -51,50 +51,50 @@ export class SimulatorManager {
|
|
|
51
51
|
!this.state.slices.length
|
|
52
52
|
) {
|
|
53
53
|
// Catch not embedded error and display message
|
|
54
|
-
this.state.message = sliceSimulatorAccessedDirectly
|
|
54
|
+
this.state.message = sliceSimulatorAccessedDirectly
|
|
55
55
|
}
|
|
56
|
-
console.error(error)
|
|
56
|
+
console.error(error)
|
|
57
57
|
|
|
58
|
-
return
|
|
58
|
+
return
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
// Init listeners
|
|
62
|
-
this._initListeners()
|
|
62
|
+
this._initListeners()
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
private async _initAPI(): Promise<void> {
|
|
66
66
|
// Register SimulatorAPI request handlers
|
|
67
67
|
this._api = new SimulatorAPI({
|
|
68
68
|
[ClientRequestType.SetSliceZone]: (req, res) => {
|
|
69
|
-
this.state.setSliceZone(req.data)
|
|
69
|
+
this.state.setSliceZone(req.data)
|
|
70
70
|
|
|
71
|
-
return res.success()
|
|
71
|
+
return res.success()
|
|
72
72
|
},
|
|
73
73
|
[ClientRequestType.ScrollToSlice]: (req, res) => {
|
|
74
74
|
// Error if `sliceIndex` is invalid
|
|
75
75
|
if (req.data.sliceIndex < 0) {
|
|
76
|
-
return res.error("`sliceIndex` must be > 0", 400)
|
|
76
|
+
return res.error("`sliceIndex` must be > 0", 400)
|
|
77
77
|
} else if (req.data.sliceIndex >= this.state.slices.length) {
|
|
78
78
|
return res.error(
|
|
79
79
|
`\`sliceIndex\` must be < ${this.state.slices.length} (\`<SliceZone />\` current length)`,
|
|
80
80
|
400,
|
|
81
|
-
)
|
|
81
|
+
)
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
const $sliceZone = getSliceZoneDOM(this.state.slices.length)
|
|
84
|
+
const $sliceZone = getSliceZoneDOM(this.state.slices.length)
|
|
85
85
|
if (!$sliceZone) {
|
|
86
|
-
return res.error("Failed to find `<SliceZone />`", 500)
|
|
86
|
+
return res.error("Failed to find `<SliceZone />`", 500)
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// Destroy existing active slice as we're about to scroll
|
|
90
|
-
this.state.activeSlice = null
|
|
90
|
+
this.state.activeSlice = null
|
|
91
91
|
|
|
92
|
-
const $slice = $sliceZone.children[req.data.sliceIndex]
|
|
92
|
+
const $slice = $sliceZone.children[req.data.sliceIndex]
|
|
93
93
|
if (!$slice) {
|
|
94
94
|
return res.error(
|
|
95
95
|
`Failed fo find slice at index $\`{req.data.sliceIndex}\` in \`<SliceZone />\``,
|
|
96
96
|
500,
|
|
97
|
-
)
|
|
97
|
+
)
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
// Scroll to Slice
|
|
@@ -102,97 +102,98 @@ export class SimulatorManager {
|
|
|
102
102
|
behavior: req.data.behavior,
|
|
103
103
|
block: req.data.block,
|
|
104
104
|
inline: req.data.inline,
|
|
105
|
-
})
|
|
105
|
+
})
|
|
106
106
|
|
|
107
107
|
// Update active slice after scrolling
|
|
108
|
-
this._api?.options.activeSliceAPI
|
|
109
|
-
setTimeout(this.state.setActiveSlice, 750)
|
|
108
|
+
if (this._api?.options.activeSliceAPI) {
|
|
109
|
+
setTimeout(this.state.setActiveSlice, 750)
|
|
110
|
+
}
|
|
110
111
|
|
|
111
|
-
return res.success()
|
|
112
|
+
return res.success()
|
|
112
113
|
},
|
|
113
|
-
})
|
|
114
|
+
})
|
|
114
115
|
|
|
115
116
|
// Mark API as ready
|
|
116
|
-
await this._api.ready()
|
|
117
|
+
await this._api.ready()
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
private _initListeners(): void {
|
|
120
121
|
if (this._api?.options.activeSliceAPI) {
|
|
121
122
|
// Update active slice on mouse move
|
|
122
123
|
window.addEventListener("mousemove", () => {
|
|
123
|
-
this.state.setActiveSlice()
|
|
124
|
-
})
|
|
124
|
+
this.state.setActiveSlice()
|
|
125
|
+
})
|
|
125
126
|
// Update active slice on events impacting slices rendering
|
|
126
127
|
window.addEventListener("resize", () => {
|
|
127
|
-
this.state.setActiveSlice()
|
|
128
|
-
})
|
|
128
|
+
this.state.setActiveSlice()
|
|
129
|
+
})
|
|
129
130
|
window.addEventListener("mousewheel", () => {
|
|
130
|
-
setTimeout(this.state.setActiveSlice, 200)
|
|
131
|
-
})
|
|
131
|
+
setTimeout(this.state.setActiveSlice, 200)
|
|
132
|
+
})
|
|
132
133
|
|
|
133
134
|
// Update active slice when slices are set
|
|
134
135
|
this.state.on(StateEventType.Slices, () => {
|
|
135
|
-
this.state.setActiveSlice()
|
|
136
|
-
})
|
|
136
|
+
this.state.setActiveSlice()
|
|
137
|
+
})
|
|
137
138
|
// Send active slices to renderer
|
|
138
139
|
this.state.on(StateEventType.ActiveSlice, async (activeSlice) => {
|
|
139
140
|
if (this._api) {
|
|
140
141
|
try {
|
|
141
|
-
await this._api.setActiveSlice(activeSlice)
|
|
142
|
+
await this._api.setActiveSlice(activeSlice)
|
|
142
143
|
} catch (error) {
|
|
143
144
|
// Just log bad requests, throw everything else
|
|
144
145
|
if (
|
|
145
146
|
error instanceof ResponseError &&
|
|
146
147
|
error.response.status === 400
|
|
147
148
|
) {
|
|
148
|
-
console.error(error.response)
|
|
149
|
+
console.error(error.response)
|
|
149
150
|
} else {
|
|
150
|
-
throw error
|
|
151
|
+
throw error
|
|
151
152
|
}
|
|
152
153
|
}
|
|
153
154
|
}
|
|
154
|
-
})
|
|
155
|
+
})
|
|
155
156
|
}
|
|
156
157
|
|
|
157
158
|
if (this._api?.options.sliceZoneSizeAPI) {
|
|
158
159
|
// Monitor Simulator root node size
|
|
159
160
|
const resizeObserver = new ResizeObserver(
|
|
160
161
|
throttle(async (entries) => {
|
|
161
|
-
const [entry] = entries
|
|
162
|
+
const [entry] = entries
|
|
162
163
|
|
|
163
164
|
if (this._api && entry) {
|
|
164
165
|
try {
|
|
165
|
-
await this._api.setSliceZoneSize({ rect: entry.contentRect })
|
|
166
|
+
await this._api.setSliceZoneSize({ rect: entry.contentRect })
|
|
166
167
|
} catch (error) {
|
|
167
168
|
// Just log bad requests, throw everything else
|
|
168
169
|
if (
|
|
169
170
|
error instanceof ResponseError &&
|
|
170
171
|
error.response.status === 400
|
|
171
172
|
) {
|
|
172
|
-
console.error(error.response)
|
|
173
|
+
console.error(error.response)
|
|
173
174
|
} else {
|
|
174
|
-
throw error
|
|
175
|
+
throw error
|
|
175
176
|
}
|
|
176
177
|
}
|
|
177
178
|
}
|
|
178
179
|
}, 16),
|
|
179
|
-
)
|
|
180
|
+
)
|
|
180
181
|
const observeSimulatorRoot = () => {
|
|
181
|
-
const simulatorRootDOM = getSimulatorRootDOM()
|
|
182
|
-
resizeObserver.disconnect()
|
|
182
|
+
const simulatorRootDOM = getSimulatorRootDOM()
|
|
183
|
+
resizeObserver.disconnect()
|
|
183
184
|
if (simulatorRootDOM) {
|
|
184
|
-
resizeObserver.observe(simulatorRootDOM)
|
|
185
|
+
resizeObserver.observe(simulatorRootDOM)
|
|
185
186
|
}
|
|
186
|
-
}
|
|
187
|
+
}
|
|
187
188
|
|
|
188
189
|
// Monitor Simulator root node in DOM
|
|
189
|
-
const mutationObserver = new MutationObserver(observeSimulatorRoot)
|
|
190
|
+
const mutationObserver = new MutationObserver(observeSimulatorRoot)
|
|
190
191
|
// We want it to fail if not found
|
|
191
192
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
192
193
|
mutationObserver.observe(getSimulatorDOM()!, {
|
|
193
194
|
subtree: false,
|
|
194
195
|
childList: true,
|
|
195
|
-
})
|
|
196
|
+
})
|
|
196
197
|
}
|
|
197
198
|
}
|
|
198
199
|
}
|
package/src/kit/State.ts
CHANGED
|
@@ -1,100 +1,101 @@
|
|
|
1
|
-
import type { SharedSlice, SliceZone } from "@prismicio/client"
|
|
1
|
+
import type { SharedSlice, SliceZone } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { EventEmitter } from "../lib/EventEmitter"
|
|
4
|
-
import { __PRODUCTION__ } from "../lib/__PRODUCTION__"
|
|
5
|
-
import { throttle } from "../lib/throttle"
|
|
3
|
+
import { EventEmitter } from "../lib/EventEmitter"
|
|
4
|
+
import { __PRODUCTION__ } from "../lib/__PRODUCTION__"
|
|
5
|
+
import { throttle } from "../lib/throttle"
|
|
6
6
|
|
|
7
|
-
import type { ActiveSlice } from "../types"
|
|
8
|
-
import {
|
|
7
|
+
import type { ActiveSlice } from "../types"
|
|
8
|
+
import type { StateEvents } from "./types";
|
|
9
|
+
import { StateEventType } from "./types"
|
|
9
10
|
|
|
10
|
-
import { getActiveSliceDOM, getSliceZoneDOM } from "./domHelpers"
|
|
11
|
-
import { getDefaultSlices } from "./getDefault"
|
|
11
|
+
import { getActiveSliceDOM, getSliceZoneDOM } from "./domHelpers"
|
|
12
|
+
import { getDefaultSlices } from "./getDefault"
|
|
12
13
|
|
|
13
14
|
type StateConstructorArgs = {
|
|
14
|
-
slices?: SliceZone
|
|
15
|
-
}
|
|
15
|
+
slices?: SliceZone
|
|
16
|
+
}
|
|
16
17
|
|
|
17
18
|
export class State extends EventEmitter<StateEvents> {
|
|
18
|
-
private _slices: SliceZone
|
|
19
|
+
private _slices: SliceZone
|
|
19
20
|
public set slices(slices: SliceZone) {
|
|
20
|
-
this._slices = slices
|
|
21
|
+
this._slices = slices
|
|
21
22
|
// Clean up message
|
|
22
|
-
this.message = ""
|
|
23
|
+
this.message = ""
|
|
23
24
|
// Dispatch event
|
|
24
|
-
this.emit(StateEventType.Slices, slices)
|
|
25
|
+
this.emit(StateEventType.Slices, slices)
|
|
25
26
|
}
|
|
26
27
|
public get slices(): SliceZone {
|
|
27
|
-
return this._slices
|
|
28
|
+
return this._slices
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
private _activeSlice: ActiveSlice | null
|
|
31
|
+
private _activeSlice: ActiveSlice | null
|
|
31
32
|
public set activeSlice(activeSlice: ActiveSlice | null) {
|
|
32
|
-
this._activeSlice = activeSlice
|
|
33
|
-
this.emit(StateEventType.ActiveSlice, activeSlice)
|
|
33
|
+
this._activeSlice = activeSlice
|
|
34
|
+
this.emit(StateEventType.ActiveSlice, activeSlice)
|
|
34
35
|
}
|
|
35
36
|
public get activeSlice(): ActiveSlice | null {
|
|
36
|
-
return this._activeSlice
|
|
37
|
+
return this._activeSlice
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
private _message: string
|
|
40
|
+
private _message: string
|
|
40
41
|
public set message(message: string) {
|
|
41
|
-
this._message = message
|
|
42
|
-
this.emit(StateEventType.Message, message)
|
|
42
|
+
this._message = message
|
|
43
|
+
this.emit(StateEventType.Message, message)
|
|
43
44
|
}
|
|
44
45
|
public get message(): string {
|
|
45
|
-
return this._message
|
|
46
|
+
return this._message
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
private _mouse: { x: number; y: number }
|
|
49
|
+
private _mouse: { x: number; y: number }
|
|
49
50
|
|
|
50
51
|
constructor(args?: StateConstructorArgs) {
|
|
51
|
-
super()
|
|
52
|
+
super()
|
|
52
53
|
|
|
53
|
-
this._slices = args?.slices || getDefaultSlices()
|
|
54
|
-
this._activeSlice = null
|
|
55
|
-
this._message = ""
|
|
54
|
+
this._slices = args?.slices || getDefaultSlices()
|
|
55
|
+
this._activeSlice = null
|
|
56
|
+
this._message = ""
|
|
56
57
|
|
|
57
58
|
// StateManager needs to keep track of mouse position for the active slice
|
|
58
|
-
this._mouse = { x: 0, y: 0 }
|
|
59
|
+
this._mouse = { x: 0, y: 0 }
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
async init(): Promise<void> {
|
|
62
63
|
// Init listener
|
|
63
64
|
window.addEventListener("mousemove", (event) => {
|
|
64
|
-
this._mouse = { x: event.clientX, y: event.clientY }
|
|
65
|
-
})
|
|
65
|
+
this._mouse = { x: event.clientX, y: event.clientY }
|
|
66
|
+
})
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
private _setActiveSlice = (): void => {
|
|
69
70
|
// There can't be an active slice if there's no slice displayed
|
|
70
71
|
if (this.slices.length === 0) {
|
|
71
|
-
this.activeSlice = null
|
|
72
|
+
this.activeSlice = null
|
|
72
73
|
|
|
73
|
-
return
|
|
74
|
+
return
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
const $sliceZone = getSliceZoneDOM(this.slices.length)
|
|
77
|
+
const $sliceZone = getSliceZoneDOM(this.slices.length)
|
|
77
78
|
|
|
78
79
|
// SliceZone has not been found, therefore active slice cannot be found
|
|
79
80
|
if (!$sliceZone) {
|
|
80
|
-
this.activeSlice = null
|
|
81
|
+
this.activeSlice = null
|
|
81
82
|
|
|
82
|
-
return
|
|
83
|
+
return
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
const $activeSlice = getActiveSliceDOM($sliceZone, this._mouse)
|
|
86
|
+
const $activeSlice = getActiveSliceDOM($sliceZone, this._mouse)
|
|
86
87
|
|
|
87
88
|
// Active slice has not been found
|
|
88
89
|
if (!$activeSlice) {
|
|
89
|
-
this.activeSlice = null
|
|
90
|
+
this.activeSlice = null
|
|
90
91
|
|
|
91
|
-
return
|
|
92
|
+
return
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
const activeSliceIndex = Array.prototype.indexOf.call(
|
|
95
96
|
$sliceZone.children,
|
|
96
97
|
$activeSlice,
|
|
97
|
-
)
|
|
98
|
+
)
|
|
98
99
|
|
|
99
100
|
// Active slice has been found
|
|
100
101
|
this.activeSlice = {
|
|
@@ -102,12 +103,12 @@ export class State extends EventEmitter<StateEvents> {
|
|
|
102
103
|
sliceID: this.slices[activeSliceIndex].slice_type,
|
|
103
104
|
variationID: (this.slices[activeSliceIndex] as SharedSlice).variation,
|
|
104
105
|
index: activeSliceIndex,
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
setActiveSlice = throttle(this._setActiveSlice, 16)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
setActiveSlice = throttle(this._setActiveSlice, 16)
|
|
108
109
|
|
|
109
110
|
setSliceZone(slices: SliceZone): void {
|
|
110
111
|
// Set slices
|
|
111
|
-
this.slices = slices
|
|
112
|
+
this.slices = slices
|
|
112
113
|
}
|
|
113
114
|
}
|
package/src/kit/domHelpers.ts
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
export const simulatorClass = "slice-simulator"
|
|
2
|
-
export const simulatorRootClass = "slice-simulator--root"
|
|
1
|
+
export const simulatorClass = "slice-simulator"
|
|
2
|
+
export const simulatorRootClass = "slice-simulator--root"
|
|
3
3
|
|
|
4
4
|
export const getSimulatorDOM = (): Element | null => {
|
|
5
|
-
return document.querySelector(`.${simulatorClass}`)
|
|
6
|
-
}
|
|
5
|
+
return document.querySelector(`.${simulatorClass}`)
|
|
6
|
+
}
|
|
7
7
|
|
|
8
8
|
export const getSimulatorRootDOM = (): Element | null => {
|
|
9
|
-
return document.querySelector(`.${simulatorRootClass}`)
|
|
10
|
-
}
|
|
9
|
+
return document.querySelector(`.${simulatorRootClass}`)
|
|
10
|
+
}
|
|
11
11
|
|
|
12
12
|
export const getSliceZoneDOM = (
|
|
13
13
|
expectedNumberOfChildren: number,
|
|
14
14
|
): Element | null => {
|
|
15
15
|
// If SliceZone has been tagged (#5)
|
|
16
|
-
let node = document.querySelector(`.${simulatorClass} [data-slice-zone]`)
|
|
16
|
+
let node = document.querySelector(`.${simulatorClass} [data-slice-zone]`)
|
|
17
17
|
if (node) {
|
|
18
18
|
if (node.children.length !== expectedNumberOfChildren) {
|
|
19
19
|
console.warn(
|
|
20
20
|
`Flagged SliceZone has an unexpected number of children, found ${node.children.length} but expected ${expectedNumberOfChildren}. This might lead to unexpected behaviors. Are you sure you tagged the right element?`,
|
|
21
|
-
)
|
|
21
|
+
)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
return node
|
|
24
|
+
return node
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// Start searching
|
|
28
|
-
node = document.querySelector(`.${simulatorClass} .${simulatorRootClass}`)
|
|
28
|
+
node = document.querySelector(`.${simulatorClass} .${simulatorRootClass}`)
|
|
29
29
|
|
|
30
30
|
if (!node) {
|
|
31
|
-
return null
|
|
31
|
+
return null
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
const searchDepth = 5
|
|
34
|
+
const searchDepth = 5
|
|
35
35
|
for (let i = 0; i < searchDepth; i++) {
|
|
36
36
|
if (node.children.length === expectedNumberOfChildren) {
|
|
37
37
|
// Return the first node that matches the expected number of children
|
|
38
|
-
return node
|
|
38
|
+
return node
|
|
39
39
|
} else if (node.children.length) {
|
|
40
40
|
// Continue searching if current node has children
|
|
41
|
-
node = node.children[0]
|
|
41
|
+
node = node.children[0]
|
|
42
42
|
} else {
|
|
43
43
|
// Stop searching if current node has no children
|
|
44
|
-
break
|
|
44
|
+
break
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// Failed to find SliceZone
|
|
49
|
-
return null
|
|
50
|
-
}
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
51
|
|
|
52
52
|
export const getActiveSliceDOM = (
|
|
53
53
|
$sliceZone: Element,
|
|
54
54
|
mouse: { x: number; y: number },
|
|
55
55
|
): Element | null => {
|
|
56
56
|
// Raycast cursor position on DOM
|
|
57
|
-
const raycast = document.elementsFromPoint(mouse.x, mouse.y).reverse()
|
|
57
|
+
const raycast = document.elementsFromPoint(mouse.x, mouse.y).reverse()
|
|
58
58
|
|
|
59
59
|
// Find SliceZone in raycast
|
|
60
|
-
const sliceZoneIndex = raycast.indexOf($sliceZone)
|
|
60
|
+
const sliceZoneIndex = raycast.indexOf($sliceZone)
|
|
61
61
|
|
|
62
62
|
// SliceZone is not part of raycast
|
|
63
63
|
if (sliceZoneIndex === -1) {
|
|
64
|
-
return null
|
|
64
|
+
return null
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
const $slice = raycast[sliceZoneIndex + 1]
|
|
67
|
+
const $slice = raycast[sliceZoneIndex + 1]
|
|
68
68
|
|
|
69
69
|
// Slice is not part of raycast
|
|
70
70
|
if (!$slice) {
|
|
71
|
-
return null
|
|
71
|
+
return null
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
return $slice
|
|
75
|
-
}
|
|
74
|
+
return $slice
|
|
75
|
+
}
|
package/src/kit/eventHandlers.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const disableEventHandler = (event: Event): void => {
|
|
2
|
-
event.preventDefault()
|
|
3
|
-
event.stopPropagation()
|
|
4
|
-
}
|
|
2
|
+
event.preventDefault()
|
|
3
|
+
event.stopPropagation()
|
|
4
|
+
}
|
|
5
5
|
|
|
6
6
|
export const onClickHandler = (
|
|
7
7
|
event: MouseEvent & { path?: HTMLElement[] },
|
|
@@ -10,7 +10,7 @@ export const onClickHandler = (
|
|
|
10
10
|
event.path &&
|
|
11
11
|
event.path.slice(0, 5).some((el) => el instanceof HTMLAnchorElement)
|
|
12
12
|
) {
|
|
13
|
-
event.preventDefault()
|
|
14
|
-
event.stopPropagation()
|
|
13
|
+
event.preventDefault()
|
|
14
|
+
event.stopPropagation()
|
|
15
15
|
}
|
|
16
|
-
}
|
|
16
|
+
}
|
package/src/kit/getDefault.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { SliceZone } from "@prismicio/client"
|
|
1
|
+
import type { SliceZone } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { SliceSimulatorProps } from "./types"
|
|
3
|
+
import type { SliceSimulatorProps } from "./types"
|
|
4
4
|
|
|
5
5
|
export const getDefaultProps = (): Required<{
|
|
6
|
-
[K in keyof SliceSimulatorProps]: NonNullable<SliceSimulatorProps[K]
|
|
6
|
+
[K in keyof SliceSimulatorProps]: NonNullable<SliceSimulatorProps[K]>
|
|
7
7
|
}> => ({
|
|
8
8
|
zIndex: 100,
|
|
9
9
|
background: "#ffffff",
|
|
10
|
-
})
|
|
10
|
+
})
|
|
11
11
|
|
|
12
12
|
export const getDefaultSlices = (): SliceZone => {
|
|
13
|
-
return []
|
|
14
|
-
}
|
|
13
|
+
return []
|
|
14
|
+
}
|
|
15
15
|
|
|
16
16
|
export const getDefaultMessage = (): string => {
|
|
17
|
-
return ""
|
|
18
|
-
}
|
|
17
|
+
return ""
|
|
18
|
+
}
|
package/src/kit/index.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// Main
|
|
2
|
-
export { SimulatorManager } from "./SimulatorManager"
|
|
3
|
-
export { State } from "./State"
|
|
2
|
+
export { SimulatorManager } from "./SimulatorManager"
|
|
3
|
+
export { State } from "./State"
|
|
4
4
|
|
|
5
5
|
// Defaults
|
|
6
6
|
export {
|
|
7
7
|
getDefaultProps,
|
|
8
8
|
getDefaultSlices,
|
|
9
9
|
getDefaultMessage,
|
|
10
|
-
} from "./getDefault"
|
|
10
|
+
} from "./getDefault"
|
|
11
11
|
|
|
12
12
|
// DOM helpers
|
|
13
13
|
export {
|
|
@@ -15,19 +15,19 @@ export {
|
|
|
15
15
|
simulatorRootClass,
|
|
16
16
|
getSliceZoneDOM,
|
|
17
17
|
getActiveSliceDOM,
|
|
18
|
-
} from "./domHelpers"
|
|
18
|
+
} from "./domHelpers"
|
|
19
19
|
|
|
20
20
|
// Events
|
|
21
|
-
export { disableEventHandler, onClickHandler } from "./eventHandlers"
|
|
21
|
+
export { disableEventHandler, onClickHandler } from "./eventHandlers"
|
|
22
22
|
|
|
23
23
|
// Messages
|
|
24
|
-
export { sliceSimulatorAccessedDirectly } from "./messages"
|
|
24
|
+
export { sliceSimulatorAccessedDirectly } from "./messages"
|
|
25
25
|
|
|
26
26
|
// Types
|
|
27
|
-
export { StateEventType } from "./types"
|
|
27
|
+
export { StateEventType } from "./types"
|
|
28
28
|
export type {
|
|
29
29
|
StateEvents,
|
|
30
30
|
SliceSimulatorProps,
|
|
31
31
|
SliceSimulatorState,
|
|
32
32
|
SliceSimulatorOptions,
|
|
33
|
-
} from "./types"
|
|
33
|
+
} from "./types"
|