From cd83d918e593d45128574a79036ed5df0ea48216 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Wed, 24 Sep 2025 15:45:04 +0300 Subject: [PATCH] fix csp --- app/controllers/application_controller.rb | 2 +- app/javascript/application.js | 5 ++ app/javascript/elements/autosize_field.js | 21 ++++++++ app/javascript/lib/turbo_instant_click.js | 2 +- .../devise/shared/_select_server.html.erb | 52 ------------------- app/views/layouts/application.html.erb | 2 +- app/views/scripts/_server_selector.html.erb | 26 ---------- app/views/shared/_body_scripts.html.erb | 0 app/views/submissions/show.html.erb | 4 +- 9 files changed, 32 insertions(+), 82 deletions(-) create mode 100644 app/javascript/elements/autosize_field.js delete mode 100644 app/views/scripts/_server_selector.html.erb create mode 100644 app/views/shared/_body_scripts.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 843ef605..0ba4a117 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,7 +13,7 @@ class ApplicationController < ActionController::Base before_action :maybe_redirect_to_setup, unless: :signed_in? before_action :authenticate_user!, unless: :devise_controller? - before_action :set_csp, if: -> { request.get? && !turbo_frame_request? && !request.headers['HTTP_VND.PREFETCH'] } + before_action :set_csp, if: -> { request.get? && !request.headers['HTTP_X_TURBO'] } helper_method :button_title, :current_account, diff --git a/app/javascript/application.js b/app/javascript/application.js index b9467861..1a1ee2a5 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -47,6 +47,7 @@ import ReviewForm from './elements/review_form' import ShowOnValue from './elements/show_on_value' import CustomValidation from './elements/custom_validation' import ToggleClasses from './elements/toggle_classes' +import AutosizeField from './elements/autosize_field' import * as TurboInstantClick from './lib/turbo_instant_click' @@ -63,6 +64,9 @@ document.addEventListener('keyup', (e) => { }) document.addEventListener('turbo:before-fetch-request', encodeMethodIntoRequestBody) +document.addEventListener('turbo:before-fetch-request', (event) => { + event.detail.fetchOptions.headers['X-Turbo'] = 'true' +}) document.addEventListener('turbo:submit-end', async (event) => { const resp = event.detail?.formSubmission?.result?.fetchResponse?.response @@ -129,6 +133,7 @@ safeRegisterElement('review-form', ReviewForm) safeRegisterElement('show-on-value', ShowOnValue) safeRegisterElement('custom-validation', CustomValidation) safeRegisterElement('toggle-classes', ToggleClasses) +safeRegisterElement('autosize-field', AutosizeField) safeRegisterElement('template-builder', class extends HTMLElement { connectedCallback () { diff --git a/app/javascript/elements/autosize_field.js b/app/javascript/elements/autosize_field.js new file mode 100644 index 00000000..b6f56763 --- /dev/null +++ b/app/javascript/elements/autosize_field.js @@ -0,0 +1,21 @@ +export default class extends HTMLElement { + connectedCallback () { + const originalFontValue = this.field.style.fontSize + + if (this.field.scrollHeight > this.field.clientHeight) { + this.field.style.fontSize = `calc(${originalFontValue} / 1.5)` + this.field.style.lineHeight = `calc(${this.field.style.fontSize} * 1.3)` + + if (this.field.scrollHeight > this.field.clientHeight) { + this.field.style.fontSize = `calc(${originalFontValue} / 2.0)` + this.field.style.lineHeight = `calc(${this.field.style.fontSize} * 1.3)` + } + } + + this.field.classList.remove('hidden') + } + + get field () { + return this.closest('field-value') + } +} diff --git a/app/javascript/lib/turbo_instant_click.js b/app/javascript/lib/turbo_instant_click.js index 01789e95..d72d4ce8 100644 --- a/app/javascript/lib/turbo_instant_click.js +++ b/app/javascript/lib/turbo_instant_click.js @@ -56,7 +56,7 @@ function mouseoverListener (event) { const requestOptions = { credentials: 'same-origin', - headers: { Accept: 'text/html, application/xhtml+xml', 'VND.PREFETCH': 'true' }, + headers: { Accept: 'text/html, application/xhtml+xml', 'VND.PREFETCH': 'true', 'X-Turbo': 'true' }, method: 'GET', redirect: 'follow' } diff --git a/app/views/devise/shared/_select_server.html.erb b/app/views/devise/shared/_select_server.html.erb index 58b1dbec..e69de29b 100644 --- a/app/views/devise/shared/_select_server.html.erb +++ b/app/views/devise/shared/_select_server.html.erb @@ -1,52 +0,0 @@ -<% eu_server = request.host == 'docuseal.eu' %> - - -
- -
-
-<%= render 'scripts/server_selector' %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 60c6222e..0dee5392 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,7 +18,6 @@ <% end %> <%= stylesheet_pack_tag 'application', media: 'all' %> - <%= render 'shared/plausible' if !signed_in? && ENV['PLAUSIBLE_DOMAIN'] %> @@ -28,5 +27,6 @@
<%= yield %>
+ <%= render 'shared/body_scripts' %> diff --git a/app/views/scripts/_server_selector.html.erb b/app/views/scripts/_server_selector.html.erb deleted file mode 100644 index c67da29b..00000000 --- a/app/views/scripts/_server_selector.html.erb +++ /dev/null @@ -1,26 +0,0 @@ - diff --git a/app/views/shared/_body_scripts.html.erb b/app/views/shared/_body_scripts.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/app/views/submissions/show.html.erb b/app/views/submissions/show.html.erb index 0c1d897f..dbd75575 100644 --- a/app/views/submissions/show.html.erb +++ b/app/views/submissions/show.html.erb @@ -302,4 +302,6 @@ -<%= render 'scripts/autosize_field' %> +<% unless request.headers['HTTP_X_TURBO'] %> + <%= render 'scripts/autosize_field' %> +<% end %>