Getting started | Default settings | List of Wysiwyg4All commands | License
Wysiwyg4All is a free opensource minimal WYSIWYG editor for your website. It is highly expandable and customizable.
You can easily build your own full fledged WYSIWYG application with your own css design with this library.
These following steps show basic demonstration of how to install Wysiwyg4All.
-
Inside HTML <head> add below:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="https://cdn.jsdelivr.net/npm/wysiwyg4all@latest/dist/wysiwyg4all.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/wysiwyg4all@latest/wysiwyg4all.css" />If you are working on webpack based projects:
Then, import the library from your project main file:
import { Wysiwyg4All } from 'wysiwyg4all'; import 'wysiwyg4all/css'; -
Inside <body>, create <div> with an id ('myeditor' ,for example) and give custom css style. Buttons can be created to actuate wysiwyg functionality. We added a button which will change text size to 'h1' at the 'onclick' event using wysiwyg.command('h1') function. Diverse command options can be selected, which are listed in below List of Wysiwyg4All commands. Of course, the button design can be customized by your own taste.
Example 1
<div id="myeditor" style="width: 512px; padding:1rem; border: solid 1px teal"></div> <button onclick="wysiwyg.command('h1')"> H1 </button>
In Wysiwyg4All function, you shuold set default properties for element id, placeholder string, spell check, highlight color, last line blank, hash-tag and URL link, log mutation. Add Wysiwyg4All default setting script inside your <script>. The <script> tag should come after closing the </body> tag. Following script is an example for setting some of the default properties. Wysiwyg4All function will be created under the name of wysiwyg in the entire examples.
Example 2
Callback function is used to log properties of command tracker, images, hashtags, URL links, caret position and log mutation. Include callback function inside your <script>.
Following code example shows default setting of .commandTracker, which shows current status of the text style in the console log.
Example 3
Image style can be pre-processed in .image. Following code example shows setting default width size of image by 8rem with border of red color, border width of 2px and triggering 'image clicked' message pop-up alert on the 'onclick' event.
Example 4
Default hashtag properties can be modified in .hashtag . Following code example shows setting default color of hashtag as red and giving message pop-up alert, whenever clicking hashtag string.
Example 5
Default URL link properties can be modified in .urllink. Following code example shows setting default color of URL link as red and displaying message pop-up alert, whenever clicking URL string.
Example 6
Default caret position properties can be modified in .caratPosition . Copy and paste the following code example. Specific details can be referred in API manual.
Example 7
Default log mutation properties can be modified in .mutation . Copy and paste the following code example. Specific details can be referred in API manual.
Example 8
HTML string or node element can be assigned in wysiwyg.command() element value. In the following example code, smile emoji (😀) is loaded in the custom element that would be added inline, whenever customElement() function is called such as by using customElement command button. Following code should be included in <script>.
Example 9
wysiwyg.export() should be included in <script>. It exports brief summary of Document Object Model(DOM) including HTML element.
Example 10
The Wysiwyg can edit text styles and text input field in diverse manners by using wysiwyg.command() function. Buttons can be created inside your <body> as shown in the Getting started example, and to activate commands on 'onclick' event. The list of the command inputs is shown in the following.
wysiwyg.command('bold' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'small' | 'italic' | 'underline' | 'strike') changes the text style by bold, heading level (h1 ~ h6), small letter, italic, underline or strike.
Example 11
When executing wysiwyg commands, the text selection in wysiwyg should be present.
For some browsers, wysiwyg text selection can be disabled the moment the user clicks on the command button. To prevent this, it might be a good idea to prevent default on command buttons:
wysiwyg.command('color') changes the text color ('black') to wysiwyg default highlight color ('teal' in this example).
Example 12
Other color choice can be provided to user by creating HTML color picker. It is important to restore the last selected text on 'onblur' action (whenever losing focus in the input field) by using wysiwyg.restoreLastSelection() to change the text color in color picker.
Example 13
wysiwyg.command('divider')adds horizontal line below the current text position.
Example 14
wysiwyg.command('quote')adds block quote on the selected line. Note that the default highlight color is applied on the block quote.
Example 15
wysiwyg.command('unorderedList') adds unordered list andwysiwyg.command('orderedList')adds ordered list on the selected line. Following code shows creating command button to add unorderedList.
Example 16
wysiwyg.command('alignLeft') , wysiwyg.command('alignCenter') or wysiwyg.command('alignRight')aligns selected text to the left, center or to the right. Following code shows creating command button for aligning text to the center of the text area. Clicking the command button again restore to the initial alignment.
Example 17
wysiwyg.command('image') adds image below selected line. By clicking the 'image' command button, directory panel pops up and opening the image will make insertion into the text input field.
Example 18
customElement() adds pre-loaded HTML string or node elements inside a line. Smile emoji will be inserted whenever 'Smile' button is clicked in the following example as it was pre-loaded in the default setting custom element example.
Example 19
This project is licensed under the terms of the MIT license.