Skip to content

Commit

Permalink
Added new markdown block. Fixes #1744
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Oct 25, 2021
1 parent 99515b9 commit 9d087db
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!-- Tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<li v-if="regions.length > 0" class="nav-item">
<a v-on:click.prevent="selectRegion({ uid: 'uid-settings' })" href="#" class="nav-link" :class="{ active: selectedRegion.uid === 'uid-settings' }">@Localizer.General["Settings"]</a>
</li>
<li v-for="(region) in regions" class="nav-item">
Expand Down
10 changes: 5 additions & 5 deletions core/Piranha.Manager/assets/dist/css/full.min.css

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions core/Piranha.Manager/assets/dist/css/slim.min.css

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion core/Piranha.Manager/assets/dist/js/piranha.components.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,30 @@ Vue.component("image-block", {
},
template: "\n<div class=\"block-body has-media-picker rounded\" :class=\"{ empty: isEmpty }\">\n <img class=\"rounded\" :src=\"mediaUrl\">\n <div class=\"media-picker\">\n <div class=\"btn-group float-right\">\n <button :id=\"uid + '-aspect'\" class=\"btn btn-info btn-aspect text-center\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">\n <i v-if=\"model.aspect.value === 0\" class=\"fas fa-cog\"></i>\n <img v-else :src=\"iconUrl\">\n </button>\n <div class=\"dropdown-menu aspect-menu\" :aria-labelledby=\"uid + '-aspect'\">\n <label class=\"mb-0\">{{ piranha.resources.texts.aspectLabel }}</label>\n <div class=\"dropdown-divider\"></div>\n <a v-on:click.prevent=\"selectAspect(0)\" class=\"dropdown-item\" :class=\"{ active: isAspectSelected(0) }\" href=\"#\">\n <img :src=\"piranha.utils.formatUrl('~/manager/assets/img/icons/img-original.svg')\"><span>{{ piranha.resources.texts.aspectOriginal }}</span>\n </a>\n <a v-on:click.prevent=\"selectAspect(1)\" class=\"dropdown-item\" :class=\"{ active: isAspectSelected(1) }\" href=\"#\">\n <img :src=\"piranha.utils.formatUrl('~/manager/assets/img/icons/img-landscape.svg')\"><span>{{ piranha.resources.texts.aspectLandscape }}</span>\n </a>\n <a v-on:click.prevent=\"selectAspect(2)\" class=\"dropdown-item\" :class=\"{ active: isAspectSelected(2) }\" href=\"#\">\n <img :src=\"piranha.utils.formatUrl('~/manager/assets/img/icons/img-portrait.svg')\"><span>{{ piranha.resources.texts.aspectPortrait }}</span>\n </a>\n <a v-on:click.prevent=\"selectAspect(3)\" class=\"dropdown-item\" :class=\"{ active: isAspectSelected(3) }\" href=\"#\">\n <img :src=\"piranha.utils.formatUrl('~/manager/assets/img/icons/img-landscape.svg')\"><span>{{ piranha.resources.texts.aspectWidescreen }}</span>\n </a>\n <a v-on:click.prevent=\"selectAspect(4)\" class=\"dropdown-item\" :class=\"{ active: isAspectSelected(4) }\" href=\"#\">\n <img :src=\"piranha.utils.formatUrl('~/manager/assets/img/icons/img-square.svg')\"><span>{{ piranha.resources.texts.aspectSquare }}</span>\n </a>\n </div>\n <button v-on:click.prevent=\"select\" class=\"btn btn-primary text-center\">\n <i class=\"fas fa-plus\"></i>\n </button>\n <button v-on:click.prevent=\"remove\" class=\"btn btn-danger text-center\">\n <i class=\"fas fa-times\"></i>\n </button>\n </div>\n <div class=\"card text-left\">\n <div class=\"card-body\" v-if=\"isEmpty\">\n &nbsp;\n </div>\n <div class=\"card-body\" v-else>\n {{ model.body.media.filename }}\n </div>\n </div>\n </div>\n</div>\n"
});
Vue.component("markdown-block", {
props: ["uid", "model"],
methods: {
onBlur: function (e) {
// Tell parent that title has been updated
var title = this.model.body.value.replace(/(<([^>]+)>)/ig, "");

if (title.length > 40) {
title = title.substring(0, 40) + "...";
}

this.$emit('update-title', {
uid: this.uid,
title: title
});
}
},
computed: {
isEmpty: function () {
return piranha.utils.isEmptyText(this.model.body.value);
}
},
template: "\n<div class=\"block-body\" :class=\"{ empty: isEmpty }\">\n <markdown-field :uid=\"uid\" :model=\"model.body\" />\n</div>\n"
});
Vue.component("missing-block", {
props: ["model"],
template: "\n<div class=\"alert alert-danger text-center\" role=\"alert\">No component registered for <code>{{ model.type }}</code></div>\n"
Expand Down Expand Up @@ -1279,7 +1303,7 @@ Vue.component("markdown-field", {
update: function (md) {
this.model.value = md; // Tell parent that title has been updated

if (this.meta.notifyChange) {
if (this.meta && this.meta.notifyChange) {
var title = this.model.value;

if (title.length > 40) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<div class="block-body" :class="{ empty: isEmpty }">
<markdown-field :uid="uid" :model="model.body" />
</div>
</template>

<script>
export default {
props: ["uid", "model"],
methods: {
onBlur: function (e) {
// Tell parent that title has been updated
var title = this.model.body.value.replace(/(<([^>]+)>)/ig, "");
if (title.length > 40) {
title = title.substring(0, 40) + "...";
}
this.$emit('update-title', {
uid: this.uid,
title: title
});
}
},
computed: {
isEmpty: function () {
return piranha.utils.isEmptyText(this.model.body.value);
}
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
this.model.value = md;
// Tell parent that title has been updated
if (this.meta.notifyChange) {
if (this.meta && this.meta.notifyChange) {
var title = this.model.value;
if (title.length > 40) {
title = title.substring(0, 40) + "...";
Expand Down
18 changes: 18 additions & 0 deletions core/Piranha.Manager/assets/src/scss/inc/_blocks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@
}
}

.markdown-block {
.markdown-field{
border: none;

.editor-toolbar:not(.fullscreen) {
border: solid 1px $gray-400;
}

.CodeMirror:not(.CodeMirror-fullscreen) {
padding-left: 0;
min-height: 150px;
}
.CodeMirror-scroll:not(.CodeMirror-fullscreen) {
min-height: 150px;
}
}
}

.text-block {
.block-body {
display: grid;
Expand Down
8 changes: 4 additions & 4 deletions core/Piranha.Manager/assets/src/scss/inc/_blocktypes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
color: $black;
font-weight: bold;
text-transform: uppercase;
padding: .25rem 1rem;
padding: .25rem 0;
margin: .25rem 0;
}

Expand All @@ -14,7 +14,7 @@

.blocktype {
display: inline-block;
width: 25%;
width: 20%;
padding: .25rem;

.btn {
Expand All @@ -31,13 +31,13 @@

i {
display: block;
font-size: 1.2rem;
font-size: 1rem;
margin-bottom: .5rem;
}

span {
display: block;
font-size: .8rem;
font-size: .75rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
7 changes: 7 additions & 0 deletions core/Piranha.Manager/assets/src/scss/inc/_fields.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
padding: .4rem 1rem;
margin-right: 1rem;
border: solid 1px $gray-400;

span {
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/Piranha.Manager/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ var js = [
"assets/src/js/components/blocks/html-block.vue",
"assets/src/js/components/blocks/html-column-block.vue",
"assets/src/js/components/blocks/image-block.vue",
"assets/src/js/components/blocks/markdown-block.vue",
"assets/src/js/components/blocks/missing-block.vue",
"assets/src/js/components/blocks/page-block.vue",
"assets/src/js/components/blocks/post-block.vue",
Expand Down
1 change: 1 addition & 0 deletions core/Piranha/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ static App()
Instance._blocks.Register<Extend.Blocks.HtmlColumnBlock>();
Instance._blocks.Register<Extend.Blocks.ImageBlock>();
Instance._blocks.Register<Extend.Blocks.ImageGalleryBlock>();
Instance._blocks.Register<Extend.Blocks.MarkdownBlock>();
Instance._blocks.Register<Extend.Blocks.PageBlock>();
Instance._blocks.Register<Extend.Blocks.PostBlock>();
Instance._blocks.Register<Extend.Blocks.QuoteBlock>();
Expand Down
9 changes: 9 additions & 0 deletions core/Piranha/Extend/Blocks/HtmlBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ public string GetIndexedContent()
{
return !string.IsNullOrEmpty(Body.Value) ? Body.Value : "";
}

/// <summary>
/// Implicitly converts the Html block to a string.
/// </summary>
/// <param name="block">The block</param>
public static implicit operator string(HtmlBlock block)
{
return block.Body?.Value;
}
}
}
65 changes: 65 additions & 0 deletions core/Piranha/Extend/Blocks/MarkdownBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) .NET Foundation and Contributors
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* https://github.com/piranhacms/piranha.core
*
*/

using System.Text.RegularExpressions;
using Piranha.Extend.Fields;

namespace Piranha.Extend.Blocks
{
/// <summary>
/// Single column HTML block.
/// </summary>
[BlockType(Name = "Markdown", Category = "Content", Icon = "fab fa-markdown", Component = "markdown-block")]
public class MarkdownBlock : Block, ISearchable, ITranslatable
{
/// <summary>
/// Gets/sets the Markdown body.
/// </summary>
public MarkdownField Body { get; set; }

/// <summary>
/// Gets the title of the block when used in a block group.
/// </summary>
/// <returns>The title</returns>
public override string GetTitle()
{
if (Body?.Value != null)
{
var html = App.Markdown.Transform(Body.Value);

var title = Regex.Replace(html, @"<[^>]*>", "");

if (title.Length > 40)
{
title = title.Substring(0, 40) + "...";
}
return title;
}
return "Empty";
}

/// <summary>
/// Gets the content that should be indexed for searching.
/// </summary>
public string GetIndexedContent()
{
return !string.IsNullOrEmpty(Body.Value) ? Body.Value : "";
}

/// <summary>
/// Implicitly converts the markdown block to a HTML string.
/// </summary>
/// <param name="block">The block</param>
public static implicit operator string(MarkdownBlock block)
{
return App.Markdown.Transform(block.Body?.Value);
}
}
}
9 changes: 9 additions & 0 deletions core/Piranha/Extend/Blocks/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@ public string GetIndexedContent()
{
return !string.IsNullOrEmpty(Body.Value) ? Body.Value : "";
}

/// <summary>
/// Implicitly converts the Text block to a string.
/// </summary>
/// <param name="block">The block</param>
public static implicit operator string(TextBlock block)
{
return block.Body?.Value;
}
}
}
2 changes: 1 addition & 1 deletion examples/RazorWeb/Pages/DisplayTemplates/HtmlBlock.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@model Piranha.Extend.Blocks.HtmlBlock

<div class="block">
@Html.Raw(Model.Body)
@Html.Raw(Model)
</div>
5 changes: 5 additions & 0 deletions examples/RazorWeb/Pages/DisplayTemplates/MarkdownBlock.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@model Piranha.Extend.Blocks.MarkdownBlock

<div class="block">
@Html.Raw(Model)
</div>
2 changes: 1 addition & 1 deletion examples/RazorWeb/Pages/DisplayTemplates/TextBlock.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@model Piranha.Extend.Blocks.TextBlock

<div class="block">
@Model.Body
@Model
</div>

0 comments on commit 9d087db

Please sign in to comment.