-
-
Notifications
You must be signed in to change notification settings - Fork 365
feat(Watermark): add Watermark component #5099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+366
−5
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
69f85ce
feat: 增加水印组件
ArgoZhang a47b8ba
feat: 增加样式
ArgoZhang 48e5ab0
doc: 增加菜单
ArgoZhang e72049e
doc: 增加路由源码映射
ArgoZhang b2c6c0d
doc: 增加示例
ArgoZhang bcd999f
refactor: 增加脚本
ArgoZhang 94ae140
feat: 增加防篡改逻辑
ArgoZhang 054b92c
refactor: 增加更新逻辑
ArgoZhang dbf2adf
doc: 更新示例
ArgoZhang 0e4c962
feat: 增加颜色参数支持
ArgoZhang 5e43472
doc: 更新示例
ArgoZhang 010c7dc
feat: 增加 Gap 参数
ArgoZhang 0a6591d
doc: 增加默认值
ArgoZhang 768cdfa
doc: 更新文档
ArgoZhang 6f9cd6f
refactor: 更新参数数据类型
ArgoZhang 48ec06b
doc: 更新示例
ArgoZhang 7c7df2f
test: 更新单元测试
ArgoZhang 11693d4
chore: bump version 9.2.7
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/BootstrapBlazor.Server/Components/Samples/Watermarks.razor
This file contains hidden or 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,50 @@ | ||
| @page "/watermark" | ||
| @inject IStringLocalizer<Watermarks> Localizer | ||
|
|
||
| <h3>@Localizer["WatermarkTitle"]</h3> | ||
|
|
||
| <h4>@Localizer["Watermarkntro"]</h4> | ||
|
|
||
| <DemoBlock Title="@Localizer["WatermarkNormalTitle"]" Introduction="@Localizer["WatermarkNormalIntro"]" Name="Normal"> | ||
| <section ignore> | ||
| <div class="row form-inline g-3"> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="Text"></BootstrapInputGroupLabel> | ||
| <BootstrapInput @bind-Value="@_text"></BootstrapInput> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="FontSize"></BootstrapInputGroupLabel> | ||
| <Slider @bind-Value="@_fontSize" Min="12" Max="20"></Slider> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="Color"></BootstrapInputGroupLabel> | ||
| <ColorPicker @bind-Value="@_color" IsSupportOpacity="true"></ColorPicker> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="Rotate"></BootstrapInputGroupLabel> | ||
| <Slider @bind-Value="@_rotate" Min="-180" Max="180"></Slider> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="Gap"></BootstrapInputGroupLabel> | ||
| <Slider @bind-Value="@_gap" Min="0" Max="100"></Slider> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| </div> | ||
| </section> | ||
| <Watermark Text="@_text" FontSize="@_fontSize" Color="@_color" Rotate="@_rotate" | ||
| Gap="@_gap"> | ||
| <div style="height: 500px; padding-top: 40px; text-align: center;"> | ||
| <p>this is a watermark demo</p> | ||
| <div>这是 watermark 演示</div> | ||
| </div> | ||
| </Watermark> | ||
| </DemoBlock> |
22 changes: 22 additions & 0 deletions
22
src/BootstrapBlazor.Server/Components/Samples/Watermarks.razor.cs
This file contains hidden or 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,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Samples; | ||
|
|
||
| /// <summary> | ||
| /// Watermarks 组件 | ||
| /// </summary> | ||
| public partial class Watermarks | ||
| { | ||
| private string _text = "BootstrapBlazor"; | ||
|
|
||
| private int _fontSize = 16; | ||
|
|
||
| private int _gap = 40; | ||
|
|
||
| private int _rotate = -40; | ||
|
|
||
| private string _color = "#0000004d"; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| @namespace BootstrapBlazor.Components | ||
| @inherits BootstrapModuleComponentBase | ||
| @attribute [BootstrapModuleAutoLoader] | ||
|
|
||
| <div @attributes="AdditionalAttributes" id="@Id" class="@ClassString"> | ||
| @ChildContent | ||
| </div> |
100 changes: 100 additions & 0 deletions
100
src/BootstrapBlazor/Components/Watermark/Watermark.razor.cs
This file contains hidden or 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,100 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// Watermark 组件 | ||
| /// </summary> | ||
| public partial class Watermark | ||
| { | ||
| /// <summary> | ||
| /// 获得/设置 组件内容 | ||
| /// </summary> | ||
| [Parameter] | ||
| [EditorRequired] | ||
| public RenderFragment? ChildContent { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 水印文本 默认 BootstrapBlazor | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Text { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 字体大小 默认 null 未设置 默认使用 16px 字体大小 单位 px | ||
| /// </summary> | ||
| [Parameter] | ||
| public int? FontSize { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 颜色 默认 null 未设置 | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Color { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 水印的旋转角度 默认 null 45° | ||
| /// </summary> | ||
| [Parameter] | ||
| public int? Rotate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 水印元素的 z-index 值 默认 null | ||
| /// </summary> | ||
| [Parameter] | ||
| public int? ZIndex { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 水印之间的间距 值 默认 null | ||
| /// </summary> | ||
| [Parameter] | ||
| public int? Gap { get; set; } | ||
|
|
||
| private string? ClassString => CssBuilder.Default("bb-watermark") | ||
| .AddClassFromAttributes(AdditionalAttributes) | ||
| .Build(); | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| protected override void OnParametersSet() | ||
| { | ||
| base.OnParametersSet(); | ||
|
|
||
| Text ??= "BootstrapBlazor"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="firstRender"></param> | ||
| /// <returns></returns> | ||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| await base.OnAfterRenderAsync(firstRender); | ||
|
|
||
| if (!firstRender) | ||
| { | ||
| await InvokeVoidAsync("update", Id, GetOptions()); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, GetOptions()); | ||
|
|
||
| private object GetOptions() => new | ||
| { | ||
| Text, | ||
| FontSize, | ||
| Color, | ||
| Rotate, | ||
| Gap, | ||
| ZIndex | ||
| }; | ||
| } |
130 changes: 130 additions & 0 deletions
130
src/BootstrapBlazor/Components/Watermark/Watermark.razor.js
This file contains hidden or 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,130 @@ | ||
| import Data from "../../modules/data.js" | ||
|
|
||
| export function init(id, options) { | ||
| const el = document.getElementById(id); | ||
| if (el === null) { | ||
| return; | ||
| } | ||
| const watermark = { el, options }; | ||
| createWatermark(watermark); | ||
|
|
||
| const observer = ob => { | ||
| ob.observe(el, { | ||
| childList: true, | ||
| attributes: true, | ||
| subtree: true | ||
| }); | ||
| } | ||
|
|
||
| const observerCallback = records => { | ||
| ob.disconnect(); | ||
| updateWatermark(records, watermark); | ||
| observer(ob); | ||
| }; | ||
|
|
||
| const ob = new MutationObserver(observerCallback); | ||
| observer(ob); | ||
| watermark.ob = ob; | ||
|
|
||
| Data.set(id, watermark); | ||
| } | ||
|
|
||
| export function update(id, options) { | ||
| const watermark = Data.get(id); | ||
| watermark.options = options; | ||
|
|
||
| createWatermark(watermark); | ||
| } | ||
|
|
||
| export function dispose(id) { | ||
| const watermark = Data.get(id); | ||
| Data.remove(id); | ||
|
|
||
| if (watermark) { | ||
| const { ob } = watermark; | ||
| ob.disconnect(); | ||
|
|
||
| delete watermark.ob; | ||
| } | ||
| } | ||
|
|
||
| const updateWatermark = (records, watermark) => { | ||
| for (const record of records) { | ||
| for (const dom of record.removedNodes) { | ||
| if (dom.classList.contains('bb-watermark-bg')) { | ||
| createWatermark(watermark); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (record.target.classList.contains('bb-watermark-bg')) { | ||
| createWatermark(watermark); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const createWatermark = watermark => { | ||
| const { el, options } = watermark; | ||
| const defaults = { | ||
| gap: 40, | ||
| fontSize: 16, | ||
| text: 'BootstrapBlazor', | ||
| rotate: -40, | ||
| color: '#0000004d' | ||
| }; | ||
|
|
||
| for (const key in options) { | ||
| if (options[key] === void 0 || options[key] === null) { | ||
| delete options[key]; | ||
| } | ||
| } | ||
|
|
||
| const bg = getWatermark({ ...defaults, ...options }); | ||
| const div = document.createElement('div'); | ||
| const { base64, styleSize } = bg; | ||
| div.style.backgroundImage = `url(${base64})`; | ||
| div.style.backgroundSize = `${styleSize}px ${styleSize}px`; | ||
| div.style.backgroundRepeat = 'repeat'; | ||
| div.style.pointerEvents = 'none'; | ||
| div.style.opacity = '1'; | ||
| div.style.position = 'absolute'; | ||
| div.style.inset = '0'; | ||
| div.style.zIndex = '999'; | ||
| div.classList.add("bb-watermark-bg"); | ||
|
|
||
| const mark = el.querySelector('.bb-watermark-bg'); | ||
| if (mark) { | ||
| mark.remove(); | ||
| } | ||
| el.appendChild(div); | ||
| } | ||
|
|
||
| const getWatermark = props => { | ||
| const canvas = document.createElement('canvas'); | ||
| const devicePixelRatio = window.devicePixelRatio || 1; | ||
|
|
||
| const fontSize = props.fontSize * devicePixelRatio; | ||
| const font = fontSize + 'px serif'; | ||
| const ctx = canvas.getContext('2d'); | ||
|
|
||
| ctx.font = font; | ||
| const { width } = ctx.measureText(props.text); | ||
| const canvasSize = Math.max(100, width) + props.gap * devicePixelRatio; | ||
| canvas.width = canvasSize; | ||
| canvas.height = canvasSize; | ||
| ctx.translate(canvas.width / 2, canvas.height / 2); | ||
|
|
||
| ctx.rotate((Math.PI / 180) * props.rotate); | ||
| ctx.fillStyle = props.color; | ||
| ctx.font = font; | ||
| ctx.textAlign = 'center'; | ||
| ctx.textBaseline = 'middle'; | ||
|
|
||
| ctx.fillText(props.text, 0, 0); | ||
| return { | ||
| base64: canvas.toDataURL(), | ||
| size: canvasSize, | ||
| styleSize: canvasSize / devicePixelRatio, | ||
| }; | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
src/BootstrapBlazor/Components/Watermark/Watermark.razor.scss
This file contains hidden or 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,3 @@ | ||
| .bb-watermark { | ||
| position: relative; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.