-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
284 lines (234 loc) · 9.08 KB
/
index.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import Alpine from 'alpinejs'
import {Editor} from '@tiptap/core'
import ExtensionBold from '@tiptap/extension-bold'
import ExtensionBubbleMenu from '@tiptap/extension-bubble-menu'
import ExtensionHeading from '@tiptap/extension-heading'
import ExtensionImage from '@tiptap/extension-image'
import ExtensionItalic from '@tiptap/extension-italic'
import ExtensionLink from '@tiptap/extension-link'
import ExtensionOrderedList from '@tiptap/extension-ordered-list'
import ExtensionBulletList from '@tiptap/extension-bullet-list'
import ExtensionListItem from '@tiptap/extension-list-item'
import ExtensionParagraph from '@tiptap/extension-paragraph'
import ExtensionStrike from '@tiptap/extension-strike'
import ExtensionText from '@tiptap/extension-text'
import ExtensionDocument from '@tiptap/extension-document'
import 'tippy.js/dist/tippy.css'
const ImageBlobReduce = require('image-blob-reduce')()
import { TextSelection } from 'prosemirror-state'
import Cookies from 'js-cookie'
interface GetsS3UrlResponse {
uploadUrl: string
uploadUrlFormData: { [key: string]: string }
uploadKeyPrefix: string
downloadUrlPrefix: string
}
const data = (content: any, userOptions: any) => ({
editor: null,
content,
isBold: false,
isItalic: false,
isStrike: false,
isHeading: false,
isLink: false,
isImage: false,
options: {
enableImageUpload: false,
enableLinks: true,
maxSize: 1000,
generateImageUploadConfigUrl: '/laravel-tiptap/generate-image-upload-config',
...userOptions,
},
imageUploadConfig: null as null | GetsS3UrlResponse,
init() {
// @ts-ignore
this.editor = new Editor({
// @ts-ignore
element: this.$refs['editor'],
extensions: [
ExtensionBold,
ExtensionBubbleMenu.configure({
pluginKey: 'link-bubble-menu',
// @ts-ignore
element: this.$refs['link-bubble-menu'],
shouldShow: ({ editor }) => editor.isActive('link'),
}),
ExtensionBubbleMenu.configure({
pluginKey: 'image-bubble-menu',
// @ts-ignore
element: this.$refs['image-bubble-menu'],
shouldShow: ({ editor }) => editor.isActive('image'),
}),
ExtensionHeading,
ExtensionImage,
ExtensionItalic,
ExtensionLink.configure({
HTMLAttributes: { target: '_blank', rel: 'noopener' },
openOnClick: false,
}),
ExtensionOrderedList,
ExtensionBulletList,
ExtensionListItem,
ExtensionParagraph,
ExtensionStrike,
ExtensionText,
ExtensionDocument,
],
content: this.content,
onUpdate: ({ editor }) => {
this.content = editor.getHTML()
},
onSelectionUpdate: () => {
// @ts-ignore
this.isBold = this.editor.isActive('bold')
// @ts-ignore
this.isItalic = this.editor.isActive('italic')
// @ts-ignore
this.isStrike = this.editor.isActive('strike')
// @ts-ignore
this.isHeading = this.editor.isActive('heading')
// @ts-ignore
this.isLink = this.editor.isActive('link')
// @ts-ignore
this.isImage = this.editor.isActive('image')
},
})
// @ts-ignore
this.$watch('content', (content) => {
// If the new content matches TipTap's then we just skip.
if (content === Alpine.raw(this.editor).getHTML()) return
/*
Otherwise, it means that a force external to TipTap
is modifying the data on this Alpine component,
which could be Livewire itself.
In this case, we just need to update TipTap's
content and we're good to do.
For more information on the `setContent()` method, see:
https://www.tiptap.dev/api/commands/set-content
*/
Alpine.raw(this.editor).commands.setContent(content, false)
})
},
get currentLinkHref() {
if (! this.editor) return ''
const state = Alpine.raw(this.editor).state
const { from, to } = state.selection
let marks: any = []
state.doc.nodesBetween(from, to, (node: any) => {
marks = [...marks, ...node.marks]
})
const mark = marks.find((markItem: any) => markItem.type.name === 'link')
return mark && mark.attrs.href ? mark.attrs.href : ''
},
captureLinkHref() {
const href = window.prompt('Please give the URL', this.currentLinkHref)
if (! href) return
Alpine.raw(this.editor).chain().extendMarkRange('link').setLink({ href }).focus().run()
},
removeLink() {
Alpine.raw(this.editor).chain().extendMarkRange('link').unsetLink().focus().run()
},
showFilePicker() {
// @ts-ignore
this.$refs['picker'].click()
},
async handleFileSelect(event: Event) {
const target = event.target as HTMLInputElement
if (!target.files?.length) return
for await (const file of Array.from(target.files)) {
await this.handleUpload(file)
}
// reset picker
(<HTMLInputElement>event.target).value = ''
},
async handleFileDrop(event: DragEvent) {
event.stopPropagation()
event.preventDefault()
if (!event.dataTransfer) return
const editor = Alpine.raw(this.editor) as Editor
const coordinates = editor.view.posAtCoords({ left: event.clientX, top: event.clientY })
for await (const file of Array.from(event.dataTransfer.files)) {
if (! file.type.startsWith('image/')) {
return
}
await this.handleUpload(file, coordinates?.pos)
}
},
async getImageUploadConfig(): Promise<GetsS3UrlResponse> {
if (this.imageUploadConfig) return this.imageUploadConfig
const getsS3UrlResponse = await typedFetch<GetsS3UrlResponse>(this.options.generateImageUploadConfigUrl, {
method: 'post',
headers: [
['X-XSRF-TOKEN', Cookies.get('XSRF-TOKEN') ?? ''],
],
})
if (!getsS3UrlResponse.data) {
throw 'Something went wrong'
}
this.imageUploadConfig = getsS3UrlResponse.data
return this.imageUploadConfig
},
async handleUpload(file: File, position?: number) {
const imageUploadConfig = await this.getImageUploadConfig()
const formData = new FormData()
for (const [key, value] of Object.entries(imageUploadConfig.uploadUrlFormData)) {
formData.set(key, value)
}
// resize our image
const resizedFile = await ImageBlobReduce.toBlob(file, {
max: this.options.maxSize,
})
formData.set('Content-Type', file.type)
formData.set('key', `${imageUploadConfig.uploadKeyPrefix}/${file.name}`)
formData.append('file', resizedFile)
const uploadResponse = await typedFetch(imageUploadConfig.uploadUrl, {
method: 'post',
body: formData,
})
if (uploadResponse.status !== 201) {
throw 'something went wrong while uploading the image'
}
const imageUrl = `${imageUploadConfig.downloadUrlPrefix}${file.name}`
const editor = Alpine.raw(this.editor) as Editor
const node = editor.schema.nodes.image.create({
src: imageUrl,
})
const insertTransaction = editor.view.state.tr.insert(
position ?? editor.view.state.selection.anchor,
node
)
editor.view.dispatch(insertTransaction)
const endPos = editor.state.selection.$to.after() - 1
const resolvedPos = editor.state.doc.resolve(endPos)
const moveCursorTransaction = editor.view.state.tr.setSelection(new TextSelection(resolvedPos))
editor.view.dispatch(moveCursorTransaction.scrollIntoView())
},
removeImage() {
const state = Alpine.raw(this.editor).state
const view = Alpine.raw(this.editor).view
const transaction = state.tr
const pos = state.selection.$anchor.pos
const nodeSize = state.selection.node.nodeSize
transaction.delete(pos, pos + nodeSize)
view.dispatch(transaction)
},
})
type FetchResponse<T> = Response & {
data?: T
}
export async function typedFetch<T>(
request: RequestInfo,
init?: RequestInit | undefined,
): Promise<FetchResponse<T>> {
const response = await fetch(request, init)
const contentType = response.headers.get('content-type')
if (contentType && contentType === 'application/json') {
const data = await response.json() as T
return {
...response,
data,
}
}
return response
}
export default data