@dinoreic/fez 0.1.1 → 0.2.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.
package/README.md CHANGED
@@ -29,9 +29,7 @@ It replaces modern JS frameworks by using native Autonomous Custom Elements to c
29
29
 
30
30
  This article, [Web Components Will Replace Your Frontend Framework](https://www.dannymoerkerke.com/blog/web-components-will-replace-your-frontend-framework/), is from 2019. Join the future, ditch React, Angular and other never defined, always "evolving" monstrosities. Vanilla is the way :)
31
31
 
32
- There is no some "internal state" that is by some magic reflected to DOM. No! All methods Fez use to manipulate DOM are just helpers around native DOM interface. Work on DOM raw, use jQuery, use built in [node builder](https://github.com/dux/fez/blob/main/src/lib/n.js) or full template mapping with [morphing](https://github.com/bigskysoftware/idiomorph).
33
-
34
- It great in combination with another widely used JS libs, as jQuery, Zepto, underscore of loDash.
32
+ There is no some "internal state" that is by some magic reflected to DOM. No! All methods Fez use to manipulate DOM are just helpers around native DOM interface. Work on DOM raw, use built in [node builder](https://github.com/dux/fez/blob/main/src/lib/n.js) or full template mapping with [morphing](https://github.com/bigskysoftware/idiomorph).
35
33
 
36
34
  ## How it works
37
35
 
@@ -49,25 +47,17 @@ Here's a simple counter component that demonstrates Fez's core features:
49
47
  ```html
50
48
  <!-- Define a counter component in ex-counter.fez.html -->
51
49
  <script>
50
+ // called when Fez node is connected to DOM
52
51
  init() {
53
- // called when Fez node is connected to DOM
54
52
  this.MAX = 6
55
53
  this.state.count = 0
56
54
  }
57
55
 
58
- onStateChange(key, value, oldValue) {
59
- // called whenever this.state changes
60
- console.log(`State ${key} changed from ${oldValue} to ${value}`)
61
- if (key === 'count' && value >= this.MAX) {
62
- console.log('Counter reached maximum!')
63
- }
64
- }
65
-
66
56
  isMax() {
67
- // is state is changed, template is re-rendered
68
57
  return this.state.count >= this.MAX
69
58
  }
70
59
 
60
+ // is state is changed, template is re-rendered
71
61
  more() {
72
62
  this.state.count += this.isMax() ? 0 : 1
73
63
  }
@@ -153,6 +143,7 @@ This example showcases:
153
143
  * **Powerful Template Engine** - Multiple syntaxes (`{{ }}` and `[[ ]]`), control flow (`#if`, `#unless`, `#for`, `#each`), and block templates
154
144
  * **Reactive State Management** - Built-in reactive `state` object automatically triggers re-renders on property changes
155
145
  * **DOM Morphing** - Uses [Idiomorph](https://github.com/bigskysoftware/idiomorph) for intelligent DOM updates that preserve element state and animations
146
+ * **Preserve DOM Elements** - Use `fez-keep="unique-key"` attribute to preserve DOM elements across re-renders (useful for animations, form inputs, or stateful elements)
156
147
  * **Style Macros** - Define custom CSS shortcuts like `Fez.styleMacro('mobile', '@media (max-width: 768px)')` and use as `:mobile { ... }`
157
148
  * **Scoped & Global Styles** - Components can define both scoped CSS (`:fez { ... }`) and global styles in the same component
158
149
 
@@ -189,57 +180,9 @@ This example showcases:
189
180
  ### Fez Static Methods
190
181
 
191
182
  ```js
192
- Fez('#foo') // find fez node with id="foo"
193
- Fez('ui-tabs', this) // find first parent node ui-tabs
194
-
195
- // add global scss
196
- Fez.globalCss(`
197
- .some-class {
198
- color: red;
199
- &.foo { ... }
200
- .foo { ... }
201
- }
202
- ...
203
- `)
204
-
205
- // internal, get unique ID for a string, poor mans MD5 / SHA1
206
- Fez.fnv1('some string')
207
-
208
- // get generated css class name, from scss source string
209
- Fez.css(text)
210
-
211
- // get generated css class name without global attachment
212
- Fez.cssClass(text)
213
-
214
- // display information about fast/slow bind components in console
215
- Fez.info()
216
-
217
- // low-level DOM morphing function
218
- Fez.morphdom(target, newNode, opts)
219
-
220
- // HTML escaping utility
221
- Fez.htmlEscape(text)
222
-
223
- // create HTML tags with encoded props
224
- Fez.tag(tag, opts, html)
225
-
226
- // execute function until it returns true
227
- Fez.untilTrue(func, pingRate)
228
-
229
- // add scripts/styles to document head
230
- // Load JavaScript from URL: Fez.head({ js: 'path/to/script.js' })
231
- // Load JavaScript with attributes: Fez.head({ js: 'path/to/script.js', type: 'module', async: true })
232
- // Load JavaScript with callback: Fez.head({ js: 'path/to/script.js' }, () => console.log('loaded'))
233
- // Load JavaScript module and auto-import to window: Fez.head({ js: 'path/to/module.js', module: 'MyModule' })
234
- // Load CSS: Fez.head({ css: 'path/to/styles.css' })
235
- // Load CSS with attributes: Fez.head({ css: 'path/to/styles.css', media: 'print' })
236
- // Execute inline script: Fez.head({ script: 'console.log("Hello world")' })
237
- Fez.head(config, callback)
238
-
239
- // define custom style macro
240
- // Fez.styleMacro('mobile', '@media (max-width: 768px)')
241
- // :mobile { ... } -> @media (max-width: 768px) { ... }
242
- Fez.styleMacro(name, value)
183
+ Fez('#foo') // find fez node with id="foo"
184
+ Fez('ui-tabs', this) // find first parent node ui-tabs
185
+ Fez('ui-tabs', (fez)=> ... ) // loop over all ui-tabs nodes
243
186
 
244
187
  // define custom DOM node name -> <foo-bar>...
245
188
  Fez('foo-bar', class {
@@ -269,103 +212,169 @@ Fez('foo-bar', class {
269
212
  GLOBAL = 'Dialog'
270
213
  GLOBAL = true // just append node to document, do not create window reference
271
214
 
272
- // use connect or created
273
- init(props) {
274
- // copy original attributes from attr hash to root node
275
- this.copy('href', 'onclick', 'style')
215
+ // called when fez element is connected to dom, before first render
216
+ // here you still have your slot available in this.root
217
+ init(props) { ... }
276
218
 
277
- // set style property to root node. look at a clock example
278
- // shortcut to this.root.style.setProperty(key, value)
279
- this.setStyle('--color', 'red')
219
+ // execute after init and first render
220
+ onMount() { ... }
280
221
 
281
- // clasic interval, that runs only while node is attached
282
- this.setInterval(func, tick) { ... }
222
+ // execute before or after every render
223
+ beforeRender() { ... }
224
+ afterRender() { ... }
283
225
 
284
- // get closest form data, as object
285
- this.formData()
226
+ // if you want to monitor new or changed node attributes
227
+ // monitors all original node attributes
228
+ // <ui-icon name="home" color="red" />
229
+ onPropsChange(attrName, attrValue) { ... }
286
230
 
287
- // mounted DOM node root
288
- this.root
231
+ // called when local component state changes
232
+ onStateChange(key, value, oldValue) { ... }
289
233
 
290
- // mounted DOM node root wrapped in $, only if jQuery is available
291
- this.$root
234
+ // called when global state changes (only if component uses key in question that key)
235
+ onGlobalStateChange(key, value) { ... }
292
236
 
293
- // node attributes, converted to properties
294
- this.props
237
+ // called when component is destroyed
238
+ onDestroy() { ... }
295
239
 
296
- // gets single node attribute or property
297
- this.prop('onclick')
240
+ /* used inside lifecycle methods (init(), onMount(), ... */
298
241
 
299
- // shortcut for this.root.querySelector(selector)
300
- this.find(selector)
242
+ // copy original attributes from attr hash to root node
243
+ this.copy('href', 'onclick', 'style')
301
244
 
302
- // gets value for FORM fields or node innerHTML
303
- this.val(selector)
304
- // set value to a node, uses value or innerHTML
305
- this.val(selector, value)
245
+ // set style property to root node. look at a clock example
246
+ // shortcut to this.root.style.setProperty(key, value)
247
+ this.setStyle('--color', 'red')
306
248
 
307
- // you can publish globally, and subscribe locally
308
- Fez.publish('channel', foo)
309
- this.subscribe('channel', (foo) => { ... })
249
+ // clasic interval, that runs only while node is attached
250
+ this.setInterval(func, tick) { ... }
310
251
 
311
- // gets root childNodes
312
- this.childNodes()
313
- this.childNodes(func) // pass function to loop forEach on selection, removed nodes from DOM
252
+ // get closest form data, as object. Searches for first parent or child FORM element
253
+ this.formData()
314
254
 
315
- // check if the this.root node is attached to dom
316
- this.isConnected()
255
+ // mounted DOM node root. Only in init() point to original <slot /> data, in onMount() to rendered data.
256
+ this.root
317
257
 
318
- // on every "this.state" props change, auto update view.
319
- this.state = this.reactiveStore()
320
- // this.state has reactiveStore() attached by default. any change will trigger this.render()
321
- this.state.foo = 123
258
+ // mounted DOM node root wrapped in $, only if jQuery is available
259
+ this.$root
322
260
 
323
- // window resize event with cleanup
324
- this.onResize(func, delay)
261
+ // node attributes, converted to properties
262
+ this.props
325
263
 
326
- // requestAnimationFrame wrapper with deduplication
327
- this.nextTick(func, name)
264
+ // gets single node attribute or property
265
+ this.prop('onclick')
328
266
 
329
- // get/set unique ID for root node
330
- this.rootId()
267
+ // shortcut for this.root.querySelector(selector)
268
+ this.find(selector)
331
269
 
332
- // get/set attributes on root node
333
- this.attr(name, value)
270
+ // gets value for FORM fields or node innerHTML
271
+ this.val(selector)
272
+ // set value to a node, uses value or innerHTML
273
+ this.val(selector, value)
334
274
 
335
- // hide the custom element wrapper and move children to parent
336
- this.fezHide()
275
+ // you can publish globally, and subscribe locally
276
+ Fez.publish('channel', foo)
277
+ this.subscribe('channel', (foo) => { ... })
337
278
 
338
- // execute after connect and initial component render
339
- this.onMount() { ... } // or this.connected() { ... }
279
+ // gets root childNodes
280
+ this.childNodes()
281
+ this.childNodes(func) // pass function to loop forEach on selection, mask nodes out of position
340
282
 
341
- // execute before or after every render
342
- this.beforeRender() { ... }
343
- this.afterRender() { ... }
283
+ // check if the this.root node is attached to dom
284
+ this.isConnected
344
285
 
345
- // if you want to monitor new or changed node attributes
346
- // monitors all original node attributes
347
- // <ui-icon name="home" color="red" />
348
- this.onPropsChange(attrName, attrValue) { ... }
286
+ // this.state has reactiveStore() attached by default. any change will trigger this.render()
287
+ this.state.foo = 123
349
288
 
350
- // called when local component state changes
351
- this.onStateChange(key, value, oldValue) { ... }
289
+ // generic window event handler with automatic cleanup
290
+ // eventName: 'resize', 'scroll', 'mousemove', etc.
291
+ // delay: throttle delay in ms (default: 200ms)
292
+ this.on(eventName, func, delay)
352
293
 
353
- // called when global state changes (if component reads that key)
354
- this.onGlobalStateChange(key, value) { ... }
294
+ // window resize event with cleanup (shorthand for this.on('resize', func, delay))
295
+ // runs immediately on init and then throttled
296
+ this.onResize(func, delay)
355
297
 
356
- // called when component is destroyed
357
- this.onDestroy() { ... }
298
+ // window scroll event with cleanup (shorthand for this.on('scroll', func, delay))
299
+ // runs immediately on init and then throttled
300
+ this.onScroll(func, delay)
358
301
 
359
- // automatic form submission handling if defined
360
- this.onSubmit(formData) { ... }
302
+ // requestAnimationFrame wrapper with deduplication
303
+ this.nextTick(func, name)
361
304
 
362
- // render template and attach result dom to root. uses Idiomorph for DOM morph
363
- this.render()
305
+ // get unique ID for root node, set one if needed
306
+ this.rootId()
364
307
 
365
- // you can render to another root too
366
- this.render(this.find('.body'), someHtmlTemplate)
367
- }
308
+ // get/set attributes on root node
309
+ this.attr(name, value)
310
+
311
+ // hide the custom element wrapper and move children to parent
312
+ this.fezHide()
313
+
314
+ // automatic form submission handling if there is FORM as parent or child node
315
+ this.onSubmit(formData) { ... }
316
+
317
+ // render template and attach result dom to root. uses Idiomorph for DOM morph
318
+ this.render()
319
+ this.render(this.find('.body'), someHtmlTemplate) // you can render to another root too
368
320
  })
321
+
322
+ /* Utility methods */
323
+
324
+ // define custom style macro
325
+ // Fez.styleMacro('mobile', '@media (max-width: 768px)')
326
+ // :mobile { ... } -> @media (max-width: 768px) { ... }
327
+ Fez.styleMacro(name, value)
328
+
329
+ // add global scss
330
+ Fez.globalCss(`
331
+ .some-class {
332
+ color: red;
333
+ &.foo { ... }
334
+ .foo { ... }
335
+ }
336
+ ...
337
+ `)
338
+
339
+ // internal, get unique ID for a string, poor mans MD5 / SHA1
340
+ Fez.fnv1('some string')
341
+
342
+ // get dom node containing passed html
343
+ Fez.domRoot(htmlData || htmlNode)
344
+
345
+ // activates node by adding class to node, and removing it from siblings
346
+ Fez.activateNode(node, className = 'active')
347
+
348
+ // get generated css class name, from scss source string
349
+ Fez.css(text)
350
+
351
+ // get generated css class name without global attachment
352
+ Fez.cssClass(text)
353
+
354
+ // display information about fast/slow bind components in console
355
+ Fez.info()
356
+
357
+ // low-level DOM morphing function
358
+ Fez.morphdom(target, newNode, opts)
359
+
360
+ // HTML escaping utility
361
+ Fez.htmlEscape(text)
362
+
363
+ // create HTML tags with encoded props
364
+ Fez.tag(tag, opts, html)
365
+
366
+ // execute function until it returns true
367
+ Fez.untilTrue(func, pingRate)
368
+
369
+ // add scripts/styles to document head
370
+ // Load JavaScript from URL: Fez.head({ js: 'path/to/script.js' })
371
+ // Load JavaScript with attributes: Fez.head({ js: 'path/to/script.js', type: 'module', async: true })
372
+ // Load JavaScript with callback: Fez.head({ js: 'path/to/script.js' }, () => console.log('loaded'))
373
+ // Load JavaScript module and auto-import to window: Fez.head({ js: 'path/to/module.js', module: 'MyModule' })
374
+ // Load CSS: Fez.head({ css: 'path/to/styles.css' })
375
+ // Load CSS with attributes: Fez.head({ css: 'path/to/styles.css', media: 'print' })
376
+ // Execute inline script: Fez.head({ script: 'console.log("Hello world")' })
377
+ Fez.head(config, callback)
369
378
  ```
370
379
 
371
380
  ## Fez script loading and definition
@@ -443,11 +452,14 @@ All parts are optional
443
452
  {{block image}}
444
453
  <img src={{ props.src}} />
445
454
  {{/block}}
446
- {{block:image}}<!-- Use the header block -->
447
- {{block:image}}<!-- Use the header block -->
455
+ {{block:image}} <!-- Use the header block -->
456
+ {{block:image}} <!-- Use the header block -->
457
+
458
+ {{raw data}} <!-- unescape HTML -->
459
+ {{json data}} <!-- JSON dump in PRE.json tag -->
448
460
 
449
461
  <!-- fez-this will link DOM node to object property (inspired by Svelte) -->
450
- <!-- this.listRoot -->
462
+ <!-- linkes to -> this.listRoot -->
451
463
  <ul fez-this="listRoot">
452
464
 
453
465
  <!-- when node is added to dom fez-use will call object function by name, and pass current node -->
@@ -456,7 +468,6 @@ All parts are optional
456
468
 
457
469
  <!-- fez-bind for two-way data binding on form elements -->
458
470
  <input type="text" fez-bind="state.username" />
459
- <input onkeyup="fez.list[{{ index }}].name = fez.value" value="{{ name }}" />
460
471
 
461
472
  <!--
462
473
  fez-class for adding classes with optional delay.
@@ -464,6 +475,9 @@ All parts are optional
464
475
  -->
465
476
  <span fez-class="active:100">Delayed class</span>
466
477
 
478
+ <!-- preserve state by key, not affected by state changes-->>
479
+ <p fez-keep="key">...</p>
480
+
467
481
  <!-- :attribute for evaluated attributes (converts to JSON) -->
468
482
  <div :data-config="state.config"></div>
469
483
  </div>
package/bin/fez ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ command = ARGV[0]
6
+ args = ARGV[1..-1]
7
+
8
+ if command.nil? || command == '--help' || command == '-h'
9
+ puts "Usage: fez <command> [<args>]"
10
+ puts ""
11
+ puts "Available commands:"
12
+
13
+ bin_dir = Pathname.new(__FILE__).dirname
14
+ commands = Dir[bin_dir.join("fez-*")].map do |path|
15
+ File.basename(path).sub(/^fez-/, '')
16
+ end.sort
17
+
18
+ commands.each do |cmd|
19
+ puts " #{cmd}"
20
+ end
21
+
22
+ exit 0
23
+ end
24
+
25
+ subcommand_path = File.join(File.dirname(__FILE__), "fez-#{command}")
26
+
27
+ if File.exist?(subcommand_path) && File.executable?(subcommand_path)
28
+ exec(subcommand_path, *args)
29
+ else
30
+ puts "fez: '#{command}' is not a fez command. See 'fez --help'."
31
+ exit 1
32
+ end
package/bin/fez-index ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ require 'json'
5
+
6
+ # Check if argument is provided
7
+ if ARGV.empty?
8
+ puts "Usage: #{$0} <directory>"
9
+ exit 1
10
+ end
11
+
12
+ dir = ARGV[0]
13
+
14
+ # If no * in argument, append /*
15
+ pattern = dir.include?('*') ? dir : File.join(dir, '*')
16
+
17
+ # Check if base directory exists (for error message)
18
+ base_dir = dir.split('*').first.chomp('/')
19
+ if !base_dir.empty? && !File.directory?(base_dir)
20
+ puts "Error: Directory '#{base_dir}' does not exist"
21
+ exit 1
22
+ end
23
+
24
+ # Get files using glob
25
+ files = Dir.glob(pattern).select { |f| File.file?(f) }
26
+
27
+ # Create index for each file
28
+ file_index = []
29
+ files.each do |path|
30
+ stat = File.stat(path)
31
+
32
+ file_info = {
33
+ base: File.basename(path, File.extname(path)),
34
+ path: path,
35
+ size: stat.size.to_i,
36
+ modified: stat.mtime.to_i,
37
+ created: stat.birthtime.to_i
38
+ }
39
+
40
+ file_index << {file: file_info}
41
+ end
42
+
43
+ # Output as JSON to screen
44
+ puts JSON.pretty_generate(file_index)