Skip to content

Commit

Permalink
feat(behavior): add 'key.down' and 'key.up' events
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Dec 3, 2024
1 parent c624130 commit 99d7335
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
48 changes: 47 additions & 1 deletion packages/editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,53 @@ export const PortableTextEditable = forwardRef<
if (!event.isDefaultPrevented()) {
slateEditor.pteWithHotKeys(event)
}
if (!event.isDefaultPrevented()) {
editorActor.send({
type: 'behavior event',
behaviorEvent: {
type: 'key.down',
keyboardEvent: {
key: event.nativeEvent.key,
code: event.nativeEvent.code,
altKey: event.nativeEvent.altKey,
ctrlKey: event.nativeEvent.ctrlKey,
metaKey: event.nativeEvent.metaKey,
shiftKey: event.nativeEvent.shiftKey,
},
nativeEvent: event.nativeEvent,
},
editor: slateEditor,
})
}
},
[props, editorActor, slateEditor],
)

const handleKeyUp = useCallback(
(event: KeyboardEvent<HTMLDivElement>) => {
if (props.onKeyUp) {
props.onKeyUp(event)
}
if (!event.isDefaultPrevented()) {
editorActor.send({
type: 'behavior event',
behaviorEvent: {
type: 'key.up',
keyboardEvent: {
key: event.nativeEvent.key,
code: event.nativeEvent.code,
altKey: event.nativeEvent.altKey,
ctrlKey: event.nativeEvent.ctrlKey,
metaKey: event.nativeEvent.metaKey,
shiftKey: event.nativeEvent.shiftKey,
},
nativeEvent: event.nativeEvent,
},
editor: slateEditor,
})
}
},
[props, slateEditor],
[props, editorActor, slateEditor],
)

const scrollSelectionIntoViewToSlate = useMemo(() => {
Expand Down Expand Up @@ -753,6 +798,7 @@ export const PortableTextEditable = forwardRef<
onDOMBeforeInput={handleOnBeforeInput}
onFocus={handleOnFocus}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onPaste={handlePaste}
readOnly={readOnly}
// We have implemented our own placeholder logic with decorations.
Expand Down
16 changes: 16 additions & 0 deletions packages/editor/src/editor/behavior/behavior.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
'effect': ({action}) => {
action.effect()
},
'key.down': () => {},
'key.up': () => {},
'paste': ({action}) => {
action.editor.insertData(action.clipboardData)
},
Expand Down Expand Up @@ -386,6 +388,20 @@ function performDefaultAction({
})
break
}
case 'key.down': {
behaviorActionImplementations['key.down']({
context,
action,
})
break
}
case 'key.up': {
behaviorActionImplementations['key.up']({
context,
action,
})
break
}
default: {
behaviorActionImplementations.paste({
context,
Expand Down
26 changes: 23 additions & 3 deletions packages/editor/src/editor/behavior/behavior.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ export type BehaviorEvent =
type: 'paste'
clipboardData: NonNullable<ClipboardEvent['clipboardData']>
}
| {
type: 'key.down'
keyboardEvent: Pick<
KeyboardEvent,
'key' | 'code' | 'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'
>
nativeEvent: KeyboardEvent
}
| {
type: 'key.up'
keyboardEvent: Pick<
KeyboardEvent,
'key' | 'code' | 'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'
>
nativeEvent: KeyboardEvent
}

/**
* @alpha
Expand All @@ -94,15 +110,19 @@ export type BehaviorGuard<
context,
event,
}: {
event: TBehaviorEvent
event: TBehaviorEvent['type'] extends 'key.down' | 'key.up'
? Omit<TBehaviorEvent, 'nativeEvent'>
: TBehaviorEvent
context: BehaviorContext
}) => TGuardResponse | false

/**
* @alpha
*/
export type BehaviorActionIntend =
| BehaviorEvent
| OmitFromUnion<BehaviorEvent, 'type', 'key.down' | 'key.up'>
| Omit<PickFromUnion<BehaviorEvent, 'type', 'key.down'>, 'nativeEvent'>
| Omit<PickFromUnion<BehaviorEvent, 'type', 'key.up'>, 'nativeEvent'>
| {
type: 'insert block object'
placement: 'auto' | 'after' | 'before'
Expand Down Expand Up @@ -208,7 +228,7 @@ export type BehaviorActionIntendSet<
event: PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>
},
guardResponse: TGuardResponse,
) => Array<BehaviorActionIntend>
) => Array<OmitFromUnion<BehaviorActionIntend, 'type', 'key.down' | 'key.up'>>

/**
* @alpha
Expand Down
6 changes: 6 additions & 0 deletions packages/editor/src/editor/editor-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ export const editorMachine = setup({
}

if (behaviorOverwritten) {
if (
event.behaviorEvent.type === 'key.down' ||
event.behaviorEvent.type === 'key.up'
) {
event.behaviorEvent.nativeEvent.preventDefault()
}
break
}
}
Expand Down

0 comments on commit 99d7335

Please sign in to comment.