Skip to content

Commit 78c3b67

Browse files
committed
maint(pat-content-mirror): Rework for class based pattern.
Note: the event binding on the propertychange event is removed as this dates back to IE times and is not supported by any modern browser.
1 parent 597a66e commit 78c3b67

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"main": "./src/pat-content-mirror.js",
77
"dependencies": {
8-
"@patternslib/patternslib": "*",
8+
"@patternslib/patternslib": ">=9.10.1-alpha.0",
99
"jquery": "^3.6.0"
1010
},
1111
"devDependencies": {

src/pat-content-mirror.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
1-
import $ from "jquery";
2-
import Base from "@patternslib/patternslib/src/core/base";
1+
import { BasePattern } from "@patternslib/patternslib/src/core/basepattern";
2+
import events from "@patternslib/patternslib/src/core/events";
33
import Parser from "@patternslib/patternslib/src/core/parser";
4+
import registry from "@patternslib/patternslib/src/core/registry";
45

56
const parser = new Parser("content-mirror");
67
parser.addArgument("target", "p.content-mirror .text");
78

8-
export default Base.extend({
9-
name: "content-mirror",
10-
trigger: ".pat-content-mirror",
9+
class Pattern extends BasePattern {
10+
static name = "content-mirror";
11+
static trigger = ".pat-content-mirror";
12+
static parser = parser;
1113

12-
init: function content_mirror_init($el) {
13-
this.options = parser.parse(this.el, this.options);
14-
const $mirror = $(this.options.target).parents("p.content-mirror").first();
15-
$el.on(
16-
"input propertychange",
17-
$.proxy(this.updateMirror, this, this.options.target)
14+
async init() {
15+
this.target = document.querySelector(this.options.target);
16+
events.add_event_listener(
17+
this.el,
18+
"input",
19+
"pat-content-mirror--update-mirror",
20+
this.update_mirror.bind(this)
1821
);
19-
$el.parents("form")
20-
.first()
21-
.on("reset", function () {
22-
$el.val("");
23-
$mirror.html($mirror.html());
24-
});
25-
$(".placeholder", this.options.target).text($el.attr("placeholder") || "");
26-
},
27-
28-
updateMirror: function updateMirror(target, ev) {
29-
const $el = $(ev.target);
30-
const the_mirror = $(target);
31-
the_mirror.text($el.val());
32-
if (!$el.val().length) {
33-
const placeholder = $el.attr("placeholder");
22+
23+
const form = this.el.form || this.el.closest("form");
24+
events.add_event_listener(
25+
form,
26+
"reset",
27+
`pat-content-mirror--reset--${this.uuid}`,
28+
() => {
29+
this.el.value = "";
30+
this.el.dispatchEvent(events.input_event());
31+
}
32+
);
33+
34+
const placeholder = this.target.querySelector(".placeholder");
35+
if (placeholder) {
36+
placeholder.textContent = this.el.getAttribute("placeholder") || "";
37+
}
38+
}
39+
40+
update_mirror(ev) {
41+
const el = ev.target;
42+
const the_mirror = this.target;
43+
const value = el.value;
44+
the_mirror.textContent = value;
45+
if (!value) {
46+
const placeholder = this.el.getAttribute("placeholder");
3447
if (placeholder) {
35-
the_mirror.html('<em class="placeholder">' + placeholder + "</em>");
48+
the_mirror.innerHTML = `<em class="placeholder">${placeholder}</em>`;
3649
}
3750
}
38-
},
39-
});
51+
}
52+
}
53+
54+
registry.register(Pattern);
55+
56+
export default Pattern;
57+
export { Pattern };

src/pat-content-mirror.test.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import pattern from "./pat-content-mirror";
2-
import utils from "@patternslib/patternslib/src/core/utils";
1+
import Pattern from "./pat-content-mirror";
2+
import events from "@patternslib/patternslib/src/core/events";
33

44
describe("pat-content-mirror", () => {
55
beforeEach(() => {
@@ -14,8 +14,8 @@ describe("pat-content-mirror", () => {
1414
data-pat-content-mirror="target:.mirror .text"></textarea>
1515
`;
1616

17-
pattern.init(document.querySelector(".pat-content-mirror"));
18-
await utils.timeout(1);
17+
const instance = new Pattern(document.querySelector(".pat-content-mirror"));
18+
await events.await_pattern_init(instance);
1919

2020
const textarea = document.querySelector("textarea");
2121
textarea.value = "this is a test text.";
@@ -25,4 +25,40 @@ describe("pat-content-mirror", () => {
2525
"this is a test text."
2626
);
2727
});
28+
29+
it("works with multiple content mirrors.", async () => {
30+
document.body.innerHTML = `
31+
<p class="mirror-1"><span class="text"></span></p>
32+
<textarea
33+
class="txt-1 pat-content-mirror"
34+
data-pat-content-mirror="target:.mirror-1 .text"></textarea>
35+
36+
<p class="mirror-2"><span class="text"></span></p>
37+
<textarea
38+
class="txt-2 pat-content-mirror"
39+
data-pat-content-mirror="target:.mirror-2 .text"></textarea>
40+
`;
41+
42+
const txt1 = document.querySelector(".txt-1");
43+
const txt2 = document.querySelector(".txt-2");
44+
45+
const mirror1 = document.querySelector(".mirror-1 .text");
46+
const mirror2 = document.querySelector(".mirror-2 .text");
47+
48+
const instance1 = new Pattern(txt1);
49+
const instance2 = new Pattern(txt2);
50+
51+
await events.await_pattern_init(instance1);
52+
await events.await_pattern_init(instance2);
53+
54+
txt1.value = "this is a test text 1.";
55+
txt2.value = "this is a test text 2.";
56+
57+
txt1.dispatchEvent(new Event("input"));
58+
txt2.dispatchEvent(new Event("input"));
59+
60+
expect(mirror1.textContent).toBe("this is a test text 1.");
61+
expect(mirror2.textContent).toBe("this is a test text 2.");
62+
});
63+
2864
});

0 commit comments

Comments
 (0)