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

feat(journal): add journal editor #475

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading