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

Fix editor: shift+enter makes single br tag before link and remove br tags from inside a tags #7910

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions decidim-admin/spec/system/admin_manages_organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@
end
end

context "when the admin terms of use content has a link" do
let(:terms_content) do
<<~HTML
<p>foo<br><a href="https://www.decidim.org" rel="noopener noreferrer" target="_blank">link</a></p>
HTML
end
let(:organization) do
create(
:organization,
admin_terms_of_use_body: Decidim::Faker::Localized.localized { terms_content }
)
end

it "creates single br tag" do
find('div[contenteditable="true"].ql-editor').send_keys([:left, :left, :left, :left, :left], [:shift, :enter])
expect(find(
"#organization-admin_terms_of_use_body-tabs-admin_terms_of_use_body-panel-0 .editor .ql-editor"
)["innerHTML"]).to eq('<p>foo<br><br><a href="https://www.decidim.org" rel="noopener noreferrer" target="_blank">link</a></p>')
end

it "doesnt create br tag inside a tag" do
find('div[contenteditable="true"].ql-editor').send_keys([:left, :left, :left, :left], [:shift, :enter])
expect(find(
"#organization-admin_terms_of_use_body-tabs-admin_terms_of_use_body-panel-0 .editor .ql-editor"
)["innerHTML"]).to eq('<p>foo<br><br><a href="https://www.decidim.org" rel="noopener noreferrer" target="_blank">link</a></p>')
end
end

context "when adding br tags to terms of use content" do
let(:another_organization) { create(:organization) }
let(:image) { create(:attachment, attached_to: another_organization) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const Break = Quill.import("blots/break");
const Embed = Quill.import("blots/embed");
const { HistoryOverride } = exports.Decidim.Editor
Quill.register({"modules/history": HistoryOverride}, true)
Quill.register({ "modules/history": HistoryOverride }, true)
let icons = Quill.import("ui/icons");
icons.linebreak = "⏎";

Expand Down Expand Up @@ -72,6 +72,13 @@
}
});

quill.clipboard.addMatcher("BR", (node) => {
if (node?.parentNode?.tagName === "A") {
return new Delta().insert("\n");
}
return new Delta().insert({ "break": "" });
});

addEnterBindings(quill);
backspaceBindingsRangeAny(quill);
backspaceBindings(quill);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,33 @@
const currentLeaf = quill.getLeaf(range.index)[0];
const nextLeaf = quill.getLeaf(range.index + 1)[0];
const previousChar = quill.getText(range.index - 1, 1);
const formats = quill.getFormat(range.index);

if (currentLeaf?.next?.domNode?.tagName === "A" || nextLeaf?.parent?.domNode?.tagName === "A") {
quill.insertEmbed(range.index, "break", true, "user");
quill.removeFormat(range.index, 1, Quill.sources.SILENT)
} else {
quill.insertEmbed(range.index, "break", true, "user");
}

quill.insertEmbed(range.index, "break", true, "user");
quill.formatText(range.index + 1, "bold", true)
if (nextLeaf === null || (currentLeaf.parent !== nextLeaf.parent)) {
if (nextLeaf === null) {
quill.insertEmbed(range.index, "break", true, "user");
} else if (context.offset === 1 && previousChar === "\n") {
const delta = new Delta().retain(range.index).insert("\n");
quill.updateContents(delta, Quill.sources.USER);
}

quill.format(name, context.format[name], Quill.sources.USER);
Object.keys(formats).forEach((format) => {
quill.format(format, context.format[format], Quill.sources.USER);
});
quill.setSelection(range.index + 1, Quill.sources.SILENT);

const lineFormats = getLineFormats(context);
continueFormats(quill, context, lineFormats);
};

const addEnterBindings = (quill) => {
quill.clipboard.addMatcher("BR", () => {
let newDelta = new Delta();
newDelta.insert({"break": ""});
return newDelta;
});

quill.keyboard.addBinding({
key: 13,
shiftKey: true
Expand Down