Skip to content

Commit

Permalink
feat: export useEditArticleEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 12, 2023
1 parent a63ce84 commit 14d24ba
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 62 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.0-alpha.53",
"version": "0.2.0-alpha.54",
"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/EditArticle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEditor } from '@tiptap/react'
import type { EditorOptions } from '@tiptap/react'
import {
makeEditArticleEditorExtensions,
MakeArticleEditorExtensionsProps,
} from './extensions'

type UseEditArticleEditorProps = {
content: string
} & MakeArticleEditorExtensionsProps &
Partial<EditorOptions>

export const useEditArticleEdtor = ({
content,
placeholder,
mentionSuggestion,
...editorProps
}: UseEditArticleEditorProps) => {
const { extensions, ...restProps } = editorProps
const editor = useEditor({
extensions: [
...makeEditArticleEditorExtensions({ placeholder, mentionSuggestion }),
...(extensions || []),
],
content,
...restProps,
})

return editor
}
8 changes: 5 additions & 3 deletions src/editors/extensions/figureEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ enum Provider {
CodePen = 'codepen',
}

export const normalizeEmbedURL = (url: string): NormalizeEmbedURLReturn => {
const normalizeEmbedURL = (url: string): NormalizeEmbedURLReturn => {
const fallbackReturn: NormalizeEmbedURLReturn = {
url: '',
allowfullscreen: false,
Expand Down Expand Up @@ -167,8 +167,8 @@ export const normalizeEmbedURL = (url: string): NormalizeEmbedURLReturn => {
}

return {
// url: `https://player.bilibili.com/player.html?bvid=${id}&autoplay=0`,
url: `https://player.bilibili.com/player.html?bvid=${id}`,
url: `https://player.bilibili.com/player.html?bvid=${id}&autoplay=0`,
// url: `https://player.bilibili.com/player.html?bvid=${id}`,
provider: Provider.Bilibili,
allowfullscreen: true,
sandbox: [],
Expand Down Expand Up @@ -304,6 +304,8 @@ export const FigureEmbed = Node.create({
...(isCode ? [`embed-code`] : []),
].join(' ')

console.log({ url })

return [
'figure',
{ class: className, ...(provider ? { 'data-provider': provider } : {}) },
Expand Down
121 changes: 67 additions & 54 deletions src/editors/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,59 @@ import { Link } from './link'
import { Mention, MentionSuggestion } from './mention'
import { Bold } from './bold'
import { HorizontalRule } from './horizontalRule'
import { ReadOnlyFigureImage } from './readOnlyFigureImage'
import { ReadOnlyFigureAudio } from './readOnlyFigureAudio'
import { ReadOnlyFigureEmbed } from './readOnlyFigureEmbed'

export * from './figureAudio'
export * from './figureEmbed'
export * from './figureImage'
export * from './readOnlyFigureAudio'
export * from './readOnlyFigureEmbed'
export * from './readOnlyFigureImage'
export * from './link'
export * from './horizontalRule'
export * from './mention'
export * from './bold'

const baseExtensions = (placeholder?: string) => [
Document,
History,
Placeholder.configure({
placeholder,
}),
// Basic Formats
Text,
Paragraph,
Bold,
Strike,
Code,
CodeBlock,
Blockquote,
HardBreak.configure({
HTMLAttributes: {
class: 'smart',
},
}),
HorizontalRule,
OrderedList,
ListItem,
BulletList,
// Custom Formats
Link,
]

const baseArticleExtensions = (placeholder?: string) => [
...baseExtensions(placeholder),
Gapcursor,
Heading.configure({
levels: [2, 3],
}),
]

/**
* Article
*/
export type MakeArticleEditorExtensionsProps = {
placeholder?: string
mentionSuggestion?: MentionSuggestion
Expand All @@ -40,34 +84,7 @@ export const makeArticleEditorExtensions = ({
mentionSuggestion,
}: MakeArticleEditorExtensionsProps) => {
const extensions = [
Document,
History,
Gapcursor,
Placeholder.configure({
placeholder,
}),
// Basic Formats
Text,
Paragraph,
Heading.configure({
levels: [2, 3],
}),
Bold,
Strike,
Code,
CodeBlock,
Blockquote,
HardBreak.configure({
HTMLAttributes: {
class: 'smart',
},
}),
HorizontalRule,
OrderedList,
ListItem,
BulletList,
// Custom Formats
Link,
...baseArticleExtensions(placeholder),
FigureImage,
FigureAudio,
FigureEmbed,
Expand All @@ -80,6 +97,27 @@ export const makeArticleEditorExtensions = ({
return extensions
}

export const makeEditArticleEditorExtensions = ({
placeholder,
mentionSuggestion,
}: MakeArticleEditorExtensionsProps) => {
const extensions = [
...baseArticleExtensions(placeholder),
ReadOnlyFigureImage,
ReadOnlyFigureAudio,
ReadOnlyFigureEmbed,
]

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

return extensions
}

/**
* Comment
*/
export type MakeCommentEditorExtensionsProps = {
placeholder?: string
mentionSuggestion?: MentionSuggestion
Expand All @@ -89,32 +127,7 @@ export const makeCommentEditorExtensions = ({
placeholder,
mentionSuggestion,
}: MakeCommentEditorExtensionsProps) => {
const extensions = [
Document,
History,
Placeholder.configure({
placeholder,
}),
// Basic Formats
Text,
Paragraph,
Bold,
Strike,
Code,
CodeBlock,
Blockquote,
HardBreak.configure({
HTMLAttributes: {
class: 'smart',
},
}),
HorizontalRule,
ListItem,
OrderedList,
BulletList,
// Custom Formats
Link,
]
const extensions = [...baseExtensions(placeholder)]

if (mentionSuggestion) {
extensions.push(Mention.configure({ suggestion: mentionSuggestion }))
Expand Down
91 changes: 91 additions & 0 deletions src/editors/extensions/readOnlyFigureAudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Node } from '@tiptap/core'

/**
* ReadyOnlyFigureAudio extension is similar to FigureAudio extension,
* but it is read-only for article revision.
*/

const pluginName = 'readOnlyFigureAudio'

export const ReadOnlyFigureAudio = Node.create({
name: pluginName,
group: 'block',
content: 'text*',
draggable: true,
isolating: true,

// read-only
atom: true,

// disallows all marks for figcaption
marks: '',

addAttributes() {
return {
src: {
default: null,
parseHTML: (element) =>
element.querySelector('source')?.getAttribute('src'),
},
title: {
default: '',
parseHTML: (element) => element.querySelector('.title')?.textContent,
},
}
},

parseHTML() {
return [
{
tag: 'figure[class="audio"]',
contentElement: 'figcaption',
},
]
},

renderHTML({ HTMLAttributes }) {
return [
'figure',
{ class: 'audio' },
[
'audio',
{
controls: true,
// for backward compatibility
// can be removed when fully switch to new editor
'data-file-name': HTMLAttributes.title,
},
[
'source',
{
src: HTMLAttributes.src,
type: 'audio/mp3',
draggable: false,
contenteditable: false,
},
],
],
[
'div',
{ class: 'player' },
[
'header',
[
'div',
{ class: 'meta' },
['h4', { class: 'title' }, HTMLAttributes.title],
[
'div',
{ class: 'time' },
['span', { class: 'current', 'data-time': '00:00' }],
['span', { class: 'duration', 'data-time': '--:--' }],
],
],
['span', { class: 'play' }],
],
['footer', ['div', { class: 'progress-bar' }, ['span', {}]]],
],
['figcaption', 0],
]
},
})
Loading

0 comments on commit 14d24ba

Please sign in to comment.