Skip to content

Instantly share code, notes, and snippets.

@MrFant
Last active May 18, 2018 02:30
Show Gist options
  • Save MrFant/af5a6d8ff54d817eacb08720d86c427b to your computer and use it in GitHub Desktop.
Save MrFant/af5a6d8ff54d817eacb08720d86c427b to your computer and use it in GitHub Desktop.
Use execCommands to edit HTML content in your browser
<div class="container">
<div class="jumbotron">
<div class="container">
<h2>execCommands supported in your browser</h2>
<div class="row">
<div class="col-md-6">The <small class="btn btn-success btn-xs">Green</small> commands are supported by your browser, where as the <small class="btn btn-error btn-xs">Grey</small> ones.. You guessed it! :-) <br />Anyway try to select contents below and click <small class="btn btn-success btn-xs">commands</small> to execute the commands on it. <br />I use the HTML attribute <code>contenteditable=true</code> to allow modifications to the content. <code>Updated</code>: Added <a href="https://fortawesome.github.io/Font-Awesome/icons/">font awesome icons</a>. <br /> <br />Inspired by <a href="https://css-tricks.com/cut-and-copy-from-javascript/">Cut and Copy (from JavaScript)</a></div>
<div class="col-md-6">
<code>2017-06-04 updated:</code><br>
<em>As pointed out by <a href="https://twitter.com/Robvanhe">@Robvanhe</a> the pen did not actually work in Safari. It was due to selection being cleared after click, so no selection to apply command existed. Fixed now. Rewritten also to vanilla javascript (no use for Angular).</em><br><br>
<code>2018-04-17 updated:</code><br>
<em>Forked by Chris Mills to create a version of this demo with clean content.</em>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 tags buttons">
</div>
</div>
<div class="row" contentEditable="true">
<div class="col-md-3">
<img src="https://mdn.mozillademos.org/files/15910/dino.gif" alt="abstract dinosaur head" class="img-responsive" /></div>
<div class="col-md-9">
<div class="row" contentEditable="true">
<div class="col-md-12">
<h3>Try to alter this text using the execCommands buttons above</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent semper in nunc at mattis. Suspendisse metus augue, pellentesque finibus luctus dictum, tempor id nunc. Phasellus semper magna ac interdum scelerisque. Vestibulum laoreet sapien a nisi ullamcorper, nec luctus neque eleifend. Aliquam vel tortor justo. Nam aliquam dapibus mi ut volutpat. Donec eros turpis, gravida et neque nec, interdum pulvinar nibh. Ut vel sodales nulla, a tempus sem. Nunc facilisis pretium aliquam. Proin nulla ante, congue at purus sit amet, commodo placerat erat. Sed malesuada eros nulla, in cursus urna elementum finibus. Suspendisse posuere purus pellentesque rutrum tempor. Nunc nec tincidunt est. Sed convallis ligula quis dui laoreet maximus. Nulla pharetra placerat quam ut rutrum. Nullam sit amet vehicula nisi.</p></div>
<div class="col-md-8"><p>Pellentesque feugiat posuere fermentum. In ut varius odio, nec venenatis lorem. Pellentesque interdum odio tortor, ac hendrerit mauris venenatis nec. Maecenas finibus dapibus eleifend. Nulla rhoncus metus non nisi rutrum tempor. Cras luctus lacinia eros sed volutpat. Duis accumsan quam libero, non accumsan nisl faucibus sit amet.</p> </div>
<div class="col-md-4"><p>Cras tincidunt magna malesuada maximus tristique. Sed a fringilla ipsum. Nam malesuada gravida commodo. In ut convallis neque. Nulla ut consequat nisi, a imperdiet ante.</p></div>
</div>
<div class="row" contentEditable="true">
<div class="col-md-12">Pellentesque feugiat posuere fermentum. In ut varius odio, nec venenatis lorem. Pellentesque interdum odio tortor, ac hendrerit mauris venenatis nec. Maecenas finibus dapibus eleifend. Nulla rhoncus metus non nisi rutrum tempor. Cras luctus lacinia eros sed volutpat. Duis accumsan quam libero, non accumsan nisl faucibus sit amet. </div>
</div>
</div>
</div>
</div>
</div>
var parms = [{
"cmd": "aCommandName",
"desc": "A DOMString representing the name of the command"
}, {
"cmd": "aShowDefaultUI",
"desc": "A Boolean indicating whether the default user interface should be shown. This is not implemented in Mozilla."
}, {
"cmd": "aValueArgument",
"desc": "A DOMString representing some commands (such as insertimage) require an extra value argument (the image's url). Pass an argument of null if no argument is needed."
}];
var commands = [{
"cmd": "backColor",
"val": "red",
"desc": "Changes the document background color. In styleWithCss mode, it affects the background color of the containing block instead. This requires a color value string to be passed in as a value argument. (Internet Explorer uses this to set text background color.)"
}, {
"cmd": "bold",
"icon": "bold",
"desc": "Toggles bold on/off for the selection or at the insertion point. (Internet Explorer uses the STRONG tag instead of B.)"
}, {
"cmd": "contentReadOnly",
"desc": "Makes the content document either read-only or editable. This requires a boolean true/false to be passed in as a value argument. (Not supported by Internet Explorer.)"
}, {
"cmd": "copy",
"icon": "clipboard",
"desc": "Copies the current selection to the clipboard. Clipboard capability must be enabled in the user.js preference file. See"
}, {
"cmd": "createLink",
"val": "https://twitter.com/netsi1964",
"icon": "link",
"desc": "Creates an anchor link from the selection, only if there is a selection. This requires the HREF URI string to be passed in as a value argument. The URI must contain at least a single character, which may be a white space. (Internet Explorer will create a link with a null URI value.)"
}, {
"cmd": "cut",
"icon": "scissors",
"desc": "Cuts the current selection and copies it to the clipboard. Clipboard capability must be enabled in the user.js preference file. See"
}, {
"cmd": "decreaseFontSize",
"desc": "Adds a SMALL tag around the selection or at the insertion point. (Not supported by Internet Explorer.)"
}, {
"cmd": "delete",
"icon": "scissors",
"desc": "Deletes the current selection."
}, {
"cmd": "enableInlineTableEditing",
"desc": "Enables or disables the table row and column insertion and deletion controls. (Not supported by Internet Explorer.)"
}, {
"cmd": "enableObjectResizing",
"desc": "Enables or disables the resize handles on images and other resizable objects. (Not supported by Internet Explorer.)"
}, {
"cmd": "fontName",
"val": "'Inconsolata', monospace",
"desc": "Changes the font name for the selection or at the insertion point. This requires a font name string (\"Arial\" for example) to be passed in as a value argument."
}, {
"cmd": "fontSize",
"val": "1-7",
"icon": "text-height",
"desc": "Changes the font size for the selection or at the insertion point. This requires an HTML font size (1-7) to be passed in as a value argument."
}, {
"cmd": "foreColor",
"val": "rgba(0,0,0,.5)",
"desc": "Changes a font color for the selection or at the insertion point. This requires a color value string to be passed in as a value argument."
}, {
"cmd": "formatBlock",
"val": "<blockquote>",
"desc": "Adds an HTML block-style tag around the line containing the current selection, replacing the block element containing the line if one exists (in Firefox, BLOCKQUOTE is the exception - it will wrap any containing block element). Requires a tag-name string to be passed in as a value argument. Virtually all block style tags can be used (eg. \"H1\", \"P\", \"DL\", \"BLOCKQUOTE\"). (Internet Explorer supports only heading tags H1 - H6, ADDRESS, and PRE, which must also include the tag delimiters &lt; &gt;, such as \"&lt;H1&gt;\".)"
}, {
"cmd": "forwardDelete",
"desc": "Deletes the character ahead of the cursor's position.  It is the same as hitting the delete key."
}, {
"cmd": "heading",
"val": "h3",
"icon": "header",
"desc": "Adds a heading tag around a selection or insertion point line. Requires the tag-name string to be passed in as a value argument (i.e. \"H1\", \"H6\"). (Not supported by Internet Explorer and Safari.)"
}, {
"cmd": "hiliteColor",
"val": "Orange",
"desc": "Changes the background color for the selection or at the insertion point. Requires a color value string to be passed in as a value argument. UseCSS must be turned on for this to function. (Not supported by Internet Explorer.)"
}, {
"cmd": "increaseFontSize",
"desc": "Adds a BIG tag around the selection or at the insertion point. (Not supported by Internet Explorer.)"
}, {
"cmd": "indent",
"icon": "indent",
"desc": "Indents the line containing the selection or insertion point. In Firefox, if the selection spans multiple lines at different levels of indentation, only the least indented lines in the selection will be indented."
}, {
"cmd": "insertBrOnReturn",
"desc": "Controls whether the Enter key inserts a br tag or splits the current block element into two. (Not supported by Internet Explorer.)"
}, {
"cmd": "insertHorizontalRule",
"desc": "Inserts a horizontal rule at the insertion point (deletes selection)."
}, {
"cmd": "insertHTML",
"val": "&lt;h3&gt;Life is great!&lt;/h3&gt;",
"icon": "code",
"desc": "Inserts an HTML string at the insertion point (deletes selection). Requires a valid HTML string to be passed in as a value argument. (Not supported by Internet Explorer.)"
}, {
"cmd": "insertImage",
"val": "http://dummyimage.com/160x90",
"icon": "picture-o",
"desc": "Inserts an image at the insertion point (deletes selection). Requires the image SRC URI string to be passed in as a value argument. The URI must contain at least a single character, which may be a white space. (Internet Explorer will create a link with a null URI value.)"
}, {
"cmd": "insertOrderedList",
"icon": "list-ol",
"desc": "Creates a numbered ordered list for the selection or at the insertion point."
}, {
"cmd": "insertUnorderedList",
"icon": "list-ul",
"desc": "Creates a bulleted unordered list for the selection or at the insertion point."
}, {
"cmd": "insertParagraph",
"icon": "paragraph",
"desc": "Inserts a paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.)"
}, {
"cmd": "insertText",
"val": new Date(),
"icon": "file-text-o",
"desc": "Inserts the given plain text at the insertion point (deletes selection)."
}, {
"cmd": "italic",
"icon": "italic",
"desc": "Toggles italics on/off for the selection or at the insertion point. (Internet Explorer uses the EM tag instead of I.)"
}, {
"cmd": "justifyCenter",
"icon": "align-center",
"desc": "Centers the selection or insertion point."
}, {
"cmd": "justifyFull",
"icon": "align-justify",
"desc": "Justifies the selection or insertion point."
}, {
"cmd": "justifyLeft",
"icon": "align-left",
"desc": "Justifies the selection or insertion point to the left."
}, {
"cmd": "justifyRight",
"icon": "align-right",
"desc": "Right-justifies the selection or the insertion point."
}, {
"cmd": "outdent",
"icon": "outdent",
"desc": "Outdents the line containing the selection or insertion point."
}, {
"cmd": "paste",
"icon": "clipboard",
"desc": "Pastes the clipboard contents at the insertion point (replaces current selection). Clipboard capability must be enabled in the user.js preference file. See"
}, {
"cmd": "redo",
"icon": "repeat",
"desc": "Redoes the previous undo command."
}, {
"cmd": "removeFormat",
"desc": "Removes all formatting from the current selection."
}, {
"cmd": "selectAll",
"desc": "Selects all of the content of the editable region."
}, {
"cmd": "strikeThrough",
"icon": "strikethrough",
"desc": "Toggles strikethrough on/off for the selection or at the insertion point."
}, {
"cmd": "subscript",
"icon": "subscript",
"desc": "Toggles subscript on/off for the selection or at the insertion point."
}, {
"cmd": "superscript",
"icon": "superscript",
"desc": "Toggles superscript on/off for the selection or at the insertion point."
}, {
"cmd": "underline",
"icon": "underline",
"desc": "Toggles underline on/off for the selection or at the insertion point."
}, {
"cmd": "undo",
"icon": "undo",
"desc": "Undoes the last executed command."
}, {
"cmd": "unlink",
"icon": "chain-broken",
"desc": "Removes the anchor tag from a selected anchor link."
}, {
"cmd": "useCSS ",
"desc": "Toggles the use of HTML tags or CSS for the generated markup. Requires a boolean true/false as a value argument. NOTE: This argument is logically backwards (i.e. use false to use CSS, true to use HTML). (Not supported by Internet Explorer.) This has been deprecated; use the styleWithCSS command instead."
}, {
"cmd": "styleWithCSS",
"desc": "Replaces the useCSS command; argument works as expected, i.e. true modifies/generates style attributes in markup, false generates formatting elements."
}];
var commandRelation = {};
function supported(cmd) {
var css = !!document.queryCommandSupported(cmd.cmd) ? "btn-succes" : "btn-error"
return css
};
function icon(cmd) {
return (typeof cmd.icon !== "undefined") ? "fa fa-" + cmd.icon : "";
};
function doCommand(cmdKey) {
var cmd = commandRelation[cmdKey];
if (supported(cmd) === "btn-error") {
alert("execCommand(“" + cmd.cmd + "”)\nis not supported in your browser");
return;
}
val = (typeof cmd.val !== "undefined") ? prompt("Value for " + cmd.cmd + "?", cmd.val) : "";
document.execCommand(cmd.cmd, false, (val || "")); // Thanks to https://codepen.io/bluestreak for finding this bug
}
function init() {
var html = '',
template = '<span><code class="btn btn-xs %btnClass%" title="%desc%" onmousedown="event.preventDefault();" onclick="doCommand(\'%cmd%\')"><i class="%iconClass%"></i> %cmd%</code></span>';
commands.map(function(command, i) {
commandRelation[command.cmd] = command;
var temp = template;
temp = temp.replace(/%iconClass%/gi, icon(command));
temp = temp.replace(/%desc%/gi, command.desc);
temp = temp.replace(/%btnClass%/gi, supported(command));
temp = temp.replace(/%cmd%/gi, command.cmd);
html+=temp;
});
document.querySelector(".buttons").innerHTML = html;
}
init();
@import url(https://fonts.googleapis.com/css?family=Inconsolata:400,700|Open+Sans:400,800);
body {
font-family: 'Open Sans', sans-serif;
}
.jumbotron {
padding: 0 0 20px;
font-family: 'Inconsolata', monospace ;
}
.btn-succes {
background-color: green;
color: yellow;
}
.btn-error {
background-color: grey;
color: black;
}
.btn-succes, .btn-succes {
margin: 5px;
}
i {
color:white;
}
.btn-error i {
color: black;
}
.btn {
-webkit-user-select: none; /* webkit (safari, chrome) browsers */
-moz-user-select: none; /* mozilla browsers */
-khtml-user-select: none; /* webkit (konqueror) browsers */
-ms-user-select: none; /* IE10+ */
}
:focus {
outline: none;
}
img {
margin-top: 30px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

Use execCommands to edit HTML content in your browser

This is inspired by a e-mail news update from CSS-Tricks "Cut and Copy (from Javascript" by Chris Coyier.

A Pen by Fant on CodePen.

License.

@MrFant
Copy link
Author

MrFant commented May 18, 2018

Use execCommands to edit HTML content in your browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment