File size: 5,414 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { RichText, InspectorControls } from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import {
	BaseControl,
	Button,
	CustomSelectControl,
	DatePicker,
	Dropdown,
	TextControl,
	PanelBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import moment from 'moment';

import './editor.scss';
const name = 'a8c/task';

const edit = ( { attributes, setAttributes, mergeBlocks, onReplace, className } ) => {
	const { assignedTo, content, placeholder, status, dueDate, startDate } = attributes;
	const todoClass = clsx( 'wp-block-todo', className, { 'is-checked': status === 'done' } );

	const options = [
		{
			key: 'new',
			name: 'New',
		},
		{
			key: 'in-progress',
			name: 'In Progress',
		},
		{
			key: 'done',
			name: 'Done',
		},
	];

	const dueDateDisplay = dueDate ? moment( dueDate ).format( 'll' ) : '';
	const startDateDisplay = startDate ? moment( startDate ).format( 'll' ) : '';

	return (
		<>
			<InspectorControls>
				<PanelBody title={ __( 'Status' ) }>
					<CustomSelectControl
						label={ __( 'Status' ) }
						options={ options }
						value={ options.find( ( option ) => option.key === status ) || options[ 0 ] }
						onChange={ ( value ) => setAttributes( { status: value.selectedItem.key } ) }
					/>
				</PanelBody>
				<PanelBody title={ __( 'Assignment' ) }>
					<TextControl
						label={ __( 'Username' ) }
						value={ assignedTo || '' }
						onChange={ ( value ) => setAttributes( { assignedTo: value } ) }
					/>
				</PanelBody>
				<PanelBody title={ __( 'Date' ) }>
					<Dropdown
						renderToggle={ ( { isOpen, onToggle } ) => (
							<>
								{ startDate ? (
									<BaseControl
										label="Start Date"
										id="wp-block-task__start-date-button"
										className="wp-block-task__date-button"
									>
										<Button
											id="wp-block-task__start-date-button"
											isLink={ ! startDate }
											isLarge={ !! startDate }
											onClick={ onToggle }
											aria-expanded={ isOpen }
										>
											{ startDateDisplay || 'Set start date' }
										</Button>
									</BaseControl>
								) : (
									<Button
										id="wp-block-task__start-date-button"
										isLink={ ! startDate }
										isLarge={ !! startDate }
										onClick={ onToggle }
										aria-expanded={ isOpen }
										style={ { marginBottom: '20px' } }
									>
										{ startDateDisplay || 'Set start date' }
									</Button>
								) }
							</>
						) }
						renderContent={ () => (
							<DatePicker
								currentDate={ startDate }
								onChange={ ( date ) => setAttributes( { startDate: date } ) }
							/>
						) }
					/>
					<Button isLink onClick={ () => setAttributes( { startDate: '' } ) }>
						Clear
					</Button>
					<Dropdown
						renderToggle={ ( { isOpen, onToggle } ) => (
							<BaseControl
								label="Due Date"
								id="wp-block-task__due-date-button"
								className="wp-block-task__date-button"
							>
								<Button
									id="wp-block-task__due-date-button"
									isLarge
									onClick={ onToggle }
									aria-expanded={ isOpen }
								>
									{ dueDateDisplay || 'No due date' }
								</Button>
							</BaseControl>
						) }
						renderContent={ () => (
							<DatePicker
								currentDate={ dueDate }
								onChange={ ( date ) => setAttributes( { dueDate: date } ) }
							/>
						) }
					/>
					<Button isLink onClick={ () => setAttributes( { dueDate: '' } ) }>
						Clear
					</Button>
				</PanelBody>
			</InspectorControls>
			<div className={ todoClass }>
				{ ( status === 'done' || status === 'new' ) && (
					<Button
						className="wp-block-todo__status"
						onClick={ () => setAttributes( { status: 'in-progress' } ) }
					/>
				) }
				{ status === 'in-progress' && (
					<Button
						className="wp-block-todo__is-in-progress"
						onClick={ () => setAttributes( { status: 'done' } ) }
					>
						In Progress
					</Button>
				) }
				<RichText
					identifier="content"
					wrapperClassName="wp-block-todo__text"
					value={ content }
					onChange={ ( value ) => setAttributes( { content: value } ) }
					onMerge={ mergeBlocks }
					onSplit={ ( value ) => {
						if ( ! content.length ) {
							return createBlock( 'core/paragraph' );
						}

						if ( ! value ) {
							return createBlock( name );
						}

						return createBlock( name, {
							...attributes,
							content: value,
						} );
					} }
					onReplace={ onReplace }
					onRemove={ onReplace ? () => onReplace( [] ) : undefined }
					className={ className }
					placeholder={ placeholder || __( 'Add task…' ) }
				/>
				{ assignedTo && (
					<div className="wp-block-todo__assigned">
						<span>@{ assignedTo }</span>
						<span className="wp-block-todo__avatar">{ assignedTo[ 0 ] }</span>
					</div>
				) }
				{ dueDate && (
					<span className="wp-block-todo__date">
						<Dropdown
							renderToggle={ ( { isOpen, onToggle } ) => (
								<Button onClick={ onToggle } aria-expanded={ isOpen }>
									{ dueDateDisplay || 'No due date' }
								</Button>
							) }
							renderContent={ () => (
								<DatePicker
									currentDate={ dueDate }
									onChange={ ( date ) => setAttributes( { dueDate: date } ) }
								/>
							) }
						/>
					</span>
				) }
			</div>
		</>
	);
};

export default edit;