PluginProbe
codoc / 0.9.20
codoc v0.9.20
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.20, at src/block/block.js

426 lines 12.6 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
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
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 toggleAffiliateMode = () => setAttributes( { affiliateMode: ! affiliateMode } );
132 const toggleShowSupport = () => setAttributes( { showSupport: ! showSupport } );
133
134 const SubscriptionCheckBoxes = withState({
135 checked_obj: Object.assign(new Object, subscriptions)
136 })( ( { checked_obj , setState } ) => (
137 <ul>
138 {
139 this.subscriptionsFetched.map((v) => (
140 <li><CheckboxControl
141 className="check_items"
142 label={v.label}
143 checked={checked_obj[v.value]}
144
145 onChange={ ( check ) => {
146 check ? checked_obj[v.value] = true : delete checked_obj[v.value]
147 setAttributes({subscriptions : checked_obj})
148 setState({checked_obj})
149 } }
150 /></li>
151 ) )
152 }
153 </ul>
154 ) )
155 const affiliateRateOptions = [
156 { value: '0.0500', label:'販売価格の5%' },
157 { value: '0.1000', label:'販売価格の10%' },
158 { value: '0.1500', label:'販売価格の15%' },
159 { value: '0.2000', label:'販売価格の20%' },
160 { value: '0.2500', label:'販売価格の25%' },
161 { value: '0.3000', label:'販売価格の30%' },
162 { value: '0.3500', label:'販売価格の35%' },
163 { value: '0.4000', label:'販売価格の40%' },
164 { value: '0.4500', label:'販売価格の45%' },
165 { value: '0.5000', label:'販売価格の50%' },
166 ];
167
168 return(
169 <InspectorControls key="CodocControls">
170
171 <PanelBody>
172
173 <ToggleControl
174 label={ __( '単体販売' ) }
175 checked={ !! showPrice }
176 help={ this.getShowPriceHelp }
177 ref="showPrice"
178 onChange={ toggleShowPrice }
179 />
180
181 <div style={{ display: showPrice ? 'initial' : 'none' }}>
182
183 <RangeControl
184 label={ __( '価格(円)') + __(showPaywalledSupport ? '【希望額として表示】' : '' ) }
185 value={ price }
186 initialPosition={ price }
187 onChange={ ( value ) => {
188 // do validate this
189 setAttributes({ price: value });
190 }}
191 min="100"
192 max="50000"
193 /><br />
194
195 <ToggleControl
196 label={ __( '自由課金' ) }
197 checked={ !! showPaywalledSupport }
198 help={ this.getShowPaywalledSupportHelp }
199 onChange={ toggleShowPaywalledSupport }
200 />
201
202 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
203 <ToggleControl
204 label={ __( '数量限定販売' ) }
205 checked={ !! limited }
206 onChange={ toggleLimited }
207 />
208
209 <div style={{ display: limited ? 'initial' : 'none' }}>
210 <RangeControl
211 label={ __( '限定数' ) }
212 value={ limitedCount }
213 onChange={ ( value ) => {
214 // do validate this
215 setAttributes({ limitedCount: value });
216 }}
217 initialPosition={ limitedCount }
218 min="1"
219 max="100"
220 /><br />
221 </div>
222
223 <ToggleControl
224 label={ __( 'アフィリエイト' ) }
225 checked={ !! affiliateMode }
226 onChange={ toggleAffiliateMode }
227 />
228
229 <div style={{ display: affiliateMode ? 'initial' : 'none' }}>
230 <SelectControl
231 label={ __( '料率' ) }
232 description={ __( '購�
233 ��
234 を対象に指定した料率でアフィリエイトをオファーできます') }
235 options={ affiliateRateOptions }
236 value={ affiliateRate }
237 onChange={ ( value ) => {
238 setAttributes( { affiliateRate: value } );
239 }}/><br />
240 </div>
241
242 </div>
243 </div>
244
245 <div style={{ display: showPaywalledSupport ? 'none' : 'initial' }}>
246 <ToggleControl
247 label={ __( 'サポート' ) }
248 checked={ !! showSupport }
249 value="1"
250 onChange={ toggleShowSupport }
251 help={ this.getShowSupportHelp }
252 />
253 </div>
254
255 <div class="codoc-subscription-title">
256 <label>サブスクリプション</label>
257 <a
258 href={ CODOC_URL + '/me/subscriptions'}
259 target="_blank" class="codoc-subscription-add">追加</a>
260 </div>
261
262 <SubscriptionCheckBoxes />
263
264
265 </PanelBody>
266 </InspectorControls>
267 );
268 }
269 };
270
271 class EditBlockContent extends Component {
272 constructor() {
273 super( ...arguments );
274 // デフォルト値
275 this.props.setAttributes({ version: CODOC_PLUGIN_VERSION });
276 this.props.setAttributes({ showPrice: this.props.attributes.showPrice == null ? true : this.props.attributes.showPrice });
277 this.props.setAttributes({ price: this.props.attributes.price == null ? 100 : this.props.attributes.price });
278 this.props.setAttributes({ limited: this.props.attributes.limited == null ? false : this.props.attributes.limited });
279 this.props.setAttributes({ limitedCount: this.props.attributes.limitedCount == null ? 10 : this.props.attributes.limitedCount });
280 this.props.setAttributes({ affiliateMode: this.props.attributes.affiliateMode == null ? false : this.props.attributes.affiliateMode });
281 this.props.setAttributes({ affiliateRate: this.props.attributes.affiliateRate == null ? '0.0500' : this.props.attributes.affiliateRate });
282 this.props.setAttributes({ showSupport: this.props.attributes.showSupport == null ? false : this.props.attributes.showSupport });
283 this.props.setAttributes({ showPaywalledSupport: this.props.attributes.showPaywalledSupport == null ? false : this.props.attributes.showPaywalledSupport });
284 this.props.setAttributes({ subscriptions: this.props.attributes.subscriptions == null ? {} : this.props.attributes.subscriptions });
285
286 this.onChangeInput = this.onChangeInput.bind( this );
287 this.onKeyDown = this.onKeyDown.bind( this );
288 this.state = {
289 defaultText: __( 'この続きをみるには' ),
290 };
291 }
292 onChangeInput( event ) {
293 this.setState( {
294 defaultText: '',
295 } );
296
297 const value = event.target.value.length === 0 ? undefined : event.target.value;
298 this.props.setAttributes( { customText: value } );
299 }
300 onKeyDown( event ) {
301 const { keyCode } = event;
302 const { insertBlocksAfter } = this.props;
303 if ( keyCode === ENTER ) {
304 insertBlocksAfter( [ createBlock( getDefaultBlockName() ) ] );
305 }
306 }
307
308 render() {
309 const {
310 attributes: {
311 customText,
312 },
313 setAttributes
314 } = this.props;
315
316 const { defaultText } = this.state;
317 //const value = customText !== undefined ? customText : defaultText;
318 const value = customText ? customText : defaultText;
319 //const value = defaultText;
320 const inputLength = value.length + 10;
321
322 return[
323 <CodocControls { ...{setAttributes, ...this.props } } />,
324
325 <div className="wp-block-more">
326 <input
327 type="text"
328 value={ value }
329 size={ inputLength }
330 onChange={ this.onChangeInput }
331 onKeyDown={ this.onKeyDown }
332 />
333 </div>
334
335 ];
336 }
337 };
338
339
340 registerBlockType( 'codoc/codoc-block', {
341 title: __( 'codoc' ),
342 description: __( 'このブロックから下のブロックは認証されたcodocユーザーのみが閲覧できる有料エリアとなります。' ),
343 icon,
344 category: 'layout',
345 supports: {
346 customClassName: false,
347 className: false,
348 html: false,
349 multiple: false,
350 },
351 keywords: [
352 __( 'codoc Block' ),
353 __( 'plugin for codoc' ),
354 __( 'codoc-block' ),
355 ],
356 attributes: {
357 showPrice: {
358 type: 'boolean',
359 default: null,
360 },
361 price: {
362 type: 'number',
363 default: null,
364 },
365 limited: {
366 type: 'boolean',
367 default: null,
368 },
369 limitedCount: {
370 type: 'number',
371 default: null,
372 },
373 affiliateMode: {
374 type: 'boolean',
375 default: null,
376 },
377 affiliateRate: {
378 type: 'string',
379 default: null,
380 },
381 showSupport: {
382 type: 'boolean',
383 default: null,
384 },
385 showPaywalledSupport: {
386 type: 'boolean',
387 default: null,
388 },
389 subscriptions: {
390 type: 'object',
391 default: null,
392 },
393 customText: {
394 type: 'string',
395 default: '',
396 },
397 version: {
398 type: 'string',
399 default: null,
400 },
401 },
402
403 edit: EditBlockContent,
404
405 save: function( props ) {
406 const {
407 setAttributes,
408 attributes: {
409 customText,
410 }
411 } = props;
412 return ( // return if link behavior normal
413 <div
414 data-id='codoc-tag'
415 className={ CODOC_ENTRIES_CLASS }
416 >
417 { customText && !! customText.length && (
418 <RichText.Content
419 value={ customText }
420 />
421 )}
422 </div>
423 )
424 },
425 } );
426