-
Notifications
You must be signed in to change notification settings - Fork 2k
/
editor.js
154 lines (139 loc) · 3.96 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { TextControl, TextareaControl } from '@wordpress/components';
import { Icon } from '@wordpress/icons';
import * as createIssueUrl from 'new-github-issue-url';
import { SVG, Rect, Path } from '@wordpress/primitives';
import classNames from 'classnames';
import './editor.scss';
const githubIcon = (
<SVG
xmlns="http://www.w3.org/2000/svg"
aria-label="GitHub"
role="img"
viewBox="0 0 512 512"
width="24"
height="24"
>
<Rect width="512" height="512" rx="15%" fill="#1B1817" />
<Path
fill="#fff"
d="M335 499c14 0 12 17 12 17H165s-2-17 12-17c13 0 16-6 16-12l-1-50c-71 16-86-28-86-28-12-30-28-37-28-37-24-16 1-16 1-16 26 2 40 26 40 26 22 39 59 28 74 22 2-17 9-28 16-35-57-6-116-28-116-126 0-28 10-51 26-69-3-6-11-32 3-67 0 0 21-7 70 26 42-12 86-12 128 0 49-33 70-26 70-26 14 35 6 61 3 67 16 18 26 41 26 69 0 98-60 120-117 126 10 8 18 24 18 48l-1 70c0 6 3 12 16 12z"
/>
</SVG>
);
const Shell = ( { as: El = 'div', className, title, subTitle, icon, body, ...elementProps } ) => {
return (
<El { ...elementProps } className={ classNames( 'wp-block-a8c-github-template', className ) }>
{ typeof icon === 'string' ? (
<div className="github-template__icon">{ icon }</div>
) : (
<Icon className="github-template__icon" icon={ icon || githubIcon } />
) }
<span className="github-template__title">{ title }</span>
<div className="github-template__sub-title">{ subTitle }</div>
{ body && <span className="github-template__body">{ body }</span> }
</El>
);
};
const Edit = ( {
onChangeTitle,
onChangeUserOrOrg,
onChangeRepoName,
onChangeBody,
title,
userOrOrg,
repo,
body,
} ) => (
<Shell
className="is-edit"
title={ <TextControl placeholder="Issue Title" onChange={ onChangeTitle } value={ title } /> }
subTitle={
<>
<TextControl
placeholder="User or Organization"
onChange={ onChangeUserOrOrg }
value={ userOrOrg }
/>
/
<TextControl placeholder="Repository" onChange={ onChangeRepoName } value={ repo } />
</>
}
body={ <TextareaControl placeholder="Issue Body" onChange={ onChangeBody } value={ body } /> }
/>
);
const View = ( { title, userOrOrg, repo, ...rest } ) => (
<Shell
title={ `${ __( 'Create Issue:' ) } ${ title }` }
subTitle={ `${ userOrOrg }/${ repo }` }
{ ...rest }
/>
);
const Invalid = () => (
<Shell
className="is-warning"
title={ __( 'Please fill in the required fields' ) }
subTitle={ __(
'Title, Org, and Repository are required. Select this block to open the form and fill them.'
) }
icon="⚠"
/>
);
registerBlockType( 'a8c/github-issue-template', {
title: __( 'Github Issue Template', 'a8c' ),
icon: githubIcon,
category: 'layout',
attributes: {
userOrOrg: {
type: 'string',
},
repo: {
type: 'string',
},
title: {
type: 'string',
},
body: {
type: 'string',
},
},
edit: ( { setAttributes, attributes, attributes: { title, userOrOrg, repo }, isSelected } ) => {
const handlers = {
onChangeUserOrOrg( newUserOrOrg ) {
setAttributes( { userOrOrg: newUserOrOrg } );
},
onChangeRepoName( newRepo ) {
setAttributes( { repo: newRepo } );
},
onChangeTitle( newTitle ) {
setAttributes( { title: newTitle } );
},
onChangeBody( newBody ) {
setAttributes( { body: newBody } );
},
};
const isValid = title && userOrOrg && repo;
const { body, ...viewAttributes } = attributes;
if ( isSelected ) {
return <Edit { ...handlers } { ...attributes } />;
}
return isValid ? <View { ...viewAttributes } /> : <Invalid />;
},
save: ( { attributes: { userOrOrg, repo, title, body } } ) => {
const isValid = title && userOrOrg && repo;
if ( isValid ) {
const url = createIssueUrl( {
title,
repo,
user: userOrOrg,
body,
} );
const viewAttributes = { title, userOrOrg, repo };
return <View as="a" href={ url } { ...viewAttributes } />;
}
},
} );