2022-09-13 00:06:37 +00:00
|
|
|
import { generateText, JSONContent } from '@tiptap/core'
|
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
|
|
|
// Tiptap starter extensions
|
|
|
|
import { Blockquote } from '@tiptap/extension-blockquote'
|
|
|
|
import { Bold } from '@tiptap/extension-bold'
|
|
|
|
import { BulletList } from '@tiptap/extension-bullet-list'
|
|
|
|
import { Code } from '@tiptap/extension-code'
|
|
|
|
import { CodeBlock } from '@tiptap/extension-code-block'
|
|
|
|
import { Document } from '@tiptap/extension-document'
|
|
|
|
import { HardBreak } from '@tiptap/extension-hard-break'
|
|
|
|
import { Heading } from '@tiptap/extension-heading'
|
|
|
|
import { History } from '@tiptap/extension-history'
|
|
|
|
import { HorizontalRule } from '@tiptap/extension-horizontal-rule'
|
|
|
|
import { Italic } from '@tiptap/extension-italic'
|
|
|
|
import { ListItem } from '@tiptap/extension-list-item'
|
|
|
|
import { OrderedList } from '@tiptap/extension-ordered-list'
|
|
|
|
import { Paragraph } from '@tiptap/extension-paragraph'
|
|
|
|
import { Strike } from '@tiptap/extension-strike'
|
|
|
|
import { Text } from '@tiptap/extension-text'
|
|
|
|
// other tiptap extensions
|
|
|
|
import { Image } from '@tiptap/extension-image'
|
2022-07-13 22:56:15 +00:00
|
|
|
import { Link } from '@tiptap/extension-link'
|
2022-07-24 03:37:34 +00:00
|
|
|
import { Mention } from '@tiptap/extension-mention'
|
2022-07-22 16:12:23 +00:00
|
|
|
import Iframe from './tiptap-iframe'
|
2022-08-12 03:18:01 +00:00
|
|
|
import TiptapTweet from './tiptap-tweet-type'
|
2022-09-12 23:10:32 +00:00
|
|
|
import { find } from 'linkifyjs'
|
2022-10-04 01:25:44 +00:00
|
|
|
import { cloneDeep, uniq } from 'lodash'
|
2022-09-29 21:28:04 +00:00
|
|
|
import { TiptapSpoiler } from './tiptap-spoiler'
|
2022-03-09 17:08:57 +00:00
|
|
|
|
2022-09-12 23:10:32 +00:00
|
|
|
/** get first url in text. like "notion.so " -> "http://notion.so"; "notion" -> null */
|
|
|
|
export function getUrl(text: string) {
|
|
|
|
const results = find(text, 'url')
|
|
|
|
return results.length ? results[0].href : null
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:16:00 +00:00
|
|
|
// TODO: fuzzy matching
|
|
|
|
export const wordIn = (word: string, corpus: string) =>
|
|
|
|
corpus.toLocaleLowerCase().includes(word.toLocaleLowerCase())
|
|
|
|
|
|
|
|
const checkAgainstQuery = (query: string, corpus: string) =>
|
|
|
|
query.split(' ').every((word) => wordIn(word, corpus))
|
|
|
|
|
|
|
|
export const searchInAny = (query: string, ...fields: string[]) =>
|
|
|
|
fields.some((field) => checkAgainstQuery(query, field))
|
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
/** @return user ids of all \@mentions */
|
|
|
|
export function parseMentions(data: JSONContent): string[] {
|
|
|
|
const mentions = data.content?.flatMap(parseMentions) ?? [] //dfs
|
|
|
|
if (data.type === 'mention' && data.attrs) {
|
|
|
|
mentions.push(data.attrs.id as string)
|
|
|
|
}
|
|
|
|
return uniq(mentions)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// can't just do [StarterKit, Image...] because it doesn't work with cjs imports
|
2022-10-11 01:47:02 +00:00
|
|
|
const stringParseExts = [
|
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
|
|
|
Blockquote,
|
|
|
|
Bold,
|
|
|
|
BulletList,
|
|
|
|
Code,
|
|
|
|
CodeBlock,
|
|
|
|
Document,
|
|
|
|
HardBreak,
|
|
|
|
Heading,
|
|
|
|
History,
|
|
|
|
HorizontalRule,
|
|
|
|
Italic,
|
|
|
|
ListItem,
|
|
|
|
OrderedList,
|
|
|
|
Paragraph,
|
|
|
|
Strike,
|
|
|
|
Text,
|
2022-09-13 00:06:37 +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
|
|
|
Image,
|
2022-07-13 22:56:15 +00:00
|
|
|
Link,
|
2022-10-11 01:47:02 +00:00
|
|
|
Mention, // user @mention
|
|
|
|
Mention.extend({ name: 'contract-mention' }), // market %mention
|
2022-07-22 16:12:23 +00:00
|
|
|
Iframe,
|
2022-08-12 03:18:01 +00:00
|
|
|
TiptapTweet,
|
2022-09-29 21:28:04 +00:00
|
|
|
TiptapSpoiler,
|
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 richTextToString(text?: JSONContent) {
|
2022-10-04 01:25:44 +00:00
|
|
|
if (!text) return ''
|
|
|
|
// remove spoiler tags.
|
|
|
|
const newText = cloneDeep(text)
|
|
|
|
dfs(newText, (current) => {
|
|
|
|
if (current.marks?.some((m) => m.type === TiptapSpoiler.name)) {
|
|
|
|
current.text = '[spoiler]'
|
2022-10-05 20:08:51 +00:00
|
|
|
} else if (current.type === 'image') {
|
|
|
|
current.text = '[Image]'
|
|
|
|
// This is a hack, I've no idea how to change a tiptap extenstion's schema
|
|
|
|
current.type = 'text'
|
|
|
|
} else if (current.type === 'iframe') {
|
|
|
|
const src = current.attrs?.['src'] ? current.attrs['src'] : ''
|
|
|
|
current.text = '[Iframe]' + (src ? ` url:${src}` : '')
|
|
|
|
// This is a hack, I've no idea how to change a tiptap extenstion's schema
|
|
|
|
current.type = 'text'
|
2022-10-04 01:25:44 +00:00
|
|
|
}
|
|
|
|
})
|
2022-10-11 01:47:02 +00:00
|
|
|
return generateText(newText, stringParseExts)
|
2022-10-04 01:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const dfs = (data: JSONContent, f: (current: JSONContent) => any) => {
|
|
|
|
data.content?.forEach((d) => dfs(d, f))
|
|
|
|
f(data)
|
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
|
|
|
}
|