Skip to content

Commit

Permalink
feat(journal): add journal editor
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 5, 2024
1 parent fe24334 commit b4a5621
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.4",
"version": "0.2.5-alpha.0",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
30 changes: 30 additions & 0 deletions src/editors/Journal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type EditorOptions, useEditor } from '@tiptap/react'

import {
makeJournalEditorExtensions,
type MakeJournalEditorExtensionsProps,
} from './extensions'

type UseJournalEditorProps = {
content: string
} & MakeJournalEditorExtensionsProps &
Partial<EditorOptions>

export const useJournalEditor = ({
content,
placeholder,
mentionSuggestion,
...editorProps
}: UseJournalEditorProps) => {
const { extensions, ...restProps } = editorProps
const editor = useEditor({
extensions: [
...makeJournalEditorExtensions({ placeholder, mentionSuggestion }),
...(extensions ?? []),
],
content,
...restProps,
})

return editor
}
21 changes: 21 additions & 0 deletions src/editors/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ export const makeCommentEditorExtensions = ({

return extensions
}

/**
* Journal
*/
export interface MakeJournalEditorExtensionsProps {
placeholder?: string
mentionSuggestion?: MentionSuggestion
}

export const makeJournalEditorExtensions = ({
placeholder,
mentionSuggestion,
}: MakeJournalEditorExtensionsProps) => {
const extensions = [...baseExtensions(placeholder)]

if (mentionSuggestion) {
extensions.push(Mention.configure({ suggestion: mentionSuggestion }))
}

return extensions
}
7 changes: 7 additions & 0 deletions src/transformers/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createHTMLDocument, parseHTML, type VHTMLDocument } from 'zeed-dom'
import {
makeArticleEditorExtensions,
makeCommentEditorExtensions,
makeJournalEditorExtensions,
Mention,
} from '../editors/extensions'

Expand Down Expand Up @@ -40,3 +41,9 @@ export const normalizeCommentHTML = (html: string): string => {
const normalizer = makeNormalizer([...extensions, Mention])
return normalizer(html)
}

export const normalizeJournalHTML = (html: string): string => {
const extensions = makeJournalEditorExtensions({})
const normalizer = makeNormalizer([...extensions, Mention])
return normalizer(html)
}

0 comments on commit b4a5621

Please sign in to comment.