@@ -11,26 +11,57 @@ import { elementFromPoint } from '../../utils/src/polyfill';
11
11
import { VRange } from '../../core/src/VRange' ;
12
12
import { InsertTextParams , Char } from '../../plugin-char/src/Char' ;
13
13
import { Context } from '../../core/src/ContextManager' ;
14
+ import { AbstractParser } from '../../plugin-parser/src/AbstractParser' ;
15
+ import { Constructor } from '../../utils/src/utils' ;
16
+ import { HtmlDomParsingEngine } from '../../plugin-html/src/HtmlDomParsingEngine' ;
17
+ import {
18
+ TableRowXmlDomParser ,
19
+ TableSectionAttributes ,
20
+ } from '../../plugin-table/src/TableRowXmlDomParser' ;
21
+ import {
22
+ ListItemXmlDomParser ,
23
+ ListItemAttributes ,
24
+ } from '../../plugin-list/src/ListItemXmlDomParser' ;
14
25
15
26
export class DomHelpers < T extends JWPluginConfig = JWPluginConfig > extends JWPlugin < T > {
27
+ static dependencies = [ Parser ] ;
28
+ private _specializedAttributes : Map < AbstractParser < Node > , Constructor < Attributes > > = new Map ( ) ;
29
+
30
+ async start ( ) : Promise < void > {
31
+ await super . start ( ) ;
32
+ const engine = this . editor . plugins . get ( Parser ) . engines [
33
+ HtmlDomParsingEngine . id
34
+ ] as HtmlDomParsingEngine ;
35
+ for ( const parser of engine . parsers ) {
36
+ if ( parser . constructor === TableRowXmlDomParser ) {
37
+ this . _specializedAttributes . set ( parser , TableSectionAttributes ) ;
38
+ } else if ( parser . constructor === ListItemXmlDomParser ) {
39
+ this . _specializedAttributes . set ( parser , ListItemAttributes ) ;
40
+ }
41
+ }
42
+ }
43
+
16
44
//--------------------------------------------------------------------------
17
45
// Public
18
46
//--------------------------------------------------------------------------
19
-
20
47
/**
21
48
* Add a class or a list of classes to a DOM node or a list of DOM nodes.
22
49
*
23
50
* @param params
24
51
*/
25
52
async addClass (
26
53
context : ExecutionContext ,
27
- domNode : Node | Node [ ] ,
54
+ originalDomNode : Node | Node [ ] ,
28
55
className : string | string [ ] ,
29
56
) : Promise < ExecCommandResult > {
30
57
const domHelpersAddClass = async ( ) : Promise < void > => {
31
- const classes = Array . isArray ( className ) ? className : [ className ] ;
32
- for ( const node of this . getNodes ( domNode ) ) {
33
- node . modifiers . get ( Attributes ) . classList . add ( ...classes ) ;
58
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
59
+ for ( const domNode of domNodes ) {
60
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
61
+ const classes = Array . isArray ( className ) ? className : [ className ] ;
62
+ for ( const node of this . getNodes ( domNode ) ) {
63
+ node . modifiers . get ( Attributes ) . classList . add ( ...classes ) ;
64
+ }
34
65
}
35
66
} ;
36
67
return context . execCommand ( domHelpersAddClass ) ;
@@ -42,15 +73,19 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
42
73
*/
43
74
async removeClass (
44
75
context : ExecutionContext ,
45
- domNode : Node | Node [ ] ,
76
+ originalDomNode : Node | Node [ ] ,
46
77
className : string | string [ ] ,
47
78
) : Promise < ExecCommandResult > {
48
79
const domHelpersRemoveClass = async ( ) : Promise < void > => {
49
80
const classes = Array . isArray ( className ) ? className : [ className ] ;
50
- for ( const node of this . getNodes ( domNode ) ) {
51
- node . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
52
- for ( const modifier of node . modifiers . filter ( Format ) ) {
53
- modifier . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
81
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
82
+ for ( const domNode of domNodes ) {
83
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
84
+ for ( const node of this . getNodes ( domNode ) ) {
85
+ node . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
86
+ for ( const modifier of node . modifiers . filter ( Format ) ) {
87
+ modifier . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
88
+ }
54
89
}
55
90
}
56
91
} ;
@@ -64,13 +99,17 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
64
99
*/
65
100
async toggleClass (
66
101
context : ExecutionContext ,
67
- domNode : Node | Node [ ] ,
102
+ originalDomNode : Node | Node [ ] ,
68
103
className : string ,
69
104
) : Promise < ExecCommandResult > {
70
105
const domHelpersToggleClass = async ( ) : Promise < void > => {
71
106
const classes = Array . isArray ( className ) ? className : [ className ] ;
72
- for ( const node of this . getNodes ( domNode ) ) {
73
- node . modifiers . get ( Attributes ) . classList . toggle ( ...classes ) ;
107
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
108
+ for ( const domNode of domNodes ) {
109
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
110
+ for ( const node of this . getNodes ( domNode ) ) {
111
+ node . modifiers . get ( Attributes ) . classList . toggle ( ...classes ) ;
112
+ }
74
113
}
75
114
} ;
76
115
return context . execCommand ( domHelpersToggleClass ) ;
@@ -82,13 +121,17 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
82
121
*/
83
122
async setAttribute (
84
123
context : ExecutionContext ,
85
- domNode : Node | Node [ ] ,
124
+ originalDomNode : Node | Node [ ] ,
86
125
name : string ,
87
126
value : string ,
88
127
) : Promise < ExecCommandResult > {
89
128
const domHelpersSetAttribute = async ( ) : Promise < void > => {
90
- for ( const node of this . getNodes ( domNode ) ) {
91
- node . modifiers . get ( Attributes ) . set ( name , value ) ;
129
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
130
+ for ( const domNode of domNodes ) {
131
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
132
+ for ( const node of this . getNodes ( domNode ) ) {
133
+ node . modifiers . get ( Attributes ) . set ( name , value ) ;
134
+ }
92
135
}
93
136
} ;
94
137
return context . execCommand ( domHelpersSetAttribute ) ;
@@ -100,14 +143,18 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
100
143
*/
101
144
async updateAttributes (
102
145
context : ExecutionContext ,
103
- domNode : Node | Node [ ] ,
146
+ originalDomNode : Node | Node [ ] ,
104
147
attributes : { [ key : string ] : string } ,
105
148
) : Promise < ExecCommandResult > {
106
149
const domHelpersUpdateAttribute = async ( ) : Promise < void > => {
107
- for ( const node of this . getNodes ( domNode ) ) {
108
- node . modifiers . get ( Attributes ) . clear ( ) ;
109
- for ( const [ name , value ] of Object . entries ( attributes ) ) {
110
- node . modifiers . get ( Attributes ) . set ( name , value ) ;
150
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
151
+ for ( const domNode of domNodes ) {
152
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
153
+ for ( const node of this . getNodes ( domNode ) ) {
154
+ node . modifiers . get ( Attributes ) . clear ( ) ;
155
+ for ( const [ name , value ] of Object . entries ( attributes ) ) {
156
+ node . modifiers . get ( Attributes ) . set ( name , value ) ;
157
+ }
111
158
}
112
159
}
113
160
} ;
@@ -120,15 +167,19 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
120
167
*/
121
168
async setStyle (
122
169
context : ExecutionContext ,
123
- domNode : Node | Node [ ] ,
170
+ originalDomNode : Node | Node [ ] ,
124
171
name : string ,
125
172
value : string ,
126
173
important ?: boolean ,
127
174
) : Promise < ExecCommandResult > {
128
175
const domHelpersSetStyle = async ( ) : Promise < void > => {
129
- for ( const node of this . getNodes ( domNode ) ) {
130
- value = important ? value + ' !important' : value ;
131
- node . modifiers . get ( Attributes ) . style . set ( name , value ) ;
176
+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
177
+ for ( const domNode of domNodes ) {
178
+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
179
+ for ( const node of this . getNodes ( domNode ) ) {
180
+ value = important ? value + ' !important' : value ;
181
+ node . modifiers . get ( Attributes ) . style . set ( name , value ) ;
182
+ }
132
183
}
133
184
} ;
134
185
return context . execCommand ( domHelpersSetStyle ) ;
@@ -419,4 +470,12 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
419
470
div . innerHTML = html ;
420
471
return parser . parse ( 'dom/html' , ...div . childNodes ) ;
421
472
}
473
+ private _getAttributesConstructor ( node : Node ) : Constructor < Attributes > {
474
+ for ( const [ parser , Attributes ] of this . _specializedAttributes ) {
475
+ if ( parser . predicate ( node ) ) {
476
+ return Attributes ;
477
+ }
478
+ }
479
+ return Attributes ;
480
+ }
422
481
}
0 commit comments