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

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:

主站蜘蛛池模板: 嘉义市| 南投县| 津市市| 沭阳县| 孟州市| 礼泉县| 盐城市| 慈利县| 慈利县| 罗江县| 长阳| 介休市| 怀远县| 长丰县| 治多县| 平定县| 时尚| 嘉定区| 乌拉特前旗| 安乡县| 磴口县| 五家渠市| 新营市| 河源市| 梨树县| 格尔木市| 普格县| 海伦市| 抚顺市| 阿巴嘎旗| 大港区| 彭山县| 哈尔滨市| 阿坝县| 永清县| 监利县| 孟村| 江都市| 绥宁县| 会泽县| 斗六市|