From 9be21fac85ddf5a546f3734a6b714756c7b867cd Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Tue, 17 Dec 2019 10:49:33 -0500 Subject: [PATCH] work in progress on flexible intl fields --- demo/collections/Page.js | 5 ++-- demo/globals/Header.js | 5 ++-- package.json | 2 +- src/localization/localization.plugin.js | 25 +++++++++++------- src/mongoose/schema/fieldToSchemaMap.js | 34 +++++++++++++++++++------ src/mongoose/schema/schemaLoader.js | 9 ++++--- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/demo/collections/Page.js b/demo/collections/Page.js index d847e3b725..f3c6119573 100644 --- a/demo/collections/Page.js +++ b/demo/collections/Page.js @@ -77,8 +77,9 @@ module.exports = { name: 'flexible', label: 'Flexible Content Blocks', type: 'flexible', - blocks: ['group', 'quote', 'cta'], - hasMany: true, + blocks: ['quote', 'cta'], + localized: true, + hasMany: false, }, { label: 'Meta Information', diff --git a/demo/globals/Header.js b/demo/globals/Header.js index 161a3ecbbb..726fbac78c 100644 --- a/demo/globals/Header.js +++ b/demo/globals/Header.js @@ -31,11 +31,12 @@ module.exports = { required: true }, { - name: 'flexible', + name: 'flexibleGlobal', label: 'Global Flexible Block', type: 'flexible', blocks: ['quote', 'cta'], - hasMany: true, + localized: true, + hasMany: false, }, { // TODO: this is some proof of concept parts that are not done. diff --git a/package.json b/package.json index 48d6f0d52a..e65c3eb9ef 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "react-redux": "^5.0.7", "react-router-dom": "^4.3.1", "redux": "^4.0.4", - "sharp": "^0.21.1", + "sharp": "^0.22.1", "superagent": "^3.8.3", "superagent-promise": "^1.1.0", "universal-cookie": "^3.1.0", diff --git a/src/localization/localization.plugin.js b/src/localization/localization.plugin.js index 457f4a2a80..f98f8a32de 100644 --- a/src/localization/localization.plugin.js +++ b/src/localization/localization.plugin.js @@ -19,18 +19,23 @@ export default function localizationPlugin(schema, options) { } schema.eachPath((path, schemaType) => { - + // TODO: We need a way to set if this should recurse or not. What is happening on global's flexible field is that it tries to save flexibleGlobal.en.en on post. + let recurse = true; + if (path.endsWith('global.en')) { + recurse = false; + } if (schemaType.schema) { // propagate plugin initialization for sub-documents schemas schemaType.schema.plugin(localizationPlugin, pluginOptions); + if (!recurse) + return; + } + + if (!schemaType.options.localized && !(schemaType.schema && schemaType.schema.options.localized)) { return; } - if (!schemaType.options.localized) { - return; - } - - if (!(schemaType instanceof mongoose.Schema.Types.String)) { - throw new mongoose.Error('localization can only be used on type String'); + if (!((schemaType instanceof mongoose.Schema.Types.String) || (schemaType instanceof mongoose.Schema.Types.Embedded))) { + throw new mongoose.Error('localization can only be used on type String or Embedded'); } let pathArray = path.split('.'), @@ -89,11 +94,11 @@ export default function localizationPlugin(schema, options) { let locales = this.schema.options.localization.locales; locales.forEach(locale => { if (!value[locale]) { + // this.set(`${path}.${locale}`, value); return; } - this.set(path + '.' + locale, value[locale]); + this.set(`${path}.${locale}`, value[locale]); }, this); - return; } // embedded and sub-documents will use locale methods from the top level document @@ -107,6 +112,8 @@ export default function localizationPlugin(schema, options) { // delete schemaType.options.localized; // This was removed to allow viewing inside query parser let localizedObject = {}; + // TODO: setting equal to object is good for hasMany: false, but breaking for hasMany: true; + // console.log(path, schemaType.options); localizedObject[key] = {}; pluginOptions.locales.forEach(function (locale) { let localeOptions = Object.assign({}, schemaType.options); diff --git a/src/mongoose/schema/fieldToSchemaMap.js b/src/mongoose/schema/fieldToSchemaMap.js index 4c14e1bc9c..9bcf7b0dc1 100644 --- a/src/mongoose/schema/fieldToSchemaMap.js +++ b/src/mongoose/schema/fieldToSchemaMap.js @@ -1,4 +1,6 @@ import mongoose from 'mongoose'; +import localizationPlugin from '../../localization/localization.plugin'; +import autopopulate from '../autopopulate.plugin'; const formatBaseSchema = field => { return { @@ -11,13 +13,13 @@ const formatBaseSchema = field => { const fieldToSchemaMap = { number: field => { - return { ...formatBaseSchema(field), type: Number }; + return {...formatBaseSchema(field), type: Number}; }, input: field => { - return { ...formatBaseSchema(field), type: String }; + return {...formatBaseSchema(field), type: String}; }, textarea: field => { - return { ...formatBaseSchema(field), type: String }; + return {...formatBaseSchema(field), type: String}; }, date: field => { return { @@ -50,17 +52,33 @@ const fieldToSchemaMap = { enum: field.enum, }; }, - flexible: (field, path) => { - const schema = { + flexible: (field, options = {}) => { + const flexible = { value: { type: mongoose.Types.ObjectId, autopopulate: true, - refPath: `${path ? (path + '.') : ''}${field.name}.blockType`, + refPath: `${options.path ? (options.path + '.') : ''}${field.name}.blockType`, }, - blockType: { type: String, enum: field.blocks }, + blockType: {type: String, enum: field.blocks}, _id: false, }; - return field.hasMany !== false ? [schema] : schema; + + const schema = new mongoose.Schema( + field.hasMany !== false ? [flexible] : flexible, + { + hasMany: field.hasMany, + localized: field.localized || false, + } + ); +if (field.name === 'flexibleGlobal') { + console.log(field.hasMany !== false ? [flexible] : flexible); +} + if (field.localized) { + schema.plugin(localizationPlugin, options.localization); + } + schema.plugin(autopopulate); + + return schema; }, }; diff --git a/src/mongoose/schema/schemaLoader.js b/src/mongoose/schema/schemaLoader.js index 00fe8877e4..bbea814a03 100644 --- a/src/mongoose/schema/schemaLoader.js +++ b/src/mongoose/schema/schemaLoader.js @@ -49,6 +49,7 @@ class SchemaLoader { const Schema = new mongoose.Schema(fields) .plugin(paginate) + .plugin(localizationPlugin, config.localization) .plugin(buildQueryPlugin) .plugin(autopopulate); @@ -96,11 +97,11 @@ class SchemaLoader { collectionConfig.fields.forEach(field => { const fieldSchema = fieldToSchemaMap[field.type]; - if (fieldSchema) fields[field.name] = fieldSchema(field); + if (fieldSchema) fields[field.name] = fieldSchema(field, {localization: config.localization}); }); - const Schema = new mongoose.Schema(fields, { timestamps: collectionConfig.timestamps }); - Schema.plugin(paginate) + const Schema = new mongoose.Schema(fields, { timestamps: collectionConfig.timestamps }) + .plugin(paginate) .plugin(buildQueryPlugin) .plugin(localizationPlugin, config.localization) .plugin(autopopulate); @@ -128,7 +129,7 @@ class SchemaLoader { globalConfig.fields.forEach(field => { const fieldSchema = fieldToSchemaMap[field.type]; - if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, globalConfig.slug); + if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, {path: globalConfig.slug, localization: config.localization}); }); globalSchemaGroups[globalConfig.slug] = new mongoose.Schema(globalFields[globalConfig.slug], { _id: false }) .plugin(localizationPlugin, config.localization)