2023-04-26 02:37:29 +00:00
|
|
|
import { createEffect } from 'solid-js'
|
2023-03-23 17:15:50 +00:00
|
|
|
import { createTiptapEditor, useEditorHTML } from 'solid-tiptap'
|
2023-03-08 16:35:13 +00:00
|
|
|
import { useLocalize } from '../../context/localize'
|
|
|
|
import { Blockquote } from '@tiptap/extension-blockquote'
|
|
|
|
import { Bold } from '@tiptap/extension-bold'
|
|
|
|
import { BubbleMenu } from '@tiptap/extension-bubble-menu'
|
|
|
|
import { Dropcursor } from '@tiptap/extension-dropcursor'
|
|
|
|
import { Italic } from '@tiptap/extension-italic'
|
|
|
|
import { Strike } from '@tiptap/extension-strike'
|
|
|
|
import { HorizontalRule } from '@tiptap/extension-horizontal-rule'
|
|
|
|
import { Underline } from '@tiptap/extension-underline'
|
|
|
|
import { FloatingMenu } from '@tiptap/extension-floating-menu'
|
|
|
|
import { BulletList } from '@tiptap/extension-bullet-list'
|
|
|
|
import { OrderedList } from '@tiptap/extension-ordered-list'
|
|
|
|
import { ListItem } from '@tiptap/extension-list-item'
|
|
|
|
import { CharacterCount } from '@tiptap/extension-character-count'
|
|
|
|
import { Placeholder } from '@tiptap/extension-placeholder'
|
|
|
|
import { Gapcursor } from '@tiptap/extension-gapcursor'
|
|
|
|
import { HardBreak } from '@tiptap/extension-hard-break'
|
|
|
|
import { Heading } from '@tiptap/extension-heading'
|
|
|
|
import { Highlight } from '@tiptap/extension-highlight'
|
|
|
|
import { Link } from '@tiptap/extension-link'
|
|
|
|
import { Document } from '@tiptap/extension-document'
|
|
|
|
import { Text } from '@tiptap/extension-text'
|
2023-05-07 13:47:10 +00:00
|
|
|
import { CustomImage } from './extensions/CustomImage'
|
|
|
|
import { Figure } from './extensions/Figure'
|
2023-03-08 16:35:13 +00:00
|
|
|
import { Paragraph } from '@tiptap/extension-paragraph'
|
|
|
|
import Focus from '@tiptap/extension-focus'
|
2023-03-29 08:51:27 +00:00
|
|
|
import * as Y from 'yjs'
|
|
|
|
import { CollaborationCursor } from '@tiptap/extension-collaboration-cursor'
|
|
|
|
import { Collaboration } from '@tiptap/extension-collaboration'
|
2023-05-07 13:47:10 +00:00
|
|
|
|
2023-03-29 08:51:27 +00:00
|
|
|
import { IndexeddbPersistence } from 'y-indexeddb'
|
|
|
|
import { useSession } from '../../context/session'
|
2023-03-29 10:22:03 +00:00
|
|
|
import uniqolor from 'uniqolor'
|
2023-03-29 15:36:12 +00:00
|
|
|
import { HocuspocusProvider } from '@hocuspocus/provider'
|
2023-05-06 12:38:22 +00:00
|
|
|
import { Embed } from './extensions/Embed'
|
2023-05-04 12:16:39 +00:00
|
|
|
import { TextBubbleMenu } from './TextBubbleMenu'
|
|
|
|
import { ImageBubbleMenu } from './ImageBubbleMenu'
|
2023-05-04 04:43:52 +00:00
|
|
|
import { EditorFloatingMenu } from './EditorFloatingMenu'
|
2023-04-26 02:37:29 +00:00
|
|
|
import { useEditorContext } from '../../context/editor'
|
2023-05-04 15:09:09 +00:00
|
|
|
import { isTextSelection } from '@tiptap/core'
|
2023-05-05 20:05:50 +00:00
|
|
|
import type { Doc } from 'yjs/dist/src/utils/Doc'
|
2023-05-07 13:47:10 +00:00
|
|
|
import './Prosemirror.scss'
|
2023-05-08 17:21:06 +00:00
|
|
|
import { TrailingNode } from './extensions/TrailingNode'
|
2023-03-08 16:35:13 +00:00
|
|
|
|
|
|
|
type EditorProps = {
|
2023-05-04 04:43:52 +00:00
|
|
|
shoutId: number
|
2023-03-08 16:35:13 +00:00
|
|
|
initialContent?: string
|
2023-03-23 17:15:50 +00:00
|
|
|
onChange: (text: string) => void
|
2023-03-08 16:35:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
const yDocs: Record<string, Doc> = {}
|
2023-03-29 10:14:39 +00:00
|
|
|
const persisters: Record<string, IndexeddbPersistence> = {}
|
2023-03-29 15:36:12 +00:00
|
|
|
const providers: Record<string, HocuspocusProvider> = {}
|
2023-03-08 16:35:13 +00:00
|
|
|
|
|
|
|
export const Editor = (props: EditorProps) => {
|
|
|
|
const { t } = useLocalize()
|
2023-03-29 08:51:27 +00:00
|
|
|
const { user } = useSession()
|
|
|
|
|
2023-05-04 04:43:52 +00:00
|
|
|
const docName = `shout-${props.shoutId}`
|
2023-03-29 08:51:27 +00:00
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
if (!yDocs[docName]) {
|
|
|
|
yDocs[docName] = new Y.Doc()
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:14:39 +00:00
|
|
|
if (!providers[docName]) {
|
2023-03-29 15:36:12 +00:00
|
|
|
providers[docName] = new HocuspocusProvider({
|
2023-04-11 13:57:48 +00:00
|
|
|
url: 'wss://hocuspocus.discours.io',
|
2023-03-29 15:36:12 +00:00
|
|
|
name: docName,
|
2023-05-05 20:05:50 +00:00
|
|
|
document: yDocs[docName]
|
2023-03-29 15:36:12 +00:00
|
|
|
})
|
2023-03-29 10:14:39 +00:00
|
|
|
}
|
2023-03-08 16:35:13 +00:00
|
|
|
|
2023-03-30 01:22:39 +00:00
|
|
|
if (!persisters[docName]) {
|
2023-05-05 20:05:50 +00:00
|
|
|
persisters[docName] = new IndexeddbPersistence(docName, yDocs[docName])
|
2023-03-30 01:22:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:35:13 +00:00
|
|
|
const editorElRef: {
|
|
|
|
current: HTMLDivElement
|
|
|
|
} = {
|
|
|
|
current: null
|
|
|
|
}
|
|
|
|
|
2023-05-04 12:16:39 +00:00
|
|
|
const textBubbleMenuRef: {
|
|
|
|
current: HTMLDivElement
|
|
|
|
} = {
|
|
|
|
current: null
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageBubbleMenuRef: {
|
2023-03-08 16:35:13 +00:00
|
|
|
current: HTMLDivElement
|
|
|
|
} = {
|
|
|
|
current: null
|
|
|
|
}
|
|
|
|
|
|
|
|
const floatingMenuRef: {
|
|
|
|
current: HTMLDivElement
|
|
|
|
} = {
|
|
|
|
current: null
|
|
|
|
}
|
|
|
|
|
|
|
|
const editor = createTiptapEditor(() => ({
|
|
|
|
element: editorElRef.current,
|
|
|
|
extensions: [
|
|
|
|
Document,
|
|
|
|
Text,
|
|
|
|
Paragraph,
|
|
|
|
Dropcursor,
|
|
|
|
Blockquote,
|
|
|
|
Bold,
|
|
|
|
Italic,
|
|
|
|
Strike,
|
2023-05-04 17:38:50 +00:00
|
|
|
HorizontalRule.configure({
|
|
|
|
HTMLAttributes: {
|
|
|
|
class: 'horizontalRule'
|
|
|
|
}
|
|
|
|
}),
|
2023-03-08 16:35:13 +00:00
|
|
|
Underline,
|
2023-03-20 09:19:14 +00:00
|
|
|
Link.configure({
|
|
|
|
openOnClick: false
|
|
|
|
}),
|
2023-03-22 07:47:51 +00:00
|
|
|
Heading.configure({
|
|
|
|
levels: [1, 2, 3]
|
|
|
|
}),
|
2023-03-08 16:35:13 +00:00
|
|
|
BulletList,
|
|
|
|
OrderedList,
|
|
|
|
ListItem,
|
2023-03-29 08:51:27 +00:00
|
|
|
Collaboration.configure({
|
2023-05-05 20:05:50 +00:00
|
|
|
document: yDocs[docName]
|
2023-03-29 08:51:27 +00:00
|
|
|
}),
|
2023-03-29 10:14:39 +00:00
|
|
|
CollaborationCursor.configure({
|
|
|
|
provider: providers[docName],
|
|
|
|
user: {
|
|
|
|
name: user().name,
|
2023-03-29 10:28:40 +00:00
|
|
|
color: uniqolor(user().slug).color
|
2023-03-29 10:14:39 +00:00
|
|
|
}
|
|
|
|
}),
|
2023-03-08 16:35:13 +00:00
|
|
|
Placeholder.configure({
|
|
|
|
placeholder: t('Short opening')
|
|
|
|
}),
|
|
|
|
Focus,
|
|
|
|
Gapcursor,
|
|
|
|
HardBreak,
|
2023-05-09 17:31:28 +00:00
|
|
|
Highlight.configure({
|
|
|
|
multicolor: true,
|
|
|
|
HTMLAttributes: {
|
|
|
|
class: 'highlight'
|
|
|
|
}
|
|
|
|
}),
|
2023-05-06 12:38:22 +00:00
|
|
|
CustomImage.configure({
|
2023-05-04 04:43:52 +00:00
|
|
|
HTMLAttributes: {
|
|
|
|
class: 'uploadedImage'
|
|
|
|
}
|
|
|
|
}),
|
2023-05-07 13:16:03 +00:00
|
|
|
Figure,
|
2023-04-26 02:37:29 +00:00
|
|
|
Embed,
|
2023-05-04 04:43:52 +00:00
|
|
|
CharacterCount,
|
2023-04-20 13:58:56 +00:00
|
|
|
BubbleMenu.configure({
|
2023-05-04 12:16:39 +00:00
|
|
|
pluginKey: 'textBubbleMenu',
|
|
|
|
element: textBubbleMenuRef.current,
|
2023-05-07 13:47:10 +00:00
|
|
|
shouldShow: ({ editor: e, view, state, from, to }) => {
|
2023-05-04 15:09:09 +00:00
|
|
|
const { doc, selection } = state
|
|
|
|
const { empty } = selection
|
|
|
|
|
|
|
|
const isEmptyTextBlock = doc.textBetween(from, to).length === 0 && isTextSelection(selection)
|
|
|
|
|
2023-05-07 13:16:03 +00:00
|
|
|
return (
|
|
|
|
view.hasFocus() && !empty && !isEmptyTextBlock && !e.isActive('image') && !e.isActive('figure')
|
|
|
|
)
|
2023-05-04 12:16:39 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
BubbleMenu.configure({
|
|
|
|
pluginKey: 'imageBubbleMenu',
|
|
|
|
element: imageBubbleMenuRef.current,
|
2023-05-07 13:47:10 +00:00
|
|
|
shouldShow: ({ editor: e, view }) => {
|
2023-05-04 15:09:09 +00:00
|
|
|
return view.hasFocus() && e.isActive('image')
|
2023-05-04 12:16:39 +00:00
|
|
|
}
|
2023-04-20 13:58:56 +00:00
|
|
|
}),
|
|
|
|
FloatingMenu.configure({
|
|
|
|
tippyOptions: {
|
|
|
|
placement: 'left'
|
|
|
|
},
|
|
|
|
element: floatingMenuRef.current
|
2023-05-08 17:21:06 +00:00
|
|
|
}),
|
|
|
|
TrailingNode
|
2023-03-08 16:35:13 +00:00
|
|
|
]
|
|
|
|
}))
|
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
const html = useEditorHTML(() => editor())
|
|
|
|
|
|
|
|
const {
|
|
|
|
actions: { countWords }
|
|
|
|
} = useEditorContext()
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
props.onChange(html())
|
|
|
|
if (html()) {
|
|
|
|
countWords({
|
|
|
|
characters: editor().storage.characterCount.characters(),
|
|
|
|
words: editor().storage.characterCount.words()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-08 16:35:13 +00:00
|
|
|
return (
|
2023-03-12 20:16:20 +00:00
|
|
|
<>
|
|
|
|
<div ref={(el) => (editorElRef.current = el)} />
|
2023-05-04 12:16:39 +00:00
|
|
|
<TextBubbleMenu editor={editor()} ref={(el) => (textBubbleMenuRef.current = el)} />
|
|
|
|
<ImageBubbleMenu editor={editor()} ref={(el) => (imageBubbleMenuRef.current = el)} />
|
2023-03-08 16:35:13 +00:00
|
|
|
<EditorFloatingMenu editor={editor()} ref={(el) => (floatingMenuRef.current = el)} />
|
2023-03-12 20:16:20 +00:00
|
|
|
</>
|
2023-03-08 16:35:13 +00:00
|
|
|
)
|
|
|
|
}
|