Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
spelkey-ucd committed Feb 9, 2024
1 parent 79db644 commit 30ac362
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 16 deletions.
Binary file added assets/img/block-defaults/382x382.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 17 additions & 16 deletions src/editor/lib/block-components/image-picker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { html } from "../utils";
import {
PanelBody,
PanelRow,
Button,
ResponsiveWrapper,
ToggleControl,
import {
PanelBody,
PanelRow,
Button,
ResponsiveWrapper,
ToggleControl,
TextareaControl } from "@wordpress/components";
import { MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
import { useState, Fragment } from '@wordpress/element';
Expand Down Expand Up @@ -80,7 +80,7 @@ function ImagePicker({
src: defaultImage.source_url,
modifiers: [{type: 'rotate', args: {angle: 0}}]
};
apiFetch({
apiFetch({
path: `/wp/v2/media/${ defaultImage.id }/edit`,
method: 'POST',
data: attrs,})
Expand All @@ -103,8 +103,8 @@ function ImagePicker({

const uploadButton = ({open}) => {
if ( onOpen ) onOpen();
const showText =
image != undefined &&
const showText =
image != undefined &&
renderImageName &&
image.title &&
image.title.rendered;
Expand Down Expand Up @@ -169,9 +169,9 @@ function ImagePicker({
${imageId != 0 && html`
<${MediaUploadCheck}>
<${Button}
<${Button}
onClick=${onRemove}
isLink
isLink
isDestructive
style=${horizontal ? {marginTop: 0} : {}}
>Remove Image
Expand All @@ -181,15 +181,16 @@ function ImagePicker({
${defaultImage && html`
<${MediaUploadCheck}>
<${Button}
<${Button}
isSecondary
onClick=${onClone}
disabled=${cloneInProgress}
style=${{marginTop: '1rem'}}
>${cloneText}
</${Button}>
</${MediaUploadCheck}>
`}
</div>
</div>
`;

const renderHelpText = () => html`
Expand All @@ -203,13 +204,13 @@ function ImagePicker({
${showCaptionOptions && html`
<div style=${{marginTop:'15px'}}>
<${PanelRow}>
<${ToggleControl}
<${ToggleControl}
label="Show Caption"
checked=${captionOptions.show}
onChange=${() => _onCaptionChange({show: !captionOptions.show })}
/>
</${PanelRow}>
<${TextareaControl}
<${TextareaControl}
label="Custom Caption"
help="Will be displayed instead of caption for image from media library"
value=${ captionOptions.customText }
Expand Down Expand Up @@ -238,4 +239,4 @@ function ImagePicker({
`;
}

export default ImagePicker
export default ImagePicker
2 changes: 2 additions & 0 deletions src/editor/lib/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import socialMedia from "./ucd-theme-social-media";
import spacer from "./ucd-theme-spacer";
import teaser from "./ucd-theme-teaser";
import teasers from "./ucd-theme-teasers";
import tileLink from "./ucd-theme-tile-link";
import trumba from "./ucd-theme-trumba";
import trumbaUpcoming from "./ucd-theme-trumba-upcoming";
import trumbaFilter from "./ucd-theme-trumba-filter";
Expand Down Expand Up @@ -107,6 +108,7 @@ export default [
spacer,
teaser,
teasers,
tileLink,
trumba,
trumbaUpcoming,
trumbaFilter
Expand Down
206 changes: 206 additions & 0 deletions src/editor/lib/blocks/ucd-theme-tile-link/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import {
html,
BlockSettings,
SelectUtils } from "../../utils";
import {
ImagePicker,
ToolbarColorPicker,
ToolbarPostReset,
ToolbarSectionDisplay,
ToolbarLinkPicker } from "../../block-components";
import {
useBlockProps,
BlockControls,
InspectorControls } from '@wordpress/block-editor';

import {
PanelBody,
PanelRow,
TextControl,
TextareaControl } from "@wordpress/components";
import classnames from 'classnames';

export default ( props ) => {
const { attributes, setAttributes } = props;
const blockProps = useBlockProps();

// retrieve needed wp data
const {customImage, post, postTitle, postExcerpt, postImage} = SelectUtils.card(attributes);
let customPostImage = 0;
if ( post && post.meta ) {
if ( post.meta.ucd_thumbnail_1x1_large ) {
customPostImage = post.meta.ucd_thumbnail_1x1_large;
} else if ( post.meta.ucd_thumbnail_4x3 ) {
customPostImage = post.meta.ucd_thumbnail_4x3;
}
}
customPostImage = SelectUtils.image(customPostImage);

// set up image picker
const onSelectImage = (image) => {
setAttributes({imageId: image.id});
}
const onRemoveImage = () => {
setAttributes({imageId: 0});
}

// set up post reset
const onPostReset = (part) => {
if ( part.slug === 'title') {
setAttributes({title: ""});
} else if (part.slug === 'excerpt') {
setAttributes({excerpt: ""});
} else if (part.slug === 'thumbnail') {
setAttributes({imageId: 0});
}
}
const postParts = (() => {
return [
{slug: "thumbnail", isDisabled: !attributes.imageId || !postImage},
{slug: 'title', isDisabled: !attributes.title},
{slug: 'excerpt', isDisabled: !attributes.excerpt}]
})();

// set up section hider
const onSectionToggle = (section) => {
let attrs = {};
let attr = `hide${section.slug.charAt(0).toUpperCase() + section.slug.slice(1)}`;
attrs[attr] = !attributes[attr];
setAttributes(attrs);
}
const cardSections = (() => {
return [
{slug: "title", isHidden: attributes.hideTitle},
{slug: 'excerpt', isHidden: attributes.hideExcerpt}
]
})();

// set up link picker
const onHrefChange = (value) => {
setAttributes({
href: value.kind === 'post-type' ? "" : value.url,
newTab: value.opensInNewTab ? true : false,
post: value.kind === 'post-type' ? {id: value.id, type: value.type} : {}
});
}
const hrefContent = (() => {
let value = {opensInNewTab: attributes.newTab, url: ""};
if ( attributes.href ) {
value.url = attributes.href;
} else if ( post && post.link ) {
value.url = post.link;
value.kind = 'post-type';
value.type = post.type;
}
return value;
})();

// set up color picker
const onColorChange = (value) => {
setAttributes( {brandColor: value ? value.slug : "" } );
}

let imgSrc = BlockSettings.getImage('tile-link');
if ( customImage ) {
imgSrc = customImage.source_url;
} else if ( customPostImage ){
imgSrc = customPostImage.source_url;
} else if ( postImage ){
imgSrc = postImage.source_url;
}

const classes = classnames({
'tile-link': true,
[`category-brand--${attributes.brandColor}`]: attributes.brandColor
})

let title = 'Use the block sidebar to add a custom title';
if ( attributes.title ){
title = attributes.title;
} else if ( postTitle ){
title = postTitle;
}

let excerpt = 'Use the block sidebar to add a custom excerpt';
if ( attributes.excerpt ){
excerpt = attributes.excerpt;
} else if ( postExcerpt ){
excerpt = postExcerpt;
}

return html`
<div ...${ blockProps }>
<${BlockControls} group="block">
<${ToolbarLinkPicker} onChange=${onHrefChange} value=${hrefContent} />
<${ToolbarColorPicker}
onChange=${onColorChange}
value=${attributes.brandColor}
ucdBlock="tile-link"
/>
<${ToolbarSectionDisplay}
sections=${cardSections}
onChange=${onSectionToggle}
/>
${post && html`
<${ToolbarPostReset}
postProps=${postParts}
onChange=${onPostReset}
/>
`}
</${BlockControls}>
<${InspectorControls}>
<${PanelBody} title="Card Content" initialOpen=${true}>
<${PanelRow}>
<${TextControl}
label="Title"
help="Primary text on card. Recommended 37 characters or less."
value=${ attributes.title }
onChange=${ ( title ) => setAttributes({title}) }
/>
</${PanelRow}>
<${PanelRow}>
<${TextareaControl}
label="Excerpt"
help="Will be displayed when card is hovered over"
value=${ attributes.excerpt }
onChange=${ ( excerpt ) => setAttributes({excerpt}) }
/>
</${PanelRow}>
</${PanelBody}>
<${ImagePicker}
imageId=${attributes.imageId}
image=${customImage}
onSelect=${onSelectImage}
onRemove=${onRemoveImage}
defaultImageId=${postImage && !attributes.imageId ? postImage.id : 0}
helpText="Use a 1:1 image and at least 382px for best results"
panelAttributes=${{title: 'Custom Card Image'}}
/>
</${InspectorControls}>
<a className="${classes}">
${(!attributes.hideTitle) && html`
<div className="tile-link__title">
<h3 className="tile-link__title-heading">${title}</h3>
</div>
`}
${(!attributes.hideExcerpt) && html`
<div className="tile-link__description">
<p>${excerpt}</p>
</div>
`}
<div className="tile-link__indicator">
<svg aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path d="M504 256C504 119 393 8 256 8S8 119 8 256s111 248 248 248 248-111 248-248zm-448 0c0-110.5 89.5-200 200-200s200 89.5 200 200-89.5 200-200 200S56 366.5 56 256zm72 20v-40c0-6.6 5.4-12 12-12h116v-67c0-10.7 12.9-16 20.5-8.5l99 99c4.7 4.7 4.7 12.3 0 17l-99 99c-7.6 7.6-20.5 2.2-20.5-8.5v-67H140c-6.6 0-12-5.4-12-12z"></path>
</svg>
</div>
<div className="o-filtered-image">
<div className="aspect--1x1 u-background-image" role='img' style=${ {backgroundImage: 'url(' + imgSrc + ')'} }></div>
</div>
<div className="tile-link__overlay"></div>
</a>
</div>
`;
}
57 changes: 57 additions & 0 deletions src/editor/lib/blocks/ucd-theme-tile-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { UCDIcons } from "../../utils";
import Edit from './edit';

const name = 'ucd-theme/tile-link';
const settings = {
api_version: 2,
title: "Tile Link",
description: "Preview content on another webpage with an image and hover description.",
icon: UCDIcons.renderPublic('fa-circle-right'),
category: 'ucd-cards',
keywords: ['post', 'page', "link" ],
supports: {
"html": false,
"customClassName": false
},
attributes: {
imageId: {
type: "number",
default: 0
},
post: {
type: "object",
default: {}
},
href: {
type: "string",
default: ""
},
newTab: {
type: "boolean",
default: false
},
brandColor: {
type: "string",
default: "primary"
},
title: {
type: "string",
default: ""
},
excerpt: {
type: "string",
default: ""
},
hideTitle: {
type: "boolean",
default: false
},
hideExcerpt: {
type: "boolean",
default: false
}
},
edit: Edit,
};

export default { name, settings };
Loading

0 comments on commit 30ac362

Please sign in to comment.