This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
SnippetEditorExample.js
185 lines (168 loc) · 5.12 KB
/
SnippetEditorExample.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
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
// External dependencies.
import React, { Component } from "react";
// Yoast dependencies.
import { assessments } from "yoastseo";
import { SnippetEditor } from "@yoast/search-metadata-previews";
// Internal dependencies.
import ExamplesContainer from "./ExamplesContainer";
// CSS dependencies.
import "draft-js-mention-plugin/lib/plugin.css";
const { MetaDescriptionLengthAssessment } = assessments.seo;
const replacementVariables = [
{
name: "title",
label: "Title",
value: "Title",
description: "This is the title of your post",
},
{
name: "post_type",
label: "Post type",
value: "Gallery",
description: "This is the post type of your post",
},
{
name: "sep",
label: "Separator",
value: " - ",
description: "A separator that clarifies your search result snippet",
},
{
name: "term404",
label: "Error 404 slug",
value: "Error 404 slug",
description: "The slug which caused the error 404",
},
];
const recommendedReplacementVariables = [
replacementVariables[ 0 ].name,
replacementVariables[ 1 ].name,
];
/**
* The SnippetEditorExample class.
*/
export default class SnippetEditorExample extends Component {
/**
* Constructs a snippet preview example
*
* @param {Object} props The props for the snippet preview example.
*
* @returns {void}
*/
constructor( props ) {
super( props );
const descriptionLengthAssessment = new MetaDescriptionLengthAssessment();
this.maximumMetaDescriptionLength = descriptionLengthAssessment.getMaximumLength();
this.state = {
title: "Welcome to the Gutenberg Editor - Local WordPress Dev. Snippet Title Snippet" +
" Snippet: %%snippet%% Title: %%title%% Manual: %%snippet_manual%% Type: %%post_type%%" +
" %%these%% %%are%% %%not%% %%tags%% and throw in some % here %%%%%%% and %%there too%%",
url: "https://local.wordpress.test/welcome-to-the-gutenberg-editor/",
slug: "welcome-to-the-gutenberg-editor",
description: "Merci, merçi, Of Mountains & Printing Presses The goal of this new editor is to make" +
" adding rich content to WordPress simple and enjoyable. This whole post is composed" +
" of. Of Mountains & Printing Presses The goal of this new editor is to make adding" +
" rich content to WordPress simple and enjoyable. This whole post is composed of. Of" +
" Mountains & Printing Presses The goal of this new editor is to make adding rich" +
" content to WordPress simple and enjoyable. This whole post is composed of Of Mountains" +
" & Printing Presses The goal of this new editor is to make adding rich content to" +
" WordPress simple and enjoyable. This whole post is composed of. Of Mountains & Printing" +
" Presses The goal of this new editor is to make adding rich content to WordPress" +
" simple and enjoyable. This whole post is composed of. Of Mountains & Printing Presses" +
" The goal of this new editor is to make adding rich content to WordPress simple and" +
" enjoyable. This whole post is composed of",
keyword: "merci",
date: "Jan 8, 2018",
locale: "fr",
/**
* Logs the click event and its type.
*
* @param {object} type The click type.
*
* @returns {void}
*/
onClick( type ) {
// eslint-disable-next-line no-console
console.log( "clicked:", type );
},
breadcrumbs: [ "hallo", "is", "it", "me", "you" ],
isAmp: true,
isOpen: false,
currentTitleLength: 0,
currentDescriptionLength: 0,
};
this.onChangedData = this.onChangedData.bind( this );
}
/**
* Updates title in the state based on an event.
*
* @param {Object} event The event that occurred.
*
* @returns {void}
*/
updateTitle( event ) {
this.setState( {
title: event.target.value,
} );
}
/**
* Updates the URL in the state based on an event.
*
* @param {Object} event The event that occurred.
*
* @returns {void}
*/
updateUrl( event ) {
this.setState( {
url: event.target.value,
} );
}
/**
* Handles a piece of changed data.
*
* @param {string} key The key for this data.
* @param {*} data The data itself.
*
* @returns {void}
*/
onChangedData( key, data ) {
this.setState( {
[ key ]: data,
} );
}
/**
* Renders an example of how to use the snippet editor.
*
* @returns {ReactElement} The rendered snippet editor.
*/
render() {
const data = {
title: this.state.title,
url: this.state.url,
description: this.state.description,
slug: this.state.slug,
};
const titleLengthProgress = {
max: 600,
actual: this.state.currentTitleLength,
score: this.state.currentTitleLength > 300 ? 9 : 6,
};
const descriptionLengthProgress = {
max: this.maximumMetaDescriptionLength,
actual: this.state.currentDescriptionLength,
score: this.state.currentDescriptionLength > 120 ? 9 : 3,
};
return <ExamplesContainer>
<SnippetEditor
{ ...this.state }
data={ data }
baseUrl="https://local.wordpress.test/"
onChange={ this.onChangedData }
replacementVariables={ replacementVariables }
recommendedReplacementVariables={ recommendedReplacementVariables }
titleLengthProgress={ titleLengthProgress }
descriptionLengthProgress={ descriptionLengthProgress }
/>
</ExamplesContainer>;
}
}