| @@ -26,13 +26,10 @@ | ||
| 26 | 26 | ToggleControl, |
| 27 | 27 | CheckboxControl, |
| 28 | 28 | RangeControl, |
| 29 | 29 | TextControl, |
| 30 | - Notice, | |
| 31 | 30 | } = wp.components; |
| 32 | 31 | |
| 33 | -const { withSelect } = wp.data; | |
| 34 | - | |
| 35 | 32 | const { |
| 36 | 33 | getDefaultBlockName, |
| 37 | 34 | createBlock, |
| 38 | 35 | } = wp.blocks; |
| @@ -49,104 +46,8 @@ | ||
| 49 | 46 | |
| 50 | 47 | //import Cookies from 'universal-cookie'; |
| 51 | 48 | //const cookies = new Cookies(); |
| 52 | 49 | |
| 53 | -const CODOC_MAX_LENGTHS = OPTIONS.codoc_max_lengths || {}; | |
| 54 | - | |
| 55 | -// codoc ブロックの直前/直後で本文を分割 | |
| 56 | -function splitCodocContent(content) { | |
| 57 | - const blockRegex = /<!--\s*wp:codoc\/codoc-block[\s\S]*?<!--\s*\/wp:codoc\/codoc-block\s*-->/; | |
| 58 | - const splited = (content || '').split(blockRegex); | |
| 59 | - return { | |
| 60 | - bodyFree: splited[0] || '', | |
| 61 | - bodyPaywalled: splited.slice(1).join('') || '', | |
| 62 | - }; | |
| 63 | -} | |
| 64 | - | |
| 65 | -function buildLengthErrors(title, bodyFree, bodyPaywalled) { | |
| 66 | - const errors = []; | |
| 67 | - const total = bodyFree.length + bodyPaywalled.length; | |
| 68 | - if (CODOC_MAX_LENGTHS.title && title.length > CODOC_MAX_LENGTHS.title) { | |
| 69 | - errors.push(__('Title exceeds the maximum length of', 'codoc') + ' ' + CODOC_MAX_LENGTHS.title + ' (' + title.length + ')'); | |
| 70 | - } | |
| 71 | - if (CODOC_MAX_LENGTHS.body_free && bodyFree.length > CODOC_MAX_LENGTHS.body_free) { | |
| 72 | - errors.push(__('Free area exceeds the maximum length of', 'codoc') + ' ' + CODOC_MAX_LENGTHS.body_free + ' (' + bodyFree.length + ')'); | |
| 73 | - } | |
| 74 | - if (CODOC_MAX_LENGTHS.body_paywalled && bodyPaywalled.length > CODOC_MAX_LENGTHS.body_paywalled) { | |
| 75 | - errors.push(__('Paid area exceeds the maximum length of', 'codoc') + ' ' + CODOC_MAX_LENGTHS.body_paywalled + ' (' + bodyPaywalled.length + ')'); | |
| 76 | - } | |
| 77 | - if (CODOC_MAX_LENGTHS.body && total > CODOC_MAX_LENGTHS.body) { | |
| 78 | - errors.push(__('Total body exceeds the maximum length of', 'codoc') + ' ' + CODOC_MAX_LENGTHS.body + ' (' + total + ')'); | |
| 79 | - } | |
| 80 | - return errors; | |
| 81 | -} | |
| 82 | - | |
| 83 | -// codoc ブロック選択時の Inspector サイドバーに警告を表示する | |
| 84 | -const CodocLengthNotices = withSelect((select) => { | |
| 85 | - const editor = select('core/editor'); | |
| 86 | - return { | |
| 87 | - postTitle: editor ? editor.getEditedPostAttribute('title') : '', | |
| 88 | - postContent: editor ? editor.getEditedPostAttribute('content') : '', | |
| 89 | - }; | |
| 90 | -})(({ postTitle, postContent }) => { | |
| 91 | - const { bodyFree, bodyPaywalled } = splitCodocContent(postContent); | |
| 92 | - const errors = buildLengthErrors(postTitle || '', bodyFree, bodyPaywalled); | |
| 93 | - if (errors.length === 0) { | |
| 94 | - return null; | |
| 95 | - } | |
| 96 | - return ( | |
| 97 | - <Notice status="error" isDismissible={ false }> | |
| 98 | - <strong>{ __('codoc length limit exceeded:', 'codoc') }</strong> | |
| 99 | - <ul style={{ marginTop: '0.5em', marginBottom: 0 }}> | |
| 100 | - { errors.map((e, i) => <li key={i}>{ e }</li>) } | |
| 101 | - </ul> | |
| 102 | - </Notice> | |
| 103 | - ); | |
| 104 | -}); | |
| 105 | - | |
| 106 | -// codoc ブロックを選択していなくても、本文中に存在すればエディタ上部に通知を表示する | |
| 107 | -(function() { | |
| 108 | - if (!wp.data || !wp.data.subscribe) { | |
| 109 | - return; | |
| 110 | - } | |
| 111 | - const NOTICE_ID = 'codoc-length-limit'; | |
| 112 | - let lastState = ''; | |
| 113 | - wp.data.subscribe(function() { | |
| 114 | - const editor = wp.data.select('core/editor'); | |
| 115 | - const blockEditor = wp.data.select('core/block-editor') || wp.data.select('core/editor'); | |
| 116 | - const noticesDispatch = wp.data.dispatch('core/notices'); | |
| 117 | - if (!editor || !blockEditor || !noticesDispatch) { | |
| 118 | - return; | |
| 119 | - } | |
| 120 | - const blocks = blockEditor.getBlocks ? blockEditor.getBlocks() : []; | |
| 121 | - const hasCodocBlock = blocks.some(function(b) { return b && b.name === 'codoc/codoc-block'; }); | |
| 122 | - if (!hasCodocBlock) { | |
| 123 | - if (lastState !== '') { | |
| 124 | - noticesDispatch.removeNotice(NOTICE_ID); | |
| 125 | - lastState = ''; | |
| 126 | - } | |
| 127 | - return; | |
| 128 | - } | |
| 129 | - const title = editor.getEditedPostAttribute('title') || ''; | |
| 130 | - const content = editor.getEditedPostAttribute('content') || ''; | |
| 131 | - const { bodyFree, bodyPaywalled } = splitCodocContent(content); | |
| 132 | - const errors = buildLengthErrors(title, bodyFree, bodyPaywalled); | |
| 133 | - const state = errors.join('||'); | |
| 134 | - if (state === lastState) { | |
| 135 | - return; | |
| 136 | - } | |
| 137 | - lastState = state; | |
| 138 | - noticesDispatch.removeNotice(NOTICE_ID); | |
| 139 | - if (errors.length > 0) { | |
| 140 | - noticesDispatch.createNotice( | |
| 141 | - 'error', | |
| 142 | - __('codoc length limit exceeded:', 'codoc') + ' ' + errors.join(' / '), | |
| 143 | - { id: NOTICE_ID, isDismissible: false } | |
| 144 | - ); | |
| 145 | - } | |
| 146 | - }); | |
| 147 | -})(); | |
| 148 | - | |
| 149 | 50 | class CodocControls extends Component { |
| 150 | 51 | constructor( props ) { |
| 151 | 52 | super( ...arguments ); |
| 152 | 53 | this.subscriptionsFetched = []; |
| @@ -324,10 +225,8 @@ | ||
| 324 | 225 | ]; |
| 325 | 226 | |
| 326 | 227 | return( |
| 327 | 228 | <InspectorControls key="CodocControls"> |
| 328 | - | |
| 329 | - <CodocLengthNotices /> | |
| 330 | 229 | |
| 331 | 230 | <PanelBody> |
| 332 | 231 | |
| 333 | 232 | <ToggleControl |