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

Release: v0.3.0-alpha.2 #508

Merged
merged 2 commits into from
Aug 4, 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.3.0-alpha.1",
"version": "0.3.0-alpha.2",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
50 changes: 48 additions & 2 deletions src/editors/extensions/figcaptionKit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Editor } from '@tiptap/core'
import { Node } from '@tiptap/core'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { Decoration, DecorationSet } from '@tiptap/pm/view'

/**
* FigcaptionKit extension works with FigureAudio,
Expand All @@ -9,16 +10,21 @@ import { Plugin, PluginKey } from '@tiptap/pm/state'
* - handle enter key event to insert a new paragraph
* - handle backspace key event to remove the figcaption if it's empty
* - handle click event to select the figcaption
* - customize the empty node class and placeholder
*
* @see {https://github.com/ueberdosis/tiptap/issues/629}
*/

type FigcaptionKitOptions = {
maxCaptionLength?: number
emptyNodeClass?: string
placeholder?: string
}

const pluginName = 'figcaptionKit'

const supportedFigureExtensions = ['figureAudio', 'figureEmbed', 'figureImage']

export const makeFigcaptionEventHandlerPlugin = ({
editor,
}: {
Expand Down Expand Up @@ -51,10 +57,12 @@ export const makeFigcaptionEventHandlerPlugin = ({
}

const anchorParent = view.state.selection.$anchor.parent
const isCurrentPlugin = anchorParent.type.name === pluginName
const isFigureExtensions = supportedFigureExtensions.includes(
anchorParent.type.name,
)
const isEmptyFigcaption = anchorParent.content.size <= 0

if (!isCurrentPlugin) {
if (!isFigureExtensions) {
return
}

Expand Down Expand Up @@ -96,11 +104,14 @@ export const FigcaptionKit = Node.create<FigcaptionKitOptions>({
addOptions() {
return {
maxCaptionLength: undefined,
emptyNodeClass: 'is-figure-empty',
placeholder: 'Write something …',
}
},

addProseMirrorPlugins() {
return [
/* figcaptionLimit */
new Plugin({
key: new PluginKey('figcaptionLimit'),
filterTransaction: (transaction) => {
Expand Down Expand Up @@ -132,6 +143,41 @@ export const FigcaptionKit = Node.create<FigcaptionKitOptions>({
},
}),

/* figcaptionPlaceholder */
new Plugin({
key: new PluginKey('figcaptionPlaceholder'),
props: {
decorations: ({ doc, selection }) => {
const decorations: Decoration[] = []

doc.descendants((node, pos) => {
const isFigureExtensions = supportedFigureExtensions.includes(
node.type.name,
)

if (!isFigureExtensions) return

const isEmpty = !node.isLeaf && !node.childCount
if (!isEmpty) return

// focus on the figcaption node
const isAtFigcaption = selection.$anchor.pos === pos + 1
if (isAtFigcaption) return

const decoration = Decoration.node(pos, pos + node.nodeSize, {
class: this.options.emptyNodeClass,
'data-figure-placeholder': this.options.placeholder,
})

decorations.push(decoration)
})

return DecorationSet.create(doc, decorations)
},
},
}),

/* figcaptionEventHandler */
makeFigcaptionEventHandlerPlugin({ editor: this.editor }),
]
},
Expand Down
Loading