| 1 |
import { useEffect, useLayoutEffect, useRef } from '@wordpress/element'; |
| 2 |
import { applyFilters } from '@wordpress/hooks'; |
| 3 |
import Editor from 'react-simple-code-editor'; |
| 4 |
import { useDefaults } from '../hooks/useDefaults'; |
| 5 |
import { useTheme } from '../hooks/useTheme'; |
| 6 |
import { useLanguageStore } from '../state/language'; |
| 7 |
import { AttributesPropsAndSetter } from '../types'; |
| 8 |
|
| 9 |
export const Edit = ({ |
| 10 |
attributes, |
| 11 |
setAttributes, |
| 12 |
}: AttributesPropsAndSetter) => { |
| 13 |
const { |
| 14 |
language, |
| 15 |
theme, |
| 16 |
code = '', |
| 17 |
bgColor: backgroundColor, |
| 18 |
textColor: color, |
| 19 |
disablePadding, |
| 20 |
lineNumbersWidth, |
| 21 |
lineNumbers, |
| 22 |
startingLineNumber, |
| 23 |
footerType, |
| 24 |
fontSize, |
| 25 |
fontFamily, |
| 26 |
lineHeight, |
| 27 |
} = attributes; |
| 28 |
const textAreaRef = useRef<HTMLDivElement>(null); |
| 29 |
const handleChange = (code: string) => setAttributes({ code }); |
| 30 |
const { previousLanguage } = useLanguageStore(); |
| 31 |
const { highlighter, error, loading } = useTheme({ |
| 32 |
theme, |
| 33 |
lang: language ?? previousLanguage, |
| 34 |
}); |
| 35 |
const hasFooter = footerType && footerType !== 'none'; |
| 36 |
useDefaults({ attributes, setAttributes }); |
| 37 |
|
| 38 |
useEffect(() => { |
| 39 |
if (!highlighter) return; |
| 40 |
setAttributes({ |
| 41 |
bgColor: highlighter.getBackgroundColor(), |
| 42 |
textColor: highlighter.getForegroundColor(), |
| 43 |
}); |
| 44 |
}, [theme, highlighter, setAttributes]); |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
if (!highlighter) return; |
| 48 |
setAttributes({ |
| 49 |
codeHTML: applyFilters( |
| 50 |
'blocks.codeBlockPro.codeHTML', |
| 51 |
highlighter.codeToHtml(code, { |
| 52 |
lang: language ?? previousLanguage, |
| 53 |
}), |
| 54 |
attributes, |
| 55 |
) as string, |
| 56 |
}); |
| 57 |
}, [ |
| 58 |
highlighter, |
| 59 |
code, |
| 60 |
language, |
| 61 |
setAttributes, |
| 62 |
previousLanguage, |
| 63 |
attributes, |
| 64 |
]); |
| 65 |
|
| 66 |
useLayoutEffect(() => { |
| 67 |
if (!textAreaRef.current) return; |
| 68 |
if (!lineNumbers) { |
| 69 |
setAttributes({ lineNumbersWidth: undefined }); |
| 70 |
return; |
| 71 |
} |
| 72 |
// Get the last line (assumingly the widest) |
| 73 |
const lastLine = Array.from( |
| 74 |
textAreaRef?.current?.querySelectorAll('.line') ?? [], |
| 75 |
)?.at(-1) as HTMLElement; |
| 76 |
// Make sure there are no width constraints |
| 77 |
lastLine?.classList?.add('cbp-line-number-width-forced'); |
| 78 |
const lastLineWidth = lastLine?.getBoundingClientRect()?.width ?? 0; |
| 79 |
// Remove the width constraints |
| 80 |
lastLine?.classList?.remove('cbp-line-number-width-forced'); |
| 81 |
// Add .cbp-line-number-disabled to disable th eline number |
| 82 |
lastLine?.classList.add('cbp-line-number-disabled'); |
| 83 |
// Re calculate the width of the last line |
| 84 |
const newWidth = lastLine?.getBoundingClientRect()?.width ?? 0; |
| 85 |
// Remove the class |
| 86 |
lastLine?.classList.remove('cbp-line-number-disabled'); |
| 87 |
// Calculate the difference |
| 88 |
if (lastLineWidth - newWidth > 0) { |
| 89 |
setAttributes({ lineNumbersWidth: lastLineWidth - newWidth - 12 }); |
| 90 |
} |
| 91 |
}, [ |
| 92 |
lineNumbers, |
| 93 |
startingLineNumber, |
| 94 |
code, |
| 95 |
loading, |
| 96 |
error, |
| 97 |
textAreaRef, |
| 98 |
setAttributes, |
| 99 |
fontSize, |
| 100 |
fontFamily, |
| 101 |
lineHeight, |
| 102 |
]); |
| 103 |
|
| 104 |
if ((loading && code) || error) { |
| 105 |
return ( |
| 106 |
<div |
| 107 |
className="p-8 px-4 text-left" |
| 108 |
style={{ backgroundColor, color }}> |
| 109 |
{error?.message ?? ''} |
| 110 |
</div> |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
return ( |
| 115 |
<div ref={textAreaRef}> |
| 116 |
<Editor |
| 117 |
value={code} |
| 118 |
onValueChange={handleChange} |
| 119 |
padding={{ |
| 120 |
top: disablePadding ? 0 : 16, |
| 121 |
bottom: disablePadding || hasFooter ? 0 : 16, |
| 122 |
left: |
| 123 |
(disablePadding ? 0 : 16) + |
| 124 |
// If line numbers are disabled, just offset the 12px padding |
| 125 |
(lineNumbersWidth ?? -12) + |
| 126 |
12, |
| 127 |
right: disablePadding ? 0 : 16, |
| 128 |
}} |
| 129 |
style={{ backgroundColor, color }} |
| 130 |
// eslint-disable-next-line |
| 131 |
onKeyDown={(e: any) => |
| 132 |
e.key === 'Tab' && |
| 133 |
// Tab lock here. Pressing Escape will unlock. |
| 134 |
textAreaRef.current?.querySelector('textarea')?.focus() |
| 135 |
} |
| 136 |
highlight={(code: string) => |
| 137 |
highlighter |
| 138 |
?.codeToHtml(code, { |
| 139 |
lang: language ?? previousLanguage, |
| 140 |
}) |
| 141 |
?.replace(/<\/?[pre|code][^>]*>/g, '') |
| 142 |
} |
| 143 |
/> |
| 144 |
</div> |
| 145 |
); |
| 146 |
}; |
| 147 |
|