|
| 1 | +import React from "react" |
| 2 | + |
| 3 | +class BaseBox extends React.Component { |
| 4 | + parseCodeMarkdown(text) { |
| 5 | + if (typeof text !== 'string') return text; |
| 6 | + |
| 7 | + // Replace triple backticks (code blocks) first |
| 8 | + text = text.replace(/```([^`]+)```/g, (match, code) => { |
| 9 | + return `<div style="font-family: monospace; background-color: ${this.getCodeBlockBg()}; padding: 12px; border-radius: 4px; margin: 8px 0; border-left: 4px solid ${this.getCodeBlockBorder()}; overflow-x: auto; white-space: pre;">${code.trim()}</div>`; |
| 10 | + }); |
| 11 | + |
| 12 | + // Replace single backticks (inline code) |
| 13 | + text = text.replace(/`([^`]+)`/g, (match, code) => { |
| 14 | + return `<span style="font-family: monospace; background-color: ${this.getInlineCodeBg()}; padding: 2px 4px; border-radius: 3px; font-size: 14px;">${code}</span>`; |
| 15 | + }); |
| 16 | + |
| 17 | + return text; |
| 18 | + } |
| 19 | + |
| 20 | + // Methods to be overridden by child components |
| 21 | + getBackgroundColor() { return '#f5f5f5'; } |
| 22 | + getTextColor() { return '#424242'; } |
| 23 | + getBorderColor() { return '#e0e0e0'; } |
| 24 | + getIcon() { return '📋'; } |
| 25 | + getCodeBlockBg() { return '#f1f1f1'; } |
| 26 | + getCodeBlockBorder() { return '#007acc'; } |
| 27 | + getInlineCodeBg() { return '#e8e8e8'; } |
| 28 | + getButtonStyles() { |
| 29 | + return { |
| 30 | + backgroundColor: 'rgba(66, 66, 66, 0.1)', |
| 31 | + color: '#424242', |
| 32 | + border: '1px solid rgba(66, 66, 66, 0.3)', |
| 33 | + hoverBg: 'rgba(66, 66, 66, 0.2)' |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + render() { |
| 38 | + const { textContent, buttonText, hyperlink, children, scale = 1.0 } = this.props; |
| 39 | + const buttonStyles = this.getButtonStyles(); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div style={{ |
| 43 | + backgroundColor: this.getBackgroundColor(), |
| 44 | + color: this.getTextColor(), |
| 45 | + padding: `${16 * scale}px ${20 * scale}px`, |
| 46 | + marginBottom: `${20 * scale}px`, |
| 47 | + borderRadius: `${6 * scale}px`, |
| 48 | + border: `1px solid ${this.getBorderColor()}`, |
| 49 | + display: 'flex', |
| 50 | + alignItems: 'stretch', |
| 51 | + gap: `${16 * scale}px`, |
| 52 | + minHeight: `${60 * scale}px`, |
| 53 | + transform: `scale(${scale})`, |
| 54 | + transformOrigin: 'center center', |
| 55 | + margin: scale < 1 ? `${20 * scale * (1 - scale)}px auto ${20 * scale}px auto` : `0 auto ${20 * scale}px auto` |
| 56 | + }}> |
| 57 | + <div style={{ |
| 58 | + display: 'flex', |
| 59 | + alignItems: 'center', |
| 60 | + justifyContent: 'center', |
| 61 | + flexShrink: 0, |
| 62 | + width: `${40 * scale}px`, |
| 63 | + fontSize: `clamp(${24 * scale}px, 4vw, ${48 * scale}px)`, |
| 64 | + lineHeight: '1' |
| 65 | + }}> |
| 66 | + {this.getIcon()} |
| 67 | + </div> |
| 68 | + <div style={{ |
| 69 | + flex: '1', |
| 70 | + display: 'flex', |
| 71 | + alignItems: 'center' |
| 72 | + }}> |
| 73 | + <div style={{ |
| 74 | + fontWeight: 'normal', |
| 75 | + lineHeight: '1.6', |
| 76 | + fontFamily: 'inherit', |
| 77 | + width: '100%', |
| 78 | + fontSize: `${14 * scale}px`, |
| 79 | + whiteSpace: 'pre-line' |
| 80 | + }} dangerouslySetInnerHTML={{ |
| 81 | + __html: children ? null : (textContent ? this.parseCodeMarkdown(textContent.replace(/\\n/g, '\n')) : '') |
| 82 | + }}> |
| 83 | + {children} |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + {buttonText && hyperlink && ( |
| 87 | + <div style={{ |
| 88 | + display: 'flex', |
| 89 | + alignItems: 'center', |
| 90 | + flexShrink: 0 |
| 91 | + }}> |
| 92 | + <a |
| 93 | + href={hyperlink} |
| 94 | + target="_blank" |
| 95 | + rel="noopener noreferrer" |
| 96 | + style={{ |
| 97 | + backgroundColor: buttonStyles.backgroundColor, |
| 98 | + color: buttonStyles.color, |
| 99 | + textDecoration: 'none', |
| 100 | + padding: `${4 * scale}px ${8 * scale}px`, |
| 101 | + borderRadius: `${3 * scale}px`, |
| 102 | + border: buttonStyles.border, |
| 103 | + fontSize: `${12 * scale}px`, |
| 104 | + fontWeight: 'normal', |
| 105 | + transition: 'background-color 0.2s', |
| 106 | + whiteSpace: 'nowrap' |
| 107 | + }} |
| 108 | + onMouseOver={(e) => e.target.style.backgroundColor = buttonStyles.hoverBg} |
| 109 | + onMouseOut={(e) => e.target.style.backgroundColor = buttonStyles.backgroundColor} |
| 110 | + > |
| 111 | + {buttonText} |
| 112 | + </a> |
| 113 | + </div> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +export default BaseBox |
0 commit comments