Skip to content

Commit

Permalink
fix(behavior): add context/event back in `BehaviorActionIntendSet…
Browse files Browse the repository at this point in the history
…` callback
  • Loading branch information
christianhg committed Dec 9, 2024
1 parent 8d9d669 commit af9e9e4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 67 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/editor/behavior/behavior.code-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createCodeEditorBehaviors(config: CodeEditorBehaviorsConfig) {
return {paths: selectedBlocks.map((block) => block.path)}
},
actions: [
({paths}) =>
(_, {paths}) =>
paths.map((at) => ({
type: 'move.block up',
at,
Expand All @@ -65,7 +65,7 @@ export function createCodeEditorBehaviors(config: CodeEditorBehaviorsConfig) {
return {paths: selectedBlocks.map((block) => block.path).reverse()}
},
actions: [
({paths}) =>
(_, {paths}) =>
paths.map((at) => ({
type: 'move.block down',
at,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const deletingEmptyTextBlockAfterBlockObject = defineBehavior({
return false
},
actions: [
({focusTextBlock, previousBlock}) => [
(_, {focusTextBlock, previousBlock}) => [
{
type: 'delete.block',
blockPath: focusTextBlock.path,
Expand Down Expand Up @@ -108,7 +108,7 @@ const deletingEmptyTextBlockBeforeBlockObject = defineBehavior({
return false
},
actions: [
({focusTextBlock, nextBlock}) => [
(_, {focusTextBlock, nextBlock}) => [
{
type: 'delete.block',
blockPath: focusTextBlock.path,
Expand Down
22 changes: 6 additions & 16 deletions packages/editor/src/editor/behavior/behavior.core.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import {defineBehavior} from './behavior.types'

const decoratorAdd = defineBehavior({
on: 'decorator.add',
guard: ({event}) => ({decorator: event.decorator}),
actions: [
({decorator}) => [
{
type: 'decorator.add',
decorator,
},
({event}) => [
event,
{
type: 'reselect',
},
Expand All @@ -20,11 +16,8 @@ const decoratorRemove = defineBehavior({
on: 'decorator.remove',
guard: ({event}) => ({decorator: event.decorator}),
actions: [
({decorator}) => [
{
type: 'decorator.remove',
decorator,
},
({event}) => [
event,
{
type: 'reselect',
},
Expand All @@ -36,11 +29,8 @@ const decoratorToggle = defineBehavior({
on: 'decorator.toggle',
guard: ({event}) => ({decorator: event.decorator}),
actions: [
({decorator}) => [
{
type: 'decorator.toggle',
decorator,
},
({event}) => [
event,
{
type: 'reselect',
},
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/editor/behavior/behavior.core.lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const clearListOnBackspace = defineBehavior({
return false
},
actions: [
({focusTextBlock}) => [
(_, {focusTextBlock}) => [
{
type: 'text block.unset',
props: ['listItem', 'level'],
Expand Down Expand Up @@ -70,7 +70,7 @@ const unindentListOnBackspace = defineBehavior({
return false
},
actions: [
({focusTextBlock, level}) => [
(_, {focusTextBlock, level}) => [
{
type: 'text block.set',
level,
Expand All @@ -97,7 +97,7 @@ const clearListOnEnter = defineBehavior({
return {focusListBlock}
},
actions: [
({focusListBlock}) => [
(_, {focusListBlock}) => [
{
type: 'text block.unset',
props: ['listItem', 'level'],
Expand Down Expand Up @@ -136,7 +136,7 @@ const indentListOnTab = defineBehavior({
return false
},
actions: [
({selectedListBlocks}) =>
(_, {selectedListBlocks}) =>
selectedListBlocks.map((selectedListBlock) => ({
type: 'text block.set',
level: Math.min(
Expand Down Expand Up @@ -177,7 +177,7 @@ const unindentListOnShiftTab = defineBehavior({
return false
},
actions: [
({selectedListBlocks}) =>
(_, {selectedListBlocks}) =>
selectedListBlocks.map((selectedListBlock) => ({
type: 'text block.set',
level: Math.min(
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/editor/behavior/behavior.links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function createLinkBehaviors(config: LinkBehaviorsConfig) {
return false
},
actions: [
({annotation}) => [
(_, {annotation}) => [
{
type: 'annotation.add',
annotation,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function createLinkBehaviors(config: LinkBehaviorsConfig) {
return false
},
actions: [
({annotation, url}) => [
(_, {annotation, url}) => [
{
type: 'insert.span',
text: url,
Expand Down
30 changes: 10 additions & 20 deletions packages/editor/src/editor/behavior/behavior.markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
text: ' ',
},
],
({focusTextBlock, style}) => [
(_, {focusTextBlock, style}) => [
{
type: 'text block.unset',
props: ['listItem', 'level'],
Expand Down Expand Up @@ -163,13 +163,13 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
},
actions: [
({hrCharacter}) => [
(_, {hrCharacter}) => [
{
type: 'insert.text',
text: hrCharacter,
},
],
({hrObject, hrBlockOffsets}) => [
(_, {hrObject, hrBlockOffsets}) => [
{
type: 'insert.block object',
placement: 'before',
Expand Down Expand Up @@ -198,13 +198,13 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return {hrCharacters, hrObject, focusBlock}
},
actions: [
({hrCharacters}) => [
(_, {hrCharacters}) => [
{
type: 'insert.text',
text: hrCharacters,
},
],
({hrObject, focusBlock}) =>
(_, {hrObject, focusBlock}) =>
isPortableTextTextBlock(focusBlock.node)
? [
{
Expand Down Expand Up @@ -288,13 +288,8 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
},
actions: [
() => [
{
type: 'insert.text',
text: ' ',
},
],
({focusTextBlock, style, level}) => [
({event}) => [event],
(_, {focusTextBlock, style, level}) => [
{
type: 'text block.unset',
props: ['listItem', 'level'],
Expand Down Expand Up @@ -347,7 +342,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
},
actions: [
({defaultStyle, focusTextBlock}) => [
(_, {defaultStyle, focusTextBlock}) => [
{
type: 'text block.set',
style: defaultStyle,
Expand Down Expand Up @@ -430,13 +425,8 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
},
actions: [
() => [
{
type: 'insert.text',
text: ' ',
},
],
({focusTextBlock, style, listItem, listItemLength}) => [
({event}) => [event],
(_, {focusTextBlock, style, listItem, listItemLength}) => [
{
type: 'text block.set',
listItem,
Expand Down
48 changes: 29 additions & 19 deletions packages/editor/src/editor/behavior/behavior.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ export type NativeBehaviorEvent =
data: DataTransfer
}

/**
* @alpha
*/
export type BehaviorGuard<
TAnyBehaviorEvent extends BehaviorEvent,
TGuardResponse,
> = ({
context,
event,
}: {
context: EditorContext
event: TAnyBehaviorEvent
}) => TGuardResponse | false

/**
* @alpha
*/
Expand Down Expand Up @@ -238,32 +224,56 @@ export type BehaviorEvent = SyntheticBehaviorEvent | NativeBehaviorEvent
* @alpha
*/
export type Behavior<
TAnyBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
TGuardResponse = true,
> = {
/**
* The internal editor event that triggers this behavior.
*/
on: TAnyBehaviorEventType
on: TBehaviorEventType
/**
* Predicate function that determines if the behavior should be executed.
* Returning a non-nullable value from the guard will pass the value to the
* actions and execute them.
*/
guard?: BehaviorGuard<
PickFromUnion<BehaviorEvent, 'type', TAnyBehaviorEventType>,
PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>,
TGuardResponse
>
/**
* Array of behavior action sets.
*/
actions: Array<BehaviorActionIntendSet<TGuardResponse>>
actions: Array<BehaviorActionIntendSet<TBehaviorEventType, TGuardResponse>>
}

/**
* @alpha
*/
export type BehaviorActionIntendSet<TGuardResponse = true> = (
export type BehaviorGuard<
TBehaviorEvent extends BehaviorEvent,
TGuardResponse,
> = ({
context,
event,
}: {
context: EditorContext
event: TBehaviorEvent
}) => TGuardResponse | false

/**
* @alpha
*/
export type BehaviorActionIntendSet<
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
TGuardResponse = true,
> = (
{
context,
event,
}: {
context: EditorContext
event: PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>
},
guardResponse: TGuardResponse,
) => Array<BehaviorActionIntend>

Expand Down
5 changes: 4 additions & 1 deletion packages/editor/src/editor/editor-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ export const editorMachine = setup({
}

const actionIntendSets = eventBehavior.actions.map((actionSet) =>
actionSet(shouldRun),
actionSet(
{context: editorContext, event: event.behaviorEvent},
shouldRun,
),
)

for (const actionIntends of actionIntendSets) {
Expand Down

0 comments on commit af9e9e4

Please sign in to comment.