Skip to content

Commit

Permalink
Add support to replace the turbo_frame contents
Browse files Browse the repository at this point in the history
  • Loading branch information
hajee committed Jul 24, 2024
1 parent 73cb728 commit 1cc6ddc
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '*'
push:
branches:
- main
- '*'

jobs:
prettier:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/standardrb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '*'
push:
branches:
- main
- '*'

jobs:
standard:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ on:
# change this accordingly.
push:
branches:
- main
- '*'

# Run weekly, so that the baseline AppMap artifact does not expire.
schedule:
Expand Down
2 changes: 1 addition & 1 deletion app/assets/builds/@turbo-boost/commands.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions app/assets/builds/@turbo-boost/commands.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/javascript/invoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const parseError = error => {
dispatch(lifecycle.events.clientError, document, { detail: { message, error } }, true)
}

const parseAndRenderResponse = response => {
const parseAndRenderResponse = frameId => response => {
const { strategy } = headers.tokenize(response.headers.get(headers.RESPONSE_HEADER))
response.text().then(content => render(strategy, content))
response.text().then(content => render(strategy, content, frameId))
}

const invoke = (payload = {}) => {
Expand All @@ -21,7 +21,7 @@ const invoke = (payload = {}) => {
headers: headers.prepare({}),
body: JSON.stringify(payload)
})
.then(parseAndRenderResponse)
.then(parseAndRenderResponse(payload.frameId))
.catch(parseError)
} catch (error) {
parseError(error)
Expand Down
18 changes: 17 additions & 1 deletion app/javascript/renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import elements from './elements'

const getElementByIdFromHTMLString = (htmlString, id) => {
var parser = new DOMParser()
var doc = parser.parseFromString(htmlString, 'text/html')
var element = doc.getElementById(id)
return element.outerHTML
}

const frameReplace = (content, frameId) => {
const newFrame = getElementByIdFromHTMLString(content, frameId)
const frame = document.querySelector(`#${frameId}`)
if (frame && newFrame) TurboBoost?.Streams?.morph?.method(frame, newFrame)
}

const append = content => {
document.body.insertAdjacentHTML('beforeend', content)
}
Expand All @@ -14,10 +29,11 @@ const replace = content => {
}

// TODO: dispatch events after append/replace so we can apply page state
export const render = (strategy, content) => {
export const render = (strategy, content, frameId) => {
if (strategy && content) {
if (strategy.match(/^Append$/i)) return append(content)
if (strategy.match(/^Replace$/i)) return replace(content)
if (strategy.match(/^FrameReplace$/i)) return frameReplace(content, frameId)
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/turbo_boost/commands/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def turbo_stream_template_exists?
# Commands support the following redering strategies on the client.
# 1. Replace: The entire page (head, body) is replaced with the new content via morph
# 2. Append: The new content is appended to the body
# 3. FrameReplace: Replace the turbo frame specified in the command with the frame in the content. The content can contain more( e.g. whole content)
def client_render_strategy
# Use the replace strategy if the follow things are true:
#
Expand All @@ -244,6 +245,9 @@ def client_render_strategy
if command_params[:driver] == "window" && controller_action_allowed?
return "Replace" unless turbo_stream_template_exists?
end
if command_params[:driver] == "frame" && controller_action_allowed?
return "FrameReplace" unless turbo_stream_template_exists?
end

"Append"
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/assets/builds/tailwind.css

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand Down

0 comments on commit 1cc6ddc

Please sign in to comment.