Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 83bedaa

Browse files
committed
Initial commit
0 parents  commit 83bedaa

10 files changed

Lines changed: 296 additions & 0 deletions

File tree

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root=true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
max_line_length = 100
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.npmrc

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Andi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Pattern Wrapper Plugin for Pattern Lab Node
2+
3+
This plugin allows users to add a wrapper div with css class(es) around a pattern when shown in the
4+
single preview.
5+
If it gets included in another pattern, the wrapper is not added.
6+
7+
This comes in handy if you, for example, use theming classes to visualize different backgrounds, colors etc.
8+
9+
## Configuration
10+
11+
After the installation, you will see the config in your `patternlab-config.json`:
12+
13+
```
14+
"plugins": {
15+
"patternlab-plugin-pattern-wrap": {
16+
"enabled": true,
17+
"initialized": false,
18+
"options": {
19+
"wrapClassKey": [""]
20+
}
21+
}
22+
}
23+
```
24+
25+
If you don't see this config object, add the plugin via the command:
26+
27+
```
28+
patternlab install --plugins patternlab-plugin-pattern-wrap
29+
```
30+
31+
In the `wrapClassKey` array you can add the data keys which should be used to get the class names.
32+
33+
## How does it work?
34+
35+
The plugin will look for any "data key" added to the `wrapClassKey` array and then will add that
36+
entry to the wrapper element.
37+
38+
Data key can be set inside the Markdown or JSON file of any pattern.
39+
40+
**Example Config**
41+
42+
```
43+
"wrapClassKey": ["theme-class"]
44+
```
45+
46+
### Markdown
47+
48+
Usage https://patternlab.io/docs/documenting-patterns/
49+
50+
_my-pattern.md_
51+
52+
```
53+
---
54+
theme-class: my-theme-class
55+
---
56+
```
57+
58+
Will create `<div class="pl-pattern-wrapper-element my-theme-class"></div>` around the rendered
59+
pattern.
60+
61+
### JSON
62+
63+
Usage https://patternlab.io/docs/creating-pattern-specific-values/
64+
65+
_my-pattern.json_
66+
67+
```
68+
{
69+
"theme-class": "my-other-theme-class"
70+
}
71+
```
72+
73+
Will create `<div class="pl-pattern-wrapper-element my-other-theme-class"></div>` around the
74+
rendered pattern.
75+
76+
#### Pseudo-Patterns
77+
78+
This will work with pseudo-patterns too (Usage https://patternlab.io/docs/using-pseudo-patterns/)
79+
80+
_my-pattern~variant.json_
81+
82+
```
83+
{
84+
"theme-class": "my-variant-theme-class"
85+
}
86+
```
87+
88+
Will create `<div class="pl-pattern-wrapper-element my-variant-theme-class"></div>` around the
89+
rendered pattern.
90+
91+
### Multiple entries in "wrapClassKey"
92+
93+
Will result in multiple classes in the wrapper div.
94+
95+
**Example Config**
96+
97+
```
98+
"wrapClassKey": ["theme-class", "other-class"]
99+
```
100+
101+
_my-pattern.json_
102+
103+
```
104+
{
105+
"theme-class": "theme-class",
106+
"other-class": "some-other-class"
107+
}
108+
```
109+
110+
_Result_
111+
112+
```
113+
<div class="pl-pattern-wrapper-element theme-class some-other-class"></div>
114+
```

config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"wrapClassKey": [""]
3+
}

helpers.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* the plugin name
3+
* @return {string}
4+
*/
5+
function getPluginName() {
6+
return 'patternlab-plugin-pattern-wrap';
7+
}
8+
9+
/**
10+
* get plugin options from patternlab-config
11+
* @param patternlab
12+
* @return {*}
13+
*/
14+
function getPluginOptions(patternlab) {
15+
return patternlab.config.plugins[getPluginName()].options;
16+
}
17+
18+
/**
19+
* get the classes from pattern markdown or json configured by the plugin options
20+
* @param patternlab
21+
* @param pattern
22+
* @return {string}
23+
*/
24+
function getWrapClasses(patternlab, pattern) {
25+
const config = getPluginOptions(patternlab);
26+
const { wrapClassKey } = config;
27+
if (!wrapClassKey || wrapClassKey.length === 0) {
28+
return '';
29+
}
30+
31+
const classes = [];
32+
wrapClassKey.forEach((key) => {
33+
const { allMarkdown, jsonFileData } = pattern;
34+
35+
if (allMarkdown && allMarkdown[key]) {
36+
classes.push(allMarkdown[key]);
37+
}
38+
39+
if (jsonFileData && jsonFileData[key]) {
40+
classes.push(jsonFileData[key]);
41+
}
42+
});
43+
44+
return classes.join(' ');
45+
}
46+
47+
48+
module.exports = {
49+
getPluginName,
50+
getWrapClasses,
51+
};

index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { getPluginName, getWrapClasses } = require('./helpers');
2+
3+
const pluginName = getPluginName();
4+
5+
/**
6+
* PL event handler
7+
* @param patternlab
8+
* @param pattern
9+
*/
10+
function onPatternIterate([patternlab, pattern]) {
11+
const classes = getWrapClasses(patternlab, pattern);
12+
if (classes.length !== 0) {
13+
pattern.patternPartialCode = `<div class="pl-pattern-wrapper-element ${classes}">${pattern.patternPartialCode}</div>`;
14+
}
15+
}
16+
17+
/**
18+
* Define what hooks you wish to invoke to here
19+
* For a full list of hooks - check out https://github.com/pattern-lab/patternlab-node/blob/master/packages/core/docs/events.md
20+
* @param patternlab - global data store which has the handle to hooks
21+
*/
22+
function registerEvents(patternlab) {
23+
patternlab.events.on('patternlab-pattern-write-begin', onPatternIterate);
24+
}
25+
26+
/**
27+
* A single place to define the frontend configuration
28+
* This configuration is outputted to the frontend explicitly as well as included in the plugins object.
29+
*/
30+
function getPluginFrontendConfig() {
31+
return {
32+
name: pluginName,
33+
templates: [],
34+
stylesheets: [],
35+
javascripts: [],
36+
onready: '',
37+
callback: '',
38+
};
39+
}
40+
41+
/**
42+
* The entry point for the plugin. You should not have to alter this code much under many circumstances.
43+
* Instead, alter getPluginFrontendConfig() and registerEvents() methods
44+
*/
45+
function pluginInit(patternlab) {
46+
47+
if (!patternlab) {
48+
console.error('patternlab object not provided to plugin-init');
49+
process.exit(1);
50+
}
51+
52+
//setup listeners if not already active. we also enable and set the plugin as initialized
53+
if (!patternlab.config.plugins) {
54+
patternlab.config.plugins = {};
55+
}
56+
57+
//attempt to only register events once
58+
if (patternlab.config.plugins[pluginName] !== undefined &&
59+
patternlab.config.plugins[pluginName].enabled &&
60+
!patternlab.config.plugins[pluginName].initialized) {
61+
62+
//register hooks
63+
registerEvents(patternlab);
64+
65+
//set the plugin initialized flag to true to indicate it is installed and ready
66+
patternlab.config.plugins[pluginName].initialized = true;
67+
}
68+
69+
}
70+
71+
module.exports = pluginInit;

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@hadl/patternlab-plugin-pattern-wrap",
3+
"version": "1.0.0",
4+
"description": "Pattern Wrapper Plugin for Pattern Lab Node",
5+
"main": "index.js",
6+
"scripts": {
7+
"postinstall": "node ./postinstall.js"
8+
},
9+
"author": "Andreas Bibow",
10+
"license": "MIT",
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/hadl/patternlab-plugin-pattern-wrap.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/hadl/patternlab-plugin-pattern-wrap/issues"
17+
},
18+
"homepage": "https://github.com/hadl/patternlab-plugin-pattern-wrap#readme"
19+
}

postinstall.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
console.log(`Pattern Lab Node Plugin - "patternlab-plugin-pattern-wrap" installed.
2+
Use the CLI to install this: patternlab install --plugins patternlab-plugin-pattern-wrap
3+
Configure or disable this plugin inside your patternlab-config.json file.
4+
`);

0 commit comments

Comments
 (0)