docs: add figure button

This commit is contained in:
Philipp Kühn
2021-06-03 15:10:27 +02:00
parent 6be2c38692
commit d21d556150
2 changed files with 42 additions and 6 deletions

View File

@@ -15,7 +15,12 @@ declare module '@tiptap/core' {
/** /**
* Add a figure element * Add a figure element
*/ */
setFigure: (options: { src: string, alt?: string, title?: string }) => Command, setFigure: (options: {
src: string,
alt?: string,
title?: string,
caption?: string,
}) => Command,
} }
} }
} }
@@ -86,11 +91,24 @@ export const Figure = Node.create<FigureOptions>({
addCommands() { addCommands() {
return { return {
setFigure: options => ({ commands }) => { setFigure: ({ caption, ...attrs }) => ({ chain }) => {
return commands.insertContent({ return chain()
type: this.name, .insertContent({
attrs: options, type: this.name,
}) attrs,
content: [
{
type: 'text',
text: caption,
},
],
})
// try to set cursor within caption field
// but this fails when inserting at an empty document
// .command(({ tr, commands }) => {
// return commands.setTextSelection(tr.selection.$from.before() - 1)
// })
.run()
}, },
} }
}, },

View File

@@ -1,5 +1,8 @@
<template> <template>
<div v-if="editor"> <div v-if="editor">
<button @click="addFigure">
figure
</button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />
<h2>HTML</h2> <h2>HTML</h2>
@@ -23,6 +26,21 @@ export default {
} }
}, },
methods: {
addFigure() {
const url = window.prompt('URL')
const caption = window.prompt('caption')
if (url) {
this.editor
.chain()
.focus()
.setFigure({ src: url, caption })
.run()
}
},
},
mounted() { mounted() {
this.editor = new Editor({ this.editor = new Editor({
extensions: [ extensions: [