Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript support #9

Open
doublejosh opened this issue Jul 6, 2020 · 4 comments
Open

TypeScript support #9

doublejosh opened this issue Jul 6, 2020 · 4 comments

Comments

@doublejosh
Copy link

Anyone already setup a declaration file?

@joelewis
Copy link
Owner

joelewis commented Jul 7, 2020

Not yet.

@doublejosh
Copy link
Author

doublejosh commented Jul 10, 2020

Workarounds...

/src/@types/prosemirror-mentions/index.d.ts

/// <reference types="node" />

declare module 'prosemirror-mentions' {
  import { getMentionsPlugin, addMentionNodes, addTagNodes, tagNode, mentionNode } from 'prosemirror-mentions'

  export const getMentionsPlugin
  export const addMentionNodes
  export const addTagNodes
  export const tagNode
  export const mentionNode
}

/tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "noImplicitAny": true,
    "strictNullChecks": true
  },
  "include": ["src/**/*"],
}

/src/components/MyEditorComponent/plugins/mentions.tsx

import React from 'react'
import ReactDOMServer from 'react-dom/server'
import * as prosemirrorMentions from 'prosemirror-mentions'

export type UserRecord = {
  name: string
  id: string
}

export type SuggestionsComplete = (arg0: UserRecord[]) => void

const SuggestionList: React.FC<{ items: UserRecord[] }> = ({ items }) => {
  return (
    <div className="suggestion-item-list">
      {items && items.map(i =>
        <div className="suggestion-item">{i.name}</div>
      )}
    </div>
  )
}

export const mentionPlugin = prosemirrorMentions.getMentionsPlugin({
  getSuggestions: (type: string, text: any, done: SuggestionsComplete) => {
    setTimeout(() => {
      if (type === 'mention') {
        done([
          { name: 'John Doe', id: '101' },
          { name: 'Joe Lewis', id: '102' }
        ])
      }
    }, 0)
  },
  getSuggestionsHTML: (items: UserRecord[], type: string) => {
    if (type === 'mention') {
      return ReactDOMServer.renderToString(<SuggestionList items={items} />)
    }
  }
})

/src/components/MyEditorComponent/index.tsx

import { mentionPlugin } from './plugins/mentions'
...
EditorState.create({ doc, schema, plugins: [mentionPlugin] })

/src/components/MyEditorComponent/schema.tsx

import * as prosemirrorMentions from 'prosemirror-mentions'
... 
new Schema({ nodes: prosemirrorMentions.addMentionNodes(schema.spec.nodes) })

@doublejosh doublejosh changed the title TypeScript declarations TypeScript support Jul 10, 2020
@doublejosh
Copy link
Author

doublejosh commented Aug 10, 2020

Instead of the above workaround, I recommend looking at this TypeScript fork for now...

https://github.com/themaven-net/prosemirror-mentions

@GitHubish
Copy link

Who would be interested to rewrite with me this plugin completely in Typescript? The plugin would be more maintainable and it would allow to make it evolve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants