官术网_书友最值得收藏!

Text stats

The last stats we can display are more "writer-oriented"--the lines, words, and characters count:

  1. Let's create three new computed properties for each counter, with some Regular Expressions to get the job done:
      computed: {
        linesCount () {
          if (this.selectedNote) {
            // Count the number of new line characters
            return this.selectedNote.content.split(/\r\n|\r|\n/).length
          }
        },

        wordsCount () {
          if (this.selectedNote) {
            var s = this.selectedNote.content
            // Turn new line cahracters into white-spaces
            s = s.replace(/\n/g, ' ')
            // Exclude start and end white-spaces
            s = s.replace(/(^\s*)|(\s*$)/gi, '')
            // Turn 2 or more duplicate white-spaces into 1
            s = s.replace(/\s\s+/gi, ' ')
            // Return the number of spaces
            return s.split(' ').length
          }
        },

        charactersCount () {
          if (this.selectedNote) {
            return this.selectedNote.content.split('').length
          }
        },
      }

Here, we added some conditions to prevent the code from running if no note is currently selected. This will avoid crashes if you use the Vue devtools to inspect the app in this case, because it will try to compute all the properties.

  1. You can now add three new stat span elements with the corresponding computed properties:
      <span class="lines">
        <span class="label">Lines</span>
        <span class="value">{{ linesCount }}</span>
      </span>
      <span class="words">
        <span class="label">Words</span>
        <span class="value">{{ wordsCount }}</span>
      </span>
      <span class="characters">
        <span class="label">Characters</span>
        <span class="value">{{ charactersCount }}</span>
      </span>

The final status bar should look like this:

主站蜘蛛池模板: 江华| 兴仁县| 绵阳市| 霍林郭勒市| 汕头市| 礼泉县| 白河县| 维西| 平果县| 永州市| 梁河县| 大足县| 如东县| 肥东县| 左云县| 昭觉县| 邓州市| 盐城市| 古蔺县| 延安市| 兰西县| 阿克苏市| 昭通市| 乐安县| 西华县| 乐至县| 静安区| 海南省| 东城区| 漠河县| 柳州市| 中西区| 赞皇县| 海伦市| 河南省| 永平县| 皮山县| 文安县| 宜丰县| 武山县| 体育|