14
14
*/
15
15
16
16
import {
17
+ AnnotationEditorParamsType ,
17
18
AnnotationEditorType ,
18
19
assert ,
19
20
LINE_FACTOR ,
20
21
} from "../../shared/util.js" ;
21
22
import { AnnotationEditor } from "./editor.js" ;
22
23
import { bindEvents } from "./tools.js" ;
24
+ import { getRGB } from "../display_utils.js" ;
23
25
24
26
/**
25
27
* Basic text editor in order to create a FreeTex annotation.
@@ -41,10 +43,14 @@ class FreeTextEditor extends AnnotationEditor {
41
43
42
44
static _internalPadding = 0 ;
43
45
46
+ static _defaultFontSize = 10 ;
47
+
48
+ static _defaultColor = "CanvasText" ;
49
+
44
50
constructor ( params ) {
45
51
super ( { ...params , name : "freeTextEditor" } ) ;
46
- this . #color = params . color || "CanvasText" ;
47
- this . #fontSize = params . fontSize || 10 ;
52
+ this . #color = params . color || FreeTextEditor . _defaultColor ;
53
+ this . #fontSize = params . fontSize || FreeTextEditor . _defaultFontSize ;
48
54
}
49
55
50
56
static initialize ( l10n ) {
@@ -89,6 +95,94 @@ class FreeTextEditor extends AnnotationEditor {
89
95
return editor ;
90
96
}
91
97
98
+ static updateDefaultParams ( type , value ) {
99
+ switch ( type ) {
100
+ case AnnotationEditorParamsType . FREETEXT_SIZE :
101
+ FreeTextEditor . _defaultFontSize = value ;
102
+ break ;
103
+ case AnnotationEditorParamsType . FREETEXT_COLOR :
104
+ FreeTextEditor . _defaultColor = value ;
105
+ break ;
106
+ }
107
+ }
108
+
109
+ /** @inheritdoc */
110
+ updateParams ( type , value ) {
111
+ switch ( type ) {
112
+ case AnnotationEditorParamsType . FREETEXT_SIZE :
113
+ this . #updateFontSize( value ) ;
114
+ break ;
115
+ case AnnotationEditorParamsType . FREETEXT_COLOR :
116
+ this . #updateColor( value ) ;
117
+ break ;
118
+ }
119
+ }
120
+
121
+ static get defaultPropertiesToUpdate ( ) {
122
+ return [
123
+ [
124
+ AnnotationEditorParamsType . FREETEXT_SIZE ,
125
+ FreeTextEditor . _defaultFontSize ,
126
+ ] ,
127
+ [ AnnotationEditorParamsType . FREETEXT_COLOR , FreeTextEditor . _defaultColor ] ,
128
+ ] ;
129
+ }
130
+
131
+ /** @inheritdoc */
132
+ get propertiesToUpdate ( ) {
133
+ return [
134
+ [ AnnotationEditorParamsType . FREETEXT_SIZE , this . #fontSize] ,
135
+ [ AnnotationEditorParamsType . FREETEXT_COLOR , this . #color] ,
136
+ ] ;
137
+ }
138
+
139
+ /**
140
+ * Update the font size and make this action as undoable.
141
+ * @param {number } fontSize
142
+ */
143
+ #updateFontSize( fontSize ) {
144
+ const setFontsize = size => {
145
+ this . editorDiv . style . fontSize = `calc(${ size } px * var(--scale-factor))` ;
146
+ this . translate ( 0 , - ( size - this . #fontSize) * this . parent . scaleFactor ) ;
147
+ this . #fontSize = size ;
148
+ } ;
149
+ const savedFontsize = this . #fontSize;
150
+ this . parent . addCommands ( {
151
+ cmd : ( ) => {
152
+ setFontsize ( fontSize ) ;
153
+ } ,
154
+ undo : ( ) => {
155
+ setFontsize ( savedFontsize ) ;
156
+ } ,
157
+ mustExec : true ,
158
+ type : AnnotationEditorParamsType . FREETEXT_SIZE ,
159
+ overwriteIfSameType : true ,
160
+ keepUndo : true ,
161
+ } ) ;
162
+ }
163
+
164
+ /**
165
+ * Update the color and make this action undoable.
166
+ * @param {string } color
167
+ */
168
+ #updateColor( color ) {
169
+ const savedColor = this . #color;
170
+ this . parent . addCommands ( {
171
+ cmd : ( ) => {
172
+ this . #color = color ;
173
+ this . editorDiv . style . color = color ;
174
+ } ,
175
+ undo : ( ) => {
176
+ this . #color = savedColor ;
177
+ this . editorDiv . style . color = savedColor ;
178
+ } ,
179
+ mustExec : true ,
180
+ type : AnnotationEditorParamsType . FREETEXT_COLOR ,
181
+ overwriteIfSameType : true ,
182
+ keepUndo : true ,
183
+ } ) ;
184
+ }
185
+
92
186
/** @inheritdoc */
93
187
getInitialTranslation ( ) {
94
188
// The start of the base line is where the user clicked.
@@ -116,13 +210,15 @@ class FreeTextEditor extends AnnotationEditor {
116
210
enableEditMode ( ) {
117
211
super . enableEditMode ( ) ;
118
212
this . overlayDiv . classList . remove ( "enabled" ) ;
213
+ this . editorDiv . contentEditable = true ;
119
214
this . div . draggable = false ;
120
215
}
121
216
122
217
/** @inheritdoc */
123
218
disableEditMode ( ) {
124
219
super . disableEditMode ( ) ;
125
220
this . overlayDiv . classList . add ( "enabled" ) ;
221
+ this . editorDiv . contentEditable = false ;
126
222
this . div . draggable = true ;
127
223
}
128
224
@@ -223,7 +319,7 @@ class FreeTextEditor extends AnnotationEditor {
223
319
this . editorDiv . contentEditable = true ;
224
320
225
321
const { style } = this . editorDiv ;
226
- style . fontSize = `${ this . #fontSize} % ` ;
322
+ style . fontSize = `calc( ${ this . #fontSize} px * var(--scale-factor)) ` ;
227
323
style . color = this . #color;
228
324
229
325
this . div . append ( this . editorDiv ) ;
@@ -248,6 +344,7 @@ class FreeTextEditor extends AnnotationEditor {
248
344
) ;
249
345
// eslint-disable-next-line no-unsanitized/property
250
346
this . editorDiv . innerHTML = this . #contentHTML;
347
+ this . div . draggable = true ;
251
348
}
252
349
253
350
return this . div ;
@@ -258,9 +355,12 @@ class FreeTextEditor extends AnnotationEditor {
258
355
const padding = FreeTextEditor . _internalPadding * this . parent . scaleFactor ;
259
356
const rect = this . getRect ( padding , padding ) ;
260
357
358
+ // We don't use this.#color directly because it can be CanvasText.
359
+ const color = getRGB ( getComputedStyle ( this . editorDiv ) . color ) ;
360
+
261
361
return {
262
362
annotationType : AnnotationEditorType . FREETEXT ,
263
- color : [ 0 , 0 , 0 ] ,
363
+ color,
264
364
fontSize : this . #fontSize,
265
365
value : this . #content,
266
366
pageIndex : this . parent . pageIndex ,
0 commit comments