markdown-translatable.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Field, { getValidationRules } from './_field';
  2. import { View } from 'app/design/view';
  3. import { lazyComponent } from 'app/lib/lazy-component';
  4. // Same heavy editor as the `markdown` field — code-split (see form-fields/_map.js).
  5. const MarkdownTextInput = lazyComponent(
  6. () => import('./editor-markdown').then((m) => ({ default: m.MarkdownTextInput })),
  7. { name: 'MarkdownTextInput' }
  8. );
  9. import {
  10. useTranslatableField,
  11. TranslatableFieldLayout,
  12. } from 'app/lib/form/form-fields-helpers';
  13. function LangMarkdownInput({
  14. fieldName,
  15. defaultValue,
  16. rules,
  17. autoFocus,
  18. disabled,
  19. placeholder,
  20. initialHeight,
  21. maxHeight,
  22. showToolbar,
  23. bg,
  24. active,
  25. }) {
  26. return (
  27. <View
  28. className={active ? 'w-full' : 'hidden'}
  29. aria-hidden={!active}
  30. >
  31. <MarkdownTextInput
  32. name={fieldName}
  33. value={defaultValue}
  34. placeholder={placeholder}
  35. autofocus={autoFocus}
  36. disabled={disabled}
  37. initialHeight={initialHeight}
  38. maxHeight={maxHeight}
  39. showToolbar={showToolbar}
  40. bg={bg}
  41. rules={rules}
  42. />
  43. </View>
  44. );
  45. }
  46. export default function FormFieldMarkdownTranslatable(props) {
  47. const {
  48. formContext,
  49. entries,
  50. errorFieldName,
  51. currentFieldName,
  52. setActiveFieldName,
  53. } = useTranslatableField(props);
  54. const rules = getValidationRules(props);
  55. const placeholder = props.use_caption_as_placeholder
  56. ? props.caption
  57. : props.placeholder;
  58. const disabled =
  59. props?.attrs?.readonly == 'readonly' ||
  60. props?.attrs?.disabled == 'disabled' ||
  61. !!props.disabled;
  62. const isCommentsForm = props.container_class === 'comments';
  63. const initialHeight = props.height || (isCommentsForm ? 48 : 120);
  64. if (!entries.length) return null;
  65. return (
  66. <Field
  67. {...props}
  68. error2={
  69. errorFieldName
  70. ? formContext.formState.errors[errorFieldName]
  71. : formContext.formState.errors[props.name]
  72. }
  73. >
  74. <TranslatableFieldLayout
  75. entries={entries}
  76. currentFieldName={currentFieldName}
  77. setActiveFieldName={setActiveFieldName}
  78. >
  79. {entries.map((entry) => (
  80. <LangMarkdownInput
  81. key={entry.fieldName}
  82. fieldName={entry.fieldName}
  83. defaultValue={entry.defaultValue}
  84. rules={rules}
  85. autoFocus={
  86. entry.fieldName === currentFieldName
  87. ? props.auto_focus || props.autofocus
  88. : false
  89. }
  90. disabled={disabled}
  91. placeholder={placeholder}
  92. initialHeight={initialHeight}
  93. maxHeight={props.maxHeight || 400}
  94. showToolbar={props.showToolbar !== false}
  95. bg={props.bg}
  96. active={entry.fieldName === currentFieldName}
  97. />
  98. ))}
  99. </TranslatableFieldLayout>
  100. </Field>
  101. );
  102. }