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 30ac362 commit 0d0d8d8
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/editor/lib/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import mediaLink from "./ucd-theme-media-link";
import mediaLinks from "./ucd-theme-media-links";
import objectBox from "./ucd-theme-object-box";
import panelWithIcon from "./ucd-theme-panel-with-icon";
import photoCard from "./ucd-theme-photo-card";
import poster from "./ucd-theme-poster";
import posterList from "./ucd-theme-poster-list";
import prefixedIconLink from "./ucd-theme-prefixed-icon-link";
Expand Down Expand Up @@ -91,6 +92,7 @@ export default [
mediaLinks,
objectBox,
panelWithIcon,
photoCard,
poster,
posterList,
prefixedIconLink,
Expand Down
182 changes: 182 additions & 0 deletions src/editor/lib/blocks/ucd-theme-photo-card/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import {
html,
BlockSettings,
SelectUtils } from "../../utils";
import {
ImagePicker,
ToolbarColorPicker,
ToolbarPostReset,
ToolbarSectionDisplay,
ToolbarLinkPicker } from "../../block-components";
import {
useBlockProps,
BlockControls,
InspectorControls } from '@wordpress/block-editor';
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 === 'thumbnail') {
setAttributes({imageId: 0});
}
}
const postParts = (() => {
return [
{slug: "thumbnail", isDisabled: !attributes.imageId || !postImage},
{slug: 'title', isDisabled: !attributes.title}]
})();

// 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}
]
})();

// 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('photo-card');
if ( customImage ) {
imgSrc = customImage.source_url;
} else if ( customPostImage ){
imgSrc = customPostImage.source_url;
} else if ( postImage ){
imgSrc = postImage.source_url;
}

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

let title = '';
if ( attributes.title ){
title = attributes.title;
} else if ( postTitle ){
title = postTitle;
}

return html`
<div ...${ blockProps }>
<style>
.photo-card textarea {
text-align: center;
border: none;
box-shadow: none;
color: var(--forced-contrast, var(--category-brand--on-white, #022851));
margin-bottom: 2rem;
}
.photo-card textarea:focus {
background-color: transparent;
}
</style>
<${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}>
<${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}">
<div className="photo-card__image">
<div className="photo-card__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="aspect--1x1 u-background-image" role='img' style=${ {backgroundImage: 'url(' + imgSrc + ')'} }></div>
</div>
${(!attributes.hideTitle) && html`
<h3 className="photo-card__title">
<textarea
type="text"
value=${title}
placeholder="Write a title..."
onChange=${ (e) => setAttributes({title: e.target.value}) }>
</textarea>
</h3>
`}
</a>
</div>
`;
}
49 changes: 49 additions & 0 deletions src/editor/lib/blocks/ucd-theme-photo-card/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { UCDIcons } from "../../utils";
import Edit from './edit';

const name = 'ucd-theme/photo-card';
const settings = {
api_version: 2,
title: "Photo Card",
description: "Preview content on another webpage with an image and overlayed title.",
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: ""
},
hideTitle: {
type: "boolean",
default: false
}
},
edit: Edit,
};

export default { name, settings };
8 changes: 8 additions & 0 deletions src/public/scss/temp.scss
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,12 @@ hr.y2 {
float: right;
}

// photo card
.photo-card__image .u-background-image {
z-index: 1;
width: 100%;
height: auto;
transition: all .3s ease-in-out;
}


6 changes: 6 additions & 0 deletions theme/includes/classes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ function __construct($editor_script_slug, $settings=array()) {
"hasBrandColors" => true,
"transform" => array("getPermalink")
),
"ucd-theme/photo-card" => [
"twig" => "@ucd/blocks/photo-card.twig",
"img" => "382x382.png",
"transform" => ["getPost", "addSpacing"],
"hasBrandColors" => true
],
"ucd-theme/query" => array(
"twig" => "@ucd/blocks/query.twig",
"transform" => array("getPosts", 'addSpacing')
Expand Down
5 changes: 5 additions & 0 deletions views/blocks/photo-card.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% from getUcdMacro('cards') import photo_card %}
{% if not attributes.brandColor %}
{% set attributes = attributes|merge({brandColor: 'primary'})%}
{% endif %}
{{ photo_card(attributes) }}
68 changes: 68 additions & 0 deletions views/macros/cards.twig
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,74 @@ kwargs:
</a>
{% endmacro %}

{#
http://webstyleguide.ucdavis.edu/?p=molecules-photo-card
kwargs:
post {Timber\Post} - A post on this site.
imageId {Number} - Id of an image. Will override 'post' featured image
image {Timber\Image} - A preview image. Will override 'post' featured image and imageId
title {String} - The card title. Will override 'post' title
hideTitle {Boolean} - Hides the card title.
href {String} - Url for card link. Will override 'post' permalink
newTab {Boolean} - Opens card link in new tab
brandColor {String} - UCD alt color slug
#}
{% macro photo_card( kwargs ) %}

{% set title = kwargs.title ? kwargs.title : kwargs.post.title %}
{% set title = title|striptags %}
{% set href = kwargs.href ? kwargs.href : kwargs.post.link %}
{% set target = kwargs.newTab ? "target='_blank'" : "" %}

{% if kwargs.image %}
{% set src = kwargs.image.src %}
{% set alt = kwargs.image.alt %}
{% elseif kwargs.imageId %}
{% set i = get_post(kwargs.imageId) %}
{% set src = i.src %}
{% set alt = i.alt %}
{% elseif kwargs.post.card_image_square %}
{% set src = kwargs.post.card_image_square.src %}
{% set alt = kwargs.post.card_image_square.alt %}
{% endif %}

{% if not src %}
{% set src = DefaultImage('photo-card') %}
{% endif %}

{% if alt %}
{% set alt = alt|striptags %}
{% endif %}


{% set classes = 'photo-card' %}
{% if kwargs.brandColor %}
{% set classes = classes ~ " category-brand--" ~ kwargs.brandColor %}
{% endif %}
{% if kwargs.panel %}
{% set classes = classes ~ " u-space-mb" %}
{% endif %}
{#
{% if kwargs.oBox %}
{% set classes = classes ~ " o-box" %}
{% endif %}
#}

<a href="{{href}}" class="{{classes}}" {{target}}>
<div class="photo-card__image">
<div class="photo-card__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 class="aspect--1x1 u-background-image" role='img' aria-label='{{alt}}' style="background-image:url({{src}});"></div>
</div>
{% if title and not kwargs.hideTitle %}
<h3 class="photo-card__title">{{title}}</h3>
{% endif %}
</a>
{% endmacro %}


{% macro featured_article( kwargs ) %}

Expand Down

0 comments on commit 0d0d8d8

Please sign in to comment.