Last active
April 7, 2023 06:03
-
-
Save joelrojo/ebf8ed6ded0339b780a446f4f1280edf to your computer and use it in GitHub Desktop.
Using TipTap in react-native with react-native-pell-rich-editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// extracts text string from prosemirror json object | |
import { schema } from '~app/utils/prosemirror' | |
const extractTextFromJSON = json => { | |
let text = '' | |
schema.nodeFromJSON(json).descendants(node => { | |
if (node.isText) { | |
text += `${node.text} ` | |
} | |
}) | |
return text.trim() | |
} | |
export default extractTextFromJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DOMSerializer, Node } from 'prosemirror-model' | |
import { createHTMLDocument, VHTMLDocument } from 'zeed-dom' | |
import { schema } from '~app/utils/prosemirror' | |
const getHTMLFromFragment = (doc: Node) => { | |
const document = DOMSerializer.fromSchema(schema).serializeFragment( | |
doc.content, | |
{ | |
document: createHTMLDocument() as unknown as Document | |
} | |
) as unknown as VHTMLDocument | |
return document.render() | |
} | |
const generateHtml = json => { | |
if (!json || Object.keys(json).length === 0) return '' | |
const contentNode = schema.nodeFromJSON(json) | |
const htmlString = getHTMLFromFragment(contentNode) | |
return htmlString | |
} | |
export default generateHtml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DOMParser as ProseMirrorDOMParser } from 'prosemirror-model' | |
import { parseHTML } from 'zeed-dom' | |
import { schema } from '~app/utils/prosemirror' // pulled from schema.ts file | |
const generateJson = html => { | |
const dom = parseHTML(html) as unknown as Node | |
return ProseMirrorDOMParser.fromSchema(schema).parse(dom).toJSON() | |
} | |
export default generateJson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getSchema } from '@tiptap/core' | |
// These are the extensions we're using, you would include only your specific extensions | |
import Link from '@tiptap/extension-link' | |
import StarterKit from '@tiptap/starter-kit' | |
import Typography from '@tiptap/extension-typography' | |
import Highlight from '@tiptap/extension-highlight' | |
import Image from '@tiptap/extension-image' | |
// generate schema from extensions | |
const schema = getSchema([StarterKit, Link, Typography, Highlight, Image]) | |
export default schema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment