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

(Re)-init Tinymce for elements with ingredients #2163

Merged
merged 1 commit into from
Aug 6, 2021
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
1 change: 1 addition & 0 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def update
end

def destroy
@richtext_ids = @element.richtext_contents_ids + @element.richtext_ingredients_ids
@element.destroy
@notice = Alchemy.t("Successfully deleted element") % { element: @element.display_name }
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/elements/create.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

Alchemy.growl('<%= Alchemy.t(:successfully_added_element) %>');
Alchemy.closeCurrentDialog();
Alchemy.Tinymce.init(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.init(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);
Alchemy.PreviewWindow.refresh(function() {
Alchemy.ElementEditors.focusElementPreview(<%= @element.id %>);
});
Expand Down
4 changes: 1 addition & 3 deletions app/views/alchemy/admin/elements/destroy.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ $('#element_<%= @element.id %>').hide(200, function() {
Alchemy.growl('<%= j @notice %>');
$('#element_area .sortable-elements').sortable('refresh');
Alchemy.PreviewWindow.refresh();
<% @element.richtext_contents_ids.each do |id| %>
tinymce.get('tinymce_<%= id %>').remove();
<% end %>
Alchemy.Tinymce.remove(<%= @richtext_ids.to_json %>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

<% if @element.fixed? %>
Alchemy.FixedElements.removeTab(<%= @element.id %>);
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/alchemy/admin/elements/fold.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

<% if @element.folded? -%>

Alchemy.Tinymce.remove(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.remove(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);

<% else -%>

$el.trigger('FocusElementEditor.Alchemy');
Alchemy.Tinymce.init(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.init(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);
Alchemy.GUI.initElement($el);
Alchemy.SortableElements(
<%= @page.id %>,
Expand Down
52 changes: 52 additions & 0 deletions spec/requests/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::Admin::ElementsController do
before do
authorize_user(:as_admin)
end

describe "#create" do
let(:page_version) { create(:alchemy_page_version) }

context "element with ingredients" do
it "inits Tinymce for richtext ingredients" do
post admin_elements_path(element: { page_version_id: page_version.id, name: "element_with_ingredients" }, format: :js)
element = Alchemy::Element.last
expect(response.body).to include("Alchemy.Tinymce.init([#{element.ingredient_by_role(:text).id}]);")
end
end
end

describe "#fold" do
context "expanded element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients) }

it "removes Tinymce for richtext ingredients" do
post fold_admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.remove([#{element.ingredient_by_role(:text).id}]);")
end
end

context "folded element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients, folded: true) }

it "inits Tinymce for richtext ingredients" do
post fold_admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.init([#{element.ingredient_by_role(:text).id}]);")
end
end
end

describe "#destroy" do
context "element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients) }

it "removes Tinymce for richtext ingredients" do
delete admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.remove([#{element.ingredient_by_role(:text).id}]);")
end
end
end
end