Skip to content

Commit

Permalink
feat: register components with plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmkruger committed Apr 22, 2022
1 parent 53b97f9 commit 70cf27e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
62 changes: 62 additions & 0 deletions lib/components/SplitContent/definition.js
@@ -0,0 +1,62 @@
const { values, concat } = require('lodash');

const extractComponentImages = ({ content, definitionsArray }) => {
return concat(
...content.map(item => {
const contentDef = definitionsArray.find(def => def.componentType === item.componentType);

if (contentDef && contentDef.extractImages) return contentDef.extractImages(item);

return [];
})
);
};

const extractComponentLinks = ({ content, definitionsArray }) => {
return concat(
...content.map(item => {
const contentDef = definitionsArray.find(def => def.componentType === item.componentType);

if (contentDef && contentDef.extractLinks) return contentDef.extractLinks(item);

return [];
})
);
};

export default (definitions, prefix) => ({
key: `${prefix}SplitContent`,
name: 'Split Content',
componentType: 'SplitContent',
hideSpacingInContent: true,
init: ({ $set }, content = {}) => {
if (!content.width) $set(content, 'width', 6);
if (!content.gap) $set(content, 'gap', 1);
if (!content.left) $set(content, 'left', []);
if (!content.right) $set(content, 'right', []);
if (!content.fullWidth) $set(content, 'fullWidth', false);
if (!content.reverse) $set(content, 'reverse', { mobile: false, desktop: false });

return content;
},
extractImages: content => {
const definitionsArray = values(definitions);

const res = [
...extractComponentImages({ content: content.left, definitionsArray }),
...extractComponentImages({ content: content.right, definitionsArray }),
];

return res;
},
extractLinks: content => {
const definitionsArray = values(definitions);

const res = [
...extractComponentLinks({ content: content.left, definitionsArray }),
...extractComponentLinks({ content: content.right, definitionsArray }),
];

return res;
},
});
6 changes: 6 additions & 0 deletions lib/components/definitions.js
@@ -0,0 +1,6 @@
import SplitContent from './SplitContent/definition';

export default function (definitions, prefix = '') {
const splitContent = SplitContent(definitions, prefix);
return [splitContent];
}
11 changes: 10 additions & 1 deletion lib/module.js
Expand Up @@ -20,6 +20,12 @@ module.exports = function (moduleOptions) {
options,
});

this.addTemplate({
src: resolve(__dirname, 'components/SplitContent.vue'),
fileName: join(filesDir, 'components/SplitContent.vue'),
options,
});

this.addTemplate({
src: resolve(__dirname, 'components/SplitContent/Editor.vue'),
fileName: join(filesDir, 'components/SplitContent/Editor.vue'),
Expand All @@ -39,5 +45,8 @@ module.exports = function (moduleOptions) {
});
};

module.exports.install = (Vue, options) => require('./register').default(Vue, options);
// module.exports.install = (Vue, options) => {
// console.log('🚀 ~ file: module.js ~ line 46 ~ test');
// return require('./register').default(Vue, options);
// };
module.exports.meta = require('../package.json');
6 changes: 6 additions & 0 deletions lib/plugin.js
@@ -1,4 +1,10 @@
import Vue from 'vue';

import EditorComponent from './components/SplitContent/Editor';
import SplitContent from './components/SplitContent';
console.log('🚀 ~ file: plugin.js ~ line 5 ~ SplitContent', SplitContent);

Vue.component(`SplitContent`, SplitContent);

export default async function ({ $whppt }) {
const splitContent = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"lodash": "^4.17.20",
"vue": "^2.6.14",
"vuex": "^3.5.1"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -10293,6 +10293,11 @@ vue@^2.6.12:
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.12.tgz#f5ebd4fa6bd2869403e29a896aed4904456c9123"
integrity sha512-uhmLFETqPPNyuLLbsKz6ioJ4q7AZHzD8ZVFNATNyICSZouqP2Sz0rotWQC8UNBF6VGSCs5abnKJoStA6JbCbfg==

vue@^2.6.14:
version "2.6.14"
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.14.tgz#e51aa5250250d569a3fbad3a8a5a687d6036e235"
integrity sha512-x2284lgYvjOMj3Za7kqzRcUSxBboHqtgRE2zlos1qWaOye5yUmHn42LB1250NJBLRwEcdrB0JRwyPTEPhfQjiQ==

vuex@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.5.1.tgz#f1b8dcea649bc25254cf4f4358081dbf5da18b3d"
Expand Down

0 comments on commit 70cf27e

Please sign in to comment.