Class: Domternal::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/domternal/install/install_generator.rb

Overview

rails g domternal:install

Detects whether the host app uses importmap-rails or a Node/Bun bundler (jsbundling-rails, vite_rails, ...) and wires up the JS accordingly, mirroring ActionText's install generator (lib/generators/action_text/install/install_generator.rb).

Constant Summary collapse

CORE_PACKAGE =

@domternal/vanilla and every @domternal/extension-* package declare @domternal/core (and, transitively, @domternal/pm — the ProseMirror re-export used for plugin identity) as peer dependencies. esm.sh only emits real import statements for a peer (rather than inlining its own private copy) when it's passed via ?external=, so every consumer below must externalize the same set — otherwise two different copies of ProseMirror's Plugin/PluginKey classes end up on the page and construction fails with "Adding different instances of a keyed plugin". @domternal/pm's own subpaths (state, view, model, ...) are covered by a single prefix pin instead of being listed one by one.

"@domternal/core"
PM_PACKAGE =
"@domternal/pm"
NPM_PACKAGES =
{
  CORE_PACKAGE => nil,
  "@domternal/vanilla" => [CORE_PACKAGE],
  "@domternal/extension-image" => [CORE_PACKAGE, PM_PACKAGE]
}.freeze
OPTIONAL_NPM_PACKAGES =
%w[
  @domternal/extension-table @domternal/extension-mention @domternal/extension-markdown
  @domternal/extension-emoji @domternal/extension-math @domternal/extension-code-block-lowlight
  @domternal/extension-toc @domternal/extension-block-controls @domternal/extension-details
].index_with { [CORE_PACKAGE, PM_PACKAGE] }.freeze

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



48
49
50
# File 'lib/generators/domternal/install/install_generator.rb', line 48

def create_initializer
  template "initializer.rb", "config/initializers/domternal.rb"
end

#create_migrationsObject



62
63
64
# File 'lib/generators/domternal/install/install_generator.rb', line 62

def create_migrations
  rails_command "railties:install:migrations FROM=active_storage,domternal", inline: true
end

#create_stylesheetObject



52
53
54
55
56
57
58
59
60
# File 'lib/generators/domternal/install/install_generator.rb', line 52

def create_stylesheet
  template "domternal.css.erb", "app/assets/stylesheets/domternal.css"
  say <<~INSTRUCTIONS, :green
    Add `<%= stylesheet_link_tag "domternal" %>` to your layout (next to your
    application stylesheet tag) to load the Domternal theme and editor styles.
  INSTRUCTIONS

  create_content_stylesheet
end

#show_readmeObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/generators/domternal/install/install_generator.rb', line 79

def show_readme
  say <<~INSTRUCTIONS, :green

    Domternal is installed. Next steps:

      1. bin/rails db:migrate
      2. In a model:      has_rich_domternal :body
      3. In a form:       <%= form.domternal_area :body %>
      4. To render:       <%= domternal_content @post.body %>
  INSTRUCTIONS
end

#wire_up_javascriptObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/domternal/install/install_generator.rb', line 66

def wire_up_javascript
  if using_importmap?
    wire_up_importmap
  elsif using_jsbundling?
    wire_up_jsbundling
  else
    say <<~INSTRUCTIONS, :yellow
      Could not detect importmap-rails or a package.json — add the Domternal JS
      yourself. See #{__dir__}/../../../../README.md for both approaches.
    INSTRUCTIONS
  end
end