Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor style tweaks #46

Merged
merged 8 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ You can set configuration options on your `development.rb`. For example:
config.hotwire.spark.html_paths += %w[ lib ]
```

| Name | Description |
|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `html_paths` | Paths where file changes trigger a content refresh. By default: `app/controllers`, `app/helpers`, `app/models`, `app/views`. |
| `css_paths` | Paths where file changes trigger a CSS refresh. By default: `app/assets/stylesheets` or `app/assets/builds` if exists. |
| `stimulus_paths` | Paths where file changes trigger a Stimulus controller refresh. By default: `app/javascript/controllers`. |
| `enabled` | Enable or disable live reloading. By default, it's only enabled in `development`. |
| `logging` | Show logs in the browser console when reloading happens. It's false by default. |
| `html_reload_method` | Reload html by morphing (`"morph"`) or with a Turbo page reload (`"replace"`). By default `"morph"`. Use `"replace"` if your app is incompatible with morphing. |
| Name | Description |
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `html_paths` | Paths where file changes trigger a content refresh. By default: `app/controllers`, `app/helpers`, `app/models`, `app/views`. |
| `css_paths` | Paths where file changes trigger a CSS refresh. By default: `app/assets/stylesheets` or `app/assets/builds` if exists. |
| `stimulus_paths` | Paths where file changes trigger a Stimulus controller refresh. By default: `app/javascript/controllers`. |
| `enabled` | Enable or disable live reloading. By default, it's only enabled in `development`. |
| `logging` | Show logs in the browser console when reloading happens. It's false by default. |
| `html_reload_method` | How to perform reloads when HTML contents changes: `:morph` or `:replace`. By default, it is `:morph` and it will morph the `<body>` of the page. Set to `:replace` to reload the page with a regular Turbo navigation that will replace the `<body>`. |

## License

Expand Down
37 changes: 23 additions & 14 deletions app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,10 +1475,10 @@ var HotwireSpark = (function () {
return new MorphHtmlReloader().reload();
}
async reload() {
const reloadedDocument = await this.#reloadWithMorph();
const reloadedDocument = await this.#reloadHtml();
await this.#reloadStimulus(reloadedDocument);
}
async #reloadWithMorph() {
async #reloadHtml() {
log("Reload html with morph...");
const reloadedDocument = await reloadHtmlDocument();
this.#updateBody(reloadedDocument.body);
Expand Down Expand Up @@ -1553,17 +1553,12 @@ var HotwireSpark = (function () {
return new ReplaceHtmlReloader().reload();
}
async reload() {
await this.#reloadWithTurbo();
await this.#reloadHtml();
}
#reloadWithTurbo() {
async #reloadHtml() {
log("Reload html with Turbo...");
this.#maintainScrollPosition();
return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), {
once: true
});
window.Turbo.visit(window.location);
});
await this.#visitCurrentPage();
}
#maintainScrollPosition() {
document.addEventListener("turbo:render", () => {
Expand All @@ -1572,6 +1567,14 @@ var HotwireSpark = (function () {
once: true
});
}
#visitCurrentPage() {
return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), {
once: true
});
window.Turbo.visit(window.location);
});
}
}

consumer.subscriptions.create({
Expand Down Expand Up @@ -1605,8 +1608,8 @@ var HotwireSpark = (function () {
}
},
reloadHtml() {
const HtmlReloader = HotwireSpark.config.htmlReloadMethod == "morph" ? MorphHtmlReloader : ReplaceHtmlReloader;
return HtmlReloader.reload();
const htmlReloader = HotwireSpark.config.htmlReloadMethod == "morph" ? MorphHtmlReloader : ReplaceHtmlReloader;
return htmlReloader.reload();
},
reloadCss(fileName) {
return CssReloader.reload(new RegExp(fileName));
Expand All @@ -1622,9 +1625,15 @@ var HotwireSpark = (function () {
htmlReloadMethod: "morph"
}
};
const configProperties = {
loggingEnabled: "logging",
htmlReloadMethod: "html-reload-method"
};
document.addEventListener("DOMContentLoaded", function () {
HotwireSpark$1.config.loggingEnabled = getConfigurationProperty("logging");
HotwireSpark$1.config.htmlReloadMethod = getConfigurationProperty("html-reload-method");
Object.entries(configProperties).forEach(_ref => {
let [key, property] = _ref;
HotwireSpark$1.config[key] = getConfigurationProperty(property);
});
});

return HotwireSpark$1;
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js.map

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions app/javascript/hotwire/spark/channels/monitoring_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ consumer.subscriptions.create({ channel: "Hotwire::Spark::Channel" }, {
},

reloadHtml() {
const HtmlReloader = HotwireSpark.config.htmlReloadMethod == "morph"
? MorphHtmlReloader
: ReplaceHtmlReloader
return HtmlReloader.reload()
const htmlReloader = HotwireSpark.config.htmlReloadMethod == "morph" ? MorphHtmlReloader : ReplaceHtmlReloader
return htmlReloader.reload()
},

reloadCss(fileName) {
Expand Down
10 changes: 8 additions & 2 deletions app/javascript/hotwire/spark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ const HotwireSpark = {
}
}

const configProperties = {
loggingEnabled: "logging",
htmlReloadMethod: "html-reload-method",
}

document.addEventListener("DOMContentLoaded", function() {
HotwireSpark.config.loggingEnabled = getConfigurationProperty("logging");
HotwireSpark.config.htmlReloadMethod = getConfigurationProperty("html-reload-method");
Object.entries(configProperties).forEach(([key, property]) => {
HotwireSpark.config[key] = getConfigurationProperty(property)
})
})

export default HotwireSpark
4 changes: 2 additions & 2 deletions app/javascript/hotwire/spark/reloaders/morph_html_reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class MorphHtmlReloader {
}

async reload() {
const reloadedDocument = await this.#reloadWithMorph()
const reloadedDocument = await this.#reloadHtml()
await this.#reloadStimulus(reloadedDocument)
}

async #reloadWithMorph() {
async #reloadHtml() {
log("Reload html with morph...")

const reloadedDocument = await reloadHtmlDocument()
Expand Down
17 changes: 10 additions & 7 deletions app/javascript/hotwire/spark/reloaders/replace_html_reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ export class ReplaceHtmlReloader {
}

async reload() {
await this.#reloadWithTurbo()
await this.#reloadHtml()
}

#reloadWithTurbo() {
async #reloadHtml() {
log("Reload html with Turbo...")

this.#maintainScrollPosition()

return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), { once: true })
window.Turbo.visit(window.location)
})
await this.#visitCurrentPage()
}

#maintainScrollPosition() {
document.addEventListener("turbo:render", () => {
Turbo.navigator.currentVisit.scrolled = true
}, { once: true })
}

#visitCurrentPage() {
return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), { once: true })
window.Turbo.visit(window.location)
})
}
}
6 changes: 1 addition & 5 deletions test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
<%= javascript_importmap_tags %>

<script>
let firstLoad = true
document.addEventListener("turbo:load", (event) => {
if (!firstLoad) {
document.body.setAttribute("data-turbo-navigated", "")
}
firstLoad = false
document.body.setAttribute("data-turbo-navigated", "")
})
</script>
</head>
Expand Down
2 changes: 0 additions & 2 deletions test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@

# Raise error when a before_action's only/except options reference missing actions.
config.action_controller.raise_on_missing_callback_actions = true

config.middleware.use Hotwire::Spark::Middleware
config.hotwire.spark.enabled = true
end
4 changes: 2 additions & 2 deletions test/morph_reload_test.rb → test/morph_html_reload_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "application_system_test_case"

class MorphReloadTest < ApplicationSystemTestCase
class MorphHtmlReloadTest < ApplicationSystemTestCase
setup do
Hotwire::Spark.html_reload_method = "morph"
Hotwire::Spark.html_reload_method = :morph
end

test "reload HTML changes" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "application_system_test_case"

class ReplaceReloadTest < ApplicationSystemTestCase
class ReplaceHtmlReloadTest < ApplicationSystemTestCase
setup do
Hotwire::Spark.html_reload_method = "replace"
Hotwire::Spark.html_reload_method = :replace
end

test "reload HTML changes" do
Expand Down
Loading