@@ -55,36 +55,106 @@ export class FontWatcher implements IWatcher {
55
55
this . updateFont ( ) ;
56
56
this . dispatcherRef = dis . register ( this . onAction ) ;
57
57
/**
58
- * TODO To change before review
59
58
* baseFontSize is an account level setting which is loaded after the initial
60
59
* sync. Hence why we can't do that in the `constructor`
61
60
*/
62
61
await this . migrateBaseFontSize ( ) ;
63
62
}
64
63
65
64
/**
66
- * Migrating the old `baseFontSize` for Compound.
67
- * Everything will becomes slightly larger, and getting rid of the `SIZE_DIFF`
68
- * weirdness for locally persisted values
65
+ * Migrate the base font size from the V1 and V2 version to the V3 version
66
+ * @private
69
67
*/
70
68
private async migrateBaseFontSize ( ) : Promise < void > {
71
- const legacyBaseFontSize = SettingsStore . getValue ( "baseFontSize" ) ;
72
- if ( legacyBaseFontSize ) {
73
- console . log ( "Migrating base font size for Compound, current value" , legacyBaseFontSize ) ;
74
-
75
- // For some odd reason, the persisted value in user storage has an offset
76
- // of 5 pixels for all values stored under `baseFontSize`
77
- const LEGACY_SIZE_DIFF = 5 ;
78
- // Compound uses a base font size of `16px`, whereas the old Element
79
- // styles based their calculations off a `15px` root font size.
80
- const ROOT_FONT_SIZE_INCREASE = 1 ;
81
-
82
- const baseFontSize = legacyBaseFontSize + ROOT_FONT_SIZE_INCREASE + LEGACY_SIZE_DIFF ;
83
-
84
- await SettingsStore . setValue ( "baseFontSizeV2" , null , SettingLevel . DEVICE , baseFontSize ) ;
85
- await SettingsStore . setValue ( "baseFontSize" , null , SettingLevel . DEVICE , "" ) ;
86
- console . log ( "Migration complete, deleting legacy `baseFontSize`" ) ;
87
- }
69
+ await this . migrateBaseFontV1toV3 ( ) ;
70
+ await this . migrateBaseFontV2toV3 ( ) ;
71
+ }
72
+
73
+ /**
74
+ * Migrating from the V1 version of the base font size to the V3 version
75
+ * The V3 is using the default browser font size as a base
76
+ * Everything will become slightly larger, and getting rid of the `SIZE_DIFF`
77
+ * weirdness for locally persisted values
78
+ * @private
79
+ */
80
+ private async migrateBaseFontV1toV3 ( ) : Promise < void > {
81
+ const legacyBaseFontSize = SettingsStore . getValue < number > ( "baseFontSize" ) ;
82
+ // No baseFontV1 found, nothing to migrate
83
+ if ( ! legacyBaseFontSize ) return ;
84
+
85
+ console . log (
86
+ "Migrating base font size -> base font size V2 -> base font size V3 for Compound, current value" ,
87
+ legacyBaseFontSize ,
88
+ ) ;
89
+
90
+ // Compute the new font size of the V2 version before migrating to V3
91
+ const baseFontSizeV2 = this . computeBaseFontSizeV1toV2 ( legacyBaseFontSize ) ;
92
+
93
+ // Compute the difference between the V2 and the V3 version
94
+ const deltaV3 = this . computeFontSizeDeltaFromV2BaseFont ( baseFontSizeV2 ) ;
95
+
96
+ await SettingsStore . setValue ( "baseFontSizeV3" , null , SettingLevel . DEVICE , deltaV3 ) ;
97
+ await SettingsStore . setValue ( "baseFontSize" , null , SettingLevel . DEVICE , 0 ) ;
98
+ console . log ( "Migration complete, deleting legacy `baseFontSize`" ) ;
99
+ }
100
+
101
+ /**
102
+ * Migrating from the V2 version of the base font size to the V3 version
103
+ * @private
104
+ */
105
+ private async migrateBaseFontV2toV3 ( ) : Promise < void > {
106
+ const legacyBaseFontV2Size = SettingsStore . getValue < number > ( "baseFontSizeV2" ) ;
107
+ // No baseFontV2 found, nothing to migrate
108
+ if ( ! legacyBaseFontV2Size ) return ;
109
+
110
+ console . log ( "Migrating base font size V2 for Compound, current value" , legacyBaseFontV2Size ) ;
111
+
112
+ // Compute the difference between the V2 and the V3 version
113
+ const deltaV3 = this . computeFontSizeDeltaFromV2BaseFont ( legacyBaseFontV2Size ) ;
114
+
115
+ await SettingsStore . setValue ( "baseFontSizeV3" , null , SettingLevel . DEVICE , deltaV3 ) ;
116
+ await SettingsStore . setValue ( "baseFontSizeV2" , null , SettingLevel . DEVICE , 0 ) ;
117
+ console . log ( "Migration complete, deleting legacy `baseFontSizeV2`" ) ;
118
+ }
119
+
120
+ /**
121
+ * Compute the V2 font size from the V1 font size
122
+ * @param legacyBaseFontSize
123
+ * @private
124
+ */
125
+ private computeBaseFontSizeV1toV2 ( legacyBaseFontSize : number ) : number {
126
+ // For some odd reason, the persisted value in user storage has an offset
127
+ // of 5 pixels for all values stored under `baseFontSize`
128
+ const LEGACY_SIZE_DIFF = 5 ;
129
+
130
+ // Compound uses a base font size of `16px`, whereas the old Element
131
+ // styles based their calculations off a `15px` root font size.
132
+ const ROOT_FONT_SIZE_INCREASE = 1 ;
133
+
134
+ // Compute the font size of the V2 version before migrating to V3
135
+ return legacyBaseFontSize + ROOT_FONT_SIZE_INCREASE + LEGACY_SIZE_DIFF ;
136
+ }
137
+
138
+ /**
139
+ * Compute the difference between the V2 font size and the default browser font size
140
+ * @param legacyBaseFontV2Size
141
+ * @private
142
+ */
143
+ private computeFontSizeDeltaFromV2BaseFont ( legacyBaseFontV2Size : number ) : number {
144
+ const browserDefaultFontSize = this . getBrowserDefaultFontSize ( ) ;
145
+
146
+ // Compute the difference between the V2 font size and the default browser font size
147
+ return legacyBaseFontV2Size - browserDefaultFontSize ;
148
+ }
149
+
150
+ /**
151
+ * Get the default font size of the browser
152
+ * Fallback to 16px if the value is not found
153
+ * @private
154
+ * @returns {number } the value of ${@link DEFAULT_SIZE} in pixels
155
+ */
156
+ private getBrowserDefaultFontSize ( ) : number {
157
+ return parseInt ( window . getComputedStyle ( document . documentElement ) . getPropertyValue ( "font-size" ) , 10 ) || 16 ;
88
158
}
89
159
90
160
public stop ( ) : void {
@@ -123,6 +193,10 @@ export class FontWatcher implements IWatcher {
123
193
}
124
194
} ;
125
195
196
+ /**
197
+ * Set the root font size of the document
198
+ * @param delta {number} the delta to add to the default font size
199
+ */
126
200
private setRootFontSize = async ( delta : number ) : Promise < void > => {
127
201
// Check that the new delta doesn't exceed the limits
128
202
const fontDelta = clamp ( delta , FontWatcher . MIN_DELTA , FontWatcher . MAX_DELTA ) ;
0 commit comments