-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
/
index.js
169 lines (153 loc) · 5.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from 'react';
import get from 'lodash/get';
import * as yup from 'yup';
import pluginPkg from '../../package.json';
import pluginLogo from './assets/images/logo.svg';
import CheckboxConfirmation from './components/CheckboxConfirmation';
import CMEditViewInjectedComponents from './components/CMEditViewInjectedComponents';
import Initializer from './containers/Initializer';
import SettingsPage from './containers/SettingsPage';
import LocalePicker from './components/LocalePicker';
import middlewares from './middlewares';
import pluginPermissions from './permissions';
import pluginId from './pluginId';
import trads from './translations';
import { getTrad } from './utils';
import mutateCTBContentTypeSchema from './utils/mutateCTBContentTypeSchema';
import LOCALIZED_FIELDS from './utils/localizedFields';
import i18nReducers from './hooks/reducers';
import DeleteModalAdditionalInfos from './components/DeleteModalAdditionalInfos';
export default strapi => {
const pluginDescription = pluginPkg.strapi.description || pluginPkg.description;
middlewares.forEach(middleware => {
strapi.middlewares.add(middleware);
});
const plugin = {
description: pluginDescription,
icon: pluginPkg.strapi.icon,
id: pluginId,
isReady: false,
isRequired: pluginPkg.strapi.required || false,
mainComponent: null,
name: pluginPkg.strapi.name,
pluginLogo,
preventComponentRendering: false,
settings: {
global: {
links: [
{
title: {
id: getTrad('plugin.name'),
defaultMessage: 'Internationalization',
},
name: 'internationalization',
to: `${strapi.settingsBaseURL}/internationalization`,
Component: () => <SettingsPage />,
permissions: pluginPermissions.accessMain,
},
],
},
},
trads,
reducers: i18nReducers,
boot(app) {
const ctbPlugin = app.getPlugin('content-type-builder');
const cmPlugin = app.getPlugin('content-manager');
if (cmPlugin) {
cmPlugin.injectComponent('editView', 'informations', {
name: 'i18n-locale-filter-edit-view',
Component: CMEditViewInjectedComponents,
});
cmPlugin.injectComponent('listView', 'actions', {
name: 'i18n-locale-filter',
Component: LocalePicker,
});
cmPlugin.injectComponent('listView', 'deleteModalAdditionalInfos', {
name: 'i18n-delete-bullets-in-modal',
Component: DeleteModalAdditionalInfos,
});
}
if (ctbPlugin) {
const ctbFormsAPI = ctbPlugin.apis.forms;
ctbFormsAPI.addContentTypeSchemaMutation(mutateCTBContentTypeSchema);
ctbFormsAPI.components.add({ id: 'checkboxConfirmation', component: CheckboxConfirmation });
ctbFormsAPI.extendContentType({
validator: () => ({
i18n: yup.object().shape({
localized: yup.bool(),
}),
}),
form: {
advanced() {
return [
[
{
name: 'pluginOptions.i18n.localized',
description: {
id: getTrad('plugin.schema.i18n.localized.description-content-type'),
},
type: 'checkboxConfirmation',
label: { id: getTrad('plugin.schema.i18n.localized.label-content-type') },
},
],
];
},
},
});
ctbFormsAPI.extendFields(LOCALIZED_FIELDS, {
validator: args => ({
i18n: yup.object().shape({
localized: yup.bool().test({
name: 'ensure-unique-localization',
message: getTrad('plugin.schema.i18n.ensure-unique-localization'),
test(value) {
if (value === undefined || value) {
return true;
}
const unique = get(args, ['3', 'modifiedData', 'unique'], null);
// Unique fields must be localized
if (unique && !value) {
return false;
}
return true;
},
}),
}),
}),
form: {
advanced({ contentTypeSchema, forTarget, type, step }) {
if (forTarget !== 'contentType') {
return [];
}
const hasI18nEnabled = get(
contentTypeSchema,
['schema', 'pluginOptions', 'i18n', 'localized'],
false
);
if (!hasI18nEnabled) {
return [];
}
if (type === 'component' && step === '1') {
return [];
}
return [
[
{
name: 'pluginOptions.i18n.localized',
description: {
id: getTrad('plugin.schema.i18n.localized.description-field'),
},
type: 'checkbox',
label: { id: getTrad('plugin.schema.i18n.localized.label-field') },
},
],
];
},
},
});
}
},
initializer: Initializer,
};
return strapi.registerPlugin(plugin);
};