Skip to content

Commit

Permalink
Markdown preview and help (#243)
Browse files Browse the repository at this point in the history
* Markdown is working

I don't like the way the table "overflows" in the "cheat sheet" if the window is certain sizes - I shall ascertain if this is an issue and try to mitigate

* Redone to use popover

Also modified popovers to allow for scrolling

* Changes as suggested

* Style changes for markdown preview

* Completed markdown (I think)

* Fixed styling for preview and added scrolling

* Removed commented code and updated spacing
  • Loading branch information
droberts-ctrlo authored Oct 11, 2023
1 parent 18a0d1a commit 152a500
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 56 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"jquery-ui-sortable-npm": "^1.0.0",
"jstree": "^3.3.12",
"keycharm": "^0.3.0",
"marked": "^9.1.0",
"moment": "^2.24.0",
"npm-run-all": "^4.1.5",
"popper.js": "^1.16.1",
Expand Down
6 changes: 6 additions & 0 deletions src/frontend/components/markdown/_markdown.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
div.form-group {
div.js-markdown-preview {
height: 8rem;
overflow: auto;
}
}
5 changes: 5 additions & 0 deletions src/frontend/components/markdown/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { initializeComponent } from "../../js/lib/component";
import MarkdownComponent from "./lib/component";

export default (scope) =>
initializeComponent(scope, ".js-markdown-section", MarkdownComponent);
33 changes: 33 additions & 0 deletions src/frontend/components/markdown/lib/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { marked } from "marked";
import { Component } from "../../../js/lib/component";

class MarkdownComponent extends Component {
constructor(element) {
super(element);
this.initMarkdownEditor();
}

initMarkdownEditor() {
marked.use({ breaks: true });

const $textArea = $(this.element).find(".js-markdown-input");
const $preview = $(this.element).find(".js-markdown-preview");
$().ready(() => {
if ($textArea.val() !== "") {
const htmlText = marked($textArea.val());
$preview.html(htmlText);
}
});
$textArea.keyup(() => {
const markdownText = $textArea.val();
if (!markdownText || markdownText === "") {
$preview.html('<p class="text-info">Nothing to preview!</p>');
} else {
const htmlText = marked(markdownText);
$preview.html(htmlText);
}
});
}
}

export default MarkdownComponent;
1 change: 1 addition & 0 deletions src/frontend/components/popover/_popover.scss
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
margin-top: 0.375rem;
overflow-y: auto;
border-color: $brand-secundary;
max-height: 15rem;
}

.popover.show {
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/css/stylesheets/base/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ h3 {
display: block;
}
}

table.table-bordered {
border: solid 1px $gray;
thead>tr>th,
tbody>tr>td {
border: solid 1px $gray;
}
}
1 change: 1 addition & 0 deletions src/frontend/css/stylesheets/general.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@
@import 'table-header/table-header';
@import 'timeline/timeline';
@import 'user/user';
@import 'markdown/markdown';
2 changes: 2 additions & 0 deletions src/frontend/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import TippyComponent from 'components/timeline/tippy'
import TreeComponent from 'components/form-group/tree'
import UserModalComponent from 'components/modal/modals/user'
import ValueLookupComponent from 'components/form-group/value-lookup'
import MarkdownComponent from "../components/markdown"

// Register them
registerComponent(AddTableModalComponent)
Expand Down Expand Up @@ -72,6 +73,7 @@ registerComponent(TippyComponent)
registerComponent(TreeComponent)
registerComponent(UserModalComponent)
registerComponent(ValueLookupComponent)
registerComponent(MarkdownComponent)

// Initialize all components at some point
initializeRegisteredComponents(document.body)
2 changes: 1 addition & 1 deletion views/fields/textarea.tt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
[% INCLUDE fields/sub/label_input.tt label_type="textarea"; %]
<div class="input__field">
<textarea
class="form-control"
class="form-control [% custom_classes %]"
id="[% id %]"
name="[% name %]"
rows="[% rows %]"
Expand Down
22 changes: 15 additions & 7 deletions views/layout.tt
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,24 @@
</div>
<div class="row">
<div class="col">
<div class="js-markdown-section">
[%
$popover = INCLUDE snippets/markdown.tt;
INCLUDE fields/textarea.tt
id = "helptext"
name = "helptext"
label = "Help text for the user"
placeholder = "Helptext"
value = column.helptext
filter = "html"
rows = 5;
id = "helptext"
name = "helptext"
label = "Help text for the user"
placeholder = "Helptext"
value = column.helptext
filter = "html"
custom_classes = "js-markdown-input"
popover_body = $popover
popover_title = "Markdown Help"
rows = 5;
%]
[% INCLUDE snippets/markdown_section.tt
label = "Help text preview" %]
</div>
</div>
</div>
</div>
Expand Down
65 changes: 65 additions & 0 deletions views/snippets/markdown.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<p>The following Markdown can be used</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<pre># Heading 1</pre>
</td>
<td>
<h1>Heading 1</h1>
</td>
</tr>
<tr>
<td>
<pre>## Heading 2</pre>
</td>
<td>
<h2>Heading 2</h2>
</td>
</tr>
<tr>
<td>
<pre>- bullet
- bullet
- bullet
- bullet</pre>
</td>
<td>
<ul>
<li>bullet</li>
<li>bullet
<ul>
<li>bullet</li>
<li>bullet</li>
</ul>
</li>
</ul>
</td>
</tr>
<tr>
<td>
<pre>*italic*</pre>
</td>
<td><em>italic</em></td>
</tr>
<tr>
<td>
<pre>**bold**</pre>
</td>
<td><strong>bold</strong></td>
</tr>
<tr>
<td>
<pre>[link](https://www.google.com)</pre>
</td>
<td><a href="https://www.google.com">link</a></td>
</tr>
</tbody>
</table>
<p>For more information on Markdown, see <a href="https://www.markdownguide.org/cheat-sheet/">this cheat sheet</a>.</p>
8 changes: 8 additions & 0 deletions views/snippets/markdown_section.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="form-group">
<div class="textarea__label">
<label for="preview"><strong>[% IF label; label; ELSE %]Preview[% END; %]</strong></label>
</div>
<div class="js-markdown-preview form-control">
<p class="text-info">Nothing to preview!</p>
</div>
</div>
89 changes: 44 additions & 45 deletions views/topic.tt
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,60 @@
Edit Table
</legend>
</div>

<div class="content-block__main-content">
<div class="card card--primary mb-3">
<div class="card__body row">
<div class="card__content">
<div class="row">
<div class='col'>
[%
INCLUDE fields/input.tt
id = "name"
name = "name"
label = "Name of topic"
placeholder = "Name of topic"
value = topic.name
type = "text"
filter = "html"
is_required = 1
autofocus = 1;
%]
</div>
</div>
<div class="row">
<div class='col'>
<div class="row">
<div class="col">
<div class="card card--primary mb-3">
<div class="card__content">
[%
INCLUDE fields/input.tt
id = "name"
name = "name"
label = "Name of topic"
placeholder = "Name of topic"
value = topic.name
type = "text"
filter = "html"
is_required = 1
autofocus = 1;
%]
<div class="js-markdown-section">
[%
$help = INCLUDE snippets/markdown.tt;
INCLUDE fields/textarea.tt
id = "description"
name = "description"
label = "Description"
value = topic.description
filter = "html"
help_text = "Use Markdown for formatting"
rows = 5;
%]
</div>
</div>
<div class="row">
<div class='col'>
[%
INCLUDE fields/switch_single.tt
id = "initial_state"
name = "initial_state"
label = "Initial state of topic's fields when editing"
placeholder = "Expanded"
fieldset_name = "switch-expanded"
value = "open"
checked = topic.initial_state == "collapsed" ? 0 : 1
filter = "html";
id = "description"
name = "description"
label = "Description"
value = topic.description
filter = "html"
help_text = "Use Markdown for formatting"
rows = 5
popover_body = $help
popover_title = "Markdown Help"
custom_classes = "js-markdown-input";
%]
[% INCLUDE snippets/markdown_section.tt
label = "Description Preview" %]
</div>
[%
INCLUDE fields/switch_single.tt
id = "initial_state"
name = "initial_state"
label = "Initial state of topic's fields when editing"
placeholder = "Expanded"
fieldset_name = "switch-expanded"
value = "open"
checked = topic.initial_state == "collapsed" ? 0 : 1
filter = "html";
%]
</div>
</div>
</div>
</div>
</div>

</div>

<footer class="content-block__footer">
<div class="content-block__footer-container">
[%
Expand Down
11 changes: 8 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2279,9 +2279,9 @@ camelcase@^5.3.1:
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==

caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001449:
version "1.0.30001449"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001449.tgz#a8d11f6a814c75c9ce9d851dc53eb1d1dfbcd657"
integrity sha512-CPB+UL9XMT/Av+pJxCKGhdx+yg1hzplvFJQlJ2n68PyQGMz9L/E2zCyLdOL8uasbouTUgnPl+y0tccI/se+BEw==
version "1.0.30001546"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001546.tgz"
integrity sha512-zvtSJwuQFpewSyRrI3AsftF6rM0X80mZkChIt1spBGEvRglCrjTniXvinc8JKRoqTwXAgvqTImaN9igfSMtUBw==

capture-stack-trace@^1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -5465,6 +5465,11 @@ markdown-table@^1.1.0:
resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.3.tgz#9fcb69bcfdb8717bfd0398c6ec2d93036ef8de60"
integrity sha512-1RUZVgQlpJSPWYbFSpmudq5nHY1doEIv89gBtF0s4gW1GF2XorxcA/70M5vq7rLv0a6mhOUccRsqkwhwLCIQ2Q==

marked@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-9.1.0.tgz#7a085c7d46730dee2b801f1c1b35c5745479e270"
integrity sha512-VZjm0PM5DMv7WodqOUps3g6Q7dmxs9YGiFUZ7a2majzQTTCgX+6S6NAJHPvOhgFBzYz8s4QZKWWMfZKFmsfOgA==

mathml-tag-names@^2.1.1:
version "2.1.3"
resolved "https://registry.yarnpkg.com/mathml-tag-names/-/mathml-tag-names-2.1.3.tgz#4ddadd67308e780cf16a47685878ee27b736a0a3"
Expand Down

0 comments on commit 152a500

Please sign in to comment.