UNPKG

@tanglemedia/directus-collection-flex-editor

Version:

Creates the flex table and some basic related nodes like image, resource, embed, and section

86 lines (75 loc) 2.32 kB
const FLEX_CONTENT_FIELD = "tngl_flx_cntnt"; let flexCollection = process.env.FLEX_COLLECTION; if (!flexCollection) { flexCollection = []; } else { flexCollection = flexCollection.split(","); } // add flex content to some basic tables aside from the ones specified in the FLEX_COLLECTION let basicFlexCollection = "tngl_article,tngl_event,tngl_job,tngl_people,tngl_page"; basicFlexCollection = basicFlexCollection.split(","); flexCollection = [...flexCollection, ...basicFlexCollection]; flexCollection = [...new Set(flexCollection)]; export async function up(knex) { // check if the tables in the FLEX_COLLECTION exist let flexCollectionExistsPromises = flexCollection.map((collection) => { return knex.schema.hasTable(collection); }); flexCollectionExistsPromises = await Promise.all( flexCollectionExistsPromises ); const finalFlexCollection = flexCollection.filter( (collection, index) => flexCollectionExistsPromises[index] ); // check if the tables in the FLEX_COLLECTION has the tngl_flex_content_id field let flexCollectionFieldExistsPromises = finalFlexCollection.map( (collection) => { return knex.schema.hasColumn(collection, FLEX_CONTENT_FIELD); } ); flexCollectionFieldExistsPromises = await Promise.all( flexCollectionFieldExistsPromises ); // add tngl_flex_content editor to the collections in the FLEX_COLLECTION let flexCollectionRegFieldPromises = finalFlexCollection.map( (collection, index) => { if (flexCollectionFieldExistsPromises[index]) { return knex("directus_fields").insert([ { collection: collection, field: FLEX_CONTENT_FIELD, special: "m2o", interface: "inline-form-m2o", options: null, display: null, display_options: null, readonly: false, hidden: false, sort: 29, width: "full", translations: JSON.stringify([ { language: "en-US", translation: " ", }, { language: "en-CA", translation: " ", }, ]), note: null, conditions: null, required: false, group: null, validation: null, validation_message: null, }, ]); } } ); flexCollectionRegFieldPromises = await Promise.all( flexCollectionRegFieldPromises ); } export async function down(knex) {}