@portabletext/editor 2.3.7 → 2.4.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.
@@ -15,6 +15,8 @@ import {
15
15
  isNativeBehaviorEvent,
16
16
  isSyntheticBehaviorEvent,
17
17
  type BehaviorEvent,
18
+ type CustomBehaviorEvent,
19
+ type SyntheticBehaviorEvent,
18
20
  } from './behavior.types.event'
19
21
 
20
22
  const debug = debugWithName('behaviors:event')
@@ -54,7 +56,12 @@ export function performEvent({
54
56
  preventDefault: () => void
55
57
  }
56
58
  | undefined
57
- sendBack: (event: {type: 'set drag ghost'; ghost: HTMLElement}) => void
59
+ sendBack: (
60
+ event:
61
+ | {type: 'set drag ghost'; ghost: HTMLElement}
62
+ | SyntheticBehaviorEvent
63
+ | CustomBehaviorEvent,
64
+ ) => void
58
65
  }) {
59
66
  debug(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2))
60
67
 
@@ -196,7 +203,9 @@ export function performEvent({
196
203
  for (const action of actions) {
197
204
  if (action.type === 'effect') {
198
205
  try {
199
- action.effect()
206
+ action.effect({
207
+ send: sendBack,
208
+ })
200
209
  } catch (error) {
201
210
  console.error(
202
211
  new Error(
@@ -267,7 +276,9 @@ export function performEvent({
267
276
  for (const action of actions) {
268
277
  if (action.type === 'effect') {
269
278
  try {
270
- action.effect()
279
+ action.effect({
280
+ send: sendBack,
281
+ })
271
282
  } catch (error) {
272
283
  console.error(
273
284
  new Error(
@@ -304,7 +315,8 @@ export function performEvent({
304
315
  performEvent({
305
316
  mode: 'raise',
306
317
  behaviors,
307
- remainingEventBehaviors: behaviors,
318
+ remainingEventBehaviors:
319
+ mode === 'execute' ? remainingEventBehaviors : behaviors,
308
320
  event: action.event,
309
321
  editor,
310
322
  keyGenerator,
@@ -25,7 +25,31 @@ export type BehaviorAction =
25
25
  }
26
26
  | {
27
27
  type: 'effect'
28
- effect: () => void
28
+ effect: (payload: {
29
+ /**
30
+ * Send a Behavior Event back into the Editor.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * defineBehavior({
35
+ * on: '...',
36
+ * actions: [
37
+ * () => [
38
+ * effect(({send}) => {
39
+ * doSomethingAsync()
40
+ * .then(() => {
41
+ * send({
42
+ * type: '...',
43
+ * })
44
+ * })
45
+ * })
46
+ * ],
47
+ * ],
48
+ * })
49
+ * ```
50
+ */
51
+ send: (event: SyntheticBehaviorEvent | CustomBehaviorEvent) => void
52
+ }) => void
29
53
  }
30
54
 
31
55
  /**
@@ -59,7 +83,7 @@ export function raise(
59
83
  * @beta
60
84
  */
61
85
  export function effect(
62
- effect: () => void,
86
+ effect: PickFromUnion<BehaviorAction, 'type', 'effect'>['effect'],
63
87
  ): PickFromUnion<BehaviorAction, 'type', 'effect'> {
64
88
  return {type: 'effect', effect}
65
89
  }
@@ -233,6 +233,7 @@ const abstractBehaviorEventTypes = [
233
233
  'delete.forward',
234
234
  'delete.text',
235
235
  'deserialize',
236
+ 'deserialize.data',
236
237
  'deserialization.success',
237
238
  'deserialization.failure',
238
239
  'insert.blocks',
@@ -246,6 +247,7 @@ const abstractBehaviorEventTypes = [
246
247
  'select.previous block',
247
248
  'select.next block',
248
249
  'serialize',
250
+ 'serialize.data',
249
251
  'serialization.success',
250
252
  'serialization.failure',
251
253
  'split',
@@ -308,6 +310,18 @@ type AbstractBehaviorEvent =
308
310
  >
309
311
  | InputBehaviorEvent
310
312
  }
313
+ | {
314
+ type: StrictExtract<SyntheticBehaviorEventType, 'deserialize.data'>
315
+ mimeType: MIMEType
316
+ data: string
317
+ originEvent:
318
+ | PickFromUnion<
319
+ NativeBehaviorEvent,
320
+ 'type',
321
+ 'drag.drop' | 'clipboard.paste'
322
+ >
323
+ | InputBehaviorEvent
324
+ }
311
325
  | {
312
326
  type: StrictExtract<SyntheticBehaviorEventType, 'serialize'>
313
327
  originEvent: PickFromUnion<
@@ -316,6 +330,15 @@ type AbstractBehaviorEvent =
316
330
  'clipboard.copy' | 'clipboard.cut' | 'drag.dragstart'
317
331
  >
318
332
  }
333
+ | {
334
+ type: StrictExtract<SyntheticBehaviorEventType, 'serialize.data'>
335
+ mimeType: MIMEType
336
+ originEvent: PickFromUnion<
337
+ NativeBehaviorEvent,
338
+ 'type',
339
+ 'clipboard.copy' | 'clipboard.cut' | 'drag.dragstart'
340
+ >
341
+ }
319
342
  | {
320
343
  type: StrictExtract<SyntheticBehaviorEventType, 'deserialization.success'>
321
344
  mimeType: MIMEType
@@ -279,7 +279,18 @@ export const editorMachine = setup({
279
279
  schema: context.schema,
280
280
  }),
281
281
  nativeEvent: event.nativeEvent,
282
- sendBack: (event) => self.send(event),
282
+ sendBack: (eventSentBack) => {
283
+ if (eventSentBack.type === 'set drag ghost') {
284
+ self.send(eventSentBack)
285
+ return
286
+ }
287
+
288
+ self.send({
289
+ type: 'behavior event',
290
+ behaviorEvent: eventSentBack,
291
+ editor: event.editor,
292
+ })
293
+ },
283
294
  })
284
295
  } catch (error) {
285
296
  console.error(
@@ -51,6 +51,20 @@ export async function createTestEditor(
51
51
 
52
52
  await vi.waitFor(() => expect.element(locator).toBeInTheDocument())
53
53
 
54
+ function paste(dataTransfer: DataTransfer) {
55
+ editorActorRef.current?.send({
56
+ type: 'behavior event',
57
+ behaviorEvent: {
58
+ type: 'clipboard.paste',
59
+ originEvent: {dataTransfer},
60
+ position: {
61
+ selection: editorRef.current?.getSnapshot().context.selection!,
62
+ },
63
+ },
64
+ editor: slateRef.current!,
65
+ })
66
+ }
67
+
54
68
  return {
55
69
  editorActorRef,
56
70
  editorRef,
@@ -58,5 +72,6 @@ export async function createTestEditor(
58
72
  locator,
59
73
  onEvent,
60
74
  slateRef,
75
+ paste,
61
76
  }
62
77
  }