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

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:

主站蜘蛛池模板: 连云港市| 宁津县| 清丰县| 金山区| 隆回县| 建瓯市| 济宁市| 华亭县| 广河县| 连平县| 濮阳县| 晋江市| 米脂县| 合川市| 滁州市| 山西省| 盐山县| 苍山县| 从化市| 阳春市| 县级市| 德保县| 都兰县| 澜沧| 娱乐| 九寨沟县| 伊金霍洛旗| 花莲市| 宝山区| 渝中区| 邹平县| 湟中县| 修水县| 霍州市| 甘洛县| 舟曲县| 宣化县| 徐水县| 荥阳市| 佛坪县| 慈溪市|