Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
import CharacterCount from '@tiptap/extension-character-count'
|
|
|
|
import Placeholder from '@tiptap/extension-placeholder'
|
|
|
|
import {
|
|
|
|
useEditor,
|
|
|
|
EditorContent,
|
|
|
|
FloatingMenu,
|
|
|
|
JSONContent,
|
|
|
|
Content,
|
|
|
|
Editor,
|
|
|
|
} from '@tiptap/react'
|
|
|
|
import StarterKit from '@tiptap/starter-kit'
|
|
|
|
import { Image } from '@tiptap/extension-image'
|
2022-07-13 22:56:15 +00:00
|
|
|
import { Link } from '@tiptap/extension-link'
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { Linkify } from './linkify'
|
|
|
|
import { uploadImage } from 'web/lib/firebase/storage'
|
|
|
|
import { useMutation } from 'react-query'
|
|
|
|
import { exhibitExts } from 'common/util/parse'
|
|
|
|
import { FileUploadButton } from './file-upload-button'
|
2022-07-13 23:12:19 +00:00
|
|
|
import { linkClass } from './site-link'
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
|
2022-07-13 22:56:15 +00:00
|
|
|
const proseClass = clsx(
|
2022-07-16 17:16:18 +00:00
|
|
|
'prose prose-p:my-0 prose-li:my-0 prose-blockquote:not-italic max-w-none prose-quoteless font-light'
|
2022-07-13 22:56:15 +00:00
|
|
|
)
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
|
|
|
|
export function useTextEditor(props: {
|
|
|
|
placeholder?: string
|
|
|
|
max?: number
|
|
|
|
defaultValue?: Content
|
|
|
|
disabled?: boolean
|
|
|
|
}) {
|
|
|
|
const { placeholder, max, defaultValue = '', disabled } = props
|
|
|
|
|
|
|
|
const editorClass = clsx(
|
|
|
|
proseClass,
|
|
|
|
'box-content min-h-[6em] textarea textarea-bordered'
|
|
|
|
)
|
|
|
|
|
|
|
|
const editor = useEditor({
|
|
|
|
editorProps: { attributes: { class: editorClass } },
|
|
|
|
extensions: [
|
|
|
|
StarterKit.configure({
|
|
|
|
heading: { levels: [1, 2, 3] },
|
|
|
|
}),
|
|
|
|
Placeholder.configure({
|
|
|
|
placeholder,
|
|
|
|
emptyEditorClass:
|
|
|
|
'before:content-[attr(data-placeholder)] before:text-slate-500 before:float-left before:h-0',
|
|
|
|
}),
|
|
|
|
CharacterCount.configure({ limit: max }),
|
|
|
|
Image,
|
2022-07-13 23:50:08 +00:00
|
|
|
Link.configure({
|
|
|
|
HTMLAttributes: {
|
|
|
|
class: clsx('no-underline !text-indigo-700', linkClass),
|
|
|
|
},
|
|
|
|
}),
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
],
|
|
|
|
content: defaultValue,
|
|
|
|
})
|
|
|
|
|
|
|
|
const upload = useUploadMutation(editor)
|
|
|
|
|
|
|
|
editor?.setOptions({
|
|
|
|
editorProps: {
|
|
|
|
handlePaste(view, event) {
|
|
|
|
const imageFiles = Array.from(event.clipboardData?.files ?? []).filter(
|
|
|
|
(file) => file.type.startsWith('image')
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!imageFiles.length) {
|
|
|
|
return // if no files pasted, use default paste handler
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
upload.mutate(imageFiles)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
editor?.setEditable(!disabled)
|
|
|
|
}, [editor, disabled])
|
|
|
|
|
|
|
|
return { editor, upload }
|
|
|
|
}
|
|
|
|
|
|
|
|
export function TextEditor(props: {
|
|
|
|
editor: Editor | null
|
|
|
|
upload: ReturnType<typeof useUploadMutation>
|
|
|
|
}) {
|
|
|
|
const { editor, upload } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{/* hide placeholder when focused */}
|
2022-07-16 19:50:49 +00:00
|
|
|
<div className="w-full [&:focus-within_p.is-empty]:before:content-none">
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
{editor && (
|
|
|
|
<FloatingMenu
|
|
|
|
editor={editor}
|
2022-07-16 17:19:53 +00:00
|
|
|
className="-ml-2 mr-2 w-full text-sm text-slate-300"
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
>
|
2022-07-16 17:19:53 +00:00
|
|
|
Type <em>*markdown*</em>. Paste or{' '}
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
<FileUploadButton
|
|
|
|
className="link text-blue-300"
|
|
|
|
onFiles={upload.mutate}
|
|
|
|
>
|
2022-07-16 17:19:53 +00:00
|
|
|
upload
|
|
|
|
</FileUploadButton>{' '}
|
|
|
|
images!
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
</FloatingMenu>
|
|
|
|
)}
|
|
|
|
<EditorContent editor={editor} />
|
|
|
|
</div>
|
|
|
|
{upload.isLoading && <span className="text-xs">Uploading image...</span>}
|
|
|
|
{upload.isError && (
|
|
|
|
<span className="text-error text-xs">Error uploading image :(</span>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const useUploadMutation = (editor: Editor | null) =>
|
|
|
|
useMutation(
|
|
|
|
(files: File[]) =>
|
2022-07-16 18:37:28 +00:00
|
|
|
// TODO: Images should be uploaded under a particular username
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
Promise.all(files.map((file) => uploadImage('default', file))),
|
|
|
|
{
|
|
|
|
onSuccess(urls) {
|
|
|
|
if (!editor) return
|
|
|
|
let trans = editor.view.state.tr
|
|
|
|
urls.forEach((src: any) => {
|
|
|
|
const node = editor.view.state.schema.nodes.image.create({ src })
|
|
|
|
trans = trans.insert(editor.view.state.selection.to, node)
|
|
|
|
})
|
|
|
|
editor.view.dispatch(trans)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
function RichContent(props: { content: JSONContent }) {
|
|
|
|
const { content } = props
|
|
|
|
const editor = useEditor({
|
|
|
|
editorProps: { attributes: { class: proseClass } },
|
|
|
|
extensions: exhibitExts,
|
|
|
|
content,
|
|
|
|
editable: false,
|
|
|
|
})
|
|
|
|
useEffect(() => void editor?.commands?.setContent(content), [editor, content])
|
|
|
|
|
|
|
|
return <EditorContent editor={editor} />
|
|
|
|
}
|
|
|
|
|
|
|
|
// backwards compatibility: we used to store content as strings
|
|
|
|
export function Content(props: { content: JSONContent | string }) {
|
|
|
|
const { content } = props
|
|
|
|
return typeof content === 'string' ? (
|
|
|
|
<Linkify text={content} />
|
|
|
|
) : (
|
|
|
|
<RichContent content={content} />
|
|
|
|
)
|
|
|
|
}
|