1- import type { NodeSpec } from 'prosemirror-model' ;
1+ import { Fragment , type NodeSpec } from 'prosemirror-model' ;
22
3- import { PlaceholderOptions } from '../../../../utils/placeholder' ;
3+ import type { PlaceholderOptions } from '../../../../utils/placeholder' ;
44
5- import { CheckboxAttr , CheckboxNode , b } from './const' ;
5+ import { CheckboxAttr , CheckboxNode , b , checkboxInputType , checkboxLabelType } from './const' ;
66
77import type { CheckboxSpecsOptions } from './index' ;
88
@@ -13,22 +13,59 @@ export const getSchemaSpecs = (
1313 placeholder ?: PlaceholderOptions ,
1414) : Record < CheckboxNode , NodeSpec > => ( {
1515 [ CheckboxNode . Checkbox ] : {
16- group : 'block' ,
16+ group : 'block checkbox ' ,
1717 content : `${ CheckboxNode . Input } ${ CheckboxNode . Label } ` ,
1818 selectable : true ,
1919 allowSelection : false ,
20- parseDOM : [ ] ,
2120 attrs : {
2221 [ CheckboxAttr . Class ] : { default : b ( ) } ,
2322 } ,
23+ parseDOM : [
24+ {
25+ tag : 'div.checkbox' ,
26+ priority : 100 ,
27+ getContent ( node , schema ) {
28+ const input = ( node as HTMLElement ) . querySelector < HTMLInputElement > (
29+ 'input[type=checkbox]' ,
30+ ) ;
31+ const label = ( node as HTMLElement ) . querySelector < HTMLLabelElement > (
32+ 'label[for]' ,
33+ ) ;
34+
35+ const checked = input ?. checked ? 'true' : null ;
36+ const text = label ?. textContent ;
37+
38+ return Fragment . from ( [
39+ checkboxInputType ( schema ) . create ( { [ CheckboxAttr . Checked ] : checked } ) ,
40+ checkboxLabelType ( schema ) . create ( null , text ? schema . text ( text ) : null ) ,
41+ ] ) ;
42+ } ,
43+ } ,
44+ {
45+ tag : 'input[type=checkbox]' ,
46+ priority : 50 ,
47+ getContent ( node , schema ) {
48+ const id = ( node as HTMLElement ) . id ;
49+ const checked = ( node as HTMLInputElement ) . checked ? 'true' : null ;
50+ const text = node . parentNode ?. querySelector < HTMLLabelElement > (
51+ `label[for=${ id } ]` ,
52+ ) ?. textContent ;
53+
54+ return Fragment . from ( [
55+ checkboxInputType ( schema ) . create ( { [ CheckboxAttr . Checked ] : checked } ) ,
56+ checkboxLabelType ( schema ) . create ( null , text ? schema . text ( text ) : null ) ,
57+ ] ) ;
58+ } ,
59+ } ,
60+ ] ,
2461 toDOM ( node ) {
2562 return [ 'div' , node . attrs , 0 ] ;
2663 } ,
2764 complex : 'root' ,
2865 } ,
2966
3067 [ CheckboxNode . Input ] : {
31- group : 'block' ,
68+ group : 'block checkbox ' ,
3269 parseDOM : [ ] ,
3370 attrs : {
3471 [ CheckboxAttr . Type ] : { default : 'checkbox' } ,
@@ -45,14 +82,21 @@ export const getSchemaSpecs = (
4582
4683 [ CheckboxNode . Label ] : {
4784 content : 'inline*' ,
48- group : 'block' ,
85+ group : 'block checkbox ' ,
4986 parseDOM : [
5087 {
5188 tag : `span[class="${ b ( 'label' ) } "]` ,
5289 getAttrs : ( node ) => ( {
5390 [ CheckboxAttr . For ] : ( node as Element ) . getAttribute ( CheckboxAttr . For ) || '' ,
5491 } ) ,
5592 } ,
93+ {
94+ // input handled by checkbox node parse rule
95+ // ignore label
96+ tag : 'input[type=checkbox] ~ label[for]' ,
97+ ignore : true ,
98+ consuming : true ,
99+ } ,
56100 ] ,
57101 attrs : {
58102 [ CheckboxAttr . For ] : { default : null } ,
0 commit comments