PluginProbe
codoc / 0.9.51
codoc v0.9.51
0.9.8.8 0.9.8.9 0.9.9 0.9.9.1 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.10 0.9.11 All 114 releases
codoc / src / block / block.js

block.js in codoc 0.9.51, at src/block/block.js

445 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * BLOCK: codoc-block
3 *
4 * Registering a basic block with Gutenberg.
5 * Simple block, renders and saves the same content without any interactivity.
6 */
7
8 // Import CSS.
9 import './style.scss';
10 import './editor.scss';
11 import icon from './icon';
12 //import { ENTER } from '@wordpress/keycodes';
13 const { ENTER } = wp.keycodes;
14 const { __ } = wp.i18n; // Import __() from wp.i18n
15 const { Component } = wp.element;
16
17 const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
18 const {
19 InspectorControls,
20 RichText
21 } = wp.editor;
22
23 const {
24 SelectControl,
25 PanelBody,
26 ToggleControl,
27 CheckboxControl,
28 RangeControl,
29 } = wp.components;
30
31 const {
32 getDefaultBlockName,
33 createBlock,
34 } = wp.blocks;
35
36 const { withState } = wp.compose;
37
38 const CODOC_URL = OPTIONS.codoc_url;
39 const CODOC_USER_CODE = OPTIONS.codoc_usercode;
40 const CODOC_PLUGIN_VERSION = OPTIONS.codoc_plugin_version;
41 const CODOC_ENTRIES_CLASS = 'codoc-entries';
42
43 //import Cookies from 'universal-cookie';
44 //const cookies = new Cookies();
45
46 class CodocControls extends Component {
47 constructor( props ) {
48 super( ...arguments );
49 this.subscriptionsFetched = [];
50 this.fetchSubscriptions();
51 }
52
53 fetchSubscriptions() {
54 const {
55 setAttributes,
56 attributes: {
57 fetching,
58 }
59 } = this.props;
60
61 if (fetching) {
62 return
63 }
64
65 setAttributes( { fetching: true } )
66 let url = CODOC_URL + '/api/v1/cms/' + CODOC_USER_CODE + '/subscriptions?without_token=1';
67
68 fetch(url)
69 .then( res => res.json() )
70 .then( res => {
71 let list = [];
72 if (res.status && res.subscriptions) {
73 for ( var i = 0; i < res.subscriptions.length; i++ ) {
74 let info = {
75 value: res.subscriptions[i].code,
76 label: res.subscriptions[i].title,
77 }
78 list[i] = info
79 }
80 }
81 if (list.length) {
82 this.subscriptionsFetched = list;
83 }
84 setAttributes( { fetching: false } )
85 })
86 }
87
88 getShowPriceHelp( checked ) {
89 return checked ?
90 __( 'Enable','codoc' ) :
91 __( 'Disable','codoc' );
92 }
93 getShowSupportHelp( checked ) {
94 return checked ?
95 __( 'Accept' ,'codoc') :
96 __( 'Do not accept' ,'codoc');
97 }
98 getShowPaywalledSupportHelp( checked ) {
99 return checked ?
100 __( 'The paid part will be hidden upon access, but will be displayed after tapping "Pay to Read". Viewers can freely specify the amount to purchase based on the content. Please note that there may be cases where it is not purchased.' ,'codoc') :
101 __( ' ' );
102 }
103 getStatusLimitedHelp( checked ) {
104 return checked ?
105 __( 'Unlisted' ,'codoc') :
106 __( 'Published' ,'codoc');
107 }
108
109 render() {
110 const {
111 setAttributes,
112 attributes: {
113 showPrice,
114 price,
115 limited,
116 limitedCount,
117
118 affiliateMode,
119 affiliateRate,
120
121 showSupport,
122 showPaywalledSupport,
123 statusLimited,
124 subscriptions,
125 }
126 } = this.props;
127
128 const toggleShowPrice = () => setAttributes( { showPrice: ! showPrice } );
129 const toggleLimited = () => setAttributes( { limited: ! limited } );
130 const toggleShowPaywalledSupport = () => setAttributes( { showPaywalledSupport: ! showPaywalledSupport } );
131 const toggleStatusLimited = () => setAttributes( { statusLimited: ! statusLimited } );
132 const toggleAffiliateMode = () => setAttributes( { affiliateMode: ! affiliateMode } );
133 const toggleShowSupport = () => setAttributes( { showSupport: ! showSupport } );
134
135 const SubscriptionCheckBoxes = withState({
136 checked_obj: Object.assign(new Object, subscriptions)
137 })( ( { checked_obj , setState } ) => (
138 <ul>
139 {
140 this.subscriptionsFetched.map((v) => (
141 <li><CheckboxControl
142 className="check_items"
143 label={v.label}
144 checked={checked_obj[v.value]}
145
146 onChange={ ( check ) => {
147 check ? checked_obj[v.value] = true : delete checked_obj[v.value]
148 setAttributes({subscriptions : checked_obj})
149 setState({checked_obj})
150 } }
151 /></li>
152 ) )
153 }
154 </ul>
155 ) )
156 const affiliateRateOptions = [
157 { value: '0.0500', label:'5%' },
158 { value: '0.1000', label:'10%' },
159 { value: '0.1500', label:'15%' },
160 { value: '0.2000', label:'20%' },
161 { value: '0.2500', label:'25%' },
162 { value: '0.3000', label:'30%' },
163 { value: '0.3500', label:'35%' },
164 { value: '0.4000', label:'40%' },
165 { value: '0.4500', label:'45%' },
166 { value: '0.5000', label:'50%' },
167 ];
168
169 return(
170 <InspectorControls key="CodocControls">
171
172 <PanelBody>
173
174 <ToggleControl
175 label={ __( 'individual sale','codoc' ) }
176 checked={ !! showPrice }
177 help={ this.getShowPriceHelp }
178 ref="showPrice"
179 onChange={ toggleShowPrice }
180 />
181
182 <div style={{ display: showPrice ? 'initial' : 'none' }}>
183
184 <RangeControl
185 label={ __('price','codoc') + '(' + (__('yen','codoc')) + ')' + __(showPaywalledSupport ? '' + __('Displayed as the your suggested price','codoc') + '' : '' ) }
186 value={ price }
187 initialPosition={ price }
188 onChange={ ( value ) => {
189 // do validate this
190 setAttributes({ price: value });
191 }}
192 min="100"
193 max="50000"
194 /><br />
195
196 <ToggleControl
197 label={ __( 'Pay-what-you-want','codoc' ) }
198 checked={ !! showPaywalledSupport }
199 help={ this.getShowPaywalledSupportHelp }
200 onChange={ toggleShowPaywalledSupport }
201 />
202
203 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
204 <ToggleControl
205 label={ __( 'Limited quantity sale','codoc' ) }
206 checked={ !! limited }
207 onChange={ toggleLimited }
208 />
209
210 <div style={{ display: limited ? 'initial' : 'none' }}>
211 <RangeControl
212 label={ __( 'Limited quantity','codoc' ) }
213 value={ limitedCount }
214 onChange={ ( value ) => {
215 // do validate this
216 setAttributes({ limitedCount: value });
217 }}
218 initialPosition={ limitedCount }
219 min="0"
220 max="100"
221 /><br />
222 </div>
223
224 <ToggleControl
225 label={ __( 'Affiliate','codoc' ) }
226 checked={ !! affiliateMode }
227 onChange={ toggleAffiliateMode }
228 />
229
230 <div style={{ display: affiliateMode ? 'initial' : 'none' }}>
231 <SelectControl
232 label={ __( 'Rate' , 'codoc') }
233 description={ __( 'You can offer affiliate marketing at a specified rate for purchasers.', 'codoc') }
234 options={ affiliateRateOptions }
235 value={ affiliateRate }
236 onChange={ ( value ) => {
237 setAttributes( { affiliateRate: value } );
238 }}/><br />
239 </div>
240
241 </div>
242 </div>
243
244 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
245 <ToggleControl
246 label={ __( 'Tipping', 'codoc') }
247 checked={ !! showSupport }
248 value="1"
249 onChange={ toggleShowSupport }
250 help={ this.getShowSupportHelp }
251 />
252 </div>
253
254 <div style={{ display: 'initial' }}>
255 <ToggleControl
256 label={ __( 'Unlisted','codoc' ) }
257 checked={ !! statusLimited }
258 value="1"
259 onChange={ toggleStatusLimited }
260 help={ this.getStatusLimitedHelp }
261 />
262 </div>
263
264 <div class="codoc-subscription-title">
265 <label>
266 { __('Subscriptions','codoc') }
267 </label>
268 <a
269 href={ CODOC_URL + '/me/subscriptions'}
270 target="_blank" class="codoc-subscription-add"
271 >
272 { __('Add','codoc') }
273 </a>
274 </div>
275
276 <SubscriptionCheckBoxes />
277
278
279 </PanelBody>
280 </InspectorControls>
281 );
282 }
283 };
284
285 class EditBlockContent extends Component {
286 constructor() {
287 super( ...arguments );
288 // デフォルト値
289 this.props.setAttributes({ version: CODOC_PLUGIN_VERSION });
290 this.props.setAttributes({ showPrice: this.props.attributes.showPrice == null ? true : this.props.attributes.showPrice });
291 this.props.setAttributes({ price: this.props.attributes.price == null ? 100 : this.props.attributes.price });
292 this.props.setAttributes({ limited: this.props.attributes.limited == null ? false : this.props.attributes.limited });
293 this.props.setAttributes({ limitedCount: this.props.attributes.limitedCount == null ? 10 : this.props.attributes.limitedCount });
294 this.props.setAttributes({ affiliateMode: this.props.attributes.affiliateMode == null ? false : this.props.attributes.affiliateMode });
295 this.props.setAttributes({ affiliateRate: this.props.attributes.affiliateRate == null ? '0.0500' : this.props.attributes.affiliateRate });
296 this.props.setAttributes({ showSupport: this.props.attributes.showSupport == null ? false : this.props.attributes.showSupport });
297 this.props.setAttributes({ showPaywalledSupport: this.props.attributes.showPaywalledSupport == null ? false : this.props.attributes.showPaywalledSupport });
298 this.props.setAttributes({ statusLimited: this.props.attributes.statusLimited == null ? false : this.props.attributes.statusLimited });
299 this.props.setAttributes({ subscriptions: this.props.attributes.subscriptions == null ? {} : this.props.attributes.subscriptions });
300
301 this.onChangeInput = this.onChangeInput.bind( this );
302 this.onKeyDown = this.onKeyDown.bind( this );
303 this.state = {
304 defaultText: __( 'To continue reading, ...','codoc' ),
305 };
306 }
307 onChangeInput( event ) {
308 this.setState( {
309 defaultText: '',
310 } );
311
312 const value = event.target.value.length === 0 ? undefined : event.target.value;
313 this.props.setAttributes( { customText: value } );
314 }
315 onKeyDown( event ) {
316 const { keyCode } = event;
317 const { insertBlocksAfter } = this.props;
318 if ( keyCode === ENTER ) {
319 insertBlocksAfter( [ createBlock( getDefaultBlockName() ) ] );
320 }
321 }
322
323 render() {
324 const {
325 attributes: {
326 customText,
327 },
328 setAttributes
329 } = this.props;
330
331 const { defaultText } = this.state;
332 //const value = customText !== undefined ? customText : defaultText;
333 const value = customText ? customText : defaultText;
334 //const value = defaultText;
335 const inputLength = value.length + 10;
336
337 return[
338 <CodocControls { ...{setAttributes, ...this.props } } />,
339
340 <div className="wp-block-more">
341 <input
342 type="text"
343 value={ value }
344 size={ inputLength }
345 onChange={ this.onChangeInput }
346 onKeyDown={ this.onKeyDown }
347 />
348 </div>
349
350 ];
351 }
352 };
353
354
355 registerBlockType( 'codoc/codoc-block', {
356 title: __( 'codoc' ,'codoc'),
357 description: __( 'The area from this block down is a paid area that only authenticated codoc users can view.','codoc' ),
358 icon,
359 category: 'layout',
360 supports: {
361 customClassName: false,
362 className: false,
363 html: false,
364 multiple: false,
365 },
366 keywords: [
367 __( 'codoc Block' ,'codoc'),
368 __( 'plugin for codoc' ,'codoc'),
369 __( 'codoc-block' ,'codoc'),
370 ],
371 attributes: {
372 showPrice: {
373 type: 'boolean',
374 default: null,
375 },
376 price: {
377 type: 'number',
378 default: null,
379 },
380 limited: {
381 type: 'boolean',
382 default: null,
383 },
384 limitedCount: {
385 type: 'number',
386 default: null,
387 },
388 affiliateMode: {
389 type: 'boolean',
390 default: null,
391 },
392 affiliateRate: {
393 type: 'string',
394 default: null,
395 },
396 showSupport: {
397 type: 'boolean',
398 default: null,
399 },
400 showPaywalledSupport: {
401 type: 'boolean',
402 default: null,
403 },
404 statusLimited: {
405 type: 'boolean',
406 default: null,
407 },
408 subscriptions: {
409 type: 'object',
410 default: null,
411 },
412 customText: {
413 type: 'string',
414 default: '',
415 },
416 version: {
417 type: 'string',
418 default: null,
419 },
420 },
421
422 edit: EditBlockContent,
423
424 save: function( props ) {
425 const {
426 setAttributes,
427 attributes: {
428 customText,
429 }
430 } = props;
431 return ( // return if link behavior normal
432 <div
433 data-id='codoc-tag'
434 className={ CODOC_ENTRIES_CLASS }
435 >
436 { customText && !! customText.length && (
437 <RichText.Content
438 value={ customText }
439 />
440 )}
441 </div>
442 )
443 },
444 } );
445