-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add setState Method in editor.js #9035
Changes from 10 commits
298355f
dda4d2d
a4ae7c3
aff84c9
7a64190
e0b148b
2af718b
0784378
aacb494
16447e2
ca93f14
f44a6bb
2f04589
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
}, | ||
|
@@ -54,14 +53,19 @@ $E = { | |
// preview | ||
$E.preview = ($D.selected).find('.comment-preview').eq(0); | ||
}, | ||
isRichTextEditor: function(url) { | ||
// this RegEx matches three different cases where the legacy editor is still used: | ||
// 1. /wiki/new | ||
// 2. /wiki/{wiki name}/edit | ||
// 3. /features/new | ||
const legacyEditorPath = RegExp(/\/(wiki|features)(\/[^\/]+\/edit|\/new)/); | ||
return !legacyEditorPath.test(url); // if we're not on one of these pages, we are using the rich-text editor. | ||
}, | ||
// wraps currently selected text in textarea with strings a and b | ||
wrap: function(a,b,args) { | ||
// this RegEx is: /wiki/ + any char not "/" + /edit | ||
const isWikiCommentPage = (/\/wiki\/[^\/]+\/edit/).test(window.location.pathname); | ||
// we don't need to refresh $E's values if we're on a page with a single comment form, ie. /wiki/new or /wiki/edit | ||
if (window.location.pathname !== "/wiki/new" && !isWikiCommentPage && $D.selected) { | ||
this.refresh(); | ||
} | ||
wrap: function(a, b, args) { | ||
noi5e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// we only refresh $E's values if we are on a page using the rich-text editor (most pages). | ||
// the legacy editor pages only have one editor form, unlike pages with multiple comments. | ||
if (this.isRichTextEditor(window.location.pathname)) { this.refresh(); } | ||
var len = $E.textarea.val().length; | ||
var start = $E.textarea[0].selectionStart; | ||
var end = $E.textarea[0].selectionEnd; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jywarren codeclimate won't let this pass because of 'cognitive complexity' in the wrap function... I think the rules are stricter now since the recent changes in CI. Honestly, I have a feeling that it's the older stuff later on in the method that's holding this back (conditionals like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jywarren Never mind! I got it to work. Tests are passing now too. Ready to merge! |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,6 @@ | |
|
||
<script> | ||
jQuery(document).ready(function() { | ||
$E.initialize({}) | ||
$E.initialize(); | ||
}) | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aka ES6 destructuring assignment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oohhhh!!! 🤗