|
1 | 1 | <template>
|
2 | 2 | <div class="editor">
|
3 |
| - <div class="rich" contenteditable="true"></div> |
| 3 | + <div |
| 4 | + ref="rich" |
| 5 | + class="rich" |
| 6 | + contenteditable="true" |
| 7 | + :placehoder="placehoder" |
| 8 | + @keydown="onKeydown" |
| 9 | + @input="onInput" |
| 10 | + @paste="onPaste" |
| 11 | + ></div> |
| 12 | + <div class="send" :class="{ disabled: disabledSend }" @click="eventSend"></div> |
| 13 | + <img class="author-avatar" src="https://avatars1.githubusercontent.com/u/6846113" alt="preload-avatar"> |
| 14 | + <div class="auto"> |
| 15 | + <button @click="eventClickMockReceived" class="button button-clear item">received one random</button> |
| 16 | + <span class="line">|</span> |
| 17 | + <button @click="eventClickMockSend" class="button button-clear item">send one random</button> |
| 18 | + </div> |
4 | 19 | </div>
|
5 | 20 | </template>
|
6 | 21 |
|
7 | 22 | <script>
|
| 23 | +import { isMobile } from '../../common/ua' |
| 24 | +import { genBody, genSid } from './util' |
| 25 | +
|
| 26 | +const genMessage = (content) => { |
| 27 | + let body = genBody() |
| 28 | + body.user = { |
| 29 | + name: 'tangbc', |
| 30 | + avatar: 'https://avatars1.githubusercontent.com/u/6846113' |
| 31 | + } |
| 32 | + body.sid = genSid(), |
| 33 | + body.content = content |
| 34 | + body.isCreator = true |
| 35 | + return body |
| 36 | +} |
| 37 | +
|
8 | 38 | export default {
|
9 |
| - name: 'editor' |
| 39 | + name: 'editor', |
| 40 | +
|
| 41 | + props: { |
| 42 | + send: { |
| 43 | + type: Function |
| 44 | + }, |
| 45 | + received: { |
| 46 | + type: Function |
| 47 | + } |
| 48 | + }, |
| 49 | +
|
| 50 | + data () { |
| 51 | + return { |
| 52 | + disabledSend: true, |
| 53 | + placehoder: isMobile ? 'Input message here' : 'Input message here and press enter to send' |
| 54 | + } |
| 55 | + }, |
| 56 | +
|
| 57 | + mounted () { |
| 58 | + if (this.$refs.rich) { |
| 59 | + this.$refs.rich.focus() |
| 60 | + } |
| 61 | + }, |
| 62 | +
|
| 63 | + methods: { |
| 64 | + onKeydown (e) { |
| 65 | + if (e.keyCode === 13) { |
| 66 | + this.eventSend() |
| 67 | + e.preventDefault() |
| 68 | + } |
| 69 | + }, |
| 70 | +
|
| 71 | + onInput () { |
| 72 | + if (this.$refs.rich) { |
| 73 | + this.disabledSend = !(this.$refs.rich.textContent || '').trim('') |
| 74 | + } |
| 75 | + }, |
| 76 | +
|
| 77 | + onPaste (e) { |
| 78 | + e.preventDefault() |
| 79 | + const plainText = (e.originalEvent || e).clipboardData.getData('text/plain') |
| 80 | + document.execCommand("insertText", false, plainText) |
| 81 | + }, |
| 82 | +
|
| 83 | + eventSend () { |
| 84 | + if (this.$refs.rich && !this.disabledSend) { |
| 85 | + const content = (this.$refs.rich.textContent || '').trim() |
| 86 | + const message = genMessage(content) |
| 87 | + this.$refs.rich.innerHTML = '' |
| 88 | + this.$emit('send', message) |
| 89 | + } |
| 90 | + }, |
| 91 | +
|
| 92 | + eventClickMockReceived () { |
| 93 | + this.received() |
| 94 | + }, |
| 95 | +
|
| 96 | + eventClickMockSend () { |
| 97 | + this.send(genMessage('')) |
| 98 | + } |
| 99 | + } |
10 | 100 | }
|
11 | 101 | </script>
|
12 | 102 |
|
13 | 103 | <style lang="less" scoped>
|
14 | 104 | .editor {
|
15 |
| - padding: 1em; |
16 |
| - border: 2px solid; |
17 |
| - border-color: dimgray; |
18 |
| - border-top: 1px solid; |
| 105 | + position: relative; |
19 | 106 | }
|
20 | 107 | .rich {
|
| 108 | + border: 2px solid; |
| 109 | + border-color: dimgray; |
| 110 | + border-top: none; |
| 111 | + width: 100%; |
| 112 | + padding: 1em 4em 1em 1em; |
21 | 113 | outline: none;
|
| 114 | + cursor: text; |
| 115 | + background-color: #fffaf0; |
| 116 | + &:empty::before{ |
| 117 | + color: darkgray; |
| 118 | + content: attr(placehoder); |
| 119 | + } |
| 120 | +} |
| 121 | +.send { |
| 122 | + position: absolute; |
| 123 | + right: 20px; |
| 124 | + top: 15px; |
| 125 | + width: 33px; |
| 126 | + height: 30px; |
| 127 | + cursor: pointer; |
| 128 | + background-size: cover; |
| 129 | + background-image: url("data:image/svg+xml,%3Csvg t='1587742943994' class='icon' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='3418' width='200' height='200'%3E%3Cpath d='M0 561.6l366.4 100.8 400-356.8-304 401.6 363.2 107.2L1024 46.4zM460.8 977.6l126.4-192-126.4-25.6z' p-id='3419' fill='%239b4dca'%3E%3C/path%3E%3C/svg%3E"); |
| 130 | + &:active { |
| 131 | + opacity: .7; |
| 132 | + } |
| 133 | + &.disabled { |
| 134 | + opacity: .3; |
| 135 | + pointer-events: none; |
| 136 | + } |
| 137 | +} |
| 138 | +.author-avatar { |
| 139 | + display: none; |
| 140 | +} |
| 141 | +.item { |
| 142 | + padding: 0; |
| 143 | +} |
| 144 | +.line { |
| 145 | + font-size: 12px; |
| 146 | + margin: 0 1em; |
22 | 147 | }
|
23 | 148 | </style>
|
0 commit comments