Skip to content

Commit 30ab0b3

Browse files
authored
feat(Watermark): add IsPage parameter (#5793)
* refactor: support isPage option * feat: 支持整页面水印 * refactor: 完善判断空逻辑 * feat: 怎加 is-page 样式 * feat: 增加 IsPage 参数 * doc: 增加文档 * refactor: 增加样式 * feat: 增加报错功能 * test: 更新单元测试
1 parent bda6a4f commit 30ab0b3

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
lines changed

src/BootstrapBlazor.Server/Components/Samples/Watermarks.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
<DemoBlock Title="@Localizer["WatermarkNormalTitle"]" Introduction="@Localizer["WatermarkNormalIntro"]" Name="Normal">
99
<section ignore>
10+
@((MarkupString)Localizer["WatermarkDescription"].Value)
11+
<Pre>&lt;Layout&gt;&lt;/Layout&gt;
12+
&lt;Watermark IsPage="true" Text="BootstrapBlazor" FontSize="20" Color="#ddd" Rotate="-45" Gap="40"&gt;&lt;/Watermark&gt;</Pre>
1013
<div class="row form-inline g-3">
1114
<div class="col-12 col-sm-6">
1215
<BootstrapInputGroup>

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7017,7 +7017,8 @@
70177017
"WatermarkTitle": "Watermark",
70187018
"WatermarkIntro": "Add specific text or patterns to the page",
70197019
"WatermarkNormalTitle": "Basic usage",
7020-
"WatermarkNormalIntro": "Use the <code>Text</code> property to set a string to specify the watermark text"
7020+
"WatermarkNormalIntro": "Use the <code>Text</code> property to set a string to specify the watermark text",
7021+
"WatermarkDescription": "<p>How to add a watermark globally</p><p>You can add a watermark component to the template page <code>MainLayout</code> and set <code>IsPage=\"true\"</code></p>"
70217022
},
70227023
"BootstrapBlazor.Server.Data.AttributeItem": {
70237024
"Name": "Name",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7017,7 +7017,8 @@
70177017
"WatermarkTitle": "Watermark 水印组件",
70187018
"WatermarkIntro": "在页面上添加文本或图片等水印信息",
70197019
"WatermarkNormalTitle": "基础用法",
7020-
"WatermarkNormalIntro": "使用 <code>Text</code> 属性设置一个字符串指定水印内容"
7020+
"WatermarkNormalIntro": "使用 <code>Text</code> 属性设置一个字符串指定水印内容",
7021+
"WatermarkDescription": "<p>全局增加水印实现方法</p><p>可以在模板页 <code>MainLayout</code> 中加水印组件并设置 <code>IsPage=\"true\"</code> 即可</p>"
70217022
},
70227023
"BootstrapBlazor.Server.Data.AttributeItem": {
70237024
"Name": "参数",

src/BootstrapBlazor/Components/Watermark/Watermark.razor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ public partial class Watermark
5353
[Parameter]
5454
public int? Gap { get; set; }
5555

56+
/// <summary>
57+
/// 获得/设置 是否为整页面水印 默认 false
58+
/// </summary>
59+
[Parameter]
60+
public bool IsPage { get; set; }
61+
5662
private string? ClassString => CssBuilder.Default("bb-watermark")
63+
.AddClass("is-page", IsPage)
5764
.AddClassFromAttributes(AdditionalAttributes)
5865
.Build();
5966

@@ -65,6 +72,11 @@ protected override void OnParametersSet()
6572
base.OnParametersSet();
6673

6774
Text ??= "BootstrapBlazor";
75+
76+
if(IsPage && ChildContent is not null)
77+
{
78+
throw new InvalidOperationException($"{nameof(IsPage)} is true, {nameof(ChildContent)} cannot be set.");
79+
}
6880
}
6981

7082
/// <summary>
@@ -95,6 +107,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
95107
Color,
96108
Rotate,
97109
Gap,
98-
ZIndex
110+
ZIndex,
111+
IsPage
99112
};
100113
}

src/BootstrapBlazor/Components/Watermark/Watermark.razor.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const createWatermark = watermark => {
7171
fontSize: 16,
7272
text: 'BootstrapBlazor',
7373
rotate: -40,
74-
color: '#0000004d'
74+
color: '#0000004d',
75+
zIndex: '9999'
7576
};
7677

7778
for (const key in options) {
@@ -90,31 +91,40 @@ const createWatermark = watermark => {
9091
div.style.opacity = '1';
9192
div.style.position = 'absolute';
9293
div.style.inset = '0';
93-
div.style.zIndex = '9999';
9494
div.classList.add("bb-watermark-bg");
9595

96+
if (options.zIndex === void 0) {
97+
options.zIndex = defaults.zIndex;
98+
}
99+
div.style.zIndex = options.zIndex;
100+
96101
const mark = el.querySelector('.bb-watermark-bg');
97102
if (mark) {
98103
mark.remove();
99104
}
100-
el.appendChild(div);
101105

106+
if (bg.isPage) {
107+
document.body.appendChild(div);
108+
}
109+
else {
110+
el.appendChild(div);
111+
}
102112
options.bg = bg;
103113
requestAnimationFrame(() => monitor(watermark));
104114
}
105115

106116
const monitor = watermark => {
107-
const { el, options, ob } = watermark;
117+
const { el, options } = watermark;
108118
if (el === null) {
109119
return;
110120
}
111121

112-
if (el.children.length !== 2) {
122+
if (options.isPage === false && el.children.length !== 2) {
113123
clearWatermark(watermark);
114124
return;
115125
}
116126

117-
const mark = el.children[1];
127+
const mark = options.isPage ? el.children[0] : el.children[1];
118128
if (mark.className !== 'bb-watermark-bg') {
119129
clearWatermark(watermark);
120130
return;
@@ -138,7 +148,7 @@ const monitor = watermark => {
138148
clearWatermark(watermark);
139149
return;
140150
}
141-
if (zIndex !== '9999') {
151+
if (zIndex !== options.zIndex) {
142152
clearWatermark(watermark);
143153
return;
144154
}
@@ -198,6 +208,6 @@ const getWatermark = props => {
198208
return {
199209
base64: canvas.toDataURL(),
200210
size: canvasSize,
201-
styleSize: canvasSize / devicePixelRatio,
211+
styleSize: canvasSize / devicePixelRatio
202212
};
203213
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
.bb-watermark {
1+
.bb-watermark {
22
position: relative;
3+
4+
&.is-page {
5+
position: fixed;
6+
inset: 0;
7+
z-index: 9999;
8+
pointer-events: none;
9+
}
310
}

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ const deepMerge = (obj1, obj2, skipNull = true) => {
801801
}
802802
else {
803803
const value = obj2[key];
804-
if (skipNull && value === null) {
804+
if (skipNull && (value === null || value === void 0)) {
805805
continue;
806806
}
807807
obj1[key] = obj2[key];

test/UnitTest/Components/WatermarkTest.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public void Watermark_Ok()
2222
});
2323
cut.MarkupMatches("<div id:ignore class=\"bb-watermark\"><span>Test</span></div>");
2424

25-
cut.SetParametersAndRender();
25+
var ex = Assert.ThrowsAny<InvalidOperationException>(() => cut.SetParametersAndRender(pb =>
26+
{
27+
pb.Add(a => a.IsPage, true);
28+
}));
29+
Assert.Equal($"IsPage is true, ChildContent cannot be set.", ex.Message);
30+
31+
cut.SetParametersAndRender(pb =>
32+
{
33+
pb.Add(a => a.IsPage, true);
34+
pb.Add(a => a.ChildContent, (RenderFragment?)null);
35+
});
36+
cut.MarkupMatches("<div id:ignore class=\"bb-watermark is-page\"></div>");
2637
}
2738
}

0 commit comments

Comments
 (0)