-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathuseEmbedModal.ts
More file actions
252 lines (243 loc) · 10.4 KB
/
useEmbedModal.ts
File metadata and controls
252 lines (243 loc) · 10.4 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import Quill, { Range } from "quill";
import { Delta } from "quill/core";
import Emitter from "quill/core/emitter";
import { Dispatch, MutableRefObject, SetStateAction, useState } from "react";
import { RichTextContainerProps } from "typings/RichTextProps";
import { IMG_MIME_TYPES } from "./constants";
import {
imageConfigType,
type linkConfigType,
type videoConfigType,
type videoEmbedConfigType,
viewCodeConfigType
} from "../../utils/formats";
import { type ChildDialogProps } from "../ModalDialog/Dialog";
import { type VideoFormType } from "../ModalDialog/VideoDialog";
type ModalReturnType = {
showDialog: boolean;
setShowDialog: Dispatch<SetStateAction<boolean>>;
dialogConfig: ChildDialogProps;
customLinkHandler(value: any): void;
customVideoHandler(value: any): void;
customViewCodeHandler(value: any): void;
customImageUploadHandler(value: any): void;
};
export function useEmbedModal(
ref: MutableRefObject<Quill | null>,
props: Pick<RichTextContainerProps, "formOrientation">
): ModalReturnType {
const [showDialog, setShowDialog] = useState<boolean>(false);
const [dialogConfig, setDialogConfig] = useState<ChildDialogProps>({});
const openDialog = (): void => {
setShowDialog(true);
};
const closeDialog = (): void => {
setShowDialog(false);
setTimeout(() => ref.current?.focus(), 50);
};
const customLinkHandler = (value: any): void => {
const selection = ref.current?.getSelection();
const text = selection ? ref.current?.getText(selection.index, selection.length) : "";
if (value) {
setDialogConfig({
dialogType: "link",
config: {
onSubmit: (value: linkConfigType) => {
const index = selection?.index ?? 0;
const length = selection?.length ?? 0;
const textToDisplay = value.text ?? value.href;
const linkDelta = new Delta().retain(index).delete(length).insert(textToDisplay);
ref.current?.updateContents(linkDelta, Emitter.sources.SILENT);
ref.current?.setSelection(index, textToDisplay.length);
ref.current?.format("link", value);
closeDialog();
},
onClose: closeDialog,
defaultValue: { ...value, text },
formOrientation: props.formOrientation
}
});
openDialog();
} else {
ref.current?.format("link", false);
closeDialog();
}
};
const customVideoHandler = (value: any): void => {
const selection = ref.current?.getSelection();
if (value === true || value.type === "video") {
setDialogConfig({
dialogType: "video",
config: {
onSubmit: (submittedValue: VideoFormType) => {
if (
Object.hasOwn(submittedValue, "src") &&
(submittedValue as videoConfigType).src !== undefined
) {
const currentValue = submittedValue as videoConfigType;
if (value.type === "video") {
const index = selection?.index ?? 0;
const length = selection?.length ?? 1;
const videoConfig = {
width: currentValue.width,
height: currentValue.height
};
// update existing video value
const delta = new Delta().retain(index).retain(length, videoConfig);
ref.current?.updateContents(delta, Emitter.sources.USER);
} else {
// insert new video
const delta = new Delta()
.retain(selection?.index ?? 0)
.delete(selection?.length ?? 0)
.insert(
{ video: currentValue },
{ width: currentValue.width, height: currentValue.height }
);
ref.current?.updateContents(delta, Emitter.sources.USER);
}
} else {
const currentValue = submittedValue as videoEmbedConfigType;
const res = ref.current?.clipboard.convert({
html: currentValue.embedcode
});
if (res) {
// insert video via embed code;
const delta = new Delta()
.retain(selection?.index ?? 0)
.delete(selection?.length ?? 0)
.concat(res);
ref.current?.updateContents(delta, Emitter.sources.USER);
}
}
closeDialog();
},
onClose: closeDialog,
selection: ref.current?.getSelection(),
defaultValue: { ...value },
formOrientation: props.formOrientation
}
});
openDialog();
} else {
ref.current?.format("link", false);
closeDialog();
}
};
const customViewCodeHandler = (value: any): void => {
if (value === true) {
setDialogConfig({
dialogType: "view-code",
config: {
currentCode: ref.current?.getSemanticHTML(),
onSubmit: (value: viewCodeConfigType) => {
const newDelta = ref.current?.clipboard.convert({ html: value.src });
if (newDelta) {
ref.current?.setContents(newDelta, Emitter.sources.USER);
}
closeDialog();
},
onClose: closeDialog,
formOrientation: props.formOrientation
}
});
openDialog();
} else {
ref.current?.format("link", false);
closeDialog();
}
};
const customImageUploadHandler = (value: any): void => {
const selection = ref.current?.getSelection(true);
setDialogConfig({
dialogType: "image",
config: {
onSubmit: (value: imageConfigType) => {
const defaultImageConfig = {
alt: value.alt,
width: value.width,
height: value.keepAspectRatio ? undefined : value.height
};
if (value.src) {
const index = selection?.index ?? 0;
const length = 1;
const imageConfig = defaultImageConfig;
// update existing image attribute
const imageUpdateDelta = new Delta().retain(index).retain(length, imageConfig);
ref.current?.updateContents(imageUpdateDelta, Emitter.sources.USER);
} else {
// upload new image
if (selection) {
if (value.files) {
uploadImage(ref, selection, value);
} else if (value.entityGuid) {
const imageConfig = {
...defaultImageConfig,
"data-src": value.entityGuid
};
const delta = new Delta()
.retain(selection.index)
.delete(selection.length)
.insert({ image: value.entityGuid }, imageConfig);
ref.current?.updateContents(delta, Emitter.sources.USER);
}
}
}
closeDialog();
},
onClose: closeDialog,
defaultValue: { ...value },
formOrientation: props.formOrientation
}
});
openDialog();
};
return {
showDialog,
setShowDialog,
dialogConfig,
customLinkHandler,
customVideoHandler,
customViewCodeHandler,
customImageUploadHandler
};
}
function uploadImage(ref: MutableRefObject<Quill | null>, range: Range, options: imageConfigType): void {
const uploads: File[] = [];
const { files } = options;
if (files) {
Array.from(files).forEach(file => {
if (file && IMG_MIME_TYPES.includes(file.type)) {
uploads.push(file);
}
});
if (uploads.length > 0) {
if (!ref.current?.scroll.query("image")) {
return;
}
const promises = uploads.map<Promise<string>>(file => {
return new Promise(resolve => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result as string);
};
reader.readAsDataURL(file);
});
});
Promise.all(promises).then(images => {
const update = images.reduce((delta: Delta, image) => {
return delta.insert(
{ image },
{
alt: options.alt,
width: options.width,
height: options.height
}
);
}, new Delta().retain(range.index).delete(range.length)) as Delta;
ref.current?.updateContents(update, Emitter.sources.USER);
ref.current?.setSelection(range.index + images.length, Emitter.sources.SILENT);
});
}
}
}