| 1 |
(function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var __ = wp.i18n.__; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 8 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 9 |
var MediaUpload = wp.blockEditor.MediaUpload; |
| 10 |
var MediaUploadCheck = wp.blockEditor.MediaUploadCheck; |
| 11 |
var PanelBody = wp.components.PanelBody; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var ToggleControl = wp.components.ToggleControl; |
| 14 |
var TextControl = wp.components.TextControl; |
| 15 |
var TextareaControl = wp.components.TextareaControl; |
| 16 |
var SelectControl = wp.components.SelectControl; |
| 17 |
var Button = wp.components.Button; |
| 18 |
|
| 19 |
function getTypographyControl() { return (window.bkbgTypographyControl || function () { return null; }); } |
| 20 |
function getTypoCssVars() { return (window.bkbgTypoCssVars || function () { return {}; }); } |
| 21 |
function _tv(typo, prefix) { var fn = getTypoCssVars(); return fn(typo || {}, prefix); } |
| 22 |
|
| 23 |
var CHAT_STYLE_OPTIONS = [ |
| 24 |
{ label: 'Modern (rounded, colored right)', value: 'modern' }, |
| 25 |
{ label: 'Classic (minimal borders)', value: 'classic' }, |
| 26 |
{ label: 'Dark (dark background)', value: 'dark' }, |
| 27 |
]; |
| 28 |
|
| 29 |
function makeId() { return 'm' + Math.random().toString(36).substr(2, 6); } |
| 30 |
|
| 31 |
function Avatar(props) { |
| 32 |
var msg = props.msg; |
| 33 |
var size = props.size; |
| 34 |
var avatarBg = props.avatarBg; |
| 35 |
if (msg.avatarUrl) { |
| 36 |
return el('img', { src: msg.avatarUrl, alt: msg.senderName, style: { width: size + 'px', height: size + 'px', borderRadius: '50%', objectFit: 'cover', flexShrink: 0 } }); |
| 37 |
} |
| 38 |
return el('div', { style: { width: size + 'px', height: size + 'px', borderRadius: '50%', background: avatarBg, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: Math.round(size * 0.38) + 'px', fontWeight: 700, color: '#374151', flexShrink: 0 } }, (msg.senderInitials || (msg.senderName || 'U').charAt(0)).toUpperCase()); |
| 39 |
} |
| 40 |
|
| 41 |
registerBlockType('blockenberg/chat-bubble', { |
| 42 |
edit: function (props) { |
| 43 |
var attributes = props.attributes; |
| 44 |
var setAttributes = props.setAttributes; |
| 45 |
var messages = attributes.messages; |
| 46 |
|
| 47 |
var blockProps = useBlockProps({ |
| 48 |
style: Object.assign({ paddingTop: attributes.paddingTop + 'px', paddingBottom: attributes.paddingBottom + 'px', backgroundColor: attributes.bgColor || undefined }, _tv(attributes.typoBubble, '--bkbg-cb-b-')) |
| 49 |
}); |
| 50 |
|
| 51 |
function setMsg(id, patch) { |
| 52 |
setAttributes({ messages: messages.map(function (m) { return m.id === id ? Object.assign({}, m, patch) : m; }) }); |
| 53 |
} |
| 54 |
function addMsg() { |
| 55 |
var isRight = messages.length % 2 !== 0; |
| 56 |
setAttributes({ messages: messages.concat([{ id: makeId(), side: isRight ? 'right' : 'left', senderName: isRight ? 'Customer' : 'Support', senderInitials: isRight ? 'C' : 'S', avatarUrl: '', avatarId: 0, text: 'New message…', time: '' }]) }); |
| 57 |
} |
| 58 |
function removeMsg(id) { |
| 59 |
if (messages.length <= 1) return; |
| 60 |
setAttributes({ messages: messages.filter(function (m) { return m.id !== id; }) }); |
| 61 |
} |
| 62 |
|
| 63 |
var isDark = attributes.chatStyle === 'dark'; |
| 64 |
var containerBg = isDark ? '#1f2937' : attributes.chatBg; |
| 65 |
|
| 66 |
return el(Fragment, null, |
| 67 |
el(InspectorControls, null, |
| 68 |
el(PanelBody, { title: __('Chat Style', 'blockenberg'), initialOpen: true }, |
| 69 |
el(SelectControl, { label: __('Style preset', 'blockenberg'), value: attributes.chatStyle, options: CHAT_STYLE_OPTIONS, onChange: function (v) { setAttributes({ chatStyle: v }); } }), |
| 70 |
el(RangeControl, { label: __('Max width (px)', 'blockenberg'), value: attributes.maxWidth, min: 280, max: 900, onChange: function (v) { setAttributes({ maxWidth: v }); } }), |
| 71 |
el(RangeControl, { label: __('Bubble border radius (px)', 'blockenberg'), value: attributes.bubbleRadius, min: 0, max: 32, onChange: function (v) { setAttributes({ bubbleRadius: v }); } }), |
| 72 |
el(RangeControl, { label: __('Container border radius (px)', 'blockenberg'), value: attributes.containerRadius, min: 0, max: 32, onChange: function (v) { setAttributes({ containerRadius: v }); } }), |
| 73 |
el(RangeControl, { label: __('Message gap (px)', 'blockenberg'), value: attributes.messageGap, min: 4, max: 40, onChange: function (v) { setAttributes({ messageGap: v }); } }), |
| 74 |
el(RangeControl, { label: __('Avatar size (px)', 'blockenberg'), value: attributes.avatarSize, min: 24, max: 64, onChange: function (v) { setAttributes({ avatarSize: v }); } }), |
| 75 |
el(ToggleControl, { label: __('Show avatars', 'blockenberg'), checked: attributes.showAvatars, onChange: function (v) { setAttributes({ showAvatars: v }); }, __nextHasNoMarginBottom: true }), |
| 76 |
el(ToggleControl, { label: __('Show sender names', 'blockenberg'), checked: attributes.showSenderName, onChange: function (v) { setAttributes({ showSenderName: v }); }, __nextHasNoMarginBottom: true }), |
| 77 |
el(ToggleControl, { label: __('Show timestamps', 'blockenberg'), checked: attributes.showTimestamp, onChange: function (v) { setAttributes({ showTimestamp: v }); }, __nextHasNoMarginBottom: true }), |
| 78 |
el(ToggleControl, { label: __('Typing indicator at end', 'blockenberg'), checked: attributes.typingIndicator, onChange: function (v) { setAttributes({ typingIndicator: v }); }, __nextHasNoMarginBottom: true }), |
| 79 |
el(ToggleControl, { label: __('Show container border', 'blockenberg'), checked: attributes.showBorder, onChange: function (v) { setAttributes({ showBorder: v }); }, __nextHasNoMarginBottom: true }) |
| 80 |
), |
| 81 |
|
| 82 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 83 |
el(getTypographyControl(), { label: __('Bubble Text', 'blockenberg'), value: attributes.typoBubble, onChange: function (v) { setAttributes({ typoBubble: v }); } }) |
| 84 |
), |
| 85 |
el(PanelColorSettings, { |
| 86 |
title: __('Colors', 'blockenberg'), |
| 87 |
initialOpen: false, |
| 88 |
colorSettings: [ |
| 89 |
{ value: attributes.leftBubbleBg, onChange: function (v) { setAttributes({ leftBubbleBg: v || '' }); }, label: __('Left bubble background', 'blockenberg') }, |
| 90 |
{ value: attributes.leftBubbleColor, onChange: function (v) { setAttributes({ leftBubbleColor: v || '' }); }, label: __('Left bubble text', 'blockenberg') }, |
| 91 |
{ value: attributes.rightBubbleBg, onChange: function (v) { setAttributes({ rightBubbleBg: v || '' }); }, label: __('Right bubble background', 'blockenberg') }, |
| 92 |
{ value: attributes.rightBubbleColor, onChange: function (v) { setAttributes({ rightBubbleColor: v || '' }); }, label: __('Right bubble text', 'blockenberg') }, |
| 93 |
{ value: attributes.avatarBg, onChange: function (v) { setAttributes({ avatarBg: v || '' }); }, label: __('Avatar background', 'blockenberg') }, |
| 94 |
{ value: attributes.chatBg, onChange: function (v) { setAttributes({ chatBg: v || '' }); }, label: __('Chat container background', 'blockenberg') }, |
| 95 |
{ value: attributes.timeColor, onChange: function (v) { setAttributes({ timeColor: v || '' }); }, label: __('Timestamp color', 'blockenberg') }, |
| 96 |
{ value: attributes.borderColor, onChange: function (v) { setAttributes({ borderColor: v || '' }); }, label: __('Container border color', 'blockenberg') }, |
| 97 |
{ value: attributes.bgColor, onChange: function (v) { setAttributes({ bgColor: v || '' }); }, label: __('Section background', 'blockenberg') }, |
| 98 |
] |
| 99 |
}), |
| 100 |
el(PanelBody, { title: __('Spacing', 'blockenberg'), initialOpen: false }, |
| 101 |
el(RangeControl, { label: __('Padding top (px)', 'blockenberg'), value: attributes.paddingTop, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingTop: v }); } }), |
| 102 |
el(RangeControl, { label: __('Padding bottom (px)', 'blockenberg'), value: attributes.paddingBottom, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingBottom: v }); } }) |
| 103 |
), |
| 104 |
el(PanelBody, { title: __('Messages', 'blockenberg'), initialOpen: false }, |
| 105 |
messages.map(function (msg, idx) { |
| 106 |
return el(PanelBody, { key: msg.id, title: (msg.senderName || 'Message') + ' #' + (idx + 1), initialOpen: false }, |
| 107 |
el(SelectControl, { label: __('Side', 'blockenberg'), value: msg.side, options: [{ label: 'Left', value: 'left' }, { label: 'Right', value: 'right' }], onChange: function (v) { setMsg(msg.id, { side: v }); } }), |
| 108 |
el(TextControl, { label: __('Sender name', 'blockenberg'), value: msg.senderName, onChange: function (v) { setMsg(msg.id, { senderName: v }); } }), |
| 109 |
el(TextControl, { label: __('Initials (fallback avatar)', 'blockenberg'), value: msg.senderInitials, onChange: function (v) { setMsg(msg.id, { senderInitials: v }); } }), |
| 110 |
el(TextareaControl, { label: __('Message text', 'blockenberg'), value: msg.text, rows: 2, onChange: function (v) { setMsg(msg.id, { text: v }); } }), |
| 111 |
el(TextControl, { label: __('Timestamp', 'blockenberg'), value: msg.time, placeholder: '9:41 AM', onChange: function (v) { setMsg(msg.id, { time: v }); } }), |
| 112 |
el('p', { style: { margin: '8px 0 4px', fontWeight: 600, fontSize: '11px', textTransform: 'uppercase', letterSpacing: '0.06em' } }, __('Avatar image (optional)', 'blockenberg')), |
| 113 |
el(MediaUploadCheck, null, |
| 114 |
el(MediaUpload, { |
| 115 |
onSelect: function (media) { setMsg(msg.id, { avatarUrl: media.url, avatarId: media.id }); }, |
| 116 |
allowedTypes: ['image'], |
| 117 |
value: msg.avatarId, |
| 118 |
render: function (ref) { |
| 119 |
return el(Button, { onClick: ref.open, variant: 'secondary', size: 'compact', style: { marginBottom: '6px' } }, |
| 120 |
msg.avatarUrl ? __('Change avatar', 'blockenberg') : __('Upload avatar', 'blockenberg') |
| 121 |
); |
| 122 |
} |
| 123 |
}) |
| 124 |
), |
| 125 |
msg.avatarUrl && el(Button, { onClick: function () { setMsg(msg.id, { avatarUrl: '', avatarId: 0 }); }, variant: 'tertiary', size: 'compact', isDestructive: true, style: { display: 'block', marginBottom: '6px' } }, __('Remove avatar', 'blockenberg')), |
| 126 |
el(Button, { onClick: function () { removeMsg(msg.id); }, variant: 'tertiary', isDestructive: true, size: 'compact' }, __('Remove message', 'blockenberg')) |
| 127 |
); |
| 128 |
}), |
| 129 |
el(Button, { onClick: addMsg, variant: 'primary', style: { marginTop: '8px' } }, __('+ Add Message', 'blockenberg')) |
| 130 |
) |
| 131 |
), |
| 132 |
el('div', blockProps, |
| 133 |
el('div', { style: { display: 'flex', justifyContent: 'center' } }, |
| 134 |
el('div', { |
| 135 |
className: 'bkbg-chat-container bkbg-chat-container--' + attributes.chatStyle, |
| 136 |
style: { |
| 137 |
maxWidth: attributes.maxWidth + 'px', |
| 138 |
width: '100%', |
| 139 |
background: containerBg, |
| 140 |
borderRadius: attributes.containerRadius + 'px', |
| 141 |
padding: '24px', |
| 142 |
border: attributes.showBorder ? '1px solid ' + (isDark ? 'rgba(255,255,255,0.1)' : attributes.borderColor) : 'none', |
| 143 |
display: 'flex', |
| 144 |
flexDirection: 'column', |
| 145 |
gap: attributes.messageGap + 'px', |
| 146 |
} |
| 147 |
}, |
| 148 |
messages.map(function (msg) { |
| 149 |
var isRight = msg.side === 'right'; |
| 150 |
var bubbleBg = isRight ? attributes.rightBubbleBg : attributes.leftBubbleBg; |
| 151 |
var bubbleColor = isRight ? attributes.rightBubbleColor : attributes.leftBubbleColor; |
| 152 |
if (isDark && !isRight) { bubbleBg = '#374151'; bubbleColor = '#f9fafb'; } |
| 153 |
return el('div', { key: msg.id, className: 'bkbg-chat-msg' + (isRight ? ' bkbg-chat-msg--right' : ''), style: { display: 'flex', flexDirection: isRight ? 'row-reverse' : 'row', alignItems: 'flex-end', gap: '8px' } }, |
| 154 |
attributes.showAvatars && el(Avatar, { msg: msg, size: attributes.avatarSize, avatarBg: attributes.avatarBg }), |
| 155 |
el('div', { style: { maxWidth: '80%', display: 'flex', flexDirection: 'column', alignItems: isRight ? 'flex-end' : 'flex-start', gap: '3px' } }, |
| 156 |
attributes.showSenderName && msg.senderName && el('p', { style: { fontSize: '11px', color: isDark ? 'rgba(255,255,255,0.5)' : attributes.timeColor, margin: 0, fontWeight: 600 } }, msg.senderName), |
| 157 |
el('div', { className: 'bkbg-chat-bubble', style: { background: bubbleBg, color: bubbleColor, borderRadius: attributes.bubbleRadius + 'px', padding: '10px 14px' } }, msg.text), |
| 158 |
attributes.showTimestamp && msg.time && el('p', { style: { fontSize: '11px', color: isDark ? 'rgba(255,255,255,0.4)' : attributes.timeColor, margin: 0 } }, msg.time) |
| 159 |
) |
| 160 |
); |
| 161 |
}), |
| 162 |
attributes.typingIndicator && el('div', { className: 'bkbg-chat-msg', style: { display: 'flex', alignItems: 'flex-end', gap: '8px' } }, |
| 163 |
el('div', { style: { width: attributes.avatarSize + 'px', height: attributes.avatarSize + 'px', borderRadius: '50%', background: isDark ? '#374151' : attributes.avatarBg } }), |
| 164 |
el('div', { className: 'bkbg-typing-indicator', style: { background: isDark ? '#374151' : attributes.leftBubbleBg, borderRadius: attributes.bubbleRadius + 'px', padding: '10px 16px', display: 'flex', gap: '4px', alignItems: 'center' } }, |
| 165 |
el('span', { className: 'bkbg-dot' }), |
| 166 |
el('span', { className: 'bkbg-dot' }), |
| 167 |
el('span', { className: 'bkbg-dot' }) |
| 168 |
) |
| 169 |
) |
| 170 |
) |
| 171 |
) |
| 172 |
) |
| 173 |
); |
| 174 |
}, |
| 175 |
|
| 176 |
save: function (props) { |
| 177 |
var attributes = props.attributes; |
| 178 |
var messages = attributes.messages; |
| 179 |
var isDark = attributes.chatStyle === 'dark'; |
| 180 |
var containerBg = isDark ? '#1f2937' : attributes.chatBg; |
| 181 |
var blockProps = wp.blockEditor.useBlockProps.save({ className: 'bkbg-chat-bubble-wrap' }); |
| 182 |
|
| 183 |
return el('div', Object.assign({}, blockProps, { style: Object.assign({ paddingTop: attributes.paddingTop + 'px', paddingBottom: attributes.paddingBottom + 'px', backgroundColor: attributes.bgColor || undefined }, _tv(attributes.typoBubble, '--bkbg-cb-b-')) }), |
| 184 |
el('div', { style: { display: 'flex', justifyContent: 'center' } }, |
| 185 |
el('div', { |
| 186 |
className: 'bkbg-chat-container bkbg-chat-container--' + attributes.chatStyle, |
| 187 |
style: { |
| 188 |
maxWidth: attributes.maxWidth + 'px', |
| 189 |
width: '100%', |
| 190 |
background: containerBg, |
| 191 |
borderRadius: attributes.containerRadius + 'px', |
| 192 |
padding: '24px', |
| 193 |
border: attributes.showBorder ? '1px solid ' + (isDark ? 'rgba(255,255,255,0.1)' : attributes.borderColor) : 'none', |
| 194 |
display: 'flex', |
| 195 |
flexDirection: 'column', |
| 196 |
gap: attributes.messageGap + 'px', |
| 197 |
} |
| 198 |
}, |
| 199 |
messages.map(function (msg) { |
| 200 |
var isRight = msg.side === 'right'; |
| 201 |
var bubbleBg = isRight ? attributes.rightBubbleBg : attributes.leftBubbleBg; |
| 202 |
var bubbleColor = isRight ? attributes.rightBubbleColor : attributes.leftBubbleColor; |
| 203 |
if (isDark && !isRight) { bubbleBg = '#374151'; bubbleColor = '#f9fafb'; } |
| 204 |
var avatarEl = attributes.showAvatars |
| 205 |
? (msg.avatarUrl |
| 206 |
? el('img', { src: msg.avatarUrl, alt: msg.senderName, className: 'bkbg-chat-avatar', style: { width: attributes.avatarSize + 'px', height: attributes.avatarSize + 'px', borderRadius: '50%', objectFit: 'cover', flexShrink: 0 } }) |
| 207 |
: el('div', { className: 'bkbg-chat-avatar', style: { width: attributes.avatarSize + 'px', height: attributes.avatarSize + 'px', borderRadius: '50%', background: attributes.avatarBg, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: Math.round(attributes.avatarSize * 0.38) + 'px', fontWeight: 700, color: '#374151', flexShrink: 0 } }, (msg.senderInitials || (msg.senderName || 'U').charAt(0)).toUpperCase())) |
| 208 |
: null; |
| 209 |
return el('div', { key: msg.id, className: 'bkbg-chat-msg' + (isRight ? ' bkbg-chat-msg--right' : ''), style: { display: 'flex', flexDirection: isRight ? 'row-reverse' : 'row', alignItems: 'flex-end', gap: '8px' } }, |
| 210 |
avatarEl, |
| 211 |
el('div', { style: { maxWidth: '80%', display: 'flex', flexDirection: 'column', alignItems: isRight ? 'flex-end' : 'flex-start', gap: '3px' } }, |
| 212 |
attributes.showSenderName && msg.senderName && el('p', { className: 'bkbg-chat-sender', style: { fontSize: '11px', color: isDark ? 'rgba(255,255,255,0.5)' : attributes.timeColor, margin: 0, fontWeight: 600 } }, msg.senderName), |
| 213 |
el('div', { className: 'bkbg-chat-bubble', style: { background: bubbleBg, color: bubbleColor, borderRadius: attributes.bubbleRadius + 'px', padding: '10px 14px' } }, msg.text), |
| 214 |
attributes.showTimestamp && msg.time && el('p', { className: 'bkbg-chat-time', style: { fontSize: '11px', color: isDark ? 'rgba(255,255,255,0.4)' : attributes.timeColor, margin: 0 } }, msg.time) |
| 215 |
) |
| 216 |
); |
| 217 |
}), |
| 218 |
attributes.typingIndicator && el('div', { className: 'bkbg-chat-msg', style: { display: 'flex', alignItems: 'flex-end', gap: '8px' } }, |
| 219 |
el('div', { style: { width: attributes.avatarSize + 'px', height: attributes.avatarSize + 'px', borderRadius: '50%', background: isDark ? '#374151' : attributes.avatarBg } }), |
| 220 |
el('div', { className: 'bkbg-typing-indicator', style: { background: isDark ? '#374151' : attributes.leftBubbleBg, borderRadius: attributes.bubbleRadius + 'px', padding: '10px 16px', display: 'flex', gap: '4px', alignItems: 'center' } }, |
| 221 |
el('span', { className: 'bkbg-dot' }), |
| 222 |
el('span', { className: 'bkbg-dot' }), |
| 223 |
el('span', { className: 'bkbg-dot' }) |
| 224 |
) |
| 225 |
) |
| 226 |
) |
| 227 |
) |
| 228 |
); |
| 229 |
} |
| 230 |
}); |
| 231 |
}()); |
| 232 |
|