mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-10-15 12:02:18 +08:00
feat: 水印支持动态关闭和打开
This commit is contained in:
parent
6c9620975b
commit
ac4ea95b7f
@ -49,6 +49,14 @@ import { createWatermark } from '@fesjs/fes'
|
|||||||
createWatermark({ content: '我是水印' });
|
createWatermark({ content: '我是水印' });
|
||||||
```
|
```
|
||||||
|
|
||||||
|
若需要动态关闭和重新打开水印,则:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const { close, open } = createWatermark({ content: '我是水印' });
|
||||||
|
|
||||||
|
close(); // 关闭水印
|
||||||
|
open(); // 重新打开水印
|
||||||
|
```
|
||||||
|
|
||||||
默认参数是:
|
默认参数是:
|
||||||
```js
|
```js
|
||||||
|
@ -30,6 +30,36 @@ function timeFormat(date, format = 'YYYY-MM-DD') {
|
|||||||
return format.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Q/g, str => String(map[str]));
|
return format.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Q/g, str => String(map[str]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let _wmEnable = true; // 开关控制
|
||||||
|
let _wmMo = null; // MutationObserver
|
||||||
|
let _wmTimer = null; // timestamp
|
||||||
|
|
||||||
|
function _close(param) {
|
||||||
|
// 开关关闭
|
||||||
|
_wmEnable = false;
|
||||||
|
|
||||||
|
// 监听器关闭
|
||||||
|
_wmMo && _wmMo.disconnect();
|
||||||
|
_wmMo = null;
|
||||||
|
_wmTimer && clearTimeout(_wmTimer);
|
||||||
|
_wmTimer = null;
|
||||||
|
|
||||||
|
// 删除水印元素
|
||||||
|
const __wm = document.querySelector('.__wm');
|
||||||
|
__wm && param.container.removeChild(__wm);
|
||||||
|
}
|
||||||
|
function _open(param) {
|
||||||
|
// 避免重复触发
|
||||||
|
_close(param);
|
||||||
|
|
||||||
|
// 开关打开
|
||||||
|
_wmEnable = true;
|
||||||
|
|
||||||
|
// 生成水印
|
||||||
|
// eslint-disable-next-line no-use-before-define
|
||||||
|
createWatermark(param);
|
||||||
|
}
|
||||||
|
|
||||||
// canvas 实现 watermark
|
// canvas 实现 watermark
|
||||||
export function createWatermark({
|
export function createWatermark({
|
||||||
container = document.body,
|
container = document.body,
|
||||||
@ -63,6 +93,9 @@ export function createWatermark({
|
|||||||
zIndex,
|
zIndex,
|
||||||
timestamp
|
timestamp
|
||||||
};
|
};
|
||||||
|
if (!_wmEnable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const canvas = document.createElement('canvas');
|
const canvas = document.createElement('canvas');
|
||||||
canvas.setAttribute('width', `${width}px`);
|
canvas.setAttribute('width', `${width}px`);
|
||||||
canvas.setAttribute('height', `${height}px`);
|
canvas.setAttribute('height', `${height}px`);
|
||||||
@ -108,17 +141,17 @@ export function createWatermark({
|
|||||||
|
|
||||||
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
||||||
if (MutationObserver) {
|
if (MutationObserver) {
|
||||||
let mo = new MutationObserver(() => {
|
_wmMo = new MutationObserver(() => {
|
||||||
__wm = document.querySelector('.__wm');
|
__wm = document.querySelector('.__wm');
|
||||||
if ((__wm && __wm.getAttribute('style') !== styleStr) || !__wm) {
|
if ((__wm && __wm.getAttribute('style') !== styleStr) || !__wm) {
|
||||||
// 避免一直触发
|
// 避免一直触发
|
||||||
mo.disconnect();
|
_wmMo.disconnect();
|
||||||
mo = null;
|
_wmMo = null;
|
||||||
createWatermark(param);
|
createWatermark(param);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mo.observe(container, {
|
_wmMo.observe(container, {
|
||||||
attributes: true,
|
attributes: true,
|
||||||
subtree: true,
|
subtree: true,
|
||||||
childList: true
|
childList: true
|
||||||
@ -135,9 +168,14 @@ export function createWatermark({
|
|||||||
timeout = 1000 * 60 * 60;
|
timeout = 1000 * 60 * 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
_wmTimer = setTimeout(() => {
|
||||||
// 触发MutationObserver
|
// 触发MutationObserver
|
||||||
watermarkDiv.style.bottom = '0';
|
watermarkDiv.style.bottom = '0';
|
||||||
}, timeout);
|
}, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
close: () => _close(param),
|
||||||
|
open: () => _open(param)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user