hyper-scope-rails 1.0.0.alpha.1

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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +104 -0
  4. data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js +4 -0
  5. data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js.map +1 -0
  6. data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js +4 -0
  7. data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js.map +1 -0
  8. data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js +4 -0
  9. data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js.map +1 -0
  10. data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js +4 -0
  11. data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js.map +1 -0
  12. data/app/assets/javascripts/hyper-scope/extensions/validate.esm.js +4 -0
  13. data/app/assets/javascripts/hyper-scope/extensions/validate.esm.js.map +1 -0
  14. data/app/assets/javascripts/hyper-scope/extensions/validate.iife.js +4 -0
  15. data/app/assets/javascripts/hyper-scope/extensions/validate.iife.js.map +1 -0
  16. data/app/assets/javascripts/hyper-scope/global.esm.js +4 -0
  17. data/app/assets/javascripts/hyper-scope/global.esm.js.map +1 -0
  18. data/app/assets/javascripts/hyper-scope.esm.js +7 -0
  19. data/app/assets/javascripts/hyper-scope.esm.js.map +1 -0
  20. data/app/assets/javascripts/hyper-scope.iife.js +7 -0
  21. data/app/assets/javascripts/hyper-scope.iife.js.map +1 -0
  22. data/config/importmap.rb +16 -0
  23. data/jsbundle/extensions/turbo-drive.esm.js +25 -0
  24. data/jsbundle/extensions/turbo-frame.esm.js +43 -0
  25. data/jsbundle/extensions/validate.esm.js +182 -0
  26. data/jsbundle/hyper-scope.esm.js +4354 -0
  27. data/lib/hyper-scope-rails.rb +4 -0
  28. data/lib/hyper_scope/rails/engine.rb +59 -0
  29. data/lib/hyper_scope/rails/helper.rb +76 -0
  30. data/lib/hyper_scope/rails/version.rb +9 -0
  31. data/lib/hyper_scope/rails.rb +22 -0
  32. metadata +88 -0
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Bundler requires a gem by its name, so `gem "hyper-scope-rails"` in a Gemfile lands here.
4
+ require "hyper_scope/rails"
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HyperScope
4
+ module Rails
5
+ # Puts the build (app/assets/javascripts/) on the host app's asset path:
6
+ #
7
+ # hyper-scope.iife.js · hyper-scope.esm.js
8
+ # hyper-scope/global.esm.js — the bridge: re-exports the window.HyperScope a bundle published
9
+ # hyper-scope/extensions/{turbo-drive,turbo-frame,validate}.{iife,esm}.js
10
+ # each with its source map (.js.map) — minified files, debuggable as the original TypeScript in devtools
11
+ #
12
+ # Propshaft picks up an engine's app/assets/* on its own; Sprockets needs them in the precompile list.
13
+ # With importmap-rails, the pins in config/importmap.rb are added. Set config.hyper_scope.importmap = :global
14
+ # when the app bundles hyper-scope itself, so "hyper-scope" resolves to the bridge instead of a second runtime.
15
+ class Engine < ::Rails::Engine
16
+ ASSETS = %w[
17
+ hyper-scope.iife.js
18
+ hyper-scope.iife.js.map
19
+ hyper-scope.esm.js
20
+ hyper-scope.esm.js.map
21
+ hyper-scope/global.esm.js
22
+ hyper-scope/global.esm.js.map
23
+ hyper-scope/extensions/turbo-drive.iife.js
24
+ hyper-scope/extensions/turbo-drive.iife.js.map
25
+ hyper-scope/extensions/turbo-drive.esm.js
26
+ hyper-scope/extensions/turbo-drive.esm.js.map
27
+ hyper-scope/extensions/turbo-frame.iife.js
28
+ hyper-scope/extensions/turbo-frame.iife.js.map
29
+ hyper-scope/extensions/turbo-frame.esm.js
30
+ hyper-scope/extensions/turbo-frame.esm.js.map
31
+ hyper-scope/extensions/validate.iife.js
32
+ hyper-scope/extensions/validate.iife.js.map
33
+ hyper-scope/extensions/validate.esm.js
34
+ hyper-scope/extensions/validate.esm.js.map
35
+ ].freeze
36
+
37
+ config.hyper_scope = ActiveSupport::OrderedOptions.new
38
+ # :module — "hyper-scope" is the runtime file. :global — "hyper-scope" is the bridge to window.HyperScope.
39
+ config.hyper_scope.importmap = :module
40
+
41
+ initializer "hyper_scope.assets" do |app|
42
+ if app.config.respond_to?(:assets) && defined?(::Sprockets)
43
+ app.config.assets.precompile += ASSETS
44
+ end
45
+ end
46
+
47
+ initializer "hyper_scope.importmap", before: "importmap" do |app|
48
+ if app.config.respond_to?(:importmap)
49
+ app.config.importmap.paths << root.join("config/importmap.rb")
50
+ app.config.importmap.cache_sweepers << root.join("app/assets/javascripts")
51
+ end
52
+ end
53
+
54
+ initializer "hyper_scope.helper" do
55
+ ActiveSupport.on_load(:action_view) { include HyperScope::Rails::Helper }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "erb"
5
+
6
+ module HyperScope
7
+ module Rails
8
+ # View helpers, included into ActionView by the engine.
9
+ module Helper
10
+ # Server values for a scope's $props, as an attributes hash for tag helpers:
11
+ #
12
+ # <%= tag.div "h-scope": "", **h_props(user: @user.slice(:id, :name), update_path: path) %>
13
+ # <%= tag.div **h_props(@props) %>
14
+ #
15
+ # Keys are written as given — nothing is renamed unless camelize: true (snake_case → camelCase, deeply).
16
+ # nil values are kept, exactly as #to_json writes them.
17
+ def h_props(props = nil, camelize: false, **more)
18
+ { "h-props": HyperScope::Rails::Helper.props_json(props, more, camelize) }
19
+ end
20
+
21
+ # The same as an escaped attribute string, for markup written by hand:
22
+ #
23
+ # <div h-scope <%= h_props_attr(count: 1) %>>
24
+ def h_props_attr(props = nil, camelize: false, **more)
25
+ json = HyperScope::Rails::Helper.props_json(props, more, camelize)
26
+ %(h-props="#{ERB::Util.html_escape(json)}").html_safe
27
+ end
28
+
29
+ # Adds a plugin from an inline script, wherever it sits and whenever the runtime loads: if window.HyperScope is
30
+ # already there the plugin is added at once, otherwise it waits for the runtime's `hyper-scope:init` event.
31
+ # Pass the plugin function as JavaScript source — as an argument or from a block.
32
+ #
33
+ # <%= hyper_scope_plugin_tag "({ define }) => define('today', () => new Date())" %>
34
+ # <%= hyper_scope_plugin_tag nonce: true do %>
35
+ # ({ directive }) => { directive('focus', ({ el }) => el.focus()) }
36
+ # <% end %>
37
+ #
38
+ # nonce: true adds the request's Content-Security-Policy nonce.
39
+ def hyper_scope_plugin_tag(source = nil, nonce: false, &block)
40
+ source = capture(&block) if block
41
+ raise ArgumentError, "hyper_scope_plugin_tag needs the plugin function as JavaScript source" if source.to_s.strip.empty?
42
+
43
+ javascript_tag(HyperScope::Rails::Helper.plugin_script(source.to_s.strip), nonce: nonce)
44
+ end
45
+
46
+ def self.props_json(props, more, camelize)
47
+ hash = (props || {}).to_h.merge(more)
48
+ hash = camelize_keys(hash) if camelize
49
+ hash.to_json
50
+ end
51
+
52
+ def self.camelize_keys(value)
53
+ case value
54
+ when Hash then value.to_h { |k, v| [lower_camel(k.to_s), camelize_keys(v)] }
55
+ when Array then value.map { |v| camelize_keys(v) }
56
+ else value
57
+ end
58
+ end
59
+
60
+ def self.lower_camel(key)
61
+ head, *rest = key.split("_")
62
+ ([head] + rest.map(&:capitalize)).join
63
+ end
64
+
65
+ def self.plugin_script(source)
66
+ <<~JS.strip
67
+ (function (fn) {
68
+ var H = window.HyperScope
69
+ if (H && typeof H.plugin === "function") H.plugin(fn)
70
+ else document.addEventListener("hyper-scope:init", function (e) { fn(e.detail) }, { once: true })
71
+ })(#{source});
72
+ JS
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HyperScope
4
+ module Rails
5
+ # The single source of the gem version. Write prereleases with dots (1.0.0.alpha.1) —
6
+ # RubyGems rewrites a hyphen as ".pre.". A published version can never be reused, so bump it before releasing.
7
+ VERSION = "1.0.0.alpha.1"
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hyper_scope/rails/version"
4
+
5
+ module HyperScope
6
+ module Rails
7
+ # Unminified ES modules for apps that bundle hyper-scope themselves (jsbundling, esbuild, Bun.build):
8
+ #
9
+ # <jsbundle_path>/hyper-scope.esm.js
10
+ # <jsbundle_path>/extensions/{turbo-drive,turbo-frame,validate}.esm.js
11
+ #
12
+ # process.env.NODE_ENV is left in place for the bundler to define. These files are not on the asset path.
13
+ # Without Ruby, the same directory is `$(bundle info --path hyper-scope-rails)/jsbundle`.
14
+ def self.jsbundle_path
15
+ File.expand_path("../../jsbundle", __dir__)
16
+ end
17
+ end
18
+ end
19
+
20
+ require "hyper_scope/rails/helper"
21
+ # Outside Rails (the gem is only required), the engine is not loaded.
22
+ require "hyper_scope/rails/engine" if defined?(::Rails::Engine)
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyper-scope-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.alpha.1
5
+ platform: ruby
6
+ authors:
7
+ - super-loops
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
26
+ description: 'Ships the hyper-scope runtime and its extensions (turbo-drive, turbo-frame,
27
+ validate) as a Rails engine: assets for Propshaft or Sprockets, and importmap pins.
28
+ Write h-* attributes in your ERB and the markup comes alive, through Turbo visits,
29
+ frames and streams.'
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - app/assets/javascripts/hyper-scope.esm.js
37
+ - app/assets/javascripts/hyper-scope.esm.js.map
38
+ - app/assets/javascripts/hyper-scope.iife.js
39
+ - app/assets/javascripts/hyper-scope.iife.js.map
40
+ - app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js
41
+ - app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js.map
42
+ - app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js
43
+ - app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js.map
44
+ - app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js
45
+ - app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js.map
46
+ - app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js
47
+ - app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js.map
48
+ - app/assets/javascripts/hyper-scope/extensions/validate.esm.js
49
+ - app/assets/javascripts/hyper-scope/extensions/validate.esm.js.map
50
+ - app/assets/javascripts/hyper-scope/extensions/validate.iife.js
51
+ - app/assets/javascripts/hyper-scope/extensions/validate.iife.js.map
52
+ - app/assets/javascripts/hyper-scope/global.esm.js
53
+ - app/assets/javascripts/hyper-scope/global.esm.js.map
54
+ - config/importmap.rb
55
+ - jsbundle/extensions/turbo-drive.esm.js
56
+ - jsbundle/extensions/turbo-frame.esm.js
57
+ - jsbundle/extensions/validate.esm.js
58
+ - jsbundle/hyper-scope.esm.js
59
+ - lib/hyper-scope-rails.rb
60
+ - lib/hyper_scope/rails.rb
61
+ - lib/hyper_scope/rails/engine.rb
62
+ - lib/hyper_scope/rails/helper.rb
63
+ - lib/hyper_scope/rails/version.rb
64
+ homepage: https://hyper-scope.super-loops.com
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://hyper-scope.super-loops.com
69
+ documentation_uri: https://hyper-scope.super-loops.com/docs
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '3.1'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.6.9
85
+ specification_version: 4
86
+ summary: hyper-scope for Rails — template-first reactive scopes for server-rendered
87
+ HTML.
88
+ test_files: []