Skip to content

Commit

Permalink
feat(#693): initial druxt-blocks update for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 6, 2024
1 parent 2857806 commit 069780f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 89 deletions.
5 changes: 4 additions & 1 deletion packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
"require": "./dist/druxt-blocks.ssr.js",
"import": "./dist/druxt-blocks.esm.js"
},
"./components/*": "./dist/components/*"
"./components/*": "./dist/components/*",
"./nuxt/*": "./nuxt/*"
},
"main": "dist/druxt-blocks.ssr.js",
"module": "dist/druxt-blocks.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.12.4",
"axios": "0.27.2",
"drupal-jsonapi-params": "^2.2.0",
"druxt": "^0.22.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/blocks/src/components/DruxtBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default {
*
* @return {object}
*/
block: ({ resource }) => (resource || {}).data,
block: ({ resource }) => resource?.data,
},
watch: {
Expand Down Expand Up @@ -156,7 +156,7 @@ export default {
const type = 'block--block'
const query = new DrupalJsonApiParams()
const fields = ((this.$druxt.settings.blocks || {}).query || {}).fields
const fields = this.$druxt?.settings.blocks?.query?.fields
if (Array.isArray(fields)) {
query.addFields(type, [
...fields,
Expand Down Expand Up @@ -214,10 +214,10 @@ export default {
scopedSlots.default = () => {
let summary, description
if ((this.block || {}).attributes) {
if (this.block?.attributes) {
summary = `Missing Vue template for the '${this.block.attributes.drupal_internal__id}' block`
description = [
h('p', `Create a Druxt theme component to render the "${this.block.attributes.settings.label}" block.`),
h('p', `Create a Druxt theme component to render the "${this.block.attributes?.settings?.label}" block.`),
]
// Ensure an ID or UUID.
} else if (!this.id && !this.uuid) {
Expand All @@ -229,7 +229,7 @@ export default {
{ props: { summary } },
[
h('div', description),
!!this.component.options.length && h('DruxtDevelTemplate', { props: { options: this.component.options }}),
!!this.component.options?.length && h('DruxtDevelTemplate', { props: { options: this.component.options }}),
!!this.block && h('details', [h('summary', 'JSON:API resource'), h('pre', [h('code', [JSON.stringify(this.block, null, ' ')])])])
]
)
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/components/DruxtBlockRegion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default {
type,
query
})
this.blocks = collection.data
this.blocks = collection?.data
},
/**
Expand Down Expand Up @@ -207,7 +207,7 @@ export default {
slots(h) {
// Build scoped slots for each block.
const scopedSlots = {}
this.blocks.map((block) => {
this.blocks?.map((block) => {
scopedSlots[block.attributes.drupal_internal__id] = (attrs) => {
delete (attrs || {})['data-fetch-key']
return h('DruxtBlock', {
Expand All @@ -223,7 +223,7 @@ export default {
})
// Build default slot.
scopedSlots.default = (attrs) => h('div', this.blocks.map((block) =>
scopedSlots.default = (attrs) => h('div', this.blocks?.map((block) =>
this.isVisible(block)
? scopedSlots[block.attributes.drupal_internal__id](attrs)
: false
Expand Down
24 changes: 0 additions & 24 deletions packages/blocks/src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
import { DruxtBlocksNuxtModule } from './nuxtModule'

/**
* The Nuxt.js module function.
*
* Installs the module functionality in a Nuxt application.
*
* @type {Function}
* @exports default
* @name DruxtBlocksModule
* @see {@link /api/packages/blocks/nuxtModule|DruxtBlocksModule}
*
* @example <caption>nuxt.config.js</caption> @lang js
* module.exports = {
* modules: [
* 'druxt-blocks'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
*/
export default DruxtBlocksNuxtModule

/**
* The DruxtBlocksBlockMixin adds props and computed props to your DruxtBlock
* wrapper component.
Expand Down
73 changes: 73 additions & 0 deletions packages/blocks/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { defineNuxtModule, installModule } from '@nuxt/kit'
import { join } from 'path'
// import DruxtBlocksStorybook from './storybook'

/**
* The Nuxt.js module function.
*
* - Adds the Vue.js components to the Nuxt.js frontend.
*
* The module function should not be used directly, but rather installed via yout Nuxt.js configuration file.
*
* Options are set on the root level `druxt` Nuxt.js config object.
*
* @example @lang js
* // `nuxt.config.js`
* module.exports = {
* modules: [
* 'druxt-blocks'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
*
* @param {object} moduleOptions - Nuxt.js module options object.
*/
const DruxtBlocksNuxtModule = defineNuxtModule({
meta: {
name: 'druxt-blocks'
},
defaults: {
baseUrl: '',
endpoint: '/jsonapi',
},

async setup(moduleOptions, nuxt) {
// Prevent issue "FATAL: Cannot determine nuxt version! Is current
// instance passed?".
nuxt._version = nuxt._version || '2.'
// This is required to prevent "FATAL: nuxt.options._layers is not iterable"
// error when using `installModule()`.
nuxt.options._layers = nuxt.options._layers || []

// Set default options.
const options = {
baseUrl: moduleOptions.baseUrl,
...nuxt.options?.druxt || {},
blocks: {
query: {},
...nuxt.options?.druxt?.blocks,
...moduleOptions,
}
}

// Add Druxt module.
await installModule('druxt', options, nuxt)

// Register components directories.
nuxt.hook('components:dirs', dirs => {
dirs.push({ path: join(__dirname, '../dist/components') })
dirs.push({ path: join(__dirname, '../dist/components/blocks') })
})

// Nuxt Storybook.
// @TODO - @nuxt/kit and @nuxt/storybook aren't compatible.
// nuxt.hook('storybook:config', async ({ stories }) => {
// await DruxtBlocksStorybook.call(nuxt, { stories })
// })
}
})

export default DruxtBlocksNuxtModule

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addTemplate } from '@nuxt/kit'
import { resolve } from 'path'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { DruxtClient } from 'druxt'
Expand All @@ -8,7 +9,7 @@ const titleFn = (parts) =>
).join('/')

export default async function ({ stories }) {
const { addTemplate, options } = this
const { options } = this

// Setup DruxtClient instance.
const druxt = new DruxtClient(options.druxt.baseUrl, { ...options.druxt, proxy: { api: false } })
Expand Down
55 changes: 0 additions & 55 deletions packages/blocks/src/nuxtModule.js

This file was deleted.

0 comments on commit 069780f

Please sign in to comment.