@dative-gpi/foundation-shared-components 0.0.206 → 0.0.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ <template>
2
+ <FSCol
3
+ v-if="$props.translationsExpanded"
4
+ >
5
+ <FSCol
6
+ gap="16px"
7
+ >
8
+ <FSRichTextField
9
+ :editable="false"
10
+ :label="$tr('ui.translateRichTextField.defaultValue', 'Default value')"
11
+ :modelValue="$props.modelValue"
12
+ v-bind="$attrs"
13
+ />
14
+ <FSRichTextField
15
+ v-for="(language, index) in languages"
16
+ :editable="$props.editable"
17
+ :key="index"
18
+ :modelValue="getTranslation(language.code)"
19
+ @update:modelValue="setTranslation(language.code, $event)"
20
+ v-bind="$attrs"
21
+ >
22
+ <template
23
+ #label
24
+ >
25
+ <FSRow
26
+ :wrap="false"
27
+ >
28
+ <FSSpan
29
+ class="fs-translate-field-label"
30
+ font="text-overline"
31
+ >
32
+ {{ $tr('ui.translateRichTextField.translateIn', 'Translate in {0}', language.label) }}
33
+ </FSSpan>
34
+ <FSIcon>{{ language.icon }}</FSIcon>
35
+ </FSRow>
36
+ </template>
37
+ </FSRichTextField>
38
+ </FSCol>
39
+ <FSRow
40
+ :wrap="false"
41
+ >
42
+ <FSButton
43
+ prependIcon="mdi-cancel"
44
+ :label="$tr('ui.translateRichTextField.cancelButton.label', 'Cancel')"
45
+ :fullWidth="true"
46
+ @click="onCancelTranslations"
47
+ />
48
+ <FSButton
49
+ v-if="$props.editable"
50
+ prependIcon="mdi-check"
51
+ color="primary"
52
+ :label="$tr('ui.translateRichTextField.validateButton.label', 'Validate translations')"
53
+ :fullWidth="true"
54
+ @click="onSubmitTranslations"
55
+ />
56
+ </FSRow>
57
+ </FSCol>
58
+ <FSRichTextField
59
+ v-else
60
+ :editable="$props.editable"
61
+ :modelValue="$props.modelValue"
62
+ @update:modelValue="$emit('update:modelValue', $event)"
63
+ v-bind="$attrs"
64
+ >
65
+ <template
66
+ #append-inner
67
+ >
68
+ <FSButton
69
+ prependIcon="mdi-translate"
70
+ color="primary"
71
+ :label="$tr('ui.translateRichTextField.translateButton.label', 'Manage translations')"
72
+ :fullWidth="true"
73
+ @click="() => $emit('update:translationsExpanded', true)"
74
+ />
75
+ </template>
76
+ </FSRichTextField>
77
+ </template>
78
+
79
+ <script lang="ts">
80
+ import { defineComponent, type PropType, ref } from 'vue';
81
+
82
+ import { useAppLanguages } from "@dative-gpi/foundation-shared-services/composables";
83
+
84
+ import { emptyLexicalState } from '../../utils';
85
+
86
+ import FSRichTextField from './FSRichTextField.vue';
87
+ import FSButton from '../FSButton.vue';
88
+ import FSIcon from '../FSIcon.vue';
89
+ import FSSpan from '../FSSpan.vue';
90
+ import FSCol from '../FSCol.vue';
91
+ import FSRow from '../FSRow.vue';
92
+
93
+ export default defineComponent({
94
+ name: 'FSTranslateRichTextField',
95
+ components: {
96
+ FSRichTextField,
97
+ FSButton,
98
+ FSIcon,
99
+ FSSpan,
100
+ FSCol,
101
+ FSRow
102
+ },
103
+ props: {
104
+ translationsExpanded: {
105
+ type: Boolean,
106
+ default: false,
107
+ },
108
+ editable: {
109
+ type: Boolean,
110
+ default: true,
111
+ },
112
+ modelValue: {
113
+ type: String as PropType<string | null>,
114
+ required: false,
115
+ default: null
116
+ },
117
+ translations: {
118
+ type: Array as PropType<{ languageCode: string; [key: string]: string }[]>,
119
+ required: false,
120
+ default: () => []
121
+ },
122
+ property: {
123
+ type: String as PropType<string>,
124
+ required: false,
125
+ default: "label"
126
+ }
127
+ },
128
+ emits: ['update:translationsExpanded', 'update:modelValue', 'update:translations'],
129
+ setup(props, { emit }) {
130
+ const { languages } = useAppLanguages();
131
+
132
+ const innerTranslations = ref(props.translations);
133
+
134
+ const getTranslation = (languageCode: string): string => {
135
+ if (!innerTranslations.value) {
136
+ return emptyLexicalState;
137
+ }
138
+ const translation = innerTranslations.value.find((t) => t.languageCode === languageCode);
139
+ if (!translation || !translation[props.property]) {
140
+ return emptyLexicalState;
141
+ }
142
+ return translation[props.property].toString();
143
+ };
144
+
145
+ const setTranslation = (languageCode: string, value: string): void => {
146
+ if (!innerTranslations.value) {
147
+ innerTranslations.value = [{
148
+ languageCode,
149
+ [props.property]: value
150
+ }]
151
+ return;
152
+ }
153
+ const translation = innerTranslations.value.find((t) => t.languageCode === languageCode);
154
+ if (translation) {
155
+ translation[props.property] = value;
156
+ }
157
+ else {
158
+ innerTranslations.value.push({
159
+ languageCode,
160
+ [props.property]: value
161
+ });
162
+ }
163
+ };
164
+
165
+ const onSubmitTranslations = (): void => {
166
+ if (props.editable) {
167
+ emit("update:translations", innerTranslations.value);
168
+ }
169
+ emit('update:translationsExpanded', false);
170
+ };
171
+
172
+ const onCancelTranslations = (): void => {
173
+ emit('update:translationsExpanded', false);
174
+ };
175
+
176
+ return {
177
+ languages,
178
+ onCancelTranslations,
179
+ onSubmitTranslations,
180
+ getTranslation,
181
+ setTranslation
182
+ };
183
+ }
184
+ });
185
+ </script>