Skip to content

Commit

Permalink
feat: handle keydown on figcaption
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed May 4, 2023
1 parent 60922e4 commit 99415f7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 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.33",
"version": "0.2.0-alpha.34",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
32 changes: 29 additions & 3 deletions src/editors/extensions/figureAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ declare module '@tiptap/core' {
}
}

const pluginName = 'figureAudio'

export const FigureAudio = Node.create({
name: 'figureAudio',
name: pluginName,
group: 'block',
content: 'text*',
draggable: true,
Expand Down Expand Up @@ -128,10 +130,9 @@ export const FigureAudio = Node.create({
},
{
type: 'paragraph',
content: [],
},
])

.focus()
.run()
},
}
Expand All @@ -142,6 +143,31 @@ export const FigureAudio = Node.create({
new Plugin({
key: new PluginKey('removePastedFigureAudio'),
props: {
handleKeyDown(view, event) {
const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0

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

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

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

transformPastedHTML(html) {
// remove
html = html.replace(
Expand Down
31 changes: 29 additions & 2 deletions src/editors/extensions/figureEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ export const normalizeEmbedURL = (url: string): NormalizeEmbedURLReturn => {
return fallbackReturn
}

const pluginName = 'figureEmbed'

export const FigureEmbed = Node.create({
name: 'figureEmbed',
name: pluginName,
group: 'block',
content: 'text*',
draggable: true,
Expand Down Expand Up @@ -312,9 +314,9 @@ export const FigureEmbed = Node.create({
},
{
type: 'paragraph',
content: [],
},
])
.focus()
.run()
},
}
Expand All @@ -325,6 +327,31 @@ export const FigureEmbed = Node.create({
new Plugin({
key: new PluginKey('removePastedFigureEmbed'),
props: {
handleKeyDown(view, event) {
const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0

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

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

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

transformPastedHTML(html) {
// remove
html = html.replace(
Expand Down
29 changes: 28 additions & 1 deletion src/editors/extensions/figureImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ declare module '@tiptap/core' {
}
}

const pluginName = 'figureImage'

export const FigureImage = Node.create({
name: 'figureImage',
group: 'block',
Expand Down Expand Up @@ -82,9 +84,9 @@ export const FigureImage = Node.create({
},
{
type: 'paragraph',
content: [],
},
])
.focus()
.run()
},
}
Expand All @@ -95,6 +97,31 @@ export const FigureImage = Node.create({
new Plugin({
key: new PluginKey('removePastedFigureImage'),
props: {
handleKeyDown(view, event) {
const anchorParent = view.state.selection.$anchor.parent
const isFigure = anchorParent.type.name === pluginName
const isEmptyFigcaption = anchorParent.content.size <= 0

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

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

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

transformPastedHTML(html) {
// remove
html = html.replace(
Expand Down

0 comments on commit 99415f7

Please sign in to comment.