PluginProbe
codoc / 0.9.41
codoc v0.9.41
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.41, at src/block/block.js

450 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
15 const { __ } = wp.i18n; // Import __() from wp.i18n
16 const { Component } = wp.element;
17
18 const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
19 const {
20 InspectorControls,
21 RichText
22 } = wp.editor;
23
24 const {
25 SelectControl,
26 PanelBody,
27 ToggleControl,
28 CheckboxControl,
29 RangeControl,
30 } = wp.components;
31
32 const {
33 getDefaultBlockName,
34 createBlock,
35 } = wp.blocks;
36
37 const { withState } = wp.compose;
38
39 const CODOC_URL = OPTIONS.codoc_url;
40 const CODOC_USER_CODE = OPTIONS.codoc_usercode;
41 const CODOC_PLUGIN_VERSION = OPTIONS.codoc_plugin_version;
42 const CODOC_ENTRIES_CLASS = 'codoc-entries';
43
44 //import Cookies from 'universal-cookie';
45 //const cookies = new Cookies();
46
47 class CodocControls extends Component {
48 constructor( props ) {
49 super( ...arguments );
50 this.subscriptionsFetched = [];
51 this.fetchSubscriptions();
52 }
53
54 fetchSubscriptions() {
55 const {
56 setAttributes,
57 attributes: {
58 fetching,
59 }
60 } = this.props;
61
62 if (fetching) {
63 return
64 }
65
66 setAttributes( { fetching: true } )
67 let url = CODOC_URL + '/api/v1/cms/' + CODOC_USER_CODE + '/subscriptions?without_token=1';
68
69 fetch(url)
70 .then( res => res.json() )
71 .then( res => {
72 let list = [];
73 if (res.status && res.subscriptions) {
74 for ( var i = 0; i < res.subscriptions.length; i++ ) {
75 let info = {
76 value: res.subscriptions[i].code,
77 label: res.subscriptions[i].title,
78 }
79 list[i] = info
80 }
81 }
82 if (list.length) {
83 this.subscriptionsFetched = list;
84 }
85 setAttributes( { fetching: false } )
86 })
87 }
88
89 getShowPriceHelp( checked ) {
90 return checked ?
91 __( 'する' ) :
92 __( 'しない' );
93 }
94 getShowSupportHelp( checked ) {
95 return checked ?
96 __( '受け付ける' ) :
97 __( '受け付けない' );
98 }
99 getShowPaywalledSupportHelp( checked ) {
100 return checked ?
101 __( '有料パートはアクセス時に非表示となりますが、「自由課金で読む」をタップ後に表示されます。閲覧�
102 は�
103 容に応じて金額を自由に指定して購�
104 �できます。購�
105 �されない場合もあるのでご注意ください。' ) :
106 __( '' );
107 }
108 getStatusLimitedHelp( checked ) {
109 return checked ?
110 __( '限定�
111 �開:codoc側で一覧されません' ) :
112 __( '通常�
113 �開' );
114 }
115
116 render() {
117 const {
118 setAttributes,
119 attributes: {
120 showPrice,
121 price,
122 limited,
123 limitedCount,
124
125 affiliateMode,
126 affiliateRate,
127
128 showSupport,
129 showPaywalledSupport,
130 statusLimited,
131 subscriptions,
132 }
133 } = this.props;
134
135 const toggleShowPrice = () => setAttributes( { showPrice: ! showPrice } );
136 const toggleLimited = () => setAttributes( { limited: ! limited } );
137 const toggleShowPaywalledSupport = () => setAttributes( { showPaywalledSupport: ! showPaywalledSupport } );
138 const toggleStatusLimited = () => setAttributes( { statusLimited: ! statusLimited } );
139 const toggleAffiliateMode = () => setAttributes( { affiliateMode: ! affiliateMode } );
140 const toggleShowSupport = () => setAttributes( { showSupport: ! showSupport } );
141
142 const SubscriptionCheckBoxes = withState({
143 checked_obj: Object.assign(new Object, subscriptions)
144 })( ( { checked_obj , setState } ) => (
145 <ul>
146 {
147 this.subscriptionsFetched.map((v) => (
148 <li><CheckboxControl
149 className="check_items"
150 label={v.label}
151 checked={checked_obj[v.value]}
152
153 onChange={ ( check ) => {
154 check ? checked_obj[v.value] = true : delete checked_obj[v.value]
155 setAttributes({subscriptions : checked_obj})
156 setState({checked_obj})
157 } }
158 /></li>
159 ) )
160 }
161 </ul>
162 ) )
163 const affiliateRateOptions = [
164 { value: '0.0500', label:'販売価格の5%' },
165 { value: '0.1000', label:'販売価格の10%' },
166 { value: '0.1500', label:'販売価格の15%' },
167 { value: '0.2000', label:'販売価格の20%' },
168 { value: '0.2500', label:'販売価格の25%' },
169 { value: '0.3000', label:'販売価格の30%' },
170 { value: '0.3500', label:'販売価格の35%' },
171 { value: '0.4000', label:'販売価格の40%' },
172 { value: '0.4500', label:'販売価格の45%' },
173 { value: '0.5000', label:'販売価格の50%' },
174 ];
175
176 return(
177 <InspectorControls key="CodocControls">
178
179 <PanelBody>
180
181 <ToggleControl
182 label={ __( '単体販売' ) }
183 checked={ !! showPrice }
184 help={ this.getShowPriceHelp }
185 ref="showPrice"
186 onChange={ toggleShowPrice }
187 />
188
189 <div style={{ display: showPrice ? 'initial' : 'none' }}>
190
191 <RangeControl
192 label={ __( '価格(円)') + __(showPaywalledSupport ? '【希望額として表示】' : '' ) }
193 value={ price }
194 initialPosition={ price }
195 onChange={ ( value ) => {
196 // do validate this
197 setAttributes({ price: value });
198 }}
199 min="100"
200 max="50000"
201 /><br />
202
203 <ToggleControl
204 label={ __( '自由課金' ) }
205 checked={ !! showPaywalledSupport }
206 help={ this.getShowPaywalledSupportHelp }
207 onChange={ toggleShowPaywalledSupport }
208 />
209
210 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
211 <ToggleControl
212 label={ __( '数量限定販売' ) }
213 checked={ !! limited }
214 onChange={ toggleLimited }
215 />
216
217 <div style={{ display: limited ? 'initial' : 'none' }}>
218 <RangeControl
219 label={ __( '限定数' ) }
220 value={ limitedCount }
221 onChange={ ( value ) => {
222 // do validate this
223 setAttributes({ limitedCount: value });
224 }}
225 initialPosition={ limitedCount }
226 min="1"
227 max="100"
228 /><br />
229 </div>
230
231 <ToggleControl
232 label={ __( 'アフィリエイト' ) }
233 checked={ !! affiliateMode }
234 onChange={ toggleAffiliateMode }
235 />
236
237 <div style={{ display: affiliateMode ? 'initial' : 'none' }}>
238 <SelectControl
239 label={ __( '料率' ) }
240 description={ __( '購�
241 ��
242 を対象に指定した料率でアフィリエイトをオファーできます') }
243 options={ affiliateRateOptions }
244 value={ affiliateRate }
245 onChange={ ( value ) => {
246 setAttributes( { affiliateRate: value } );
247 }}/><br />
248 </div>
249
250 </div>
251 </div>
252
253 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
254 <ToggleControl
255 label={ __( 'サポート' ) }
256 checked={ !! showSupport }
257 value="1"
258 onChange={ toggleShowSupport }
259 help={ this.getShowSupportHelp }
260 />
261 </div>
262
263 <div style={{ display: 'initial' }}>
264 <ToggleControl
265 label={ __( '限定�
266 �開' ) }
267 checked={ !! statusLimited }
268 value="1"
269 onChange={ toggleStatusLimited }
270 help={ this.getStatusLimitedHelp }
271 />
272 </div>
273
274 <div class="codoc-subscription-title">
275 <label>サブスクリプション</label>
276 <a
277 href={ CODOC_URL + '/me/subscriptions'}
278 target="_blank" class="codoc-subscription-add">追加</a>
279 </div>
280
281 <SubscriptionCheckBoxes />
282
283
284 </PanelBody>
285 </InspectorControls>
286 );
287 }
288 };
289
290 class EditBlockContent extends Component {
291 constructor() {
292 super( ...arguments );
293 // デフォルト値
294 this.props.setAttributes({ version: CODOC_PLUGIN_VERSION });
295 this.props.setAttributes({ showPrice: this.props.attributes.showPrice == null ? true : this.props.attributes.showPrice });
296 this.props.setAttributes({ price: this.props.attributes.price == null ? 100 : this.props.attributes.price });
297 this.props.setAttributes({ limited: this.props.attributes.limited == null ? false : this.props.attributes.limited });
298 this.props.setAttributes({ limitedCount: this.props.attributes.limitedCount == null ? 10 : this.props.attributes.limitedCount });
299 this.props.setAttributes({ affiliateMode: this.props.attributes.affiliateMode == null ? false : this.props.attributes.affiliateMode });
300 this.props.setAttributes({ affiliateRate: this.props.attributes.affiliateRate == null ? '0.0500' : this.props.attributes.affiliateRate });
301 this.props.setAttributes({ showSupport: this.props.attributes.showSupport == null ? false : this.props.attributes.showSupport });
302 this.props.setAttributes({ showPaywalledSupport: this.props.attributes.showPaywalledSupport == null ? false : this.props.attributes.showPaywalledSupport });
303 this.props.setAttributes({ statusLimited: this.props.attributes.statusLimited == null ? false : this.props.attributes.statusLimited });
304 this.props.setAttributes({ subscriptions: this.props.attributes.subscriptions == null ? {} : this.props.attributes.subscriptions });
305
306 this.onChangeInput = this.onChangeInput.bind( this );
307 this.onKeyDown = this.onKeyDown.bind( this );
308 this.state = {
309 defaultText: __( 'この続きをみるには' ),
310 };
311 }
312 onChangeInput( event ) {
313 this.setState( {
314 defaultText: '',
315 } );
316
317 const value = event.target.value.length === 0 ? undefined : event.target.value;
318 this.props.setAttributes( { customText: value } );
319 }
320 onKeyDown( event ) {
321 const { keyCode } = event;
322 const { insertBlocksAfter } = this.props;
323 if ( keyCode === ENTER ) {
324 insertBlocksAfter( [ createBlock( getDefaultBlockName() ) ] );
325 }
326 }
327
328 render() {
329 const {
330 attributes: {
331 customText,
332 },
333 setAttributes
334 } = this.props;
335
336 const { defaultText } = this.state;
337 //const value = customText !== undefined ? customText : defaultText;
338 const value = customText ? customText : defaultText;
339 //const value = defaultText;
340 const inputLength = value.length + 10;
341
342 return[
343 <CodocControls { ...{setAttributes, ...this.props } } />,
344
345 <div className="wp-block-more">
346 <input
347 type="text"
348 value={ value }
349 size={ inputLength }
350 onChange={ this.onChangeInput }
351 onKeyDown={ this.onKeyDown }
352 />
353 </div>
354
355 ];
356 }
357 };
358
359
360 registerBlockType( 'codoc/codoc-block', {
361 title: __( 'codoc' ),
362 description: __( 'このブロックから下のブロックは認証されたcodocユーザーのみが閲覧できる有料エリアとなります。' ),
363 icon,
364 category: 'layout',
365 supports: {
366 customClassName: false,
367 className: false,
368 html: false,
369 multiple: false,
370 },
371 keywords: [
372 __( 'codoc Block' ),
373 __( 'plugin for codoc' ),
374 __( 'codoc-block' ),
375 ],
376 attributes: {
377 showPrice: {
378 type: 'boolean',
379 default: null,
380 },
381 price: {
382 type: 'number',
383 default: null,
384 },
385 limited: {
386 type: 'boolean',
387 default: null,
388 },
389 limitedCount: {
390 type: 'number',
391 default: null,
392 },
393 affiliateMode: {
394 type: 'boolean',
395 default: null,
396 },
397 affiliateRate: {
398 type: 'string',
399 default: null,
400 },
401 showSupport: {
402 type: 'boolean',
403 default: null,
404 },
405 showPaywalledSupport: {
406 type: 'boolean',
407 default: null,
408 },
409 statusLimited: {
410 type: 'boolean',
411 default: null,
412 },
413 subscriptions: {
414 type: 'object',
415 default: null,
416 },
417 customText: {
418 type: 'string',
419 default: '',
420 },
421 version: {
422 type: 'string',
423 default: null,
424 },
425 },
426
427 edit: EditBlockContent,
428
429 save: function( props ) {
430 const {
431 setAttributes,
432 attributes: {
433 customText,
434 }
435 } = props;
436 return ( // return if link behavior normal
437 <div
438 data-id='codoc-tag'
439 className={ CODOC_ENTRIES_CLASS }
440 >
441 { customText && !! customText.length && (
442 <RichText.Content
443 value={ customText }
444 />
445 )}
446 </div>
447 )
448 },
449 } );
450