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: remove enter and backspace handlers #511

Merged
merged 1 commit into from
Aug 8, 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.3",
"version": "0.3.0-alpha.4",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
48 changes: 0 additions & 48 deletions src/editors/extensions/figcaptionKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Decoration, DecorationSet } from '@tiptap/pm/view'
* FigcaptionKit extension works with FigureAudio,
* FigureEmbed and FigureImage extensions to:
* - limit figcaption length
* - 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
*
Expand Down Expand Up @@ -48,52 +46,6 @@ export const makeFigcaptionEventHandlerPlugin = ({
// @see {@url https://discuss.prosemirror.net/t/prevent-nodeview-selection-on-click/3193}
return true
},
handleKeyDown(view, event) {
const isBackSpace = event.key.toLowerCase() === 'backspace'
const isEnter = event.key.toLowerCase() === 'enter'

if (!isBackSpace && !isEnter) {
return
}

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

if (!isFigureExtensions) {
return
}

// backSpace to remove if the figcaption is empty
if (isBackSpace && isEmptyFigcaption) {
// FIXME: setTimeOut to avoid repetitive deletion
setTimeout(() => {
editor.commands.deleteNode(pluginName)
})
return
}

// insert a new paragraph
if (isEnter) {
const { $from, $to } = editor.state.selection
const isTextAfter = $to.nodeAfter?.type?.name === 'text'

// skip if figcaption text is selected
// or has text after current selection
if ($from !== $to || isTextAfter) {
return
}

// FIXME: setTimeOut to avoid repetitive paragraph insertion
setTimeout(() => {
editor.commands.insertContentAt($to.pos + 1, {
type: 'paragraph',
})
})
}
},
},
})
}
Expand Down
Loading