InputEditorCore.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <div
  3. class='input_editor_core'
  4. >
  5. <div>
  6. <emoji-selector
  7. v-model='emojiSelectorVisible'
  8. @emoji='addEmoji'
  9. :right-align='rightAlignEmoji'
  10. ></emoji-selector>
  11. <div class='input_editor_core__format_bar'>
  12. <div
  13. class='input_editor_core__format_button input_editor_core__format_button--emoji'
  14. title='Emoji'
  15. @click='emojiSelectorVisible = true'
  16. >
  17. <span class='fa fa-smile'></span>
  18. </div>
  19. <div
  20. class='input_editor_core__format_button'
  21. title='Bold (ctrl + b)'
  22. @click='replaceSelectedText("__", "__")'
  23. >
  24. B
  25. </div>
  26. <div
  27. class='input_editor_core__format_button'
  28. title='Italic (ctrl + i)'
  29. @click='replaceSelectedText("*", "*")'
  30. >
  31. I
  32. </div>
  33. <div
  34. class='input_editor_core__format_button'
  35. title='Link (ctrl + l)'
  36. @click='setModalState("link", true)'
  37. >
  38. <span class='fa fa-link'></span>
  39. </div>
  40. <div
  41. class='input_editor_core__format_button'
  42. style='margin-left: 0.25rem;'
  43. title='Code (ctrl + k)'
  44. @click='formatCode'
  45. >
  46. <span class='fa fa-code'></span>
  47. </div>
  48. </div>
  49. <textarea
  50. class='input_editor_core__input'
  51. placeholder='Type here - you can format using Markdown'
  52. ref='textarea'
  53. :value='value'
  54. @input='setEditor($event.target.value)'
  55. @focus='$emit("focus")'
  56. @blur='$emit("blur")'
  57. @keydown.ctrl.66.prevent='replaceSelectedText("__", "__")'
  58. @keydown.ctrl.73.prevent='replaceSelectedText("*", "*")'
  59. @keydown.ctrl.76.prevent='setModalState("link", true)'
  60. @keydown.ctrl.75.prevent='formatCode'
  61. >
  62. </textarea>
  63. </div>
  64. <modal-window v-model='linkModalVisible'>
  65. <div slot='main'>
  66. <p>
  67. Enter the web address in the input box below
  68. </p>
  69. <fancy-input placeholder='Text for link' width='100%' v-model='linkText'></fancy-input>
  70. <fancy-input placeholder='Web address for link' width='100%' v-model='linkURL'></fancy-input>
  71. </div>
  72. <div slot='footer'>
  73. <button class='button button--green button--modal' @click='addLink'>
  74. Add link
  75. </button>
  76. <button class='button button--modal' @click='setModalState("link", false)'>
  77. Cancel
  78. </button>
  79. </div>
  80. </modal-window>
  81. </div>
  82. </template>
  83. <script>
  84. import ModalWindow from './ModalWindow'
  85. import FancyInput from './FancyInput'
  86. import TabView from './TabView'
  87. import ErrorTooltip from './ErrorTooltip'
  88. import EmojiSelector from './EmojiSelector'
  89. let usernames = {}
  90. export default {
  91. name: 'InputEditorCore',
  92. props: ['value', 'error', 'right-align-emoji'],
  93. components: {
  94. ModalWindow,
  95. FancyInput,
  96. ErrorTooltip,
  97. EmojiSelector
  98. },
  99. data () {
  100. return {
  101. linkText: '',
  102. linkURL: '',
  103. linkModalVisible: false,
  104. imageModalVisible: false,
  105. emojiSelectorVisible: false
  106. }
  107. },
  108. methods: {
  109. setModalState (modal, state) {
  110. if(modal === 'link') {
  111. this.linkModalVisible = state
  112. if(state) {
  113. this.linkText = this.getSelectionData().val;
  114. } else {
  115. this.linkText = '';
  116. this.linkURL = '';
  117. }
  118. } else if(modal === 'image') {
  119. this.imageModalVisible = state
  120. }
  121. },
  122. checkUsernames (matches) {
  123. let doneCount = 0
  124. let mentions = []
  125. let done = res => {
  126. doneCount++
  127. if(res) mentions.push(res)
  128. if(doneCount === matches.length) {
  129. this.$emit('mentions', mentions)
  130. }
  131. }
  132. matches.forEach(match => {
  133. this.checkUsername(match, done)
  134. })
  135. },
  136. checkUsername (match, cb) {
  137. let username = match.trim().slice(1)
  138. let checkedUsername = usernames[username]
  139. if(checkedUsername !== undefined) {
  140. cb(checkedUsername)
  141. } else if(checkedUsername === undefined) {
  142. this.axios
  143. .get('/api/v1/user/' + username)
  144. .then(_ => {
  145. usernames[username] = username
  146. cb(username)
  147. })
  148. .catch(_ => {
  149. usernames[username] = null
  150. cb(null)
  151. })
  152. }
  153. },
  154. setEditor (value) {
  155. let matches = value.match(/(^|\s)@[^\s]+/g) || []
  156. this.checkUsernames(matches)
  157. this.$emit('input', value)
  158. },
  159. getSelectionData () {
  160. var el = this.$refs.textarea,
  161. start = el.selectionStart,
  162. end = el.selectionEnd;
  163. return {
  164. val: el.value.slice(start, end),
  165. start,
  166. end
  167. };
  168. },
  169. replaceSelectedText (before, after) {
  170. var selectionData = this.getSelectionData();
  171. var el = this.$refs.textarea;
  172. if(
  173. this.value.substr(selectionData.start - before.length, before.length) === before &&
  174. this.value.substr(selectionData.end, after.length) === after
  175. ) {
  176. this.setEditor(
  177. this.value.slice(0, selectionData.start - before.length) +
  178. selectionData.val +
  179. this.value.slice(selectionData.end + after.length)
  180. );
  181. setTimeout(function() {
  182. el.selectionStart = selectionData.start - before.length;
  183. el.selectionEnd = selectionData.end - after.length;
  184. }, 0);
  185. } else {
  186. this.setEditor(
  187. this.value.slice(0, selectionData.start) +
  188. before + selectionData.val + after +
  189. this.value.slice(selectionData.end)
  190. );
  191. setTimeout(function() {
  192. el.selectionStart = selectionData.start + before.length;
  193. el.selectionEnd = selectionData.end + after.length;
  194. }, 0);
  195. }
  196. el.focus();
  197. },
  198. addLink () {
  199. var linkTextLength = this.linkText.length;
  200. var selectionData = this.getSelectionData();
  201. var el = this.$refs.textarea;
  202. this.setEditor(
  203. this.value.slice(0, selectionData.start) +
  204. '[' + this.linkText + '](' + this.linkURL + ')' +
  205. this.value.slice(selectionData.end)
  206. );
  207. el.focus();
  208. setTimeout(function() {
  209. el.selectionStart = selectionData.start + 1;
  210. el.selectionEnd = selectionData.start + 1 + linkTextLength;
  211. }, 0);
  212. this.setModalState('link', false);
  213. },
  214. addEmoji (emoji) {
  215. var selectionData = this.getSelectionData();
  216. var el = this.$refs.textarea;
  217. this.setEditor(
  218. this.value.slice(0, selectionData.start) +
  219. emoji +
  220. this.value.slice(selectionData.end)
  221. );
  222. el.focus();
  223. setTimeout(function() {
  224. el.selectionStart = selectionData.start + emoji.length;
  225. el.selectionEnd = selectionData.start + emoji.length;
  226. }, 0);
  227. },
  228. formatCode (e) {
  229. e.preventDefault()
  230. var selectionData = this.getSelectionData();
  231. if(this.value[selectionData.start-1] === '\n' || selectionData.start === 0) {
  232. var el = this.$refs.textarea;
  233. var matches = ( selectionData.val.match(/\n/g) || [] ).length
  234. var replacedText = ' ' + selectionData.val.replace(/\n/g, '\n ')
  235. this.setEditor(
  236. this.value.slice(0, selectionData.start) +
  237. replacedText +
  238. this.value.slice(selectionData.end)
  239. );
  240. el.focus();
  241. setTimeout(function() {
  242. el.selectionStart = selectionData.start + 4;
  243. el.selectionEnd = selectionData.end + (matches + 1)*4;
  244. }, 0);
  245. } else {
  246. this.replaceSelectedText('`', '`');
  247. }
  248. }
  249. }
  250. }
  251. </script>
  252. <style lang='scss' scoped>
  253. @import '../assets/scss/variables.scss';
  254. .input_editor_core {
  255. @at-root #{&}__format_bar {
  256. width: auto;
  257. position: absolute;
  258. height: 2rem;
  259. top: 0.25rem;
  260. right: 0;
  261. background-color: transparent;
  262. display: flex;
  263. align-items: center;
  264. padding: 0 0.125rem;
  265. margin-right: 2.4rem;
  266. }
  267. @at-root #{&}__format_button {
  268. height: 1.5rem;
  269. width: 1.5rem;
  270. border-radius: 0.25rem;
  271. text-align: center;
  272. line-height: 1.4rem;
  273. cursor: pointer;
  274. @include user-select(none);
  275. @include text($font--role-default, 1rem, 600);
  276. color: $color__darkgray--primary;
  277. border: thin solid $color__gray--primary;
  278. transition: background-color 0.2s;
  279. &:hover {
  280. background-color: $color__gray--darker;
  281. }
  282. &:active {
  283. background-color: $color__gray--darkest;
  284. }
  285. }
  286. @at-root #{&}__spacer {
  287. width: 0.6rem;
  288. }
  289. @at-root #{&}__input {
  290. width: 100%;
  291. height: 8rem;
  292. border: 0;
  293. padding: 0.5rem;
  294. @include text;
  295. outline: none;
  296. resize: none;
  297. @include placeholder {
  298. @include text($font--role-emphasis, 1rem);
  299. display: flex;
  300. align-content: center;
  301. @include user-select(none);
  302. cursor: default;
  303. }
  304. }
  305. @at-root #{&}__error {
  306. position: absolute;
  307. background-color: #ffeff1;
  308. border: 0.125rem solid #D32F2F;
  309. font-size: 0.9rem;
  310. padding: 0.1rem 0.25rem;
  311. top: 0.2125rem;
  312. left: calc(100% + 0.25rem);
  313. white-space: nowrap;
  314. &:first-letter{ text-transform: capitalize; }
  315. opacity: 0;
  316. pointer-events: none;
  317. margin-top: -1rem;
  318. transition: opacity 0.2s, margin-top 0.2s;
  319. @at-root #{&}--show {
  320. opacity: 1;
  321. pointer-events: all;
  322. margin-top: 0;
  323. transition: opacity 0.2s, margin-top 0.2s;
  324. }
  325. &::after {
  326. content: '';
  327. position: relative;
  328. width: 0;
  329. height: 0;
  330. display: inline-block;
  331. right: calc(100% + 0.3rem);
  332. border-top: 0.3rem solid transparent;
  333. border-bottom: 0.3rem solid transparent;
  334. border-right: 0.3rem solid #D32F2F;
  335. }
  336. }
  337. }
  338. @media (max-width: 420px) {
  339. .input_editor_core__format_button--emoji {
  340. display: none;
  341. }
  342. }
  343. </style>