InputEditorCore.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div
  3. class='input_editor_core'
  4. >
  5. <error-tooltip :error='error'></error-tooltip>
  6. <div>
  7. <div class='input_editor_core__format_bar'>
  8. <div class='input_editor_core__format_button' @click='replaceSelectedText("**", "**")'>B</div>
  9. <div class='input_editor_core__format_button' @click='replaceSelectedText("*", "*")'>I</div>
  10. <div class='input_editor_core__format_button' @click='setModalState("link", true)'><span class='fa fa-link'></span></div>
  11. <div class='input_editor_core__format_button' @click='formatCode'><span class='fa fa-code'></span></div>
  12. <div class='input_editor_core__format_button' @click='setModalState("image", true)'><span class='fa fa-picture-o'></span></div>
  13. </div>
  14. <textarea
  15. class='input_editor_core__input'
  16. placeholder='Type here - you can format using Markdown'
  17. ref='textarea'
  18. :value='value'
  19. @input='setEditor($event.target.value)'
  20. @focus='$emit("focus")'
  21. @blur='$emit("blur")'
  22. @keydown.prevent.ctrl.66='replaceSelectedText("**", "**")'
  23. @keydown.prevent.ctrl.73='replaceSelectedText("*", "*")'
  24. @keydown.prevent.ctrl.76='setModalState("link", true)'
  25. @keydown.prevent.ctrl.75='formatCode'
  26. >
  27. </textarea>
  28. </div>
  29. <modal-window v-model='linkModalVisible'>
  30. <div style='padding: 1rem;'>
  31. <p style='margin-top: 0;'>
  32. Enter the web address in the input box below
  33. </p>
  34. <fancy-input placeholder='Text for link' width='100%' v-model='linkText'></fancy-input>
  35. <fancy-input placeholder='Web address for link' width='100%' v-model='linkURL'></fancy-input>
  36. <button class='button' @click='addLink'>
  37. OK
  38. </button>
  39. <button class='button' @click='setModalState("link", false)'>
  40. Cancel
  41. </button>
  42. </div>
  43. </modal-window>
  44. <modal-window v-model='imageModalVisible'></modal-window>
  45. </div>
  46. </template>
  47. <script>
  48. import ModalWindow from './ModalWindow'
  49. import FancyInput from './FancyInput'
  50. import TabView from './TabView'
  51. import ErrorTooltip from './ErrorTooltip'
  52. export default {
  53. name: 'InputEditorCore',
  54. props: ['value', 'error'],
  55. components: {
  56. ModalWindow,
  57. FancyInput,
  58. ErrorTooltip
  59. },
  60. data () {
  61. return {
  62. linkText: '',
  63. linkURL: '',
  64. linkModalVisible: false,
  65. imageModalVisible: false,
  66. }
  67. },
  68. methods: {
  69. setModalState (modal, state) {
  70. if(modal === 'link') {
  71. this.linkModalVisible = state
  72. if(state) {
  73. this.linkText = this.getSelectionData().val;
  74. } else {
  75. this.linkText = '';
  76. this.linkURL = '';
  77. }
  78. } else if(modal === 'image') {
  79. this.imageModalVisible = state
  80. }
  81. },
  82. setEditor (value) {
  83. this.$emit('input', value)
  84. },
  85. getSelectionData () {
  86. var el = this.$refs.textarea,
  87. start = el.selectionStart,
  88. end = el.selectionEnd;
  89. return {
  90. val: el.value.slice(start, end),
  91. start,
  92. end
  93. };
  94. },
  95. replaceSelectedText (before, after) {
  96. var selectionData = this.getSelectionData();
  97. var el = this.$refs.textarea;
  98. this.setEditor(
  99. this.value.slice(0, selectionData.start) +
  100. before + selectionData.val + after +
  101. this.value.slice(selectionData.end)
  102. );
  103. el.focus();
  104. setTimeout(function() {
  105. el.selectionStart = selectionData.start + before.length;
  106. el.selectionEnd = selectionData.end + before.length;
  107. }, 1);
  108. },
  109. addLink () {
  110. var linkTextLength = this.linkText.length;
  111. var selectionData = this.getSelectionData();
  112. var el = this.$refs.textarea;
  113. this.setEditor(
  114. this.value.slice(0, selectionData.start) +
  115. '[' + this.linkText + '](' + this.linkURL + ')' +
  116. this.value.slice(selectionData.end)
  117. );
  118. el.focus();
  119. setTimeout(function() {
  120. el.selectionStart = selectionData.start + 1;
  121. el.selectionEnd = selectionData.start + 1 + linkTextLength;
  122. }, 1);
  123. this.setModalState('link', false);
  124. },
  125. formatCode () {
  126. var selectionData = this.getSelectionData();
  127. if(this.value[selectionData.start-1] === '\n' || selectionData.start === 0) {
  128. var el = this.$refs.textarea;
  129. var matches = selectionData.val.match(/\n/g || [] ).length
  130. var replacedText = ' ' + selectionData.val.replace(/\n/g, '\n ')
  131. this.setEditor(
  132. this.value.slice(0, selectionData.start) +
  133. replacedText +
  134. this.value.slice(selectionData.end)
  135. );
  136. el.focus();
  137. setTimeout(function() {
  138. el.selectionStart = selectionData.start + 4;
  139. el.selectionEnd = selectionData.end + matches*4;
  140. }, 1);
  141. } else {
  142. this.replaceSelectedText('`', '`');
  143. }
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang='scss' scoped>
  149. @import '../assets/scss/variables.scss';
  150. .input_editor_core {
  151. @at-root #{&}__format_bar {
  152. width: auto;
  153. position: absolute;
  154. height: 2rem;
  155. top: 0.25rem;
  156. right: 0;
  157. background-color: transparent;
  158. display: flex;
  159. align-items: center;
  160. padding: 0 0.125rem;
  161. margin-right: 2.4rem;
  162. }
  163. @at-root #{&}__format_button {
  164. height: 1.5rem;
  165. width: 1.5rem;
  166. text-align: center;
  167. line-height: 1.4rem;
  168. cursor: pointer;
  169. @include user-select(none);
  170. @include text($font--role-default, 1rem, 600);
  171. color: $color__darkgray--primary;
  172. border: thin solid $color__gray--primary;
  173. transition: background-color 0.2s;
  174. margin: 0 0.25rem;
  175. &:hover {
  176. background-color: $color__gray--darker;
  177. }
  178. &:active {
  179. background-color: $color__gray--darkest;
  180. }
  181. }
  182. @at-root #{&}__spacer {
  183. width: 0.6rem;
  184. }
  185. @at-root #{&}__input {
  186. width: 100%;
  187. height: 8rem;
  188. border: 0;
  189. padding: 0.5rem;
  190. @include text;
  191. outline: none;
  192. resize: none;
  193. @include placeholder {
  194. @include text($font--role-emphasis, 1rem);
  195. display: flex;
  196. align-content: center;
  197. @include user-select(none);
  198. cursor: default;
  199. color: $color__gray--darker;
  200. }
  201. }
  202. @at-root #{&}__error {
  203. position: absolute;
  204. background-color: #ffeff1;
  205. border: 0.125rem solid #D32F2F;
  206. font-size: 0.9rem;
  207. padding: 0.1rem 0.25rem;
  208. top: 0.2125rem;
  209. left: calc(100% + 0.25rem);
  210. white-space: nowrap;
  211. &:first-letter{ text-transform: capitalize; }
  212. opacity: 0;
  213. pointer-events: none;
  214. margin-top: -1rem;
  215. transition: opacity 0.2s, margin-top 0.2s;
  216. @at-root #{&}--show {
  217. opacity: 1;
  218. pointer-events: all;
  219. margin-top: 0;
  220. transition: opacity 0.2s, margin-top 0.2s;
  221. }
  222. &::after {
  223. content: '';
  224. position: relative;
  225. width: 0;
  226. height: 0;
  227. display: inline-block;
  228. right: calc(100% + 0.3rem);
  229. border-top: 0.3rem solid transparent;
  230. border-bottom: 0.3rem solid transparent;
  231. border-right: 0.3rem solid #D32F2F;
  232. }
  233. }
  234. }
  235. </style>