-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a form field/widget to edit markdown for text fields. Implement this field initially for the `description` and `selected_bibliography` fields on the `Source` model. Add helper tag `render_markdown` for displaying markdown fields.
- Loading branch information
Showing
11 changed files
with
546 additions
and
215 deletions.
There are no files selected for viewing
This file contains 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 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 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
42 changes: 42 additions & 0 deletions
42
django/cantusdb_project/main_app/templates/widgets/markdown_widget.html
This file contains 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,42 @@ | ||
<div class="markdown-field"> | ||
<ul class="nav nav-tabs markdown-field-tabs" | ||
id="{{ widget.name }}-tabs" | ||
role="tablist"> | ||
<li class="nav-item" role="presentation"> | ||
<button class="nav-link active edit-tab" | ||
id="{{ widget.name }}-edit-tab" | ||
data-bs-toggle="tab" | ||
data-bs-target="#{{ widget.name }}-edit" | ||
type="button" | ||
role="tab" | ||
aria-controls="{{ widget.name }}-edit" | ||
aria-selected="true">Edit</button> | ||
</li> | ||
<li class="nav-item" role="presentation"> | ||
<button class="nav-link preview-tab" | ||
id="{{ widget.name }}-preview-tab" | ||
data-bs-toggle="tab" | ||
data-bs-target="#{{ widget.name }}-preview" | ||
type="button" | ||
role="tab" | ||
aria-controls="{{ widget.name }}-preview" | ||
aria-selected="false">Preview</button> | ||
</li> | ||
</ul> | ||
<div class="tab-content" id="{{ widget.name }}-content"> | ||
<div class="tab-pane fade show active" | ||
id="{{ widget.name }}-edit" | ||
role="tabpanel" | ||
aria-labelledby="{{ widget.name }}-edit-tab" | ||
tabindex="0"> | ||
<textarea id="id_{{ widget.name }}" | ||
name="{{ widget.name }}" | ||
class="form-control form-control-sm markdown-textarea">{% if widget.value %}{{ widget.value }}{% endif %}</textarea> | ||
</div> | ||
<div class="tab-pane fade markdown-preview form-control form-control-sm overflow-scroll" | ||
id="{{ widget.name }}-preview" | ||
role="tabpanel" | ||
aria-labelledby="{{ widget.name }}-preview-tab" | ||
tabindex="0"></div> | ||
</div> | ||
</div> |
This file contains 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 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 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,47 @@ | ||
MarkdownWidget = (function () { | ||
// Initialize the markdown widget, attaching the necessary event listeners | ||
// to the textarea and preview elements | ||
function init() { | ||
var markdownFields = document.getElementsByClassName('markdown-field'); | ||
for (var i = 0; i < markdownFields.length; i++) { | ||
let field = markdownFields[i] | ||
let textarea = field.getElementsByClassName('markdown-textarea')[0]; | ||
let preview = field.getElementsByClassName('markdown-preview')[0]; | ||
let previewTab = field.getElementsByClassName('preview-tab')[0]; | ||
previewTab.addEventListener("show.bs.tab", function (event) { | ||
var markdownText = textarea.value; | ||
var parsed = marked.parse(markdownText); | ||
preview.innerHTML = parsed; | ||
// Set height of preview to match height of textarea | ||
preview.style.height = textarea.clientHeight + "px"; | ||
}); | ||
|
||
textarea.addEventListener('keydown', function (e) { | ||
if (e.key == 'Tab') { | ||
e.preventDefault(); | ||
var start = this.selectionStart; | ||
var end = this.selectionEnd; | ||
|
||
// set textarea value to: text before caret + tab + text after caret | ||
this.value = this.value.substring(0, start) + | ||
"\t" + this.value.substring(end); | ||
|
||
// put caret at right position again | ||
this.selectionStart = | ||
this.selectionEnd = start + 1; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
init: function () { | ||
return init(); | ||
} | ||
}; | ||
})(); | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
MarkdownWidget.init(); | ||
}); | ||
|
This file contains 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
31 changes: 31 additions & 0 deletions
31
django/cantusdb_project/static/stylesheets/markdown_widget.css
This file contains 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,31 @@ | ||
.markdown-field-tabs { | ||
border-bottom: none; | ||
} | ||
|
||
/* Aligns top-left border of markdown-field input with tab */ | ||
.markdown-field textarea { | ||
border-top-left-radius: 0; | ||
height: 250px; | ||
} | ||
|
||
.markdown-preview, | ||
.markdown-preview:focus { | ||
border-style: solid none none none; | ||
border-color: var(--bs-border-color); | ||
border-radius: 0; | ||
box-shadow: none; | ||
} | ||
|
||
.markdown-preview table { | ||
border-collapse: collapse; | ||
} | ||
|
||
.markdown-preview table td { | ||
border: 1px solid #ddd; | ||
padding: 6px; | ||
} | ||
|
||
.markdown-preview table th { | ||
border: 1px solid #ddd; | ||
padding: 6px; | ||
} |
Oops, something went wrong.