A Pen by YingshanDeng on CodePen.
Created
July 10, 2019 05:21
-
-
Save ZSX-JOJO/f108e500a44ac3a52d81bb3e54f6439d to your computer and use it in GitHub Desktop.
RichInputBox
This file contains hidden or 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
| <div class="rich-editor"> | |
| <div class="wrapper"> | |
| <div class="editor-area" contenteditable="true" placeholder="Enter text here..."></div> | |
| <div class="editor-backer"></div> | |
| </div> | |
| <span class="counter">140</span> | |
| </div> |
This file contains hidden or 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
| var editorArea = document.querySelector('.editor-area') | |
| var editorBacker = document.querySelector('.editor-backer') | |
| var counter = document.querySelector('.counter') | |
| var limitCnt = 140 | |
| var isComposing = false | |
| editorArea.addEventListener('compositionstart', () => { | |
| isComposing = true | |
| }) | |
| editorArea.addEventListener('compositionend', () => { | |
| isComposing = false | |
| // | |
| let text = editorArea.innerText | |
| setCounter(limitCnt - text.length) | |
| }) | |
| function setCounter (cnt) { | |
| counter.innerHTML = cnt | |
| if (cnt < 0) { | |
| counter.classList.add('max-reached') | |
| } else { | |
| counter.classList.remove('max-reached') | |
| } | |
| } | |
| var inputCompose = function () { | |
| if (editorArea.innerHTML === '<div><br></div>' || | |
| editorArea.innerHTML === '<br>' || | |
| editorArea.innerHTML === '') { | |
| editorArea.classList.add('is-showPlaceholder') | |
| } else { | |
| editorArea.classList.remove('is-showPlaceholder') | |
| } | |
| let text = editorArea.innerText | |
| let remainingCnt = limitCnt - text.trim().length | |
| if (remainingCnt < 0) { | |
| let allowedText = text.substring(0, limitCnt) | |
| let refusedText = text.substring(limitCnt) | |
| editorBacker.innerHTML = allowedText + '<em>' + refusedText + '</em>' | |
| } else { | |
| editorBacker.innerHTML = '' | |
| } | |
| if (!isComposing) { | |
| setCounter(remainingCnt) | |
| } | |
| } | |
| inputCompose(); | |
| editorArea.oninput = inputCompose |
This file contains hidden or 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
| html, | |
| body { | |
| display: flex; | |
| width: 100%; | |
| height: 100%; | |
| padding: 0; | |
| margin: 0; | |
| background: #E3F3FD; | |
| font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; | |
| } | |
| .rich-editor { | |
| width: 508px; | |
| height: auto; | |
| /* max-height: 134px; | |
| overflow-y: scroll;*/ | |
| margin: auto; | |
| padding: 8px; | |
| background: #fff; | |
| border-radius: 8px; | |
| border: 1px solid #A4D9F9; | |
| box-shadow: 0 0 0 1px #A4D9F9; | |
| } | |
| .wrapper { | |
| position: relative; | |
| width: 100%; | |
| min-height: 116px; | |
| z-index: 1; | |
| } | |
| .editor-area, .editor-backer { | |
| width: 100%; | |
| height: auto; | |
| min-height: 116px; | |
| font-size: 14px; | |
| line-height: 20px; | |
| word-wrap: break-word; | |
| background: transparent; | |
| } | |
| .editor-area { | |
| outline: none; | |
| border: none; | |
| } | |
| .editor-area.is-showPlaceholder[contenteditable=true]:before { | |
| content: attr(placeholder); | |
| color: #ccd6dd; | |
| position: absolute; | |
| } | |
| .editor-backer { | |
| position: absolute; | |
| left: 0; | |
| top: 0; | |
| color: transparent; | |
| z-index: -1; | |
| } | |
| .editor-backer em { | |
| background-color: #ffb8c2; | |
| font-style: normal; | |
| font-size: 14px; | |
| line-height: 20px; | |
| white-space: pre-wrap; | |
| word-wrap: break-word; | |
| } | |
| .counter { | |
| float: right; | |
| color: #657786; | |
| font-size: 14px; | |
| line-height: 20px; | |
| text-align: right; | |
| user-select: none; | |
| } | |
| .counter.max-reached { | |
| color: #e0245e; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment