marjoram 0.0.3
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/LICENSE +7 -0
- package/README.md +317 -0
- package/dist/marjoram.cjs.js +16 -0
- package/dist/marjoram.umd.js +15 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2017 [these people](https://github.com/rollup/rollup-starter-lib/graphs/contributors)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# Marjoram 🌿
|
|
2
|
+
|
|
3
|
+
**A lightweight JavaScript library to create and manipulate DOM elements and to manage their state**
|
|
4
|
+
|
|
5
|
+
✨ *0 dependencies* ✨
|
|
6
|
+
✨ *Safe from XSS attacks* ✨
|
|
7
|
+
✨ *TypeScript Support* ✨
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
## Table of Contents
|
|
11
|
+
- [Marjoram 🌿](#marjoram-)
|
|
12
|
+
- [Table of Contents](#table-of-contents)
|
|
13
|
+
- [Getting Started](#getting-started)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Implementation](#implementation)
|
|
16
|
+
- [API](#api)
|
|
17
|
+
- [View](#view)
|
|
18
|
+
- [How to create a View](#how-to-create-a-view)
|
|
19
|
+
- [How to use refs](#how-to-use-refs)
|
|
20
|
+
- [ViewModel](#viewmodel)
|
|
21
|
+
- [How to create a ViewModel](#how-to-create-a-viewmodel)
|
|
22
|
+
- [How to use a ViewModel](#how-to-use-a-viewmodel)
|
|
23
|
+
- [SchemaProp](#schemaprop)
|
|
24
|
+
- [Computed Values](#computed-values)
|
|
25
|
+
- [Observe Values](#observe-values)
|
|
26
|
+
- [Examples](#examples)
|
|
27
|
+
- [Stateless View](#stateless-view)
|
|
28
|
+
- [Stateful View](#stateful-view)
|
|
29
|
+
- [Nested Views](#nested-views)
|
|
30
|
+
- [Arrays](#arrays)
|
|
31
|
+
- [More](#more)
|
|
32
|
+
- [Glossary](#glossary)
|
|
33
|
+
- [View](#view-1)
|
|
34
|
+
- [Model](#model)
|
|
35
|
+
- [ViewModel](#viewmodel-1)
|
|
36
|
+
- [SchemaProp](#schemaprop-1)
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
## Getting Started
|
|
40
|
+
|
|
41
|
+
### Installation
|
|
42
|
+
```bash
|
|
43
|
+
npm i marjoram
|
|
44
|
+
```
|
|
45
|
+
### Implementation
|
|
46
|
+
Views can be stateless or stateful when used in conjunction with `useViewModel`.
|
|
47
|
+
|
|
48
|
+
**Example:**
|
|
49
|
+
```javascript
|
|
50
|
+
import { html, useViewModel } from 'marjoram'
|
|
51
|
+
|
|
52
|
+
const Greeting = () => {
|
|
53
|
+
// Create the view's state
|
|
54
|
+
const viewModel = useViewModel({ name: 'world 🌎 '})
|
|
55
|
+
|
|
56
|
+
// Create the view
|
|
57
|
+
const view = html`
|
|
58
|
+
<div>
|
|
59
|
+
<h1>Hello, ${viewModel.$name}</h1>
|
|
60
|
+
</div>
|
|
61
|
+
`
|
|
62
|
+
|
|
63
|
+
// Expose the viewModel
|
|
64
|
+
view.viewModel = viewModel
|
|
65
|
+
|
|
66
|
+
return view
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const greeting = Greeting()
|
|
70
|
+
|
|
71
|
+
// Appends h1 that reads "Hello, world 🌎" to DOM
|
|
72
|
+
document.body.appendChild(greeting)
|
|
73
|
+
|
|
74
|
+
// Changes h1 text to "Hello, hotdog 🌭"
|
|
75
|
+
greeting.viewModel.name = "hotdog 🌭"
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
---
|
|
79
|
+
## API
|
|
80
|
+
|
|
81
|
+
### View
|
|
82
|
+
#### How to create a View
|
|
83
|
+
|
|
84
|
+
Use the `html` tagged template literal to create a `View`.
|
|
85
|
+
|
|
86
|
+
**Returns:**
|
|
87
|
+
A View, which is an extention of the DocumentFragment object.
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
**Example:**
|
|
91
|
+
```javascript
|
|
92
|
+
import { html } from 'marjoram'
|
|
93
|
+
|
|
94
|
+
const view = html`<h1>Hello, world!</h1>`
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
#### How to use refs
|
|
99
|
+
|
|
100
|
+
Create a ref adding a `ref` attribute to any element in your view.
|
|
101
|
+
|
|
102
|
+
Collect refs using `View.prototype.collect()`
|
|
103
|
+
|
|
104
|
+
The `collect()` method can be called from any `View` instance and enables us to collect elements within our view with a `ref` attribute. This is useful for a variety of reasons including to attach event listeners.
|
|
105
|
+
|
|
106
|
+
**Returns**
|
|
107
|
+
An object containing every HTML Element with a unique `ref` attribute.
|
|
108
|
+
|
|
109
|
+
**Example:**
|
|
110
|
+
```javascript
|
|
111
|
+
const view = html`
|
|
112
|
+
<div>
|
|
113
|
+
<h1 ref="heading">Welcome Back!</h1>
|
|
114
|
+
<h2 ref="subheading">You have 3 notifications</h2>
|
|
115
|
+
</div>
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
const elements = view.collect();
|
|
119
|
+
/*
|
|
120
|
+
elements = {
|
|
121
|
+
heading: h1 Node,
|
|
122
|
+
subheading: h2 Node
|
|
123
|
+
}
|
|
124
|
+
*/
|
|
125
|
+
```
|
|
126
|
+
---
|
|
127
|
+
### ViewModel
|
|
128
|
+
A ViewModel is a stateful representation of the model provided when creating it.
|
|
129
|
+
|
|
130
|
+
Getting and setting view model properties works the same way it would for any other object in JavaScript.
|
|
131
|
+
|
|
132
|
+
When a `ViewModel` is created, a `SchemaProp` will be made for each property and can be accessed by prefixing the property name with `$`. Avoid reassigning this value.
|
|
133
|
+
#### How to create a ViewModel
|
|
134
|
+
The `useViewModel({})` function creates a stateful "view model" object, given an object as an argument.
|
|
135
|
+
|
|
136
|
+
It is tightly coupled to a `html`, such that when a view model property is modified, your view is also modified.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
**Example:**
|
|
140
|
+
```javascript
|
|
141
|
+
import { useViewModel } from 'marjoram'
|
|
142
|
+
|
|
143
|
+
const viewModel = useViewModel({ name: 'Patrick Stewart' })
|
|
144
|
+
```
|
|
145
|
+
#### How to use a ViewModel
|
|
146
|
+
|
|
147
|
+
**❗️ IMPORTANT**
|
|
148
|
+
Prefix your view model properties with `$` when we are accessing them within a view. This convention allows us to use a view model property more than once within a view.
|
|
149
|
+
|
|
150
|
+
[Learn more about SchemaProps the `$` prefix here](#schemaprop)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
**Example**
|
|
154
|
+
```javascript
|
|
155
|
+
import { html, useViewModel } from 'marjoram'
|
|
156
|
+
|
|
157
|
+
const viewModel = useViewModel({ firstName: 'Patrick', lastName: 'Stewart' })
|
|
158
|
+
|
|
159
|
+
const view = html`<h1>Hello, ${viewModel.$firstName} ${viewModel.$lastName}!</h1>`
|
|
160
|
+
// h1 text: Hello, Patrick Stewart!
|
|
161
|
+
|
|
162
|
+
viewModel.lastName = "Star"
|
|
163
|
+
// h1 text: Hello, Patrick Star!
|
|
164
|
+
```
|
|
165
|
+
---
|
|
166
|
+
### SchemaProp
|
|
167
|
+
When a `ViewModel` is created, a `SchemaProp` will be made for each property and can be accessed by prefixing the property name with `$`
|
|
168
|
+
|
|
169
|
+
Excluding the `$` prefix will return the property value as expected rather than the schemaProp.
|
|
170
|
+
|
|
171
|
+
It is necessary to add the `$` prefix to your property name if that property is being used accessed by a `View`.
|
|
172
|
+
|
|
173
|
+
**❗️ IMPORTANT**
|
|
174
|
+
Avoid reassigning this value if we want to avoid breaking things.
|
|
175
|
+
|
|
176
|
+
#### Computed Values
|
|
177
|
+
|
|
178
|
+
When we call the `SchemaProp.prototype.compute(callback)` method on a schemaProp, we can perform some logic on the value that we want to render every time the schemaProp value changes.
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
**Example**
|
|
182
|
+
```javascript
|
|
183
|
+
const viewModel = useViewModel({ number: 4 });
|
|
184
|
+
const { $number } = viewModel
|
|
185
|
+
// Create a computed property
|
|
186
|
+
const doubledNumber = $number.compute((val) => val * 2)
|
|
187
|
+
|
|
188
|
+
const view = html`
|
|
189
|
+
<p>
|
|
190
|
+
${$number} x 2 = ${doubledNumber}
|
|
191
|
+
</p>
|
|
192
|
+
`;
|
|
193
|
+
/* 4 x 2 = 8 */
|
|
194
|
+
|
|
195
|
+
// Updating the value also updates all related computed properties
|
|
196
|
+
viewModel.number = 99
|
|
197
|
+
/* 99 x 2 = 198 */
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### Observe Values
|
|
201
|
+
TODO
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
## Examples
|
|
205
|
+
|
|
206
|
+
### Stateless View
|
|
207
|
+
```javascript
|
|
208
|
+
const view = html`
|
|
209
|
+
<div>
|
|
210
|
+
<h1 ref="heading">Welcome Back!</h1>
|
|
211
|
+
<h2 ref="subheading">You have 3 notifications</h2>
|
|
212
|
+
</div>
|
|
213
|
+
`;
|
|
214
|
+
|
|
215
|
+
const elements = view.collect();
|
|
216
|
+
|
|
217
|
+
elements.subheading.addEventListener('click', () => {
|
|
218
|
+
// Do something
|
|
219
|
+
})
|
|
220
|
+
```
|
|
221
|
+
### Stateful View
|
|
222
|
+
```javascript
|
|
223
|
+
const viewModel = useViewModel({text:'Confirm'})
|
|
224
|
+
const view = html`
|
|
225
|
+
<button ref="btn">${viewModel.$text}</button>
|
|
226
|
+
`;
|
|
227
|
+
|
|
228
|
+
const elements = view.collect();
|
|
229
|
+
|
|
230
|
+
elements.btn.addEventListener('click', () => {
|
|
231
|
+
// Do something
|
|
232
|
+
viewModel.text = 'Cancel'
|
|
233
|
+
})
|
|
234
|
+
```
|
|
235
|
+
### Nested Views
|
|
236
|
+
```javascript
|
|
237
|
+
const numbers = [1, 2, 3, 4];
|
|
238
|
+
const viewModel = useViewModel({numbers});
|
|
239
|
+
|
|
240
|
+
const listItems = viewModel.numbers.map(
|
|
241
|
+
(num, i) =>
|
|
242
|
+
html`
|
|
243
|
+
<li ref="listItem${i}">
|
|
244
|
+
List Item: ${num} +
|
|
245
|
+
<span>${num.compute((val) => val + 1)}</span> =
|
|
246
|
+
<span>${num.compute((val) => val * 2 + 1)}</span>
|
|
247
|
+
</li>
|
|
248
|
+
`
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
const view = html`
|
|
252
|
+
<ul ref="listEl">
|
|
253
|
+
${listItems}
|
|
254
|
+
</ul>
|
|
255
|
+
`;
|
|
256
|
+
|
|
257
|
+
/*
|
|
258
|
+
List Item: 1 + 2 = 3
|
|
259
|
+
List Item: 2 + 3 = 5
|
|
260
|
+
List Item: 3 + 4 = 7
|
|
261
|
+
List Item: 4 + 5 = 9
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Arrays
|
|
267
|
+
TODO
|
|
268
|
+
|
|
269
|
+
### More
|
|
270
|
+
- #### [JumpLink](demo/src/components/JumpLink.ts)
|
|
271
|
+
- #### [Form](demo/src/components/ContactForm.ts)
|
|
272
|
+
- #### [Modal](demo/src/components/Modal.ts.ts)
|
|
273
|
+
- #### [SVG Icons](demo/src/components/Icons.ts)
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
## Glossary
|
|
277
|
+
|
|
278
|
+
### View
|
|
279
|
+
A DOM node, specifically a DocumentFragment, that is returned by the `html` tagged template literal
|
|
280
|
+
|
|
281
|
+
**Example**
|
|
282
|
+
```javascript
|
|
283
|
+
const view = html`<h1>Hello ✌️</h1>`
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Model
|
|
287
|
+
An object with arbitrary values used to populate a `ViewModel`
|
|
288
|
+
|
|
289
|
+
**Example**
|
|
290
|
+
```javascript
|
|
291
|
+
const model = { firstName: 'Patrick', lastName: 'Stewart' }
|
|
292
|
+
```
|
|
293
|
+
### ViewModel
|
|
294
|
+
A stateful representation of the model provided and whose properties and are bound to the `View` they populate.
|
|
295
|
+
|
|
296
|
+
**Example**
|
|
297
|
+
```javascript
|
|
298
|
+
const model = { firstName: 'Patrick', lastName: 'Stewart' }
|
|
299
|
+
const viewModel = useViewModel(model)
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### SchemaProp
|
|
303
|
+
An individual property within a `ViewModel` whose accessor is prefixed with a `$` and is bound to the `View` it populates.
|
|
304
|
+
|
|
305
|
+
**Example**
|
|
306
|
+
```javascript
|
|
307
|
+
const model = { firstName: 'Patrick', lastName: 'Stewart' }
|
|
308
|
+
const viewModel = useViewModel(model)
|
|
309
|
+
|
|
310
|
+
// Bind schemaProps to the view with the `$` syntax
|
|
311
|
+
const view = html`<h1>Hello, ${viewModel.$firstName} ${viewModel.$lastName}!</h1>`
|
|
312
|
+
// Rendered text: Hello, Patrick Stewart!
|
|
313
|
+
|
|
314
|
+
viewModel.lastName = "Star"
|
|
315
|
+
// Rendered text: Hello, Patrick Star!
|
|
316
|
+
```
|
|
317
|
+
---
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*! *****************************************************************************
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
***************************************************************************** */
|
|
16
|
+
function e(e,t,r,o){if("a"===r&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?o:"a"===r?o.call(e):o?o.value:t.get(e)}var t,r;Object.defineProperty(exports,"__esModule",{value:!0});class o{constructor(e,o,n){t.set(this,void 0),r.set(this,[]),this.key=o,this.value=n,this.id="_"+Math.random().toString(36).substr(2,9),this.compute=this.compute.bind(this,e)}update(o){if(this.value!==o){const n=e(this,t,"f")?e(this,t,"f").call(this,o):o;e(this,r,"f")&&e(this,r,"f").forEach((e=>e(n))),this.value=n}return this}observe(t,o=this){t instanceof Node&&(t=this.nodeObserver(t)),e(this,r,"f").push(t.bind(o))}compute(e,r){const o=e.defineProperty(r(this.value));return function(e,t,r,o,n){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===o?n.call(e,r):n?n.value=r:t.set(e,r)}(o,t,r,"f"),this.observe(o.update,o),o}nodeObserver(e){let t=this.value;const r=e.parentElement;return o=>{t=this.value,e instanceof Attr?e.value=e.value.replace(t.toString(),o.toString()):Array.isArray(o)?r.replaceChildren(...o):e.textContent=e.textContent.replace(t.toString(),o.toString())}}}t=new WeakMap,r=new WeakMap;const n=function(e){if(e)return e;return{ids:[],props:[],defineProperty(e,t){if(o.prototype.isPrototypeOf(e)){const t=e,{id:r}=t;return this.hasId(r)||(this.props.push(t),this.ids.push(r)),t}const r=new o(this,t,e);return this.props.push(r),this.ids.push(r.id),r},getPropertyByKey(e){return this.props.find((({key:t})=>t===e))},getPropertyByValue(e){return this.props.find((({value:t})=>t===e))},getPropertyById(e){return this.props.find((({id:t})=>t===e))},hasProperty(e){return this.props.some((t=>t.key===e))},hasId(e){return this.ids.some((t=>t===e))}}},s=(e,t,r)=>t.reduce(((t,o,n)=>{const s=r.defineProperty(o,null==o?void 0:o.key);return[...t,((e,{id:t,value:r})=>{const o=e=>e instanceof Node;if(Array.isArray(r)&&r.every(o)||o(r))return`<del ${t}></del>`})(0,s)||s.id,e[n+1]]}),[e[0]]).join(""),i=e=>{const{id:t,value:r}=e;return o=>{var n;(e=>{Array.isArray(e)||(e instanceof DocumentFragment||Element)})(r),(null===(n=o.textContent)||void 0===n?void 0:n.includes(t))&&(e.observe(o,e),Array.isArray(r)?(r.forEach(i(e)),o.textContent=o.textContent.replace(t,"")):"object"!=typeof r&&(o.textContent=o.textContent.replace(t,r.toString())))}},a=e=>{const{attributes:t,childNodes:r}=e,o=Array.from(t),n=Array.from(r).filter((({nodeType:e,textContent:t})=>e===Node.TEXT_NODE&&t.trim()));return e=>{((e,t)=>{e.forEach(i(t))})(n,e),((e,t)=>{e.forEach((e=>{const{id:t,value:r}=e;return o=>{(o.value.includes(t)&&"data-id"!==o.name||"string"==typeof r&&"number"==typeof r)&&(e.observe(o,e),o.value=o.value.replace(t,r))}})(t))})(o,e)}},c=(e,t)=>{const r=e.props;r.forEach(((e,t)=>e=>{const{id:r,value:o}=e,n=t.querySelector(`del[${r}]`),s=e=>e instanceof Node,i=Array.isArray(o)&&o.every(s);if(n&&(s||i)){e.observe(n,e);const t=Array.isArray(o)?o:[o];n.replaceWith(...t)}})(0,t));return Array.from(t.querySelectorAll("*")).forEach((e=>t=>e.forEach(a(t)))(r)),t};exports.html=function(e,...t){const r=n(),o=s(e,t,r),i=document.createElement("template");i.innerHTML=o;const a=c(r,i.content);return a.collect=function(e){const t=e.querySelectorAll("[ref]");return()=>{if(null==e?void 0:e.refs)return e.refs;const r=Array.from(t).reduce(((e,t)=>(e[t.getAttribute("ref")]=t,e)),{});return e.refs=r,r}}(a),a},exports.useViewModel=function(e){const t=n();e=Object.assign({},e);const r={get(e,r){if("symbol"==typeof r)return;const n="$"===r[0];if(!((r=n?r.replace("$",""):r)in e))return;const s=Reflect.get(e,r),i=o.prototype.isPrototypeOf(s),a=t.defineProperty(s,r);if(Reflect.set(e,r,a),n)return a;if(i)return Reflect.get(s,"value");if(!("object"!=typeof s||Array.isArray(s)||s instanceof Node)){const t=new Proxy(s,this);return Reflect.set(e,r,t),t}return s},set(e,r,o){if("symbol"==typeof r)return!1;const n=t.getPropertyByKey(r);return!n||(n.update(o),!!n)}};return new Proxy(e,r)};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).marjoram={})}(this,(function(e){"use strict";
|
|
2
|
+
/*! *****************************************************************************
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
***************************************************************************** */function t(e,t,r,o){if("a"===r&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?o:"a"===r?o.call(e):o?o.value:t.get(e)}var r,o;class n{constructor(e,t,n){r.set(this,void 0),o.set(this,[]),this.key=t,this.value=n,this.id="_"+Math.random().toString(36).substr(2,9),this.compute=this.compute.bind(this,e)}update(e){if(this.value!==e){const n=t(this,r,"f")?t(this,r,"f").call(this,e):e;t(this,o,"f")&&t(this,o,"f").forEach((e=>e(n))),this.value=n}return this}observe(e,r=this){e instanceof Node&&(e=this.nodeObserver(e)),t(this,o,"f").push(e.bind(r))}compute(e,t){const o=e.defineProperty(t(this.value));return function(e,t,r,o,n){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===o?n.call(e,r):n?n.value=r:t.set(e,r)}(o,r,t,"f"),this.observe(o.update,o),o}nodeObserver(e){let t=this.value;const r=e.parentElement;return o=>{t=this.value,e instanceof Attr?e.value=e.value.replace(t.toString(),o.toString()):Array.isArray(o)?r.replaceChildren(...o):e.textContent=e.textContent.replace(t.toString(),o.toString())}}}r=new WeakMap,o=new WeakMap;const i=function(e){if(e)return e;return{ids:[],props:[],defineProperty(e,t){if(n.prototype.isPrototypeOf(e)){const t=e,{id:r}=t;return this.hasId(r)||(this.props.push(t),this.ids.push(r)),t}const r=new n(this,t,e);return this.props.push(r),this.ids.push(r.id),r},getPropertyByKey(e){return this.props.find((({key:t})=>t===e))},getPropertyByValue(e){return this.props.find((({value:t})=>t===e))},getPropertyById(e){return this.props.find((({id:t})=>t===e))},hasProperty(e){return this.props.some((t=>t.key===e))},hasId(e){return this.ids.some((t=>t===e))}}},s=(e,t,r)=>t.reduce(((t,o,n)=>{const i=r.defineProperty(o,null==o?void 0:o.key);return[...t,((e,{id:t,value:r})=>{const o=e=>e instanceof Node;if(Array.isArray(r)&&r.every(o)||o(r))return`<del ${t}></del>`})(0,i)||i.id,e[n+1]]}),[e[0]]).join(""),a=e=>{const{id:t,value:r}=e;return o=>{var n;(e=>{Array.isArray(e)||(e instanceof DocumentFragment||Element)})(r),(null===(n=o.textContent)||void 0===n?void 0:n.includes(t))&&(e.observe(o,e),Array.isArray(r)?(r.forEach(a(e)),o.textContent=o.textContent.replace(t,"")):"object"!=typeof r&&(o.textContent=o.textContent.replace(t,r.toString())))}},u=e=>{const{attributes:t,childNodes:r}=e,o=Array.from(t),n=Array.from(r).filter((({nodeType:e,textContent:t})=>e===Node.TEXT_NODE&&t.trim()));return e=>{((e,t)=>{e.forEach(a(t))})(n,e),((e,t)=>{e.forEach((e=>{const{id:t,value:r}=e;return o=>{(o.value.includes(t)&&"data-id"!==o.name||"string"==typeof r&&"number"==typeof r)&&(e.observe(o,e),o.value=o.value.replace(t,r))}})(t))})(o,e)}},c=(e,t)=>{const r=e.props;r.forEach(((e,t)=>e=>{const{id:r,value:o}=e,n=t.querySelector(`del[${r}]`),i=e=>e instanceof Node,s=Array.isArray(o)&&o.every(i);if(n&&(i||s)){e.observe(n,e);const t=Array.isArray(o)?o:[o];n.replaceWith(...t)}})(0,t));return Array.from(t.querySelectorAll("*")).forEach((e=>t=>e.forEach(u(t)))(r)),t};e.html=function(e,...t){const r=i(),o=s(e,t,r),n=document.createElement("template");n.innerHTML=o;const a=c(r,n.content);return a.collect=function(e){const t=e.querySelectorAll("[ref]");return()=>{if(null==e?void 0:e.refs)return e.refs;const r=Array.from(t).reduce(((e,t)=>(e[t.getAttribute("ref")]=t,e)),{});return e.refs=r,r}}(a),a},e.useViewModel=function(e){const t=i();e=Object.assign({},e);const r={get(e,r){if("symbol"==typeof r)return;const o="$"===r[0];if(!((r=o?r.replace("$",""):r)in e))return;const i=Reflect.get(e,r),s=n.prototype.isPrototypeOf(i),a=t.defineProperty(i,r);if(Reflect.set(e,r,a),o)return a;if(s)return Reflect.get(i,"value");if(!("object"!=typeof i||Array.isArray(i)||i instanceof Node)){const t=new Proxy(i,this);return Reflect.set(e,r,t),t}return i},set(e,r,o){if("symbol"==typeof r)return!1;const n=t.getPropertyByKey(r);return!n||(n.update(o),!!n)}};return new Proxy(e,r)},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "marjoram",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"main": "dist/marjoram.cjs.js",
|
|
5
|
+
"module": "dist/marjoram.esm.js",
|
|
6
|
+
"browser": "dist/marjoram.umd.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"dependencies": {},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@emotion/css": "^11.1.3",
|
|
11
|
+
"@rollup/plugin-typescript": "^8.2.1",
|
|
12
|
+
"@testing-library/dom": "^8.5.0",
|
|
13
|
+
"@testing-library/jest-dom": "^5.14.1",
|
|
14
|
+
"@types/jest": "^27.0.1",
|
|
15
|
+
"@types/ms": "^0.7.31",
|
|
16
|
+
"@types/testing-library__jest-dom": "^5.14.1",
|
|
17
|
+
"canvas": "^2.8.0",
|
|
18
|
+
"jest": "^27.2.0",
|
|
19
|
+
"rollup": "^2.47.0",
|
|
20
|
+
"rollup-plugin-commonjs": "^9.2.0",
|
|
21
|
+
"rollup-plugin-inject-process-env": "^1.3.1",
|
|
22
|
+
"rollup-plugin-livereload": "^2.0.0",
|
|
23
|
+
"rollup-plugin-node-resolve": "^4.0.0",
|
|
24
|
+
"rollup-plugin-serve": "^1.1.0",
|
|
25
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
26
|
+
"ts-jest": "^27.0.5",
|
|
27
|
+
"ts-node": "^7.0.1",
|
|
28
|
+
"tslib": "^2.2.0",
|
|
29
|
+
"typescript": "^4.2.4"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "rm -rf dist/* && rollup -c rollup.config.js && tsc --emitDeclarationOnly",
|
|
33
|
+
"dev": "rollup -c rollup.config.dev.js -w",
|
|
34
|
+
"test": "jest",
|
|
35
|
+
"test:watch": "jest --watchAll",
|
|
36
|
+
"pretest": "yarn build",
|
|
37
|
+
"publish": "yarn build && npm publish --access=public"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"./dist"
|
|
41
|
+
]
|
|
42
|
+
}
|