rails_command_palette 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ca48265d126c8cc7ab903d30a2f310b0e2208c8e000fd3ef836100f190dd185
4
- data.tar.gz: 20591c07d75d52373423d684f603024d7df589babd50ca121ca47f4652782ef1
3
+ metadata.gz: 683e84e27b724804149e4b7a31953f73e1f38ba308d2cf9123edead6846f5bd8
4
+ data.tar.gz: a7a16641eb989cf6306bd3899a4f82009d2f9a6309323d1e0dcbeb2305db0aef
5
5
  SHA512:
6
- metadata.gz: 3a6b2de6b696b99b83b66a1b5d077baa29d5202172e88e39bdd1c7580049365f7850b034cfe9a9258aa44066b4fac52132eefb9caaa9581a5aa1a1ba2efd3ada
7
- data.tar.gz: aafb6d9089af23a396cfc5aaf8d01eee6ea78279f1e9ad257c0b5c0739804b4c8bfc22a8afe3bf27725be83607a6202d4353dd2ef91daf5431c214a4c535e7e0
6
+ metadata.gz: 8a53fea2aef2c10652db8abd9764f65e91d088ffc7de62884416212d79c7380ec472d0debc58dc2f1502f94493767057084bb21516e010313774c80f24f31771
7
+ data.tar.gz: e9ff20dc4fd7ced37533fbf35795f74d63ede12afba0168f75f48d890013525f1877e68efd0a02ee3cf85b37b845bcaba5c6c3f1017116f002393a1d1deea3f3
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2023 Alexander C. Mingoia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # rails_command_palette
2
+
3
+ A command palette for Rails using Stimulus and [Ninja Keys](https://github.com/ssleptsov/ninja-keys).
4
+
5
+ ## Install
6
+
7
+ Install the gem:
8
+
9
+ bundle add rails_command_palette
10
+
11
+ Install Ninja Keys:
12
+
13
+ yarn add --dev ninja-keys
14
+
15
+ Install the Stimulus controller:
16
+
17
+ rails g command_palette:install
18
+
19
+ ## Usage
20
+
21
+ ### Controller
22
+
23
+ Add commands using `add_command(id:, title:, href: nil, frame: nil, hotkey: nil, icon: nil, section: nil, parent: nil, children: nil, **options)`.
24
+
25
+ `frame` is used when calling `Turbo.visit` for commands with `href`. `icon` accepts HTML.
26
+
27
+ ### View
28
+
29
+ Render the command palette in your layout using `<%= command_palette %>`
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ module CommandPalette
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+ desc "This generator installs the javascript needed for rails_command_palette."
6
+
7
+ def copy_the_javascript
8
+ copy_file "command_palette_controller.js", "app/javascript/controllers/command_palette_controller.js"
9
+ if (Rails.root.join("app/javascript/controllers/index.js")).exist?
10
+ append_to_file "app/javascript/controllers/index.js",
11
+ %(import CommandPaletteController from "./command_palette_controller"\napplication.register("command-palette", CommandPaletteController)\n)
12
+ else
13
+ say %(Couldn't find "app/javascript/controllers/index.js".), :red
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import "ninja-keys"
3
+
4
+ export default class CommandPaletteController extends Controller {
5
+ static values = {
6
+ commands: Array
7
+ }
8
+
9
+ connect() {
10
+ this.close = this.close.bind(this)
11
+ this.handleChange = this.handleChange.bind(this)
12
+ this.handleSelected = this.handleSelected.bind(this)
13
+ this.element.data = this.commandsValue.map((cmd) => Object.assign(cmd, {
14
+ handler: !cmd.children && this.handleSelected.bind(this)
15
+ }))
16
+ this.element.addEventListener("change", this.handleChange)
17
+ this.element.addEventListener("selected", this.handleSelected)
18
+ }
19
+
20
+ // @param ev Event `{ detail: { search: string } }`
21
+ handleChange(ev) {}
22
+
23
+ handleSelected({ frame, href }) {
24
+ if (href) {
25
+ document.removeEventListener("turbo:load", this.close)
26
+ document.removeEventListener("turbo:frame-load", this.close)
27
+ document.addEventListener("turbo:load", this.close, { once: true })
28
+ document.addEventListener("turbo:frame-load", this.close, { once: true })
29
+
30
+ Turbo.visit(href, { frame: frame })
31
+ }
32
+ }
33
+
34
+ get commands() {
35
+ return this.element.data
36
+ }
37
+
38
+ open(opts) {
39
+ this.element.open(opts)
40
+ }
41
+
42
+ close() {
43
+ this.element.close()
44
+ }
45
+ }
@@ -0,0 +1,34 @@
1
+ module RailsCommandPalette
2
+ module ActionController
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ helper HelperMethods
7
+ helper_method :add_command, :prepend_command, :commands
8
+
9
+ unless base.respond_to?(:before_action)
10
+ base.alias_method :before_action, :before_filter
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ def commands
17
+ @command_palette_commands ||= []
18
+ end
19
+
20
+ def add_command(id:, title:, href: nil, frame: nil, hotkey: nil, icon: nil, section: nil, parent: nil, children: nil, **options)
21
+ commands << {id:, title:, href:, frame:, hotkey:, icon:, section:, parent:, children:, options:}
22
+ end
23
+
24
+ def prepend_command(id:, title:, href: nil, frame: nil, hotkey: nil, icon: nil, section: nil, parent: nil, children: nil, **options)
25
+ commands.unshift({id:, title:, href:, frame:, hotkey:, icon:, section:, parent:, children:, options:})
26
+ end
27
+
28
+ module HelperMethods
29
+ def command_palette(options = {})
30
+ CommandPalette.new(commands, options).to_html
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ module RailsCommandPalette
2
+ class CommandPalette
3
+ include ActionView::Helpers::TagHelper
4
+
5
+ def initialize(commands, options = {})
6
+ @commands = flatten_commands(commands)
7
+ @options = options
8
+ end
9
+
10
+ def flatten_commands(commands, flattened = [])
11
+ commands.each do |cmd|
12
+ flattened << cmd
13
+ if cmd[:children].present?
14
+ flatten_commands(cmd[:children], flattened)
15
+ cmd[:children] = cmd[:children].map do |childCmd|
16
+ childCmd[:parent] = cmd[:id]
17
+ childCmd[:id]
18
+ end
19
+ end
20
+ end
21
+ flattened
22
+ end
23
+
24
+ def to_html
25
+ tag("ninja-keys", @options.merge(
26
+ data: {
27
+ controller: "command-palette",
28
+ command_palette_commands_value: @commands
29
+ }
30
+ ))
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module RailsCommandPalette
2
+ class Railtie < Rails::Railtie
3
+ ActiveSupport.on_load(:action_controller) do
4
+ ::ActionController::Base.send(:include, RailsCommandPalette::ActionController)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RailsCommandPalette
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "rails_command_palette/version"
2
+ require "rails_command_palette/command_palette.rb"
3
+ require "rails_command_palette/action_controller.rb"
4
+ require "rails_command_palette/railtie.rb"
5
+
6
+ module RailsCommandPalette
7
+ NAME = "Rails command palette"
8
+ GEM = "rails_command_palette"
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_command_palette
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexmingoia
@@ -70,7 +70,16 @@ email:
70
70
  executables: []
71
71
  extensions: []
72
72
  extra_rdoc_files: []
73
- files: []
73
+ files:
74
+ - LICENSE
75
+ - README.md
76
+ - lib/generators/command_palette/install_generator.rb
77
+ - lib/generators/command_palette/templates/command_palette_controller.js
78
+ - lib/rails_command_palette.rb
79
+ - lib/rails_command_palette/action_controller.rb
80
+ - lib/rails_command_palette/command_palette.rb
81
+ - lib/rails_command_palette/railtie.rb
82
+ - lib/rails_command_palette/version.rb
74
83
  homepage: https://github.com/alexmingoia/rails_command_palette
75
84
  licenses:
76
85
  - MIT