@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.
@@ -1,111 +1,91 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
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
- * editor.execute( 'highlight', { value: 'greenMarker' } );
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
- * @inheritDoc
27
- */
28
- refresh() {
29
- const model = this.editor.model;
30
- const doc = model.document;
31
-
32
- /**
33
- * A value indicating whether the command is active. If the selection has some highlight attribute,
34
- * it corresponds to the value of that attribute.
35
- *
36
- * @observable
37
- * @readonly
38
- * @member {undefined|String} module:highlight/highlightcommand~HighlightCommand#value
39
- */
40
- this.value = doc.selection.getAttribute( 'highlight' );
41
- this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
42
- }
43
-
44
- /**
45
- * Executes the command.
46
- *
47
- * @param {Object} [options] Options for the executed command.
48
- * @param {String} [options.value] The value to apply.
49
- *
50
- * @fires execute
51
- */
52
- execute( options = {} ) {
53
- const model = this.editor.model;
54
- const document = model.document;
55
- const selection = document.selection;
56
-
57
- const highlighter = options.value;
58
-
59
- model.change( writer => {
60
- if ( selection.isCollapsed ) {
61
- const position = selection.getFirstPosition();
62
-
63
- // When selection is inside text with `highlight` attribute.
64
- if ( selection.hasAttribute( 'highlight' ) ) {
65
- // Find the full highlighted range.
66
- const isSameHighlight = value => {
67
- return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
68
- };
69
-
70
- const highlightStart = position.getLastMatchingPosition( isSameHighlight, { direction: 'backward' } );
71
- const highlightEnd = position.getLastMatchingPosition( isSameHighlight );
72
-
73
- const highlightRange = writer.createRange( highlightStart, highlightEnd );
74
-
75
- // Then depending on current value...
76
- if ( !highlighter || this.value === highlighter ) {
77
- // ...remove attribute when passing highlighter different then current or executing "eraser".
78
-
79
- // If we're at the end of the highlighted range, we don't want to remove highlight of the range.
80
- if ( !position.isEqual( highlightEnd ) ) {
81
- writer.removeAttribute( 'highlight', highlightRange );
82
- }
83
-
84
- writer.removeSelectionAttribute( 'highlight' );
85
- } else {
86
- // ...update `highlight` value.
87
-
88
- // If we're at the end of the highlighted range, we don't want to change the highlight of the range.
89
- if ( !position.isEqual( highlightEnd ) ) {
90
- writer.setAttribute( 'highlight', highlighter, highlightRange );
91
- }
92
-
93
- writer.setSelectionAttribute( 'highlight', highlighter );
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
+ }
@@ -1,124 +1,109 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
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
- * @inheritDoc
25
- */
26
- static get pluginName() {
27
- return 'HighlightEditing';
28
- }
29
-
30
- /**
31
- * @inheritDoc
32
- */
33
- constructor( editor ) {
34
- super( editor );
35
-
36
- editor.config.define( 'highlight', {
37
- options: [
38
- {
39
- model: 'yellowMarker',
40
- class: 'marker-yellow',
41
- title: 'Yellow marker',
42
- color: 'var(--ck-highlight-marker-yellow)',
43
- type: 'marker'
44
- },
45
- {
46
- model: 'greenMarker',
47
- class: 'marker-green',
48
- title: 'Green marker',
49
- color: 'var(--ck-highlight-marker-green)',
50
- type: 'marker'
51
- },
52
- {
53
- model: 'pinkMarker',
54
- class: 'marker-pink',
55
- title: 'Pink marker',
56
- color: 'var(--ck-highlight-marker-pink)',
57
- type: 'marker'
58
- },
59
- {
60
- model: 'blueMarker',
61
- class: 'marker-blue',
62
- title: 'Blue marker',
63
- color: 'var(--ck-highlight-marker-blue)',
64
- type: 'marker'
65
- },
66
- {
67
- model: 'redPen',
68
- class: 'pen-red',
69
- title: 'Red pen',
70
- color: 'var(--ck-highlight-pen-red)',
71
- type: 'pen'
72
- },
73
- {
74
- model: 'greenPen',
75
- class: 'pen-green',
76
- title: 'Green pen',
77
- color: 'var(--ck-highlight-pen-green)',
78
- type: 'pen'
79
- }
80
- ]
81
- } );
82
- }
83
-
84
- /**
85
- * @inheritDoc
86
- */
87
- init() {
88
- const editor = this.editor;
89
-
90
- // Allow highlight attribute on text nodes.
91
- editor.model.schema.extend( '$text', { allowAttributes: 'highlight' } );
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
- // Converts the options array to a converter definition.
103
- //
104
- // @param {Array.<module:highlight/highlight~HighlightOption>} options An array with configured options.
105
- // @returns {module:engine/conversion/conversion~ConverterDefinition}
106
- function _buildDefinition( options ) {
107
- const definition = {
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;
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
+ }