@ckeditor/ckeditor5-highlight 35.3.2 → 36.0.0
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.
- package/LICENSE.md +1 -1
- package/build/highlight.js +2 -2
- package/package.json +21 -16
- package/src/highlight.js +13 -174
- package/src/highlight.ts +225 -0
- package/src/highlightcommand.js +73 -93
- package/src/highlightcommand.ts +113 -0
- package/src/highlightediting.js +92 -107
- package/src/highlightediting.ts +139 -0
- package/src/highlightui.js +183 -232
- package/src/highlightui.ts +282 -0
- package/src/index.js +1 -3
- package/src/index.ts +12 -0
- package/theme/highlight.css +1 -1
- package/build/highlight.js.map +0 -1
package/src/highlightcommand.js
CHANGED
@@ -1,111 +1,91 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
4
|
*/
|
5
|
-
|
6
5
|
/**
|
7
6
|
* @module highlight/highlightcommand
|
8
7
|
*/
|
9
|
-
|
10
8
|
import { Command } from 'ckeditor5/src/core';
|
11
|
-
|
12
9
|
/**
|
13
10
|
* The highlight command. It is used by the {@link module:highlight/highlightediting~HighlightEditing highlight feature}
|
14
11
|
* to apply the text highlighting.
|
15
12
|
*
|
16
|
-
*
|
13
|
+
* ```ts
|
14
|
+
* editor.execute( 'highlight', { value: 'greenMarker' } );
|
15
|
+
* ```
|
17
16
|
*
|
18
17
|
* **Note**: Executing the command without a value removes the attribute from the model. If the selection is collapsed
|
19
18
|
* inside a text with the highlight attribute, the command will remove the attribute from the entire range
|
20
19
|
* of that text.
|
21
|
-
*
|
22
|
-
* @extends module:core/command~Command
|
23
20
|
*/
|
24
21
|
export default class HighlightCommand extends Command {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
}
|
95
|
-
} else if ( highlighter ) {
|
96
|
-
writer.setSelectionAttribute( 'highlight', highlighter );
|
97
|
-
}
|
98
|
-
} else {
|
99
|
-
const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
|
100
|
-
|
101
|
-
for ( const range of ranges ) {
|
102
|
-
if ( highlighter ) {
|
103
|
-
writer.setAttribute( 'highlight', highlighter, range );
|
104
|
-
} else {
|
105
|
-
writer.removeAttribute( 'highlight', range );
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
} );
|
110
|
-
}
|
22
|
+
/**
|
23
|
+
* @inheritDoc
|
24
|
+
*/
|
25
|
+
refresh() {
|
26
|
+
const model = this.editor.model;
|
27
|
+
const doc = model.document;
|
28
|
+
this.value = doc.selection.getAttribute('highlight');
|
29
|
+
this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, 'highlight');
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Executes the command.
|
33
|
+
*
|
34
|
+
* @param options Options for the executed command.
|
35
|
+
* @param options.value The value to apply.
|
36
|
+
*
|
37
|
+
* @fires execute
|
38
|
+
*/
|
39
|
+
execute(options = {}) {
|
40
|
+
const model = this.editor.model;
|
41
|
+
const document = model.document;
|
42
|
+
const selection = document.selection;
|
43
|
+
const highlighter = options.value;
|
44
|
+
model.change(writer => {
|
45
|
+
if (selection.isCollapsed) {
|
46
|
+
const position = selection.getFirstPosition();
|
47
|
+
// When selection is inside text with `highlight` attribute.
|
48
|
+
if (selection.hasAttribute('highlight')) {
|
49
|
+
// Find the full highlighted range.
|
50
|
+
const isSameHighlight = (value) => {
|
51
|
+
return value.item.hasAttribute('highlight') && value.item.getAttribute('highlight') === this.value;
|
52
|
+
};
|
53
|
+
const highlightStart = position.getLastMatchingPosition(isSameHighlight, { direction: 'backward' });
|
54
|
+
const highlightEnd = position.getLastMatchingPosition(isSameHighlight);
|
55
|
+
const highlightRange = writer.createRange(highlightStart, highlightEnd);
|
56
|
+
// Then depending on current value...
|
57
|
+
if (!highlighter || this.value === highlighter) {
|
58
|
+
// ...remove attribute when passing highlighter different then current or executing "eraser".
|
59
|
+
// If we're at the end of the highlighted range, we don't want to remove highlight of the range.
|
60
|
+
if (!position.isEqual(highlightEnd)) {
|
61
|
+
writer.removeAttribute('highlight', highlightRange);
|
62
|
+
}
|
63
|
+
writer.removeSelectionAttribute('highlight');
|
64
|
+
}
|
65
|
+
else {
|
66
|
+
// ...update `highlight` value.
|
67
|
+
// If we're at the end of the highlighted range, we don't want to change the highlight of the range.
|
68
|
+
if (!position.isEqual(highlightEnd)) {
|
69
|
+
writer.setAttribute('highlight', highlighter, highlightRange);
|
70
|
+
}
|
71
|
+
writer.setSelectionAttribute('highlight', highlighter);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
else if (highlighter) {
|
75
|
+
writer.setSelectionAttribute('highlight', highlighter);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
else {
|
79
|
+
const ranges = model.schema.getValidRanges(selection.getRanges(), 'highlight');
|
80
|
+
for (const range of ranges) {
|
81
|
+
if (highlighter) {
|
82
|
+
writer.setAttribute('highlight', highlighter, range);
|
83
|
+
}
|
84
|
+
else {
|
85
|
+
writer.removeAttribute('highlight', range);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
});
|
90
|
+
}
|
111
91
|
}
|
@@ -0,0 +1,113 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
|
6
|
+
/**
|
7
|
+
* @module highlight/highlightcommand
|
8
|
+
*/
|
9
|
+
|
10
|
+
import { Command } from 'ckeditor5/src/core';
|
11
|
+
import type { TreeWalkerValue } from 'ckeditor5/src/engine';
|
12
|
+
|
13
|
+
/**
|
14
|
+
* The highlight command. It is used by the {@link module:highlight/highlightediting~HighlightEditing highlight feature}
|
15
|
+
* to apply the text highlighting.
|
16
|
+
*
|
17
|
+
* ```ts
|
18
|
+
* editor.execute( 'highlight', { value: 'greenMarker' } );
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* **Note**: Executing the command without a value removes the attribute from the model. If the selection is collapsed
|
22
|
+
* inside a text with the highlight attribute, the command will remove the attribute from the entire range
|
23
|
+
* of that text.
|
24
|
+
*/
|
25
|
+
export default class HighlightCommand extends Command {
|
26
|
+
/**
|
27
|
+
* A value indicating whether the command is active. If the selection has some highlight attribute,
|
28
|
+
* it corresponds to the value of that attribute.
|
29
|
+
*
|
30
|
+
* @observable
|
31
|
+
* @readonly
|
32
|
+
*/
|
33
|
+
declare public value: string | undefined;
|
34
|
+
|
35
|
+
/**
|
36
|
+
* @inheritDoc
|
37
|
+
*/
|
38
|
+
public override refresh(): void {
|
39
|
+
const model = this.editor.model;
|
40
|
+
const doc = model.document;
|
41
|
+
|
42
|
+
this.value = doc.selection.getAttribute( 'highlight' ) as string | undefined;
|
43
|
+
this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Executes the command.
|
48
|
+
*
|
49
|
+
* @param options Options for the executed command.
|
50
|
+
* @param options.value The value to apply.
|
51
|
+
*
|
52
|
+
* @fires execute
|
53
|
+
*/
|
54
|
+
public override execute( options: { value?: string | null } = {} ): void {
|
55
|
+
const model = this.editor.model;
|
56
|
+
const document = model.document;
|
57
|
+
const selection = document.selection;
|
58
|
+
|
59
|
+
const highlighter = options.value;
|
60
|
+
|
61
|
+
model.change( writer => {
|
62
|
+
if ( selection.isCollapsed ) {
|
63
|
+
const position = selection.getFirstPosition()!;
|
64
|
+
|
65
|
+
// When selection is inside text with `highlight` attribute.
|
66
|
+
if ( selection.hasAttribute( 'highlight' ) ) {
|
67
|
+
// Find the full highlighted range.
|
68
|
+
const isSameHighlight = ( value: TreeWalkerValue ) => {
|
69
|
+
return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
|
70
|
+
};
|
71
|
+
|
72
|
+
const highlightStart = position.getLastMatchingPosition( isSameHighlight, { direction: 'backward' } );
|
73
|
+
const highlightEnd = position.getLastMatchingPosition( isSameHighlight );
|
74
|
+
|
75
|
+
const highlightRange = writer.createRange( highlightStart, highlightEnd );
|
76
|
+
|
77
|
+
// Then depending on current value...
|
78
|
+
if ( !highlighter || this.value === highlighter ) {
|
79
|
+
// ...remove attribute when passing highlighter different then current or executing "eraser".
|
80
|
+
|
81
|
+
// If we're at the end of the highlighted range, we don't want to remove highlight of the range.
|
82
|
+
if ( !position.isEqual( highlightEnd ) ) {
|
83
|
+
writer.removeAttribute( 'highlight', highlightRange );
|
84
|
+
}
|
85
|
+
|
86
|
+
writer.removeSelectionAttribute( 'highlight' );
|
87
|
+
} else {
|
88
|
+
// ...update `highlight` value.
|
89
|
+
|
90
|
+
// If we're at the end of the highlighted range, we don't want to change the highlight of the range.
|
91
|
+
if ( !position.isEqual( highlightEnd ) ) {
|
92
|
+
writer.setAttribute( 'highlight', highlighter, highlightRange );
|
93
|
+
}
|
94
|
+
|
95
|
+
writer.setSelectionAttribute( 'highlight', highlighter );
|
96
|
+
}
|
97
|
+
} else if ( highlighter ) {
|
98
|
+
writer.setSelectionAttribute( 'highlight', highlighter );
|
99
|
+
}
|
100
|
+
} else {
|
101
|
+
const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
|
102
|
+
|
103
|
+
for ( const range of ranges ) {
|
104
|
+
if ( highlighter ) {
|
105
|
+
writer.setAttribute( 'highlight', highlighter, range );
|
106
|
+
} else {
|
107
|
+
writer.removeAttribute( 'highlight', range );
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
} );
|
112
|
+
}
|
113
|
+
}
|
package/src/highlightediting.js
CHANGED
@@ -1,124 +1,109 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
4
|
*/
|
5
|
-
|
6
5
|
/**
|
7
6
|
* @module highlight/highlightediting
|
8
7
|
*/
|
9
|
-
|
10
8
|
import { Plugin } from 'ckeditor5/src/core';
|
11
|
-
|
12
9
|
import HighlightCommand from './highlightcommand';
|
13
|
-
|
14
10
|
/**
|
15
11
|
* The highlight editing feature. It introduces the {@link module:highlight/highlightcommand~HighlightCommand command} and the `highlight`
|
16
12
|
* attribute in the {@link module:engine/model/model~Model model} which renders in the {@link module:engine/view/view view}
|
17
13
|
* as a `<mark>` element with a `class` attribute (`<mark class="marker-green">...</mark>`) depending
|
18
14
|
* on the {@link module:highlight/highlight~HighlightConfig configuration}.
|
19
|
-
*
|
20
|
-
* @extends module:core/plugin~Plugin
|
21
15
|
*/
|
22
16
|
export default class HighlightEditing extends Plugin {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
const options = editor.config.get( 'highlight.options' );
|
94
|
-
|
95
|
-
// Set-up the two-way conversion.
|
96
|
-
editor.conversion.attributeToElement( _buildDefinition( options ) );
|
97
|
-
|
98
|
-
editor.commands.add( 'highlight', new HighlightCommand( editor ) );
|
99
|
-
}
|
17
|
+
/**
|
18
|
+
* @inheritDoc
|
19
|
+
*/
|
20
|
+
static get pluginName() {
|
21
|
+
return 'HighlightEditing';
|
22
|
+
}
|
23
|
+
/**
|
24
|
+
* @inheritDoc
|
25
|
+
*/
|
26
|
+
constructor(editor) {
|
27
|
+
super(editor);
|
28
|
+
editor.config.define('highlight', {
|
29
|
+
options: [
|
30
|
+
{
|
31
|
+
model: 'yellowMarker',
|
32
|
+
class: 'marker-yellow',
|
33
|
+
title: 'Yellow marker',
|
34
|
+
color: 'var(--ck-highlight-marker-yellow)',
|
35
|
+
type: 'marker'
|
36
|
+
},
|
37
|
+
{
|
38
|
+
model: 'greenMarker',
|
39
|
+
class: 'marker-green',
|
40
|
+
title: 'Green marker',
|
41
|
+
color: 'var(--ck-highlight-marker-green)',
|
42
|
+
type: 'marker'
|
43
|
+
},
|
44
|
+
{
|
45
|
+
model: 'pinkMarker',
|
46
|
+
class: 'marker-pink',
|
47
|
+
title: 'Pink marker',
|
48
|
+
color: 'var(--ck-highlight-marker-pink)',
|
49
|
+
type: 'marker'
|
50
|
+
},
|
51
|
+
{
|
52
|
+
model: 'blueMarker',
|
53
|
+
class: 'marker-blue',
|
54
|
+
title: 'Blue marker',
|
55
|
+
color: 'var(--ck-highlight-marker-blue)',
|
56
|
+
type: 'marker'
|
57
|
+
},
|
58
|
+
{
|
59
|
+
model: 'redPen',
|
60
|
+
class: 'pen-red',
|
61
|
+
title: 'Red pen',
|
62
|
+
color: 'var(--ck-highlight-pen-red)',
|
63
|
+
type: 'pen'
|
64
|
+
},
|
65
|
+
{
|
66
|
+
model: 'greenPen',
|
67
|
+
class: 'pen-green',
|
68
|
+
title: 'Green pen',
|
69
|
+
color: 'var(--ck-highlight-pen-green)',
|
70
|
+
type: 'pen'
|
71
|
+
}
|
72
|
+
]
|
73
|
+
});
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* @inheritDoc
|
77
|
+
*/
|
78
|
+
init() {
|
79
|
+
const editor = this.editor;
|
80
|
+
// Allow highlight attribute on text nodes.
|
81
|
+
editor.model.schema.extend('$text', { allowAttributes: 'highlight' });
|
82
|
+
const options = editor.config.get('highlight.options');
|
83
|
+
// Set-up the two-way conversion.
|
84
|
+
editor.conversion.attributeToElement(_buildDefinition(options));
|
85
|
+
editor.commands.add('highlight', new HighlightCommand(editor));
|
86
|
+
}
|
100
87
|
}
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
function _buildDefinition(
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
return definition;
|
88
|
+
/**
|
89
|
+
* Converts the options array to a converter definition.
|
90
|
+
*
|
91
|
+
* @param options An array with configured options.
|
92
|
+
*/
|
93
|
+
function _buildDefinition(options) {
|
94
|
+
const definition = {
|
95
|
+
model: {
|
96
|
+
key: 'highlight',
|
97
|
+
values: []
|
98
|
+
},
|
99
|
+
view: {}
|
100
|
+
};
|
101
|
+
for (const option of options) {
|
102
|
+
definition.model.values.push(option.model);
|
103
|
+
definition.view[option.model] = {
|
104
|
+
name: 'mark',
|
105
|
+
classes: option.class
|
106
|
+
};
|
107
|
+
}
|
108
|
+
return definition;
|
124
109
|
}
|
@@ -0,0 +1,139 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
|
6
|
+
/**
|
7
|
+
* @module highlight/highlightediting
|
8
|
+
*/
|
9
|
+
|
10
|
+
import { Plugin, type Editor } from 'ckeditor5/src/core';
|
11
|
+
|
12
|
+
import HighlightCommand from './highlightcommand';
|
13
|
+
import type { HighlightOption } from './highlight';
|
14
|
+
|
15
|
+
/**
|
16
|
+
* The highlight editing feature. It introduces the {@link module:highlight/highlightcommand~HighlightCommand command} and the `highlight`
|
17
|
+
* attribute in the {@link module:engine/model/model~Model model} which renders in the {@link module:engine/view/view view}
|
18
|
+
* as a `<mark>` element with a `class` attribute (`<mark class="marker-green">...</mark>`) depending
|
19
|
+
* on the {@link module:highlight/highlight~HighlightConfig configuration}.
|
20
|
+
*/
|
21
|
+
export default class HighlightEditing extends Plugin {
|
22
|
+
/**
|
23
|
+
* @inheritDoc
|
24
|
+
*/
|
25
|
+
public static get pluginName(): 'HighlightEditing' {
|
26
|
+
return 'HighlightEditing';
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @inheritDoc
|
31
|
+
*/
|
32
|
+
constructor( editor: Editor ) {
|
33
|
+
super( editor );
|
34
|
+
|
35
|
+
editor.config.define( 'highlight', {
|
36
|
+
options: [
|
37
|
+
{
|
38
|
+
model: 'yellowMarker',
|
39
|
+
class: 'marker-yellow',
|
40
|
+
title: 'Yellow marker',
|
41
|
+
color: 'var(--ck-highlight-marker-yellow)',
|
42
|
+
type: 'marker'
|
43
|
+
},
|
44
|
+
{
|
45
|
+
model: 'greenMarker',
|
46
|
+
class: 'marker-green',
|
47
|
+
title: 'Green marker',
|
48
|
+
color: 'var(--ck-highlight-marker-green)',
|
49
|
+
type: 'marker'
|
50
|
+
},
|
51
|
+
{
|
52
|
+
model: 'pinkMarker',
|
53
|
+
class: 'marker-pink',
|
54
|
+
title: 'Pink marker',
|
55
|
+
color: 'var(--ck-highlight-marker-pink)',
|
56
|
+
type: 'marker'
|
57
|
+
},
|
58
|
+
{
|
59
|
+
model: 'blueMarker',
|
60
|
+
class: 'marker-blue',
|
61
|
+
title: 'Blue marker',
|
62
|
+
color: 'var(--ck-highlight-marker-blue)',
|
63
|
+
type: 'marker'
|
64
|
+
},
|
65
|
+
{
|
66
|
+
model: 'redPen',
|
67
|
+
class: 'pen-red',
|
68
|
+
title: 'Red pen',
|
69
|
+
color: 'var(--ck-highlight-pen-red)',
|
70
|
+
type: 'pen'
|
71
|
+
},
|
72
|
+
{
|
73
|
+
model: 'greenPen',
|
74
|
+
class: 'pen-green',
|
75
|
+
title: 'Green pen',
|
76
|
+
color: 'var(--ck-highlight-pen-green)',
|
77
|
+
type: 'pen'
|
78
|
+
}
|
79
|
+
]
|
80
|
+
} );
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* @inheritDoc
|
85
|
+
*/
|
86
|
+
public init(): void {
|
87
|
+
const editor = this.editor;
|
88
|
+
|
89
|
+
// Allow highlight attribute on text nodes.
|
90
|
+
editor.model.schema.extend( '$text', { allowAttributes: 'highlight' } );
|
91
|
+
|
92
|
+
const options = editor.config.get( 'highlight.options' )!;
|
93
|
+
|
94
|
+
// Set-up the two-way conversion.
|
95
|
+
editor.conversion.attributeToElement( _buildDefinition( options ) );
|
96
|
+
|
97
|
+
editor.commands.add( 'highlight', new HighlightCommand( editor ) );
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Converts the options array to a converter definition.
|
103
|
+
*
|
104
|
+
* @param options An array with configured options.
|
105
|
+
*/
|
106
|
+
function _buildDefinition( options: Array<HighlightOption> ): HighlightConverterDefinition {
|
107
|
+
const definition: HighlightConverterDefinition = {
|
108
|
+
model: {
|
109
|
+
key: 'highlight',
|
110
|
+
values: []
|
111
|
+
},
|
112
|
+
view: {}
|
113
|
+
};
|
114
|
+
|
115
|
+
for ( const option of options ) {
|
116
|
+
definition.model.values.push( option.model );
|
117
|
+
definition.view[ option.model ] = {
|
118
|
+
name: 'mark',
|
119
|
+
classes: option.class
|
120
|
+
};
|
121
|
+
}
|
122
|
+
|
123
|
+
return definition;
|
124
|
+
}
|
125
|
+
|
126
|
+
type HighlightConverterDefinition = {
|
127
|
+
model: { key: string; values: Array<string> };
|
128
|
+
view: Record<string, { name: string; classes: string }>;
|
129
|
+
};
|
130
|
+
|
131
|
+
declare module '@ckeditor/ckeditor5-core' {
|
132
|
+
interface CommandsMap {
|
133
|
+
highlight: HighlightCommand;
|
134
|
+
}
|
135
|
+
|
136
|
+
interface PluginsMap {
|
137
|
+
[ HighlightEditing.pluginName ]: HighlightEditing;
|
138
|
+
}
|
139
|
+
}
|