2022-03-09 17:08:57 +00:00
|
|
|
import { MAX_TAG_LENGTH } from '../contract'
|
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 { generateText, JSONContent } from '@tiptap/core'
|
|
|
|
// 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-08-04 22:35:55 +00:00
|
|
|
import { uniq } from 'lodash'
|
2022-03-09 17:08:57 +00:00
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export function parseTags(text: string) {
|
|
|
|
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
|
|
|
|
const matches = (text.match(regex) || []).map((match) =>
|
2022-03-09 17:08:57 +00:00
|
|
|
match.trim().substring(1).substring(0, MAX_TAG_LENGTH)
|
2022-01-21 23:21:46 +00:00
|
|
|
)
|
2022-01-31 03:25:50 +00:00
|
|
|
const tagSet = new Set()
|
2022-01-21 23:21:46 +00:00
|
|
|
const uniqueTags: string[] = []
|
2022-01-31 03:25:50 +00:00
|
|
|
// Keep casing of last tag.
|
|
|
|
matches.reverse()
|
|
|
|
for (const tag of matches) {
|
|
|
|
const lowercase = tag.toLowerCase()
|
|
|
|
if (!tagSet.has(lowercase)) {
|
|
|
|
tagSet.add(lowercase)
|
|
|
|
uniqueTags.push(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uniqueTags.reverse()
|
2022-01-21 23:21:46 +00:00
|
|
|
return uniqueTags
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseWordsAsTags(text: string) {
|
2022-01-31 03:25:50 +00:00
|
|
|
const taggedText = text
|
|
|
|
.split(/\s+/)
|
2022-02-01 17:02:30 +00:00
|
|
|
.map((tag) => (tag.startsWith('#') ? tag : `#${tag}`))
|
2022-01-31 03:25:50 +00:00
|
|
|
.join(' ')
|
|
|
|
return parseTags(taggedText)
|
2022-01-21 23:21:46 +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
|
|
|
|
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
|
|
|
|
export const exhibitExts = [
|
|
|
|
Blockquote,
|
|
|
|
Bold,
|
|
|
|
BulletList,
|
|
|
|
Code,
|
|
|
|
CodeBlock,
|
|
|
|
Document,
|
|
|
|
HardBreak,
|
|
|
|
Heading,
|
|
|
|
History,
|
|
|
|
HorizontalRule,
|
|
|
|
Italic,
|
|
|
|
ListItem,
|
|
|
|
OrderedList,
|
|
|
|
Paragraph,
|
|
|
|
Strike,
|
|
|
|
Text,
|
|
|
|
|
|
|
|
Image,
|
2022-07-13 22:56:15 +00:00
|
|
|
Link,
|
2022-07-24 03:37:34 +00:00
|
|
|
Mention,
|
2022-07-22 16:12:23 +00:00
|
|
|
Iframe,
|
2022-08-12 03:18:01 +00:00
|
|
|
TiptapTweet,
|
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) {
|
|
|
|
return !text ? '' : generateText(text, exhibitExts)
|
|
|
|
}
|