mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-07 21:54:53 +03:00
implement 'get from clipboard' button
This commit is contained in:
parent
d7a0a4da17
commit
921c3feb68
45
demo/ClipboardImporter.tsx
Normal file
45
demo/ClipboardImporter.tsx
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import * as React from 'react';
|
||||||
|
import styled from '../src/styled-components';
|
||||||
|
|
||||||
|
const Button = styled.button`
|
||||||
|
background-color: #fff;
|
||||||
|
color: #333;
|
||||||
|
padding: 2px 10px;
|
||||||
|
touch-action: manipulation;
|
||||||
|
cursor: pointer;
|
||||||
|
user-select: none;
|
||||||
|
border: none;
|
||||||
|
font-size: 16px;
|
||||||
|
height: 28px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
vertical-align: middle;
|
||||||
|
line-height: 1;
|
||||||
|
outline: none;
|
||||||
|
white-space: nowrap;
|
||||||
|
`;
|
||||||
|
|
||||||
|
export interface ClipboardImporterProps {
|
||||||
|
onPaste?: (val: any) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class ClipboardImporter extends React.Component<any, any> {
|
||||||
|
render() {
|
||||||
|
return <Button title='Get spec from clipboard' onClick={() => this.import()}>📋</Button>;
|
||||||
|
}
|
||||||
|
|
||||||
|
private import() {
|
||||||
|
navigator.clipboard.readText()
|
||||||
|
.then(text => {
|
||||||
|
if (!text) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const asJson = JSON.parse(text);
|
||||||
|
if (asJson) {
|
||||||
|
this.props.onPaste(asJson);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
console.error('Failed to get spec from clipboard')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import styled from 'styled-components';
|
||||||
import { resolve as urlResolve } from 'url';
|
import { resolve as urlResolve } from 'url';
|
||||||
import { RedocStandalone } from '../src';
|
import { RedocStandalone } from '../src';
|
||||||
import ComboBox from './ComboBox';
|
import ComboBox from './ComboBox';
|
||||||
|
import ClipboardImporter from './ClipboardImporter';
|
||||||
|
|
||||||
const demos = [
|
const demos = [
|
||||||
{ value: 'https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml', label: 'Instagram' },
|
{ value: 'https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml', label: 'Instagram' },
|
||||||
|
@ -20,7 +21,7 @@ const DEFAULT_SPEC = 'openapi.yaml';
|
||||||
|
|
||||||
class DemoApp extends React.Component<
|
class DemoApp extends React.Component<
|
||||||
{},
|
{},
|
||||||
{ specUrl: string; dropdownOpen: boolean; cors: boolean }
|
{ spec?: object; specUrl: string; dropdownOpen: boolean; cors: boolean }
|
||||||
> {
|
> {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
@ -38,14 +39,23 @@ class DemoApp extends React.Component<
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
|
spec: undefined,
|
||||||
specUrl: url,
|
specUrl: url,
|
||||||
dropdownOpen: false,
|
dropdownOpen: false,
|
||||||
cors,
|
cors,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handlePaste = (spec: object) => {
|
||||||
|
this.setState({
|
||||||
|
spec,
|
||||||
|
specUrl: '',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
handleChange = (url: string) => {
|
handleChange = (url: string) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
|
spec: undefined,
|
||||||
specUrl: url,
|
specUrl: url,
|
||||||
});
|
});
|
||||||
window.history.pushState(
|
window.history.pushState(
|
||||||
|
@ -85,6 +95,7 @@ class DemoApp extends React.Component<
|
||||||
/>
|
/>
|
||||||
</a>
|
</a>
|
||||||
<ControlsContainer>
|
<ControlsContainer>
|
||||||
|
<ClipboardImporter onPaste={this.handlePaste}/>
|
||||||
<ComboBox
|
<ComboBox
|
||||||
placeholder={'URL to a spec to try'}
|
placeholder={'URL to a spec to try'}
|
||||||
options={demos}
|
options={demos}
|
||||||
|
@ -105,6 +116,7 @@ class DemoApp extends React.Component<
|
||||||
/>
|
/>
|
||||||
</Heading>
|
</Heading>
|
||||||
<RedocStandalone
|
<RedocStandalone
|
||||||
|
spec={this.state.spec}
|
||||||
specUrl={proxiedUrl}
|
specUrl={proxiedUrl}
|
||||||
options={{ scrollYOffset: 'nav', untrustedSpec: true }}
|
options={{ scrollYOffset: 'nav', untrustedSpec: true }}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user