-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMudHtmlEditor.razor.js
More file actions
164 lines (141 loc) · 5 KB
/
MudHtmlEditor.razor.js
File metadata and controls
164 lines (141 loc) · 5 KB
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
var Embed = Quill.import('blots/block/embed');
const Delta = Quill.import('delta');
class Divider extends Embed {
static create(value) {
let node = super.create(value);
node.setAttribute('style', "height: 0px; margin-top: 0.5em 0; border-width; 1px; border-style: solid none none none;");
return node;
}
}
Divider.blotName = 'hr';
Divider.tagName = 'hr';
Quill.register(Divider, true);
try {
Quill.register('modules/blotFormatter', QuillBlotFormatter.default);
} catch { }
export function createQuillInterop(dotNetRef, editorRef, toolbarRef, settings) {
var interop = new MudQuillInterop(dotNetRef, editorRef, toolbarRef, settings);
var properties = {
modules: {
toolbar: {
container: toolbarRef
},
blotFormatter: {},
uploader: {
mimetypes: settings.allowedImageMimeTypes
}
},
placeholder: settings.placeholder,
theme: 'snow'
};
// Use custom handler if specified
if (settings.blazorImageUpload || settings.imageUploadUrl) {
properties.modules.uploader.handler = interop.uploadImageHandler;
}
var quill = new Quill(editorRef, properties);
interop.addQuill(quill);
return interop;
}
export class MudQuillInterop {
/**
* @param {Quill} quill
* @param {Element} editorRef
* @param {Element} toolbarRef
* @param {object} toolbarRef
*/
constructor(dotNetRef, editorRef, toolbarRef, settings) {
this.dotNetRef = dotNetRef;
this.quill;
this.editorRef = editorRef;
this.toolbarRef = toolbarRef;
this.blazorImageUpload = settings.blazorImageUpload;
this.imageBytes = {};
this.imageUploadUrl = settings.imageUploadUrl;
}
addQuill = (quill) => {
quill.getModule('toolbar').addHandler('hr', this.insertDividerHandler);
quill.on('text-change', this.textChangedHandler);
this.quill = quill;
}
getText = () => {
return this.quill.getText();
};
getHtml = () => {
return this.quill.root.innerHTML;
};
setHtml = (html) => {
this.quill.root.innerHTML = html;
}
insertDividerHandler = () => {
const range = this.quill.getSelection();
if (range) {
this.quill.insertEmbed(range.index, "hr", "null");
}
};
/**
*
* @param {Delta} delta
* @param {Delta} oldDelta
* @param {any} source
*/
textChangedHandler = (delta, oldDelta, source) => {
this.dotNetRef.invokeMethodAsync('HandleHtmlContentChanged', this.getHtml());
this.dotNetRef.invokeMethodAsync('HandleTextContentChanged', this.getText());
};
// Get imageBytes by filename
getImageBytes = (key) => {
return this.imageBytes[key];
}
// Read file and upload it via the blazor or controller method
upload(file) {
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.addEventListener("load", () => {
// Pass file via DotNetInterop
if (this.blazorImageUpload) {
this.imageBytes[file.name] = new Uint8Array(fileReader.result);
this.dotNetRef.invokeMethodAsync("SaveImage", file.name, file.type, file.size).then(url => resolve({name: file.name, url: url }));
}
// Upload to API endpoint
else if (this.imageUploadUrl) {
const formData = new FormData();
formData.append("files", file);
fetch(this.imageUploadUrl, {
method: "POST",
headers: {},
body: formData
})
.then(response => {
if (response.status === 200) {
response.text().then(url =>
resolve({ name: file.name, url: url })
);
}
else {
reject("Error uploading image to " + this.imageUploadUrl);
}
});
}
});
if (file) {
fileReader.readAsArrayBuffer(file);
} else {
reject("No file selected");
}
});
}
// Handle when images are copy/pasted or dragged/dropped into the editor
uploadImageHandler = (range, files) => {
for (let file of files) {
this.upload(file).then((data) => {
let delta = new Delta().retain(range.index).delete(range.length).insert({ image: data.url });
delete this.imageBytes[data.name]
this.quill.updateContents(delta, Quill.sources.USER);
this.quill.setSelection(range.index, Quill.sources.SILENT);
},
(error) => {
console.warn(error);
});
}
}
}