@blockquote-web-components/blockquote-base-meta 1.1.4 → 1.2.0
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.
- package/README.md +55 -30
- package/package.json +6 -3
- package/src/BlockquoteBaseMeta.js +73 -63
package/README.md
CHANGED
|
@@ -1,60 +1,85 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
`BlockquoteBaseMeta` is
|
|
4
|
-
It
|
|
5
|
-
You can use `BlockquoteBaseMeta` to share whatever you want.
|
|
6
|
-
The `BlockquoteBaseMeta` instances contain your actual data. The only requirement is that you
|
|
7
|
-
create them before you try to access them.
|
|
3
|
+
`BlockquoteBaseMeta` is a generic class for sharing information across the DOM tree, inspired by Polymer's [`iron-meta`](https://github.com/PolymerElements/iron-meta/blob/master/iron-meta.js).
|
|
4
|
+
It employs the [monostate pattern](http://c2.com/cgi/wiki?MonostatePattern) to allow any instance to access the shared information.
|
|
8
5
|
|
|
9
|
-
|
|
6
|
+
The class instances hold the actual data. They must be created before attempting to access them.
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
But the main difference is that Map allows keys of any type.
|
|
8
|
+
`BlockquoteBaseMeta` uses a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) to store the data. Unlike an Object, a Map allows keys of any type.
|
|
13
9
|
|
|
14
10
|
### Demo
|
|
15
11
|
|
|
16
12
|
[](https://stackblitz.com/github/oscarmarina/blockquote-web-components/tree/main/packages/base/blockquote-base-meta)
|
|
17
13
|
|
|
18
|
-
|
|
14
|
+
#### Usage
|
|
19
15
|
|
|
20
16
|
```js
|
|
21
17
|
import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
const myDefault = new BlockquoteBaseMeta({
|
|
20
|
+
key: 'basic',
|
|
21
|
+
value: 'foo/bar',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
console.log(myDefault.byKey('basic')); // foo/bar
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
#### Value Getter - String
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
31
|
+
|
|
32
|
+
const myDefault = new BlockquoteBaseMeta({
|
|
33
|
+
key: 'basic',
|
|
34
|
+
value: 'foo/bar',
|
|
35
|
+
});
|
|
27
36
|
|
|
28
37
|
console.log(myDefault.value); // foo/bar
|
|
29
38
|
```
|
|
30
39
|
|
|
31
|
-
|
|
40
|
+
#### List Getter - Array
|
|
41
|
+
|
|
42
|
+
The `list` getter returns an array of all values stored in the `BlockquoteBaseMeta` instance.
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
46
|
+
|
|
47
|
+
const myDefault = new BlockquoteBaseMeta({
|
|
48
|
+
type: 'three',
|
|
49
|
+
key: 'basic',
|
|
50
|
+
value: 'foo/bar',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
console.log(myDefault.list); // ['foo/bar']
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### ObjectList Getter - Object
|
|
32
57
|
|
|
33
58
|
```js
|
|
34
59
|
import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
35
60
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
const myDefault = new BlockquoteBaseMeta({
|
|
62
|
+
type: 'one',
|
|
63
|
+
key: 'basic',
|
|
64
|
+
value: 'foo/bar',
|
|
65
|
+
});
|
|
41
66
|
|
|
42
|
-
|
|
67
|
+
console.log(myDefault.objectList); // {basic: 'foo/bar'}
|
|
43
68
|
```
|
|
44
69
|
|
|
45
|
-
|
|
70
|
+
#### MapList Getter - Map
|
|
46
71
|
|
|
47
72
|
```js
|
|
48
73
|
import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
49
74
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
const keyInfo = { id: 'dsfaskj0' };
|
|
76
|
+
const myDefault = new BlockquoteBaseMeta({
|
|
77
|
+
type: 'two',
|
|
78
|
+
key: keyInfo,
|
|
79
|
+
value: 'foo/bar',
|
|
80
|
+
});
|
|
56
81
|
|
|
57
|
-
console.log(myDefault.mapList); // {{ id: 'dsfaskj0' }
|
|
82
|
+
console.log(myDefault.mapList); // Map(1) { { id: 'dsfaskj0' } => 'foo/bar' }
|
|
58
83
|
```
|
|
59
84
|
|
|
60
85
|
|
|
@@ -77,8 +102,8 @@ console.log(myDefault.mapList); // {{ id: 'dsfaskj0' }: 'foo/bar'}
|
|
|
77
102
|
| `list` | | | | Returns a list (Array) of the values for that instance \`type\` | |
|
|
78
103
|
| `mapList` | | | | Returns a list (Map) for that instance \`type\` | |
|
|
79
104
|
| `objectList` | | | | Returns a list (Object) for that instance \`type\` | |
|
|
80
|
-
| `type` | | |
|
|
81
|
-
| `key` | | |
|
|
105
|
+
| `type` | | | `type` | Type of Meta | |
|
|
106
|
+
| `key` | | | `key` | Key for Meta | |
|
|
82
107
|
|
|
83
108
|
##### Methods
|
|
84
109
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockquote-web-components/blockquote-base-meta",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Webcomponent blockquote-base-meta following open-wc recommendations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lit",
|
|
@@ -128,6 +128,9 @@
|
|
|
128
128
|
"import/prefer-default-export": "off",
|
|
129
129
|
"lit/no-classfield-shadowing": "off",
|
|
130
130
|
"lit/no-native-attributes": "off"
|
|
131
|
+
},
|
|
132
|
+
"globals": {
|
|
133
|
+
"globalThis": "readonly"
|
|
131
134
|
}
|
|
132
135
|
},
|
|
133
136
|
"stylelint": {
|
|
@@ -144,11 +147,11 @@
|
|
|
144
147
|
},
|
|
145
148
|
"devDependencies": {
|
|
146
149
|
"@blockquote-web-components/blockquote-base-common-dev-dependencies": "^1.7.2",
|
|
147
|
-
"@blockquote-web-components/blockquote-base-embedded-webview": "^1.6.
|
|
150
|
+
"@blockquote-web-components/blockquote-base-embedded-webview": "^1.6.5"
|
|
148
151
|
},
|
|
149
152
|
"publishConfig": {
|
|
150
153
|
"access": "public"
|
|
151
154
|
},
|
|
152
155
|
"customElements": "custom-elements.json",
|
|
153
|
-
"gitHead": "
|
|
156
|
+
"gitHead": "1706d179569702af0715c0bd85e405f88fc1a34e"
|
|
154
157
|
}
|
|
@@ -9,19 +9,21 @@ part of the polymer project is also subject to an additional IP rights grant
|
|
|
9
9
|
found at http://polymer.github.io/PATENTS.txt
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
const globalThisOrWindow = globalThis /* c8 ignore next */ || window;
|
|
13
|
+
|
|
12
14
|
const BLOCKQUOTE = Symbol.for('BLOCKQUOTE');
|
|
13
15
|
const blockquoteBaseMeta = Symbol.for('blockquoteBaseMeta');
|
|
14
16
|
const types = Symbol.for('types');
|
|
15
17
|
const uuid = Symbol.for('uuid');
|
|
16
18
|
|
|
17
|
-
const blockquote =
|
|
19
|
+
const blockquote = globalThisOrWindow[BLOCKQUOTE] || Object.create(null);
|
|
18
20
|
const baseMeta = blockquote[blockquoteBaseMeta] || Object.create(null);
|
|
19
21
|
|
|
20
22
|
baseMeta[types] = baseMeta[types] || new Map();
|
|
21
23
|
baseMeta[uuid] = baseMeta[uuid] || Math.random().toString(36).substring(2, 10);
|
|
22
24
|
|
|
23
25
|
blockquote[blockquoteBaseMeta] = baseMeta;
|
|
24
|
-
|
|
26
|
+
globalThisOrWindow[BLOCKQUOTE] = blockquote;
|
|
25
27
|
|
|
26
28
|
// https://www.oreilly.com/library/view/learning-javascript-design/9781449334840/ch13s15.html
|
|
27
29
|
// https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/
|
|
@@ -29,61 +31,86 @@ window[BLOCKQUOTE] = blockquote;
|
|
|
29
31
|
/**
|
|
30
32
|
* 
|
|
31
33
|
*
|
|
32
|
-
* `BlockquoteBaseMeta` is
|
|
33
|
-
* It
|
|
34
|
-
* You can use `BlockquoteBaseMeta` to share whatever you want.
|
|
35
|
-
* The `BlockquoteBaseMeta` instances contain your actual data. The only requirement is that you
|
|
36
|
-
* create them before you try to access them.
|
|
34
|
+
* `BlockquoteBaseMeta` is a generic class for sharing information across the DOM tree, inspired by Polymer's [`iron-meta`](https://github.com/PolymerElements/iron-meta/blob/master/iron-meta.js).
|
|
35
|
+
* It employs the [monostate pattern](http://c2.com/cgi/wiki?MonostatePattern) to allow any instance to access the shared information.
|
|
37
36
|
*
|
|
38
|
-
*
|
|
37
|
+
* The class instances hold the actual data. They must be created before attempting to access them.
|
|
39
38
|
*
|
|
40
|
-
*
|
|
41
|
-
* But the main difference is that Map allows keys of any type.
|
|
39
|
+
* `BlockquoteBaseMeta` uses a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) to store the data. Unlike an Object, a Map allows keys of any type.
|
|
42
40
|
*
|
|
43
41
|
* ### Demo
|
|
44
42
|
*
|
|
45
43
|
* [](https://stackblitz.com/github/oscarmarina/blockquote-web-components/tree/main/packages/base/blockquote-base-meta)
|
|
46
44
|
*
|
|
47
|
-
*
|
|
45
|
+
* #### Usage
|
|
48
46
|
*
|
|
49
47
|
* ```js
|
|
50
48
|
* import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
51
49
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
50
|
+
* const myDefault = new BlockquoteBaseMeta({
|
|
51
|
+
* key: 'basic',
|
|
52
|
+
* value: 'foo/bar',
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* console.log(myDefault.byKey('basic')); // foo/bar
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* #### Value Getter - String
|
|
59
|
+
*
|
|
60
|
+
* ```js
|
|
61
|
+
* import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
62
|
+
*
|
|
63
|
+
* const myDefault = new BlockquoteBaseMeta({
|
|
64
|
+
* key: 'basic',
|
|
65
|
+
* value: 'foo/bar',
|
|
66
|
+
* });
|
|
56
67
|
*
|
|
57
68
|
* console.log(myDefault.value); // foo/bar
|
|
58
69
|
* ```
|
|
59
70
|
*
|
|
60
|
-
*
|
|
71
|
+
* #### List Getter - Array
|
|
72
|
+
*
|
|
73
|
+
* The `list` getter returns an array of all values stored in the `BlockquoteBaseMeta` instance.
|
|
61
74
|
*
|
|
62
75
|
* ```js
|
|
63
76
|
* import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
64
77
|
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
78
|
+
* const myDefault = new BlockquoteBaseMeta({
|
|
79
|
+
* type: 'three',
|
|
80
|
+
* key: 'basic',
|
|
81
|
+
* value: 'foo/bar',
|
|
82
|
+
* });
|
|
70
83
|
*
|
|
71
|
-
*
|
|
84
|
+
* console.log(myDefault.list); // ['foo/bar']
|
|
72
85
|
* ```
|
|
73
86
|
*
|
|
74
|
-
*
|
|
87
|
+
* #### ObjectList Getter - Object
|
|
75
88
|
*
|
|
76
89
|
* ```js
|
|
77
90
|
* import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
78
91
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* });
|
|
92
|
+
* const myDefault = new BlockquoteBaseMeta({
|
|
93
|
+
* type: 'one',
|
|
94
|
+
* key: 'basic',
|
|
95
|
+
* value: 'foo/bar',
|
|
96
|
+
* });
|
|
85
97
|
*
|
|
86
|
-
* console.log(myDefault.
|
|
98
|
+
* console.log(myDefault.objectList); // {basic: 'foo/bar'}
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* #### MapList Getter - Map
|
|
102
|
+
*
|
|
103
|
+
* ```js
|
|
104
|
+
* import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
|
|
105
|
+
*
|
|
106
|
+
* const keyInfo = { id: 'dsfaskj0' };
|
|
107
|
+
* const myDefault = new BlockquoteBaseMeta({
|
|
108
|
+
* type: 'two',
|
|
109
|
+
* key: keyInfo,
|
|
110
|
+
* value: 'foo/bar',
|
|
111
|
+
* });
|
|
112
|
+
*
|
|
113
|
+
* console.log(myDefault.mapList); // Map(1) { { id: 'dsfaskj0' } => 'foo/bar' }
|
|
87
114
|
* ```
|
|
88
115
|
*/
|
|
89
116
|
export class BlockquoteBaseMeta {
|
|
@@ -94,18 +121,18 @@ export class BlockquoteBaseMeta {
|
|
|
94
121
|
* value?: *
|
|
95
122
|
* }} [options]
|
|
96
123
|
*/
|
|
97
|
-
constructor(
|
|
124
|
+
constructor({ type = 'default', key, value } = {}) {
|
|
98
125
|
/**
|
|
99
126
|
* Type of Meta
|
|
100
127
|
*/
|
|
101
|
-
this.type =
|
|
128
|
+
this.type = type;
|
|
102
129
|
|
|
103
130
|
/**
|
|
104
131
|
* Key for Meta
|
|
105
132
|
*/
|
|
106
|
-
this.key =
|
|
107
|
-
if (
|
|
108
|
-
this.value =
|
|
133
|
+
this.key = key;
|
|
134
|
+
if (value != null) {
|
|
135
|
+
this.value = value;
|
|
109
136
|
}
|
|
110
137
|
}
|
|
111
138
|
|
|
@@ -122,32 +149,25 @@ export class BlockquoteBaseMeta {
|
|
|
122
149
|
* @return {*}
|
|
123
150
|
*/
|
|
124
151
|
get value() {
|
|
125
|
-
const { type } = this;
|
|
126
152
|
const key = this.__key || this.key;
|
|
127
153
|
this.__key = undefined;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
return undefined;
|
|
154
|
+
const typeMap = BlockquoteBaseMeta.types.get(this.type);
|
|
155
|
+
return typeMap?.get(key);
|
|
132
156
|
}
|
|
133
157
|
|
|
134
158
|
/**
|
|
135
159
|
* Sets value to instance type and key
|
|
136
160
|
* @param {*} value
|
|
137
161
|
*/
|
|
162
|
+
|
|
138
163
|
set value(value) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
BlockquoteBaseMeta.types.set(type, new Map());
|
|
145
|
-
}
|
|
146
|
-
__type = BlockquoteBaseMeta.types.get(type);
|
|
147
|
-
if (value === null) {
|
|
148
|
-
__type.delete(key);
|
|
164
|
+
if (this.type && this.key) {
|
|
165
|
+
const typeMap = BlockquoteBaseMeta.types.get(this.type) ?? new Map();
|
|
166
|
+
BlockquoteBaseMeta.types.set(this.type, typeMap);
|
|
167
|
+
if (value != null) {
|
|
168
|
+
typeMap.set(this.key, value);
|
|
149
169
|
} else {
|
|
150
|
-
|
|
170
|
+
typeMap.delete(this.key);
|
|
151
171
|
}
|
|
152
172
|
}
|
|
153
173
|
}
|
|
@@ -157,12 +177,7 @@ export class BlockquoteBaseMeta {
|
|
|
157
177
|
* @return {Array}
|
|
158
178
|
*/
|
|
159
179
|
get list() {
|
|
160
|
-
|
|
161
|
-
const itemsType = BlockquoteBaseMeta.types.get(type);
|
|
162
|
-
if (itemsType) {
|
|
163
|
-
return Array.from(itemsType.values());
|
|
164
|
-
}
|
|
165
|
-
return [];
|
|
180
|
+
return Array.from(this.mapList.values());
|
|
166
181
|
}
|
|
167
182
|
|
|
168
183
|
/**
|
|
@@ -170,12 +185,7 @@ export class BlockquoteBaseMeta {
|
|
|
170
185
|
* @return {Map}
|
|
171
186
|
*/
|
|
172
187
|
get mapList() {
|
|
173
|
-
|
|
174
|
-
const itemsType = BlockquoteBaseMeta.types.get(type);
|
|
175
|
-
if (itemsType) {
|
|
176
|
-
return itemsType;
|
|
177
|
-
}
|
|
178
|
-
return new Map();
|
|
188
|
+
return BlockquoteBaseMeta.types.get(this.type) ?? new Map();
|
|
179
189
|
}
|
|
180
190
|
|
|
181
191
|
/**
|