@portabletext/editor 1.51.0 → 1.52.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/lib/_chunks-es/selector.is-selection-expanded.js +1 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +26 -5
- package/lib/behaviors/index.d.ts +26 -5
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +434 -309
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +360 -31
- package/lib/index.d.ts +360 -31
- package/lib/index.js +455 -330
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +26 -5
- package/lib/plugins/index.d.ts +26 -5
- package/lib/selectors/index.d.cts +0 -14
- package/lib/selectors/index.d.ts +0 -14
- package/lib/utils/index.d.cts +0 -14
- package/lib/utils/index.d.ts +0 -14
- package/package.json +3 -3
- package/src/behaviors/behavior.abstract.delete.ts +0 -1
- package/src/behaviors/behavior.abstract.ts +0 -113
- package/src/behaviors/behavior.core.block-element.ts +9 -3
- package/src/behaviors/behavior.core.dnd.ts +328 -1
- package/src/behaviors/behavior.perform-event.ts +10 -0
- package/src/behaviors/behavior.types.action.ts +2 -0
- package/src/behaviors/behavior.types.event.ts +4 -0
- package/src/behaviors/behavior.types.guard.ts +2 -0
- package/src/converters/converter.portable-text.ts +2 -7
- package/src/converters/converter.text-html.ts +1 -3
- package/src/converters/converter.text-plain.ts +3 -5
- package/src/editor/Editable.tsx +6 -133
- package/src/editor/editor-machine.ts +15 -8
- package/src/editor/editor-selector.ts +0 -1
- package/src/editor/editor-snapshot.ts +0 -13
- package/src/internal-utils/create-test-snapshot.ts +0 -1
- package/src/internal-utils/event-position.ts +41 -27
- package/src/internal-utils/selection-elements.ts +108 -0
- package/src/operations/behavior.operation.decorator.add.ts +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isKeySegment } from "@sanity/types";
|
|
2
|
-
import { isTextBlock, isSpan$1 as isSpan, isListBlock,
|
|
2
|
+
import { getBlockKeyFromSelectionPoint, isTextBlock, isSpan$1 as isSpan, isListBlock, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint, getChildKeyFromSelectionPoint, isSpan as isSpan$1, sliceBlocks } from "./selection-point.js";
|
|
3
3
|
const getSelectionStartPoint = (snapshot) => {
|
|
4
4
|
if (snapshot.context.selection)
|
|
5
5
|
return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/behaviors/behavior.types.action.ts","../../src/behaviors/behavior.types.behavior.ts"],"sourcesContent":["import type {EditorSnapshot} from '../editor/editor-snapshot'\nimport type {PickFromUnion} from '../type-utils'\nimport type {\n CustomBehaviorEvent,\n NativeBehaviorEvent,\n SyntheticBehaviorEvent,\n} from './behavior.types.event'\n\n/**\n * @beta\n */\nexport type BehaviorAction =\n | {\n type: 'execute'\n event: SyntheticBehaviorEvent\n }\n | {\n type: 'forward'\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'raise'\n event: SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'effect'\n effect: () => void\n }\n\n/**\n * @beta\n */\nexport function execute(\n event: SyntheticBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'execute'> {\n return {type: 'execute', event}\n}\n\n/**\n * @beta\n */\nexport function forward(\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'forward'> {\n return {type: 'forward', event}\n}\n\n/**\n * @beta\n */\nexport function raise(\n event: SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'raise'> {\n return {type: 'raise', event}\n}\n\n/**\n * @beta\n */\nexport function effect(\n effect: () => void,\n): PickFromUnion<BehaviorAction, 'type', 'effect'> {\n return {type: 'effect', effect}\n}\n\n/**\n * @beta\n */\nexport type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (\n payload: {\n snapshot: EditorSnapshot\n event: TBehaviorEvent\n },\n guardResponse: TGuardResponse,\n) => Array<BehaviorAction>\n","import type {BehaviorActionSet} from './behavior.types.action'\nimport type {\n BehaviorEvent,\n BehaviorEventTypeNamespace,\n CustomBehaviorEvent,\n ResolveBehaviorEvent,\n} from './behavior.types.event'\nimport type {BehaviorGuard} from './behavior.types.guard'\n\n/**\n * @beta\n */\nexport type Behavior<\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] =\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends\n ResolveBehaviorEvent<TBehaviorEventType> = ResolveBehaviorEvent<TBehaviorEventType>,\n> = {\n /**\n * Editor Event that triggers this Behavior.\n */\n on: TBehaviorEventType\n /**\n * Predicate function that determines if the Behavior should be executed.\n * Returning a non-nullable value from the guard will pass the value to the\n * actions and execute them.\n */\n guard?: BehaviorGuard<TBehaviorEvent, TGuardResponse>\n /**\n * Array of Behavior Action sets.\n * Each set represents a step in the history stack.\n */\n actions: Array<BehaviorActionSet<TBehaviorEvent, TGuardResponse>>\n}\n\n/**\n * @beta\n *\n * @example\n *\n * ```tsx\n * const noLowerCaseA = defineBehavior({\n * on: 'insert.text',\n * guard: ({event, snapshot}) => event.text === 'a',\n * actions: [({event, snapshot}) => [{type: 'insert.text', text: 'A'}]],\n * })\n * ```\n *\n */\nexport function defineBehavior<\n TPayload extends Record<string, unknown>,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = CustomBehaviorEvent['type'],\n TGuardResponse = true,\n>(\n behavior: Behavior<\n TBehaviorEventType,\n TGuardResponse,\n ResolveBehaviorEvent<TBehaviorEventType, TPayload>\n >,\n): Behavior\nexport function defineBehavior<\n TPayload extends never = never,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends ResolveBehaviorEvent<\n TBehaviorEventType,\n TPayload\n > = ResolveBehaviorEvent<TBehaviorEventType, TPayload>,\n>(\n behavior: Behavior<TBehaviorEventType, TGuardResponse, TBehaviorEvent>,\n): Behavior {\n return behavior as unknown as Behavior\n}\n"],"names":["execute","event","type","forward","raise","effect","defineBehavior","behavior"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/behaviors/behavior.types.action.ts","../../src/behaviors/behavior.types.behavior.ts"],"sourcesContent":["import type {EditorSnapshot} from '../editor/editor-snapshot'\nimport type {EditorDom} from '../internal-utils/selection-elements'\nimport type {PickFromUnion} from '../type-utils'\nimport type {\n CustomBehaviorEvent,\n NativeBehaviorEvent,\n SyntheticBehaviorEvent,\n} from './behavior.types.event'\n\n/**\n * @beta\n */\nexport type BehaviorAction =\n | {\n type: 'execute'\n event: SyntheticBehaviorEvent\n }\n | {\n type: 'forward'\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'raise'\n event: SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'effect'\n effect: () => void\n }\n\n/**\n * @beta\n */\nexport function execute(\n event: SyntheticBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'execute'> {\n return {type: 'execute', event}\n}\n\n/**\n * @beta\n */\nexport function forward(\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'forward'> {\n return {type: 'forward', event}\n}\n\n/**\n * @beta\n */\nexport function raise(\n event: SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'raise'> {\n return {type: 'raise', event}\n}\n\n/**\n * @beta\n */\nexport function effect(\n effect: () => void,\n): PickFromUnion<BehaviorAction, 'type', 'effect'> {\n return {type: 'effect', effect}\n}\n\n/**\n * @beta\n */\nexport type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (\n payload: {\n snapshot: EditorSnapshot\n event: TBehaviorEvent\n dom: EditorDom\n },\n guardResponse: TGuardResponse,\n) => Array<BehaviorAction>\n","import type {BehaviorActionSet} from './behavior.types.action'\nimport type {\n BehaviorEvent,\n BehaviorEventTypeNamespace,\n CustomBehaviorEvent,\n ResolveBehaviorEvent,\n} from './behavior.types.event'\nimport type {BehaviorGuard} from './behavior.types.guard'\n\n/**\n * @beta\n */\nexport type Behavior<\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] =\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends\n ResolveBehaviorEvent<TBehaviorEventType> = ResolveBehaviorEvent<TBehaviorEventType>,\n> = {\n /**\n * Editor Event that triggers this Behavior.\n */\n on: TBehaviorEventType\n /**\n * Predicate function that determines if the Behavior should be executed.\n * Returning a non-nullable value from the guard will pass the value to the\n * actions and execute them.\n */\n guard?: BehaviorGuard<TBehaviorEvent, TGuardResponse>\n /**\n * Array of Behavior Action sets.\n * Each set represents a step in the history stack.\n */\n actions: Array<BehaviorActionSet<TBehaviorEvent, TGuardResponse>>\n}\n\n/**\n * @beta\n *\n * @example\n *\n * ```tsx\n * const noLowerCaseA = defineBehavior({\n * on: 'insert.text',\n * guard: ({event, snapshot}) => event.text === 'a',\n * actions: [({event, snapshot}) => [{type: 'insert.text', text: 'A'}]],\n * })\n * ```\n *\n */\nexport function defineBehavior<\n TPayload extends Record<string, unknown>,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = CustomBehaviorEvent['type'],\n TGuardResponse = true,\n>(\n behavior: Behavior<\n TBehaviorEventType,\n TGuardResponse,\n ResolveBehaviorEvent<TBehaviorEventType, TPayload>\n >,\n): Behavior\nexport function defineBehavior<\n TPayload extends never = never,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends ResolveBehaviorEvent<\n TBehaviorEventType,\n TPayload\n > = ResolveBehaviorEvent<TBehaviorEventType, TPayload>,\n>(\n behavior: Behavior<TBehaviorEventType, TGuardResponse, TBehaviorEvent>,\n): Behavior {\n return behavior as unknown as Behavior\n}\n"],"names":["execute","event","type","forward","raise","effect","defineBehavior","behavior"],"mappings":";;AAiCO,SAASA,QACdC,OACkD;AAC3C,SAAA;AAAA,IAACC,MAAM;AAAA,IAAWD;AAAAA,EAAK;AAChC;AAKO,SAASE,QACdF,OACkD;AAC3C,SAAA;AAAA,IAACC,MAAM;AAAA,IAAWD;AAAAA,EAAK;AAChC;AAKO,SAASG,MACdH,OACgD;AACzC,SAAA;AAAA,IAACC,MAAM;AAAA,IAASD;AAAAA,EAAK;AAC9B;AAKO,SAASI,OACdA,SACiD;AAC1C,SAAA;AAAA,IAACH,MAAM;AAAA,IAAUG,QAAAA;AAAAA,EAAM;AAChC;ACKO,SAASC,eAYdC,UACU;AACHA,SAAAA;AACT;;;;;;"}
|
|
@@ -261,6 +261,7 @@ export declare type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
|
|
|
261
261
|
payload: {
|
|
262
262
|
snapshot: EditorSnapshot
|
|
263
263
|
event: TBehaviorEvent
|
|
264
|
+
dom: EditorDom
|
|
264
265
|
},
|
|
265
266
|
guardResponse: TGuardResponse,
|
|
266
267
|
) => Array<BehaviorAction>
|
|
@@ -284,6 +285,7 @@ declare type BehaviorEventTypeNamespace =
|
|
|
284
285
|
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
|
|
285
286
|
snapshot: EditorSnapshot
|
|
286
287
|
event: TBehaviorEvent
|
|
288
|
+
dom: EditorDom
|
|
287
289
|
}) => TGuardResponse | false
|
|
288
290
|
|
|
289
291
|
/**
|
|
@@ -428,6 +430,8 @@ declare type DragBehaviorEvent =
|
|
|
428
430
|
| {
|
|
429
431
|
type: StrictExtract<NativeBehaviorEventType, 'drag.dragstart'>
|
|
430
432
|
originEvent: {
|
|
433
|
+
clientX: number
|
|
434
|
+
clientY: number
|
|
431
435
|
dataTransfer: DataTransfer
|
|
432
436
|
}
|
|
433
437
|
position: Pick<EventPosition, 'selection'>
|
|
@@ -456,6 +460,7 @@ declare type DragBehaviorEvent =
|
|
|
456
460
|
originEvent: {
|
|
457
461
|
dataTransfer: DataTransfer
|
|
458
462
|
}
|
|
463
|
+
dragOrigin?: Pick<EventPosition, 'selection'>
|
|
459
464
|
position: EventPosition
|
|
460
465
|
}
|
|
461
466
|
| {
|
|
@@ -463,6 +468,7 @@ declare type DragBehaviorEvent =
|
|
|
463
468
|
originEvent: {
|
|
464
469
|
dataTransfer: DataTransfer
|
|
465
470
|
}
|
|
471
|
+
dragOrigin?: Pick<EventPosition, 'selection'>
|
|
466
472
|
position: EventPosition
|
|
467
473
|
}
|
|
468
474
|
| {
|
|
@@ -484,6 +490,26 @@ declare type EditorContext = {
|
|
|
484
490
|
value: Array<PortableTextBlock>
|
|
485
491
|
}
|
|
486
492
|
|
|
493
|
+
declare type EditorDom = {
|
|
494
|
+
getBlockNodes: (snapshot: EditorSnapshot) => Array<Node>
|
|
495
|
+
getChildNodes: (snapshot: EditorSnapshot) => Array<Node>
|
|
496
|
+
/**
|
|
497
|
+
* Let the Editor set the drag ghost. This is to be sure that it will get
|
|
498
|
+
* properly removed again when the drag ends.
|
|
499
|
+
*/
|
|
500
|
+
setDragGhost: ({
|
|
501
|
+
event,
|
|
502
|
+
ghost,
|
|
503
|
+
}: {
|
|
504
|
+
event: PickFromUnion<BehaviorEvent, 'type', 'drag.dragstart'>
|
|
505
|
+
ghost: {
|
|
506
|
+
element: HTMLElement
|
|
507
|
+
x: number
|
|
508
|
+
y: number
|
|
509
|
+
}
|
|
510
|
+
}) => void
|
|
511
|
+
}
|
|
512
|
+
|
|
487
513
|
/**
|
|
488
514
|
* @public
|
|
489
515
|
*/
|
|
@@ -572,11 +598,6 @@ declare type EditorSnapshot = {
|
|
|
572
598
|
beta: {
|
|
573
599
|
activeAnnotations: Array<string>
|
|
574
600
|
activeDecorators: Array<string>
|
|
575
|
-
internalDrag:
|
|
576
|
-
| {
|
|
577
|
-
origin: Pick<EventPosition, 'selection'>
|
|
578
|
-
}
|
|
579
|
-
| undefined
|
|
580
601
|
}
|
|
581
602
|
}
|
|
582
603
|
|
package/lib/behaviors/index.d.ts
CHANGED
|
@@ -261,6 +261,7 @@ export declare type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
|
|
|
261
261
|
payload: {
|
|
262
262
|
snapshot: EditorSnapshot
|
|
263
263
|
event: TBehaviorEvent
|
|
264
|
+
dom: EditorDom
|
|
264
265
|
},
|
|
265
266
|
guardResponse: TGuardResponse,
|
|
266
267
|
) => Array<BehaviorAction>
|
|
@@ -284,6 +285,7 @@ declare type BehaviorEventTypeNamespace =
|
|
|
284
285
|
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
|
|
285
286
|
snapshot: EditorSnapshot
|
|
286
287
|
event: TBehaviorEvent
|
|
288
|
+
dom: EditorDom
|
|
287
289
|
}) => TGuardResponse | false
|
|
288
290
|
|
|
289
291
|
/**
|
|
@@ -428,6 +430,8 @@ declare type DragBehaviorEvent =
|
|
|
428
430
|
| {
|
|
429
431
|
type: StrictExtract<NativeBehaviorEventType, 'drag.dragstart'>
|
|
430
432
|
originEvent: {
|
|
433
|
+
clientX: number
|
|
434
|
+
clientY: number
|
|
431
435
|
dataTransfer: DataTransfer
|
|
432
436
|
}
|
|
433
437
|
position: Pick<EventPosition, 'selection'>
|
|
@@ -456,6 +460,7 @@ declare type DragBehaviorEvent =
|
|
|
456
460
|
originEvent: {
|
|
457
461
|
dataTransfer: DataTransfer
|
|
458
462
|
}
|
|
463
|
+
dragOrigin?: Pick<EventPosition, 'selection'>
|
|
459
464
|
position: EventPosition
|
|
460
465
|
}
|
|
461
466
|
| {
|
|
@@ -463,6 +468,7 @@ declare type DragBehaviorEvent =
|
|
|
463
468
|
originEvent: {
|
|
464
469
|
dataTransfer: DataTransfer
|
|
465
470
|
}
|
|
471
|
+
dragOrigin?: Pick<EventPosition, 'selection'>
|
|
466
472
|
position: EventPosition
|
|
467
473
|
}
|
|
468
474
|
| {
|
|
@@ -484,6 +490,26 @@ declare type EditorContext = {
|
|
|
484
490
|
value: Array<PortableTextBlock>
|
|
485
491
|
}
|
|
486
492
|
|
|
493
|
+
declare type EditorDom = {
|
|
494
|
+
getBlockNodes: (snapshot: EditorSnapshot) => Array<Node>
|
|
495
|
+
getChildNodes: (snapshot: EditorSnapshot) => Array<Node>
|
|
496
|
+
/**
|
|
497
|
+
* Let the Editor set the drag ghost. This is to be sure that it will get
|
|
498
|
+
* properly removed again when the drag ends.
|
|
499
|
+
*/
|
|
500
|
+
setDragGhost: ({
|
|
501
|
+
event,
|
|
502
|
+
ghost,
|
|
503
|
+
}: {
|
|
504
|
+
event: PickFromUnion<BehaviorEvent, 'type', 'drag.dragstart'>
|
|
505
|
+
ghost: {
|
|
506
|
+
element: HTMLElement
|
|
507
|
+
x: number
|
|
508
|
+
y: number
|
|
509
|
+
}
|
|
510
|
+
}) => void
|
|
511
|
+
}
|
|
512
|
+
|
|
487
513
|
/**
|
|
488
514
|
* @public
|
|
489
515
|
*/
|
|
@@ -572,11 +598,6 @@ declare type EditorSnapshot = {
|
|
|
572
598
|
beta: {
|
|
573
599
|
activeAnnotations: Array<string>
|
|
574
600
|
activeDecorators: Array<string>
|
|
575
|
-
internalDrag:
|
|
576
|
-
| {
|
|
577
|
-
origin: Pick<EventPosition, 'selection'>
|
|
578
|
-
}
|
|
579
|
-
| undefined
|
|
580
601
|
}
|
|
581
602
|
}
|
|
582
603
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/behaviors/behavior.types.action.ts","../../src/behaviors/behavior.types.behavior.ts"],"sourcesContent":["import type {EditorSnapshot} from '../editor/editor-snapshot'\nimport type {PickFromUnion} from '../type-utils'\nimport type {\n CustomBehaviorEvent,\n NativeBehaviorEvent,\n SyntheticBehaviorEvent,\n} from './behavior.types.event'\n\n/**\n * @beta\n */\nexport type BehaviorAction =\n | {\n type: 'execute'\n event: SyntheticBehaviorEvent\n }\n | {\n type: 'forward'\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'raise'\n event: SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'effect'\n effect: () => void\n }\n\n/**\n * @beta\n */\nexport function execute(\n event: SyntheticBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'execute'> {\n return {type: 'execute', event}\n}\n\n/**\n * @beta\n */\nexport function forward(\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'forward'> {\n return {type: 'forward', event}\n}\n\n/**\n * @beta\n */\nexport function raise(\n event: SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'raise'> {\n return {type: 'raise', event}\n}\n\n/**\n * @beta\n */\nexport function effect(\n effect: () => void,\n): PickFromUnion<BehaviorAction, 'type', 'effect'> {\n return {type: 'effect', effect}\n}\n\n/**\n * @beta\n */\nexport type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (\n payload: {\n snapshot: EditorSnapshot\n event: TBehaviorEvent\n },\n guardResponse: TGuardResponse,\n) => Array<BehaviorAction>\n","import type {BehaviorActionSet} from './behavior.types.action'\nimport type {\n BehaviorEvent,\n BehaviorEventTypeNamespace,\n CustomBehaviorEvent,\n ResolveBehaviorEvent,\n} from './behavior.types.event'\nimport type {BehaviorGuard} from './behavior.types.guard'\n\n/**\n * @beta\n */\nexport type Behavior<\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] =\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends\n ResolveBehaviorEvent<TBehaviorEventType> = ResolveBehaviorEvent<TBehaviorEventType>,\n> = {\n /**\n * Editor Event that triggers this Behavior.\n */\n on: TBehaviorEventType\n /**\n * Predicate function that determines if the Behavior should be executed.\n * Returning a non-nullable value from the guard will pass the value to the\n * actions and execute them.\n */\n guard?: BehaviorGuard<TBehaviorEvent, TGuardResponse>\n /**\n * Array of Behavior Action sets.\n * Each set represents a step in the history stack.\n */\n actions: Array<BehaviorActionSet<TBehaviorEvent, TGuardResponse>>\n}\n\n/**\n * @beta\n *\n * @example\n *\n * ```tsx\n * const noLowerCaseA = defineBehavior({\n * on: 'insert.text',\n * guard: ({event, snapshot}) => event.text === 'a',\n * actions: [({event, snapshot}) => [{type: 'insert.text', text: 'A'}]],\n * })\n * ```\n *\n */\nexport function defineBehavior<\n TPayload extends Record<string, unknown>,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = CustomBehaviorEvent['type'],\n TGuardResponse = true,\n>(\n behavior: Behavior<\n TBehaviorEventType,\n TGuardResponse,\n ResolveBehaviorEvent<TBehaviorEventType, TPayload>\n >,\n): Behavior\nexport function defineBehavior<\n TPayload extends never = never,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends ResolveBehaviorEvent<\n TBehaviorEventType,\n TPayload\n > = ResolveBehaviorEvent<TBehaviorEventType, TPayload>,\n>(\n behavior: Behavior<TBehaviorEventType, TGuardResponse, TBehaviorEvent>,\n): Behavior {\n return behavior as unknown as Behavior\n}\n"],"names":["execute","event","type","forward","raise","effect","defineBehavior","behavior"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/behaviors/behavior.types.action.ts","../../src/behaviors/behavior.types.behavior.ts"],"sourcesContent":["import type {EditorSnapshot} from '../editor/editor-snapshot'\nimport type {EditorDom} from '../internal-utils/selection-elements'\nimport type {PickFromUnion} from '../type-utils'\nimport type {\n CustomBehaviorEvent,\n NativeBehaviorEvent,\n SyntheticBehaviorEvent,\n} from './behavior.types.event'\n\n/**\n * @beta\n */\nexport type BehaviorAction =\n | {\n type: 'execute'\n event: SyntheticBehaviorEvent\n }\n | {\n type: 'forward'\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'raise'\n event: SyntheticBehaviorEvent | CustomBehaviorEvent\n }\n | {\n type: 'effect'\n effect: () => void\n }\n\n/**\n * @beta\n */\nexport function execute(\n event: SyntheticBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'execute'> {\n return {type: 'execute', event}\n}\n\n/**\n * @beta\n */\nexport function forward(\n event: NativeBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'forward'> {\n return {type: 'forward', event}\n}\n\n/**\n * @beta\n */\nexport function raise(\n event: SyntheticBehaviorEvent | CustomBehaviorEvent,\n): PickFromUnion<BehaviorAction, 'type', 'raise'> {\n return {type: 'raise', event}\n}\n\n/**\n * @beta\n */\nexport function effect(\n effect: () => void,\n): PickFromUnion<BehaviorAction, 'type', 'effect'> {\n return {type: 'effect', effect}\n}\n\n/**\n * @beta\n */\nexport type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (\n payload: {\n snapshot: EditorSnapshot\n event: TBehaviorEvent\n dom: EditorDom\n },\n guardResponse: TGuardResponse,\n) => Array<BehaviorAction>\n","import type {BehaviorActionSet} from './behavior.types.action'\nimport type {\n BehaviorEvent,\n BehaviorEventTypeNamespace,\n CustomBehaviorEvent,\n ResolveBehaviorEvent,\n} from './behavior.types.event'\nimport type {BehaviorGuard} from './behavior.types.guard'\n\n/**\n * @beta\n */\nexport type Behavior<\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] =\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends\n ResolveBehaviorEvent<TBehaviorEventType> = ResolveBehaviorEvent<TBehaviorEventType>,\n> = {\n /**\n * Editor Event that triggers this Behavior.\n */\n on: TBehaviorEventType\n /**\n * Predicate function that determines if the Behavior should be executed.\n * Returning a non-nullable value from the guard will pass the value to the\n * actions and execute them.\n */\n guard?: BehaviorGuard<TBehaviorEvent, TGuardResponse>\n /**\n * Array of Behavior Action sets.\n * Each set represents a step in the history stack.\n */\n actions: Array<BehaviorActionSet<TBehaviorEvent, TGuardResponse>>\n}\n\n/**\n * @beta\n *\n * @example\n *\n * ```tsx\n * const noLowerCaseA = defineBehavior({\n * on: 'insert.text',\n * guard: ({event, snapshot}) => event.text === 'a',\n * actions: [({event, snapshot}) => [{type: 'insert.text', text: 'A'}]],\n * })\n * ```\n *\n */\nexport function defineBehavior<\n TPayload extends Record<string, unknown>,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = CustomBehaviorEvent['type'],\n TGuardResponse = true,\n>(\n behavior: Behavior<\n TBehaviorEventType,\n TGuardResponse,\n ResolveBehaviorEvent<TBehaviorEventType, TPayload>\n >,\n): Behavior\nexport function defineBehavior<\n TPayload extends never = never,\n TBehaviorEventType extends\n | '*'\n | `${BehaviorEventTypeNamespace}.*`\n | BehaviorEvent['type'] = BehaviorEvent['type'],\n TGuardResponse = true,\n TBehaviorEvent extends ResolveBehaviorEvent<\n TBehaviorEventType,\n TPayload\n > = ResolveBehaviorEvent<TBehaviorEventType, TPayload>,\n>(\n behavior: Behavior<TBehaviorEventType, TGuardResponse, TBehaviorEvent>,\n): Behavior {\n return behavior as unknown as Behavior\n}\n"],"names":["execute","event","type","forward","raise","effect","defineBehavior","behavior"],"mappings":"AAiCO,SAASA,QACdC,OACkD;AAC3C,SAAA;AAAA,IAACC,MAAM;AAAA,IAAWD;AAAAA,EAAK;AAChC;AAKO,SAASE,QACdF,OACkD;AAC3C,SAAA;AAAA,IAACC,MAAM;AAAA,IAAWD;AAAAA,EAAK;AAChC;AAKO,SAASG,MACdH,OACgD;AACzC,SAAA;AAAA,IAACC,MAAM;AAAA,IAASD;AAAAA,EAAK;AAC9B;AAKO,SAASI,OACdA,SACiD;AAC1C,SAAA;AAAA,IAACH,MAAM;AAAA,IAAUG,QAAAA;AAAAA,EAAM;AAChC;ACKO,SAASC,eAYdC,UACU;AACHA,SAAAA;AACT;"}
|