-
Notifications
You must be signed in to change notification settings - Fork 116
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
Create highlight dialog #184
Merged
JustinHoldstock
merged 11 commits into
box:master
from
JustinHoldstock:Create-highlight-dialog
Jun 28, 2017
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0e5714a
Chore: Separate create highlight dialog and quad point creation
7dbcb0c
Chore: remove unused constants and change scss
60f722b
Chore: Adding tests
21be4db
Merge branch 'master' of github.com:JustinHoldstock/box-content-previ…
f0cc97d
Chore: PR Feedback
fe74d48
Chore: More PR feedback
6d75f03
Chore: Test updates
59ca760
Chore: Fixed last char not un-highlighting when removing plain
c9f025f
Chore: Set state to render highlights
f87f0af
Merge branch 'master' of github.com:JustinHoldstock/box-content-previ…
fe0bdd1
Chore: Remove old test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
import EventEmitter from 'events'; | ||
import { CLASS_ACTIVE } from '../constants'; | ||
import { hideElement, showElement } from './annotatorUtil'; | ||
|
||
// Display Text | ||
const TEXT_ANNOTATION_CANCEL = __('annotation_cancel'); | ||
const TEXT_ANNOTATION_POST = __('annotation_post'); | ||
const TEXT_ADD_COMMENT_PLACEHOLDER = __('annotation_add_comment_placeholder'); | ||
|
||
class CommentBox extends EventEmitter { | ||
/** | ||
* Text displayed in the Cancel button element. | ||
* | ||
* @property {string} | ||
*/ | ||
cancelText = TEXT_ANNOTATION_CANCEL; | ||
|
||
/** | ||
* Text displayed in the Post button element. | ||
* | ||
* @property {string} | ||
*/ | ||
postText = TEXT_ANNOTATION_POST; | ||
|
||
/** | ||
* Placeholder text displayed in the text area element. | ||
* | ||
* @property {string} | ||
*/ | ||
placeholderText = TEXT_ADD_COMMENT_PLACEHOLDER; | ||
|
||
/** | ||
* Reference to the comment box element. Contains buttons and text area. | ||
* | ||
* @property {HTMLElement} | ||
*/ | ||
containerEl; | ||
|
||
/** | ||
* Reference to the cancel button element in the comment box. | ||
* | ||
* @property {HTMLElement} | ||
*/ | ||
cancelEl; | ||
|
||
/** | ||
* Reference to the post button element in the comment box. | ||
* | ||
* @property {HTMLElement} | ||
*/ | ||
postEl; | ||
|
||
/** | ||
* Reference to the text area element in the comment box. | ||
* | ||
* @property {HTMLElement} | ||
*/ | ||
textAreaEl; | ||
|
||
/** | ||
* Reference to parent element that the comment box should be nested inside. | ||
* | ||
* @property {HTMLElement} | ||
*/ | ||
parentEl; | ||
|
||
/* Events that the comment box can emit. */ | ||
static CommentEvents = { | ||
cancel: 'comment_cancel', | ||
post: 'comment_post' | ||
}; | ||
|
||
/** | ||
* Creates an element for text entry, submission and cancellation. | ||
* | ||
* @param {Object} [config] - Object containing text values to be displayed to the user. | ||
* config.cancel - Text displayed in the "Cancel" button | ||
* config.post - Text displayed in the "Post" button | ||
* config.placeholder - Placeholder text displayed in the text area | ||
*/ | ||
constructor(parentEl, config = {}) { | ||
super(); | ||
|
||
this.parentEl = parentEl; | ||
|
||
this.cancelText = config.cancel || this.cancelText; | ||
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. Might be overkill to make these class vars if they are only used once. 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. Sticking to it until we find a standard to follow |
||
this.postText = config.post || this.postText; | ||
this.placeholderText = config.placeholder || this.placeholderText; | ||
|
||
// Explicit scope binding for event listeners | ||
this.onCancel = this.onCancel.bind(this); | ||
this.onPost = this.onPost.bind(this); | ||
} | ||
|
||
/** | ||
* Focus on the text box. | ||
* | ||
* @public | ||
* @return {void} | ||
*/ | ||
focus() { | ||
if (!this.containerEl) { | ||
return; | ||
} | ||
|
||
this.textAreaEl.focus(); | ||
} | ||
|
||
/** | ||
* Clear out the text box. | ||
* | ||
* @public | ||
* @return {void} | ||
*/ | ||
clear() { | ||
if (!this.containerEl) { | ||
return; | ||
} | ||
|
||
this.textAreaEl.value = ''; | ||
} | ||
|
||
/** | ||
* Hide the element. | ||
* | ||
* @public | ||
* @return {void} | ||
*/ | ||
hide() { | ||
if (!this.containerEl) { | ||
return; | ||
} | ||
|
||
hideElement(this.containerEl); | ||
} | ||
|
||
/** | ||
* Show the element. | ||
* | ||
* @public | ||
* @return {void} | ||
*/ | ||
show() { | ||
if (!this.containerEl) { | ||
this.containerEl = this.createCommentBox(); | ||
this.parentEl.appendChild(this.containerEl); | ||
} | ||
|
||
showElement(this.containerEl); | ||
} | ||
|
||
/** | ||
* Destructor | ||
* | ||
* @public | ||
*/ | ||
destroy() { | ||
if (!this.containerEl) { | ||
return; | ||
} | ||
|
||
this.containerEl.remove(); | ||
this.parentEl = null; | ||
this.containerEl = null; | ||
this.cancelEl.removeEventListener('click', this.onCancel); | ||
this.postEl.removeEventListener('click', this.onPost); | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Private | ||
//-------------------------------------------------------------------------- | ||
|
||
/** | ||
* Create HTML containing the UI for the comment box. | ||
* | ||
* @private | ||
* @return {HTMLElement} HTML containing UI for the comment box. | ||
*/ | ||
createHTML() { | ||
const containerEl = document.createElement('section'); | ||
containerEl.classList.add('bp-create-highlight-comment'); | ||
containerEl.innerHTML = ` | ||
<textarea class="bp-textarea annotation-textarea ${CLASS_ACTIVE}" | ||
placeholder="${this.placeholderText}"></textarea> | ||
<div class="button-container"> | ||
<button class="bp-btn cancel-annotation-btn"> | ||
${this.cancelText} | ||
</button> | ||
<button class="bp-btn bp-btn-primary post-annotation-btn"> | ||
${this.postText} | ||
</button> | ||
</div>`.trim(); | ||
|
||
return containerEl; | ||
} | ||
|
||
/** | ||
* Clear the current text in the textarea element and notify listeners. | ||
* | ||
* @private | ||
* @return {void} | ||
*/ | ||
onCancel() { | ||
this.clear(); | ||
this.emit(CommentBox.CommentEvents.cancel); | ||
} | ||
|
||
/** | ||
* Notify listeners of submit event and then clear textarea element. | ||
* | ||
* @private | ||
* @return {void} | ||
*/ | ||
onPost() { | ||
this.emit(CommentBox.CommentEvents.post, this.textAreaEl.value); | ||
this.clear(); | ||
} | ||
|
||
/** | ||
* Create HTML for the comment box. Assigns references to elements, attach event listeners. | ||
* ie) Post button, cancel button | ||
* | ||
* @private | ||
* @return {HTMLElement} The HTML to append to this.parentElement | ||
*/ | ||
createCommentBox() { | ||
const containerEl = this.createHTML(); | ||
|
||
// Reference HTML | ||
this.textAreaEl = containerEl.querySelector('.annotation-textarea'); | ||
this.cancelEl = containerEl.querySelector('.cancel-annotation-btn'); | ||
this.postEl = containerEl.querySelector('.post-annotation-btn'); | ||
|
||
// Add event listeners | ||
this.cancelEl.addEventListener('click', this.onCancel); | ||
this.postEl.addEventListener('click', this.onPost); | ||
|
||
return containerEl; | ||
} | ||
} | ||
|
||
export default CommentBox; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why static and not a constant at the top of the file?
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.
DONE! :D