@storepress/create-plugin-html 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,24 +10,10 @@ export function Plugin (element, options) {
10
10
  const PRIVATE = {}
11
11
 
12
12
  // Collecting settings from html attribute
13
- const ATTRIBUTE = 'plugin-settings' //
13
+ const ATTRIBUTE = 'plugin-settings' // html data-plugin-settings
14
14
 
15
- const CLASSES = {}
16
-
17
- // Do what you need and return expose fn.
18
- const init = () => {
19
- this.$element = element
20
- this.settings = {
21
- ...DEFAULTS, ...options, ...getOptionsFromAttribute(this.$element, ATTRIBUTE), ...PRIVATE,
22
- }
23
-
24
- initial()
25
-
26
- addClasses()
27
-
28
- addEvents()
29
-
30
- return expose()
15
+ const CLASSES = {
16
+ NEW: 'new-class',
31
17
  }
32
18
 
33
19
  const noop = () => () => {}
@@ -41,27 +27,31 @@ export function Plugin (element, options) {
41
27
  }
42
28
 
43
29
  const initial = () => {
44
- console.log(this.settings)
30
+ console.log(this.settings)
45
31
  triggerEvent(this.$element, 'afterInit', {})
46
32
  }
47
33
 
48
- const cssVariableIsTrue = (string) => {
49
- return string === 'true' || string === '1' || string === 'yes'
50
- }
51
34
  const addClasses = () => {
52
-
35
+ this.$element.classList.add(CLASSES.NEW)
53
36
  }
54
37
 
55
- const removeClasses = () => {
38
+ const addEvents = () => {
56
39
 
57
- }
40
+ this.$element.addEventListener('click', (e) => {
58
41
 
59
- const addEvents = () => {
42
+ console.log(e.target.getAttribute('class'))
43
+
44
+ }, { signal: this.signal, passive: true })
60
45
 
61
46
  }
62
47
 
63
48
  const removeEvents = () => {
64
49
 
50
+ this.controller.abort('Reset')
51
+ }
52
+
53
+ const removeClasses = () => {
54
+ this.$element.classList.remove(CLASSES.NEW)
65
55
  }
66
56
 
67
57
  const reset = () => {
@@ -69,10 +59,29 @@ export function Plugin (element, options) {
69
59
  removeEvents()
70
60
  }
71
61
 
72
- // Expose to public.
62
+ // Expose reset to public.
73
63
  const expose = () => ({
74
- removeEvents, reset,
64
+ reset,
75
65
  })
76
66
 
67
+ // Do what you need and return expose fn.
68
+ const init = () => {
69
+ this.$element = element
70
+ this.settings = {
71
+ ...DEFAULTS, ...options, ...getOptionsFromAttribute(this.$element, ATTRIBUTE), ...PRIVATE,
72
+ }
73
+
74
+ this.controller = new AbortController()
75
+ this.signal = this.controller.signal
76
+
77
+ initial()
78
+
79
+ addClasses()
80
+
81
+ addEvents()
82
+
83
+ return expose()
84
+ }
85
+
77
86
  return init()
78
87
  }
@@ -1,64 +1,89 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { createPluginInstance, triggerEvent } from '@storepress/utils'
4
+ import { createPluginInstance, triggerEvent, getPluginInstance } from '@storepress/utils'
5
5
 
6
6
  /**
7
7
  * Internal dependencies
8
8
  */
9
- import { Plugin } from './Plugin'
9
+ import { Plugin as Instance } from './Plugin'
10
10
 
11
- export default function StorePressPlugin () {
12
- const StorePressPlugin = {
13
- getInstance (element) {
14
- return createPluginInstance(element, false, Plugin)
11
+ export default function {{pascaleSlug}}Plugin () {
12
+ const Plugin = {
13
+ createInstance (element, options) {
14
+ return createPluginInstance(element, options, Instance)
15
15
  },
16
16
 
17
- initWith ($selector = '[data-plugin-settings]') {
18
- for (const { element, unregister } of this.getInstance($selector)) {
19
- element.addEventListener('destroy', unregister)
20
- }
17
+ getInstance (element) {
18
+ return getPluginInstance(element)
21
19
  },
22
20
 
23
- init () {
24
- for (const { element, unregister } of this.getInstance('[data-plugin-settings]')) {
25
- element.addEventListener('destroy', unregister)
21
+ init ($selector, options) {
22
+ for (const { element, reset } of this.createInstance($selector, options)) {
23
+ element.addEventListener('destroy', reset)
26
24
  }
27
25
  },
28
26
 
29
- destroy () {
30
- for (const { destroy } of this.getInstance('[data-plugin-settings]')) {
27
+ destroy ($selector) {
28
+ for (const { destroy } of this.getInstance($selector)) {
31
29
  destroy()
32
30
  }
33
31
  },
34
32
 
35
- destroyWith ($selector = '[data-plugin-settings]') {
36
- for (const { destroy } of this.getInstance($selector)) {
37
- destroy()
38
- }
33
+ reload ($selector, options) {
34
+ this.destroy($selector)
35
+ this.init($selector, options)
39
36
  },
40
37
  }
41
38
 
42
- document.addEventListener('storepress_plugin_init', () => {
43
- StorePressPlugin.init()
44
- })
39
+ document.addEventListener('storepress_plugin_init', (event) => {
45
40
 
46
- document.addEventListener('storepress_plugin_destroy', () => {
47
- StorePressPlugin.destroy()
48
- })
41
+ const defaultSettings = {}
42
+ const settings = { ...defaultSettings, ...event.detail?.settings }
43
+ const element = event.detail?.element
49
44
 
50
- document.addEventListener('storepress_plugin_init_with', (event) => {
51
- const selector = event.detail?.element
52
- StorePressPlugin.initWith(selector)
53
- })
45
+ if (Array.isArray(element)) {
46
+ for (const el of element) {
47
+ Plugin.init(el, settings)
48
+ }
49
+ } else {
50
+ Plugin.init(element, settings)
51
+ }
52
+ }, { passive: true })
54
53
 
55
- document.addEventListener('storepress_plugin_destroy_with', (event) => {
56
- const selector = event.detail?.element
57
- StorePressPlugin.destroyWith(selector)
58
- })
54
+ document.addEventListener('storepress_plugin_destroy', (event) => {
55
+
56
+ const element = event.detail?.element
57
+
58
+ if (Array.isArray(element)) {
59
+ for (const el of element) {
60
+ Plugin.destroy(el)
61
+ }
62
+ } else {
63
+ Plugin.destroy(element)
64
+ }
65
+
66
+ }, { passive: true })
67
+
68
+ document.addEventListener('storepress_plugin_reload', (event) => {
69
+ const defaultSettings = {}
70
+ const settings = { ...defaultSettings, ...event.detail?.settings }
71
+ const element = event.detail?.element
72
+
73
+ if (Array.isArray(element)) {
74
+ for (const el of element) {
75
+ Plugin.reload(el, settings)
76
+ }
77
+ } else {
78
+ Plugin.reload(element, settings)
79
+ }
80
+ }, { passive: true })
59
81
  }
60
82
 
61
83
  document.addEventListener('DOMContentLoaded', () => {
62
- StorePressPlugin()
63
- triggerEvent(document, 'storepress_plugin_init')
84
+ {{pascaleSlug}}Plugin()
85
+ triggerEvent(document, 'storepress_plugin_init', {
86
+ element: ['[data-plugin-settings]'],
87
+ settings: {},
88
+ })
64
89
  })
package/index.js CHANGED
@@ -27,7 +27,7 @@ module.exports = {
27
27
  folderName: 'src',
28
28
  namespace: 'storepress',
29
29
  slug: 'plugin',
30
- version: '0.1.0',
30
+ version: '0.0.1',
31
31
  title: 'StorePress Plugin',
32
32
  description: 'A StorePress Plugin',
33
33
  author: 'EmranAhmed',
@@ -127,6 +127,7 @@ module.exports = {
127
127
  const kebabNamespace = kebabCase(view.namespace)
128
128
  const constantSlug = constantCase(view.slug) // TWO_WORDS
129
129
  const kebabSlug = kebabCase(view.slug) // two-words
130
+ const pascaleSlug = pascalCase(pascalStorePress(view.slug)) // 'TwoWords'
130
131
  return {
131
132
  ...view,
132
133
  todayDate: todayDate,
@@ -136,6 +137,7 @@ module.exports = {
136
137
 
137
138
  constantSlug: constantSlug,
138
139
  kebabSlug: kebabSlug,
140
+ pascaleSlug: pascaleSlug,
139
141
  }
140
142
  },
141
143
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storepress/create-plugin-html",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A template to be used with `@wordpress/create-block` to create a StorePress HTML based JS Extension.",
5
5
  "author": "StorePress",
6
6
  "main": "index.js",