终于要接近尾声了,上一篇基本上将文章模块的全部功能都完成了,整个博客页面也都完成了,本篇主要来美化几个地方,修修补补。javascript
当咱们新增和编辑文章的时候,默认编辑器是白色的,若是点击了头部切换主题按钮,我想要把编辑器主题颜色也作相应的改变该如何去实现呢?css
恰好,editor.md
是支持主题切换的,这就比较舒服了,直接按照要求调用对应的方法便可。html
在app.js
的renderEditor
函数中咱们已经自定义了三个参数theme
、editorTheme
、previewTheme
,这三个参数就是来改变编辑器主题颜色的。java
仍是将值存在localStorage中,和咱们博客的主题切换同样,这里叫editorTheme
。git
theme: localStorage.editorTheme || 'default', editorTheme: localStorage.editorTheme === 'dark' ? 'pastel-on-dark' : 'default', previewTheme: localStorage.editorTheme || 'default',
默认从localStorage
中取数据,若是没取到的话,给对应的默认值。第二个参数有点特殊,用了一个三元表达式给不一样的值。github
而后在主题切换的时候也对编辑器作相应的调整便可。缓存
打开Header.razor
头部组件,找到SwitchTheme()
切换主题的方法,添加一句await Common.SwitchEditorTheme(currentTheme);
。markdown
/// <summary> /// 切换主题 /// </summary> private async Task SwitchTheme() { currentTheme = currentTheme == "Light" ? "Dark" : "Light"; await Common.SetStorageAsync("theme", currentTheme); await Common.InvokeAsync("window.func.switchTheme"); var uri = await Common.CurrentUri(); if (uri.AbsolutePath.StartsWith("/admin/post")) { await Common.SwitchEditorTheme(currentTheme); } }
将具体切换逻辑放到SwitchEditorTheme
中,他接收一个参数currentTheme
,用来判断是切换黑的仍是白的。app
/// <summary> /// 切换编辑器主题 /// </summary> /// <param name="currentTheme"></param> /// <returns></returns> public async Task SwitchEditorTheme(string currentTheme) { var editorTheme = currentTheme == "Light" ? "default" : "dark"; await SetStorageAsync("editorTheme", editorTheme); await InvokeAsync("window.func.switchEditorTheme"); }
切换主题以前拿到当前URI对象,判断当前请求的连接是不是新增和更新文章的那个页面,即"/admin/post",才去执行切换编辑器主题的方法,当不是这个页面的时候,编辑器是没有渲染出来的,若是也执行这段代码就会报错。async
去看看效果。
如今的文章详情页是没有将markdown格式渲染出来的,这里仍是使用editor.md
提供的方法,由于须要加载几个js文件,而后才能渲染样式。
因此仍是在app.js
添加一段代码。
renderMarkdown: async function () { await this._loadScript('./editor.md/lib/zepto.min.js').then(function () { func._loadScript('./editor.md/lib/marked.min.js').then(function () { func._loadScript('./editor.md/lib/prettify.min.js').then(function () { func._loadScript('./editor.md/editormd.js').then(function () { editormd.markdownToHTML("content"); }); }); }); }); },
而后在文章详情页的组件Post.razor
中修改代码,当数据加载完成后调用renderMarkdown
便可,而后还须要引用一个css文件editormd.preview.css
。
提供一下Post.razor
最终的代码。
@page "/post/{year:int}/{month:int}/{day:int}/{name}" <link href="./editor.md/css/editormd.preview.css" rel="stylesheet" /> @if (post == null) { <Loading /> } else { @if (post.Success) { var _post = post.Result; <article class="post-wrap"> <header class="post-header"> <h1 class="post-title">@_post.Title</h1> <div class="post-meta"> Author: <a itemprop="author" rel="author" href="javascript:;">@_post.Author</a> <span class="post-time"> Date: <a href="javascript:;">@_post.CreationTime</a> </span> <span class="post-category"> Category:<a href="/category/@_post.Category.DisplayName/">@_post.Category.CategoryName</a> </span> </div> </header> <div class="post-content" id="content"> @((MarkupString)_post.Html) </div> <section class="post-copyright"> <p class="copyright-item"> <span>Author:</span> <span>@_post.Author</span> </p> <p class="copyright-item"> <span>Permalink:</span> <span><a href="/post@_post.Url">https://meowv.com/post@_post.Url</a></span> </p> <p class="copyright-item"> <span>License:</span> <span>本文采用<a target="_blank" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"> 知识共享 署名-非商业性使用-禁止演绎(CC BY-NC-ND)国际许可协议 </a>进行许可</span> </p> </section> <section class="post-tags"> <div> <span>Tag(s):</span> <span class="tag"> @if (_post.Tags.Any()) { @foreach (var tag in _post.Tags) { <a href="/tag/@tag.DisplayName/"># @tag.TagName</a> } } </span> </div> <div> <a @onclick="@(async () => await Common.NavigateTo("/posts"))">back</a> <span>· </span> <a href="/">home</a> </div> </section> <section class="post-nav"> @if (_post.Previous != null) { <a class="prev" rel="prev" @onclick="@(async () => await FetchData(_post.Previous.Url))" href="/post@_post.Previous.Url">@_post.Previous.Title</a> } @if (_post.Next != null) { <a class="next" rel="next" @onclick="@(async () => await FetchData(_post.Next.Url))" href="/post@_post.Next.Url"> @_post.Next.Title </a> } </section> </article> } else { <ErrorTip /> } } @code { [Parameter] public int year { get; set; } [Parameter] public int month { get; set; } [Parameter] public int day { get; set; } [Parameter] public string name { get; set; } /// <summary> /// URL /// </summary> private string url => $"/{year}/{(month >= 10 ? month.ToString() : $"0{month}")}/{(day >= 10 ? day.ToString() : $"0{day}")}/{name}/"; /// <summary> /// 文章详情数据 /// </summary> private ServiceResult<PostDetailDto> post; /// <summary> /// 初始化 /// </summary> protected override async Task OnInitializedAsync() { await FetchData(url); } /// <summary> /// 请求数据,渲染页面 /// </summary> /// <param name="url"></param> /// <returns></returns> private async Task FetchData(string url, bool isPostNav = false) { post = await Http.GetFromJsonAsync<ServiceResult<PostDetailDto>>($"/blog/post?url={url}"); await Common.InvokeAsync("window.func.renderMarkdown"); } }
到这里整个开发工做便结束了,这里只是一个小小的实战系列记录,没有深层次的剖析研究Blazor的全部使用方式。
若是本系列对你有些许帮助,即是我最大的收获,欢迎你们关注个人公众号:阿星Plus。