/
quill.ts
219 lines (191 loc) · 7.64 KB
/
quill.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import * as p from "@bokehjs/core/properties"
import { div } from "@bokehjs/core/dom"
import { HTMLBox, HTMLBoxView } from "./layout"
export class QuillInputView extends HTMLBoxView {
override model: QuillInput
protected container: HTMLDivElement
protected _editor: HTMLDivElement
protected _editing: boolean
protected _toolbar: HTMLDivElement | null
quill: any
connect_signals(): void {
super.connect_signals()
this.connect(this.model.properties.disabled.change, () => this.quill.enable(!this.model.disabled))
this.connect(this.model.properties.visible.change, () => {
if (this.model.visible)
this.container.style.visibility = 'visible';
})
this.connect(this.model.properties.text.change, () => {
if (this._editing)
return
this._editing = true
this.quill.enable(false)
this.quill.setContents([])
this.quill.clipboard.dangerouslyPasteHTML(this.model.text)
this.quill.enable(!this.model.disabled)
this._editing = false
})
const { mode, toolbar, placeholder } = this.model.properties
this.on_change([placeholder], () => {
this.quill.root.setAttribute('data-placeholder', this.model.placeholder)
})
this.on_change([mode, toolbar], () => {
this.render()
this._layout_toolbar()
})
}
_layout_toolbar(): void {
if (this._toolbar == null) {
this.el.style.removeProperty('padding-top')
} else {
const height = this._toolbar.getBoundingClientRect().height + 1
this.el.style.paddingTop = height + "px"
this._toolbar.style.marginTop = -height + "px"
}
}
render(): void {
super.render()
this.container = div({ style: "visibility: hidden;" })
this.shadow_el.appendChild(this.container)
const theme = (this.model.mode === 'bubble') ? 'bubble' : 'snow'
this.watch_stylesheets()
this.quill = new (window as any).Quill(this.container, {
modules: {
toolbar: this.model.toolbar
},
readOnly: true,
placeholder: this.model.placeholder,
theme: theme
});
// Apply ShadowDOM patch found at:
// https://github.com/quilljs/quill/issues/2961#issuecomment-1775999845
const hasShadowRootSelection = !!((document.createElement('div').attachShadow({ mode: 'open' }) as any).getSelection);
// Each browser engine has a different implementation for retrieving the Range
const getNativeRange = (rootNode: any) => {
try {
if (hasShadowRootSelection) {
// In Chromium, the shadow root has a getSelection function which returns the range
return rootNode.getSelection().getRangeAt(0);
} else {
const selection = window.getSelection();
if ((selection as any).getComposedRanges) {
// Webkit range retrieval is done with getComposedRanges (see: https://bugs.webkit.org/show_bug.cgi?id=163921)
return (selection as any).getComposedRanges(rootNode)[0];
} else {
// Gecko implements the range API properly in Native Shadow: https://developer.mozilla.org/en-US/docs/Web/API/Selection/getRangeAt
return (selection as any).getRangeAt(0);
}
}
} catch {
return null;
}
}
/**
* Original implementation uses document.active element which does not work in Native Shadow.
* Replace document.activeElement with shadowRoot.activeElement
**/
this.quill.selection.hasFocus = () => {
const rootNode = (this.quill.root.getRootNode() as ShadowRoot);
return rootNode.activeElement === this.quill.root;
}
/**
* Original implementation uses document.getSelection which does not work in Native Shadow.
* Replace document.getSelection with shadow dom equivalent (different for each browser)
**/
this.quill.selection.getNativeRange = () => {
const rootNode = (this.quill.root.getRootNode() as ShadowRoot);
const nativeRange = getNativeRange(rootNode);
return !!nativeRange ? this.quill.selection.normalizeNative(nativeRange) : null;
};
/**
* Original implementation relies on Selection.addRange to programmatically set the range, which does not work
* in Webkit with Native Shadow. Selection.addRange works fine in Chromium and Gecko.
**/
this.quill.selection.setNativeRange = (startNode: any, startOffset: any) => {
var endNode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : startNode;
var endOffset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : startOffset;
var force = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
if (startNode != null && (this.quill.selection.root.parentNode == null || startNode.parentNode == null || endNode.parentNode == null)) {
return;
}
var selection = document.getSelection();
if (selection == null) return;
if (startNode != null) {
if (!this.quill.selection.hasFocus()) this.quill.selection.root.focus();
var native = (this.quill.selection.getNativeRange() || {}).native;
if (native == null || force || startNode !== native.startContainer || startOffset !== native.startOffset || endNode !== native.endContainer || endOffset !== native.endOffset) {
if (startNode.tagName == "BR") {
startOffset = [].indexOf.call(startNode.parentNode.childNodes, startNode);
startNode = startNode.parentNode;
}
if (endNode.tagName == "BR") {
endOffset = [].indexOf.call(endNode.parentNode.childNodes, endNode);
endNode = endNode.parentNode;
}
selection.setBaseAndExtent(startNode, startOffset, endNode, endOffset);
}
} else {
selection.removeAllRanges();
this.quill.selection.root.blur();
document.body.focus();
}
}
this._editor = (this.shadow_el.querySelector('.ql-editor') as HTMLDivElement)
this._toolbar = (this.shadow_el.querySelector('.ql-toolbar') as HTMLDivElement)
const delta = this.quill.clipboard.convert(this.model.text);
this.quill.setContents(delta);
this.quill.on('text-change', () => {
if (this._editing)
return
this._editing = true
this.model.text = this._editor.innerHTML
this._editing = false
});
if (!this.model.disabled)
this.quill.enable(!this.model.disabled)
document.addEventListener("selectionchange", (..._args: any[]) => {
// Update selection and some other properties
this.quill.selection.update()
});
}
style_redraw(): void {
if (this.model.visible)
this.container.style.visibility = 'visible';
const delta = this.quill.clipboard.convert(this.model.text);
this.quill.setContents(delta);
this.invalidate_layout()
}
after_layout(): void {
super.after_layout()
this._layout_toolbar()
}
}
export namespace QuillInput {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
mode: p.Property<string>
placeholder: p.Property<string>
text: p.Property<string>
toolbar: p.Property<any>
}
}
export interface QuillInput extends QuillInput.Attrs { }
export class QuillInput extends HTMLBox {
properties: QuillInput.Props
constructor(attrs?: Partial<QuillInput.Attrs>) {
super(attrs)
}
static __module__ = "panel.models.quill"
static {
this.prototype.default_view = QuillInputView
this.define<QuillInput.Props>(({Any, String}) => ({
mode: [ String, 'toolbar' ],
placeholder: [ String, '' ],
text: [ String, '' ],
toolbar: [ Any, null ],
}))
this.override<QuillInput.Props>({
height: 300
})
}
}