-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANET-7635 Move remaining code from plugin
This is in preparation to finally retire it
- Loading branch information
Showing
6 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Return an editable image block | ||
* | ||
* - display image credits in caption during edition | ||
* | ||
* @param {Object} BlockEdit | ||
* @return {Object} interface to edit images on the Editor | ||
*/ | ||
export const ImageBlockEdit = BlockEdit => props => { | ||
if ('core/image' !== props.name) { | ||
return <BlockEdit {...props} />; | ||
} | ||
|
||
const {attributes, clientId} = props; | ||
const {id, className = ''} = attributes; | ||
|
||
// Get image data | ||
const image = wp.data.useSelect(select => id ? select('core').getMedia(id) : null); | ||
const credits = image?.meta?._credit_text; | ||
const captionText = image?.caption?.raw; | ||
// Compile data for insertion | ||
let image_credits = null; | ||
if (credits && credits.length > 0 && (!captionText || !captionText.includes(credits))) { | ||
image_credits = credits.includes('©') ? credits : `© ${credits}`; | ||
} | ||
|
||
const block_id = clientId ? `block-${clientId}` : null; | ||
|
||
// Update width and height when sized rounded styles are selected | ||
if (className.includes('is-style-rounded-')) { | ||
const classes = className.split(' '); | ||
const size = classes.find(c => c.includes('is-style-rounded-')).replace('is-style-rounded-', '') || 180; | ||
attributes.width = parseInt(size); | ||
attributes.height = parseInt(size); | ||
} | ||
|
||
// Force to use square images when the class `square-*` is added | ||
if (className.includes('square-')) { | ||
const size = className.slice(className.search('square-') + 'square-'.length).split(' ')[0] || 180; | ||
attributes.width = parseInt(size); | ||
attributes.height = parseInt(size); | ||
} | ||
|
||
return ( | ||
<> | ||
<BlockEdit {...props} /> | ||
{block_id && image_credits && ( | ||
captionText ? | ||
<style dangerouslySetInnerHTML={{ | ||
__html: `#${block_id} figcaption::after { content: " ${image_credits}"; }`, | ||
}}> | ||
</style> : | ||
<figcaption style={{marginTop: -24}}>{image_credits}</figcaption> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const {addFilter} = wp.hooks; | ||
|
||
import {ImageBlockEdit} from './ImageBlockEdit'; | ||
|
||
export const addBlockFilters = () => { | ||
addFileBlockFilter(); | ||
addImageBlockFilter(); | ||
addGravityFormsBlockFilter(); | ||
}; | ||
|
||
const addFileBlockFilter = () => { | ||
const setDownloadButtonToggleDefualtFalse = (settings, name) => { | ||
if ('core/file' !== name) { | ||
return settings; | ||
} | ||
|
||
settings.attributes.showDownloadButton.default = false; | ||
|
||
return settings; | ||
}; | ||
|
||
addFilter('blocks.registerBlockType', 'planet4-blocks/filters/file', setDownloadButtonToggleDefualtFalse); | ||
}; | ||
|
||
const addImageBlockFilter = () => addFilter('editor.BlockEdit', 'core/image/edit', ImageBlockEdit); | ||
|
||
// Enforce "AJAX" toggle setting enabled by default, on Gravity form block. | ||
const addGravityFormsBlockFilter = () => { | ||
const setAJAXToggleDefaultTrue = (settings, name) => { | ||
if ('gravityforms/form' !== name) { | ||
return settings; | ||
} | ||
|
||
settings.attributes.ajax.default = true; | ||
|
||
return settings; | ||
}; | ||
|
||
addFilter('blocks.registerBlockType', 'planet4-blocks/filters/file', setAJAXToggleDefaultTrue); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const addButtonLinkPasteWarning = () => document.addEventListener('DOMContentLoaded', () => { | ||
document.onpaste = event => { | ||
const {target} = event; | ||
if (!target.matches('.wp-block-button__link, .wp-block-button__link *')) { | ||
return; | ||
} | ||
|
||
const aTags = target.querySelectorAll('a'); | ||
if (aTags.length) { | ||
const {__} = wp.i18n; | ||
// eslint-disable-next-line no-alert | ||
alert(__( | ||
'You are pasting a link into the button text. Please ensure your clipboard only has text in it. Alternatively you can press control/command + SHIFT + V to paste only the text in your clipboard.', | ||
'planet4-blocks-backend' | ||
)); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.wp-block-file .wp-block-file__button { | ||
overflow: unset; | ||
padding: 0 1em; | ||
margin-inline-start: 1em; | ||
font-size: .8em; | ||
} |