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

feat: Remove line breaks. #34

Merged
merged 1 commit into from
Jan 15, 2025
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pat-content-mirror
==================

A pattern that provides rich dynamic functionality to a textarea. It is used
for comments in the social stream.
A pattern that provides rich dynamic functionality to a textarea. It is used
for single-line text input that should be able to wrap into the next line.
Line breaks are not allowed and removed from the text input.

The main functionality is provided by maintaining a "mirror" element that
is updated every time the main textarea is changed. This element, which
Expand All @@ -12,6 +13,9 @@ the html textarea element. The user interacts with the textarea, mostly by
typing, and the .content-mirror reflects the textarea content, but can also
contain links to other users or tags.

The difference to a contenteditable element is, that a textarea automatically
participates as a form element in form submissions and validation.

The pattern would typically be applied to a textarea element, though it is
not required.
In addition, another element must be present to act as the content mirror.
Expand Down
11 changes: 10 additions & 1 deletion src/pat-content-mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ class Pattern extends BasePattern {
update_mirror(ev) {
const el = ev.target;
const the_mirror = this.target;
const value = el.value;

// Get value and remove line breaks and all vertical whitespace.
// Instead add a single space so that separated lines are not glued
// together.
const value = el.value.replace(/[\r\n\v\f]+/g, " ");

// Write back the cleaned value to the textearea.
el.value = value;

// Write value (or placeholder) to the mirror.
the_mirror.textContent = value;
if (!value) {
const placeholder = this.el.getAttribute("placeholder");
Expand Down
20 changes: 20 additions & 0 deletions src/pat-content-mirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,24 @@ describe("pat-content-mirror", () => {
expect(mirror2.textContent).toBe("placeholder 2");
});

it("Removes line breaks.", async () => {
document.body.innerHTML = `
<section class="the-mirror"></section>
<textarea
class="pat-content-mirror"
data-pat-content-mirror="target:.the-mirror"></textarea>
`;

const instance = new Pattern(document.querySelector(".pat-content-mirror"));
await events.await_pattern_init(instance);

const textarea = document.querySelector("textarea");
textarea.value = "line1\nline2\rline3\r\nline4\n\rline5\vline7\fend";
textarea.dispatchEvent(new Event("input"));

expect(document.querySelector(".the-mirror").textContent).toBe(
"line1 line2 line3 line4 line5 line7 end"
);
});

});
Loading