6 Replies Latest reply on Sep 22, 2011 8:56 AM by Zoffix222

    Applying paragraph style to a single paragraph affects all previous paragraphs

    Zoffix222 Level 1

      Hey,

       

      I'm trying to create a script that would take an array of text objects that describe what the text and style of a particular line of text is. And in some lines to apply Character Styles as well.

       

      The problem is that, while the script works when the loop is in the character styles "mode", it seems to apply the paragraph styles to all the previous lines of text when the aforementioned object asks it to apply only the paragraph style (no character styles).

       

      The offending line is this:

       

      if ( typeof line.text == 'string' ) {
          frame.contents += "\r" + line.text;
          frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];
          frame.parentStory.paragraphs[-1].applyParagraphStyle(style);
          continue;
      }
      

       

      I even copied the style-apllying lines from the Character-Styles-applying section of the script (the else part), but it still applies the style to all the previous lines of text. The script below is the full code I have I long with one of the objects that text data. If I comment out the "offending lines" mentioned above, the script applyes all the styles correctly. Otherwise, only the last line (the Product - Description style) is applied, while all the previous lines attain the 'Spacer' style.

       

      Can anyone see what I'm doing wrong? (sorry for the giant piece of code, but I can't pin-point what's not relevant here).

       

       

      [{
          'text': [
              {
                  'style': 'Product - Title No Indent',
                  'text': [
                      'Test Product',
                      {
                          'style': 'Superscript',
                          'text': '®'
                      },
                      ' Title'
                  ]
              },
      
              { 'style': 'Spacer', 'text': ' x' },
              { 'style': 'Spacer', 'text': ' x' },
              { 'style': 'Spacer', 'text': ' x' },
              {
                  'style': 'Product - Description',
                  'text': ['Designed for monitoring steam sterilization processes.']
              },
          ]
      }];
      
      function add_text_to_frame (frame, product) {
          var i, l, j, k, line, style, line_styles, char_style, line_text, bit;
      
          for ( i = 0, l = product.text.length; i < l; i++ ) {
              line = product.text[i];
              style = doc.paragraphStyles.item( line.style );
              if ( typeof line.text == 'string' ) {
                  frame.contents += "\r" + line.text;
                  frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];
                  frame.parentStory.paragraphs[-1].applyParagraphStyle(style);
                  continue;
              }
              else {
                  line_styles = new Array();
                  line_text = '';
                  for ( j = 0, k = line.text.length; j < k; j++ ) {
                      bit = line.text[j];
                      if ( typeof bit == 'string' ) {
                          line_text += bit;
                      }
                      else {
                          line_styles.push({
                              'start': line_text.length,
                              'end': line_text.length + bit.text.length,
                              'style': bit.style
                          });
                          line_text += bit.text;
                      }
                  }
      
                  frame.contents += "\r" + line_text;
                  frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];
                  frame.parentStory.paragraphs[-1].applyParagraphStyle(style);
      
                  for ( j = 0, k = line_styles.length; j < k; j++ ) {
                      char_style = doc.characterStyles.item(line_styles[j].style);
                      frame.parentStory.paragraphs[-1].characters.itemByRange(
                          line_styles[j].start,
                          line_styles[j].end
                      ).applyCharacterStyle(char_style, true);
                  }
              }
          }
      }