Action

代码增加行号

Last update almost 6 years ago - Unlisted

Steps

  • script

    // Number the selected lines (or all lines) with colon and
       // two spaces after each line number.
       
       
       // Function for right-justifying text str in width n.
       function rjust(str, n) {
         var strLen = str.length;
         if (n > strLen) {
           var prefix = ' '.repeat(n - strLen)
          return prefix + str;
        } else {
          return str;
        }
      }
      
      // Get either the current selection or the entire draft.
      var sel = editor.getSelectedText();
      if (!sel || sel.length==0) {
        editor.setSelectedRange(0, editor.getText().length);
        sel = editor.getSelectedText();
        }
      
      // Break the text into lines and number them.
      // Right-justify the numbers and put a colon and
      // two spaces after the line number.
      var lines = sel.split('\n');
      var numLines = lines.length;
      var width = Math.floor(Math.log(numLines)*Math.LOG10E) + 1;
      var numbered = [];
      var lNum;
      for (var i=0; i<numLines; i++) {
        lNum = i + 1;
        numbered.push(rjust(lNum.toString(), width) + ':  ' + lines[i]);
      }
      
      // Replace the original text with the line-numbered text.
      editor.setSelectedText(numbered.join('\n'));

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.