| 1 |
import { useEffect, useRef } from '@wordpress/element'; |
| 2 |
import { applyFilters } from '@wordpress/hooks'; |
| 3 |
import Editor from 'react-simple-code-editor'; |
| 4 |
import { useTheme } from '../hooks/useTheme'; |
| 5 |
import { useGlobalStore } from '../state/global'; |
| 6 |
import { useLanguageStore } from '../state/language'; |
| 7 |
import { useThemeStore } from '../state/theme'; |
| 8 |
import { AttributesPropsAndSetter } from '../types'; |
| 9 |
|
| 10 |
export const Edit = ({ |
| 11 |
attributes, |
| 12 |
setAttributes, |
| 13 |
}: AttributesPropsAndSetter) => { |
| 14 |
const { |
| 15 |
language, |
| 16 |
theme, |
| 17 |
code = '', |
| 18 |
bgColor: backgroundColor, |
| 19 |
textColor: color, |
| 20 |
} = attributes; |
| 21 |
const textAreaRef = useRef<HTMLDivElement>(null); |
| 22 |
const handleChange = (code: string) => setAttributes({ code }); |
| 23 |
const { previousLanguage } = useLanguageStore(); |
| 24 |
const { previousSettings } = useGlobalStore(); |
| 25 |
const { previousTheme } = useThemeStore(); |
| 26 |
const { highlighter, error, loading } = useTheme({ |
| 27 |
theme, |
| 28 |
lang: language ?? previousLanguage, |
| 29 |
}); |
| 30 |
|
| 31 |
useEffect(() => { |
| 32 |
if (!attributes.copyButton) { |
| 33 |
setAttributes({ copyButton: previousSettings.copyButton }); |
| 34 |
} |
| 35 |
}, [previousSettings, attributes, setAttributes]); |
| 36 |
|
| 37 |
useEffect(() => { |
| 38 |
if (theme) return; |
| 39 |
setAttributes({ theme: previousTheme }); |
| 40 |
}, [previousTheme, theme, setAttributes]); |
| 41 |
|
| 42 |
useEffect(() => { |
| 43 |
if (!highlighter) return; |
| 44 |
setAttributes({ |
| 45 |
bgColor: highlighter.getBackgroundColor(), |
| 46 |
textColor: highlighter.getForegroundColor(), |
| 47 |
}); |
| 48 |
}, [theme, highlighter, setAttributes]); |
| 49 |
|
| 50 |
useEffect(() => { |
| 51 |
if (!highlighter) return; |
| 52 |
// applyFilters() |
| 53 |
setAttributes({ |
| 54 |
codeHTML: applyFilters( |
| 55 |
'blocks.codeBlockPro.codeHTML', |
| 56 |
highlighter.codeToHtml(code, { |
| 57 |
lang: language ?? previousLanguage, |
| 58 |
}), |
| 59 |
attributes, |
| 60 |
) as string, |
| 61 |
}); |
| 62 |
}, [ |
| 63 |
highlighter, |
| 64 |
code, |
| 65 |
language, |
| 66 |
setAttributes, |
| 67 |
previousLanguage, |
| 68 |
attributes, |
| 69 |
]); |
| 70 |
|
| 71 |
if ((loading && code) || error) { |
| 72 |
return ( |
| 73 |
<div |
| 74 |
className="p-8 px-6 text-left" |
| 75 |
style={{ backgroundColor, color }}> |
| 76 |
{error?.message ?? ''} |
| 77 |
</div> |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
return ( |
| 82 |
<div ref={textAreaRef}> |
| 83 |
<Editor |
| 84 |
value={code} |
| 85 |
onValueChange={handleChange} |
| 86 |
padding={24} |
| 87 |
style={{ backgroundColor, color }} |
| 88 |
// eslint-disable-next-line |
| 89 |
onKeyDown={(e: any) => |
| 90 |
e.key === 'Tab' && |
| 91 |
// Tab lock here. Pressing Escape will unlock. |
| 92 |
textAreaRef.current?.querySelector('textarea')?.focus() |
| 93 |
} |
| 94 |
highlight={(code: string) => |
| 95 |
highlighter |
| 96 |
?.codeToHtml(code, { |
| 97 |
lang: language ?? previousLanguage, |
| 98 |
}) |
| 99 |
?.replace(/<\/?[pre|code][^>]*>/g, '') |
| 100 |
} |
| 101 |
/> |
| 102 |
</div> |
| 103 |
); |
| 104 |
}; |
| 105 |
|