From 298355f176ab94e2dfcb4807b114a444c944a8ef Mon Sep 17 00:00:00 2001 From: Will Gutierrez Date: Mon, 18 Jan 2021 15:26:14 -0800 Subject: [PATCH 01/13] write setState method with default parameters #9004 --- app/assets/javascripts/editor.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/editor.js b/app/assets/javascripts/editor.js index 116c166f2c..b3782d9b4a 100644 --- a/app/assets/javascripts/editor.js +++ b/app/assets/javascripts/editor.js @@ -4,29 +4,22 @@ $(function() { // on pages with multiple comments, $D.selected needs to be accurate so that rich-text changes (bold, italic, etc.) go into the right comment form // however, the editor is also used on pages with JUST ONE form, and no other comments, eg. /wiki/new & /wiki/edit, so this code needs to be reusable for that context $('.rich-text-button').on('click', function(e) { - const params = getEditorParams(e.target); // defined in editorHelper.js + const { textArea, preview, dSelected } = getEditorParams(e.target); // defined in editorHelper.js // assign dSelected - if (params.hasOwnProperty('dSelected')) { - $D.selected = params['dSelected']; - } - $E.initialize(params); + if (dSelected) { $D.selected = dSelected; } + $E.setState(textArea, preview); const action = e.currentTarget.dataset.action // 'bold', 'italic', etc. $E[action](); // call the appropriate editor function }); }); $E = { - initialize: function(args) { - args = args || {} - // title - $E.title = $('#title') - // textarea - args['textarea'] = args['textarea'] || 'text-input' - $E.textarea = $('#'+args['textarea']) - $E.textarea.bind('input propertychange',$E.save) - // preview - args['preview'] = args['preview'] || 'preview-main' - $E.preview = $('#'+args['preview']) + initialize: function() { + // call setState with no parameters, aka. default parameters. + // default parameters point toward either: + // 1. the comment form at the bottom of multi-comment wikis/questions/research notes + // 2. the only editor form on /wiki/new and /wiki/edit + $E.setState(); marked.setOptions({ gfm: true, @@ -44,6 +37,12 @@ $E = { } }); }, + setState: function(textarea = 'text-input', preview = 'preview-main', title = 'title') { + $E.title = $('#' + title + 'title'); // not sure why this exists? seems like $E.title is always #title + $E.textarea = $('#' + textarea); + $E.textarea.bind('input propertychange', $E.save); + $E.preview = $('#' + preview); + }, is_editing: function() { return ($E.textarea[0].selectionStart == 0 && $E.textarea[0].selectionEnd == 0) }, From dda4d2d63c65c039548a43dd78d5ab6aac34a859 Mon Sep 17 00:00:00 2001 From: Will Gutierrez Date: Mon, 18 Jan 2021 15:27:44 -0800 Subject: [PATCH 02/13] set preview when calling E.setState #9004 --- app/assets/javascripts/dragdrop.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/dragdrop.js b/app/assets/javascripts/dragdrop.js index a7dba36628..64e79d4019 100644 --- a/app/assets/javascripts/dragdrop.js +++ b/app/assets/javascripts/dragdrop.js @@ -29,12 +29,10 @@ jQuery(function() { }); $(this).on('drop',function(e) { - const params = getEditorParams(e.target); // defined in editorHelper.js + const { textArea, preview, dSelected } = getEditorParams(e.target); // defined in editorHelper.js e.preventDefault(); - if (params.hasOwnProperty('dSelected')) { - $D.selected = params['dSelected']; - } - $E.initialize(params); + if (dSelected) { $D.selected = dSelected; } + $E.setState(textArea, preview); }); $(this).fileupload({ From a4ae7c3ff2f49ec3856180ba39cb2d4d891870bd Mon Sep 17 00:00:00 2001 From: Will Gutierrez Date: Mon, 18 Jan 2021 15:28:21 -0800 Subject: [PATCH 03/13] set preview when calling E.setState #9004 --- app/assets/javascripts/editorHelper.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/assets/javascripts/editorHelper.js b/app/assets/javascripts/editorHelper.js index 5204aeaefd..d38559f0bb 100644 --- a/app/assets/javascripts/editorHelper.js +++ b/app/assets/javascripts/editorHelper.js @@ -8,10 +8,13 @@ const getEditorParams = (targetDiv) => { params['dSelected'] = $(closestCommentFormWrapper); // assign the ID of the textarea within the closest comment-form-wrapper params['textarea'] = closestCommentFormWrapper.querySelector('textarea').id; + params['preview'] = closestCommentFormWrapper.querySelector('.comment-preview').id; } else { // default to #text-input // #text-input ID should be unique, and the only comment form on /wiki/new & /wiki/edit params['textarea'] = 'text-input'; + // #preview-main should be unique as well + params['preview'] = 'preview-main'; } return params; }; \ No newline at end of file From aff84c9afe4e7c59df877c59a231993387d138a6 Mon Sep 17 00:00:00 2001 From: Will Gutierrez Date: Mon, 18 Jan 2021 15:28:58 -0800 Subject: [PATCH 04/13] change E.initialize to E.setState; add comment-preview class #9004 --- app/views/comments/_edit.html.erb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/views/comments/_edit.html.erb b/app/views/comments/_edit.html.erb index 71196006a0..10e2204b44 100644 --- a/app/views/comments/_edit.html.erb +++ b/app/views/comments/_edit.html.erb @@ -40,11 +40,9 @@ -