quilljs2-rails 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Created by Abhinav Mathur on 16/02/17.
3
+ * Updated on 02/04/17
4
+ */
5
+
6
+ (function(){
7
+
8
+ var defaults = {
9
+ theme: 'snow',
10
+ modules: {
11
+ toolbar: [
12
+ [{ 'header': [1, 2, 3, false] }],
13
+ [{ 'color': [] }, { 'background': [] }],
14
+ ['bold', 'italic', 'underline', 'strike'],
15
+ ['blockquote', 'code-block'],
16
+ [{ 'list': 'ordered'}, { 'list': 'bullet' }],
17
+ [{ 'indent': '-1'}, { 'indent': '+1' }],
18
+ ['clean']
19
+ ]
20
+ }
21
+ };
22
+
23
+ window.Quilljs = {
24
+ //This method loads the default values from the global object
25
+ setDefaults: function(new_defaults) {
26
+ defaults = $.extend(defaults, new_defaults);
27
+ this.addQuillContainer(defaults)
28
+ },
29
+ //This method loads if no global object is defined
30
+ loadDefaults: function () {
31
+ this.addQuillContainer(defaults)
32
+ },
33
+ //This method adds the quill containers
34
+ addQuillContainer: function (settings) {
35
+
36
+ var create_quill_containers = function(index, input_field) {
37
+ var current_quill_container = create_quill_container(input_field, input_field.id + '-quill');
38
+ var placeholder_attr = $(input_field).attr('placeholder');
39
+ if (typeof placeholder_attr !== typeof undefined && placeholder_attr !== false) {
40
+ settings.placeholder = $(input_field).attr('placeholder');
41
+ }
42
+
43
+ window['quill-container-' + index] = new Quill(current_quill_container, settings);
44
+
45
+ // This function adds bootstrap maxlength functionality to the quill container
46
+ if ($(input_field).attr('maxlength') || $(input_field).attr('minlength')) {
47
+ add_bootstrap_maxlength(input_field, current_quill_container);
48
+ }
49
+
50
+ // Event listener to make sure we copy the new content of the Quill back into the field
51
+ window['quill-container-' + index].on('text-change', function () {
52
+ debounce_html(input_field, current_quill_container);
53
+ });
54
+
55
+ },
56
+ //This function creates a separate quill container since html cannot be rendered
57
+ //properly inside input fields
58
+ create_quill_container = function(field, quill) {
59
+ $(field).hide().after('<div class="form-control ' + quill + '"></div>');
60
+ quill = '.' + quill;
61
+ $(quill).html($(field).val());
62
+
63
+ //pass on the class name to quill container initializer
64
+ return quill;
65
+ },
66
+ // This adds maxLength compatibility for the editor
67
+ add_bootstrap_maxlength = function (field, quill) {
68
+ var quill_editor = quill + ' .ql-editor';
69
+
70
+ //Check if fields have minlength and maxlength properties, if yes, add them to the new quill container
71
+ if ($(field).attr('maxlength')) {
72
+ $(quill_editor).attr('maxlength', $(field).attr('maxlength'));
73
+ }
74
+ if ($(field).attr('minlength')) {
75
+ $(quill_editor).attr('minlength', $(field).attr('minlength'));
76
+ }
77
+
78
+ //removes the minlength and maxlength properties from the original container
79
+ $(field).removeAttr('maxlength').removeAttr('minlength');
80
+ },
81
+ // Synchronises changes made into their respective hidden fields.
82
+ debounce_html = debounce(function (input_field, current_quill_container) {
83
+ $(input_field).val($(current_quill_container + ' .ql-editor').html());
84
+ }, 500);
85
+
86
+
87
+ var quill_container = $('.quill_container');
88
+ if (quill_container.length > 0) {
89
+ quill_container.each(function (index, object) {
90
+ return create_quill_containers(index, object)
91
+ });
92
+ }
93
+ }
94
+ };
95
+
96
+ $( document ).on('ready page:change turbolinks:load', function() {
97
+ if ($('.ql-editor').length <= 0){
98
+ Quilljs.loadDefaults();
99
+ }
100
+ });
101
+
102
+ //Debounce function exported from underscore.js to sync the quill container with the hidden input field
103
+ function debounce(func, wait, immediate) {
104
+ var timeout;
105
+ return function() {
106
+ var context = this, args = arguments;
107
+ var later = function() {
108
+ timeout = null;
109
+ if (!immediate) func.apply(context, args);
110
+ };
111
+ var callNow = immediate && !timeout;
112
+ clearTimeout(timeout);
113
+ timeout = setTimeout(later, wait);
114
+ if (callNow) func.apply(context, args);
115
+ };
116
+ }
117
+ })(jQuery);