Skip to content
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

Hide <textarea> When Toggling Preview in Legacy Editor #9234

Merged
merged 4 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions app/assets/javascripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ class Editor {
// defaultForm - when the Editor is initialized, there needs to be a default editor form:
// 1. the main comment form in multi-comment wikis, questions, & research notes.
// 2. the only editor form on /wiki/new and /wiki/edit
// elementIDPrefixes - the ID naming conventions are different depending on context:
// isSingleFormPage - to distinguish between a) pages with multiple comments b) pages like /wiki/new, and /features/new with only one comment form
// elements have different ID naming conventions on the two kinds of pages:
// 1. multi-form pages with multiple comments: #comment-preview-123
// 2. /wiki/new and /wiki/edit: #preview-main
constructor(defaultForm = "main", elementIDPrefixes = {}) {
constructor(defaultForm = "main", isSingleFormPage = false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hopefully a lot less confusing than elementIDPrefixes.

this.commentFormID = defaultForm;
this.elementIDPrefixes = elementIDPrefixes;
this.isSingleFormPage = isSingleFormPage;
// this will get deleted in the next few PRs, so collapsing into one line to pass codeclimate
this.templates = { 'blog': "## The beginning\n\n## What we did\n\n## Why it matters\n\n## How can you help", 'default': "## What I want to do\n\n## My attempt and results\n\n## Questions and next steps\n\n## Why I'm interested", 'support': "## Details about the problem\n\n## A photo or screenshot of the setup", 'event': "## Event details\n\nWhen, where, what\n\n## Background\n\nWho, why", 'question': "## What I want to do or know\n\n## Background story" };

Expand Down Expand Up @@ -40,11 +41,8 @@ class Editor {
return this.textAreaElement.val();
}
get previewElement() {
let previewIDPrefix = "#comment-preview-";
if (this.elementIDPrefixes.hasOwnProperty("preview")) {
// eg. on /wiki/new & /wiki/edit, the preview element is called #preview-main
previewIDPrefix = "#" + this.elementIDPrefixes["preview"] + "-";
}
// eg. on /wiki/new & /wiki/edit, the preview element is called #preview-main
const previewIDPrefix = this.isSingleFormPage ? "#preview-" : "#comment-preview-"
const previewID = previewIDPrefix + this.commentFormID;
return $(previewID);
}
Expand Down Expand Up @@ -136,7 +134,8 @@ class Editor {
// if the element is part of a multi-comment page,
// ensure to grab the current element and not the other comment element.
const previewBtn = $("#toggle-preview-button-" + this.commentFormID);
const commentFormBody = $("#comment-form-body-" + this.commentFormID);
const formIdPrefix = this.isSingleFormPage ? "#form-body-" : "#comment-form-body-";
const commentFormBody = $(formIdPrefix + this.commentFormID);

this.previewElement[0].innerHTML = marked(this.textAreaValue);
this.previewElement.toggle();
Expand Down
4 changes: 2 additions & 2 deletions app/views/editor/_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%# toolbar needs location & comment_id to make unique element IDs%>
<%= render :partial => "editor/toolbar", :locals => { :location => :main } %>

<div class="form-group dropzone dropzone-large" data-form-id="main">
<div id="form-body-main" class="form-group dropzone dropzone-large" data-form-id="main">
<textarea
id="text-input-main"
class="form-control text-input"
Expand Down Expand Up @@ -56,7 +56,7 @@
jQuery(document).ready(function() {
$E = new Editor(
"main",
{ "preview": "preview" } // the preview element here is #preview-main, not #comment-preview-main
true // isSingleFormPage = true
);
})
</script>
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ en:
create_page: "Create page"
publish: "Publish"
preview: "Preview"
previewing_text: "Previewing (click to edit)"
previewing_text: "Hide Preview"
agree_to_open_source_work: "By publishing, you agree to <a target='_blank' href='%{url1}'>open
source your work</a> so that others may use it."
last_edited_by: "This page was last edited %{time} ago by <a href='%{url1}'>%{author}</a>"
Expand Down