Skip to content

Commit

Permalink
feat: return-first key down handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed May 8, 2023
1 parent 9915362 commit 99a3932
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
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.35",
"version": "0.2.0-alpha.36",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
23 changes: 16 additions & 7 deletions src/editors/extensions/figureAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,38 @@ export const FigureAudio = Node.create({
key: new PluginKey('removePastedFigureAudio'),
props: {
handleKeyDown(view, event) {
const isBackSpace = event.key === 'BackSpace'
const isEnter = event.key === 'Enter'

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

const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isCurrentPlugin = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0

// @ts-ignore
const editor = view.dom.editor as Editor

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

// @ts-ignore
const editor = view.dom.editor as Editor

// backSpace to remove if the figcaption is empty
if (event.key === 'BackSpace' && isEmptyFigcaption) {
if (isBackSpace && isEmptyFigcaption) {
editor.commands.deleteNode(pluginName)
return
}

// enter to insert a new paragraph
if (event.key === 'Enter') {
if (isEnter) {
editor
.chain()
.selectTextblockEnd()
.insertContent({ type: 'paragraph' })
.run()
return
}
},

Expand Down
12 changes: 10 additions & 2 deletions src/editors/extensions/figureEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ export const FigureEmbed = Node.create({
const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0
const isBackSpace = event.key === 'BackSpace'
const isEnter = event.key === 'Enter'

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

// @ts-ignore
const editor = view.dom.editor as Editor
Expand All @@ -340,17 +346,19 @@ export const FigureEmbed = Node.create({
}

// backSpace to remove if the figcaption is empty
if (event.key === 'BackSpace' && isEmptyFigcaption) {
if (isBackSpace && isEmptyFigcaption) {
editor.commands.deleteNode(pluginName)
return
}

// enter to insert a new paragraph
if (event.key === 'Enter') {
if (isEnter) {
editor
.chain()
.selectTextblockEnd()
.insertContent({ type: 'paragraph' })
.run()
return
}
},

Expand Down
23 changes: 16 additions & 7 deletions src/editors/extensions/figureImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,38 @@ export const FigureImage = Node.create({
key: new PluginKey('removePastedFigureImage'),
props: {
handleKeyDown(view, event) {
const isBackSpace = event.key === 'BackSpace'
const isEnter = event.key === 'Enter'

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

const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isCurrentPlugin = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0

// @ts-ignore
const editor = view.dom.editor as Editor

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

// @ts-ignore
const editor = view.dom.editor as Editor

// backSpace to remove if the figcaption is empty
if (event.key === 'BackSpace' && isEmptyFigcaption) {
if (isBackSpace && isEmptyFigcaption) {
editor.commands.deleteNode(pluginName)
return
}

// enter to insert a new paragraph
if (event.key === 'Enter') {
if (isEnter) {
editor
.chain()
.selectTextblockEnd()
.insertContent({ type: 'paragraph' })
.run()
return
}
},

Expand Down

0 comments on commit 99a3932

Please sign in to comment.