forked from Tencent/tmagic-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
roymondchen
committed
Aug 1, 2024
1 parent
9f80881
commit 59f0527
Showing
4 changed files
with
85 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export default class Flexible { | ||
public designWidth = 375; | ||
private tid: NodeJS.Timeout | undefined; | ||
|
||
constructor(options?: { designWidth?: number }) { | ||
if (globalThis.document.readyState === 'complete') { | ||
this.setBodyFontSize(); | ||
} else { | ||
globalThis.document.addEventListener('DOMContentLoaded', this.setBodyFontSize, false); | ||
} | ||
|
||
globalThis.addEventListener('resize', this.resizeHandler, false); | ||
globalThis.addEventListener('pageshow', this.pageshowHandler, false); | ||
|
||
if (typeof options?.designWidth !== 'undefined') { | ||
this.setDesignWidth(options.designWidth); | ||
} | ||
} | ||
|
||
public destroy() { | ||
globalThis.document.removeEventListener('DOMContentLoaded', this.setBodyFontSize, false); | ||
globalThis.removeEventListener('resize', this.resizeHandler, false); | ||
globalThis.removeEventListener('pageshow', this.pageshowHandler, false); | ||
} | ||
|
||
public setDesignWidth(width: number) { | ||
this.designWidth = width; | ||
this.refreshRem(); | ||
} | ||
|
||
public setBodyFontSize() { | ||
globalThis.document.body.style.fontSize = '.12rem'; | ||
} | ||
|
||
public refreshRem() { | ||
const { width } = document.documentElement.getBoundingClientRect(); | ||
const fontSize = width / (this.designWidth / 100); | ||
globalThis.document.documentElement.style.fontSize = `${fontSize}px`; | ||
globalThis.document.documentElement.style.fontSize = `${this.correctRem(fontSize)}px`; | ||
} | ||
|
||
/** | ||
* 纠正由于文字缩放导致的字体大小计算不正确问题 | ||
* @param {number} fontSize | ||
* @returns {number} | ||
*/ | ||
public correctRem(fontSize: number) { | ||
const { document } = globalThis; | ||
const d = document.createElement('div'); | ||
d.style.cssText = 'width:1rem;height:0;overflow:hidden;position:absolute;z-index:-1;visibility:hidden;'; | ||
document.documentElement.appendChild(d); | ||
const dw = d.offsetWidth; | ||
document.documentElement.removeChild(d); | ||
if (Math.abs(dw - fontSize) > 1) { | ||
return fontSize ** 2 / dw; | ||
} | ||
return fontSize; | ||
} | ||
|
||
private resizeHandler = () => { | ||
clearTimeout(this.tid); | ||
this.tid = setTimeout(() => { | ||
this.refreshRem(); | ||
}, 300); | ||
}; | ||
|
||
private pageshowHandler = (e: PageTransitionEvent) => { | ||
if (e.persisted) { | ||
this.resizeHandler(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters