-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknockout-quill.js
184 lines (143 loc) · 6.06 KB
/
knockout-quill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _quill = __webpack_require__(1);
var _quill2 = _interopRequireDefault(_quill);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var default_options = {
theme: "snow",
styles: false,
modules: {}
};
var quill_map = new WeakMap();
ko.bindingHandlers.quill = {
init: function init(element, valueAccessor, allBindings) {
var html_observable = valueAccessor();
var focus_observable = ko.isObservable(allBindings.get('quill_has_focus')) && allBindings.get('quill_has_focus') || null;
if (!ko.isObservable(html_observable)) throw new Error("key to the 'quill' binding must be an observable");
// see if user has set quill_options
var options = Object.assign({}, default_options, allBindings.get('quill_options'));
// see if the user has enabled the toolbar module
allBindings.has('quill_toolbar') && Object.assign(options.modules, {
toolbar: {
container: ko.unwrap(allBindings.get('quill_toolbar'))
}
});
// see if the user has enabled the link tooltip
allBindings.has('quill_link_tooltip') && Object.assign(options.modules, {
"link-tooltip": ko.unwrap(allBindings.get('quill_link_tooltip'))
});
// see if the user has enabled the image tooltip
allBindings.has('quill_image_tooltip') && Object.assign(options.modules, {
"image-tooltip": ko.unwrap(allBindings.get('quill_image_tooltip'))
});
// see if the user has set a specific theme
allBindings.has('quill_theme') && Object.assign(options, {
theme: ko.unwrap(allBindings.get('quill_theme'))
});
// see if the user has set any specific styles
allBindings.has('quill_styles') && Object.assign(options, {
styles: ko.unwrap(allBindings.get('quill_styles'))
});
// Initialize the quill editor and store it in the quill map
var quill = new _quill2.default(element, options);
quill_map.set(element, quill);
// Set the quill editor's initial content to the current value of the
// provided observable
quill.setHTML(ko.unwrap(html_observable) || "");
// If text changes and cursor is not in the text area, update the observable
// If text changes and curor is still in the text area, we'll update the
// observable when the cursor leaves (see: `quill.on('selection-change')`)
quill.on('text-change', function (delta) {
quill.getSelection() || html_observable(quill.getHTML());
});
// Make sure we update the observables when the editor contents change.
quill.on("selection-change", function (range) {
// range looks like: `{start: Number, end: Number}`
// range.start === range.end <=> nothing selected
if (range) {
// cursor is in the text area
// make sure focus observable is true but don't trigger an update
focus_observable(true);
} else {
// cursor just left the selection area
focus_observable(false);
var current_value = quill.getHTML();
if (current_value === "<div><br></div>") {
// Quill likes to set empty panes to "<div><br></div>"; detect it and
// update the observable to null
html_observable(null);
} else {
// update the observable to current editor contents
html_observable(current_value);
}
}
});
// when focus observable changes
focus_observable && focus_observable.subscribe(function (is_focused) {
if (is_focused) {
// set cursor to the last spot if the focus observable is truthy
var length = quill.getLength();
!quill.getSelection() && quill.setSelection(length, length);
} else {
// remove the cursor if the focus observable is falsey
quill.getSelection() && quill.setSelection(null);
}
});
// Destroy our quill when the element is removed from the DOM
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
return quill.destroy();
});
},
update: function update(element, valueAccessor, allBindings) {
var html_observable = valueAccessor();
var quill = quill_map.get(element);
if (quill.getHTML() !== ko.unwrap(html_observable)) {
// Get the user's current selection range(s)
var selection = quill.getSelection();
// Set the content of the quill editor to the current value of the
// provided observable
quill.setHTML(ko.unwrap(html_observable) || "");
// Reset the selection ranges to what the user initially had selected
quill.setSelection(selection);
}
}
};
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = Quill;
/***/ }
/******/ ]);