| 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 _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 17 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 18 |
|
| 19 |
/* ── Theme presets ─────────────────────────────────── */ |
| 20 |
var THEMES = { |
| 21 |
dark: { bg: '#1e1e2e', text: '#cdd6f4', command: '#89b4fa', output: '#a6e3a1', comment: '#6c7086', prompt: '#cba6f7' }, |
| 22 |
monokai: { bg: '#272822', text: '#f8f8f2', command: '#a6e22e', output: '#e6db74', comment: '#75715e', prompt: '#ae81ff' }, |
| 23 |
ocean: { bg: '#0d1117', text: '#e6edf3', command: '#58a6ff', output: '#3fb950', comment: '#8b949e', prompt: '#bc8cff' }, |
| 24 |
dracula: { bg: '#282a36', text: '#f8f8f2', command: '#50fa7b', output: '#ffb86c', comment: '#6272a4', prompt: '#ff79c6' }, |
| 25 |
light: { bg: '#f8f8f8', text: '#333333', command: '#0066cc', output: '#3a7c0d', comment: '#999999', prompt: '#8b5cf6' }, |
| 26 |
}; |
| 27 |
|
| 28 |
var THEME_OPTIONS = [ |
| 29 |
{ label: 'Dark (Catppuccin)', value: 'dark' }, |
| 30 |
{ label: 'Monokai', value: 'monokai' }, |
| 31 |
{ label: 'Ocean (GitHub)', value: 'ocean' }, |
| 32 |
{ label: 'Dracula', value: 'dracula' }, |
| 33 |
{ label: 'Light', value: 'light' }, |
| 34 |
]; |
| 35 |
|
| 36 |
var LINE_TYPE_OPTIONS = [ |
| 37 |
{ label: '$ Command', value: 'command' }, |
| 38 |
{ label: 'Output', value: 'output' }, |
| 39 |
{ label: '# Comment', value: 'comment' }, |
| 40 |
{ label: 'Blank line', value: 'blank' }, |
| 41 |
]; |
| 42 |
|
| 43 |
function makeId() { return 'l' + Math.random().toString(36).substr(2, 6); } |
| 44 |
|
| 45 |
function getTheme(a) { |
| 46 |
var t = THEMES[a.theme] || THEMES.dark; |
| 47 |
return { |
| 48 |
bg: a.termBg || t.bg, |
| 49 |
command: a.commandColor || t.command, |
| 50 |
output: a.outputColor || t.output, |
| 51 |
comment: a.commentColor || t.comment, |
| 52 |
prompt: a.promptColor || t.prompt, |
| 53 |
text: t.text, |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
function lineColor(type, colors) { |
| 58 |
if (type === 'command') return colors.command; |
| 59 |
if (type === 'output') return colors.output; |
| 60 |
if (type === 'comment') return colors.comment; |
| 61 |
return colors.text; |
| 62 |
} |
| 63 |
|
| 64 |
function renderLines(lines, a, colors, inEditor) { |
| 65 |
return lines.map(function (line, idx) { |
| 66 |
var isBlank = line.type === 'blank'; |
| 67 |
var isCmd = line.type === 'command'; |
| 68 |
var color = lineColor(line.type, colors); |
| 69 |
var lineText = (typeof line.text === 'string') ? line.text : (line.text == null ? '' : String(line.text)); |
| 70 |
return el('div', { |
| 71 |
key: line.id, |
| 72 |
className: 'bkbg-term-line bkbg-term-line--' + line.type, |
| 73 |
style: { display: 'flex', alignItems: 'baseline', gap: '6px', minHeight: isBlank ? '1.2em' : undefined } |
| 74 |
}, |
| 75 |
a.showLineNumbers && !isBlank && el('span', { style: { color: '#555', fontSize: '12px', userSelect: 'none', minWidth: '24px', textAlign: 'right', opacity: 0.5 } }, idx + 1), |
| 76 |
isCmd && el('span', { className: 'bkbg-term-prompt', style: { color: colors.prompt, userSelect: 'none', flexShrink: 0, fontWeight: 700 } }, |
| 77 |
a.promptHost && el('span', { style: { opacity: 0.75 } }, a.promptHost + ' '), |
| 78 |
a.promptSymbol + ' ' |
| 79 |
), |
| 80 |
!isBlank && el('span', { className: 'bkbg-term-text', 'data-type': line.type, 'data-bkbg-fulltext': lineText, style: { color: color, wordBreak: 'break-all' } }, lineText), |
| 81 |
isCmd && inEditor && el('span', { className: 'bkbg-term-cursor', style: { display: 'inline-block', width: '8px', height: '1em', background: colors.command, opacity: 0.8, animation: 'none', verticalAlign: 'text-bottom' } }) |
| 82 |
); |
| 83 |
}); |
| 84 |
} |
| 85 |
|
| 86 |
registerBlockType('blockenberg/terminal-mockup', { |
| 87 |
edit: function (props) { |
| 88 |
var attributes = props.attributes; |
| 89 |
var setAttributes = props.setAttributes; |
| 90 |
var lines = attributes.lines; |
| 91 |
var colors = getTheme(attributes); |
| 92 |
|
| 93 |
var blockProps = useBlockProps((function () { |
| 94 |
var _tvf = getTypoCssVars(); |
| 95 |
var s = { paddingTop: attributes.paddingTop + 'px', paddingBottom: attributes.paddingBottom + 'px', backgroundColor: attributes.bgColor || undefined }; |
| 96 |
if (_tvf) { Object.assign(s, _tvf(attributes.codeTypo, '--bktm-cd-')); } |
| 97 |
return { style: s }; |
| 98 |
})()); |
| 99 |
|
| 100 |
function setLine(id, patch) { |
| 101 |
setAttributes({ lines: lines.map(function (l) { return l.id === id ? Object.assign({}, l, patch) : l; }) }); |
| 102 |
} |
| 103 |
function addLine(type) { |
| 104 |
setAttributes({ lines: lines.concat([{ id: makeId(), type: type || 'command', text: '', prompt: true }]) }); |
| 105 |
} |
| 106 |
function removeLine(id) { |
| 107 |
if (lines.length <= 1) return; |
| 108 |
setAttributes({ lines: lines.filter(function (l) { return l.id !== id; }) }); |
| 109 |
} |
| 110 |
function moveLine(id, dir) { |
| 111 |
var idx = lines.findIndex(function (l) { return l.id === id; }); |
| 112 |
var newIdx = idx + dir; |
| 113 |
if (newIdx < 0 || newIdx >= lines.length) return; |
| 114 |
var arr = lines.slice(); |
| 115 |
var tmp = arr[idx]; arr[idx] = arr[newIdx]; arr[newIdx] = tmp; |
| 116 |
setAttributes({ lines: arr }); |
| 117 |
} |
| 118 |
|
| 119 |
return el(Fragment, null, |
| 120 |
el(InspectorControls, null, |
| 121 |
el(PanelBody, { title: __('Terminal Settings', 'blockenberg'), initialOpen: true }, |
| 122 |
el(SelectControl, { label: __('Color theme', 'blockenberg'), value: attributes.theme, options: THEME_OPTIONS, onChange: function (v) { setAttributes({ theme: v }); } }), |
| 123 |
el(ToggleControl, { label: __('Show window chrome (dots)', 'blockenberg'), checked: attributes.showChrome, onChange: function (v) { setAttributes({ showChrome: v }); }, __nextHasNoMarginBottom: true }), |
| 124 |
attributes.showChrome && el(TextControl, { label: __('Window tab title', 'blockenberg'), value: attributes.windowTitle, onChange: function (v) { setAttributes({ windowTitle: v }); } }), |
| 125 |
el(TextControl, { label: __('Prompt host', 'blockenberg'), value: attributes.promptHost, placeholder: 'user@server', onChange: function (v) { setAttributes({ promptHost: v }); } }), |
| 126 |
el(TextControl, { label: __('Prompt symbol', 'blockenberg'), value: attributes.promptSymbol, onChange: function (v) { setAttributes({ promptSymbol: v }); } }), |
| 127 |
el(ToggleControl, { label: __('Show line numbers', 'blockenberg'), checked: attributes.showLineNumbers, onChange: function (v) { setAttributes({ showLineNumbers: v }); }, __nextHasNoMarginBottom: true }), |
| 128 |
el(ToggleControl, { label: __('Show blinking cursor', 'blockenberg'), checked: attributes.showCursor, onChange: function (v) { setAttributes({ showCursor: v }); }, __nextHasNoMarginBottom: true }) |
| 129 |
), |
| 130 |
el(PanelBody, { title: __('Typewriter Animation', 'blockenberg'), initialOpen: false }, |
| 131 |
el(ToggleControl, { label: __('Typewriter mode (animates on scroll)', 'blockenberg'), checked: attributes.typewriterMode, onChange: function (v) { setAttributes({ typewriterMode: v }); }, __nextHasNoMarginBottom: true }), |
| 132 |
attributes.typewriterMode && el(RangeControl, { label: __('Typing speed (ms/character)', 'blockenberg'), value: attributes.typewriterSpeed, min: 10, max: 200, step: 5, onChange: function (v) { setAttributes({ typewriterSpeed: v }); } }) |
| 133 |
), |
| 134 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 135 |
getTypoControl()({ label: __('Code Text', 'blockenberg'), value: attributes.codeTypo, onChange: function (v) { setAttributes({ codeTypo: v }); } }) |
| 136 |
), |
| 137 |
el(PanelBody, { title: __('Appearance', 'blockenberg'), initialOpen: false }, |
| 138 |
el(RangeControl, { label: __('Max width (px)', 'blockenberg'), value: attributes.maxWidth, min: 300, max: 1200, onChange: function (v) { setAttributes({ maxWidth: v }); } }), |
| 139 |
el(RangeControl, { label: __('Border radius (px)', 'blockenberg'), value: attributes.borderRadius, min: 0, max: 24, onChange: function (v) { setAttributes({ borderRadius: v }); } }), |
| 140 |
el(RangeControl, { label: __('Window padding (px)', 'blockenberg'), value: attributes.windowPadding, min: 8, max: 48, onChange: function (v) { setAttributes({ windowPadding: v }); } }) |
| 141 |
), |
| 142 |
el(PanelColorSettings, { |
| 143 |
title: __('Color Overrides', 'blockenberg'), |
| 144 |
initialOpen: false, |
| 145 |
disableCustomColors: false, |
| 146 |
colorSettings: [ |
| 147 |
{ value: attributes.termBg, onChange: function (v) { setAttributes({ termBg: v || '' }); }, label: __('Terminal background', 'blockenberg') }, |
| 148 |
{ value: attributes.commandColor, onChange: function (v) { setAttributes({ commandColor: v || '' }); }, label: __('Command text', 'blockenberg') }, |
| 149 |
{ value: attributes.outputColor, onChange: function (v) { setAttributes({ outputColor: v || '' }); }, label: __('Output text', 'blockenberg') }, |
| 150 |
{ value: attributes.commentColor, onChange: function (v) { setAttributes({ commentColor: v || '' }); }, label: __('Comment text', 'blockenberg') }, |
| 151 |
{ value: attributes.promptColor, onChange: function (v) { setAttributes({ promptColor: v || '' }); }, label: __('Prompt', 'blockenberg') }, |
| 152 |
{ value: attributes.bgColor, onChange: function (v) { setAttributes({ bgColor: v || '' }); }, label: __('Section background', 'blockenberg') }, |
| 153 |
] |
| 154 |
}), |
| 155 |
el(PanelBody, { title: __('Spacing', 'blockenberg'), initialOpen: false }, |
| 156 |
el(RangeControl, { label: __('Padding top (px)', 'blockenberg'), value: attributes.paddingTop, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingTop: v }); } }), |
| 157 |
el(RangeControl, { label: __('Padding bottom (px)', 'blockenberg'), value: attributes.paddingBottom, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingBottom: v }); } }) |
| 158 |
), |
| 159 |
el(PanelBody, { title: __('Lines', 'blockenberg'), initialOpen: false }, |
| 160 |
lines.map(function (line, idx) { |
| 161 |
return el(PanelBody, { key: line.id, title: (line.type === 'blank' ? __('Blank line', 'blockenberg') : (line.text.substr(0, 28) || '(' + line.type + ')')) + ' #' + (idx + 1), initialOpen: false }, |
| 162 |
el(SelectControl, { label: __('Line type', 'blockenberg'), value: line.type, options: LINE_TYPE_OPTIONS, onChange: function (v) { setLine(line.id, { type: v }); } }), |
| 163 |
line.type !== 'blank' && el(TextControl, { label: __('Text', 'blockenberg'), value: line.text, onChange: function (v) { setLine(line.id, { text: v }); } }), |
| 164 |
el('div', { style: { display: 'flex', gap: '4px', flexWrap: 'wrap', marginTop: '6px' } }, |
| 165 |
el(Button, { onClick: function () { moveLine(line.id, -1); }, variant: 'tertiary', size: 'compact', disabled: idx === 0 }, '↑'), |
| 166 |
el(Button, { onClick: function () { moveLine(line.id, 1); }, variant: 'tertiary', size: 'compact', disabled: idx === lines.length - 1 }, '↓'), |
| 167 |
el(Button, { onClick: function () { removeLine(line.id); }, variant: 'tertiary', size: 'compact', isDestructive: true }, __('Remove', 'blockenberg')) |
| 168 |
) |
| 169 |
); |
| 170 |
}), |
| 171 |
el('div', { style: { display: 'flex', gap: '6px', flexWrap: 'wrap', marginTop: '8px' } }, |
| 172 |
el(Button, { onClick: function () { addLine('command'); }, variant: 'secondary', size: 'compact' }, __('+ Command', 'blockenberg')), |
| 173 |
el(Button, { onClick: function () { addLine('output'); }, variant: 'secondary', size: 'compact' }, __('+ Output', 'blockenberg')), |
| 174 |
el(Button, { onClick: function () { addLine('comment'); }, variant: 'secondary', size: 'compact' }, __('+ Comment', 'blockenberg')), |
| 175 |
el(Button, { onClick: function () { addLine('blank'); }, variant: 'tertiary', size: 'compact' }, __('+ Blank', 'blockenberg')) |
| 176 |
) |
| 177 |
) |
| 178 |
), |
| 179 |
el('div', blockProps, |
| 180 |
el('div', { style: { maxWidth: attributes.maxWidth + 'px', margin: '0 auto' } }, |
| 181 |
el('div', { |
| 182 |
className: 'bkbg-terminal', |
| 183 |
'data-theme': attributes.theme, |
| 184 |
'data-typewriter': attributes.typewriterMode ? '1' : '0', |
| 185 |
'data-speed': attributes.typewriterSpeed, |
| 186 |
'data-cursor': attributes.showCursor ? '1' : '0', |
| 187 |
style: { background: colors.bg, borderRadius: attributes.borderRadius + 'px', overflow: 'hidden', boxShadow: '0 20px 60px rgba(0,0,0,0.3)' } |
| 188 |
}, |
| 189 |
attributes.showChrome && el('div', { className: 'bkbg-term-chrome', style: { display: 'flex', alignItems: 'center', gap: '8px', padding: '12px ' + attributes.windowPadding + 'px', borderBottom: '1px solid rgba(255,255,255,0.06)' } }, |
| 190 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#ff5f56', flexShrink: 0 } }), |
| 191 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#ffbd2e', flexShrink: 0 } }), |
| 192 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#27c93f', flexShrink: 0 } }), |
| 193 |
attributes.windowTitle && el('span', { style: { flex: 1, textAlign: 'center', fontSize: '12px', color: 'rgba(255,255,255,0.4)', userSelect: 'none' } }, attributes.windowTitle) |
| 194 |
), |
| 195 |
el('div', { className: 'bkbg-term-body', style: { padding: attributes.windowPadding + 'px', display: 'flex', flexDirection: 'column', gap: '2px' } }, |
| 196 |
renderLines(lines, attributes, colors, true) |
| 197 |
) |
| 198 |
) |
| 199 |
) |
| 200 |
) |
| 201 |
); |
| 202 |
}, |
| 203 |
|
| 204 |
save: function (props) { |
| 205 |
var a = props.attributes; |
| 206 |
var colors = getTheme(a); |
| 207 |
var blockProps = wp.blockEditor.useBlockProps.save((function () { |
| 208 |
var _tvf = getTypoCssVars(); |
| 209 |
var s = { paddingTop: a.paddingTop + 'px', paddingBottom: a.paddingBottom + 'px', backgroundColor: a.bgColor || undefined }; |
| 210 |
if (_tvf) { Object.assign(s, _tvf(a.codeTypo, '--bktm-cd-')); } |
| 211 |
return { className: 'bkbg-terminal-mockup-wrap', style: s }; |
| 212 |
})()); |
| 213 |
|
| 214 |
return el('div', blockProps, |
| 215 |
el('div', { style: { maxWidth: a.maxWidth + 'px', margin: '0 auto' } }, |
| 216 |
el('div', { |
| 217 |
className: 'bkbg-terminal', |
| 218 |
'data-theme': a.theme, |
| 219 |
'data-typewriter': a.typewriterMode ? '1' : '0', |
| 220 |
'data-speed': a.typewriterSpeed, |
| 221 |
'data-cursor': a.showCursor ? '1' : '0', |
| 222 |
style: { background: colors.bg, borderRadius: a.borderRadius + 'px', overflow: 'hidden', boxShadow: '0 20px 60px rgba(0,0,0,0.3)' } |
| 223 |
}, |
| 224 |
a.showChrome && el('div', { className: 'bkbg-term-chrome', style: { display: 'flex', alignItems: 'center', gap: '8px', padding: '12px ' + a.windowPadding + 'px', borderBottom: '1px solid rgba(255,255,255,0.06)' } }, |
| 225 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#ff5f56', flexShrink: 0 } }), |
| 226 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#ffbd2e', flexShrink: 0 } }), |
| 227 |
el('span', { style: { width: '12px', height: '12px', borderRadius: '50%', background: '#27c93f', flexShrink: 0 } }), |
| 228 |
a.windowTitle && el('span', { style: { flex: 1, textAlign: 'center', fontSize: '12px', color: 'rgba(255,255,255,0.4)', userSelect: 'none' } }, a.windowTitle) |
| 229 |
), |
| 230 |
el('div', { className: 'bkbg-term-body', style: { padding: a.windowPadding + 'px', display: 'flex', flexDirection: 'column', gap: '2px' } }, |
| 231 |
a.lines.map(function (line, idx) { |
| 232 |
var isBlank = line.type === 'blank'; |
| 233 |
var isCmd = line.type === 'command'; |
| 234 |
var color = lineColor(line.type, colors); |
| 235 |
var lineText = (typeof line.text === 'string') ? line.text : (line.text == null ? '' : String(line.text)); |
| 236 |
return el('div', { key: line.id, className: 'bkbg-term-line bkbg-term-line--' + line.type, style: { display: 'flex', alignItems: 'baseline', gap: '6px', minHeight: isBlank ? '1.2em' : undefined } }, |
| 237 |
a.showLineNumbers && !isBlank && el('span', { className: 'bkbg-term-lnum', style: { color: '#555', fontSize: '12px', userSelect: 'none', minWidth: '24px', textAlign: 'right', opacity: 0.5 } }, idx + 1), |
| 238 |
isCmd && el('span', { className: 'bkbg-term-prompt', style: { color: colors.prompt, userSelect: 'none', flexShrink: 0, fontWeight: 700 } }, |
| 239 |
a.promptHost && el('span', { style: { opacity: 0.75 } }, a.promptHost + ' '), |
| 240 |
a.promptSymbol + ' ' |
| 241 |
), |
| 242 |
!isBlank && el('span', { className: 'bkbg-term-text', 'data-type': line.type, 'data-bkbg-fulltext': lineText, style: { color: color, wordBreak: 'break-all' } }, lineText), |
| 243 |
isCmd && a.showCursor && idx === a.lines.length - 1 && el('span', { className: 'bkbg-term-cursor', style: { display: 'inline-block', width: '8px', height: '1em', background: colors.command, verticalAlign: 'text-bottom' } }) |
| 244 |
); |
| 245 |
}) |
| 246 |
) |
| 247 |
) |
| 248 |
) |
| 249 |
); |
| 250 |
} |
| 251 |
}); |
| 252 |
}()); |
| 253 |
|