From 827deecbefc636319d0350b614d85658a43b7d74 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 22 Mar 2024 17:01:36 +0100 Subject: [PATCH 1/5] Remove unused preview_size select listeners They have not been used for a very long time. We do not use `selectBoxIt` for many years now. --- app/views/alchemy/admin/pages/edit.html.erb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/views/alchemy/admin/pages/edit.html.erb b/app/views/alchemy/admin/pages/edit.html.erb index facfe13f38..89b142106b 100644 --- a/app/views/alchemy/admin/pages/edit.html.erb +++ b/app/views/alchemy/admin/pages/edit.html.erb @@ -149,14 +149,6 @@ Alchemy.PageLeaveObserver(); Alchemy.PreviewWindow.init(<%== @preview_urls.first %>); - $('#preview_size').bind('open.selectBoxIt', function (e) { - $('#top_menu').css('z-index', 5000); - }); - - $('#preview_size').bind('blur.selectBoxIt', function (e) { - $('#top_menu').css('z-index', 30); - }); - $('select#preview_size').on('change', function() { var width = this.value; if (width === 'auto') { From c0f808c953845e4122481164ea0712eb6abd0134 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 22 Mar 2024 17:09:43 +0100 Subject: [PATCH 2/5] Extract components into own import --- app/javascript/alchemy_admin.js | 21 +------------------ .../alchemy_admin/components/index.js | 20 ++++++++++++++++++ 2 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 app/javascript/alchemy_admin/components/index.js diff --git a/app/javascript/alchemy_admin.js b/app/javascript/alchemy_admin.js index be1a37835a..896f6d64be 100644 --- a/app/javascript/alchemy_admin.js +++ b/app/javascript/alchemy_admin.js @@ -20,26 +20,7 @@ import Spinner from "alchemy_admin/spinner" import PagePublicationFields from "alchemy_admin/page_publication_fields" // Web Components -import "alchemy_admin/components/button" -import "alchemy_admin/components/char_counter" -import "alchemy_admin/components/clipboard_button" -import "alchemy_admin/components/datepicker" -import "alchemy_admin/components/dialog_link" -import "alchemy_admin/components/element_editor" -import "alchemy_admin/components/elements_window" -import "alchemy_admin/components/message" -import "alchemy_admin/components/growl" -import "alchemy_admin/components/icon" -import "alchemy_admin/components/ingredient_group" -import "alchemy_admin/components/link_buttons" -import "alchemy_admin/components/node_select" -import "alchemy_admin/components/uploader" -import "alchemy_admin/components/overlay" -import "alchemy_admin/components/page_select" -import "alchemy_admin/components/select" -import "alchemy_admin/components/spinner" -import "alchemy_admin/components/tags_autocomplete" -import "alchemy_admin/components/tinymce" +import "alchemy_admin/components" import { setDefaultAnimation } from "shoelace" diff --git a/app/javascript/alchemy_admin/components/index.js b/app/javascript/alchemy_admin/components/index.js new file mode 100644 index 0000000000..851a904d42 --- /dev/null +++ b/app/javascript/alchemy_admin/components/index.js @@ -0,0 +1,20 @@ +import "alchemy_admin/components/button" +import "alchemy_admin/components/char_counter" +import "alchemy_admin/components/clipboard_button" +import "alchemy_admin/components/datepicker" +import "alchemy_admin/components/dialog_link" +import "alchemy_admin/components/element_editor" +import "alchemy_admin/components/elements_window" +import "alchemy_admin/components/message" +import "alchemy_admin/components/growl" +import "alchemy_admin/components/icon" +import "alchemy_admin/components/ingredient_group" +import "alchemy_admin/components/link_buttons" +import "alchemy_admin/components/node_select" +import "alchemy_admin/components/uploader" +import "alchemy_admin/components/overlay" +import "alchemy_admin/components/page_select" +import "alchemy_admin/components/select" +import "alchemy_admin/components/spinner" +import "alchemy_admin/components/tags_autocomplete" +import "alchemy_admin/components/tinymce" From 2df697914c0edad25e78762ef280a668442b4a18 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 22 Mar 2024 19:46:27 +0100 Subject: [PATCH 3/5] Use regular select for Preview URLs select There are no features we need from Select2 for this --- app/views/alchemy/admin/pages/edit.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/alchemy/admin/pages/edit.html.erb b/app/views/alchemy/admin/pages/edit.html.erb index 89b142106b..9c8f217a6f 100644 --- a/app/views/alchemy/admin/pages/edit.html.erb +++ b/app/views/alchemy/admin/pages/edit.html.erb @@ -105,7 +105,7 @@ <%= select_tag 'preview_url', options_for_select(@preview_urls), - class: 'large', is: 'alchemy-select' %> + class: 'large' %> <% end %>
From ad3b01603fa5108fc55052e561642a6bc41798d8 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 22 Mar 2024 19:47:41 +0100 Subject: [PATCH 4/5] Convert Preview Window into custom web component Much nicer to init and handle state this way. --- app/assets/javascripts/alchemy/admin.js | 1 - .../alchemy/alchemy.preview_window.js.coffee | 82 ------------ .../alchemy/admin/pages_controller.rb | 1 + app/javascript/alchemy_admin.js | 4 +- .../components/element_editor.js | 10 +- .../element_editor/publish_element_button.js | 3 +- .../components/elements_window.js | 6 +- .../alchemy_admin/components/index.js | 1 + .../components/preview_window.js | 121 ++++++++++++++++++ .../alchemy_admin/sortable_elements.js | 3 +- app/views/alchemy/admin/pages/edit.html.erb | 17 +-- .../components/element_editor.spec.js | 8 +- .../publish_element_button.spec.js | 12 +- 13 files changed, 168 insertions(+), 101 deletions(-) delete mode 100644 app/assets/javascripts/alchemy/alchemy.preview_window.js.coffee create mode 100644 app/javascript/alchemy_admin/components/preview_window.js diff --git a/app/assets/javascripts/alchemy/admin.js b/app/assets/javascripts/alchemy/admin.js index 98c553cc1a..4ff490c2bb 100644 --- a/app/assets/javascripts/alchemy/admin.js +++ b/app/assets/javascripts/alchemy/admin.js @@ -9,4 +9,3 @@ //= require alchemy/alchemy.fixed_elements //= require alchemy/alchemy.image_overlay //= require alchemy/alchemy.link_dialog -//= require alchemy/alchemy.preview_window diff --git a/app/assets/javascripts/alchemy/alchemy.preview_window.js.coffee b/app/assets/javascripts/alchemy/alchemy.preview_window.js.coffee deleted file mode 100644 index ef495393f5..0000000000 --- a/app/assets/javascripts/alchemy/alchemy.preview_window.js.coffee +++ /dev/null @@ -1,82 +0,0 @@ -window.Alchemy = {} if typeof(window.Alchemy) is 'undefined' - -Alchemy.PreviewWindow = - MIN_WIDTH: 240 - HEIGHT: 75 # Top menu height - - init: (previewUrl) -> - @select = document.querySelector('#preview_url') - @previewUrl = @_getCurrentPreviewUrl() || previewUrl[1] - $iframe = $(" + <%= turbo_frame_tag "alchemy_elements_window", src: alchemy.admin_elements_path(page_version_id: @page_version.id) do %> <% end %> @@ -147,16 +154,6 @@ }); Alchemy.PagePublicationFields(); Alchemy.PageLeaveObserver(); - Alchemy.PreviewWindow.init(<%== @preview_urls.first %>); - - $('select#preview_size').on('change', function() { - var width = this.value; - if (width === 'auto') { - Alchemy.PreviewWindow.currentWindow.css('width', ''); - } else { - Alchemy.PreviewWindow.resize(width); - } - }); }); diff --git a/spec/javascript/alchemy_admin/components/element_editor.spec.js b/spec/javascript/alchemy_admin/components/element_editor.spec.js index d3b5171d4e..f19ad54168 100644 --- a/spec/javascript/alchemy_admin/components/element_editor.spec.js +++ b/spec/javascript/alchemy_admin/components/element_editor.spec.js @@ -178,8 +178,14 @@ describe("alchemy-element-editor", () => { it("focuses element in preview", () => { const click = new Event("click", { bubbles: true }) + const postMessage = jest.fn() + jest.spyOn(editor, "previewWindow", "get").mockImplementation(() => { + return { + postMessage + } + }) editor.dispatchEvent(click) - expect(Alchemy.PreviewWindow.postMessage).toHaveBeenCalledWith({ + expect(postMessage).toHaveBeenCalledWith({ message: "Alchemy.focusElement", element_id: "123" }) diff --git a/spec/javascript/alchemy_admin/components/element_editor/publish_element_button.spec.js b/spec/javascript/alchemy_admin/components/element_editor/publish_element_button.spec.js index 9ee8375031..d8d42a962f 100644 --- a/spec/javascript/alchemy_admin/components/element_editor/publish_element_button.spec.js +++ b/spec/javascript/alchemy_admin/components/element_editor/publish_element_button.spec.js @@ -1,6 +1,16 @@ import "alchemy_admin/components/element_editor/publish_element_button" import { renderComponent } from "../component.helper" +const mockReloadPreview = jest.fn() + +jest.mock("alchemy_admin/components/preview_window", () => { + return { + reloadPreview: () => { + mockReloadPreview() + } + } +}) + jest.mock("alchemy_admin/utils/ajax", () => { return { __esModule: true, @@ -63,7 +73,7 @@ describe("alchemy-publish-element-button", () => { return new Promise((resolve) => { setTimeout(() => { expect(button.tooltip.getAttribute("content")).toEqual("Hide element") - expect(Alchemy.reloadPreview).toHaveBeenCalled() + expect(mockReloadPreview).toHaveBeenCalled() resolve() }, 1) }) From 9de5715f4e3221c73089c666205a3000a0970a80 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 23 Mar 2024 21:55:10 +0100 Subject: [PATCH 5/5] Harden ingredient groups spec The details are sometimes not collapes during test runs. Since the initial state does not matter in this test we can just remove the check. --- spec/features/admin/edit_elements_feature_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/features/admin/edit_elements_feature_spec.rb b/spec/features/admin/edit_elements_feature_spec.rb index cfe54a5825..097bae7c81 100644 --- a/spec/features/admin/edit_elements_feature_spec.rb +++ b/spec/features/admin/edit_elements_feature_spec.rb @@ -156,7 +156,6 @@ before { visit alchemy.edit_admin_page_path(element.page) } scenario "expanded ingredient groups persist between visits", :js do - expect(page).not_to have_selector("#element_#{element.id}_ingredient_group_details", visible: true) page.find("details#element_#{element.id}_ingredient_group_details", text: "Details").click expect(page).to have_selector("#element_#{element.id}_ingredient_group_details", visible: true) visit alchemy.edit_admin_page_path(element.page)