forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.js
More file actions
128 lines (113 loc) · 2.95 KB
/
Copy pathedit.js
File metadata and controls
128 lines (113 loc) · 2.95 KB
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
/**
* Internal dependencies
*/
import * as Utils from './utils';
import ListLevel from './ListLevel';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { subscribe } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
IconButton,
Toolbar,
PanelBody,
ToggleControl,
} from '@wordpress/components';
import {
BlockControls,
InspectorControls,
} from '@wordpress/editor';
class TOCEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
wpDataUnsubscribe: null,
};
this.toggleAttribute = this.toggleAttribute.bind( this );
this.refresh = this.refresh.bind( this );
}
toggleAttribute( propName ) {
const value = this.props.attributes[ propName ];
const { setAttributes } = this.props;
setAttributes( { [ propName ]: ! value } );
}
refresh() {
const { setAttributes } = this.props;
const headings = Utils.getPageHeadings();
setAttributes( { headings } );
}
componentDidMount() {
const { attributes, setAttributes } = this.props;
const headings = attributes.headings || [];
const wpDataUnsubscribe = subscribe( () => {
const pageHeadings = Utils.getPageHeadings();
this.setState( { pageHeadings } );
} );
setAttributes( { headings } );
this.setState( { wpDataUnsubscribe } );
}
componentWillUnmount() {
this.state.wpDataUnsubscribe();
}
componentDidUpdate( prevProps, prevState ) {
const { attributes, setAttributes } = this.props;
const pageHeadings = Utils.getPageHeadings();
if ( JSON.stringify( pageHeadings ) !== JSON.stringify( prevState.pageHeadings ) ) {
this.setState( { pageHeadings } );
if ( attributes.autosync ) {
setAttributes( { headings: pageHeadings } ); // this is displayed on the page
}
}
}
render() {
const { attributes, setAttributes } = this.props;
const { autosync } = attributes;
const headings = attributes.headings || [];
if ( headings.length === 0 ) {
return ( <p>{ __( 'Start adding headings to generate Table of Contents' ) }</p> );
}
Utils.updateHeadingBlockAnchors();
return (
<div className={ this.props.className }>
{ ! autosync &&
<BlockControls>
<Toolbar>
<IconButton
label={ __( 'Update' ) }
aria-pressed={ this.state.isEditing }
onClick={ this.refresh }
icon="update"
/>
</Toolbar>
</BlockControls>
}
{
<InspectorControls>
<PanelBody title={ __( 'Table of Contents Settings' ) }>
<ToggleControl
label={ __( 'Auto Sync' ) }
checked={ autosync }
onChange={ () => {
if ( ! autosync ) {
this.refresh();
}
this.toggleAttribute( 'autosync' );
} }
/>
</PanelBody>
</InspectorControls>
}
<ListLevel
edit={ true }
attributes={ attributes }
setAttributes={ setAttributes }
>
{ Utils.linearToNestedList( headings ) }
</ListLevel>
</div>
);
}
}
export default TOCEdit;