Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib): add event listener for ESC key handler #404

Merged
merged 8 commits into from
Sep 25, 2024
41 changes: 26 additions & 15 deletions packages/field-plugin/src/createFieldPlugin/createFieldPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createPluginActions, ValidateContent } from './createPluginActions'
import { createHeightChangeListener } from './createHeightChangeListener'
import { createKeydownEscListener } from './createKeydownEscListener'
import { disableDefaultStoryblokStyles } from './disableDefaultStoryblokStyles'
import { pluginUrlParamsFromUrl } from '../messaging'
import { FieldPluginResponse } from './FieldPluginResponse'
Expand Down Expand Up @@ -48,6 +49,8 @@
}

const { uid, host } = params

// ToDo: In development we need to load localhost:3300

Check warning on line 53 in packages/field-plugin/src/createFieldPlugin/createFieldPlugin.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected 'todo' comment: 'ToDo: In development we need to load...'
Dawntraoz marked this conversation as resolved.
Show resolved Hide resolved
const origin =
host === 'plugin-sandbox.storyblok.com'
? 'https://plugin-sandbox.storyblok.com'
Expand Down Expand Up @@ -83,24 +86,31 @@
Exclude<typeof validateContent, undefined>
>['content']

const { actions, messageCallbacks, onHeightChange, initialize } =
createPluginActions<InferredContent>({
uid,
postToContainer,
onUpdateState: (data) => {
onUpdateState({
type: 'loaded',
data,
actions,
})
},
validateContent:
validateContent ||
((content) => ({ content: content as InferredContent })),
})
const {
actions,
messageCallbacks,
onHeightChange,
onKeydownEsc,
initialize,
} = createPluginActions<InferredContent>({
uid,
postToContainer,
onUpdateState: (data) => {
onUpdateState({
type: 'loaded',
data,
actions,
})
},
validateContent:
validateContent ||
((content) => ({ content: content as InferredContent })),
})

const cleanupHeightChangeListener = createHeightChangeListener(onHeightChange)

const cleanupKeydownEscListener = createKeydownEscListener(onKeydownEsc)
Dawntraoz marked this conversation as resolved.
Show resolved Hide resolved

const cleanupMessageListenerSideEffects = createPluginMessageListener(
params.uid,
origin,
Expand All @@ -112,6 +122,7 @@
return () => {
cleanupMessageListenerSideEffects()
cleanupHeightChangeListener()
cleanupKeydownEscListener()
cleanupStyleSideEffects()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @returns function for cleaning up side effects
*/

export const createKeydownEscListener = (onChange: () => void) => {
const handleEsc = (event: KeyboardEvent) => {
const key = event.key
if (key === 'Escape') {
onChange()
}
}

document.addEventListener('keydown', handleEsc)
return () => document.removeEventListener('keydown', handleEsc)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
messageCallbacks: PluginMessageCallbacks
// This function is called whenever the height changes
onHeightChange: (height: number) => void
// This function is called whenever the ESC key is pressed
onKeydownEsc: () => void
// This initiates the plugin
initialize: Initialize<Content>
}
Expand Down Expand Up @@ -63,9 +65,9 @@
popCallback('asset', data.callbackId)?.(data)
}
const onUnknownMessage: OnUnknownPluginMessage = (data) => {
// TODO remove side-effect, making functions in this file pure.

Check warning on line 68 in packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected 'todo' comment: 'TODO remove side-effect, making...'
// perhaps only show this message in development mode?
console.debug(

Check warning on line 70 in packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement
`Plugin received a message from container of an unknown action type "${
data.action
}". You may need to upgrade the version of the @storyblok/field-plugin library. Full message: ${JSON.stringify(
Expand All @@ -86,6 +88,10 @@
postToContainer(heightChangeMessage(uid, height))
}

const onKeydownEsc = () => {
postToContainer(modalChangeMessage({ uid, status: false }))
Dawntraoz marked this conversation as resolved.
Show resolved Hide resolved
}

return {
actions: {
setContent: (content) => {
Expand Down Expand Up @@ -131,6 +137,7 @@
},
messageCallbacks,
onHeightChange,
onKeydownEsc,
initialize: () => {
return new Promise((resolve) => {
const callbackId = pushCallback('loaded', (message) =>
Expand Down
Loading