mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-19 06:42:16 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 825bdc6a2c | |||
| e4687a9177 |
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@gitbook/embed": minor
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Emit an `escape` event from the Docs Embed when Escape is pressed inside it and nothing in the embed used it, so the host can close its own panel.
|
||||
@@ -127,6 +127,11 @@ frame.configure({
|
||||
frame.on('close', () => {
|
||||
console.log('Frame closed');
|
||||
});
|
||||
|
||||
// Escape was pressed inside the frame and nothing in it (a menu, a dialog) used it
|
||||
frame.on('escape', () => {
|
||||
closeHelpPanel();
|
||||
});
|
||||
```
|
||||
|
||||
## As React components
|
||||
@@ -222,7 +227,7 @@ function MyComponent() {
|
||||
- `frame.postUserMessage(message: string)` → `void`
|
||||
- `frame.clearChat()` → `void`
|
||||
- `frame.configure(settings: Partial<GitBookEmbeddableConfiguration>)` → `void`
|
||||
- `frame.on(event: string, listener: Function)` → `() => void` (unsubscribe)
|
||||
- `frame.on(event: string, listener: Function)` → `() => void` (unsubscribe). Events: `close`, `escape`
|
||||
|
||||
### React Components
|
||||
|
||||
|
||||
@@ -38,7 +38,10 @@ export type GitBookFrameClient = {
|
||||
configure: (settings: Partial<GitBookEmbeddableConfiguration>) => void;
|
||||
|
||||
/**
|
||||
* Register an event listener.
|
||||
* Register an event listener and get back a function that removes it.
|
||||
*
|
||||
* - `close`: the built-in close button was clicked.
|
||||
* - `escape`: Escape was pressed inside the frame and nothing in it (a menu, a dialog) used it.
|
||||
*/
|
||||
on: (event: string, listener: (...args: any[]) => void) => () => void;
|
||||
};
|
||||
@@ -64,11 +67,9 @@ export function createGitBookFrame(iframe: HTMLIFrameElement): GitBookFrameClien
|
||||
|
||||
channel.receive((message: FrameToParentMessage) => {
|
||||
console.log('[gitbook:embed] received message', message);
|
||||
if (message.type === 'close') {
|
||||
const listeners = events.get('close') || [];
|
||||
if (listeners) {
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
if (message.type === 'close' || message.type === 'escape') {
|
||||
const listeners = events.get(message.type) || [];
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -108,7 +109,7 @@ export function createGitBookFrame(iframe: HTMLIFrameElement): GitBookFrameClien
|
||||
return () => {
|
||||
events.set(
|
||||
event,
|
||||
listeners.filter((l) => l !== listener)
|
||||
(events.get(event) || []).filter((l) => l !== listener)
|
||||
);
|
||||
};
|
||||
},
|
||||
|
||||
@@ -137,6 +137,11 @@ export type ParentToFrameMessage =
|
||||
/**
|
||||
* Messages sent from the frame to the parent.
|
||||
*/
|
||||
export type FrameToParentMessage = {
|
||||
type: 'close';
|
||||
};
|
||||
export type FrameToParentMessage =
|
||||
| {
|
||||
type: 'close';
|
||||
}
|
||||
| {
|
||||
/** Escape was pressed inside the frame and nothing in the frame handled it. */
|
||||
type: 'escape';
|
||||
};
|
||||
|
||||
@@ -5,7 +5,11 @@ import React, { useEffect, useRef } from 'react';
|
||||
import { createStore, useStore } from 'zustand';
|
||||
|
||||
import type { GitBookIntegrationTool } from '@gitbook/browser-types';
|
||||
import type { GitBookEmbeddableConfiguration, ParentToFrameMessage } from '@gitbook/embed';
|
||||
import type {
|
||||
FrameToParentMessage,
|
||||
GitBookEmbeddableConfiguration,
|
||||
ParentToFrameMessage,
|
||||
} from '@gitbook/embed';
|
||||
|
||||
import { integrationsAssistantTools } from '../Integrations';
|
||||
import { Button, LinkContext, type LinkContextType } from '../primitives';
|
||||
@@ -69,6 +73,27 @@ export function EmbeddableIframeAPI(props: { baseURL: string }) {
|
||||
});
|
||||
}, [chatController]);
|
||||
|
||||
React.useEffect(() => {
|
||||
const channel = getChannel();
|
||||
if (!channel) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Menus, dialogs and the zoomed image call preventDefault when Escape closes them.
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (
|
||||
event.key === 'Escape' &&
|
||||
!event.defaultPrevented &&
|
||||
!event.isComposing &&
|
||||
!event.repeat
|
||||
) {
|
||||
channel.send({ type: 'escape' } satisfies FrameToParentMessage);
|
||||
}
|
||||
};
|
||||
window.addEventListener('keydown', onKeyDown);
|
||||
return () => window.removeEventListener('keydown', onKeyDown);
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
const channel = getChannel();
|
||||
if (!channel) {
|
||||
|
||||
@@ -206,7 +206,6 @@ export const Input = React.forwardRef<InputElement, InputProps>((props, passedRe
|
||||
event.preventDefault();
|
||||
handleSubmit();
|
||||
} else if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
event.currentTarget.blur();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -176,6 +176,7 @@ function ZoomImageModal(props: {
|
||||
React.useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user