@lesjoursfr/edith 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ import { createNodeWith, getAttribute, hasAttribute } from "../core/index.js";
2
+ export var EdithModalFieldType;
3
+ (function (EdithModalFieldType) {
4
+ EdithModalFieldType[EdithModalFieldType["input"] = 1] = "input";
5
+ EdithModalFieldType[EdithModalFieldType["checkbox"] = 2] = "checkbox";
6
+ })(EdithModalFieldType || (EdithModalFieldType = {}));
7
+ function renderInputModalField(field) {
8
+ const el = document.createElement("div");
9
+ el.setAttribute("class", "edith-modal-input");
10
+ const label = document.createElement("label");
11
+ label.textContent = field.label;
12
+ const input = document.createElement("input");
13
+ input.setAttribute("name", field.name);
14
+ input.setAttribute("type", "text");
15
+ if (field.initialState !== null) {
16
+ input.value = field.initialState.toString();
17
+ }
18
+ el.append(label);
19
+ el.append(input);
20
+ return el;
21
+ }
22
+ function renderCheckboxModalField(field) {
23
+ const el = document.createElement("div");
24
+ el.setAttribute("class", "edith-modal-checkbox");
25
+ const label = document.createElement("label");
26
+ label.textContent = field.label;
27
+ const input = document.createElement("input");
28
+ input.setAttribute("name", field.name);
29
+ input.setAttribute("type", "checkbox");
30
+ if (field.initialState) {
31
+ input.checked = true;
32
+ }
33
+ label.prepend(input);
34
+ el.append(label);
35
+ return el;
36
+ }
37
+ export function createInputModalField(label, name, initialState = null) {
38
+ return {
39
+ fieldType: EdithModalFieldType.input,
40
+ label,
41
+ name,
42
+ initialState,
43
+ };
44
+ }
45
+ export function createCheckboxModalField(label, name, initialState = false) {
46
+ return {
47
+ fieldType: EdithModalFieldType.checkbox,
48
+ label,
49
+ name,
50
+ initialState,
51
+ };
52
+ }
53
+ export class EdithModal {
54
+ el;
55
+ ctx;
56
+ title;
57
+ fields;
58
+ callback;
59
+ constructor(ctx, options) {
60
+ this.ctx = ctx;
61
+ this.title = options.title;
62
+ this.fields = options.fields || [];
63
+ this.callback = options.callback;
64
+ }
65
+ cancel(event) {
66
+ event.preventDefault();
67
+ // Call the callback with a null value
68
+ this.callback(null);
69
+ // Close the modal
70
+ this.close();
71
+ }
72
+ submit(event) {
73
+ event.preventDefault();
74
+ // Call the callback with the input & checkboxes values
75
+ const payload = {};
76
+ for (const el of this.el.querySelectorAll("input")) {
77
+ if (hasAttribute(el, "name")) {
78
+ payload[getAttribute(el, "name")] = getAttribute(el, "type") === "checkbox" ? el.checked : el.value;
79
+ }
80
+ }
81
+ this.callback(payload);
82
+ // Close the modal
83
+ this.close();
84
+ }
85
+ close() {
86
+ // Remove the element from the dom
87
+ this.el.remove();
88
+ }
89
+ show() {
90
+ // Create the modal
91
+ this.el = createNodeWith("div", {
92
+ attributes: { class: "edith-modal" },
93
+ });
94
+ // Create the header
95
+ const header = createNodeWith("div", {
96
+ attributes: { class: "edith-modal-header" },
97
+ });
98
+ const title = createNodeWith("span", {
99
+ textContent: this.title,
100
+ attributes: { class: "edith-modal-title" },
101
+ });
102
+ header.append(title);
103
+ // Create the content
104
+ const content = createNodeWith("div", {
105
+ attributes: { class: "edith-modal-content" },
106
+ });
107
+ for (const field of this.fields) {
108
+ switch (field.fieldType) {
109
+ case EdithModalFieldType.input:
110
+ content.append(renderInputModalField(field));
111
+ break;
112
+ case EdithModalFieldType.checkbox:
113
+ content.append(renderCheckboxModalField(field));
114
+ break;
115
+ default:
116
+ throw new Error(`Unknown fieldType ${field.fieldType}`);
117
+ }
118
+ }
119
+ // Create the footer
120
+ const footer = createNodeWith("div", {
121
+ attributes: { class: "edith-modal-footer" },
122
+ });
123
+ const cancel = createNodeWith("button", {
124
+ textContent: "Annuler",
125
+ attributes: { class: "edith-modal-cancel", type: "button" },
126
+ });
127
+ footer.append(cancel);
128
+ const submit = createNodeWith("button", {
129
+ textContent: "Valider",
130
+ attributes: { class: "edith-modal-submit", type: "button" },
131
+ });
132
+ footer.append(submit);
133
+ // Append everything
134
+ this.el.append(header);
135
+ this.el.append(content);
136
+ this.el.append(footer);
137
+ // Add the modal to the editor
138
+ this.ctx.modals.append(this.el);
139
+ // Bind events
140
+ cancel.onclick = this.cancel.bind(this);
141
+ submit.onclick = this.submit.bind(this);
142
+ // Return the modal
143
+ return this.el;
144
+ }
145
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lesjoursfr/edith",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "Simple WYSIWYG editor.",
5
5
  "license": "MIT",
6
6
  "repository": "lesjoursfr/edith",