Skip to content

Commit 36f0424

Browse files
committed
feat: checkbox list conversion to web html
1 parent c7ff13a commit 36f0424

4 files changed

Lines changed: 90 additions & 1 deletion

File tree

apps/example-web/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ function App() {
233233
ref.current
234234
?.getHTML()
235235
.then((html) => {
236+
console.log('getHtml', html);
236237
setEnrichedTextValue(html);
237238
})
238239
.catch((error: unknown) => {

src/web/EnrichedText.css

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,48 @@
251251
flex-shrink: 0;
252252
accent-color: var(--et-checkbox-box-color, #0000ff);
253253
}
254+
255+
.et-view ul[data-type="checkbox"] {
256+
margin: 0;
257+
list-style: none;
258+
padding: 0;
259+
padding-left: calc(
260+
var(--et-checkbox-margin-left, 16px) + var(--et-checkbox-gap-width, 16px)
261+
);
262+
}
263+
264+
.et-view ul[data-type="checkbox"] > li {
265+
position: relative;
266+
list-style: none;
267+
pointer-events: none;
268+
}
269+
270+
.et-view ul[data-type="checkbox"] > li > p {
271+
display: inline-flex;
272+
align-items: center;
273+
gap: 0;
274+
margin: 0;
275+
padding: 0;
276+
user-select: none;
277+
}
278+
279+
.et-view ul[data-type="checkbox"] > li > input[type='checkbox'] {
280+
position: absolute;
281+
left: calc(
282+
-1 * var(--et-checkbox-gap-width, 16px) - var(--et-checkbox-box-size, 24px)
283+
);
284+
width: var(--et-checkbox-box-size, 24px);
285+
height: var(--et-checkbox-box-size, 24px);
286+
margin: 0 var(--et-checkbox-gap-width, 16px) 0 0;
287+
flex-shrink: 0;
288+
accent-color: var(--et-checkbox-box-color, #0000ff);
289+
}
290+
291+
.et-view ul[data-type="checkbox"] > li > label {
292+
display: inline-flex;
293+
align-items: center;
294+
gap: 0;
295+
margin: 0;
296+
padding: 0;
297+
user-select: none;
298+
}

src/web/EnrichedText.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { htmlStyleToCSSVariables } from './styleConversion/htmlStyleToCSSVariabl
66
import { ENRICHED_TEXT_CLASSNAME } from './consts/classNames';
77
import { enrichedInputThemingToCSSProperties } from './styleConversion/enrichedInputThemingToCSSProperties';
88
import { buildMentionRulesCSS } from './styleConversion/buildMentionRulesCSS';
9+
import { checkboxHtmlToWeb } from './checkboxHtmlToWeb';
910

1011
export const EnrichedText = ({
1112
children,
1213
htmlStyle,
1314
style,
1415
selectionColor,
1516
}: EnrichedTextProps) => {
17+
const html = useMemo(() => checkboxHtmlToWeb(children), [children]);
18+
1619
const textStyle: CSSProperties = useMemo(
1720
() => enrichedTextStyleToCSSProperties(style ?? {}),
1821
[style]
@@ -44,7 +47,7 @@ export const EnrichedText = ({
4447
<div
4548
style={finalStyle}
4649
className={ENRICHED_TEXT_CLASSNAME}
47-
dangerouslySetInnerHTML={{ __html: children }}
50+
dangerouslySetInnerHTML={{ __html: html }}
4851
/>
4952
</>
5053
);

src/web/checkboxHtmlToWeb.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Native checkbox format (as produced by the editor):
3+
* <ul data-type="checkbox">
4+
* <li checked>foo</li>
5+
* <li>bar</li>
6+
* </ul>
7+
*
8+
* Web-native, display-only format:
9+
* <ul data-type="checkbox">
10+
* <li>
11+
* <input type="checkbox" id="…" checked>
12+
* <label for="…">foo</label>
13+
* </li>
14+
* <li>
15+
* <input type="checkbox" id="…">
16+
* <label for="…">bar</label>
17+
* </li>
18+
* </ul>
19+
*/
20+
export function checkboxHtmlToWeb(html: string): string {
21+
const parser = new DOMParser();
22+
const doc = parser.parseFromString(html, 'text/html');
23+
24+
let idCounter = 0;
25+
26+
doc.querySelectorAll('ul[data-type="checkbox"]').forEach((ul) => {
27+
ul.querySelectorAll('li').forEach((li) => {
28+
const checked = li.hasAttribute('checked');
29+
const id = `enriched-checkbox-${idCounter++}`;
30+
const labelContent = li.innerHTML;
31+
32+
li.removeAttribute('checked');
33+
li.innerHTML =
34+
`<input type="checkbox" id="${id}"${checked ? ' checked' : ''}>` +
35+
`<label for="${id}">${labelContent}</label>`;
36+
});
37+
});
38+
39+
return doc.body.innerHTML;
40+
}

0 commit comments

Comments
 (0)