| 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 PanelBody = wp.components.PanelBody; |
| 10 |
var RangeControl = wp.components.RangeControl; |
| 11 |
var ToggleControl = wp.components.ToggleControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
var Button = wp.components.Button; |
| 15 |
|
| 16 |
var _TypographyControl, _typoCssVars; |
| 17 |
function getTypographyControl() { return _TypographyControl || (_TypographyControl = window.bkbgTypographyControl); } |
| 18 |
function getTypoCssVars() { return _typoCssVars || (_typoCssVars = window.bkbgTypoCssVars); } |
| 19 |
|
| 20 |
var PLATFORM_OPTIONS = [ |
| 21 |
{ label: 'Mac only', value: 'mac' }, |
| 22 |
{ label: 'Windows only', value: 'windows' }, |
| 23 |
{ label: 'Both (Mac / Win)', value: 'both' } |
| 24 |
]; |
| 25 |
|
| 26 |
function upd(arr, idx, field, val) { |
| 27 |
return arr.map(function (e, i) { |
| 28 |
if (i !== idx) return e; |
| 29 |
var u = {}; u[field] = val; |
| 30 |
return Object.assign({}, e, u); |
| 31 |
}); |
| 32 |
} |
| 33 |
|
| 34 |
function updShortcut(sections, sIdx, scIdx, field, val) { |
| 35 |
return sections.map(function (sec, si) { |
| 36 |
if (si !== sIdx) return sec; |
| 37 |
return Object.assign({}, sec, { |
| 38 |
shortcuts: sec.shortcuts.map(function (sc, sci) { |
| 39 |
if (sci !== scIdx) return sc; |
| 40 |
var u = {}; u[field] = val; |
| 41 |
return Object.assign({}, sc, u); |
| 42 |
}) |
| 43 |
}); |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
function renderKeys(keysStr, a) { |
| 48 |
if (!keysStr) return null; |
| 49 |
var parts = keysStr.split(' '); |
| 50 |
return el('div', { className: 'bkbg-ks-keys', style: { display: 'flex', alignItems: 'center', gap: '3px', flexWrap: 'nowrap' } }, |
| 51 |
parts.map(function (k, i) { |
| 52 |
if (k === '/') return el('span', { key: i, style: { color: a.keyColor, opacity: 0.4, fontSize: a.keyFontSize + 'px' } }, '/'); |
| 53 |
if (k === '+') return el('span', { key: i, style: { color: a.keyColor, opacity: 0.4, fontSize: a.keyFontSize + 'px' } }, '+'); |
| 54 |
return el('kbd', { key: i, className: 'bkbg-ks-key', style: { |
| 55 |
display: 'inline-block', padding: '2px 7px', background: a.keyBg, |
| 56 |
border: '1px solid ' + a.keyBorderColor, borderBottom: '3px solid ' + a.keyBorderColor, |
| 57 |
borderRadius: a.keyRadius + 'px', fontSize: a.keyFontSize + 'px', |
| 58 |
color: a.keyColor, fontFamily: 'inherit', lineHeight: 1.4, whiteSpace: 'nowrap' |
| 59 |
} }, k); |
| 60 |
}) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
function renderShortcutRow(sc, a, idx) { |
| 65 |
var showMac = (a.platform === 'mac' || a.platform === 'both'); |
| 66 |
var showWin = (a.platform === 'windows' || a.platform === 'both'); |
| 67 |
return el('div', { key: idx, className: 'bkbg-ks-row', style: { |
| 68 |
display: 'flex', alignItems: 'center', justifyContent: 'space-between', |
| 69 |
padding: '9px 20px', gap: 16, |
| 70 |
borderTop: idx > 0 ? '1px solid ' + a.separatorColor : 'none' |
| 71 |
} }, |
| 72 |
el('span', { className: 'bkbg-ks-desc', style: { color: a.descColor, fontSize: a.fontSize + 'px', flex: 1 } }, sc.description), |
| 73 |
el('div', { className: 'bkbg-ks-keys-wrap', style: { display: 'flex', gap: a.platform === 'both' ? 16 : 0, alignItems: 'center' } }, |
| 74 |
showMac && el('div', { style: { display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 2 } }, |
| 75 |
a.platform === 'both' && el('div', { style: { fontSize: '10px', color: a.macAccentColor, fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.05em', marginBottom: 2 } }, 'Mac'), |
| 76 |
renderKeys(sc.macKeys, a) |
| 77 |
), |
| 78 |
showWin && el('div', { style: { display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 2 } }, |
| 79 |
a.platform === 'both' && el('div', { style: { fontSize: '10px', color: a.winAccentColor, fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.05em', marginBottom: 2 } }, 'Win'), |
| 80 |
renderKeys(sc.winKeys, a) |
| 81 |
) |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
registerBlockType('blockenberg/keyboard-shortcut', { |
| 87 |
edit: function (props) { |
| 88 |
var a = props.attributes; |
| 89 |
var setAttr = props.setAttributes; |
| 90 |
|
| 91 |
return el(Fragment, null, |
| 92 |
el(InspectorControls, null, |
| 93 |
el(PanelBody, { title: __('Sheet Settings'), initialOpen: true }, |
| 94 |
el(TextControl, { label: __('Title'), value: a.sheetTitle, onChange: function (v) { setAttr({ sheetTitle: v }); }, __nextHasNoMarginBottom: true }), |
| 95 |
el(ToggleControl, { label: __('Show Title'), checked: a.showTitle, onChange: function (v) { setAttr({ showTitle: v }); }, __nextHasNoMarginBottom: true }), |
| 96 |
el(SelectControl, { label: __('Platform'), value: a.platform, options: PLATFORM_OPTIONS, onChange: function (v) { setAttr({ platform: v }); }, __nextHasNoMarginBottom: true }) |
| 97 |
), |
| 98 |
el(PanelBody, { title: __('Sections & Shortcuts'), initialOpen: false }, |
| 99 |
a.sections.map(function (sec, si) { |
| 100 |
return el('div', { key: si, style: { border: '1px solid #e5e7eb', borderRadius: 8, padding: 10, marginBottom: 10, background: '#f9fafb' } }, |
| 101 |
el('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 } }, |
| 102 |
el(TextControl, { label: __('Section Heading'), value: sec.heading, onChange: function (v) { setAttr({ sections: upd(a.sections, si, 'heading', v) }); }, __nextHasNoMarginBottom: true }), |
| 103 |
el(Button, { isDestructive: true, isSmall: true, onClick: function () { setAttr({ sections: a.sections.filter(function (_, j) { return j !== si; }) }); } }, '✕') |
| 104 |
), |
| 105 |
sec.shortcuts.map(function (sc, sci) { |
| 106 |
return el('div', { key: sci, style: { border: '1px solid #e2e8f0', borderRadius: 6, padding: 8, marginBottom: 6, background: '#fff' } }, |
| 107 |
el(TextControl, { label: __('Description'), value: sc.description, onChange: function (v) { setAttr({ sections: updShortcut(a.sections, si, sci, 'description', v) }); }, __nextHasNoMarginBottom: true }), |
| 108 |
el(TextControl, { label: __('Mac Keys (space-separated)'), value: sc.macKeys, onChange: function (v) { setAttr({ sections: updShortcut(a.sections, si, sci, 'macKeys', v) }); }, __nextHasNoMarginBottom: true }), |
| 109 |
el(TextControl, { label: __('Win Keys (space-separated)'), value: sc.winKeys, onChange: function (v) { setAttr({ sections: updShortcut(a.sections, si, sci, 'winKeys', v) }); }, __nextHasNoMarginBottom: true }), |
| 110 |
el(Button, { isDestructive: true, isSmall: true, onClick: function () { |
| 111 |
setAttr({ sections: a.sections.map(function (s, sj) { |
| 112 |
if (sj !== si) return s; |
| 113 |
return Object.assign({}, s, { shortcuts: s.shortcuts.filter(function (_, j) { return j !== sci; }) }); |
| 114 |
}) }); |
| 115 |
} }, __('Remove shortcut')) |
| 116 |
); |
| 117 |
}), |
| 118 |
el(Button, { variant: 'secondary', isSmall: true, onClick: function () { |
| 119 |
setAttr({ sections: a.sections.map(function (s, sj) { |
| 120 |
if (sj !== si) return s; |
| 121 |
return Object.assign({}, s, { shortcuts: s.shortcuts.concat([{ description: 'New shortcut', macKeys: '⌘ K', winKeys: 'Ctrl K' }]) }); |
| 122 |
}) }); |
| 123 |
} }, __('+ Add Shortcut')) |
| 124 |
); |
| 125 |
}), |
| 126 |
el(Button, { variant: 'primary', onClick: function () { setAttr({ sections: a.sections.concat([{ heading: 'New Section', shortcuts: [] }]) }); } }, __('+ Add Section')) |
| 127 |
), |
| 128 |
el(PanelBody, { title: __('Typography'), initialOpen: false }, |
| 129 |
el(getTypographyControl(), { label: __('Title Typography'), value: a.titleTypo, onChange: function (v) { setAttr({ titleTypo: v }); } }), |
| 130 |
el(RangeControl, { label: __('Font Size'), value: a.fontSize, min: 10, max: 20, onChange: function (v) { setAttr({ fontSize: v }); }, __nextHasNoMarginBottom: true }), |
| 131 |
el(RangeControl, { label: __('Section Heading Size'), value: a.headingFontSize, min: 9, max: 18, onChange: function (v) { setAttr({ headingFontSize: v }); }, __nextHasNoMarginBottom: true }), |
| 132 |
el(RangeControl, { label: __('Key Font Size'), value: a.keyFontSize, min: 9, max: 18, onChange: function (v) { setAttr({ keyFontSize: v }); }, __nextHasNoMarginBottom: true }), |
| 133 |
el(RangeControl, { label: __('Line Height (%)'), value: a.lineHeight, min: 120, max: 220, onChange: function (v) { setAttr({ lineHeight: v }); }, __nextHasNoMarginBottom: true }), |
| 134 |
el(RangeControl, { label: __('Border Radius'), value: a.borderRadius, min: 0, max: 24, onChange: function (v) { setAttr({ borderRadius: v }); }, __nextHasNoMarginBottom: true }), |
| 135 |
el(RangeControl, { label: __('Key Radius'), value: a.keyRadius, min: 2, max: 12, onChange: function (v) { setAttr({ keyRadius: v }); }, __nextHasNoMarginBottom: true }) |
| 136 |
), |
| 137 |
el(PanelColorSettings, { |
| 138 |
title: __('Colors'), |
| 139 |
initialOpen: false, |
| 140 |
colorSettings: [ |
| 141 |
{ label: __('Background'), value: a.bgColor, onChange: function (v) { setAttr({ bgColor: v || '#ffffff' }); } }, |
| 142 |
{ label: __('Border'), value: a.borderColor, onChange: function (v) { setAttr({ borderColor: v || '#e5e7eb' }); } }, |
| 143 |
{ label: __('Title'), value: a.titleColor, onChange: function (v) { setAttr({ titleColor: v || '#111827' }); } }, |
| 144 |
{ label: __('Section BG'), value: a.headingBg, onChange: function (v) { setAttr({ headingBg: v || '#f8fafc' }); } }, |
| 145 |
{ label: __('Section Text'), value: a.headingColor, onChange: function (v) { setAttr({ headingColor: v || '#374151' }); } }, |
| 146 |
{ label: __('Section Border'), value: a.headingBorderColor, onChange: function (v) { setAttr({ headingBorderColor: v || '#e5e7eb' }); } }, |
| 147 |
{ label: __('Description Text'), value: a.descColor, onChange: function (v) { setAttr({ descColor: v || '#374151' }); } }, |
| 148 |
{ label: __('Key BG'), value: a.keyBg, onChange: function (v) { setAttr({ keyBg: v || '#f1f5f9' }); } }, |
| 149 |
{ label: __('Key Border'), value: a.keyBorderColor, onChange: function (v) { setAttr({ keyBorderColor: v || '#cbd5e1' }); } }, |
| 150 |
{ label: __('Key Text'), value: a.keyColor, onChange: function (v) { setAttr({ keyColor: v || '#1e293b' }); } }, |
| 151 |
{ label: __('Mac Label'), value: a.macAccentColor, onChange: function (v) { setAttr({ macAccentColor: v || '#6366f1' }); } }, |
| 152 |
{ label: __('Win Label'), value: a.winAccentColor, onChange: function (v) { setAttr({ winAccentColor: v || '#0ea5e9' }); } }, |
| 153 |
{ label: __('Row Separator'), value: a.separatorColor, onChange: function (v) { setAttr({ separatorColor: v || '#f1f5f9' }); } } |
| 154 |
] |
| 155 |
}) |
| 156 |
), |
| 157 |
el('div', useBlockProps((function () { |
| 158 |
var _tv = getTypoCssVars(); |
| 159 |
var s = { background: a.bgColor, borderRadius: a.borderRadius + 'px', border: '1px solid ' + a.borderColor, overflow: 'hidden', fontSize: a.fontSize + 'px', lineHeight: (a.lineHeight / 100).toFixed(2) }; |
| 160 |
Object.assign(s, _tv(a.titleTypo, '--bkbg-ks-tt-')); |
| 161 |
return { className: 'bkbg-ks-wrap', style: s }; |
| 162 |
})()), |
| 163 |
a.showTitle && el('div', { className: 'bkbg-ks-header', style: { padding: '18px 20px', borderBottom: '1px solid ' + a.borderColor } }, |
| 164 |
el('h3', { className: 'bkbg-ks-title', style: { margin: 0, color: a.titleColor } }, a.sheetTitle) |
| 165 |
), |
| 166 |
a.sections.map(function (sec, si) { |
| 167 |
return el('div', { key: si, className: 'bkbg-ks-section' }, |
| 168 |
el('div', { className: 'bkbg-ks-section-head', style: { |
| 169 |
background: a.headingBg, padding: '7px 20px', |
| 170 |
borderTop: si === 0 && !a.showTitle ? 'none' : '1px solid ' + a.headingBorderColor, |
| 171 |
borderBottom: '1px solid ' + a.headingBorderColor |
| 172 |
} }, |
| 173 |
el('span', { style: { fontSize: a.headingFontSize + 'px', fontWeight: 700, color: a.headingColor, textTransform: 'uppercase', letterSpacing: '0.07em' } }, sec.heading) |
| 174 |
), |
| 175 |
sec.shortcuts.map(function (sc, sci) { return renderShortcutRow(sc, a, sci); }) |
| 176 |
); |
| 177 |
}) |
| 178 |
) |
| 179 |
); |
| 180 |
}, |
| 181 |
save: function (props) { |
| 182 |
return el('div', useBlockProps.save(), |
| 183 |
el('div', { className: 'bkbg-keyboard-shortcut-app', 'data-opts': JSON.stringify(props.attributes) }) |
| 184 |
); |
| 185 |
} |
| 186 |
}); |
| 187 |
})(); |
| 188 |
|