Created
January 28, 2017 16:18
-
-
Save dyor/6e6e139253425e0c99a6737490cbe6c5 to your computer and use it in GitHub Desktop.
Quilljs Sample that Handles Tabbing Properly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Include stylesheet --> | |
<link href="https://cdn.quilljs.com/1.1.7/quill.snow.css" rel="stylesheet"> | |
<style> | |
.ql-snow { | |
width: 550px; | |
} | |
</style> | |
<!-- Create the editor container --> | |
<div id="LongDescriptionShadow" style="height:200px; width:550px"></div> | |
<!-- Include the Quill library --> | |
<script src="https://cdn.quilljs.com/1.1.7/quill.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script> | |
$(document).ready(function () { | |
var ldText = $("#LongDescription").val(); | |
quill.pasteHTML(0, ldText, '', true); | |
//keep hiddent text field in sync with Quill content | |
quill.on('text-change', function (delta, source) { | |
$("#LongDescription").val($(".ql-editor").html()); | |
}); | |
//prevent Quill controls from receiving tab focus | |
$(".ql-toolbar").find(":button").attr('tabindex', '-1'); | |
}); | |
</script> | |
<!-- Initialize Quill editor --> | |
<script> | |
var toolbarOptions = [ | |
['bold', 'italic', 'underline', 'strike'], | |
['blockquote', 'code-block'], | |
[{ 'header': 1 }, { 'header': 2 }], | |
[{ 'list': 'ordered' }, { 'list': 'bullet' }], | |
[{ 'indent': '-1' }, { 'indent': '+1' }], | |
[{ 'direction': 'rtl' }], | |
['link', 'image'] | |
]; | |
//keyboard module prevents tabbing in Quill, and instead tabs to next field | |
var quill = new Quill('#LongDescriptionShadow', { | |
modules: { | |
toolbar: toolbarOptions, | |
keyboard: { | |
bindings: { | |
tab: { | |
key: 9, | |
handler: function () { | |
var inputs = $("#LongDescriptionShadow").closest('form').find(':input'); | |
inputs.eq(inputs.index(this) + 19).focus(); | |
} | |
} | |
} | |
}, | |
}, | |
theme: 'snow' | |
}); | |
</script> |
Thanks @joshuabryant, saved me tearing my hair out!
This approach works perfectly. Thanks, @joshuabryant.
@joshuabryant thanks buddy!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Howdy. Stumbled on this gist, but ended up finding a simpler method (perhaps an addition to Quill's keyboard bindings since this was initially written):
From the Quill.js documentation:
So overwriting the default key binding for the tab key via configuration, and then letting it fall through to the next handler by returning
true
, effectively propagates the event to the default browser handler (i.e. move focus to the next input).Hope this helps.