all files / src/components/FileContent/Content/ index.vue

89.29% Statements 25/28
75% Branches 12/16
100% Functions 2/2
89.29% Lines 25/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
<template>
  <div class="content-container">
    <div class="versions" v-show="openHistory">
      <ul v-if="activeFile" class="list">
        <h5 v-if="activeFile.versions && !activeFile.versions.length">No versions</h5>
        <li v-for="(v, index) in activeFile.versions" :key="index" :class="{ active: index === activeIndex }" @click="changeActive(index)" >
          <Version :version="v" />
        </li>
      </ul>
    </div>
    <div class="content">
      <div v-if="activeFile" class="content-main">
        <div class="is-image" v-if="isImage">
          <img :src="imgUrl" :alt="activeFile.name" style="font-size: 14px; font-weight: 300">
        </div>
        <div class="is-code" v-if="isCode">
          <div class="line" v-if="openDiff">
            <div class="line-part" v-for="(d, index) in diff" :key="index">
              <span class="line" v-for="(line, idx) in d.lines" :key="idx" @click="!d.removed ? handleLine(line + d.startAt) : null">
                <div class="notice" v-if="!d.removed && line + d.startAt === activeLine"></div>
                {{d.removed ? '' : line + d.startAt}}
                <span class="question" v-if="!d.removed">?</span>
              </span>
              <div v-if="(d.added || d.removed) && diff.length > 1" class="line-bg" :style="{ background: d.added ? '#41ff79' : '#f03'}"></div>
            </div>
          </div>
          <div class="line" v-else>
            <div class="line-part">
              <span class="line" v-for="(line, idx) in version.content.split('\n').length" :key="idx" @click="handleLine(line)">
                <div class="notice" v-if="line === activeLine"></div>
                {{ line }}
                <span class="question">?</span>
              </span>
            </div>
          </div>
          <pre class="code" v-highlightjs="openDiff ? content : version.content"><code :class="[ extension ]"></code></pre>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { mapGetters, mapActions } from 'vuex'
import Version from './Version'
 
export default {
  name: 'Content',
  components: {
    Version
  },
  props: {
    openHistory: {
      type: Boolean,
      default: true
    },
    openDiff: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      index: 0
    }
  },
  computed: {
    ...mapGetters(['server']),
    ...mapGetters('files', ['activeFile', 'activeIndex', 'activeLine']),
    ...mapGetters('project', ['active']),
    extension () {
      return this.activeFile.extension
    },
 
    isImage () {
      return ['jpg', 'jpeg', 'png', 'tiff', 'gif', 'webp'].indexOf(this.extension) !== -1
    },
 
    isCode () {
      return this.activeFile.versions && this.activeFile.versions.length > 0
    },
 
    // active version
    version () {
      return this.activeFile.versions[this.activeIndex] || { content: '' }
    },
 
    // format diff data
    diff () {
      let index = 0
 
      Eif (this.version.diff && this.version.diff.length) {
        this.version.diff.map((d, idx) => {
          // calc start index of every diff part (default part & added part)
          d.startAt = index
          if (!d.removed) {
            index += d.count
          }
 
          // calc lines count of every diff part
          if (idx === this.version.diff.length - 1) {
            d.lines = d.value.split('\n').length
          } else {
            d.lines = d.count
          }
        })
      }
 
      return this.version.diff || []
    },
 
    // combine every part concent
    content () {
      let content = ''
      Eif (this.version.diff) {
        this.version.diff.map(d => {
          content += d.value
        })
      }
 
      return content
    },
 
    imgUrl () {
      return `${this.server.domain}/${this.active.slug}/files/${this.activeFile.path.full}`
    }
  },
  methods: {
    ...mapActions('files', ['changeIndex', 'setLine']),
    ...mapActions('chat', ['setTarget', 'openChat']),
    // change active version
    changeActive (index) {
      this.changeIndex(index)
      this.index = 0
 
      this.setLine(null)
    },
 
    // add index by number
    indexAdd (number) {
      this.index += number
    },
 
    // click line to set the file - version - line
    handleLine (line) {
      let target = {
        file: this.activeFile.path.full,
        version: this.activeIndex,
        line
      }
 
      this.setTarget(target)
      this.openChat()
    }
  }
}
</script>
 
<style lang="scss" scoped>
.content-container {
  display: flex;
  flex-direction: row;
  .versions {
    flex: 0 0 180px;
    .list {
      h5 {
        margin: 0;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-weight: 300;
        color: rgba(255, 255, 255, .6)
      }
      margin: 0;
      padding: 0;
      list-style: none;
      li {
        cursor: pointer;
      }
      .active {
        background: rgb(34, 32, 58);
      }
    }
  }
  .content {
    flex: 1;
    overflow: auto;
    margin-right: -15px;
    background: rgb(34, 32, 58);
    .content-main {
      overflow: auto;
      .is-image {
        text-align: center;
      }
      .is-code {
        display: flex;
        .line {
          .line-part {
            position: relative;
            .line {
              position: relative;
              display: block;
              font-size: 13px;
              height: 20px;
              line-height: 20px;
              padding: 0 20px;
              color: rgba(255, 255, 255, .6);
              cursor: pointer;
              .notice {
                position: absolute;
                left: 0;
                top: 0;
                height: 100%;
                width: calc(100vw - 250px - 180px);
                background: #00d8ff;
                opacity: .4;
                pointer-events: none;
              }
              .question {
                position: absolute;
                opacity: 0;
                right: 5px;
                top: 0;
                font-size: 12px;
              }
              &:hover {
                color: #4bd1c5;
                .question {
                  opacity: 1;
                }
              }
            }
            .line-bg {
              position: absolute;
              left: 0;
              top: 0;
              height: 100%;
              width: calc(100vw - 250px - 180px);
              opacity: .4;
              pointer-events: none;
            }
          }
        }
        pre {
          overflow: auto;
          outline: none;
          margin: 0;
          code {
            line-height: 20px;
            font-size: 14px;
            font-family: 'Roboto Mono', monospace;
          }
        }
      }
    }
  }
}
</style>