@@ -55,36 +55,96 @@ export class FontWatcher implements IWatcher {
5555 this . updateFont ( ) ;
5656 this . dispatcherRef = dis . register ( this . onAction ) ;
5757 /**
58- * TODO To change before review
5958 * baseFontSize is an account level setting which is loaded after the initial
6059 * sync. Hence why we can't do that in the `constructor`
6160 */
6261 await this . migrateBaseFontSize ( ) ;
6362 }
6463
6564 /**
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
6967 */
7068 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 . computeBaseFontV2toV3 ( 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 . computeBaseFontV2toV3 ( 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+ private computeBaseFontSizeV1toV2 ( legacyBaseFontSize : number ) : number {
121+ // For some odd reason, the persisted value in user storage has an offset
122+ // of 5 pixels for all values stored under `baseFontSize`
123+ const LEGACY_SIZE_DIFF = 5 ;
124+
125+ // Compound uses a base font size of `16px`, whereas the old Element
126+ // styles based their calculations off a `15px` root font size.
127+ const ROOT_FONT_SIZE_INCREASE = 1 ;
128+
129+ // Compute the font size of the V2 version before migrating to V3
130+ return legacyBaseFontSize + ROOT_FONT_SIZE_INCREASE + LEGACY_SIZE_DIFF ;
131+ }
132+
133+ private computeBaseFontV2toV3 ( legacyBaseFontV2Size : number ) : number {
134+ const browserDefaultFontSize = this . getBrowserDefaultFontSize ( ) ;
135+
136+ // Compute the difference between the V2 font size and the default browser font size
137+ return legacyBaseFontV2Size - browserDefaultFontSize ;
138+ }
139+
140+ /**
141+ * Get the default font size of the browser
142+ * Fallback to 16px if the value is not found
143+ * @private
144+ * @returns {number } the value of ${@link DEFAULT_SIZE} in pixels
145+ */
146+ private getBrowserDefaultFontSize ( ) : number {
147+ return parseInt ( window . getComputedStyle ( document . documentElement ) . getPropertyValue ( "font-size" ) , 10 ) || 16 ;
88148 }
89149
90150 public stop ( ) : void {
@@ -123,6 +183,10 @@ export class FontWatcher implements IWatcher {
123183 }
124184 } ;
125185
186+ /**
187+ * Set the root font size of the document
188+ * @param delta {number} the delta to add to the default font size
189+ */
126190 private setRootFontSize = async ( delta : number ) : Promise < void > => {
127191 // Check that the new delta doesn't exceed the limits
128192 const fontDelta = clamp ( delta , FontWatcher . MIN_DELTA , FontWatcher . MAX_DELTA ) ;
0 commit comments