File size: 6,879 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { registerBlockType } from '@wordpress/blocks';
import { Button } from '@wordpress/components';
import { RichText } from '@wordpress/editor';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Icon, plus } from '@wordpress/icons';
import clsx from 'clsx';
import { ItemEditor } from './item';

import './editor.scss';

const blockAttributes = {
	list: {
		type: 'array',
		source: 'children',
		selector: 'ul',
		default: [],
	},
};

const itemLineRegex = /^([ ]{0,3})([xo#*-])(\s+)(.*)/;

const edit = class extends Component {
	constructor() {
		super( ...arguments );
		this.addNewItem = this.addNewItem.bind( this );
		this.updateItem = this.updateItem.bind( this );
		this.deleteItem = this.deleteItem.bind( this );
		this.insertNewItemAfter = this.insertNewItemAfter.bind( this );

		// set the initial items in the state based on the markup that was saved
		const list = this.props.attributes.list.filter(
			( item ) => typeof item !== 'string' || item.trim() !== ''
		);
		const items = list.map( ( item ) => {
			const itemEntry = {};
			const children = item.props.children;
			const levelSpan = children[ 0 ];
			const statusSpan = children[ 1 ];
			const valueSpan = children[ 2 ];
			itemEntry.level =
				undefined !== levelSpan.props.children ? levelSpan.props.children.length : 0;
			itemEntry.done = 'x' === statusSpan.props.children;
			// children if it came from html, value if it came from a migration
			itemEntry.value = valueSpan.props.children || valueSpan.props.value;
			return itemEntry;
		} );

		this.state = {
			items,
			newItemAt: undefined,
		};
	}

	componentDidMount() {
		if ( 0 === this.state.items.length ) {
			this.addNewItem();
		}
	}

	getNewItem() {
		return {
			value: [],
			done: false,
			level: 0,
		};
	}

	insertNewItemAfter( index ) {
		const { items } = this.state;
		items.splice( index + 1, 0, this.getNewItem() );
		this.setState( { items: items, newItemAt: index + 1 } );
	}

	addNewItem() {
		const { items } = this.state;
		items.push( this.getNewItem() );
		this.setState( { items, newItemAt: items.length - 1 } );
	}

	swapItems( itemIdx, newIdx ) {
		const { items } = this.state;
		const item = items[ itemIdx ];
		const tmp = items[ newIdx ];
		items[ newIdx ] = item;
		items[ itemIdx ] = tmp;
		this.setState( { items } );
		this.props.setAttributes( { list: this.renderElements( items ) } );
	}

	moveUp( itemIdx ) {
		if ( itemIdx > 0 ) {
			this.swapItems( itemIdx, itemIdx - 1 );
		}
	}

	moveDown( itemIdx ) {
		if ( itemIdx < this.state.items.length - 1 ) {
			this.swapItems( itemIdx, itemIdx + 1 );
		}
	}

	moveLeft( itemIdx ) {
		const { items } = this.state;
		if ( items[ itemIdx ].level > 0 ) {
			items[ itemIdx ].level--;
		}
		this.setState( { items } );
		this.props.setAttributes( { list: this.renderElements( items ) } );
	}

	moveRight( itemIdx ) {
		const { items } = this.state;
		if ( items[ itemIdx ].level < 3 && itemIdx > 0 ) {
			items[ itemIdx ].level++;
		}
		this.setState( { items } );
		this.props.setAttributes( { list: this.renderElements( items ) } );
	}

	deleteItem( index ) {
		const { items } = this.state;
		const newItems = items.filter( ( item, itemIndex ) => {
			return index !== itemIndex;
		} );
		this.setState( { items: newItems, newItemAt: undefined } );
		this.props.setAttributes( { list: this.renderElements( newItems ) } );
	}

	updateItem() {
		this.props.setAttributes( { list: this.renderElements( this.state.items ) } );
	}

	renderElements( items ) {
		const x = items.map( ( item ) => {
			const done = item.done ? 'x' : 'o';
			return (
				<li>
					<span>{ ''.repeat( item.level ) }</span>
					<span>{ done }</span>
					<RichText.Content tagName="span" value={ item.value } />
				</li>
			);
		} );
		return x;
	}

	render() {
		const { className } = this.props;
		const { items, newItemAt } = this.state;
		return (
			<div className={ className }>
				<ul className={ `${ className }__list` }>
					{ items.map( ( item, itemIndex ) => {
						const moveUp = () => {
							this.moveUp( itemIndex );
						};
						const moveDown = () => {
							this.moveDown( itemIndex );
						};
						const moveLeft = () => {
							this.moveLeft( itemIndex );
						};
						const moveRight = () => {
							this.moveRight( itemIndex );
						};
						const onDelete = () => {
							this.deleteItem( itemIndex );
						};
						const onChange = ( updatedItem ) => {
							this.updateItem( updatedItem, itemIndex );
						};
						const onSplit = () => {
							this.insertNewItemAfter( itemIndex );
						};
						const classNames = clsx( `${ className }__item`, {
							[ `${ className }__item--done` ]: item.done,
						} );

						// if we've inserted an item at this index, and it does not have a value, request focus
						const shouldFocusThisItem =
							itemIndex === newItemAt && ( ! item.value || 0 === item.value.length );

						return (
							<ItemEditor
								moveUp={ moveUp }
								moveDown={ moveDown }
								moveLeft={ moveLeft }
								moveRight={ moveRight }
								canMoveUp={ itemIndex > 0 }
								canMoveDown={ itemIndex < items.length - 1 }
								classNames={ classNames }
								item={ item }
								onDelete={ onDelete }
								onChange={ onChange }
								onSplit={ onSplit }
								shouldFocus={ shouldFocusThisItem }
							/>
						);
					} ) }
				</ul>
				<div class="add-new-todo-item-form">
					<Button onClick={ this.addNewItem }>
						<Icon icon={ plus } /> Add new item
					</Button>
				</div>
			</div>
		);
	}
};

const deprecated = [
	{
		attributes: {
			items: {
				type: 'string',
			},
		},

		save: function () {
			return [];
		},

		migrate: function ( attributes ) {
			const o2list = decodeURIComponent( atob( attributes.items ) );
			const o2Items = o2list.split( '\n' );
			const items = [];

			for ( let i = 0; i < o2Items.length; i++ ) {
				const line = o2Items[ i ];
				const lineMatch = line.match( itemLineRegex );
				if ( ! lineMatch ) {
					continue;
				}
				const done = lineMatch[ 2 ] === 'x';
				const item = lineMatch[ 4 ];
				const level = lineMatch[ 1 ].length;
				items.push( { item, done, level } );
			}

			const list = items.map( ( item ) => {
				const done = item.done ? 'x' : 'o';
				return (
					<li>
						<span>{ ''.repeat( item.level ) }</span>
						<span>{ done }</span>
						<RichText.Content tagName="span" value={ item.item } />
					</li>
				);
			} );
			return { list };
		},
	},
];

const save = class extends Component {
	render() {
		const list = this.props.attributes.list.filter(
			( item ) => typeof item !== 'string' || item.trim() !== ''
		);
		return <ul>{ list }</ul>;
	}
};

registerBlockType( 'a8c/todo', {
	title: __( 'Task List' ),
	icon: 'editor-ul',
	category: 'a8c',
	keywords: [ __( 'todo' ) ],
	attributes: blockAttributes,
	edit,
	save,
	deprecated,
} );