Skip to content
arzeth edited this page Jun 14, 2016 · 28 revisions

Supported Commands

Below is a full list of formatting commands supported by wysihtml.

addTableCells

When a table cell is selected then adds a row or column to table

  • Whether table cells selection is active can be tracked with following events:
    editorInstance.on("tableselect", function() {});
    editorInstance.on("tableunselect", function() {});
  • Toolbar elements:
    <!-- adds a row above selected cell -->
    <a data-wysihtml-command="addTableCells" data-wysihtml-command-value="above">row-before</a>
    
    <!-- adds a row below selected cell -->
    <a data-wysihtml-command="addTableCells" data-wysihtml-command-value="below">row-after</a>
    
    <!-- adds a cloumn before selected cell -->
    <a data-wysihtml-command="addTableCells" data-wysihtml-command-value="before">col-before</a>
    
    <!-- adds a cloumn after selected cell -->
    <a data-wysihtml-command="addTableCells" data-wysihtml-command-value="after">col-after</a>
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("addTableCells", "above");
    editorInstance.composer.commands.exec("addTableCells", "below");
    editorInstance.composer.commands.exec("addTableCells", "before");
    editorInstance.composer.commands.exec("addTableCells", "after");

bgColorStyle

Sets/unsets text background color with style attribute. Calling allready set background value on element removes styling.

  • Generated markup:
    <span style="background-color:rgb(255,0,0)">text</span>
  • Toolbar element
    <a data-wysihtml-command="bgColorStyle">BG-Color</a>
    <div data-wysihtml-dialog="bgColorStyle" style="display: none;">
     Color:
     <input type="text" data-wysihtml-dialog-field="color" value="rgba(0,0,0,1)" />
     <a data-wysihtml-dialog-action="save">OK</a>&nbsp;<a data-wysihtml-dialog-action="cancel">Cancel</a>
    </div>
  • Required parser rules:
    var wysihtmlParserRules = {
      "type_definitions": {
          "text_bg_color_object": {
            "styles": {
              "background-color": true
            }
          },
      },
      
      tags: {
        "span": {
            "one_of_type": {
                "text_bg_color_object": 1
            },
            "keep_styles": {
                "backgroundColor": 1
            },
            "remove_action": "unwrap"
        }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("bgColorStyle", "#ABC");
    editorInstance.composer.commands.exec("bgColorStyle", "#ABCDEF");
    editorInstance.composer.commands.exec("bgColorStyle", "rgb(255,0,0)");
    editorInstance.composer.commands.exec("bgColorStyle", "rgba(255,0,0,0.5)");

bold

  • Generated markup:
    <b>text</b>
  • Toolbar element:
    <a data-wysihtml-command="bold">bold</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "b": 1,
        // if you want to turn all <strong> elements into <b> (optional)
       "strong": { "rename_tag": "b" }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("bold");
  • Source code

createLink

  • Generated markup:
    <!-- target="_blank" and rel="nofollow" are only set when defined in parser rules -->
    <a href="http://url" target="_blank" rel="nofollow">link</a>
  • Toolbar element:
    <!-- User can define the link's href: -->
    <a data-wysihtml-command="createLink">insert link</a>
    <div data-wysihtml-dialog="createLink">
      <label>
        Link:
        <input data-wysihtml-dialog-field="href" value="http://">
      </label>
      <a data-wysihtml-dialog-action="save">OK</a>
      <a data-wysihtml-dialog-action="cancel">Cancel</a>
    </div>
    <!-- Pre-defined href -->
    <a data-wysihtml-command="createLink" data-wysihtml-command-value="http://www.google.com">insert google link</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "a": {
          "check_attributes": {
            "href": "url" // make sure the entered url is really an url
          },
          "set_attributes": {
            "rel": "nofollow",   // optional
            "target": "_blank"   // optional
          }
        }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("createLink", { href: "http://google.com", target: "_blank", rel: "nofollow", text: "Google" });
  • Source code

removeLink

  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("removeLink");

createTable

  • Generated markup:
    <table>
      <tbody>
        <tr>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
  • Toolbar element:
    <a data-wysihtml-command="createTable">Table</a>
    <div data-wysihtml-dialog="createTable" style="display: none;">
      Rows: <input type="text" data-wysihtml-dialog-field="rows" /><br/>
      Cols: <input type="text" data-wysihtml-dialog-field="cols" /><br/>
      <a data-wysihtml-dialog-action="save">OK</a>&nbsp;<a data-wysihtml-dialog-action="cancel">Cancel</a>
    </div>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "table": {},
        "td": {
            "check_attributes": {
                "rowspan": "numbers",
                "colspan": "numbers"
            }
        },
        "tr": {},
        "tbody": {}
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("createTable", { rows: 5, cols: 3 });

deleteTableCells

When a table cell is selected then removes a row or column of table

  • Whether table cells selection is active can be tracked with following events:
    editorInstance.on("tableselect", function() {});
    editorInstance.on("tableunselect", function() {});
  • Toolbar elements:
    <!-- deletes the row of table containing selected cell -->
    <a data-wysihtml-command="deleteTableCells" data-wysihtml-command-value="row">delete row</a>
    
    <!-- deletes the column of table  containing selected cell -->
    <a data-wysihtml-command="deleteTableCells" data-wysihtml-command-value="column">delete column</a>
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("deleteTableCells", "row");
    editorInstance.composer.commands.exec("deleteTableCells", "column");

fontSize

  • Generated markup:
    <!-- XS -->
    <span class="wysiwyg-font-size-x-small">text</span>
    <!-- S -->
    <span class="wysiwyg-font-size-small">text</span>
    <!-- M -->
    <span class="wysiwyg-font-size-medium">text</span>
    <!-- L -->
    <span class="wysiwyg-font-size-large">text</span>
    <!-- XL -->
    <span class="wysiwyg-font-size-x-large">text</span>
    <!-- ... and many more font sizes possible --->
  • Toolbar elements:
    <a data-wysihtml-command="fontSize" data-wysihtml-command-value="small">small</a>
    <a data-wysihtml-command="fontSize" data-wysihtml-command-value="large">large</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      classes: {
        "wysiwyg-font-size-large": 1,
        "wysiwyg-font-size-small": 1
      },
      tags: {
        "span": 1
      }
    };
  • Required CSS:
    .wysiwyg-font-size-large {
      font-size: large;
    }
    .wysiwyg-font-size-small {
      font-size: small;
    }
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("fontSize", "large");
  • Source code

fontSizeStyle

Sets/unsets text font size with style attribute. Calling allready set size value on element removes styling.

  • Generated markup:
    <span style="font-size:12px">text</span>
  • Toolbar element
    <a data-wysihtml-command="fontSizeStyle">Size</a>
    <div data-wysihtml-dialog="fontSizeStyle" style="display: none;">
      Size:
      <input type="text" data-wysihtml-dialog-field="size" style="width: 60px;" value="" />
      <a data-wysihtml-dialog-action="save">OK</a>&nbsp;<a data-wysihtml-dialog-action="cancel">Cancel</a>
    </div>
  • Required parser rules:
    var wysihtmlParserRules = {
      "type_definitions": {
          "text_fontsize_object": {
              "styles": {
                "font-size": true
              }
          },
      },
      
      tags: {
        "span": {
            "one_of_type": {
                "text_fontsize_object": 1
            },
            "keep_styles": {
                "fontSize": 1
            },
            "remove_action": "unwrap"
        }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("fontSizeStyle", "12px");
    editorInstance.composer.commands.exec("fontSizeStyle", "2em");
    
    // getting active style
    editorInstance.composer.commands.statusValue("fontSizeStyle");

foreColor

  • Generated markup:
    <!-- Red -->
    <span class="wysiwyg-font-color-red">text</span>
    <!-- Green -->
    <span class="wysiwyg-font-color-green">text</span>
    <!-- Blue -->
    <span class="wysiwyg-font-color-blue">text</span>
    <!-- ... and many more colors possible -->
  • Toolbar elements:
    <a data-wysihtml-command="foreColor" data-wysihtml-command-value="red">red</a>
    <a data-wysihtml-command="foreColor" data-wysihtml-command-value="green">green</a>
    <a data-wysihtml-command="foreColor" data-wysihtml-command-value="blue">blue</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      classes: {
        "wysiwyg-color-blue": 1,
        "wysiwyg-color-green": 1,
        "wysiwyg-color-red": 1
      },
      tags: {
        "span": 1
      }
    };
  • Required CSS:
    .wysiwyg-color-red {
      color: red;
    }
    .wysiwyg-color-green {
      color: green;
    }
    .wysiwyg-color-blue {
      color: blue;
    }
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("foreColor", "blue");
  • Source code

foreColorStyle

Sets/unsets text color with style attribute. Calling allready set color value on element removes styling.

  • Generated markup:
    <span style="color:rgb(255,0,0)">text</span>
  • Toolbar element
    <div class="block">
      <a data-wysihtml-command="foreColorStyle">Color</a>
      <div data-wysihtml-dialog="foreColorStyle" style="display: none;">
        Color:
        <input type="text" data-wysihtml-dialog-field="color" value="rgba(0,0,0,1)" />
        <a data-wysihtml-dialog-action="save">OK</a>&nbsp;<a data-wysihtml-dialog-action="cancel">Cancel</a>
      </div>
    </div>
  • Required parser rules:
    var wysihtmlParserRules = {
      "type_definitions": {
          "text_color_object": {
            "styles": {
              "color": true
            }
          },
      },
      
      tags: {
        "span": {
            "one_of_type": {
                "text_color_object": 1
            },
            "keep_styles": {
                "color": 1
            },
            "remove_action": "unwrap"
        }
      }
    };
  • Trigger via JavaScript:
    // any of the following will work
    editorInstance.composer.commands.exec("foreColorStyle", "#ABC");
    editorInstance.composer.commands.exec("foreColorStyle", "#ABCDEF");
    editorInstance.composer.commands.exec("foreColorStyle", "rgb(255,0,0)");
    editorInstance.composer.commands.exec("foreColorStyle", "rgba(255,0,0,0.5)");

formatBlock

Please note: formatBlock can be used to format the current selection into any block element (h1, h2, p, blockquote, …). The following is an example using the H1 tag.

  • Generated markup:
    <h1>text</h1>
  • Toolbar element:
    <a data-wysihtml-command="formatBlock" data-wysihtml-command-value="h1">heading 1</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "h1": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("formatBlock", "h1");
  • Additional options in Javascript:
    Formatblock command takes two additional parameters for adding elements with classes. Class regex is needed for detecting classes active/inactive status and can be adjusted to detect multiple classes.
    Example case in code – Justify left.
    // editorInstance.composer.commands.exec("formatBlock", tagName, className, classRegex);
    editorInstance.composer.commands.exec("formatBlock", null, "my-class", /my-class/g);
  • Source code

formatInline

Please note: formatInline can be used to format the current selection into any inline element (span, strong, time, …). The following is an example using the SPAN tag.

  • Generated markup:
    <span>text</span>
  • Toolbar element:
    <a data-wysihtml-command="formatInline" data-wysihtml-command-value="span">span</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "span": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("formatInline", "span");
  • Source code

insertHTML

  • Toolbar element:
    <a data-wysihtml-command="insertHTML" data-wysihtml-command-value="<blockquote>foobar</blockquote>">insert quote</a>
  • Required parser rules:
    depends on the HTML that can be inserted
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("insertHTML", "<blockquote>foobar</blockquote>");
  • Source code

insertImage

  • Generated markup:
    <img src="http://url.com/foo.jpg" width="100" height="100">
  • Toolbar element:
    <!-- User can define the image's src: -->
    <a data-wysihtml-command="insertImage">insert image</a>
    <div data-wysihtml-dialog="insertImage">
      <label>
        Image:
        <input data-wysihtml-dialog-field="src" value="http://">
      </label>
      <a data-wysihtml-dialog-action="save">OK</a>
      <a data-wysihtml-dialog-action="cancel">Cancel</a>
    </div>
    <!-- Pre-defined src -->
    <a data-wysihtml-command="insertImage" data-wysihtml-command-value="http://url.com/foo.jpg">insert example image</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "img": {
            "check_attributes": {
                "width": "numbers",
                "alt": "alt",
                "src": "url",
                "height": "numbers"
            }
        }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("insertImage", { src: "http://url.com/foo.jpg", alt: "this is an image" });
  • Source code

insertLineBreak

  • Generated markup:
    <br>
  • Toolbar element:
    <a data-wysihtml-command="insertLineBreak">line break</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "br": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("insertLineBreak");
  • Source code

insertOrderedList

  • Generated markup:
    <ol><li>text</li></ol>
  • Toolbar element:
    <a data-wysihtml-command="insertOrderedList">insert ordered list</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "ol": 1,
        "li": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("insertOrderedList");
  • Source code

insertUnorderedList

  • Generated markup:
    <ul><li>text</li></ul>
  • Toolbar element:
    <a data-wysihtml-command="insertUnorderedList">insert unordered list</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "ul": 1,
        "li": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("insertUnorderedList");
  • Source code

italic

  • Generated markup:
    <i>text</i>
  • Toolbar element:
    <a data-wysihtml-command="italic">italic</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "i": 1,
        // if you want to turn all <em> elements into <i> (optional)
        "em": { "rename_tag": "i" }
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("italic");
  • Source code

justifyCenter

  • Generated markup:
    <div class="wysiwyg-text-align-center">text</div>
  • Toolbar element:
    <a data-wysihtml-command="justifyCenter">align center</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      classes: {
        "wysiwyg-text-align-center": 1
      },
      tags: {
        "div": 1
      }
    };
  • Required CSS:
    .wysiwyg-text-align-center {
      text-align: center;
    }
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("justifyCenter");
  • Source code

justifyLeft

  • Generated markup:
    <div class="wysiwyg-text-align-left">text</div>
  • Toolbar element:
    <a data-wysihtml-command="justifyLeft">align left</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      classes: {
        "wysiwyg-text-align-left": 1
      },
      tags: {
        "div": 1
      }
    };
  • Required CSS:
    .wysiwyg-text-align-left {
      text-align: left;
    }
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("justifyLeft");
  • Source code

justifyRight

  • Generated markup:
    <div class="wysiwyg-text-align-right">text</div>
  • Toolbar element:
    <a data-wysihtml-command="justifyRight">align right</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      classes: {
        "wysiwyg-text-align-right": 1
      },
      tags: {
        "div": 1
      }
    };
  • Required CSS:
    .wysiwyg-text-align-right {
      text-align: right;
    }
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("justifyRight");
  • Source code

mergeTableCells

When table cells are selected then merges selected cells to one.
If action is executed on allredy merged cell, cell is unmerged

  • Whether table cells selection is active can be tracked with following events:
    editorInstance.on("tableselect", function() {});
    editorInstance.on("tableunselect", function() {});
  • Toolbar element:
    <a data-wysihtml-command="mergeTableCells">Merge</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "table": {},
        "td": {
            "check_attributes": {
                "rowspan": "numbers",
                "colspan": "numbers"
            }
        },
        "tr": {},
        "tbody": {}
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("mergeTableCells");
    
    // cell selection status is merged can be tracked:
    editorInstance.on("interaction", function() {
      if (composer.commands.state('mergeTableCells')) {
        // selection on merged cell
      }
    });
  • Source code

redo

Calls redo

  • Toolbar element:
    <a data-wysihtml-command="redo">redo</a>
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("redo");
  • Source code

underline

  • Generated markup:
    <u>text</u>
  • Toolbar element:
    <a data-wysihtml-command="underline">underline</a>
  • Required parser rules:
    var wysihtmlParserRules = {
      tags: {
        "u": 1
      }
    };
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("underline");
  • Source code

undo

Calls undo

  • Toolbar element:
    <a data-wysihtml-command="undo">undo</a>
  • Trigger via JavaScript:
    editorInstance.composer.commands.exec("undo");
  • Source code