Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
		<div id="editor" contenteditable="true" data-placeholder="Click here and type a word"></div>
</div>
              
            
!

CSS

              
                body {
	background-color: #131313;
}

#editor {
	width: 600px!important;
	height: 200px!important;
		color: #333;
	background-color: #fff;
  position: relative;
	padding: 10px 0 0 10px;
	font-family: Arial;
  font-size: 20px;
  outline: none;
  transition: all 0.4s ease;
  -webkit-transition: all -webkit-transform 0.4s ease;
  text-overflow: ellipsis;
}

[contenteditable=true]:empty:before{
  content: attr(placeholder);
  color: grey;
  display: block;
}

@-moz-document url-prefix() {
  #autoComplete {
    border-width: 1px;
    background-size: 1.2rem;
    background-origin: border-box;
    background-position: center;
  }
}
              
            
!

JS

              
                // THIS IS A WIP/PoC.

// Instructions: just start typing one of the `randomWords` and after the threshold of 2 characters (e.g after you type
// the `r` in `agreement`), the autocomplete placeholder will appear. Then just click the placeholder to complete the
// text (this behaviour will be controlled by Tab/Enter in future, but can't be implemented right now as it involves
// modifying the autoComplete.js library.)

// The placeholder doesn't work when formatting is active (e.g press ctrl+b and then try predictive text - it won't
// work). If you can solve this, fork the pen and contact me for a bounty 😊

const randomWords = [ 'elephant', 'rhinoceros', 'australia', 'reform', 'treaty', 'agreement', 'european union',
                      'europe', 'brussels', ' recast', 'domicile', 'america', 'compensation', 'liability',
                      'constitution', 'parliament' ];

// Thanks to https://tarekraafat.github.io/autoComplete.js/#/ for providing the core autocomplete engine, which has
// been modified to suit the needs of a rich text editor.
const autoCompletejs = new autoComplete( {
    data: {
        src: randomWords,
        cache: false
    },
    selector: '#editor',
    threshold: 2,
    maxResults: 1,
    resultsList: {
        render: true,
        container: source => {
            source.setAttribute( 'id', 'suggestion__container' );
            source.style.cssText = `display: inline-flex; font-family: Arial; font-size: 20px;
                    position: absolute;overflow: hidden;opacity: 0.5;z-index: 99999; color: grey;
                    overflow: hidden;transition: all 0.1s ease-in-out;
                    -webkit-transition: all -webkit-transform 0.1s ease;`;
        },
        destination: document.querySelector( '#editor' ),
        element: 'div'
    },
    resultItem: {
        content: (data, source) => {
            console.log( data );
            console.log( source );
            source.innerHTML = data.match;
        },
        element: 'span'
    },
    onSelection: feedback => {
        const editor = document.querySelector( '#editor' );

        const incompleteText = document.getSelection().anchorNode;
        const range = document.createRange();
        range.selectNodeContents( incompleteText );

        const _incompleteText = incompleteText.textContent;
        let autocompleteText = feedback.results[ 0 ];

        autocompleteText = autocompleteText.replace( _incompleteText, '' );

        insertTextAtCursor( autocompleteText );
        // Call `normalize` on the parent node after inserting the autocomplete text, otherwise 2 separate
        // text nodes will be rendered as the child nodes. These separate nodes may appear visually to be
        // a single word, but if they are not merged it can produce major formatting issues in future.
        editor.normalize();
    }
} );

// Hide Results list when not used
document.addEventListener( 'keydown', e => {
    const resultsList = document.querySelector( '#suggestion__container' );

    if ( e.key && e.key !== 'Backspace' ) {
        const editor = document.querySelector( '#editor' );

        // Don't activate predictive text if the caret if the caret has text ahead of it. Things will just get messy.
        if ( isCaretAtEnd( editor ) ) {
            resultsList.style.display = 'inline';
            console.log( resultsList );
            const incompleteText = document.getSelection().anchorNode;
            console.log( incompleteText );
            if ( incompleteText ) {
                const range = document.createRange();
                range.selectNodeContents( incompleteText );
                let coordinates = range.getClientRects();
                if ( coordinates.length > 0 ) {
                    coordinates = coordinates[ 0 ];
                    const { top, left } = coordinates;
                    // We create the visual effect of a placeholder element by overlaying the suggestion container
                    // at the exact position of the incomplete text. Can be a bit buggy but will be improved.
                    resultsList.style.top = `${ top }px`;
                    resultsList.style.left = `${ left }px`;
                }
            }
        }
    } else {
        resultsList.style.display = 'none';
    }
} );

function isCaretAtEnd(element) {
    let atEnd = false,
        selRange,
        testRange;

    if ( window.getSelection ) {
        const sel = window.getSelection();
        if ( sel.rangeCount ) {
            selRange = sel.getRangeAt( 0 );
            testRange = selRange.cloneRange();

            testRange.selectNodeContents( element );
            testRange.setEnd( selRange.startContainer, selRange.startOffset );

            testRange.selectNodeContents( element );
            testRange.setStart( selRange.endContainer, selRange.endOffset );
            atEnd = ( testRange.toString() === "" );
        }
    }

    return atEnd;
}

function insertTextAtCursor(text) {
    let selection,
        range;
    if ( window.getSelection ) {
        selection = window.getSelection();
        if ( selection.getRangeAt && selection.rangeCount ) {
            range = selection.getRangeAt( 0 );
            range.deleteContents();
            range.insertNode( document.createTextNode( text ) );
        }
    }

    placeCaretAtEnd( document.querySelector( '#editor' ) );
}

function placeCaretAtEnd(editor) {
    if ( typeof window.getSelection !== 'undefined' && typeof document.createRange !== 'undefined' ) {
        const range = document.createRange();
        range.selectNodeContents( editor );
        range.collapse( false );
        const sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange( range );
    }
}
              
            
!
999px

Console