Skip to content

Commit cdd86f5

Browse files
committed
Add support for boolean fields (checkboxes) to updateAttrsDialog
1 parent 26789eb commit cdd86f5

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

django_prose_editor/static/django_prose_editor/editor.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

django_prose_editor/static/django_prose_editor/editor.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/editor.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,20 @@
166166
border-bottom: 1px solid var(--_r);
167167
}
168168

169+
.prose-editor-dialog input[type="checkbox"],
170+
.prose-editor-dialog input[type="radio"] {
171+
margin-right: 4px;
172+
}
173+
169174
.prose-editor-dialog label {
170175
display: block;
171176
margin-bottom: 0.5em;
172177
font-weight: bold;
178+
width: auto;
179+
min-width: 0;
173180
}
174181

175-
.prose-editor-dialog input,
182+
.prose-editor-dialog input:not([type="checkbox"], [type="radio"]),
176183
.prose-editor-dialog select,
177184
.prose-editor-dialog textarea {
178185
width: 100%;

src/utils.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
export const crel = (tagName, attributes = null, children = []) => {
22
const dom = document.createElement(tagName)
3+
dom.append(...children)
34
if (attributes) {
45
for (const [name, value] of Object.entries(attributes)) {
56
if (/^data-|^aria-|^role/.test(name)) dom.setAttribute(name, value)
67
else dom[name] = value
78
}
89
}
9-
dom.append(...children)
1010
return dom
1111
}
1212

1313
export const gettext = window.gettext || ((s) => s)
1414

15-
const formFieldForProperty = ([name, config]) => {
15+
const formFieldForProperty = (name, config, value) => {
16+
const label = crel("label", { textContent: config.title || name })
1617
let widget
1718

19+
if (config.type === "boolean") {
20+
return crel("label", {}, [
21+
crel("input", { name, type: "checkbox", checked: !!value }),
22+
config.title || name,
23+
])
24+
}
1825
if (config.format === "textarea") {
19-
widget = crel("textarea", { name, cols: 80, rows: 30 })
26+
widget = crel("textarea", { name, value, cols: 80, rows: 30 })
2027
} else if (config.enum) {
2128
widget = crel(
2229
"select",
23-
{ name },
24-
config.enum.map((value) => crel("option", { textContent: value })),
30+
{ name, value },
31+
config.enum.map((option) => crel("option", { textContent: option })),
2532
)
2633
} else {
2734
// Create input with appropriate attributes
2835
const attrs = {
2936
name,
37+
value,
3038
type: config.format || "text",
3139
size: 50,
3240
}
@@ -39,10 +47,14 @@ const formFieldForProperty = ([name, config]) => {
3947
widget = crel("input", attrs)
4048
}
4149

42-
return crel("p", {}, [
43-
crel("label", { textContent: config.title || name }),
44-
widget,
45-
])
50+
return crel("p", {}, [label, widget])
51+
}
52+
53+
const valueForFormField = (name, config, form) => {
54+
if (config.type === "boolean") {
55+
return !!form[name].checked
56+
}
57+
return form[name].value
4658
}
4759

4860
export const updateAttrsDialog =
@@ -74,8 +86,8 @@ export const updateAttrsDialog =
7486

7587
// Add form fields with dynamic enum support
7688
formElements.push(
77-
...Object.entries(properties).map((entry) =>
78-
formFieldForProperty(entry),
89+
...Object.entries(properties).map(([name, config]) =>
90+
formFieldForProperty(name, config, attrs[name]),
7991
),
8092
)
8193

@@ -92,10 +104,6 @@ export const updateAttrsDialog =
92104
const dialog = div.querySelector("dialog")
93105
const form = div.querySelector("form")
94106

95-
for (const [name, config] of Object.entries(properties)) {
96-
form[name].value = attrs[name] || config.default || ""
97-
}
98-
99107
cancel.addEventListener("click", () => {
100108
dialog.close()
101109
})
@@ -109,7 +117,10 @@ export const updateAttrsDialog =
109117
div.remove()
110118
resolve(
111119
Object.fromEntries(
112-
Object.keys(properties).map((name) => [name, form[name].value]),
120+
Object.entries(properties).map(([name, config]) => [
121+
name,
122+
valueForFormField(name, config, form),
123+
]),
113124
),
114125
)
115126
}

0 commit comments

Comments
 (0)