-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
highlightstack.ts
179 lines (152 loc) · 5.17 KB
/
highlightstack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module widget/highlightstack
*/
import { EmitterMixin, type ArrayOrItem } from '@ckeditor/ckeditor5-utils';
import type { DowncastWriter, HighlightDescriptor } from '@ckeditor/ckeditor5-engine';
/**
* Class used to handle the correct order of highlights on elements.
*
* When different highlights are applied to same element the correct order should be preserved:
*
* * highlight with highest priority should be applied,
* * if two highlights have same priority - sort by CSS class provided in
* {@link module:engine/conversion/downcasthelpers~HighlightDescriptor}.
*
* This way, highlight will be applied with the same rules it is applied on texts.
*/
export default class HighlightStack extends EmitterMixin() {
private readonly _stack: Array<HighlightDescriptor> = [];
/**
* Adds highlight descriptor to the stack.
*
* @fires change:top
*/
public add( descriptor: HighlightDescriptor, writer: DowncastWriter ): void {
const stack = this._stack;
// Save top descriptor and insert new one. If top is changed - fire event.
const oldTop = stack[ 0 ];
this._insertDescriptor( descriptor );
const newTop = stack[ 0 ];
// When new object is at the top and stores different information.
if ( oldTop !== newTop && !compareDescriptors( oldTop, newTop ) ) {
this.fire<HighlightStackChangeEvent>( 'change:top', {
oldDescriptor: oldTop,
newDescriptor: newTop,
writer
} );
}
}
/**
* Removes highlight descriptor from the stack.
*
* @fires change:top
* @param id Id of the descriptor to remove.
*/
public remove( id: string, writer: DowncastWriter ): void {
const stack = this._stack;
const oldTop = stack[ 0 ];
this._removeDescriptor( id );
const newTop = stack[ 0 ];
// When new object is at the top and stores different information.
if ( oldTop !== newTop && !compareDescriptors( oldTop, newTop ) ) {
this.fire<HighlightStackChangeEvent>( 'change:top', {
oldDescriptor: oldTop,
newDescriptor: newTop,
writer
} );
}
}
/**
* Inserts a given descriptor in correct place in the stack. It also takes care about updating information
* when descriptor with same id is already present.
*/
private _insertDescriptor( descriptor: HighlightDescriptor ) {
const stack = this._stack;
const index = stack.findIndex( item => item.id === descriptor.id );
// Inserting exact same descriptor - do nothing.
if ( compareDescriptors( descriptor, stack[ index ] ) ) {
return;
}
// If descriptor with same id but with different information is on the stack - remove it.
if ( index > -1 ) {
stack.splice( index, 1 );
}
// Find correct place to insert descriptor in the stack.
// It has different information (for example priority) so it must be re-inserted in correct place.
let i = 0;
while ( stack[ i ] && shouldABeBeforeB( stack[ i ], descriptor ) ) {
i++;
}
stack.splice( i, 0, descriptor );
}
/**
* Removes descriptor with given id from the stack.
*
* @param id Descriptor's id.
*/
private _removeDescriptor( id: string ) {
const stack = this._stack;
const index = stack.findIndex( item => item.id === id );
// If descriptor with same id is on the list - remove it.
if ( index > -1 ) {
stack.splice( index, 1 );
}
}
}
/**
* Compares two descriptors by checking their priority and class list.
*
* @returns Returns true if both descriptors are defined and have same priority and classes.
*/
function compareDescriptors( a: HighlightDescriptor, b: HighlightDescriptor ) {
return a && b && a.priority == b.priority && classesToString( a.classes ) == classesToString( b.classes );
}
/**
* Checks whenever first descriptor should be placed in the stack before second one.
*/
function shouldABeBeforeB( a: HighlightDescriptor, b: HighlightDescriptor ) {
if ( a.priority! > b.priority! ) {
return true;
} else if ( a.priority! < b.priority! ) {
return false;
}
// When priorities are equal and names are different - use classes to compare.
return classesToString( a.classes ) > classesToString( b.classes );
}
/**
* Converts CSS classes passed with {@link module:engine/conversion/downcasthelpers~HighlightDescriptor} to
* sorted string.
*/
function classesToString( classes: ArrayOrItem<string> ) {
return Array.isArray( classes ) ? classes.sort().join( ',' ) : classes;
}
/**
* Fired when top element on {@link module:widget/highlightstack~HighlightStack} has been changed
*
* @eventName ~HighlightStack#change:top
*/
export type HighlightStackChangeEvent = {
name: 'change' | 'change:top';
args: [ HighlightStackChangeEventData ];
};
/**
* Additional information about the change.
*/
export type HighlightStackChangeEventData = {
/**
* Old highlight descriptor. It will be `undefined` when first descriptor is added to the stack.
*/
oldDescriptor: HighlightDescriptor;
/**
* New highlight descriptor. It will be `undefined` when last descriptor is removed from the stack.
*/
newDescriptor: HighlightDescriptor;
/**
* View writer that can be used to modify element.
*/
writer: DowncastWriter;
};